2018-09-05 09:28:55 +00:00
|
|
|
// 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.
|
|
|
|
|
|
|
|
#include <set>
|
|
|
|
|
2019-05-23 08:51:46 +00:00
|
|
|
#include "src/objects/objects-inl.h"
|
2018-11-03 00:13:22 +00:00
|
|
|
#include "src/objects/smi.h"
|
2018-09-05 09:28:55 +00:00
|
|
|
#include "src/v8.h"
|
|
|
|
#include "test/cctest/cctest.h"
|
|
|
|
|
|
|
|
namespace v8 {
|
|
|
|
namespace internal {
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
2018-11-03 00:13:22 +00:00
|
|
|
void AddSigned(std::set<Smi>& smis, int64_t x) {
|
2018-09-05 09:28:55 +00:00
|
|
|
if (!Smi::IsValid(x)) return;
|
|
|
|
|
|
|
|
smis.insert(Smi::FromInt(static_cast<int>(x)));
|
|
|
|
smis.insert(Smi::FromInt(static_cast<int>(-x)));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Uses std::lexicographical_compare twice to convert the result to -1, 0 or 1.
|
2018-11-03 00:13:22 +00:00
|
|
|
int ExpectedCompareResult(Smi a, Smi b) {
|
2018-09-05 09:28:55 +00:00
|
|
|
std::string str_a = std::to_string(a.value());
|
|
|
|
std::string str_b = std::to_string(b.value());
|
|
|
|
bool expected_a_lt_b = std::lexicographical_compare(
|
|
|
|
str_a.begin(), str_a.end(), str_b.begin(), str_b.end());
|
|
|
|
bool expected_b_lt_a = std::lexicographical_compare(
|
|
|
|
str_b.begin(), str_b.end(), str_a.begin(), str_a.end());
|
|
|
|
|
|
|
|
if (!expected_a_lt_b && !expected_b_lt_a) {
|
|
|
|
return 0;
|
|
|
|
} else if (expected_a_lt_b) {
|
|
|
|
return -1;
|
|
|
|
} else {
|
|
|
|
CHECK(expected_b_lt_a);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-03 00:13:22 +00:00
|
|
|
bool Test(Isolate* isolate, Smi a, Smi b) {
|
|
|
|
int actual = Smi(Smi::LexicographicCompare(isolate, a, b)).value();
|
2018-09-05 09:28:55 +00:00
|
|
|
int expected = ExpectedCompareResult(a, b);
|
|
|
|
|
|
|
|
return actual == expected;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
TEST(TestSmiLexicographicCompare) {
|
|
|
|
Isolate* isolate = CcTest::InitIsolateOnce();
|
|
|
|
HandleScope scope(isolate);
|
|
|
|
|
2018-11-03 00:13:22 +00:00
|
|
|
std::set<Smi> smis;
|
2018-09-05 09:28:55 +00:00
|
|
|
|
|
|
|
for (int64_t xb = 1; xb <= Smi::kMaxValue; xb *= 10) {
|
|
|
|
for (int64_t xf = 0; xf <= 9; ++xf) {
|
|
|
|
for (int64_t xo = -1; xo <= 1; ++xo) {
|
|
|
|
AddSigned(smis, xb * xf + xo);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (int64_t yb = 1; yb <= Smi::kMaxValue; yb *= 2) {
|
|
|
|
for (int64_t yo = -2; yo <= 2; ++yo) {
|
|
|
|
AddSigned(smis, yb + yo);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-03 00:13:22 +00:00
|
|
|
for (Smi a : smis) {
|
|
|
|
for (Smi b : smis) {
|
2018-09-05 09:28:55 +00:00
|
|
|
CHECK(Test(isolate, a, b));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace internal
|
|
|
|
} // namespace v8
|