Line data Source code
1 : // Copyright 2016 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 "src/v8.h"
6 :
7 : #include "src/conversions.h"
8 : #include "test/unittests/test-utils.h"
9 :
10 : namespace v8 {
11 : namespace internal {
12 : namespace interpreter {
13 :
14 : class ConversionsTest : public ::testing::Test {
15 : public:
16 2 : ConversionsTest() = default;
17 2 : ~ConversionsTest() override = default;
18 :
19 : SourcePosition toPos(int offset) {
20 : return SourcePosition(offset, offset % 10 - 1);
21 : }
22 : };
23 :
24 : // Some random offsets, mostly at 'suspicious' bit boundaries.
25 :
26 27792 : struct IntStringPair {
27 : int integer;
28 : std::string string;
29 : };
30 :
31 9264 : static IntStringPair int_pairs[] = {{0, "0"},
32 : {101, "101"},
33 : {-1, "-1"},
34 : {1024, "1024"},
35 : {200000, "200000"},
36 : {-1024, "-1024"},
37 : {-200000, "-200000"},
38 : {kMinInt, "-2147483648"},
39 3088 : {kMaxInt, "2147483647"}};
40 :
41 15444 : TEST_F(ConversionsTest, IntToCString) {
42 1 : std::unique_ptr<char[]> buf(new char[4096]);
43 :
44 19 : for (size_t i = 0; i < arraysize(int_pairs); i++) {
45 18 : ASSERT_STREQ(IntToCString(int_pairs[i].integer, {buf.get(), 4096}),
46 : int_pairs[i].string.c_str());
47 : }
48 : }
49 :
50 18528 : struct DoubleStringPair {
51 : double number;
52 : std::string string;
53 : };
54 :
55 9264 : static DoubleStringPair double_pairs[] = {
56 : {0.0, "0"},
57 : {kMinInt, "-2147483648"},
58 : {kMaxInt, "2147483647"},
59 : // ES section 7.1.12.1 #sec-tostring-applied-to-the-number-type:
60 : // -0.0 is stringified to "0".
61 : {-0.0, "0"},
62 : {1.1, "1.1"},
63 3088 : {0.1, "0.1"}};
64 :
65 15444 : TEST_F(ConversionsTest, DoubleToCString) {
66 1 : std::unique_ptr<char[]> buf(new char[4096]);
67 :
68 13 : for (size_t i = 0; i < arraysize(double_pairs); i++) {
69 12 : ASSERT_STREQ(DoubleToCString(double_pairs[i].number, {buf.get(), 4096}),
70 : double_pairs[i].string.c_str());
71 : }
72 : }
73 :
74 : } // namespace interpreter
75 : } // namespace internal
76 9264 : } // namespace v8
|