summaryrefslogtreecommitdiff
path: root/deps/v8/test/mjsunit/lexicographic-compare.js
blob: b87dd1c1ef62c8c970c0ceac91a12f4d721c53c5 (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
// Copyright 2018 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

// Flags: --allow-natives-syntax

(function () {

  assertFalse(%IsSmi(2147483648), 'Update test for >32 bit Smi');

  // Collect a list of interesting Smis.
  const seen = {};
  const smis = [];
  function add(x) {
    if (x | 0 == x) {
      x = x | 0;  // Canonicalizes to Smi if 32-bit signed and fits in Smi.
    }
    if (%_IsSmi(x) && !seen[x]) {
      seen[x] = 1;
      smis.push(x);
    }
  }
  function addSigned(x) {
    add(x);
    add(-x);
  }

  var BIGGER_THAN_ANY_SMI = 10 * 1000 * 1000 * 1000;
  for (var xb = 1; xb <= BIGGER_THAN_ANY_SMI; xb *= 10) {
    for (var xf = 0; xf <= 9; xf++) {
      for (var xo = -1; xo <= 1; xo++) {
        addSigned(xb * xf + xo);
      }
    }
  }

  console.log("A")

  for (var yb = 1; yb <= BIGGER_THAN_ANY_SMI; yb *= 2) {
    for (var yo = -2; yo <= 2; yo++) {
      addSigned(yb + yo);
    }
  }

  function test(x,y) {
    const lex = %SmiLexicographicCompare(x, y);
    const expected = (x == y) ? 0 : (("" + x) < ("" + y) ? -1 : 1);
    return lex == expected;
  }

  console.log(smis.length);

  for (var i = 0; i < smis.length; i++) {
    for (var j = 0; j < smis.length; j++) {
      const x = smis[i];
      const y = smis[j];
      assertTrue(test(x, y), x + " < " + y);;
    }
  }

  console.log("C")
})();