exercism-perl5

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

binary-search-tree.t (3937B) - 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 BinarySearchTree qw<tree treeSort>;
     10 
     11 my @test_cases = do { local $/; @{ JSON->decode(<DATA>) }; };
     12 
     13 imported_ok qw<tree treeSort> or bail_out;
     14 
     15 subtest data => sub {
     16   for my $case ( grep { $_->{property} eq 'data' } @test_cases ) {
     17     is( tree( $case->{input}{treeData} ),
     18       $case->{expected}, $case->{description}, );
     19   }
     20 };
     21 
     22 subtest sorting => sub {
     23   for my $case ( grep { $_->{property} eq 'sortedData' } @test_cases )
     24   {
     25     is( treeSort( $case->{input}{treeData} ),
     26       $case->{expected}, $case->{description}, );
     27   }
     28 };
     29 
     30 done_testing;
     31 
     32 __DATA__
     33 [
     34   {
     35     "description": "data is retained",
     36     "expected": {
     37       "data": "4",
     38       "left": null,
     39       "right": null
     40     },
     41     "input": {
     42       "treeData": [
     43         "4"
     44       ]
     45     },
     46     "property": "data"
     47   },
     48   {
     49     "description": "insert data at proper node: smaller number at left node",
     50     "expected": {
     51       "data": "4",
     52       "left": {
     53         "data": "2",
     54         "left": null,
     55         "right": null
     56       },
     57       "right": null
     58     },
     59     "input": {
     60       "treeData": [
     61         "4",
     62         "2"
     63       ]
     64     },
     65     "property": "data"
     66   },
     67   {
     68     "description": "insert data at proper node: same number at left node",
     69     "expected": {
     70       "data": "4",
     71       "left": {
     72         "data": "4",
     73         "left": null,
     74         "right": null
     75       },
     76       "right": null
     77     },
     78     "input": {
     79       "treeData": [
     80         "4",
     81         "4"
     82       ]
     83     },
     84     "property": "data"
     85   },
     86   {
     87     "description": "insert data at proper node: greater number at right node",
     88     "expected": {
     89       "data": "4",
     90       "left": null,
     91       "right": {
     92         "data": "5",
     93         "left": null,
     94         "right": null
     95       }
     96     },
     97     "input": {
     98       "treeData": [
     99         "4",
    100         "5"
    101       ]
    102     },
    103     "property": "data"
    104   },
    105   {
    106     "description": "can create complex tree",
    107     "expected": {
    108       "data": "4",
    109       "left": {
    110         "data": "2",
    111         "left": {
    112           "data": "1",
    113           "left": null,
    114           "right": null
    115         },
    116         "right": {
    117           "data": "3",
    118           "left": null,
    119           "right": null
    120         }
    121       },
    122       "right": {
    123         "data": "6",
    124         "left": {
    125           "data": "5",
    126           "left": null,
    127           "right": null
    128         },
    129         "right": {
    130           "data": "7",
    131           "left": null,
    132           "right": null
    133         }
    134       }
    135     },
    136     "input": {
    137       "treeData": [
    138         "4",
    139         "2",
    140         "6",
    141         "1",
    142         "3",
    143         "5",
    144         "7"
    145       ]
    146     },
    147     "property": "data"
    148   },
    149   {
    150     "description": "can sort data: can sort single number",
    151     "expected": [
    152       "2"
    153     ],
    154     "input": {
    155       "treeData": [
    156         "2"
    157       ]
    158     },
    159     "property": "sortedData"
    160   },
    161   {
    162     "description": "can sort data: can sort if second number is smaller than first",
    163     "expected": [
    164       "1",
    165       "2"
    166     ],
    167     "input": {
    168       "treeData": [
    169         "2",
    170         "1"
    171       ]
    172     },
    173     "property": "sortedData"
    174   },
    175   {
    176     "description": "can sort data: can sort if second number is same as first",
    177     "expected": [
    178       "2",
    179       "2"
    180     ],
    181     "input": {
    182       "treeData": [
    183         "2",
    184         "2"
    185       ]
    186     },
    187     "property": "sortedData"
    188   },
    189   {
    190     "description": "can sort data: can sort if second number is greater than first",
    191     "expected": [
    192       "2",
    193       "3"
    194     ],
    195     "input": {
    196       "treeData": [
    197         "2",
    198         "3"
    199       ]
    200     },
    201     "property": "sortedData"
    202   },
    203   {
    204     "description": "can sort data: can sort complex tree",
    205     "expected": [
    206       "1",
    207       "2",
    208       "3",
    209       "5",
    210       "6",
    211       "7"
    212     ],
    213     "input": {
    214       "treeData": [
    215         "2",
    216         "1",
    217         "3",
    218         "6",
    219         "7",
    220         "5"
    221       ]
    222     },
    223     "property": "sortedData"
    224   }
    225 ]