Ticket #671: array.t.patch
| File array.t.patch, 17.5 KB (added by bobw, 4 years ago) |
|---|
-
t/pmc/array.t
1 #! p erl1 #! parrot 2 2 # Copyright (C) 2001-2007, Parrot Foundation. 3 3 # $Id$ 4 4 5 use strict;6 use warnings;7 use lib qw( . lib ../lib ../../lib );8 use Test::More;9 use Parrot::Test tests => 16;10 11 5 =head1 NAME 12 6 13 7 t/pmc/array.t - Array PMC … … 23 17 24 18 =cut 25 19 26 pasm_output_is( <<'CODE', <<'OUTPUT', "Setting array size" ); 27 new P0, ['Array'] 20 .namespace [] 28 21 29 set I0,P0 30 eq I0,0,OK_1 31 print "not " 32 OK_1: print "ok 1\n" 22 .include "except_types.pasm" 23 .include 'fp_equality.pasm' 33 24 34 set P0,1 35 set I0,P0 36 eq I0,1,OK_2 37 print "not " 38 OK_2: print "ok 2\n" 25 .sub main :main 26 .include 'test_more.pir' 39 27 40 set P0,2 41 set I0,P0 42 eq I0,2,OK_3 43 print "not " 44 OK_3: print "ok 3\n" 28 # plan(64) 29 plan(59) 45 30 46 new P1, ['Integer'] 47 set P1, 3 48 set P0,P1 49 set I0,P0 50 eq I0,3,OK_4 51 print "not " 52 OK_4: print "ok 4\n" 31 setting_array_size() # 4 tests 32 setting_first_element() # 3 tests 33 setting_second_element() # 3 tests 34 setting_out_of_bounds_element() # 2 tests 35 defined_sub() # 8 tests 36 exists_sub() # 6 tests 37 set_via_pmc_keys_access_via_ints() # 4 tests 38 set_via_ints_access_via_pmc_keys() # 4 tests 39 multikeyed_access_i_arg() # 5 tests 40 multikeyed_access_p_arg() # 5 tests 41 delete_sub() # 3 tests 42 check_whether_interface_is_done() # 3 tests 43 get_bool() # 4 tests 44 # freeze_thaw() # 5 tests 45 array_comparison() # 5 tests 46 .end 53 47 54 48 55 end 56 CODE 57 ok 1 58 ok 2 59 ok 3 60 ok 4 61 OUTPUT 49 .sub setting_array_size 50 .local pmc foo, ifoo 51 .local int size 62 52 63 pasm_output_is( <<'CODE', <<'OUTPUT', "Setting first element" ); 64 new P0, ['Array']65 set P0, 153 foo = new ['Array'] 54 size = foo 55 is(size, 0, 'array size initialised to 0 correctly') 66 56 67 set P0[0],-7 68 set I0,P0[0] 69 eq I0,-7,OK_1 70 print "not " 71 OK_1: print "ok 1\n" 57 foo = 1 58 size = foo 59 is(size, 1, 'array size set to 1 correctly') 72 60 73 set P0[0],3.7 74 set N0,P0[0] 75 eq N0,3.7,OK_2 76 print "not " 77 OK_2: print "ok 2\n" 61 foo = 2 62 size = foo 63 is(size, 2, 'array size set to 2 correctly') 78 64 79 set P0[0],"Buckaroo" 80 set S0,P0[0] 81 eq S0,"Buckaroo",OK_3 82 print "not " 83 OK_3: print "ok 3\n" 65 ifoo = new ['Integer'] 66 ifoo = 3 67 foo = ifoo 68 size = foo 69 is(size, 3, 'array size set to 3 correctly') 70 .end 84 71 85 end 86 CODE 87 ok 1 88 ok 2 89 ok 3 90 OUTPUT 72 .sub setting_first_element 73 .local pmc foo 74 .local int ival 75 .local num nval 76 .local string sval 91 77 92 pasm_output_is( <<'CODE', <<'OUTPUT', "Setting second element" ); 93 new P0, ['Array'] 94 set P0, 2 78 foo = new ['Array'] 79 foo = 1 95 80 96 set P0[1], -7 97 set I0, P0[1] 98 eq I0,-7,OK_1 99 print "not " 100 OK_1: print "ok 1\n" 81 foo[0] = -7 82 ival = foo[0] 83 is(ival, -7, 'array first element set to integer correctly') 101 84 102 set P0[1], 3.7 103 set N0, P0[1] 104 eq N0,3.7,OK_2 105 print "not " 106 OK_2: print "ok 2\n" 85 foo[0] = 3.7 86 nval = foo[0] 87 is(nval, 3.7, 'array first element set to number correctly') 107 88 108 set P0[1],"Buckaroo" 109 set S0, P0[1] 110 eq S0,"Buckaroo",OK_3 111 print "not " 112 OK_3: print "ok 3\n" 89 foo[0] = "Buckaroo" 90 sval = foo[0] 91 is(sval, "Buckaroo", 'array first element set to string correctly') 92 .end 113 93 114 end 115 CODE 116 ok 1 117 ok 2 118 ok 3 119 OUTPUT 94 .sub setting_second_element 95 .local pmc foo 96 .local int ival 97 .local num nval 98 .local string sval 120 99 121 pasm_error_output_like( <<'CODE', <<'OUTPUT', "Setting out-of-bounds elements" ); 122 new P0, ['Array'] 123 set P0, 1 100 foo = new ['Array'] 101 foo = 2 124 102 125 set P0[1], -7 103 foo[1] = -7 104 ival = foo[1] 105 is(ival, -7, 'array second element set to integer correctly') 126 106 127 end 128 CODE 129 /^Array index out of bounds! 130 current instr/ 131 OUTPUT 107 foo[1] = 3.7 108 nval = foo[1] 109 is(nval, 3.7, 'array second element set to number correctly') 132 110 133 pasm_error_output_like( <<'CODE', <<'OUTPUT', "Getting out-of-bounds elements" ); 134 new P0, ['Array'] 135 set P0, 1 111 foo[1] = "Buckaroo" 112 sval = foo[1] 113 is(sval, "Buckaroo", 'array second element set to string correctly') 114 .end 136 115 137 set I0, P0[1] 138 end 139 CODE 140 /^Array index out of bounds! 141 current instr/ 142 OUTPUT 116 .sub setting_out_of_bounds_element 117 .local pmc foo, eh 118 .local int ifoo 143 119 144 pasm_output_is( <<'CODE', <<OUTPUT, "defined" ); 145 new P0, ['Array'] 146 defined I0, P0 147 print I0 148 print "\n" 149 defined I0, P1 150 print I0 151 print "\n" 152 set P0, 5 153 set P0[0], 1 154 defined I0, P0[0] 155 print I0 156 print "\n" 157 defined I0, P0[1] 158 print I0 159 print "\n" 160 defined I0, P0[100] 161 print I0 162 print "\n" 163 new P1, ['Undef'] 164 set P0[2], P1 165 defined I0, P0[2] 166 print I0 167 print "\n" 168 new P2, ['Key'] 169 set P2, 3 170 set P0[3], 4 171 defined I0, P0[P2] 172 print I0 173 print "\n" 174 set P2, 4 175 defined I0, P0[P2] 176 print I0 177 print "\n" 178 end 179 CODE 180 1 181 0 182 1 183 0 184 0 185 0 186 1 187 0 188 OUTPUT 120 foo = new ['Array'] 121 foo = 1 189 122 190 pasm_output_is( <<'CODE', <<OUTPUT, "exists" ); 191 new P0, ['Array'] 192 set P0, 5 193 set P0[0], 1 194 exists I0, P0[0] 195 print I0 196 print "\n" 197 exists I0, P0[1] 198 print I0 199 print "\n" 200 exists I0, P0[100] 201 print I0 202 print "\n" 203 new P1, ['Undef'] 204 set P0[2], P1 205 exists I0, P0[2] 206 print I0 207 print "\n" 208 new P2, ['Key'] 209 set P2, 3 210 set P0[3], 4 211 exists I0, P0[P2] 212 print I0 213 print "\n" 214 set P2, 4 215 exists I0, P0[P2] 216 print I0 217 print "\n" 218 end 219 CODE 220 1 221 0 222 0 223 1 224 1 225 0 226 OUTPUT 123 eh = new ['ExceptionHandler'] 124 eh.'handle_types'(.EXCEPTION_OUT_OF_BOUNDS) 125 set_addr eh, eh_label 227 126 228 pasm_output_is( <<"CODE", <<'OUTPUT', "Set via PMC keys, access via INTs" ); 229 .include 'fp_equality.pasm' 230 new P0, ['Array'] 231 set P0, 4 232 new P1, ['Key'] 127 foo[0] = 42 128 ifoo = foo[0] 129 is(ifoo, 42, 'array in-bounds element set correctly') 233 130 234 set P1, 0 235 set P0[P1], 25 131 push_eh eh 132 foo[1] = -7 133 pop_eh 236 134 237 set P1, 1238 set P0[P1], 2.5135 ok(0, 'setting_out_of_bounds_element') 136 goto end 239 137 240 set P1, 2 241 set P0[P1], "Squeek" 138 eh_label: 139 .local string message 140 .get_results($P0) 141 message = $P0['message'] 142 is(message, "Array index out of bounds!", 'setting_out_of_bounds_element') 242 143 243 set P1, 3 244 new P2, ['Hash'] 245 set P2["a"], "apple" 246 set P0[P1], P2 144 end: 145 .end 247 146 248 set I0, P0[0] 249 eq I0, 25, OK1 250 print "not " 251 OK1: print "ok 1\\n" 147 .sub defined_sub 148 .local pmc foo, bar, baz 149 .local int ival 150 151 foo = new ['Array'] 152 defined ival, foo 153 is(ival, 1, 'newed array is defined') 252 154 253 set N0, P0[1] 254 .fp_eq_pasm(N0, 2.5, OK2) 255 print "not " 256 OK2: print "ok 2\\n" 155 defined ival, bar 156 is(ival, 0, 'unreferenced pmc is undefined') 257 157 258 set S0, P0[2]259 eq S0, "Squeek", OK3260 print "not "261 OK3: print "ok 3\\n" 158 foo = 5 159 foo[0] = 1 160 defined ival, foo[0] 161 is(ival, 1, 'assigned array element is defined') 262 162 263 set P3, P0[3] 264 set S1, P3["a"] 265 eq S1, "apple", OK4 266 print "not " 267 OK4: print "ok 4\\n" 163 defined ival, foo[1] 164 is(ival, 0, 'unassigned in-bounds array element is undefined') 268 165 269 end 270 CODE 271 ok 1 272 ok 2 273 ok 3 274 ok 4 275 OUTPUT 166 defined ival, foo[100] 167 is(ival, 0, 'unassigned out-of-bounds array element is undefined') 276 168 277 pasm_output_is( <<"CODE", <<'OUTPUT', "Set via INTs, access via PMC Keys" ); 278 .include 'fp_equality.pasm'279 new P0, ['Array']280 set P0, 1024169 bar = new ['Undef'] 170 foo[2] = bar 171 defined ival, foo[2] 172 is(ival, 0, 'assigned undef pmc is undefined') 281 173 282 set P0[25], 125 283 set P0[128], -9.9 284 set P0[513], "qwertyuiopasdfghjklzxcvbnm" 285 new P1, ['Integer'] 286 set P1, 123456 287 set P0[1023], P1 174 baz = new ['Key'] 175 baz = 3 176 foo[3] = 4 177 defined ival, foo[baz] 178 is(ival, 1, 'defined keyed array element is defined') 288 179 289 new P2, ['Key'] 290 set P2, 25 291 set I0, P0[P2] 292 eq I0, 125, OK1 293 print "not " 294 OK1: print "ok 1\\n" 180 baz = 4 181 defined ival, foo[baz] 182 is(ival, 0, 'undefined keyed array element is defined') 183 .end 295 184 296 set P2, 128 297 set N0, P0[P2] 298 .fp_eq_pasm(N0, -9.9, OK2) 299 print "not " 300 OK2: print "ok 2\\n" 185 .sub exists_sub 186 .local pmc foo, bar, baz 187 .local int ival 301 188 302 set P2, 513303 set S0, P0[P2]304 eq S0, "qwertyuiopasdfghjklzxcvbnm", OK3305 print "not "306 OK3: print "ok 3\\n" 189 foo = new ['Array'] 190 foo = 5 191 foo[0] = 1 192 exists ival, foo[0] 193 is(ival, 1, 'assigned array element exists') 307 194 308 set P2, 1023 309 set P3, P0[P2] 310 set I1, P3 311 eq I1, 123456, OK4 312 print "not " 313 OK4: print "ok 4\\n" 195 exists ival, foo[1] 196 is(ival, 0, 'unassigned in-bounds array element does not exist') 314 197 315 end 316 CODE 317 ok 1 318 ok 2 319 ok 3 320 ok 4 321 OUTPUT 198 exists ival, foo[100] 199 is(ival, 0, 'unassigned out-of-bounds array element does not exist') 322 200 323 pasm_output_is( <<'CODE', <<OUT, "multikeyed access I arg" ); 324 new P0, ['Array'] 325 set P0, 1 326 new P1, ['Array'] 327 set P1, 1 328 set P0[0], P1 329 set P0[0;0], 20 330 set P2, P0[0] 331 typeof S0, P2 332 print S0 333 print "\n" 334 set I2, P0[0;0] 335 print I2 336 set I3, 0 337 set I2, P0[I3;0] 338 print I2 339 set I2, P0[0;I3] 340 print I2 341 set I2, P0[I3;I3] 342 print I2 343 print "\n" 344 end 345 CODE 346 Array 347 20202020 348 OUT 201 bar = new ['Undef'] 202 foo[2] = bar 203 exists ival, foo[2] 204 is(ival, 1, 'assigned undef array element exists') 349 205 350 pasm_output_is( <<'CODE', <<OUT, "multikeyed access P arg" ); 351 new P0, ['Array'] 352 set P0, 1 353 new P1, ['Array'] 354 set P1, 1 355 new P3, ['Integer'] 356 set P3, 20 357 set P0[0], P1 358 set P0[0;0], P3 359 set P2, P0[0] 360 typeof S0, P2 361 print S0 362 print "\n" 363 set I2, P0[0;0] 364 print I2 365 set I3, 0 366 set I2, P0[I3;0] 367 print I2 368 set I2, P0[0;I3] 369 print I2 370 set I2, P0[I3;I3] 371 print I2 372 print "\n" 373 end 374 CODE 375 Array 376 20202020 377 OUT 206 baz = new ['Key'] 207 baz = 3 208 foo[3] = 4 209 exists ival, foo[baz] 210 is(ival, 1, 'defined keyed array element exists') 378 211 379 pasm_output_is( <<'CODE', <<OUT, "delete" ); 380 new P0, ['Array'] 381 set P0, 3 382 set P0[0], 10 383 set P0[1], 20 384 set P0[2], 30 212 baz = 4 213 exists ival, foo[baz] 214 is(ival, 0, 'undefined keyed array element does not exist') 215 .end 385 216 386 delete P0[1] 387 set I0, P0 388 print I0 217 .sub set_via_pmc_keys_access_via_ints 218 .local pmc foo, bar, baz, faz 219 .local int ival 220 .local num nval 221 .local string sval, inner 222 223 foo = new ['Array'] 224 foo = 4 225 bar = new ['Key'] 389 226 390 set I0, P0[0] 391 print I0 392 set I0, P0[1] 393 print I0 394 print "\n" 395 end 396 CODE 397 21030 398 OUT 227 bar = 0 228 foo[bar] = 25 229 ival = foo[0] 230 is(ival, 25, 'integer element can be retrieved from array') 399 231 400 pir_output_is( << 'CODE', << 'OUTPUT', "check whether interface is done" ); 232 bar = 1 233 foo[bar] = 2.5 234 nval = foo[1] 235 is(nval, 2.5, 'number element can be retrieved from array') 401 236 402 .sub _main 237 bar = 2 238 foo[bar] = "Squeek" 239 sval = foo[2] 240 is(sval, "Squeek", 'string element can be retrieved from array') 241 242 bar = 3 243 baz = new ['Hash'] 244 baz["a"] = "apple" 245 foo[bar] = baz 246 247 faz = foo[3] 248 inner = faz["a"] 249 is(inner, "apple", 'inner string element can be retrieved from array') 250 .end 251 252 .sub set_via_ints_access_via_pmc_keys 253 .local pmc foo, bar, baz, faz 254 .local int ival, inner 255 .local num nval 256 .local string sval 257 258 foo = new ['Array'] 259 foo = 1024 260 261 foo[25] = 125 262 foo[128] = -9.9 263 foo[513] = "qwertyuiopasdfghjklzxcvbnm" 264 bar = new ['Integer'] 265 bar = 123456 266 foo[1023] = bar 267 268 baz = new ['Key'] 269 baz = 25 270 271 ival = foo[baz] 272 is(ival, 125, 'integer element can be retrieved from array') 273 274 baz = 128 275 nval = foo[baz] 276 is(nval, -9.9, 'number element can be retrieved from array') 277 278 baz = 513 279 sval = foo[baz] 280 is(sval, "qwertyuiopasdfghjklzxcvbnm", 'string element can be retrieved from array') 281 282 baz = 1023 283 faz = foo[baz] 284 inner = faz 285 is(inner, 123456, 'indirect integer element can be retrieved from array') 286 .end 287 288 .sub multikeyed_access_i_arg 289 .local pmc foo, bar, baz 290 .local int ival, inum 291 .local string pmctype 292 293 foo = new ['Array'] 294 foo = 1 295 bar = new ['Array'] 296 bar = 1 297 298 foo[0] = bar 299 foo[0;0] = 20 300 baz = foo[0] 301 typeof pmctype, baz 302 is(pmctype, 'Array', 'pmc is an array') 303 304 ival = foo[0;0] 305 is(ival, 20, 'access to array via [int;int] works correctly') 306 307 inum = 0 308 ival = foo[inum;0] 309 is(ival, 20, 'access to array via [var;int] works correctly') 310 311 ival = foo[0;inum] 312 is(ival, 20, 'access to array via [int;var] works correctly') 313 314 ival = foo[inum;inum] 315 is(ival, 20, 'access to array via [var;var] works correctly') 316 .end 317 318 .sub multikeyed_access_p_arg 319 .local pmc foo, bar, baz, faz 320 .local int ival, inum 321 .local string pmctype 322 323 foo = new ['Array'] 324 foo = 1 325 bar = new ['Array'] 326 bar = 1 327 328 faz = new ['Integer'] 329 faz = 20 330 foo[0] = bar 331 foo[0;0] = faz 332 baz = foo[0] 333 typeof pmctype, baz 334 is(pmctype, 'Array', 'pmc is an array') 335 336 ival = foo[0;0] 337 is(ival, 20, 'access to array via [int;int] works correctly') 338 339 inum = 0 340 ival = foo[inum;0] 341 is(ival, 20, 'access to array via [var;int] works correctly') 342 343 ival = foo[0;inum] 344 is(ival, 20, 'access to array via [int;var] works correctly') 345 346 ival = foo[inum;inum] 347 is(ival, 20, 'access to array via [var;var] works correctly') 348 .end 349 350 .sub delete_sub 351 .local pmc foo 352 .local int ival 353 354 foo = new ['Array'] 355 foo = 3 356 foo[0] = 10 357 foo[1] = 20 358 foo[2] = 30 359 360 delete foo[1] 361 ival = foo 362 is(ival, 2, 'array with deleted element correctly sized') 363 364 ival = foo[0] 365 is(ival, 10, 'array with deleted element has correct first element') 366 367 ival = foo[1] 368 is(ival, 30, 'array with deleted element has correct first element') 369 .end 370 371 .sub check_whether_interface_is_done 403 372 .local pmc pmc1 404 373 pmc1 = new ['Array'] 405 374 .local int bool1 406 375 does bool1, pmc1, "scalar" 407 print bool1408 print "\n" 376 is(bool1, 0, 'pmc array does not do scalar correctly') 377 409 378 does bool1, pmc1, "array" 410 print bool1411 print "\n" 379 is(bool1, 1, 'pmc array does array correctly') 380 412 381 does bool1, pmc1, "no_interface" 413 print bool1 414 print "\n" 415 end 382 is(bool1, 0, 'pmc array does not do no_interface correctly') 416 383 .end 417 CODE418 0419 1420 0421 OUTPUT422 384 423 424 pir_output_is( << 'CODE', << 'OUTPUT', "get_bool" ); 425 426 .sub _main 385 .sub get_bool 427 386 .local pmc p 428 387 .local int i 429 p = new ['Array']430 388 431 if p goto L1 432 print "not " 433 L1: say "true" 434 389 p = new ['Array'] 390 is(p, 0, 'newed array is not true correctly') 391 435 392 p = 4 393 is(p, 4, 'resized array is true correctly') 436 394 437 if p goto L2438 print "is not "439 L2: say "true"440 441 442 395 p[0] = 2 443 if p goto L3 444 print "not " 445 L3: say "true" 396 is(p, 4, 'assigned array is true correctly') 446 397 447 398 p = new ['Array'] 448 399 p = 0 449 if p goto L4 450 print "not " 451 L4: say "true" 452 400 is(p, 0, 'newed array set to zero length is not true correctly') 453 401 .end 454 CODE455 not true456 true457 true458 not true459 OUTPUT460 402 403 =pod 404 405 I see the following error when uncommented : 406 407 error:imcc:syntax error, unexpected LABEL, expecting $end ('TODO') 408 in file 't/pmc/array.t' line 401 409 461 410 TODO: { 462 411 local $TODO = "freeze/thaw known to be broken"; 463 pir_output_is( << 'CODE', << 'OUTPUT', "freeze/thaw" ); 464 .sub main412 413 .sub freeze_thaw 465 414 .local pmc p, it, val 466 415 .local string s 467 416 … … 478 427 479 428 it = iter p 480 429 481 iter_loop:482 unless it goto iter_end483 430 val = shift it 484 print val 485 print "\n" 486 goto iter_loop 431 is(val, '"p"', 'first thawed array element accessed correctly') 487 432 488 iter_end: 433 val = shift it 434 is(val, '-3', 'second thawed array element accessed correctly') 489 435 436 val = shift it 437 is(val, '9999', 'third thawed array element accessed correctly') 438 439 val = shift it 440 is(val, 'foo', 'fourth thawed array element accessed correctly') 441 442 val = shift it 443 is(val, '2', 'fifth thawed array element accessed correctly') 490 444 .end 491 CODE 492 p 493 -3 494 9999 495 foo 496 2 497 OUTPUT 445 498 446 } 499 447 500 pir_output_is( << 'CODE', << 'OUTPUT', "array comparison" ); 501 .sub main 448 =cut 449 450 .sub array_comparison 502 451 .local pmc a1, a2 503 452 .local int i 504 453 505 454 a1 = new ['Array'] 506 455 a2 = new ['Array'] 507 456 508 if a1 == a2 goto L1 509 print "not " 510 L1: say "equal" 457 is(a1, a2, 'two newed arrays are equal correctly') 511 458 512 459 a1 = 4 460 isnt(a1, a2, 'a sized array is not the same as a newed array correctly') 513 461 514 if a1 == a2 goto L2515 print "not "516 L2: say "equal"517 518 462 a2 = 4 463 is(a1, a2, 'two identically sized arrays are equal correctly') 519 464 520 465 a1[0] = "foo" 521 466 a2[0] = "foo" 467 is(a1, a2, 'two identically assigned arrays are equal correctly') 522 468 523 if a1 == a2 goto L3524 print "not "525 L3: say "equal"526 527 469 a1[1] = 234 528 470 a2[1] = 234 529 471 a1[3] = "bar" 530 472 a2[3] = "bar" 531 532 if a1 == a2 goto L4 533 print "not " 534 L4: say "equal" 535 473 is(a1, a2, 'two identically assigned arrays are equal correctly') 536 474 .end 537 CODE538 equal539 not equal540 equal541 equal542 OUTPUT543 475 544 1;545 546 476 # Local Variables: 547 477 # mode: cperl 548 478 # cperl-indent-level: 4
