Ticket #1010: fba_test_and_fill.patch
File fba_test_and_fill.patch, 12.2 KB (added by flh, 12 years ago) |
---|
-
t/pmc/fixedbooleanarray.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 => 14;10 11 5 =head1 NAME 12 6 13 7 t/pmc/fixedbooleanarray.t - FixedBooleanArray PMC … … 23 17 24 18 =cut 25 19 26 pasm_output_is( <<'CODE', <<'OUTPUT', "Setting array size" ); 27 new P0, ['FixedBooleanArray'] 20 .const int TESTS = 36 28 21 29 set I0,P0 30 eq I0,0,OK_1 31 print "not " 32 OK_1: print "ok 1\n" 22 .sub 'test' :main 23 .include 'test_more.pir' 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 plan(TESTS) 39 26 40 end 41 CODE 42 ok 1 43 ok 2 44 OUTPUT 27 setting_array_size() 28 resizing_not_allowed() 29 setting_first_element() 30 setting_second_element() 31 setting_out_of_bounds() 32 getting_out_of_bounds() 33 set_pmc_access_int() 34 set_int_access_pmc() 35 interface() 36 truth() 37 pmc_keys_and_values() 38 freeze_thaw() 39 'clone'() 40 get_iter() 41 fill() 42 .end 45 43 46 pasm_error_output_like( <<'CODE', <<'OUTPUT', "Resetting array size (and getting an exception)" ); 47 new P0, ['FixedBooleanArray'] 44 .sub 'setting_array_size' 45 $P0 = new ['FixedBooleanArray'] 46 $I0 = $P0 47 is($I0, 0, 'size is initially zero') 48 48 49 set I0,P050 set P0,151 set P0,252 print "Should have gotten an exception\n" 49 $P0 = 1 50 $I0 = $P0 51 is($I0, 1, 'size set to 1') 52 .end 53 53 54 .sub 'resizing_not_allowed' 55 $P0 = new ['FixedBooleanArray'] 54 56 55 end 56 CODE 57 /FixedBooleanArray: Can't resize! 58 current instr\.:/ 59 OUTPUT 57 push_eh resizing_not_allowed_handler 58 $P0 = 1 59 $P0 = 2 60 nok(1, 'resizing should not have succeeded') 61 pop_eh 62 .return() 60 63 61 #VIM's syntax highlighter needs this line 64 resizing_not_allowed_handler: 65 .get_results($P0) 66 $S0 = $P0 67 like($S0, ":s FixedBooleanArray\\: Can\\'t resize\\!", 'Resetting array size (and getting an exception)') 68 .end 62 69 63 pasm_output_is( <<'CODE', <<'OUTPUT', "Setting first element" ); 64 new P0,['FixedBooleanArray']65 set P0,170 .sub 'setting_first_element' 71 $P0 = new ['FixedBooleanArray'] 72 $P0 = 1 66 73 67 set P0[0],-7 68 set I0,P0[0] 69 eq I0,1,OK_1 70 print "not " 71 OK_1: print "ok 1\n" 74 $P0[0] = -7 75 $I0 = $P0[0] 76 is($I0, 1, 'setting first element to a true int value') 72 77 73 set P0[0],3.7 74 set N0,P0[0] 75 eq N0,1.0,OK_2 76 print "not " 77 OK_2: print "ok 2\n" 78 $P0[0] = 3.7 79 $N0 = $P0[0] 80 is($N0, 1.0, 'setting first element to a true num value') 78 81 79 set P0[0],"17" 80 set S0,P0[0] 81 eq S0,"1",OK_3 82 print "not " 83 OK_3: print "ok 3\n" 82 $P0[0] = "17" 83 $S0 = $P0[0] 84 is($S0, "1", 'setting first element to a true string value') 85 .end 84 86 85 end 86 CODE 87 ok 1 88 ok 2 89 ok 3 90 OUTPUT 87 .sub 'setting_second_element' 88 $P0 = new ['FixedBooleanArray'] 89 $P0 = 2 91 90 92 pasm_output_is( <<'CODE', <<'OUTPUT', "Setting second element" ); 93 new P0, ['FixedBooleanArray']94 set P0, 291 $P0[1] = -7 92 $I0 = $P0[1] 93 is($I0, 1, 'setting second element to a true int value') 95 94 96 set P0[1], -7 97 set I0, P0[1] 98 eq I0,1,OK_1 99 print "not " 100 OK_1: print "ok 1\n" 95 $P0[1] = 3.7 96 $N0 = $P0[1] 97 is($N0, 1.0, 'setting second element to a true num value') 101 98 102 set P0[1], 3.7 103 set N0, P0[1] 104 eq N0,1.0,OK_2 105 print "not " 106 OK_2: print "ok 2\n" 99 $P0[1] = "17" 100 $S0 = $P0[1] 101 is($S0, "1", 'setting second element to a true string value') 102 .end 107 103 108 set P0[1],"17" 109 set S0, P0[1] 110 eq S0,"1",OK_3 111 print "not " 112 OK_3: print "ok 3\n" 104 .sub 'setting_out_of_bounds' 105 $P0 = new ['FixedBooleanArray'] 106 $P0 = 1 113 107 114 end 115 CODE 116 ok 1 117 ok 2 118 ok 3 119 OUTPUT 108 push_eh setting_out_of_bounds_handler 109 $P0[1] = -7 110 pop_eh 111 nok(1, "Setting out-of-bounds element wrongly succeeded") 112 .return() 120 113 121 pasm_error_output_like( <<'CODE', <<'OUTPUT', "Setting out-of-bounds elements" ); 122 new P0, ['FixedBooleanArray'] 123 set P0, 1 114 setting_out_of_bounds_handler: 115 .get_results($P0) 116 $S0 = $P0 117 like($S0, ":s FixedBooleanArray\\: index out of bounds\\!", "Setting out-of-bounds elements") 118 .end 124 119 125 set P0[1], -7 120 .sub 'getting_out_of_bounds' 121 $P0 = new ['FixedBooleanArray'] 122 $P0 = 1 126 123 127 end128 CODE 129 /FixedBooleanArray: index out of bounds! 130 current instr\.:/ 131 OUTPUT 124 push_eh getting_out_of_bounds_handler 125 $I0 = $P0[1] 126 pop_eh 127 nok(1, "Getting out-of-bounds element wrongly succeeded") 128 .return() 132 129 133 pasm_error_output_like( <<'CODE', <<'OUTPUT', "Getting out-of-bounds elements" ); 134 new P0, ['FixedBooleanArray'] 135 set P0, 1 130 getting_out_of_bounds_handler: 131 .get_results($P0) 132 $S0 = $P0 133 like($S0, ":s FixedBooleanArray\\: index out of bounds\\!", "Getting out-of-bounds elements") 134 .end 136 135 137 set I0, P0[1] 138 end 139 CODE 140 /FixedBooleanArray: index out of bounds! 141 current instr\.:/ 142 OUTPUT 136 .sub 'set_pmc_access_int' 137 $P0 = new ['FixedBooleanArray'] 138 $P0 = 3 139 $P1 = new ['Key'] 143 140 144 pasm_output_is( <<"CODE", <<'OUTPUT', "Set via PMC keys, access via INTs" ); 145 .include 'fp_equality.pasm' 146 new P0, ['FixedBooleanArray'] 147 set P0, 3 148 new P1, ['Key'] 141 $P1 = 0 142 $P0[$P1] = 25 149 143 150 set P1, 0151 set P0[P1], 25144 $P1 = 1 145 $P0[$P1] = 2.5 152 146 153 set P1, 1154 set P0[P1], 2.5147 $P1 = 2 148 $P0[$P1] = "17" 155 149 156 set P1, 2157 set P0[P1], "17"150 $I0 = $P0[0] 151 is($I0, 1, "Set via PMC keys, access via INTs: int value") 158 152 159 set I0, P0[0] 160 eq I0, 1, OK1 161 print "not " 162 OK1: print "ok 1\\n" 153 $N0 = $P0[1] 154 is($N0, 1.0, "Set via PMC keys, access via INTs: num value") 163 155 164 set N0, P0[1] 165 .fp_eq_pasm(N0, 1.0, OK2) 166 print "not " 167 OK2: print "ok 2\\n" 156 $S0 = $P0[0] 157 is($S0, "1", "Set via PMC keys, access via INTs: string value") 158 .end 168 159 169 set S0, P0[2] 170 eq S0, "1", OK3 171 print "not " 172 OK3: print "ok 3\\n" 160 .sub 'set_int_access_pmc' 161 $P0 = new ['FixedBooleanArray'] 162 $P0 = 1024 173 163 174 end175 CODE 176 ok 1 177 ok 2 178 ok 3 179 OUTPUT 164 $P0[25] = 125 165 $P0[128] = 10.2 166 $P0[513] = "17" 167 $P1 = new ['Integer'] 168 $P1 = 123456 169 $P0[1023] = $P1 180 170 181 pasm_output_is( <<"CODE", <<'OUTPUT', "Set via INTs, access via PMC Keys" ); 182 .include 'fp_equality.pasm' 183 new P0, ['FixedBooleanArray'] 184 set P0, 1024 171 $P2 = new ['Key'] 185 172 186 set P0[25], 125 187 set P0[128], 10.2 188 set P0[513], "17" 189 new P1, ['Integer'] 190 set P1, 123456 191 set P0[1023], P1 173 $P2 = 25 174 $I0 = $P0[$P2] 175 is($I0, 1, 'Set via INTs, access via PMC Keys: int value') 192 176 193 new P2, ['Key'] 194 set P2, 25 195 set I0, P0[P2] 196 eq I0, 1, OK1 197 print "not " 198 OK1: print "ok 1\\n" 177 $P2 = 128 178 $N0 = $P0[$P2] 179 is($N0, 1.0, 'Set via INTs, access via PMC Keys: num value') 199 180 200 set P2, 128 201 set N0, P0[P2] 202 .fp_eq_pasm(N0, 1.0, OK2) 203 print "not " 204 OK2: print "ok 2\\n" 181 $P2 = 513 182 $S0 = $P0[$P2] 183 is($S0, '1', 'Set via INTs, access via PMC Keys: string value') 205 184 206 set P2, 513 207 set S0, P0[P2] 208 eq S0, "1", OK3 209 print "not " 210 OK3: print "ok 3\\n" 185 $P2 = 1023 186 $P3 = $P0[$P2] 187 is($P3, 1, 'Set via INTs, access via PMC Keys: PMC value') 188 .end 211 189 212 set P2, 1023 213 set P3, P0[P2] 214 set I1, P3 215 eq I1, 1, OK4 216 print "not " 217 OK4: print "ok 4\\n" 190 .sub 'interface' 191 $P0 = new ['FixedBooleanArray'] 218 192 219 end220 CODE 221 ok 1 222 ok 2 223 ok 3 224 ok 4 225 OUTPUT 193 $I0 = does $P0, 'scalar' 194 nok($I0, 'FixedBooleanArray does not scalar') 195 $I0 = does $P0, 'array' 196 ok($I0, 'FixedBooleanArray does array') 197 $I0 = does $P0, 'no_interface' 198 nok($I0, 'FixedBooleanArray does not no_interface') 199 .end 226 200 227 pir_output_is( << 'CODE', << 'OUTPUT', "check whether interface is done" ); 201 .sub 'truth' 202 $P0 = new ['FixedBooleanArray'] 228 203 229 .sub _main 230 .local pmc pmc1 231 pmc1 = new ['FixedBooleanArray'] 232 .local int bool1 233 does bool1, pmc1, "scalar" 234 print bool1 235 print "\n" 236 does bool1, pmc1, "array" 237 print bool1 238 print "\n" 239 does bool1, pmc1, "no_interface" 240 print bool1 241 print "\n" 242 end 204 nok($P0, 'Empty FixedBooleanArray is false') 205 206 $P0 = 1 207 ok($P0, 'Non-empty FixedBooleanArray is true') 208 209 $P0[0] = 0 210 ok($P0, 'FixedBooleanArray is true, no matter what its values are') 243 211 .end 244 CODE245 0246 1247 0248 OUTPUT249 212 250 pasm_output_is( << 'CODE', << 'OUTPUT', "Truth" ); 251 new P0, ['FixedBooleanArray'] 252 unless P0, OK1 253 print "not " 254 OK1: print "ok 1\n" 255 set P0, 1 256 if P0, OK2 257 print "not " 258 OK2: print "ok 2\n" 259 set P0[0], 0 260 if P0, OK3 261 print "not " 262 OK3: print "ok 3\n" 263 end 264 CODE 265 ok 1 266 ok 2 267 ok 3 268 OUTPUT 213 .sub 'pmc_keys_and_values' 214 $P0 = new ['FixedBooleanArray'] 215 $P0 = 2 269 216 270 pasm_output_is( << 'CODE', << 'OUTPUT', "PMC keys & values" ); 271 new P0, ['FixedBooleanArray'] 272 set P0, 2 273 new P1, ['Key'] 274 set P1, 1 275 new P2, ['Integer'] 276 set P2, 1 277 set P0[P1], P2 278 set I0, P0[P1] 279 print I0 280 print "\n" 281 end 282 CODE 283 1 284 OUTPUT 217 $P1 = new ['Key'] 218 $P1 = 1 219 $P2 = new ['Integer'] 220 $P2 = 1 221 $P0[$P1] = $P2 285 222 286 pir_output_is( <<'CODE', <<'OUTPUT', "freeze/thaw" ); 287 .sub main :main 223 $I0 = $P0[$P1] 224 is($I0, 1, 'PMC keys & values') 225 .end 226 227 .sub 'freeze_thaw' 288 228 .local pmc fba 289 229 .local int i 290 230 .local string s … … 298 238 fba[12] = 1 299 239 fba[15] = 1 300 240 301 say fba 241 $S0 = fba 242 is($S0, '01001000100010010', 'FixedBooleanArray before freeze') 302 243 s = freeze fba 303 244 fba.'fill'(0) 304 245 fba = thaw s 305 sayfba306 246 $S0 = fba 247 is($S0, '01001000100010010', 'FixedBooleanArray after thaw') 307 248 .end 308 249 309 CODE 310 01001000100010010 311 01001000100010010 312 OUTPUT 313 314 pir_output_is( <<'CODE', <<'OUTPUT', "clone" ); 315 .sub main :main 250 .sub 'clone' 316 251 .local pmc fba1, fba2 317 252 .local int i 318 253 .local string s … … 326 261 fba1[12] = 1 327 262 fba1[15] = 1 328 263 329 say fba1 264 $S0 = fba1 265 is($S0, '01001000100010010', 'FixedBooleanArray before clone') 330 266 fba2 = clone fba1 331 sayfba2332 267 $S1 = fba2 268 is($S0, $S1, "clones have the same string representation") 333 269 .end 334 270 335 CODE 336 01001000100010010 337 01001000100010010 338 OUTPUT 339 340 pir_output_is( <<'CODE', <<'OUTPUT', "get_iter" ); 341 .sub 'main' :main 271 .sub 'get_iter' 342 272 $P0 = new ['FixedBooleanArray'] 343 273 $P0 = 3 344 274 $P0[0] = 1 345 275 $P0[1] = 0 346 276 $P0[2] = 1 277 347 278 $P1 = iter $P0 348 loop: 349 unless $P1 goto loop_end 350 $S2 = shift $P1 351 say $S2 352 goto loop 353 loop_end: 279 $I2 = shift $P1 280 is($I2, 1, 'get_iter: first element') 281 $I2 = shift $P1 282 is($I2, 0, 'get_iter: second element') 283 $I2 = shift $P1 284 is($I2, 1, 'get_iter: third element') 285 286 nok($P1, 'iterator exhausted') 354 287 .end 355 CODE356 1357 0358 1359 OUTPUT360 288 289 .sub 'fill' 290 $P0 = new ['FixedBooleanArray'] 291 $P0.'fill'(0) 292 ok(1, 'Filling empty array') 361 293 362 1; 294 .local int result, i, size 295 size = 1564 296 $P0 = size 363 297 298 $P0.'fill'(0) 299 i = 0 300 result = 0 301 $I1 = 0 302 fill_false_loop: 303 unless i < size goto fill_false_end 304 $I0 = $P0[i] 305 result = or result, $I0 306 inc i 307 goto fill_false_loop 308 fill_false_end: 309 nok(result, "Fill with 0") 364 310 311 $P0.'fill'(1) 312 i = 0 313 result = 1 314 $I1 = 0 315 fill_true_loop: 316 unless i < size goto fill_true_end 317 $I0 = $P0[i] 318 result = and result, $I0 319 inc i 320 goto fill_true_loop 321 fill_true_end: 322 ok(result, "Fill with 1") 323 .end 324 325 365 326 # Local Variables: 366 327 # mode: cperl 367 328 # cperl-indent-level: 4 -
src/pmc/fixedbooleanarray.pmc
583 583 GET_ATTR_size(INTERP, SELF, size); 584 584 j = size / BITS_PER_CHAR + 1; 585 585 586 if (fill) 587 memset(bit_array, 0xff, j); 588 else 589 memset(bit_array, 0, j); 586 if (bit_array) { 587 if (fill) 588 memset(bit_array, 0xff, j); 589 else 590 memset(bit_array, 0, j); 591 } 590 592 } 591 593 } 592 594