1 // Copyright 2006-2008 the V8 project authors. All rights reserved.
2
3 #include <stdlib.h>
4
5 #include "v8.h"
6
7 #include "platform.h"
8 #include "cctest.h"
9
10 using namespace v8::internal;
11
12
TEST(Hex)13 TEST(Hex) {
14 CHECK_EQ(0.0, StringToDouble("0x0", ALLOW_HEX | ALLOW_OCTALS));
15 CHECK_EQ(0.0, StringToDouble("0X0", ALLOW_HEX | ALLOW_OCTALS));
16 CHECK_EQ(1.0, StringToDouble("0x1", ALLOW_HEX | ALLOW_OCTALS));
17 CHECK_EQ(16.0, StringToDouble("0x10", ALLOW_HEX | ALLOW_OCTALS));
18 CHECK_EQ(255.0, StringToDouble("0xff", ALLOW_HEX | ALLOW_OCTALS));
19 CHECK_EQ(175.0, StringToDouble("0xAF", ALLOW_HEX | ALLOW_OCTALS));
20
21 CHECK_EQ(0.0, StringToDouble("0x0", ALLOW_HEX));
22 CHECK_EQ(0.0, StringToDouble("0X0", ALLOW_HEX));
23 CHECK_EQ(1.0, StringToDouble("0x1", ALLOW_HEX));
24 CHECK_EQ(16.0, StringToDouble("0x10", ALLOW_HEX));
25 CHECK_EQ(255.0, StringToDouble("0xff", ALLOW_HEX));
26 CHECK_EQ(175.0, StringToDouble("0xAF", ALLOW_HEX));
27 }
28
29
TEST(Octal)30 TEST(Octal) {
31 CHECK_EQ(0.0, StringToDouble("0", ALLOW_HEX | ALLOW_OCTALS));
32 CHECK_EQ(0.0, StringToDouble("00", ALLOW_HEX | ALLOW_OCTALS));
33 CHECK_EQ(1.0, StringToDouble("01", ALLOW_HEX | ALLOW_OCTALS));
34 CHECK_EQ(7.0, StringToDouble("07", ALLOW_HEX | ALLOW_OCTALS));
35 CHECK_EQ(8.0, StringToDouble("010", ALLOW_HEX | ALLOW_OCTALS));
36 CHECK_EQ(63.0, StringToDouble("077", ALLOW_HEX | ALLOW_OCTALS));
37
38 CHECK_EQ(0.0, StringToDouble("0", ALLOW_HEX));
39 CHECK_EQ(0.0, StringToDouble("00", ALLOW_HEX));
40 CHECK_EQ(1.0, StringToDouble("01", ALLOW_HEX));
41 CHECK_EQ(7.0, StringToDouble("07", ALLOW_HEX));
42 CHECK_EQ(10.0, StringToDouble("010", ALLOW_HEX));
43 CHECK_EQ(77.0, StringToDouble("077", ALLOW_HEX));
44 }
45
46
TEST(MalformedOctal)47 TEST(MalformedOctal) {
48 CHECK_EQ(8.0, StringToDouble("08", ALLOW_HEX | ALLOW_OCTALS));
49 CHECK_EQ(81.0, StringToDouble("081", ALLOW_HEX | ALLOW_OCTALS));
50 CHECK_EQ(78.0, StringToDouble("078", ALLOW_HEX | ALLOW_OCTALS));
51
52 CHECK(isnan(StringToDouble("07.7", ALLOW_HEX | ALLOW_OCTALS)));
53 CHECK(isnan(StringToDouble("07.8", ALLOW_HEX | ALLOW_OCTALS)));
54 CHECK(isnan(StringToDouble("07e8", ALLOW_HEX | ALLOW_OCTALS)));
55 CHECK(isnan(StringToDouble("07e7", ALLOW_HEX | ALLOW_OCTALS)));
56
57 CHECK_EQ(8.7, StringToDouble("08.7", ALLOW_HEX | ALLOW_OCTALS));
58 CHECK_EQ(8e7, StringToDouble("08e7", ALLOW_HEX | ALLOW_OCTALS));
59
60 CHECK_EQ(0.001, StringToDouble("0.001", ALLOW_HEX | ALLOW_OCTALS));
61 CHECK_EQ(0.713, StringToDouble("0.713", ALLOW_HEX | ALLOW_OCTALS));
62
63 CHECK_EQ(8.0, StringToDouble("08", ALLOW_HEX));
64 CHECK_EQ(81.0, StringToDouble("081", ALLOW_HEX));
65 CHECK_EQ(78.0, StringToDouble("078", ALLOW_HEX));
66
67 CHECK_EQ(7.7, StringToDouble("07.7", ALLOW_HEX));
68 CHECK_EQ(7.8, StringToDouble("07.8", ALLOW_HEX));
69 CHECK_EQ(7e8, StringToDouble("07e8", ALLOW_HEX));
70 CHECK_EQ(7e7, StringToDouble("07e7", ALLOW_HEX));
71
72 CHECK_EQ(8.7, StringToDouble("08.7", ALLOW_HEX));
73 CHECK_EQ(8e7, StringToDouble("08e7", ALLOW_HEX));
74
75 CHECK_EQ(0.001, StringToDouble("0.001", ALLOW_HEX));
76 CHECK_EQ(0.713, StringToDouble("0.713", ALLOW_HEX));
77 }
78
79
TEST(TrailingJunk)80 TEST(TrailingJunk) {
81 CHECK_EQ(8.0, StringToDouble("8q", ALLOW_TRAILING_JUNK));
82 CHECK_EQ(63.0, StringToDouble("077qqq", ALLOW_OCTALS | ALLOW_TRAILING_JUNK));
83 }
84
85
TEST(NonStrDecimalLiteral)86 TEST(NonStrDecimalLiteral) {
87 CHECK(isnan(StringToDouble(" ", NO_FLAGS, OS::nan_value())));
88 CHECK(isnan(StringToDouble("", NO_FLAGS, OS::nan_value())));
89 CHECK(isnan(StringToDouble(" ", NO_FLAGS, OS::nan_value())));
90 CHECK_EQ(0.0, StringToDouble("", NO_FLAGS));
91 CHECK_EQ(0.0, StringToDouble(" ", NO_FLAGS));
92 }
93
94 class OneBit1: public BitField<uint32_t, 0, 1> {};
95 class OneBit2: public BitField<uint32_t, 7, 1> {};
96 class EightBit1: public BitField<uint32_t, 0, 8> {};
97 class EightBit2: public BitField<uint32_t, 13, 8> {};
98
TEST(BitField)99 TEST(BitField) {
100 uint32_t x;
101
102 // One bit bit field can hold values 0 and 1.
103 CHECK(!OneBit1::is_valid(static_cast<uint32_t>(-1)));
104 CHECK(!OneBit2::is_valid(static_cast<uint32_t>(-1)));
105 for (int i = 0; i < 2; i++) {
106 CHECK(OneBit1::is_valid(i));
107 x = OneBit1::encode(i);
108 CHECK_EQ(i, OneBit1::decode(x));
109
110 CHECK(OneBit2::is_valid(i));
111 x = OneBit2::encode(i);
112 CHECK_EQ(i, OneBit2::decode(x));
113 }
114 CHECK(!OneBit1::is_valid(2));
115 CHECK(!OneBit2::is_valid(2));
116
117 // Eight bit bit field can hold values from 0 tp 255.
118 CHECK(!EightBit1::is_valid(static_cast<uint32_t>(-1)));
119 CHECK(!EightBit2::is_valid(static_cast<uint32_t>(-1)));
120 for (int i = 0; i < 256; i++) {
121 CHECK(EightBit1::is_valid(i));
122 x = EightBit1::encode(i);
123 CHECK_EQ(i, EightBit1::decode(x));
124 CHECK(EightBit2::is_valid(i));
125 x = EightBit2::encode(i);
126 CHECK_EQ(i, EightBit2::decode(x));
127 }
128 CHECK(!EightBit1::is_valid(256));
129 CHECK(!EightBit2::is_valid(256));
130 }
131