summaryrefslogtreecommitdiffstats
path: root/js/src/jit-test/tests/warp/string-trim.js
blob: 0ca05be8ac70e85b02205b24f001d08e8f375621 (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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
const whitespace = [
  // Ascii whitespace
  " ",
  "\t",

  // Latin-1 whitespace
  "\u{a0}",

  // Two-byte whitespace
  "\u{1680}",
];

const strings = [
  // Empty string
  "",

  // Ascii strings
  "a",
  "abc",

  // Latin-1 strings
  "á",
  "áèô",

  // Two-byte strings
  "\u{100}",
  "\u{100}\u{101}\u{102}",
].flatMap(x => [
  x,

  // Leading whitespace
  ...whitespace.flatMap(w => [w + x, w + w + x]),

  // Trailing whitespace
  ...whitespace.flatMap(w => [x + w, x + w + w]),

  // Leading and trailing whitespace
  ...whitespace.flatMap(w => [w + x + w, w + w + x + w + w]),

  // Interspersed whitespace
  ...whitespace.flatMap(w => [x + w + x, x + w + w + x]),
]);

function trim(strings) {
  // Compute expected values using RegExp.
  let expected = strings.map(x => x.replace(/(^\s+)|(\s+$)/g, ""));

  for (let i = 0; i < 1000; ++i) {
    let index = i % strings.length;
    assertEq(strings[index].trim(), expected[index]);
  }
}
for (let i = 0; i < 2; ++i) trim(strings);

function trimStart(strings) {
  // Compute expected values using RegExp.
  let expected = strings.map(x => x.replace(/(^\s+)/g, ""));

  for (let i = 0; i < 1000; ++i) {
    let index = i % strings.length;
    assertEq(strings[index].trimStart(), expected[index]);
  }
}
for (let i = 0; i < 2; ++i) trimStart(strings);

function trimEnd(strings) {
  // Compute expected values using RegExp.
  let expected = strings.map(x => x.replace(/(\s+$)/g, ""));

  for (let i = 0; i < 1000; ++i) {
    let index = i % strings.length;
    assertEq(strings[index].trimEnd(), expected[index]);
  }
}
for (let i = 0; i < 2; ++i) trimEnd(strings);