1 // Copyright (c) 2016 The WebM project authors. All Rights Reserved.
2 //
3 // Use of this source code is governed by a BSD-style license
4 // that can be found in the LICENSE file in the root of the source
5 // tree. An additional intellectual property rights grant can be found
6 // in the file PATENTS. All contributing project authors may
7 // be found in the AUTHORS file in the root of the source tree.
8 #include "src/int_parser.h"
9
10 #include "gtest/gtest.h"
11
12 #include "test_utils/element_parser_test.h"
13 #include "webm/status.h"
14
15 using webm::ElementParserTest;
16 using webm::kUnknownElementSize;
17 using webm::SignedIntParser;
18 using webm::Status;
19 using webm::UnsignedIntParser;
20
21 namespace {
22
23 class UnsignedIntParserTest : public ElementParserTest<UnsignedIntParser> {};
24
TEST_F(UnsignedIntParserTest,UnsignedInvalidSize)25 TEST_F(UnsignedIntParserTest, UnsignedInvalidSize) {
26 TestInit(9, Status::kInvalidElementSize);
27 TestInit(kUnknownElementSize, Status::kInvalidElementSize);
28 }
29
TEST_F(UnsignedIntParserTest,UnsignedCustomDefault)30 TEST_F(UnsignedIntParserTest, UnsignedCustomDefault) {
31 ResetParser(1);
32
33 ParseAndVerify();
34
35 EXPECT_EQ(static_cast<std::uint64_t>(1), parser_.value());
36 }
37
TEST_F(UnsignedIntParserTest,UnsignedValidInt)38 TEST_F(UnsignedIntParserTest, UnsignedValidInt) {
39 ParseAndVerify();
40 EXPECT_EQ(static_cast<std::uint64_t>(0), parser_.value());
41
42 SetReaderData({0x01, 0x02, 0x03});
43 ParseAndVerify();
44 EXPECT_EQ(static_cast<std::uint64_t>(0x010203), parser_.value());
45
46 SetReaderData({0xFF, 0xFF, 0xFF, 0xFF, 0xFF});
47 ParseAndVerify();
48 EXPECT_EQ(static_cast<std::uint64_t>(0xFFFFFFFFFF), parser_.value());
49
50 SetReaderData({0x12, 0x34, 0x56, 0x78, 0x9A, 0xBC, 0xDE, 0xF0});
51 ParseAndVerify();
52 EXPECT_EQ(static_cast<std::uint64_t>(0x123456789ABCDEF0), parser_.value());
53 }
54
TEST_F(UnsignedIntParserTest,UnsignedIncrementalParse)55 TEST_F(UnsignedIntParserTest, UnsignedIncrementalParse) {
56 SetReaderData({0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10});
57
58 IncrementalParseAndVerify();
59
60 EXPECT_EQ(static_cast<std::uint64_t>(0xFEDCBA9876543210), parser_.value());
61 }
62
63 class SignedIntParserTest : public ElementParserTest<SignedIntParser> {};
64
TEST_F(SignedIntParserTest,SignedInvalidSize)65 TEST_F(SignedIntParserTest, SignedInvalidSize) {
66 TestInit(9, Status::kInvalidElementSize);
67 TestInit(kUnknownElementSize, Status::kInvalidElementSize);
68 }
69
TEST_F(SignedIntParserTest,SignedCustomDefault)70 TEST_F(SignedIntParserTest, SignedCustomDefault) {
71 ResetParser(-1);
72
73 ParseAndVerify();
74
75 EXPECT_EQ(-1, parser_.value());
76 }
77
TEST_F(SignedIntParserTest,SignedValidPositiveInt)78 TEST_F(SignedIntParserTest, SignedValidPositiveInt) {
79 ParseAndVerify();
80 EXPECT_EQ(0, parser_.value());
81
82 SetReaderData({0x7f});
83 ParseAndVerify();
84 EXPECT_EQ(0x7f, parser_.value());
85
86 SetReaderData({0x12, 0xD6, 0x87});
87 ParseAndVerify();
88 EXPECT_EQ(1234567, parser_.value());
89
90 SetReaderData({0x12, 0x34, 0x56, 0x78, 0x9A, 0xBC, 0xDE, 0xF0});
91 ParseAndVerify();
92 EXPECT_EQ(0x123456789ABCDEF0, parser_.value());
93 }
94
TEST_F(SignedIntParserTest,SignedValidNegativeInt)95 TEST_F(SignedIntParserTest, SignedValidNegativeInt) {
96 SetReaderData({0xFF});
97 ParseAndVerify();
98 EXPECT_EQ(-1, parser_.value());
99
100 SetReaderData({0xF8, 0xA4, 0x32, 0xEB});
101 ParseAndVerify();
102 EXPECT_EQ(-123456789, parser_.value());
103 }
104
TEST_F(SignedIntParserTest,SignedIncrementalParse)105 TEST_F(SignedIntParserTest, SignedIncrementalParse) {
106 SetReaderData({0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x11});
107
108 IncrementalParseAndVerify();
109
110 EXPECT_EQ(-81985529216486895, parser_.value());
111 }
112
113 } // namespace
114