Skip to content

Commit a1a7372

Browse files
committed
Many more index tests, also test Unicode strings as well
Tests many more cases, as well as testing for failures as well. Has Unicode strings so will be able to make sure they cannot be stored in 8bits, to get better test coverage.
1 parent dd17265 commit a1a7372

File tree

1 file changed

+31
-20
lines changed

1 file changed

+31
-20
lines changed

t/nqp/107-index.t

Lines changed: 31 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,47 @@
11
# 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+
);
48
# XXX TODO rework this stuff into indexic.t
59
# If changing this file, also add equivalent changes to moar/xx-indexic.t
610
my str $abc-string := 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
11+
my str $russian := 'АБВГДЕЖЗИЙКЛМНОПРСТУФХЦЧШЩ';
712
my int $i := 0;
813

914
$i := 0;
1015
# 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('А') );
1526
# Test that we can find a needle from offset 0 to offset 2599 that is placed
1627
# at the requested offset
1728
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('А'));
2532

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);
2736
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");
3445
$i++;
3546
}
3647
}

0 commit comments

Comments
 (0)