OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium 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 "net/quic/quic_data_writer.h" | 5 #include "net/quic/quic_data_writer.h" |
6 | 6 |
| 7 #include <stdint.h> |
| 8 |
7 #include "base/memory/scoped_ptr.h" | 9 #include "base/memory/scoped_ptr.h" |
8 #include "net/quic/quic_data_reader.h" | 10 #include "net/quic/quic_data_reader.h" |
9 #include "net/test/gtest_util.h" | 11 #include "net/test/gtest_util.h" |
10 #include "testing/gtest/include/gtest/gtest.h" | 12 #include "testing/gtest/include/gtest/gtest.h" |
11 | 13 |
12 namespace net { | 14 namespace net { |
13 namespace test { | 15 namespace test { |
14 namespace { | 16 namespace { |
15 | 17 |
16 TEST(QuicDataWriterTest, SanityCheckUFloat16Consts) { | 18 TEST(QuicDataWriterTest, SanityCheckUFloat16Consts) { |
17 // Check the arithmetic on the constants - otherwise the values below make | 19 // Check the arithmetic on the constants - otherwise the values below make |
18 // no sense. | 20 // no sense. |
19 EXPECT_EQ(30, kUFloat16MaxExponent); | 21 EXPECT_EQ(30, kUFloat16MaxExponent); |
20 EXPECT_EQ(11, kUFloat16MantissaBits); | 22 EXPECT_EQ(11, kUFloat16MantissaBits); |
21 EXPECT_EQ(12, kUFloat16MantissaEffectiveBits); | 23 EXPECT_EQ(12, kUFloat16MantissaEffectiveBits); |
22 EXPECT_EQ(GG_UINT64_C(0x3FFC0000000), kUFloat16MaxValue); | 24 EXPECT_EQ(UINT64_C(0x3FFC0000000), kUFloat16MaxValue); |
23 } | 25 } |
24 | 26 |
25 TEST(QuicDataWriterTest, WriteUFloat16) { | 27 TEST(QuicDataWriterTest, WriteUFloat16) { |
26 struct TestCase { | 28 struct TestCase { |
27 uint64 decoded; | 29 uint64 decoded; |
28 uint16 encoded; | 30 uint16 encoded; |
29 }; | 31 }; |
30 TestCase test_cases[] = { | 32 TestCase test_cases[] = { |
31 // Small numbers represent themselves. | 33 // Small numbers represent themselves. |
32 { 0, 0 }, { 1, 1 }, { 2, 2 }, { 3, 3 }, { 4, 4 }, { 5, 5 }, { 6, 6 }, | 34 { 0, 0 }, { 1, 1 }, { 2, 2 }, { 3, 3 }, { 4, 4 }, { 5, 5 }, { 6, 6 }, |
(...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
111 EXPECT_TRUE(reader.ReadUFloat16(&value)); | 113 EXPECT_TRUE(reader.ReadUFloat16(&value)); |
112 // Check that small numbers represent themselves | 114 // Check that small numbers represent themselves |
113 if (i < 4097) | 115 if (i < 4097) |
114 EXPECT_EQ(i, value); | 116 EXPECT_EQ(i, value); |
115 // Check there's monotonic growth. | 117 // Check there's monotonic growth. |
116 EXPECT_LT(previous_value, value); | 118 EXPECT_LT(previous_value, value); |
117 // Check that precision is within 0.5% away from the denormals. | 119 // Check that precision is within 0.5% away from the denormals. |
118 if (i > 2000) | 120 if (i > 2000) |
119 EXPECT_GT(previous_value * 1005, value * 1000); | 121 EXPECT_GT(previous_value * 1005, value * 1000); |
120 // Check we're always within the promised range. | 122 // Check we're always within the promised range. |
121 EXPECT_LT(value, GG_UINT64_C(0x3FFC0000000)); | 123 EXPECT_LT(value, UINT64_C(0x3FFC0000000)); |
122 previous_value = value; | 124 previous_value = value; |
123 char buffer[6]; | 125 char buffer[6]; |
124 QuicDataWriter writer(6, buffer); | 126 QuicDataWriter writer(6, buffer); |
125 EXPECT_TRUE(writer.WriteUFloat16(value - 1)); | 127 EXPECT_TRUE(writer.WriteUFloat16(value - 1)); |
126 EXPECT_TRUE(writer.WriteUFloat16(value)); | 128 EXPECT_TRUE(writer.WriteUFloat16(value)); |
127 EXPECT_TRUE(writer.WriteUFloat16(value + 1)); | 129 EXPECT_TRUE(writer.WriteUFloat16(value + 1)); |
128 // Check minimal decoding (previous decoding has previous encoding). | 130 // Check minimal decoding (previous decoding has previous encoding). |
129 EXPECT_EQ(i - 1, *reinterpret_cast<uint16*>(writer.data())); | 131 EXPECT_EQ(i - 1, *reinterpret_cast<uint16*>(writer.data())); |
130 // Check roundtrip. | 132 // Check roundtrip. |
131 EXPECT_EQ(i, *reinterpret_cast<uint16*>(writer.data() + 2)); | 133 EXPECT_EQ(i, *reinterpret_cast<uint16*>(writer.data() + 2)); |
132 // Check next decoding. | 134 // Check next decoding. |
133 EXPECT_EQ(i < 4096 ? i + 1 : i, | 135 EXPECT_EQ(i < 4096 ? i + 1 : i, |
134 *reinterpret_cast<uint16*>(writer.data() + 4)); | 136 *reinterpret_cast<uint16*>(writer.data() + 4)); |
135 } | 137 } |
136 } | 138 } |
137 | 139 |
138 } // namespace | 140 } // namespace |
139 } // namespace test | 141 } // namespace test |
140 } // namespace net | 142 } // namespace net |
OLD | NEW |