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/float_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::FloatParser;
17 using webm::kUnknownElementSize;
18 using webm::Status;
19 
20 namespace {
21 
22 class FloatParserTest : public ElementParserTest<FloatParser> {};
23 
TEST_F(FloatParserTest,InvalidSize)24 TEST_F(FloatParserTest, InvalidSize) {
25   TestInit(1, Status::kInvalidElementSize);
26   TestInit(5, Status::kInvalidElementSize);
27   TestInit(9, Status::kInvalidElementSize);
28   TestInit(kUnknownElementSize, Status::kInvalidElementSize);
29 }
30 
TEST_F(FloatParserTest,CustomDefault)31 TEST_F(FloatParserTest, CustomDefault) {
32   const double sqrt2 = 1.4142135623730951454746218587388284504413604736328125;
33   ResetParser(sqrt2);
34 
35   ParseAndVerify();
36 
37   EXPECT_EQ(sqrt2, parser_.value());
38 }
39 
TEST_F(FloatParserTest,ValidFloat)40 TEST_F(FloatParserTest, ValidFloat) {
41   ParseAndVerify();
42   EXPECT_EQ(0, parser_.value());
43 
44   SetReaderData({0x40, 0xC9, 0x0F, 0xDB});
45   ParseAndVerify();
46   EXPECT_EQ(6.283185482025146484375, parser_.value());
47 
48   SetReaderData({0x40, 0x05, 0xBF, 0x0A, 0x8B, 0x14, 0x57, 0x69});
49   ParseAndVerify();
50   EXPECT_EQ(2.718281828459045090795598298427648842334747314453125,
51             parser_.value());
52 }
53 
TEST_F(FloatParserTest,IncrementalParse)54 TEST_F(FloatParserTest, IncrementalParse) {
55   SetReaderData({0x3F, 0xF9, 0xE3, 0x77, 0x9B, 0x97, 0xF4, 0xA8});
56 
57   IncrementalParseAndVerify();
58 
59   EXPECT_EQ(1.6180339887498949025257388711906969547271728515625,
60             parser_.value());
61 }
62 
63 }  // namespace
64