Changeset 21237

Show
Ignore:
Timestamp:
07/06/08 23:43:07 (6 months ago)
Author:
moritz
Message:

[spec] partly fudged S02-builtin_data_types/array.t. Plan is offbyone, and a
few failures are still there, but no runtime errors anymore

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • t/spec/S02-builtin_data_types/array.t

    r21231 r21237  
    1010 
    1111my @array1 = ("foo", "bar", "baz"); 
    12 #?rakudo skip "isa_ok dies" 
    13 isa_ok(@array1, 'Array'); 
     12isa_ok(@array1, Array); 
    1413 
    1514is(+@array1, 3, 'the array1 has 3 elements'); 
     
    2221is(@array1.[0], 'foo', 'got the right value at array1 index 0 using the . notation'); 
    2322 
    24 #?rakudo 999 skip "rest not properly fudged yet" 
    2523 
    2624# array with strings, numbers and undef 
    2725my @array2 = ("test", 1, undef); 
    28  
    29 isa_ok(@array2, 'Array'); 
    30  
    31 is(+@array2, 3, 'the array2 has 3 elements'); 
    32 is(@array2[0], 'test', 'got the right value at array2 index 0'); 
    33 is(@array2[1], 1,      'got the right value at array2 index 1'); 
    34 is(@array2[2], undef,  'got the right value at array2 index 2'); 
     26{ 
     27    isa_ok(@array2, Array); 
     28 
     29    is(+@array2, 3, 'the array2 has 3 elements'); 
     30    is(@array2[0], 'test', 'got the right value at array2 index 0'); 
     31    is(@array2[1], 1,      'got the right value at array2 index 1'); 
     32    is(@array2[2], undef,  'got the right value at array2 index 2'); 
     33} 
    3534 
    3635# combine 2 arrays 
    37 my @array3 = (@array1, @array2); 
    38 isa_ok(@array3, 'Array'); 
    39  
    40 is(+@array3, 6, 'the array3 has 6 elements');  
    41 is(@array3[0], 'foo', 'got the right value at array3 index 0');  
    42 is(@array3[1], 'bar', 'got the right value at array3 index 1');  
    43 is(@array3[2], 'baz', 'got the right value at array3 index 2');  
    44 is(@array3[3], 'test', 'got the right value at array3 index 3');  
    45 is(@array3[4], 1,      'got the right value at array3 index 4');  
    46 is(@array3[5], undef,  'got the right value at array3 index 5'); 
    47  
    48  
    49 # array slice 
    50 my @array4 = @array2[2, 1, 0]; 
    51 isa_ok(@array4, 'Array'); 
    52  
    53 is(+@array4, 3, 'the array4 has 3 elements'); 
    54 is(@array4[0], undef,  'got the right value at array4 index 0'); 
    55 is(@array4[1], 1,      'got the right value at array4 index 1'); 
    56 is(@array4[2], 'test', 'got the right value at array4 index 2'); 
    57  
    58 # create new array with 2 array slices 
    59 my @array5 = ( @array2[2, 1, 0], @array1[2, 1, 0] ); 
    60 isa_ok(@array5, 'Array'); 
    61  
    62 is(+@array5, 6, 'the array5 has 6 elements'); 
    63 is(@array5[0], undef,  'got the right value at array5 index 0'); 
    64 is(@array5[1], 1,      'got the right value at array5 index 1'); 
    65 is(@array5[2], 'test', 'got the right value at array5 index 2'); 
    66 is(@array5[3], 'baz',  'got the right value at array5 index 3'); 
    67 is(@array5[4], 'bar',  'got the right value at array5 index 4'); 
    68 is(@array5[5], 'foo',  'got the right value at array5 index 5'); 
    69  
    70 # create an array slice with an array (in a variable) 
    71  
    72 my @slice = (2, 0, 1); 
    73 my @array6 = @array1[@slice]; 
    74 isa_ok(@array6, 'Array'); 
    75  
    76 is(+@array6, 3, 'the array6 has 3 elements');  
    77 is(@array6[0], 'baz', 'got the right value at array6 index 0');  
    78 is(@array6[1], 'foo', 'got the right value at array6 index 1');  
    79 is(@array6[2], 'bar', 'got the right value at array6 index 2');  
    80  
    81 # create an array slice with an array constructed with () 
    82  
    83 my @array7 = @array1[(2, 1, 0)]; 
    84 isa_ok(@array7, 'Array'); 
    85  
    86 is(+@array7, 3, 'the array7 has 3 elements'); 
    87 is(@array7[0], 'baz', 'got the right value at array7 index 0'); 
    88 is(@array7[1], 'bar', 'got the right value at array7 index 1'); 
    89 is(@array7[2], 'foo', 'got the right value at array7 index 2'); 
    90  
    91 # odd slices 
    92  
    93 my $result1 = (1, 2, 3, 4)[1]; 
    94 is($result1, 2, 'got the right value from the slice'); 
    95  
    96 my $result2 = [1, 2, 3, 4][2]; 
    97 is($result2, 3, 'got the right value from the slice'); 
     36{ 
     37    my @array3 = (@array1, @array2); 
     38    isa_ok(@array3, 'Array'); 
     39 
     40    is(+@array3, 6, 'the array3 has 6 elements');  
     41    is(@array3[0], 'foo', 'got the right value at array3 index 0');  
     42    is(@array3[1], 'bar', 'got the right value at array3 index 1');  
     43    is(@array3[2], 'baz', 'got the right value at array3 index 2');  
     44    is(@array3[3], 'test', 'got the right value at array3 index 3');  
     45    is(@array3[4], 1,      'got the right value at array3 index 4');  
     46    is(@array3[5], undef,  'got the right value at array3 index 5'); 
     47} 
     48 
     49{ 
     50    # array slice 
     51    my @array4 = @array2[2, 1, 0]; 
     52    isa_ok(@array4, 'Array'); 
     53 
     54    is(+@array4, 3, 'the array4 has 3 elements'); 
     55    is(@array4[0], undef,  'got the right value at array4 index 0'); 
     56    is(@array4[1], 1,      'got the right value at array4 index 1'); 
     57    is(@array4[2], 'test', 'got the right value at array4 index 2'); 
     58} 
     59 
     60{ 
     61    # create new array with 2 array slices 
     62    my @array5 = ( @array2[2, 1, 0], @array1[2, 1, 0] ); 
     63    isa_ok(@array5, 'Array'); 
     64 
     65    is(+@array5, 6, 'the array5 has 6 elements'); 
     66    is(@array5[0], undef,  'got the right value at array5 index 0'); 
     67    is(@array5[1], 1,      'got the right value at array5 index 1'); 
     68    is(@array5[2], 'test', 'got the right value at array5 index 2'); 
     69    is(@array5[3], 'baz',  'got the right value at array5 index 3'); 
     70    is(@array5[4], 'bar',  'got the right value at array5 index 4'); 
     71    is(@array5[5], 'foo',  'got the right value at array5 index 5'); 
     72} 
     73 
     74{ 
     75    # create an array slice with an array (in a variable) 
     76 
     77    my @slice = (2, 0, 1); 
     78    my @array6 = @array1[@slice]; 
     79    isa_ok(@array6, 'Array'); 
     80 
     81    is(+@array6, 3, 'the array6 has 3 elements');  
     82    is(@array6[0], 'baz', 'got the right value at array6 index 0');  
     83    is(@array6[1], 'foo', 'got the right value at array6 index 1');  
     84    is(@array6[2], 'bar', 'got the right value at array6 index 2');  
     85} 
     86 
     87{ 
     88    # create an array slice with an array constructed with () 
     89    my @array7 = @array1[(2, 1, 0)]; 
     90    isa_ok(@array7, 'Array'); 
     91 
     92    is(+@array7, 3, 'the array7 has 3 elements'); 
     93    is(@array7[0], 'baz', 'got the right value at array7 index 0'); 
     94    is(@array7[1], 'bar', 'got the right value at array7 index 1'); 
     95    is(@array7[2], 'foo', 'got the right value at array7 index 2'); 
     96} 
     97 
     98{ 
     99    # odd slices 
     100    my $result1 = (1, 2, 3, 4)[1]; 
     101    is($result1, 2, 'got the right value from the slice'); 
     102 
     103    my $result2 = [1, 2, 3, 4][2]; 
     104    is($result2, 3, 'got the right value from the slice'); 
     105} 
    98106 
    99107# swap two elements test moved to t/op/assign.t 
    100108 
    101109# empty arrays 
    102  
    103 my @array9; 
    104 isa_ok(@array9, 'Array'); 
    105 is(+@array9, 0, "new arrays are empty"); 
    106  
    107 my @array10 = (1, 2, 3,); 
    108 is(+@array10, 3, "trailing commas make correct array");  
     110{ 
     111    my @array9; 
     112    isa_ok(@array9, 'Array'); 
     113    is(+@array9, 0, "new arrays are empty"); 
     114 
     115    my @array10 = (1, 2, 3,); 
     116    is(+@array10, 3, "trailing commas make correct array");  
     117} 
    109118 
    110119#?pugs skip "multi-dim arrays not implemented" 
     120#?rakudo skip "multi-dim arrays" 
    111121{ 
    112122# declare a multidimension array 
     
    117127    ok(eval('@array11[2,0] = 12'), "push the value to a multidimension array", :todo); 
    118128} 
     129#?rakudo 999 skip "rest not properly fudged yet" 
    119130{ 
    120131    # declare the array with data type 
     
    124135} 
    125136 
    126 my @array12 = ('a', 'b', 'c', 'e');  
    127  
    128137#?rakudo 999 skip "no whatever star yet" 
    129138#?pugs 999 skip "no whatever star yet" 
    130139{ 
     140    my @array12 = ('a', 'b', 'c', 'e');  
     141 
    131142    # indexing from the end 
    132143    is @array12[*-1],'e', "indexing from the end [*-1]"; 
    133 } 
    134  
    135 # end index range 
    136 is ~@array12[*-4 .. *-2], 'a b c', "end indices [*-4 .. *-2]"; 
    137  
    138 # end index as lvalue 
    139 @array12[*-1]   = 'd'; 
    140 is @array12[*-1], 'd', "assigns to the correct end slice index";  
    141 is ~@array12,'a b c d', "assignment to end index correctly alters the array"; 
    142  
    143 my @array13 = ('a', 'b', 'c', 'd');  
    144 # end index range as lvalue 
    145 @array13[*-4 .. *-1]   = ('d', 'c', 'b', 'a'); # ('a'..'d').reverse 
    146 is ~@array13, 'd c b a', "end range as lvalue";  
    147  
    148 #hat trick 
    149 my @array14 = ('a', 'b', 'c', 'd'); 
    150 my @b = 0..3; 
    151 ((@b[*-3,*-2,*-1,*-4] = @array14)= @array14[*-1,*-2,*-3,*-4]); 
    152  
    153 is ~@b,  
    154     'a d c b',  
    155     "hat trick: 
    156     assign to a end-indexed slice array from array   
    157     lvalue in assignment is then lvalue to end-indexed slice as rvalue";  
     144 
     145    # end index range 
     146    is ~@array12[*-4 .. *-2], 'a b c', "end indices [*-4 .. *-2]"; 
     147 
     148    # end index as lvalue 
     149    @array12[*-1]   = 'd'; 
     150    is @array12[*-1], 'd', "assigns to the correct end slice index";  
     151    is ~@array12,'a b c d', "assignment to end index correctly alters the array"; 
     152} 
     153 
     154{ 
     155    my @array13 = ('a', 'b', 'c', 'd');  
     156    # end index range as lvalue 
     157    @array13[*-4 .. *-1]   = ('d', 'c', 'b', 'a'); # ('a'..'d').reverse 
     158    is ~@array13, 'd c b a', "end range as lvalue";  
     159 
     160    #hat trick 
     161    my @array14 = ('a', 'b', 'c', 'd'); 
     162    my @b = 0..3; 
     163    ((@b[*-3,*-2,*-1,*-4] = @array14)= @array14[*-1,*-2,*-3,*-4]); 
     164 
     165    is ~@b,  
     166        'a d c b',  
     167        "hat trick: 
     168        assign to a end-indexed slice array from array   
     169        lvalue in assignment is then lvalue to end-indexed slice as rvalue";  
     170} 
    158171 
    159172# This test may seem overly simplistic, but it was actually a bug in PIL2JS, so