| 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 "base/memory/scoped_ptr.h" | 7 #include "base/memory/scoped_ptr.h" |
| 8 #include "net/quic/quic_data_reader.h" | 8 #include "net/quic/quic_data_reader.h" |
| 9 #include "net/test/gtest_util.h" | 9 #include "net/test/gtest_util.h" |
| 10 #include "testing/gtest/include/gtest/gtest.h" | 10 #include "testing/gtest/include/gtest/gtest.h" |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 44 EXPECT_EQ(11, kUFloat16MantissaBits); | 44 EXPECT_EQ(11, kUFloat16MantissaBits); |
| 45 EXPECT_EQ(12, kUFloat16MantissaEffectiveBits); | 45 EXPECT_EQ(12, kUFloat16MantissaEffectiveBits); |
| 46 EXPECT_EQ(GG_UINT64_C(0x3FFC0000000), kUFloat16MaxValue); | 46 EXPECT_EQ(GG_UINT64_C(0x3FFC0000000), kUFloat16MaxValue); |
| 47 } | 47 } |
| 48 | 48 |
| 49 TEST(QuicDataWriterTest, WriteUFloat16) { | 49 TEST(QuicDataWriterTest, WriteUFloat16) { |
| 50 struct TestCase { | 50 struct TestCase { |
| 51 uint64 decoded; | 51 uint64 decoded; |
| 52 uint16 encoded; | 52 uint16 encoded; |
| 53 }; | 53 }; |
| 54 TestCase test_cases[] = { | 54 TestCase test_cases[] = {// Small numbers represent themselves. |
| 55 // Small numbers represent themselves. | 55 {0, 0}, |
| 56 { 0, 0 }, { 1, 1 }, { 2, 2 }, { 3, 3 }, { 4, 4 }, { 5, 5 }, { 6, 6 }, | 56 {1, 1}, |
| 57 { 7, 7 }, { 15, 15 }, { 31, 31 }, { 42, 42 }, { 123, 123 }, { 1234, 1234 }, | 57 {2, 2}, |
| 58 // Check transition through 2^11. | 58 {3, 3}, |
| 59 { 2046, 2046 }, { 2047, 2047 }, { 2048, 2048 }, { 2049, 2049 }, | 59 {4, 4}, |
| 60 // Running out of mantissa at 2^12. | 60 {5, 5}, |
| 61 { 4094, 4094 }, { 4095, 4095 }, { 4096, 4096 }, { 4097, 4096 }, | 61 {6, 6}, |
| 62 { 4098, 4097 }, { 4099, 4097 }, { 4100, 4098 }, { 4101, 4098 }, | 62 {7, 7}, |
| 63 // Check transition through 2^13. | 63 {15, 15}, |
| 64 { 8190, 6143 }, { 8191, 6143 }, { 8192, 6144 }, { 8193, 6144 }, | 64 {31, 31}, |
| 65 { 8194, 6144 }, { 8195, 6144 }, { 8196, 6145 }, { 8197, 6145 }, | 65 {42, 42}, |
| 66 // Half-way through the exponents. | 66 {123, 123}, |
| 67 { 0x7FF8000, 0x87FF }, { 0x7FFFFFF, 0x87FF }, { 0x8000000, 0x8800 }, | 67 {1234, 1234}, |
| 68 { 0xFFF0000, 0x8FFF }, { 0xFFFFFFF, 0x8FFF }, { 0x10000000, 0x9000 }, | 68 // Check transition through 2^11. |
| 69 // Transition into the largest exponent. | 69 {2046, 2046}, |
| 70 { 0x1FFFFFFFFFE, 0xF7FF}, { 0x1FFFFFFFFFF, 0xF7FF}, | 70 {2047, 2047}, |
| 71 { 0x20000000000, 0xF800}, { 0x20000000001, 0xF800}, | 71 {2048, 2048}, |
| 72 { 0x2003FFFFFFE, 0xF800}, { 0x2003FFFFFFF, 0xF800}, | 72 {2049, 2049}, |
| 73 { 0x20040000000, 0xF801}, { 0x20040000001, 0xF801}, | 73 // Running out of mantissa at 2^12. |
| 74 // Transition into the max value and clamping. | 74 {4094, 4094}, |
| 75 { 0x3FF80000000, 0xFFFE}, { 0x3FFBFFFFFFF, 0xFFFE}, | 75 {4095, 4095}, |
| 76 { 0x3FFC0000000, 0xFFFF}, { 0x3FFC0000001, 0xFFFF}, | 76 {4096, 4096}, |
| 77 { 0x3FFFFFFFFFF, 0xFFFF}, { 0x40000000000, 0xFFFF}, | 77 {4097, 4096}, |
| 78 { 0xFFFFFFFFFFFFFFFF, 0xFFFF}, | 78 {4098, 4097}, |
| 79 {4099, 4097}, |
| 80 {4100, 4098}, |
| 81 {4101, 4098}, |
| 82 // Check transition through 2^13. |
| 83 {8190, 6143}, |
| 84 {8191, 6143}, |
| 85 {8192, 6144}, |
| 86 {8193, 6144}, |
| 87 {8194, 6144}, |
| 88 {8195, 6144}, |
| 89 {8196, 6145}, |
| 90 {8197, 6145}, |
| 91 // Half-way through the exponents. |
| 92 {0x7FF8000, 0x87FF}, |
| 93 {0x7FFFFFF, 0x87FF}, |
| 94 {0x8000000, 0x8800}, |
| 95 {0xFFF0000, 0x8FFF}, |
| 96 {0xFFFFFFF, 0x8FFF}, |
| 97 {0x10000000, 0x9000}, |
| 98 // Transition into the largest exponent. |
| 99 {0x1FFFFFFFFFE, 0xF7FF}, |
| 100 {0x1FFFFFFFFFF, 0xF7FF}, |
| 101 {0x20000000000, 0xF800}, |
| 102 {0x20000000001, 0xF800}, |
| 103 {0x2003FFFFFFE, 0xF800}, |
| 104 {0x2003FFFFFFF, 0xF800}, |
| 105 {0x20040000000, 0xF801}, |
| 106 {0x20040000001, 0xF801}, |
| 107 // Transition into the max value and clamping. |
| 108 {0x3FF80000000, 0xFFFE}, |
| 109 {0x3FFBFFFFFFF, 0xFFFE}, |
| 110 {0x3FFC0000000, 0xFFFF}, |
| 111 {0x3FFC0000001, 0xFFFF}, |
| 112 {0x3FFFFFFFFFF, 0xFFFF}, |
| 113 {0x40000000000, 0xFFFF}, |
| 114 {0xFFFFFFFFFFFFFFFF, 0xFFFF}, |
| 79 }; | 115 }; |
| 80 int num_test_cases = sizeof(test_cases) / sizeof(test_cases[0]); | 116 int num_test_cases = sizeof(test_cases) / sizeof(test_cases[0]); |
| 81 | 117 |
| 82 for (int i = 0; i < num_test_cases; ++i) { | 118 for (int i = 0; i < num_test_cases; ++i) { |
| 83 QuicDataWriter writer(2); | 119 QuicDataWriter writer(2); |
| 84 EXPECT_TRUE(writer.WriteUFloat16(test_cases[i].decoded)); | 120 EXPECT_TRUE(writer.WriteUFloat16(test_cases[i].decoded)); |
| 85 scoped_ptr<char[]> data(writer.take()); | 121 scoped_ptr<char[]> data(writer.take()); |
| 86 EXPECT_EQ(test_cases[i].encoded, *reinterpret_cast<uint16*>(data.get())); | 122 EXPECT_EQ(test_cases[i].encoded, *reinterpret_cast<uint16*>(data.get())); |
| 87 } | 123 } |
| 88 } | 124 } |
| 89 | 125 |
| 90 TEST(QuicDataWriterTest, ReadUFloat16) { | 126 TEST(QuicDataWriterTest, ReadUFloat16) { |
| 91 struct TestCase { | 127 struct TestCase { |
| 92 uint64 decoded; | 128 uint64 decoded; |
| 93 uint16 encoded; | 129 uint16 encoded; |
| 94 }; | 130 }; |
| 95 TestCase test_cases[] = { | 131 TestCase test_cases[] = { |
| 96 // There are fewer decoding test cases because encoding truncates, and | 132 // There are fewer decoding test cases because encoding truncates, and |
| 97 // decoding returns the smallest expansion. | 133 // decoding returns the smallest expansion. |
| 98 // Small numbers represent themselves. | 134 // Small numbers represent themselves. |
| 99 { 0, 0 }, { 1, 1 }, { 2, 2 }, { 3, 3 }, { 4, 4 }, { 5, 5 }, { 6, 6 }, | 135 {0, 0}, |
| 100 { 7, 7 }, { 15, 15 }, { 31, 31 }, { 42, 42 }, { 123, 123 }, { 1234, 1234 }, | 136 {1, 1}, |
| 101 // Check transition through 2^11. | 137 {2, 2}, |
| 102 { 2046, 2046 }, { 2047, 2047 }, { 2048, 2048 }, { 2049, 2049 }, | 138 {3, 3}, |
| 103 // Running out of mantissa at 2^12. | 139 {4, 4}, |
| 104 { 4094, 4094 }, { 4095, 4095 }, { 4096, 4096 }, | 140 {5, 5}, |
| 105 { 4098, 4097 }, { 4100, 4098 }, | 141 {6, 6}, |
| 106 // Check transition through 2^13. | 142 {7, 7}, |
| 107 { 8190, 6143 }, { 8192, 6144 }, { 8196, 6145 }, | 143 {15, 15}, |
| 108 // Half-way through the exponents. | 144 {31, 31}, |
| 109 { 0x7FF8000, 0x87FF }, { 0x8000000, 0x8800 }, | 145 {42, 42}, |
| 110 { 0xFFF0000, 0x8FFF }, { 0x10000000, 0x9000 }, | 146 {123, 123}, |
| 111 // Transition into the largest exponent. | 147 {1234, 1234}, |
| 112 { 0x1FFE0000000, 0xF7FF}, { 0x20000000000, 0xF800}, | 148 // Check transition through 2^11. |
| 113 { 0x20040000000, 0xF801}, | 149 {2046, 2046}, |
| 114 // Transition into the max value. | 150 {2047, 2047}, |
| 115 { 0x3FF80000000, 0xFFFE}, { 0x3FFC0000000, 0xFFFF}, | 151 {2048, 2048}, |
| 152 {2049, 2049}, |
| 153 // Running out of mantissa at 2^12. |
| 154 {4094, 4094}, |
| 155 {4095, 4095}, |
| 156 {4096, 4096}, |
| 157 {4098, 4097}, |
| 158 {4100, 4098}, |
| 159 // Check transition through 2^13. |
| 160 {8190, 6143}, |
| 161 {8192, 6144}, |
| 162 {8196, 6145}, |
| 163 // Half-way through the exponents. |
| 164 {0x7FF8000, 0x87FF}, |
| 165 {0x8000000, 0x8800}, |
| 166 {0xFFF0000, 0x8FFF}, |
| 167 {0x10000000, 0x9000}, |
| 168 // Transition into the largest exponent. |
| 169 {0x1FFE0000000, 0xF7FF}, |
| 170 {0x20000000000, 0xF800}, |
| 171 {0x20040000000, 0xF801}, |
| 172 // Transition into the max value. |
| 173 {0x3FF80000000, 0xFFFE}, |
| 174 {0x3FFC0000000, 0xFFFF}, |
| 116 }; | 175 }; |
| 117 int num_test_cases = sizeof(test_cases) / sizeof(test_cases[0]); | 176 int num_test_cases = sizeof(test_cases) / sizeof(test_cases[0]); |
| 118 | 177 |
| 119 for (int i = 0; i < num_test_cases; ++i) { | 178 for (int i = 0; i < num_test_cases; ++i) { |
| 120 QuicDataReader reader(reinterpret_cast<char*>(&test_cases[i].encoded), 2); | 179 QuicDataReader reader(reinterpret_cast<char*>(&test_cases[i].encoded), 2); |
| 121 uint64 value; | 180 uint64 value; |
| 122 EXPECT_TRUE(reader.ReadUFloat16(&value)); | 181 EXPECT_TRUE(reader.ReadUFloat16(&value)); |
| 123 EXPECT_EQ(test_cases[i].decoded, value); | 182 EXPECT_EQ(test_cases[i].decoded, value); |
| 124 } | 183 } |
| 125 } | 184 } |
| (...skipping 17 matching lines...) Expand all Loading... |
| 143 EXPECT_GT(previous_value * 1005, value * 1000); | 202 EXPECT_GT(previous_value * 1005, value * 1000); |
| 144 // Check we're always within the promised range. | 203 // Check we're always within the promised range. |
| 145 EXPECT_LT(value, GG_UINT64_C(0x3FFC0000000)); | 204 EXPECT_LT(value, GG_UINT64_C(0x3FFC0000000)); |
| 146 previous_value = value; | 205 previous_value = value; |
| 147 QuicDataWriter writer(6); | 206 QuicDataWriter writer(6); |
| 148 EXPECT_TRUE(writer.WriteUFloat16(value - 1)); | 207 EXPECT_TRUE(writer.WriteUFloat16(value - 1)); |
| 149 EXPECT_TRUE(writer.WriteUFloat16(value)); | 208 EXPECT_TRUE(writer.WriteUFloat16(value)); |
| 150 EXPECT_TRUE(writer.WriteUFloat16(value + 1)); | 209 EXPECT_TRUE(writer.WriteUFloat16(value + 1)); |
| 151 scoped_ptr<char[]> data(writer.take()); | 210 scoped_ptr<char[]> data(writer.take()); |
| 152 // Check minimal decoding (previous decoding has previous encoding). | 211 // Check minimal decoding (previous decoding has previous encoding). |
| 153 EXPECT_EQ(i-1, *reinterpret_cast<uint16*>(data.get())); | 212 EXPECT_EQ(i - 1, *reinterpret_cast<uint16*>(data.get())); |
| 154 // Check roundtrip. | 213 // Check roundtrip. |
| 155 EXPECT_EQ(i, *reinterpret_cast<uint16*>(data.get() + 2)); | 214 EXPECT_EQ(i, *reinterpret_cast<uint16*>(data.get() + 2)); |
| 156 // Check next decoding. | 215 // Check next decoding. |
| 157 EXPECT_EQ(i < 4096? i+1 : i, *reinterpret_cast<uint16*>(data.get() + 4)); | 216 EXPECT_EQ(i < 4096 ? i + 1 : i, *reinterpret_cast<uint16*>(data.get() + 4)); |
| 158 } | 217 } |
| 159 } | 218 } |
| 160 | 219 |
| 161 } // namespace | 220 } // namespace |
| 162 } // namespace test | 221 } // namespace test |
| 163 } // namespace net | 222 } // namespace net |
| OLD | NEW |