File: 08-indexic.t

package info (click to toggle)
nqp 2022.12%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 9,436 kB
  • sloc: java: 28,030; perl: 3,394; ansic: 451; makefile: 200; javascript: 68; sh: 1
file content (89 lines) | stat: -rw-r--r-- 3,734 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
plan(119 * 2 + 4 * 3);
ok(nqp::indexic('hi', '', 2) == 2, 'nqp::indexic finds empty string after last index');
ok(nqp::indexic('hi', '', 0) == 0, 'nqp::indexic finds empty string at start');
ok(nqp::indexic('hi', '', 1) == 1, 'nqp::indexic finds empty string in the middle');
ok(nqp::indexic('hi', '', 3) == -1, 'nqp::indexic does not find empty string at graphs + 1');
ok(nqp::indexicim('hi', '', 2) == 2, 'nqp::indexicim finds empty string after last index');
ok(nqp::indexicim('hi', '', 0) == 0, 'nqp::indexicim finds empty string at start');
ok(nqp::indexicim('hi', '', 1) == 1, 'nqp::indexicim finds empty string in the middle');
ok(nqp::indexicim('hi', '', 3) == -1, 'nqp::indexicim does not find empty string at graphs + 1');
ok(nqp::indexim('hi', '', 2) == 2, 'nqp::indexim finds empty string after last index');
ok(nqp::indexim('hi', '', 0) == 0, 'nqp::indexim finds empty string at start');
ok(nqp::indexim('hi', '', 1) == 1, 'nqp::indexim finds empty string in the middle');
ok(nqp::indexim('hi', '', 3) == -1, 'nqp::indexim does not find empty string at graphs + 1');
my @array :=
# (haystack, needle, result)
# line below is a todo example: commented out in case someone else needs to todo one of these tests
## todo example: ('chars that expand on casefolding at the end are broken with equatic', 1),
    # Make sure it doesn't find a partial match at end of the haystack and then
    # return a partial match.
    ('st', 'sta', -1),
    ('st', 'sta', -1),
    ('abcdefg', 'fghi', -1),
    ('aBcdef', 'bcd', 1),
    # With ligatures that expand under casefolding
    ('aastaa', 'st', 2),
    ('aastaa', 'st', 2),
    ('aastaa', 'st',  2 ),
    ('aastaa', 'st', 2 ),
    ('aastaa', 'st', 2 ),
    ('aastaaz', 'z', 5 ),
    ('aastzaa', 'z', 3 ),
    ('aaast', 'st', 3 ),
    ('st', 'st', 0),
    ('aaaast', 'st', 4 ),
    ('AAAAst', 'st', 4),
    #
    ('staaa', 'st', 0 ),
    ('st', 'st', 0),
;
for @array -> $elem {
    if nqp::elems($elem) < 3 {
        todo($elem[0], $elem[1]);
    }
    else {
        is( nqp::indexic($elem[0], $elem[1], 0), $elem[2],
            "nqp::indexic('" ~ $elem[0] ~ "', " ~ $elem[1] ~ "', 0)");
        is( nqp::indexicim($elem[0], $elem[1], 0), $elem[2],
            "nqp::indexic('" ~ $elem[0] ~ "', " ~ $elem[1] ~ "', 0)");
    }
}
test-it('st', 'st',  20, 1);
test-it('st', 'st',  20, 0);
test-it('st', 'st',  20, 1);
test-it('st', 'st',  20, 0);
test-it('stbbbbbbbbbbbbbbbbbbbbbb', 'st',  20, 2);

# Without codepoint which expand when casefolded
for (0,1,2,3,4,5,6) -> $val {
    my str $letter := nqp::chr($val + nqp::ord('A'));
    is( nqp::indexic('ABCDEFG', $letter, 0), $val, "nqp::indexic('ABCDEFG', '$letter', $val)");
    is( nqp::indexicim('ABCDEFG', $letter, 0), $val, "nqp::indexicim('ABCDEFG', '$letter', $val)");
}
# TODO comment on what the options for this function do…
# $opt: Bitfield type
# +1 causes an `x` of characters to be inserted at the end
# of the target needle inside the haystack to be searched
# +2 causes us to expect a failed match
sub test-it ($needle, $text, $max, $opt) {
    my int $i := 1;
    my $long-end := $opt +& 1;
    my $should-fail := $opt +& 2;
    while ($i < $max) {
        my str $str := nqp::x('a', $max - $i);
        $str := $str ~ $text;
        $str := $str ~ nqp::x('b', $i) if $long-end;
        my $expect := $should-fail ?? -1 !! $max - $i;
        is(
          nqp::indexic($str, $needle, 0),
          $expect,
          "nqp::indexic(haystack = '$str', needle = '$needle', 0) ==  " ~ $expect
        );
        is(
          nqp::indexicim($str, $needle, 0),
          $expect,
          "nqp::indexicim(haystack = '$str', needle = '$needle', 0) ==  " ~ $expect
        );
        $i++
    }
}