| 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> | 7 #include <stdint.h> |
| 8 | 8 |
| 9 #include "base/memory/scoped_ptr.h" | 9 #include "base/memory/scoped_ptr.h" |
| 10 #include "net/quic/quic_data_reader.h" | 10 #include "net/quic/quic_data_reader.h" |
| 11 #include "net/test/gtest_util.h" | 11 #include "net/test/gtest_util.h" |
| 12 #include "testing/gtest/include/gtest/gtest.h" | 12 #include "testing/gtest/include/gtest/gtest.h" |
| 13 | 13 |
| 14 namespace net { | 14 namespace net { |
| 15 namespace test { | 15 namespace test { |
| 16 namespace { | 16 namespace { |
| 17 | 17 |
| 18 TEST(QuicDataWriterTest, SanityCheckUFloat16Consts) { | 18 TEST(QuicDataWriterTest, SanityCheckUFloat16Consts) { |
| 19 // Check the arithmetic on the constants - otherwise the values below make | 19 // Check the arithmetic on the constants - otherwise the values below make |
| 20 // no sense. | 20 // no sense. |
| 21 EXPECT_EQ(30, kUFloat16MaxExponent); | 21 EXPECT_EQ(30, kUFloat16MaxExponent); |
| 22 EXPECT_EQ(11, kUFloat16MantissaBits); | 22 EXPECT_EQ(11, kUFloat16MantissaBits); |
| 23 EXPECT_EQ(12, kUFloat16MantissaEffectiveBits); | 23 EXPECT_EQ(12, kUFloat16MantissaEffectiveBits); |
| 24 EXPECT_EQ(UINT64_C(0x3FFC0000000), kUFloat16MaxValue); | 24 EXPECT_EQ(UINT64_C(0x3FFC0000000), kUFloat16MaxValue); |
| 25 } | 25 } |
| 26 | 26 |
| 27 TEST(QuicDataWriterTest, WriteUFloat16) { | 27 TEST(QuicDataWriterTest, WriteUFloat16) { |
| 28 struct TestCase { | 28 struct TestCase { |
| 29 uint64 decoded; | 29 uint64_t decoded; |
| 30 uint16 encoded; | 30 uint16_t encoded; |
| 31 }; | 31 }; |
| 32 TestCase test_cases[] = { | 32 TestCase test_cases[] = { |
| 33 // Small numbers represent themselves. | 33 // Small numbers represent themselves. |
| 34 {0, 0}, | 34 {0, 0}, |
| 35 {1, 1}, | 35 {1, 1}, |
| 36 {2, 2}, | 36 {2, 2}, |
| 37 {3, 3}, | 37 {3, 3}, |
| 38 {4, 4}, | 38 {4, 4}, |
| 39 {5, 5}, | 39 {5, 5}, |
| 40 {6, 6}, | 40 {6, 6}, |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 91 {0x3FFFFFFFFFF, 0xFFFF}, | 91 {0x3FFFFFFFFFF, 0xFFFF}, |
| 92 {0x40000000000, 0xFFFF}, | 92 {0x40000000000, 0xFFFF}, |
| 93 {0xFFFFFFFFFFFFFFFF, 0xFFFF}, | 93 {0xFFFFFFFFFFFFFFFF, 0xFFFF}, |
| 94 }; | 94 }; |
| 95 int num_test_cases = sizeof(test_cases) / sizeof(test_cases[0]); | 95 int num_test_cases = sizeof(test_cases) / sizeof(test_cases[0]); |
| 96 | 96 |
| 97 for (int i = 0; i < num_test_cases; ++i) { | 97 for (int i = 0; i < num_test_cases; ++i) { |
| 98 char buffer[2]; | 98 char buffer[2]; |
| 99 QuicDataWriter writer(2, buffer); | 99 QuicDataWriter writer(2, buffer); |
| 100 EXPECT_TRUE(writer.WriteUFloat16(test_cases[i].decoded)); | 100 EXPECT_TRUE(writer.WriteUFloat16(test_cases[i].decoded)); |
| 101 EXPECT_EQ(test_cases[i].encoded, *reinterpret_cast<uint16*>(writer.data())); | 101 EXPECT_EQ(test_cases[i].encoded, |
| 102 *reinterpret_cast<uint16_t*>(writer.data())); |
| 102 } | 103 } |
| 103 } | 104 } |
| 104 | 105 |
| 105 TEST(QuicDataWriterTest, ReadUFloat16) { | 106 TEST(QuicDataWriterTest, ReadUFloat16) { |
| 106 struct TestCase { | 107 struct TestCase { |
| 107 uint64 decoded; | 108 uint64_t decoded; |
| 108 uint16 encoded; | 109 uint16_t encoded; |
| 109 }; | 110 }; |
| 110 TestCase test_cases[] = { | 111 TestCase test_cases[] = { |
| 111 // There are fewer decoding test cases because encoding truncates, and | 112 // There are fewer decoding test cases because encoding truncates, and |
| 112 // decoding returns the smallest expansion. | 113 // decoding returns the smallest expansion. |
| 113 // Small numbers represent themselves. | 114 // Small numbers represent themselves. |
| 114 {0, 0}, | 115 {0, 0}, |
| 115 {1, 1}, | 116 {1, 1}, |
| 116 {2, 2}, | 117 {2, 2}, |
| 117 {3, 3}, | 118 {3, 3}, |
| 118 {4, 4}, | 119 {4, 4}, |
| (...skipping 30 matching lines...) Expand all Loading... |
| 149 {0x20000000000, 0xF800}, | 150 {0x20000000000, 0xF800}, |
| 150 {0x20040000000, 0xF801}, | 151 {0x20040000000, 0xF801}, |
| 151 // Transition into the max value. | 152 // Transition into the max value. |
| 152 {0x3FF80000000, 0xFFFE}, | 153 {0x3FF80000000, 0xFFFE}, |
| 153 {0x3FFC0000000, 0xFFFF}, | 154 {0x3FFC0000000, 0xFFFF}, |
| 154 }; | 155 }; |
| 155 int num_test_cases = sizeof(test_cases) / sizeof(test_cases[0]); | 156 int num_test_cases = sizeof(test_cases) / sizeof(test_cases[0]); |
| 156 | 157 |
| 157 for (int i = 0; i < num_test_cases; ++i) { | 158 for (int i = 0; i < num_test_cases; ++i) { |
| 158 QuicDataReader reader(reinterpret_cast<char*>(&test_cases[i].encoded), 2); | 159 QuicDataReader reader(reinterpret_cast<char*>(&test_cases[i].encoded), 2); |
| 159 uint64 value; | 160 uint64_t value; |
| 160 EXPECT_TRUE(reader.ReadUFloat16(&value)); | 161 EXPECT_TRUE(reader.ReadUFloat16(&value)); |
| 161 EXPECT_EQ(test_cases[i].decoded, value); | 162 EXPECT_EQ(test_cases[i].decoded, value); |
| 162 } | 163 } |
| 163 } | 164 } |
| 164 | 165 |
| 165 TEST(QuicDataWriterTest, RoundTripUFloat16) { | 166 TEST(QuicDataWriterTest, RoundTripUFloat16) { |
| 166 // Just test all 16-bit encoded values. 0 and max already tested above. | 167 // Just test all 16-bit encoded values. 0 and max already tested above. |
| 167 uint64 previous_value = 0; | 168 uint64_t previous_value = 0; |
| 168 for (uint16 i = 1; i < 0xFFFF; ++i) { | 169 for (uint16_t i = 1; i < 0xFFFF; ++i) { |
| 169 // Read the two bytes. | 170 // Read the two bytes. |
| 170 QuicDataReader reader(reinterpret_cast<char*>(&i), 2); | 171 QuicDataReader reader(reinterpret_cast<char*>(&i), 2); |
| 171 uint64 value; | 172 uint64_t value; |
| 172 // All values must be decodable. | 173 // All values must be decodable. |
| 173 EXPECT_TRUE(reader.ReadUFloat16(&value)); | 174 EXPECT_TRUE(reader.ReadUFloat16(&value)); |
| 174 // Check that small numbers represent themselves | 175 // Check that small numbers represent themselves |
| 175 if (i < 4097) | 176 if (i < 4097) |
| 176 EXPECT_EQ(i, value); | 177 EXPECT_EQ(i, value); |
| 177 // Check there's monotonic growth. | 178 // Check there's monotonic growth. |
| 178 EXPECT_LT(previous_value, value); | 179 EXPECT_LT(previous_value, value); |
| 179 // Check that precision is within 0.5% away from the denormals. | 180 // Check that precision is within 0.5% away from the denormals. |
| 180 if (i > 2000) | 181 if (i > 2000) |
| 181 EXPECT_GT(previous_value * 1005, value * 1000); | 182 EXPECT_GT(previous_value * 1005, value * 1000); |
| 182 // Check we're always within the promised range. | 183 // Check we're always within the promised range. |
| 183 EXPECT_LT(value, UINT64_C(0x3FFC0000000)); | 184 EXPECT_LT(value, UINT64_C(0x3FFC0000000)); |
| 184 previous_value = value; | 185 previous_value = value; |
| 185 char buffer[6]; | 186 char buffer[6]; |
| 186 QuicDataWriter writer(6, buffer); | 187 QuicDataWriter writer(6, buffer); |
| 187 EXPECT_TRUE(writer.WriteUFloat16(value - 1)); | 188 EXPECT_TRUE(writer.WriteUFloat16(value - 1)); |
| 188 EXPECT_TRUE(writer.WriteUFloat16(value)); | 189 EXPECT_TRUE(writer.WriteUFloat16(value)); |
| 189 EXPECT_TRUE(writer.WriteUFloat16(value + 1)); | 190 EXPECT_TRUE(writer.WriteUFloat16(value + 1)); |
| 190 // Check minimal decoding (previous decoding has previous encoding). | 191 // Check minimal decoding (previous decoding has previous encoding). |
| 191 EXPECT_EQ(i - 1, *reinterpret_cast<uint16*>(writer.data())); | 192 EXPECT_EQ(i - 1, *reinterpret_cast<uint16_t*>(writer.data())); |
| 192 // Check roundtrip. | 193 // Check roundtrip. |
| 193 EXPECT_EQ(i, *reinterpret_cast<uint16*>(writer.data() + 2)); | 194 EXPECT_EQ(i, *reinterpret_cast<uint16_t*>(writer.data() + 2)); |
| 194 // Check next decoding. | 195 // Check next decoding. |
| 195 EXPECT_EQ(i < 4096 ? i + 1 : i, | 196 EXPECT_EQ(i < 4096 ? i + 1 : i, |
| 196 *reinterpret_cast<uint16*>(writer.data() + 4)); | 197 *reinterpret_cast<uint16_t*>(writer.data() + 4)); |
| 197 } | 198 } |
| 198 } | 199 } |
| 199 | 200 |
| 200 } // namespace | 201 } // namespace |
| 201 } // namespace test | 202 } // namespace test |
| 202 } // namespace net | 203 } // namespace net |
| OLD | NEW |