OLD | NEW |
1 // Copyright 2016 the V8 project authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "src/value-serializer.h" | 5 #include "src/value-serializer.h" |
| 6 |
6 #include "include/v8.h" | 7 #include "include/v8.h" |
7 #include "src/api.h" | 8 #include "src/api.h" |
| 9 #include "src/base/build_config.h" |
8 #include "test/unittests/test-utils.h" | 10 #include "test/unittests/test-utils.h" |
9 #include "testing/gtest/include/gtest/gtest.h" | 11 #include "testing/gtest/include/gtest/gtest.h" |
10 | 12 |
11 namespace v8 { | 13 namespace v8 { |
12 namespace { | 14 namespace { |
13 | 15 |
14 class ValueSerializerTest : public TestWithIsolate { | 16 class ValueSerializerTest : public TestWithIsolate { |
15 protected: | 17 protected: |
16 ValueSerializerTest() | 18 ValueSerializerTest() |
17 : serialization_context_(Context::New(isolate())), | 19 : serialization_context_(Context::New(isolate())), |
(...skipping 138 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
156 DecodeTest({0x5f, 0x00}, | 158 DecodeTest({0x5f, 0x00}, |
157 [](Local<Value> value) { EXPECT_TRUE(value->IsUndefined()); }); | 159 [](Local<Value> value) { EXPECT_TRUE(value->IsUndefined()); }); |
158 DecodeTest({0x54, 0x00}, | 160 DecodeTest({0x54, 0x00}, |
159 [](Local<Value> value) { EXPECT_TRUE(value->IsTrue()); }); | 161 [](Local<Value> value) { EXPECT_TRUE(value->IsTrue()); }); |
160 DecodeTest({0x46, 0x00}, | 162 DecodeTest({0x46, 0x00}, |
161 [](Local<Value> value) { EXPECT_TRUE(value->IsFalse()); }); | 163 [](Local<Value> value) { EXPECT_TRUE(value->IsFalse()); }); |
162 DecodeTest({0x30, 0x00}, | 164 DecodeTest({0x30, 0x00}, |
163 [](Local<Value> value) { EXPECT_TRUE(value->IsNull()); }); | 165 [](Local<Value> value) { EXPECT_TRUE(value->IsNull()); }); |
164 } | 166 } |
165 | 167 |
| 168 TEST_F(ValueSerializerTest, RoundTripNumber) { |
| 169 RoundTripTest([this]() { return Integer::New(isolate(), 42); }, |
| 170 [](Local<Value> value) { |
| 171 ASSERT_TRUE(value->IsInt32()); |
| 172 EXPECT_EQ(42, Int32::Cast(*value)->Value()); |
| 173 }); |
| 174 RoundTripTest([this]() { return Integer::New(isolate(), -31337); }, |
| 175 [](Local<Value> value) { |
| 176 ASSERT_TRUE(value->IsInt32()); |
| 177 EXPECT_EQ(-31337, Int32::Cast(*value)->Value()); |
| 178 }); |
| 179 RoundTripTest( |
| 180 [this]() { |
| 181 return Integer::New(isolate(), std::numeric_limits<int32_t>::min()); |
| 182 }, |
| 183 [](Local<Value> value) { |
| 184 ASSERT_TRUE(value->IsInt32()); |
| 185 EXPECT_EQ(std::numeric_limits<int32_t>::min(), |
| 186 Int32::Cast(*value)->Value()); |
| 187 }); |
| 188 RoundTripTest([this]() { return Number::New(isolate(), -0.25); }, |
| 189 [](Local<Value> value) { |
| 190 ASSERT_TRUE(value->IsNumber()); |
| 191 EXPECT_EQ(-0.25, Number::Cast(*value)->Value()); |
| 192 }); |
| 193 RoundTripTest( |
| 194 [this]() { |
| 195 return Number::New(isolate(), std::numeric_limits<double>::quiet_NaN()); |
| 196 }, |
| 197 [](Local<Value> value) { |
| 198 ASSERT_TRUE(value->IsNumber()); |
| 199 EXPECT_TRUE(std::isnan(Number::Cast(*value)->Value())); |
| 200 }); |
| 201 } |
| 202 |
| 203 TEST_F(ValueSerializerTest, DecodeNumber) { |
| 204 // 42 zig-zag encoded (signed) |
| 205 DecodeTest({0xff, 0x09, 0x49, 0x54}, |
| 206 [](Local<Value> value) { |
| 207 ASSERT_TRUE(value->IsInt32()); |
| 208 EXPECT_EQ(42, Int32::Cast(*value)->Value()); |
| 209 }); |
| 210 // 42 varint encoded (unsigned) |
| 211 DecodeTest({0xff, 0x09, 0x55, 0x2a}, |
| 212 [](Local<Value> value) { |
| 213 ASSERT_TRUE(value->IsInt32()); |
| 214 EXPECT_EQ(42, Int32::Cast(*value)->Value()); |
| 215 }); |
| 216 // 160 zig-zag encoded (signed) |
| 217 DecodeTest({0xff, 0x09, 0x49, 0xc0, 0x02}, |
| 218 [](Local<Value> value) { |
| 219 ASSERT_TRUE(value->IsInt32()); |
| 220 ASSERT_EQ(160, Int32::Cast(*value)->Value()); |
| 221 }); |
| 222 // 160 varint encoded (unsigned) |
| 223 DecodeTest({0xff, 0x09, 0x55, 0xa0, 0x01}, |
| 224 [](Local<Value> value) { |
| 225 ASSERT_TRUE(value->IsInt32()); |
| 226 ASSERT_EQ(160, Int32::Cast(*value)->Value()); |
| 227 }); |
| 228 #if defined(V8_TARGET_LITTLE_ENDIAN) |
| 229 // IEEE 754 doubles, little-endian byte order |
| 230 DecodeTest({0xff, 0x09, 0x4e, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xd0, 0xbf}, |
| 231 [](Local<Value> value) { |
| 232 ASSERT_TRUE(value->IsNumber()); |
| 233 EXPECT_EQ(-0.25, Number::Cast(*value)->Value()); |
| 234 }); |
| 235 // quiet NaN |
| 236 DecodeTest({0xff, 0x09, 0x4e, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xf8, 0x7f}, |
| 237 [](Local<Value> value) { |
| 238 ASSERT_TRUE(value->IsNumber()); |
| 239 EXPECT_TRUE(std::isnan(Number::Cast(*value)->Value())); |
| 240 }); |
| 241 // signaling NaN |
| 242 DecodeTest({0xff, 0x09, 0x4e, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xf4, 0x7f}, |
| 243 [](Local<Value> value) { |
| 244 ASSERT_TRUE(value->IsNumber()); |
| 245 EXPECT_TRUE(std::isnan(Number::Cast(*value)->Value())); |
| 246 }); |
| 247 #endif |
| 248 // TODO(jbroman): Equivalent test for big-endian machines. |
| 249 } |
| 250 |
166 } // namespace | 251 } // namespace |
167 } // namespace v8 | 252 } // namespace v8 |
OLD | NEW |