grains.t (3054B) - 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 Grains qw<grains_on_square total_grains>; 10 11 my @test_cases = do { local $/; @{ JSON->decode(<DATA>) }; }; 12 plan 12; 13 14 imported_ok qw<grains_on_square total_grains> or bail_out; 15 16 for my $case (@test_cases) { 17 if ( ref $case->{expected} ) { 18 like dies( sub { grains_on_square( $case->{input}{square} ) } ), 19 qr/$case->{expected}{error}/, 20 $case->{description}; 21 } 22 elsif ( $case->{property} eq 'square' ) { 23 is( grains_on_square( $case->{input}{square} ), 24 $case->{expected}, 'square no. ' . $case->{description} ); 25 } 26 elsif ( $case->{property} eq 'total' ) { 27 is( total_grains(), $case->{expected}, $case->{description} ); 28 } 29 } 30 31 __DATA__ 32 [ 33 { 34 "description": "returns the number of grains on the square: grains on square 1", 35 "expected": 1, 36 "input": { 37 "square": 1 38 }, 39 "property": "square" 40 }, 41 { 42 "description": "returns the number of grains on the square: grains on square 2", 43 "expected": 2, 44 "input": { 45 "square": 2 46 }, 47 "property": "square" 48 }, 49 { 50 "description": "returns the number of grains on the square: grains on square 3", 51 "expected": 4, 52 "input": { 53 "square": 3 54 }, 55 "property": "square" 56 }, 57 { 58 "description": "returns the number of grains on the square: grains on square 4", 59 "expected": 8, 60 "input": { 61 "square": 4 62 }, 63 "property": "square" 64 }, 65 { 66 "description": "returns the number of grains on the square: grains on square 16", 67 "expected": 32768, 68 "input": { 69 "square": 16 70 }, 71 "property": "square" 72 }, 73 { 74 "description": "returns the number of grains on the square: grains on square 32", 75 "expected": 2147483648, 76 "input": { 77 "square": 32 78 }, 79 "property": "square" 80 }, 81 { 82 "description": "returns the number of grains on the square: grains on square 64", 83 "expected": 9223372036854775808, 84 "input": { 85 "square": 64 86 }, 87 "property": "square" 88 }, 89 { 90 "description": "returns the number of grains on the square: square 0 raises an exception", 91 "expected": { 92 "error": "square must be between 1 and 64" 93 }, 94 "input": { 95 "square": 0 96 }, 97 "property": "square" 98 }, 99 { 100 "description": "returns the number of grains on the square: negative square raises an exception", 101 "expected": { 102 "error": "square must be between 1 and 64" 103 }, 104 "input": { 105 "square": -1 106 }, 107 "property": "square" 108 }, 109 { 110 "description": "returns the number of grains on the square: square greater than 64 raises an exception", 111 "expected": { 112 "error": "square must be between 1 and 64" 113 }, 114 "input": { 115 "square": 65 116 }, 117 "property": "square" 118 }, 119 { 120 "description": "returns the total number of grains on the board", 121 "expected": 18446744073709551615, 122 "input": {}, 123 "property": "total" 124 } 125 ]