2016-04-21 10:14:34 +00:00
|
|
|
// Copyright 2016 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 "test/unittests/test-utils.h"
|
|
|
|
|
2016-06-21 12:52:57 +00:00
|
|
|
#include "src/objects-inl.h"
|
2016-04-21 10:14:34 +00:00
|
|
|
#include "src/wasm/decoder.h"
|
|
|
|
#include "src/wasm/leb-helper.h"
|
|
|
|
|
|
|
|
namespace v8 {
|
|
|
|
namespace internal {
|
|
|
|
namespace wasm {
|
|
|
|
|
|
|
|
class LEBHelperTest : public TestWithZone {};
|
|
|
|
|
|
|
|
TEST_F(LEBHelperTest, sizeof_u32v) {
|
2016-11-11 11:55:30 +00:00
|
|
|
EXPECT_EQ(1u, LEBHelper::sizeof_u32v(0));
|
|
|
|
EXPECT_EQ(1u, LEBHelper::sizeof_u32v(1));
|
|
|
|
EXPECT_EQ(1u, LEBHelper::sizeof_u32v(3));
|
2016-04-21 10:14:34 +00:00
|
|
|
|
|
|
|
for (uint32_t i = 4; i < 128; i++) {
|
2016-11-11 11:55:30 +00:00
|
|
|
EXPECT_EQ(1u, LEBHelper::sizeof_u32v(i));
|
2016-04-21 10:14:34 +00:00
|
|
|
}
|
|
|
|
|
2016-11-10 12:50:51 +00:00
|
|
|
for (uint32_t i = (1u << 7); i < (1u << 9); i++) {
|
2016-11-11 11:55:30 +00:00
|
|
|
EXPECT_EQ(2u, LEBHelper::sizeof_u32v(i));
|
2016-04-21 10:14:34 +00:00
|
|
|
}
|
|
|
|
|
2016-11-10 12:50:51 +00:00
|
|
|
for (uint32_t i = (1u << 14); i < (1u << 16); i += 33) {
|
2016-11-11 11:55:30 +00:00
|
|
|
EXPECT_EQ(3u, LEBHelper::sizeof_u32v(i));
|
2016-04-21 10:14:34 +00:00
|
|
|
}
|
|
|
|
|
2016-11-10 12:50:51 +00:00
|
|
|
for (uint32_t i = (1u << 21); i < (1u << 24); i += 33999) {
|
2016-11-11 11:55:30 +00:00
|
|
|
EXPECT_EQ(4u, LEBHelper::sizeof_u32v(i));
|
2016-04-21 10:14:34 +00:00
|
|
|
}
|
|
|
|
|
2016-11-10 12:50:51 +00:00
|
|
|
for (uint32_t i = (1u << 28); i < (1u << 31); i += 33997779u) {
|
2016-11-11 11:55:30 +00:00
|
|
|
EXPECT_EQ(5u, LEBHelper::sizeof_u32v(i));
|
2016-04-21 10:14:34 +00:00
|
|
|
}
|
|
|
|
|
2016-11-11 11:55:30 +00:00
|
|
|
EXPECT_EQ(5u, LEBHelper::sizeof_u32v(0xFFFFFFFF));
|
2016-04-21 10:14:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(LEBHelperTest, sizeof_i32v) {
|
2016-11-11 11:55:30 +00:00
|
|
|
EXPECT_EQ(1u, LEBHelper::sizeof_i32v(0));
|
|
|
|
EXPECT_EQ(1u, LEBHelper::sizeof_i32v(1));
|
|
|
|
EXPECT_EQ(1u, LEBHelper::sizeof_i32v(3));
|
2016-04-21 10:14:34 +00:00
|
|
|
|
|
|
|
for (int32_t i = 0; i < (1 << 6); i++) {
|
2016-11-11 11:55:30 +00:00
|
|
|
EXPECT_EQ(1u, LEBHelper::sizeof_i32v(i));
|
2016-04-21 10:14:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for (int32_t i = (1 << 6); i < (1 << 8); i++) {
|
2016-11-11 11:55:30 +00:00
|
|
|
EXPECT_EQ(2u, LEBHelper::sizeof_i32v(i));
|
2016-04-21 10:14:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for (int32_t i = (1 << 13); i < (1 << 15); i += 31) {
|
2016-11-11 11:55:30 +00:00
|
|
|
EXPECT_EQ(3u, LEBHelper::sizeof_i32v(i));
|
2016-04-21 10:14:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for (int32_t i = (1 << 20); i < (1 << 22); i += 31991) {
|
2016-11-11 11:55:30 +00:00
|
|
|
EXPECT_EQ(4u, LEBHelper::sizeof_i32v(i));
|
2016-04-21 10:14:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for (int32_t i = (1 << 27); i < (1 << 29); i += 3199893) {
|
2016-11-11 11:55:30 +00:00
|
|
|
EXPECT_EQ(5u, LEBHelper::sizeof_i32v(i));
|
2016-04-21 10:14:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for (int32_t i = -(1 << 6); i <= 0; i++) {
|
2016-11-11 11:55:30 +00:00
|
|
|
EXPECT_EQ(1u, LEBHelper::sizeof_i32v(i));
|
2016-04-21 10:14:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for (int32_t i = -(1 << 13); i < -(1 << 6); i++) {
|
2016-11-11 11:55:30 +00:00
|
|
|
EXPECT_EQ(2u, LEBHelper::sizeof_i32v(i));
|
2016-04-21 10:14:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for (int32_t i = -(1 << 20); i < -(1 << 18); i += 11) {
|
2016-11-11 11:55:30 +00:00
|
|
|
EXPECT_EQ(3u, LEBHelper::sizeof_i32v(i));
|
2016-04-21 10:14:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for (int32_t i = -(1 << 27); i < -(1 << 25); i += 11999) {
|
2016-11-11 11:55:30 +00:00
|
|
|
EXPECT_EQ(4u, LEBHelper::sizeof_i32v(i));
|
2016-04-21 10:14:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for (int32_t i = -(1 << 30); i < -(1 << 28); i += 1199999) {
|
2016-11-11 11:55:30 +00:00
|
|
|
EXPECT_EQ(5u, LEBHelper::sizeof_i32v(i));
|
2016-04-21 10:14:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-16 16:27:54 +00:00
|
|
|
#define DECLARE_ENCODE_DECODE_CHECKER(ctype, name) \
|
|
|
|
static void CheckEncodeDecode_##name(ctype val) { \
|
|
|
|
static const int kSize = 16; \
|
|
|
|
static byte buffer[kSize]; \
|
|
|
|
byte* ptr = buffer; \
|
|
|
|
LEBHelper::write_##name(&ptr, val); \
|
|
|
|
EXPECT_EQ(LEBHelper::sizeof_##name(val), \
|
|
|
|
static_cast<size_t>(ptr - buffer)); \
|
|
|
|
Decoder decoder(buffer, buffer + kSize); \
|
|
|
|
unsigned length = 0; \
|
|
|
|
ctype result = decoder.read_##name<Decoder::kNoValidate>(buffer, &length); \
|
|
|
|
EXPECT_EQ(val, result); \
|
|
|
|
EXPECT_EQ(LEBHelper::sizeof_##name(val), static_cast<size_t>(length)); \
|
2016-04-21 10:14:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
DECLARE_ENCODE_DECODE_CHECKER(int32_t, i32v)
|
|
|
|
DECLARE_ENCODE_DECODE_CHECKER(uint32_t, u32v)
|
|
|
|
DECLARE_ENCODE_DECODE_CHECKER(int64_t, i64v)
|
|
|
|
DECLARE_ENCODE_DECODE_CHECKER(uint64_t, u64v)
|
|
|
|
|
2017-09-08 13:59:05 +00:00
|
|
|
#undef DECLARE_ENCODE_DECODE_CHECKER
|
|
|
|
|
2016-04-21 10:14:34 +00:00
|
|
|
TEST_F(LEBHelperTest, WriteAndDecode_u32v) {
|
|
|
|
CheckEncodeDecode_u32v(0);
|
|
|
|
CheckEncodeDecode_u32v(1);
|
|
|
|
CheckEncodeDecode_u32v(5);
|
|
|
|
CheckEncodeDecode_u32v(99);
|
|
|
|
CheckEncodeDecode_u32v(298);
|
|
|
|
CheckEncodeDecode_u32v(87348723);
|
|
|
|
CheckEncodeDecode_u32v(77777);
|
|
|
|
|
|
|
|
for (uint32_t val = 0x3a; val != 0; val = val << 1) {
|
|
|
|
CheckEncodeDecode_u32v(val);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(LEBHelperTest, WriteAndDecode_i32v) {
|
|
|
|
CheckEncodeDecode_i32v(0);
|
|
|
|
CheckEncodeDecode_i32v(1);
|
|
|
|
CheckEncodeDecode_i32v(5);
|
|
|
|
CheckEncodeDecode_i32v(99);
|
|
|
|
CheckEncodeDecode_i32v(298);
|
|
|
|
CheckEncodeDecode_i32v(87348723);
|
|
|
|
CheckEncodeDecode_i32v(77777);
|
|
|
|
|
|
|
|
CheckEncodeDecode_i32v(-2);
|
|
|
|
CheckEncodeDecode_i32v(-4);
|
|
|
|
CheckEncodeDecode_i32v(-59);
|
|
|
|
CheckEncodeDecode_i32v(-288);
|
|
|
|
CheckEncodeDecode_i32v(-12608);
|
|
|
|
CheckEncodeDecode_i32v(-87328723);
|
|
|
|
CheckEncodeDecode_i32v(-77377);
|
|
|
|
|
|
|
|
for (uint32_t val = 0x3a; val != 0; val = val << 1) {
|
|
|
|
CheckEncodeDecode_i32v(bit_cast<int32_t>(val));
|
|
|
|
}
|
|
|
|
|
|
|
|
for (uint32_t val = 0xFFFFFF3B; val != 0; val = val << 1) {
|
|
|
|
CheckEncodeDecode_i32v(bit_cast<int32_t>(val));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(LEBHelperTest, WriteAndDecode_u64v) {
|
|
|
|
CheckEncodeDecode_u64v(0);
|
|
|
|
CheckEncodeDecode_u64v(1);
|
|
|
|
CheckEncodeDecode_u64v(5);
|
|
|
|
CheckEncodeDecode_u64v(99);
|
|
|
|
CheckEncodeDecode_u64v(298);
|
|
|
|
CheckEncodeDecode_u64v(87348723);
|
|
|
|
CheckEncodeDecode_u64v(77777);
|
|
|
|
|
|
|
|
for (uint64_t val = 0x3a; val != 0; val = val << 1) {
|
|
|
|
CheckEncodeDecode_u64v(val);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(LEBHelperTest, WriteAndDecode_i64v) {
|
|
|
|
CheckEncodeDecode_i64v(0);
|
|
|
|
CheckEncodeDecode_i64v(1);
|
|
|
|
CheckEncodeDecode_i64v(5);
|
|
|
|
CheckEncodeDecode_i64v(99);
|
|
|
|
CheckEncodeDecode_i64v(298);
|
|
|
|
CheckEncodeDecode_i64v(87348723);
|
|
|
|
CheckEncodeDecode_i64v(77777);
|
|
|
|
|
|
|
|
CheckEncodeDecode_i64v(-2);
|
|
|
|
CheckEncodeDecode_i64v(-4);
|
|
|
|
CheckEncodeDecode_i64v(-59);
|
|
|
|
CheckEncodeDecode_i64v(-288);
|
|
|
|
CheckEncodeDecode_i64v(-87648723);
|
|
|
|
CheckEncodeDecode_i64v(-77377);
|
|
|
|
|
|
|
|
for (uint64_t val = 0x3a; val != 0; val = val << 1) {
|
|
|
|
CheckEncodeDecode_i64v(bit_cast<int64_t>(val));
|
|
|
|
}
|
|
|
|
|
|
|
|
for (uint64_t val = 0xFFFFFFFFFFFFFF3B; val != 0; val = val << 1) {
|
|
|
|
CheckEncodeDecode_i64v(bit_cast<int64_t>(val));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} // namespace wasm
|
|
|
|
} // namespace internal
|
|
|
|
} // namespace v8
|