LCOV - code coverage report
Current view: top level - test/cctest - test-smi-lexicographic-compare.cc (source / functions) Hit Total Coverage
Test: app.info Lines: 25 25 100.0 %
Date: 2019-04-17 Functions: 6 6 100.0 %

          Line data    Source code
       1             : // Copyright 2018 the V8 project authors. All rights reserved.
       2             : // Use of this source code is governed by a BSD-style license that can be
       3             : // found in the LICENSE file.
       4             : 
       5             : #include <set>
       6             : 
       7             : #include "src/objects-inl.h"
       8             : #include "src/objects/smi.h"
       9             : #include "src/v8.h"
      10             : #include "test/cctest/cctest.h"
      11             : 
      12             : namespace v8 {
      13             : namespace internal {
      14             : 
      15             : namespace {
      16             : 
      17        2275 : void AddSigned(std::set<Smi>& smis, int64_t x) {
      18        2275 :   if (!Smi::IsValid(x)) return;
      19             : 
      20        4340 :   smis.insert(Smi::FromInt(static_cast<int>(x)));
      21        6510 :   smis.insert(Smi::FromInt(static_cast<int>(-x)));
      22             : }
      23             : 
      24             : // Uses std::lexicographical_compare twice to convert the result to -1, 0 or 1.
      25     2642645 : int ExpectedCompareResult(Smi a, Smi b) {
      26             :   std::string str_a = std::to_string(a->value());
      27             :   std::string str_b = std::to_string(b->value());
      28             :   bool expected_a_lt_b = std::lexicographical_compare(
      29             :       str_a.begin(), str_a.end(), str_b.begin(), str_b.end());
      30             :   bool expected_b_lt_a = std::lexicographical_compare(
      31             :       str_b.begin(), str_b.end(), str_a.begin(), str_a.end());
      32             : 
      33     2642645 :   if (!expected_a_lt_b && !expected_b_lt_a) {
      34             :     return 0;
      35     2639010 :   } else if (expected_a_lt_b) {
      36             :     return -1;
      37             :   } else {
      38     1319505 :     CHECK(expected_b_lt_a);
      39             :     return 1;
      40             :   }
      41             : }
      42             : 
      43     2642645 : bool Test(Isolate* isolate, Smi a, Smi b) {
      44     2642645 :   int actual = Smi(Smi::LexicographicCompare(isolate, a, b)).value();
      45     2642645 :   int expected = ExpectedCompareResult(a, b);
      46             : 
      47     2642645 :   return actual == expected;
      48             : }
      49             : 
      50             : }  // namespace
      51             : 
      52       26644 : TEST(TestSmiLexicographicCompare) {
      53             :   Isolate* isolate = CcTest::InitIsolateOnce();
      54             :   HandleScope scope(isolate);
      55             : 
      56             :   std::set<Smi> smis;
      57             : 
      58         105 :   for (int64_t xb = 1; xb <= Smi::kMaxValue; xb *= 10) {
      59        1050 :     for (int64_t xf = 0; xf <= 9; ++xf) {
      60        3500 :       for (int64_t xo = -1; xo <= 1; ++xo) {
      61        1500 :         AddSigned(smis, xb * xf + xo);
      62             :       }
      63             :     }
      64             :   }
      65             : 
      66         315 :   for (int64_t yb = 1; yb <= Smi::kMaxValue; yb *= 2) {
      67        1705 :     for (int64_t yo = -2; yo <= 2; ++yo) {
      68         775 :       AddSigned(smis, yb + yo);
      69             :     }
      70             :   }
      71             : 
      72        3640 :   for (Smi a : smis) {
      73     2646280 :     for (Smi b : smis) {
      74     2642645 :       CHECK(Test(isolate, a, b));
      75             :     }
      76             :   }
      77           5 : }
      78             : 
      79             : }  // namespace internal
      80       79917 : }  // namespace v8

Generated by: LCOV version 1.10