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

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

Issue 1746063003: [wasm] Add support for 64-bit LEB encodings. (Closed) Base URL: https://chromium.googlesource.com/v8/v8.git@master
Patch Set: Created 4 years, 9 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/decoder.h ('K') | « src/wasm/decoder.h ('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 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 "test/unittests/test-utils.h" 5 #include "test/unittests/test-utils.h"
6 6
7 #include "src/wasm/decoder.h" 7 #include "src/wasm/decoder.h"
8 #include "src/wasm/wasm-macro-gen.h" 8 #include "src/wasm/wasm-macro-gen.h"
9 9
10 namespace v8 { 10 namespace v8 {
(...skipping 20 matching lines...) Expand all
31 #define CHECK_INT32V_INLINE(expected, expected_length, ...) \ 31 #define CHECK_INT32V_INLINE(expected, expected_length, ...) \
32 do { \ 32 do { \
33 const byte data[] = {__VA_ARGS__}; \ 33 const byte data[] = {__VA_ARGS__}; \
34 decoder.Reset(data, data + sizeof(data)); \ 34 decoder.Reset(data, data + sizeof(data)); \
35 int length; \ 35 int length; \
36 EXPECT_EQ(expected, \ 36 EXPECT_EQ(expected, \
37 decoder.checked_read_i32v(decoder.start(), 0, &length)); \ 37 decoder.checked_read_i32v(decoder.start(), 0, &length)); \
38 EXPECT_EQ(expected_length, length); \ 38 EXPECT_EQ(expected_length, length); \
39 } while (false) 39 } while (false)
40 40
41 #define CHECK_UINT64V_INLINE(expected, expected_length, ...) \
42 do { \
43 const byte data[] = {__VA_ARGS__}; \
44 decoder.Reset(data, data + sizeof(data)); \
45 int length; \
46 EXPECT_EQ(expected, \
47 decoder.checked_read_u64v(decoder.start(), 0, &length)); \
48 EXPECT_EQ(expected_length, length); \
49 } while (false)
50
51 #define CHECK_INT64V_INLINE(expected, expected_length, ...) \
52 do { \
53 const byte data[] = {__VA_ARGS__}; \
54 decoder.Reset(data, data + sizeof(data)); \
55 int length; \
56 EXPECT_EQ(expected, \
57 decoder.checked_read_i64v(decoder.start(), 0, &length)); \
58 EXPECT_EQ(expected_length, length); \
59 } while (false)
60
41 TEST_F(DecoderTest, ReadU32v_OneByte) { 61 TEST_F(DecoderTest, ReadU32v_OneByte) {
42 CHECK_UINT32V_INLINE(0, 1, 0); 62 CHECK_UINT32V_INLINE(0, 1, 0);
43 CHECK_UINT32V_INLINE(5, 1, 5); 63 CHECK_UINT32V_INLINE(5, 1, 5);
44 CHECK_UINT32V_INLINE(7, 1, 7); 64 CHECK_UINT32V_INLINE(7, 1, 7);
45 CHECK_UINT32V_INLINE(9, 1, 9); 65 CHECK_UINT32V_INLINE(9, 1, 9);
46 CHECK_UINT32V_INLINE(37, 1, 37); 66 CHECK_UINT32V_INLINE(37, 1, 37);
47 CHECK_UINT32V_INLINE(69, 1, 69); 67 CHECK_UINT32V_INLINE(69, 1, 69);
48 CHECK_UINT32V_INLINE(110, 1, 110); 68 CHECK_UINT32V_INLINE(110, 1, 110);
49 CHECK_UINT32V_INLINE(125, 1, 125); 69 CHECK_UINT32V_INLINE(125, 1, 125);
50 CHECK_UINT32V_INLINE(126, 1, 126); 70 CHECK_UINT32V_INLINE(126, 1, 126);
(...skipping 350 matching lines...) Expand 10 before | Expand all | Expand 10 after
401 for (int i = 1; i < 16; i++) { 421 for (int i = 1; i < 16; i++) {
402 data[4] = static_cast<byte>(i << 4); 422 data[4] = static_cast<byte>(i << 4);
403 int length = 0; 423 int length = 0;
404 decoder.Reset(data, data + sizeof(data)); 424 decoder.Reset(data, data + sizeof(data));
405 decoder.checked_read_u32v(decoder.start(), 0, &length); 425 decoder.checked_read_u32v(decoder.start(), 0, &length);
406 EXPECT_EQ(5, length); 426 EXPECT_EQ(5, length);
407 EXPECT_FALSE(decoder.ok()); 427 EXPECT_FALSE(decoder.ok());
408 } 428 }
409 } 429 }
410 430
431 TEST_F(DecoderTest, ReadU32v_Bits) {
432 // A more exhaustive test.
433 const int kMaxSize = 5;
434 const uint32_t kVals[] = {
435 0xaabbccdd, 0x11223344, 0x33445566, 0xffeeddcc, 0xF0F0F0F0, 0x0F0F0F0F,
436 0xEEEEEEEE, 0xAAAAAAAA, 0x12345678, 0x9abcdef0, 0x80309488, 0x729ed997,
437 0xc4a0cf81, 0x16c6eb85, 0x4206db8e, 0xf3b089d5, 0xaa2e223e, 0xf99e29c8,
438 0x4a4357d8, 0x1890b1c1, 0x8d80a085, 0xacb6ae4c, 0x1b827e10, 0xeb5c7bd9,
439 0xbb1bc146, 0xdf57a33l};
440 byte data[kMaxSize];
441
442 // foreach value in above array
443 for (size_t v = 0; v < arraysize(kVals); v++) {
444 // foreach length 1...64
binji 2016/03/01 18:49:40 32
titzer 2016/03/01 19:09:19 Done.
445 for (int i = 1; i <= 32; i++) {
446 const uint32_t val = kVals[v] & ((1ull << (i - 1)) - 1);
binji 2016/03/01 18:49:40 Am I reading this wrong, or does this give the wro
titzer 2016/03/01 19:09:19 Good catch. It would strip off one extra high bit.
447
448 int length = 1 + i / 7;
449 for (int j = 0; j < kMaxSize; j++) {
450 data[j] = (val >> (7 * j)) & MASK_7;
ahaas 2016/03/01 08:22:58 static_cast?
titzer 2016/03/01 18:49:12 Done.
451 }
452 for (int j = 0; j < length - 1; j++) {
453 data[j] |= 0x80;
454 }
455
456 // foreach buffer size 0...5
457 for (int limit = 0; limit <= kMaxSize; limit++) {
458 decoder.Reset(data, data + limit);
459 int rlen;
460 uint32_t result = decoder.checked_read_u32v(data, 0, &rlen);
461 if (limit < length) {
462 EXPECT_FALSE(decoder.ok());
463 } else {
464 EXPECT_TRUE(decoder.ok());
465 EXPECT_EQ(val, result);
466 EXPECT_EQ(length, rlen);
467 }
468 }
469 }
470 }
471 }
472
473 TEST_F(DecoderTest, ReadU64v_OneByte) {
474 CHECK_UINT64V_INLINE(0, 1, 0);
475 CHECK_UINT64V_INLINE(6, 1, 6);
476 CHECK_UINT64V_INLINE(8, 1, 8);
477 CHECK_UINT64V_INLINE(12, 1, 12);
478 CHECK_UINT64V_INLINE(33, 1, 33);
479 CHECK_UINT64V_INLINE(59, 1, 59);
480 CHECK_UINT64V_INLINE(110, 1, 110);
481 CHECK_UINT64V_INLINE(125, 1, 125);
482 CHECK_UINT64V_INLINE(126, 1, 126);
483 CHECK_UINT64V_INLINE(127, 1, 127);
484 }
485
486 TEST_F(DecoderTest, ReadI64v_TwoByte) {
ahaas 2016/03/01 08:22:58 Should this be ReadI64v_OneByte?
titzer 2016/03/01 18:49:12 Done.
487 CHECK_INT64V_INLINE(0, 1, 0);
488 CHECK_INT64V_INLINE(4, 1, 4);
489 CHECK_INT64V_INLINE(6, 1, 6);
490 CHECK_INT64V_INLINE(9, 1, 9);
491 CHECK_INT64V_INLINE(33, 1, 33);
492 CHECK_INT64V_INLINE(61, 1, 61);
493 CHECK_INT64V_INLINE(63, 1, 63);
494
495 CHECK_INT64V_INLINE(-1, 1, 127);
496 CHECK_INT64V_INLINE(-2, 1, 126);
497 CHECK_INT64V_INLINE(-11, 1, 117);
498 CHECK_INT64V_INLINE(-62, 1, 66);
499 CHECK_INT64V_INLINE(-63, 1, 65);
500 CHECK_INT64V_INLINE(-64, 1, 64);
501 }
502
503 TEST_F(DecoderTest, ReadU64v_PowerOf2) {
504 const int kMaxSize = 10;
505 byte data[kMaxSize];
506
507 for (int i = 0; i < 64; i++) {
508 const uint64_t val = 1ull << i;
509 int index = i / 7;
510 data[index] = 1 << (i % 7);
511 memset(data, 0x80, index);
512
513 for (int limit = 0; limit <= kMaxSize; limit++) {
514 decoder.Reset(data, data + limit);
515 int length;
516 uint64_t result = decoder.checked_read_u64v(data, 0, &length);
517 if (limit <= index) {
518 EXPECT_FALSE(decoder.ok());
519 } else {
520 EXPECT_TRUE(decoder.ok());
521 EXPECT_EQ(val, result);
522 EXPECT_EQ(index + 1, length);
523 }
524 }
525 }
526 }
527
528 TEST_F(DecoderTest, ReadU64v_Bits) {
529 const int kMaxSize = 10;
530 const uint64_t kVals[] = {
531 0xaabbccdd11223344ull, 0x33445566ffeeddccull, 0xF0F0F0F0F0F0F0F0ull,
532 0x0F0F0F0F0F0F0F0Full, 0xEEEEEEEEEEEEEEEEull, 0xAAAAAAAAAAAAAAAAull,
533 0x123456789abcdef0ull, 0x80309488729ed997ull, 0xc4a0cf8116c6eb85ull,
534 0x4206db8ef3b089d5ull, 0xaa2e223ef99e29c8ull, 0x4a4357d81890b1c1ull,
535 0x8d80a085acb6ae4cull, 0x1b827e10eb5c7bd9ull, 0xbb1bc146df57a338ull};
536 byte data[kMaxSize];
537
538 // foreach value in above array
539 for (size_t v = 0; v < arraysize(kVals); v++) {
540 // foreach length 1...64
541 for (int i = 1; i <= 64; i++) {
542 const uint64_t val = (kVals[v] << (64 - i)) >> (64 - i);
543
544 int length = 1 + i / 7;
545 for (int j = 0; j < kMaxSize; j++) {
546 data[j] = static_cast<byte>((val >> (7 * j)) & MASK_7);
547 }
548 for (int j = 0; j < length - 1; j++) {
549 data[j] |= 0x80;
550 }
551
552 // foreach buffer size 0...10
553 for (int limit = 0; limit <= kMaxSize; limit++) {
554 decoder.Reset(data, data + limit);
555 int rlen;
556 uint64_t result = decoder.checked_read_u64v(data, 0, &rlen);
557 if (limit < length) {
558 EXPECT_FALSE(decoder.ok());
559 } else {
560 EXPECT_TRUE(decoder.ok());
561 EXPECT_EQ(val, result);
562 EXPECT_EQ(length, rlen);
563 }
564 }
565 }
566 }
567 }
568
569 TEST_F(DecoderTest, ReadI64v_Bits) {
ahaas 2016/03/01 08:22:58 Would it be possible to write one test function fo
titzer 2016/03/01 19:09:19 Too much mechanism I think.
570 const int kMaxSize = 10;
571 // Exhaustive signedness test.
572 const uint64_t kVals[] = {
573 0xaabbccdd11223344ull, 0x33445566ffeeddccull, 0xF0F0F0F0F0F0F0F0ull,
574 0x0F0F0F0F0F0F0F0Full, 0xEEEEEEEEEEEEEEEEull, 0xAAAAAAAAAAAAAAAAull,
575 0x123456789abcdef0ull, 0x80309488729ed997ull, 0xc4a0cf8116c6eb85ull,
576 0x4206db8ef3b089d5ull, 0xaa2e223ef99e29c8ull, 0x4a4357d81890b1c1ull,
577 0x8d80a085acb6ae4cull, 0x1b827e10eb5c7bd9ull, 0xbb1bc146df57a338ull};
578 byte data[kMaxSize];
579
580 // foreach value in above array
581 for (size_t v = 0; v < arraysize(kVals); v++) {
582 // foreach length 1...64
583 for (int i = 1; i <= 64; i++) {
584 const int64_t val = bit_cast<int64_t>(kVals[v] << (64 - i)) >> (64 - i);
585
586 int length = 1 + i / 7;
587 for (int j = 0; j < kMaxSize; j++) {
588 const uint64_t uval = bit_cast<uint64_t>(val);
589 data[j] = static_cast<byte>((uval >> (7 * j)) & MASK_7);
590 }
591 for (int j = 0; j < length - 1; j++) {
592 data[j] |= 0x80;
593 }
594
595 // foreach buffer size 0...10
596 for (int limit = 0; limit <= kMaxSize; limit++) {
597 decoder.Reset(data, data + limit);
598 int rlen;
599 int64_t result = decoder.checked_read_i64v(data, 0, &rlen);
600 if (limit < length) {
601 EXPECT_FALSE(decoder.ok());
602 } else {
603 EXPECT_TRUE(decoder.ok());
604 EXPECT_EQ(val, result);
605 EXPECT_EQ(length, rlen);
606 }
607 }
608 }
609 }
610 }
611
612 TEST_F(DecoderTest, ReadU64v_extra_bits) {
613 byte data[] = {0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x00};
614 for (int i = 1; i < 128; i++) {
615 data[9] = static_cast<byte>(i << 1);
616 int length = 0;
617 decoder.Reset(data, data + sizeof(data));
618 decoder.checked_read_u64v(decoder.start(), 0, &length);
619 EXPECT_EQ(10, length);
620 EXPECT_FALSE(decoder.ok());
621 }
622 }
623
411 } // namespace wasm 624 } // namespace wasm
412 } // namespace internal 625 } // namespace internal
413 } // namespace v8 626 } // namespace v8
OLDNEW
« src/wasm/decoder.h ('K') | « src/wasm/decoder.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698