|
1 | 1 | # string indexing tests.
|
2 |
| -my int $x := 100; |
3 |
| -plan(26 * $x + 26); |
| 2 | +my int $x := 200; |
| 3 | + |
| 4 | +plan( |
| 5 | + (26 * $x) * 1 |
| 6 | + + 26 * 7 |
| 7 | +); |
4 | 8 | # XXX TODO rework this stuff into indexic.t
|
5 | 9 | # If changing this file, also add equivalent changes to moar/xx-indexic.t
|
6 | 10 | my str $abc-string := 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
|
| 11 | +my str $russian := 'АБВГДЕЖЗИЙКЛМНОПРСТУФХЦЧШЩ'; |
7 | 12 | my int $i := 0;
|
8 | 13 |
|
9 | 14 | $i := 0;
|
10 | 15 | # test that we can find A through Z with the needle at all possible points of
|
11 |
| -# a string of length 26 (the alphabet) |
12 |
| -test-it($abc-string, 0, False); |
13 |
| -test-it($abc-string, 1, False); |
14 |
| -#$string' x $x" |
| 16 | +# a string of length 26 (the alphabet) from index 0 |
| 17 | +test-it($abc-string, 0, 'Finds the needle when starting at index 0 when needle in haystack at varying distances', "‘$abc-string’", :offset(0), :should-succeed); |
| 18 | +# Russian text to force non-8bit representation XXX russian tests need to start with 'А' which is not the same as Latin 'A'! |
| 19 | +test-it($russian, 0, 'Finds the needle when starting at index 0 when needle in haystack at varying distances', "‘$russian’", :offset(0), :should-succeed, :begin('А')); |
| 20 | + |
| 21 | +test-it($abc-string, 1, "Finds needle when starting at index of the needle inside the haystack", "‘$abc-string’", :offset(0), :should-succeed); |
| 22 | +test-it($russian, 1, "Finds needle when starting at index of the needle inside the haystack", "‘$russian’", :offset(0), :should-succeed, :begin('А')); |
| 23 | + |
| 24 | +test-it($abc-string, 1, 'Does not find needle when starting one index after location of the needle inside the haystack', "‘$abc-string’", :offset(1), :should-succeed(0) ); |
| 25 | +test-it($russian, 1, 'Does not find needle when starting one index after location of the needle inside the haystack', "‘$russian’", :offset(1), :should-succeed(0), :begin('А') ); |
15 | 26 | # Test that we can find a needle from offset 0 to offset 2599 that is placed
|
16 | 27 | # at the requested offset
|
17 | 28 | my str $abc-x-string := nqp::x($abc-string, $x);
|
18 |
| -#test-it($abc-x-string, 0); |
19 |
| -#while ($i < nqp::chars($abc-x-string)) { |
20 |
| -# my int $j := $i % nqp::chars($abc-string); |
21 |
| -# my str $letter := nqp::chr($j + nqp::ord('A')); |
22 |
| -# is( nqp::index($abc-x-string, $letter, $i), $i, "nqp::index('$abc-string' x $x, '$letter', $i)"); |
23 |
| -# $i++; |
24 |
| -#} |
| 29 | +# line below is not yet working replacement for the while loop below (so it can use the routine as well) |
| 30 | +test-it($abc-x-string, 1, "Stresstest: Finds needle when needle at start point in haystack", "'$abc-string' x $x", :offset(0), :should-succeed); |
| 31 | +test-it($russian, 1, "Stresstest: Finds needle when needle at start point in haystack", "'$russian' x $x", :offset(0), :should-succeed, :begin('А')); |
25 | 32 |
|
26 |
| -sub test-it ($string, $offset, $range) { |
| 33 | +sub test-it ($string, $range, $label, str $message, int :$offset, :$should-succeed, :$named, :$begin = 'A') { |
| 34 | + $message := "‘$string’" unless $message; |
| 35 | + note($label); |
27 | 36 | my $i := 0;
|
28 |
| - while ($i < nqp::chars($abc-string)) { |
29 |
| - my $h_start := $offset; |
30 |
| - my int $j := $i % nqp::chars($string); |
31 |
| - my str $letter := nqp::chr($j + nqp::ord('A')); |
32 |
| - my $needle := $letter; |
33 |
| - is( nqp::index($string, $needle, $h_start), $i, "nqp::index('$string', $needle, $h_start) = $i"); |
| 37 | + while ($i < nqp::chars($string)) { |
| 38 | + my int $j := $i % nqp::chars($abc-string); |
| 39 | + my str $letter := nqp::chr($j + nqp::ord($begin)); |
| 40 | + my int $h_start := $range ?? $i + $offset !! $offset; |
| 41 | + my int $expected := $should-succeed ?? $i !! -1; |
| 42 | + |
| 43 | + my str $needle := $letter; |
| 44 | + is( nqp::index($string, $needle, $h_start), $expected, "nqp::index($message, $needle, $h_start) = $expected"); |
34 | 45 | $i++;
|
35 | 46 | }
|
36 | 47 | }
|
0 commit comments