summaryrefslogtreecommitdiffstats
path: root/js/src/jit-test/tests/warp/string-compare-char-string.js
blob: f56009f5a18482b9044ead8f3f56cc77f39fa83a (plain)
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
// Test comparison against single-element character strings.

function makeComparator(code) {
  var str = String.fromCharCode(code);

  return Function("ch", "code", `
    assertEq(ch == "${str}", code == ${code} && ch.length == 1);
    assertEq(ch != "${str}", code != ${code} || ch.length != 1);

    assertEq(ch < "${str}", code < ${code} || (code == ${code} && ch.length < 1));
    assertEq(ch <= "${str}", code < ${code} || (code == ${code} && ch.length <= 1));
    assertEq(ch > "${str}", code > ${code} || (code == ${code} && ch.length > 1));
    assertEq(ch >= "${str}", code > ${code} || (code == ${code} && ch.length >= 1));
  `);
}

function testCompare(strings, comparator) {
  // Don't Ion compile to ensure |comparator| won't be inlined.
  with ({}) ;

  for (let i = 0; i < 1000; ++i) {
    let str = strings[i % strings.length];

    comparator("", -1);

    for (let j = 0; j < str.length; ++j) {
      let ch = str.charAt(j);
      let code = str.charCodeAt(j);

      comparator(ch, code);
      comparator(ch + "A", code);
    }
  }
}

// Compare against the Latin-1 character U+0062 ('b').
testCompare([
  "",
  "a", "b", "c",
  "a-", "b-", "c-",
  "a\u{100}", "b\u{100}", "c\u{100}",
], makeComparator(0x62));

// Compare against the maximum Latin-1 character U+00FF.
testCompare([
  "",
  "a", "b", "c",
  "a-", "b-", "c-",
  "\u{fe}", "\u{ff}", "\u{100}",
  "\u{fe}-", "\u{ff}-", "\u{100}-",
], makeComparator(0xff));

// Compare against the Two-byte character U+0101.
testCompare([
  "",
  "a", "b", "c",
  "a-", "b-", "c-",
  "\u{100}", "\u{101}", "\u{102}",
  "\u{100}-", "\u{101}-", "\u{102}-",
], makeComparator(0x101));