exercism-perl5

Repository for my Perl 5 Exercism exercises
git clone git://git.samirparikh.com/exercism-perl5
Log | Files | Refs | README

hamming.t (2527B) - raw


      1 #!/usr/bin/env perl
      2 use Test2::V0;
      3 use JSON::PP;
      4 use constant JSON => JSON::PP->new;
      5 
      6 use FindBin qw<$Bin>;
      7 use lib $Bin, "$Bin/local/lib/perl5";
      8 
      9 use Hamming qw<hamming_distance>;
     10 
     11 my @test_cases = do { local $/; @{ JSON->decode(<DATA>) }; };
     12 plan 10;
     13 
     14 imported_ok qw<hamming_distance> or bail_out;
     15 
     16 for my $case (@test_cases) {
     17   if ( !ref $case->{expected} ) {
     18     is hamming_distance( @{ $case->{input} }{qw(strand1 strand2)} ),
     19       $case->{expected}, $case->{description};
     20   }
     21   else {
     22     like dies(
     23       sub {
     24         hamming_distance( @{ $case->{input} }{qw(strand1 strand2)} );
     25       }
     26       ),
     27       qr/left and right strands must be of equal length/,
     28       $case->{description};
     29   }
     30 }
     31 
     32 __DATA__
     33 [
     34   {
     35     "description": "empty strands",
     36     "expected": 0,
     37     "input": {
     38       "strand1": "",
     39       "strand2": ""
     40     },
     41     "property": "distance"
     42   },
     43   {
     44     "description": "single letter identical strands",
     45     "expected": 0,
     46     "input": {
     47       "strand1": "A",
     48       "strand2": "A"
     49     },
     50     "property": "distance"
     51   },
     52   {
     53     "description": "single letter different strands",
     54     "expected": 1,
     55     "input": {
     56       "strand1": "G",
     57       "strand2": "T"
     58     },
     59     "property": "distance"
     60   },
     61   {
     62     "description": "long identical strands",
     63     "expected": 0,
     64     "input": {
     65       "strand1": "GGACTGAAATCTG",
     66       "strand2": "GGACTGAAATCTG"
     67     },
     68     "property": "distance"
     69   },
     70   {
     71     "description": "long different strands",
     72     "expected": 9,
     73     "input": {
     74       "strand1": "GGACGGATTCTG",
     75       "strand2": "AGGACGGATTCT"
     76     },
     77     "property": "distance"
     78   },
     79   {
     80     "description": "disallow first strand longer",
     81     "expected": {
     82       "error": "left and right strands must be of equal length"
     83     },
     84     "input": {
     85       "strand1": "AATG",
     86       "strand2": "AAA"
     87     },
     88     "property": "distance"
     89   },
     90   {
     91     "description": "disallow second strand longer",
     92     "expected": {
     93       "error": "left and right strands must be of equal length"
     94     },
     95     "input": {
     96       "strand1": "ATA",
     97       "strand2": "AGTG"
     98     },
     99     "property": "distance"
    100   },
    101   {
    102     "description": "disallow left empty strand",
    103     "expected": {
    104       "error": "left strand must not be empty"
    105     },
    106     "input": {
    107       "strand1": "",
    108       "strand2": "G"
    109     },
    110     "property": "distance"
    111   },
    112   {
    113     "description": "disallow right empty strand",
    114     "expected": {
    115       "error": "right strand must not be empty"
    116     },
    117     "input": {
    118       "strand1": "G",
    119       "strand2": ""
    120     },
    121     "property": "distance"
    122   }
    123 ]