exercism-perl5

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

difference-of-squares.t (2574B) - 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 DifferenceOfSquares
     10   qw<square_of_sum sum_of_squares difference_of_squares>;
     11 
     12 my @test_cases = do { local $/; @{ JSON->decode(<DATA>) }; };
     13 
     14 imported_ok qw<square_of_sum sum_of_squares difference_of_squares>
     15   or bail_out;
     16 
     17 for my $case (@test_cases) {
     18   my $func;
     19   if ( $case->{property} eq 'squareOfSum' ) {
     20     $func = \&square_of_sum;
     21   }
     22   elsif ( $case->{property} eq 'sumOfSquares' ) {
     23     $func = \&sum_of_squares;
     24   }
     25   elsif ( $case->{property} eq 'differenceOfSquares' ) {
     26     $func = \&difference_of_squares;
     27   }
     28 
     29   is( $func->( $case->{input}{number} ),
     30     $case->{expected}, $case->{description}, );
     31 }
     32 
     33 done_testing;
     34 
     35 __DATA__
     36 [
     37   {
     38     "description": "Square the sum of the numbers up to the given number: square of sum 1",
     39     "expected": 1,
     40     "input": {
     41       "number": 1
     42     },
     43     "property": "squareOfSum"
     44   },
     45   {
     46     "description": "Square the sum of the numbers up to the given number: square of sum 5",
     47     "expected": 225,
     48     "input": {
     49       "number": 5
     50     },
     51     "property": "squareOfSum"
     52   },
     53   {
     54     "description": "Square the sum of the numbers up to the given number: square of sum 100",
     55     "expected": 25502500,
     56     "input": {
     57       "number": 100
     58     },
     59     "property": "squareOfSum"
     60   },
     61   {
     62     "description": "Sum the squares of the numbers up to the given number: sum of squares 1",
     63     "expected": 1,
     64     "input": {
     65       "number": 1
     66     },
     67     "property": "sumOfSquares"
     68   },
     69   {
     70     "description": "Sum the squares of the numbers up to the given number: sum of squares 5",
     71     "expected": 55,
     72     "input": {
     73       "number": 5
     74     },
     75     "property": "sumOfSquares"
     76   },
     77   {
     78     "description": "Sum the squares of the numbers up to the given number: sum of squares 100",
     79     "expected": 338350,
     80     "input": {
     81       "number": 100
     82     },
     83     "property": "sumOfSquares"
     84   },
     85   {
     86     "description": "Subtract sum of squares from square of sums: difference of squares 1",
     87     "expected": 0,
     88     "input": {
     89       "number": 1
     90     },
     91     "property": "differenceOfSquares"
     92   },
     93   {
     94     "description": "Subtract sum of squares from square of sums: difference of squares 5",
     95     "expected": 170,
     96     "input": {
     97       "number": 5
     98     },
     99     "property": "differenceOfSquares"
    100   },
    101   {
    102     "description": "Subtract sum of squares from square of sums: difference of squares 100",
    103     "expected": 25164150,
    104     "input": {
    105       "number": 100
    106     },
    107     "property": "differenceOfSquares"
    108   }
    109 ]