Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1612)

Side by Side Diff: test/unittests/wasm/ast-decoder-unittest.cc

Issue 1856413002: [wasm] Refactor decoding of local declarations and make more robust. (Closed) Base URL: https://chromium.googlesource.com/v8/v8.git@master
Patch Set: Created 4 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« src/wasm/ast-decoder.cc ('K') | « src/wasm/ast-decoder.cc ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2015 the V8 project authors. All rights reserved. 1 // Copyright 2015 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 "test/unittests/test-utils.h" 5 #include "test/unittests/test-utils.h"
6 6
7 #include "src/v8.h" 7 #include "src/v8.h"
8 8
9 #include "test/cctest/wasm/test-signatures.h" 9 #include "test/cctest/wasm/test-signatures.h"
10 10
(...skipping 2179 matching lines...) Expand 10 before | Expand all | Expand 10 after
2190 EXPECT_ARITY(1, kExprF64SConvertI32); 2190 EXPECT_ARITY(1, kExprF64SConvertI32);
2191 EXPECT_ARITY(1, kExprF64UConvertI32); 2191 EXPECT_ARITY(1, kExprF64UConvertI32);
2192 EXPECT_ARITY(1, kExprF64SConvertI64); 2192 EXPECT_ARITY(1, kExprF64SConvertI64);
2193 EXPECT_ARITY(1, kExprF64UConvertI64); 2193 EXPECT_ARITY(1, kExprF64UConvertI64);
2194 EXPECT_ARITY(1, kExprF64ConvertF32); 2194 EXPECT_ARITY(1, kExprF64ConvertF32);
2195 EXPECT_ARITY(1, kExprF64ReinterpretI64); 2195 EXPECT_ARITY(1, kExprF64ReinterpretI64);
2196 EXPECT_ARITY(1, kExprI32ReinterpretF32); 2196 EXPECT_ARITY(1, kExprI32ReinterpretF32);
2197 EXPECT_ARITY(1, kExprI64ReinterpretF64); 2197 EXPECT_ARITY(1, kExprI64ReinterpretF64);
2198 } 2198 }
2199 2199
2200 typedef std::vector<LocalType>* LocalTypeMap; 2200 typedef ZoneVector<LocalType> LocalTypeMap;
2201 2201
2202 class LocalDeclDecoderTest : public TestWithZone { 2202 class LocalDeclDecoderTest : public TestWithZone {
2203 public: 2203 public:
2204 base::AccountingAllocator allocator;
2205
2204 size_t ExpectRun(LocalTypeMap map, size_t pos, LocalType expected, 2206 size_t ExpectRun(LocalTypeMap map, size_t pos, LocalType expected,
2205 size_t count) { 2207 size_t count) {
2206 for (size_t i = 0; i < count; i++) { 2208 for (size_t i = 0; i < count; i++) {
2207 EXPECT_EQ(expected, map->at(pos++)); 2209 EXPECT_EQ(expected, map[pos++]);
2208 } 2210 }
2209 return pos; 2211 return pos;
2210 } 2212 }
2213
2214 LocalTypeMap Expand(AstLocalDecls& decls) {
2215 ZoneVector<LocalType> map(zone());
2216 for (auto p : decls.local_types) {
2217 map.insert(map.end(), p.second, p.first);
2218 }
2219 return map;
2220 }
2211 }; 2221 };
2212 2222
2223 TEST_F(LocalDeclDecoderTest, EmptyLocals) {
2224 static const byte data[] = {};
2225 AstLocalDecls decls(zone());
2226 bool result = DecodeLocalDecls(decls, data, data + sizeof(data));
2227 EXPECT_FALSE(result);
2228 }
2229
2213 TEST_F(LocalDeclDecoderTest, NoLocals) { 2230 TEST_F(LocalDeclDecoderTest, NoLocals) {
2214 static const byte data[] = {0}; 2231 static const byte data[] = {0};
2215 base::AccountingAllocator allocator; 2232 AstLocalDecls decls(zone());
2216 LocalTypeMap map = 2233 bool result = DecodeLocalDecls(decls, data, data + sizeof(data));
2217 DecodeLocalDeclsForTesting(&allocator, data, data + sizeof(data)); 2234 EXPECT_TRUE(result);
2218 EXPECT_EQ(0, map->size()); 2235 EXPECT_EQ(0, decls.total_local_count);
2219 if (map) delete map;
2220 } 2236 }
2221 2237
2222 TEST_F(LocalDeclDecoderTest, OneLocal) { 2238 TEST_F(LocalDeclDecoderTest, OneLocal) {
2223 for (size_t i = 0; i < arraysize(kLocalTypes); i++) { 2239 for (size_t i = 0; i < arraysize(kLocalTypes); i++) {
2224 LocalType type = kLocalTypes[i]; 2240 LocalType type = kLocalTypes[i];
2225 const byte data[] = { 2241 const byte data[] = {
2226 1, 1, static_cast<byte>(WasmOpcodes::LocalTypeCodeFor(type))}; 2242 1, 1, static_cast<byte>(WasmOpcodes::LocalTypeCodeFor(type))};
2227 base::AccountingAllocator allocator; 2243 AstLocalDecls decls(zone());
2228 LocalTypeMap map = 2244 bool result = DecodeLocalDecls(decls, data, data + sizeof(data));
2229 DecodeLocalDeclsForTesting(&allocator, data, data + sizeof(data)); 2245 EXPECT_TRUE(result);
2230 EXPECT_EQ(1, map->size()); 2246 EXPECT_EQ(1, decls.total_local_count);
2231 EXPECT_EQ(type, map->at(0)); 2247
2232 if (map) delete map; 2248 LocalTypeMap map = Expand(decls);
2249 EXPECT_EQ(1, map.size());
2250 EXPECT_EQ(type, map.at(0));
ahaas 2016/04/05 17:04:30 Wouldn't map[0] also work here?
titzer 2016/04/05 17:14:23 Done.
2233 } 2251 }
2234 } 2252 }
2235 2253
2236 TEST_F(LocalDeclDecoderTest, FiveLocals) { 2254 TEST_F(LocalDeclDecoderTest, FiveLocals) {
2237 for (size_t i = 0; i < arraysize(kLocalTypes); i++) { 2255 for (size_t i = 0; i < arraysize(kLocalTypes); i++) {
2238 LocalType type = kLocalTypes[i]; 2256 LocalType type = kLocalTypes[i];
2239 const byte data[] = { 2257 const byte data[] = {
2240 1, 5, static_cast<byte>(WasmOpcodes::LocalTypeCodeFor(type))}; 2258 1, 5, static_cast<byte>(WasmOpcodes::LocalTypeCodeFor(type))};
2241 base::AccountingAllocator allocator; 2259 AstLocalDecls decls(zone());
2242 LocalTypeMap map = 2260 bool result = DecodeLocalDecls(decls, data, data + sizeof(data));
2243 DecodeLocalDeclsForTesting(&allocator, data, data + sizeof(data)); 2261 EXPECT_TRUE(result);
2244 EXPECT_EQ(5, map->size()); 2262 EXPECT_EQ(sizeof(data), decls.decls_encoded_size);
2263 EXPECT_EQ(5, decls.total_local_count);
2264
2265 LocalTypeMap map = Expand(decls);
2266 EXPECT_EQ(5, map.size());
2245 ExpectRun(map, 0, type, 5); 2267 ExpectRun(map, 0, type, 5);
2246 if (map) delete map;
2247 } 2268 }
2248 } 2269 }
2249 2270
2250 TEST_F(LocalDeclDecoderTest, MixedLocals) { 2271 TEST_F(LocalDeclDecoderTest, MixedLocals) {
2251 for (byte a = 0; a < 3; a++) { 2272 for (byte a = 0; a < 3; a++) {
2252 for (byte b = 0; b < 3; b++) { 2273 for (byte b = 0; b < 3; b++) {
2253 for (byte c = 0; c < 3; c++) { 2274 for (byte c = 0; c < 3; c++) {
2254 for (byte d = 0; d < 3; d++) { 2275 for (byte d = 0; d < 3; d++) {
2255 const byte data[] = {4, a, kLocalI32, b, kLocalI64, 2276 const byte data[] = {4, a, kLocalI32, b, kLocalI64,
2256 c, kLocalF32, d, kLocalF64}; 2277 c, kLocalF32, d, kLocalF64};
2257 base::AccountingAllocator allocator; 2278 AstLocalDecls decls(zone());
2258 LocalTypeMap map = 2279 bool result = DecodeLocalDecls(decls, data, data + sizeof(data));
2259 DecodeLocalDeclsForTesting(&allocator, data, data + sizeof(data)); 2280 EXPECT_TRUE(result);
2260 EXPECT_EQ(a + b + c + d, map->size()); 2281 EXPECT_EQ(sizeof(data), decls.decls_encoded_size);
2282 EXPECT_EQ(a + b + c + d, decls.total_local_count);
2283
2284 LocalTypeMap map = Expand(decls);
2285 EXPECT_EQ(a + b + c + d, map.size());
2261 2286
2262 size_t pos = 0; 2287 size_t pos = 0;
2263 pos = ExpectRun(map, pos, kAstI32, a); 2288 pos = ExpectRun(map, pos, kAstI32, a);
2264 pos = ExpectRun(map, pos, kAstI64, b); 2289 pos = ExpectRun(map, pos, kAstI64, b);
2265 pos = ExpectRun(map, pos, kAstF32, c); 2290 pos = ExpectRun(map, pos, kAstF32, c);
2266 pos = ExpectRun(map, pos, kAstF64, d); 2291 pos = ExpectRun(map, pos, kAstF64, d);
2267
2268 if (map) delete map;
2269 } 2292 }
2270 } 2293 }
2271 } 2294 }
2272 } 2295 }
2273 } 2296 }
2274 2297
2275 TEST_F(LocalDeclDecoderTest, UseEncoder) { 2298 TEST_F(LocalDeclDecoderTest, UseEncoder) {
2276 const byte* data = nullptr; 2299 const byte* data = nullptr;
2277 const byte* end = nullptr; 2300 const byte* end = nullptr;
2278 LocalDeclEncoder local_decls; 2301 LocalDeclEncoder local_decls;
2279 2302
2280 local_decls.AddLocals(5, kAstF32); 2303 local_decls.AddLocals(5, kAstF32);
2281 local_decls.AddLocals(1337, kAstI32); 2304 local_decls.AddLocals(1337, kAstI32);
2282 local_decls.AddLocals(212, kAstI64); 2305 local_decls.AddLocals(212, kAstI64);
2283 local_decls.Prepend(&data, &end); 2306 local_decls.Prepend(&data, &end);
2284 2307
2285 base::AccountingAllocator allocator; 2308 AstLocalDecls decls(zone());
2286 LocalTypeMap map = DecodeLocalDeclsForTesting(&allocator, data, end); 2309 bool result = DecodeLocalDecls(decls, data, end);
2310 EXPECT_TRUE(result);
2311 EXPECT_EQ(5 + 1337 + 212, decls.total_local_count);
2312
2313 LocalTypeMap map = Expand(decls);
2287 size_t pos = 0; 2314 size_t pos = 0;
2288 pos = ExpectRun(map, pos, kAstF32, 5); 2315 pos = ExpectRun(map, pos, kAstF32, 5);
2289 pos = ExpectRun(map, pos, kAstI32, 1337); 2316 pos = ExpectRun(map, pos, kAstI32, 1337);
2290 pos = ExpectRun(map, pos, kAstI64, 212); 2317 pos = ExpectRun(map, pos, kAstI64, 212);
2291
2292 if (map) delete map;
2293 delete[] data; 2318 delete[] data;
2294 } 2319 }
2295 2320
2296 } // namespace wasm 2321 } // namespace wasm
2297 } // namespace internal 2322 } // namespace internal
2298 } // namespace v8 2323 } // namespace v8
OLDNEW
« src/wasm/ast-decoder.cc ('K') | « src/wasm/ast-decoder.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698