OLD | NEW |
(Empty) | |
| 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 "test/unittests/test-utils.h" |
| 6 |
| 7 #include "src/wasm/decoder.h" |
| 8 #include "src/wasm/leb-helper.h" |
| 9 |
| 10 namespace v8 { |
| 11 namespace internal { |
| 12 namespace wasm { |
| 13 |
| 14 class LEBHelperTest : public TestWithZone {}; |
| 15 |
| 16 TEST_F(LEBHelperTest, sizeof_u32v) { |
| 17 EXPECT_EQ(1, LEBHelper::sizeof_u32v(0)); |
| 18 EXPECT_EQ(1, LEBHelper::sizeof_u32v(1)); |
| 19 EXPECT_EQ(1, LEBHelper::sizeof_u32v(3)); |
| 20 |
| 21 for (uint32_t i = 4; i < 128; i++) { |
| 22 EXPECT_EQ(1, LEBHelper::sizeof_u32v(i)); |
| 23 } |
| 24 |
| 25 for (uint32_t i = (1 << 7); i < (1 << 9); i++) { |
| 26 EXPECT_EQ(2, LEBHelper::sizeof_u32v(i)); |
| 27 } |
| 28 |
| 29 for (uint32_t i = (1 << 14); i < (1 << 16); i += 33) { |
| 30 EXPECT_EQ(3, LEBHelper::sizeof_u32v(i)); |
| 31 } |
| 32 |
| 33 for (uint32_t i = (1 << 21); i < (1 << 24); i += 33999) { |
| 34 EXPECT_EQ(4, LEBHelper::sizeof_u32v(i)); |
| 35 } |
| 36 |
| 37 for (uint32_t i = (1 << 28); i < (1 << 31); i += 33997779) { |
| 38 EXPECT_EQ(5, LEBHelper::sizeof_u32v(i)); |
| 39 } |
| 40 |
| 41 EXPECT_EQ(5, LEBHelper::sizeof_u32v(0xFFFFFFFF)); |
| 42 } |
| 43 |
| 44 TEST_F(LEBHelperTest, sizeof_i32v) { |
| 45 EXPECT_EQ(1, LEBHelper::sizeof_i32v(0)); |
| 46 EXPECT_EQ(1, LEBHelper::sizeof_i32v(1)); |
| 47 EXPECT_EQ(1, LEBHelper::sizeof_i32v(3)); |
| 48 |
| 49 for (int32_t i = 0; i < (1 << 6); i++) { |
| 50 EXPECT_EQ(1, LEBHelper::sizeof_i32v(i)); |
| 51 } |
| 52 |
| 53 for (int32_t i = (1 << 6); i < (1 << 8); i++) { |
| 54 EXPECT_EQ(2, LEBHelper::sizeof_i32v(i)); |
| 55 } |
| 56 |
| 57 for (int32_t i = (1 << 13); i < (1 << 15); i += 31) { |
| 58 EXPECT_EQ(3, LEBHelper::sizeof_i32v(i)); |
| 59 } |
| 60 |
| 61 for (int32_t i = (1 << 20); i < (1 << 22); i += 31991) { |
| 62 EXPECT_EQ(4, LEBHelper::sizeof_i32v(i)); |
| 63 } |
| 64 |
| 65 for (int32_t i = (1 << 27); i < (1 << 29); i += 3199893) { |
| 66 EXPECT_EQ(5, LEBHelper::sizeof_i32v(i)); |
| 67 } |
| 68 |
| 69 for (int32_t i = -(1 << 6); i <= 0; i++) { |
| 70 EXPECT_EQ(1, LEBHelper::sizeof_i32v(i)); |
| 71 } |
| 72 |
| 73 for (int32_t i = -(1 << 13); i < -(1 << 6); i++) { |
| 74 EXPECT_EQ(2, LEBHelper::sizeof_i32v(i)); |
| 75 } |
| 76 |
| 77 for (int32_t i = -(1 << 20); i < -(1 << 18); i += 11) { |
| 78 EXPECT_EQ(3, LEBHelper::sizeof_i32v(i)); |
| 79 } |
| 80 |
| 81 for (int32_t i = -(1 << 27); i < -(1 << 25); i += 11999) { |
| 82 EXPECT_EQ(4, LEBHelper::sizeof_i32v(i)); |
| 83 } |
| 84 |
| 85 for (int32_t i = -(1 << 30); i < -(1 << 28); i += 1199999) { |
| 86 EXPECT_EQ(5, LEBHelper::sizeof_i32v(i)); |
| 87 } |
| 88 } |
| 89 |
| 90 #define DECLARE_ENCODE_DECODE_CHECKER(ctype, name) \ |
| 91 static void CheckEncodeDecode_##name(ctype val) { \ |
| 92 static const int kSize = 16; \ |
| 93 static byte buffer[kSize]; \ |
| 94 byte *ptr = buffer; \ |
| 95 LEBHelper::write_##name(&ptr, val); \ |
| 96 EXPECT_EQ(LEBHelper::sizeof_##name(val), \ |
| 97 static_cast<size_t>(ptr - buffer)); \ |
| 98 Decoder decoder(buffer, buffer + kSize); \ |
| 99 int length = 0; \ |
| 100 ctype result = decoder.checked_read_##name(buffer, 0, &length); \ |
| 101 EXPECT_EQ(val, result); \ |
| 102 EXPECT_EQ(LEBHelper::sizeof_##name(val), static_cast<size_t>(length)); \ |
| 103 } |
| 104 |
| 105 DECLARE_ENCODE_DECODE_CHECKER(int32_t, i32v) |
| 106 DECLARE_ENCODE_DECODE_CHECKER(uint32_t, u32v) |
| 107 DECLARE_ENCODE_DECODE_CHECKER(int64_t, i64v) |
| 108 DECLARE_ENCODE_DECODE_CHECKER(uint64_t, u64v) |
| 109 |
| 110 TEST_F(LEBHelperTest, WriteAndDecode_u32v) { |
| 111 CheckEncodeDecode_u32v(0); |
| 112 CheckEncodeDecode_u32v(1); |
| 113 CheckEncodeDecode_u32v(5); |
| 114 CheckEncodeDecode_u32v(99); |
| 115 CheckEncodeDecode_u32v(298); |
| 116 CheckEncodeDecode_u32v(87348723); |
| 117 CheckEncodeDecode_u32v(77777); |
| 118 |
| 119 for (uint32_t val = 0x3a; val != 0; val = val << 1) { |
| 120 CheckEncodeDecode_u32v(val); |
| 121 } |
| 122 } |
| 123 |
| 124 TEST_F(LEBHelperTest, WriteAndDecode_i32v) { |
| 125 CheckEncodeDecode_i32v(0); |
| 126 CheckEncodeDecode_i32v(1); |
| 127 CheckEncodeDecode_i32v(5); |
| 128 CheckEncodeDecode_i32v(99); |
| 129 CheckEncodeDecode_i32v(298); |
| 130 CheckEncodeDecode_i32v(87348723); |
| 131 CheckEncodeDecode_i32v(77777); |
| 132 |
| 133 CheckEncodeDecode_i32v(-2); |
| 134 CheckEncodeDecode_i32v(-4); |
| 135 CheckEncodeDecode_i32v(-59); |
| 136 CheckEncodeDecode_i32v(-288); |
| 137 CheckEncodeDecode_i32v(-12608); |
| 138 CheckEncodeDecode_i32v(-87328723); |
| 139 CheckEncodeDecode_i32v(-77377); |
| 140 |
| 141 for (uint32_t val = 0x3a; val != 0; val = val << 1) { |
| 142 CheckEncodeDecode_i32v(bit_cast<int32_t>(val)); |
| 143 } |
| 144 |
| 145 for (uint32_t val = 0xFFFFFF3B; val != 0; val = val << 1) { |
| 146 CheckEncodeDecode_i32v(bit_cast<int32_t>(val)); |
| 147 } |
| 148 } |
| 149 |
| 150 TEST_F(LEBHelperTest, WriteAndDecode_u64v) { |
| 151 CheckEncodeDecode_u64v(0); |
| 152 CheckEncodeDecode_u64v(1); |
| 153 CheckEncodeDecode_u64v(5); |
| 154 CheckEncodeDecode_u64v(99); |
| 155 CheckEncodeDecode_u64v(298); |
| 156 CheckEncodeDecode_u64v(87348723); |
| 157 CheckEncodeDecode_u64v(77777); |
| 158 |
| 159 for (uint64_t val = 0x3a; val != 0; val = val << 1) { |
| 160 CheckEncodeDecode_u64v(val); |
| 161 } |
| 162 } |
| 163 |
| 164 TEST_F(LEBHelperTest, WriteAndDecode_i64v) { |
| 165 CheckEncodeDecode_i64v(0); |
| 166 CheckEncodeDecode_i64v(1); |
| 167 CheckEncodeDecode_i64v(5); |
| 168 CheckEncodeDecode_i64v(99); |
| 169 CheckEncodeDecode_i64v(298); |
| 170 CheckEncodeDecode_i64v(87348723); |
| 171 CheckEncodeDecode_i64v(77777); |
| 172 |
| 173 CheckEncodeDecode_i64v(-2); |
| 174 CheckEncodeDecode_i64v(-4); |
| 175 CheckEncodeDecode_i64v(-59); |
| 176 CheckEncodeDecode_i64v(-288); |
| 177 CheckEncodeDecode_i64v(-87648723); |
| 178 CheckEncodeDecode_i64v(-77377); |
| 179 |
| 180 for (uint64_t val = 0x3a; val != 0; val = val << 1) { |
| 181 CheckEncodeDecode_i64v(bit_cast<int64_t>(val)); |
| 182 } |
| 183 |
| 184 for (uint64_t val = 0xFFFFFFFFFFFFFF3B; val != 0; val = val << 1) { |
| 185 CheckEncodeDecode_i64v(bit_cast<int64_t>(val)); |
| 186 } |
| 187 } |
| 188 } // namespace wasm |
| 189 } // namespace internal |
| 190 } // namespace v8 |
OLD | NEW |