| 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" | 
| 11 | 11 | 
| 12 namespace net { | 12 namespace net { | 
| 13 namespace test { | 13 namespace test { | 
| 14 namespace { | 14 namespace { | 
| 15 | 15 | 
| 16 TEST(QuicDataWriterTest, WriteUInt8ToOffset) { | 16 TEST(QuicDataWriterTest, WriteUInt8ToOffset) { | 
| 17   QuicDataWriter writer(4); | 17   char buffer[4]; | 
|  | 18   QuicDataWriter writer(4, buffer); | 
| 18 | 19 | 
| 19   writer.WriteUInt32(0xfefdfcfb); | 20   writer.WriteUInt32(0xfefdfcfb); | 
| 20   EXPECT_TRUE(writer.WriteUInt8ToOffset(1, 0)); | 21   EXPECT_TRUE(writer.WriteUInt8ToOffset(1, 0)); | 
| 21   EXPECT_TRUE(writer.WriteUInt8ToOffset(2, 1)); | 22   EXPECT_TRUE(writer.WriteUInt8ToOffset(2, 1)); | 
| 22   EXPECT_TRUE(writer.WriteUInt8ToOffset(3, 2)); | 23   EXPECT_TRUE(writer.WriteUInt8ToOffset(3, 2)); | 
| 23   EXPECT_TRUE(writer.WriteUInt8ToOffset(4, 3)); | 24   EXPECT_TRUE(writer.WriteUInt8ToOffset(4, 3)); | 
| 24 | 25 | 
| 25   scoped_ptr<char[]> data(writer.take()); | 26   EXPECT_EQ(1, writer.data()[0]); | 
| 26 | 27   EXPECT_EQ(2, writer.data()[1]); | 
| 27   EXPECT_EQ(1, data[0]); | 28   EXPECT_EQ(3, writer.data()[2]); | 
| 28   EXPECT_EQ(2, data[1]); | 29   EXPECT_EQ(4, writer.data()[3]); | 
| 29   EXPECT_EQ(3, data[2]); |  | 
| 30   EXPECT_EQ(4, data[3]); |  | 
| 31 } | 30 } | 
| 32 | 31 | 
| 33 TEST(QuicDataWriterDeathTest, WriteUInt8ToOffset) { | 32 TEST(QuicDataWriterDeathTest, WriteUInt8ToOffset) { | 
| 34   QuicDataWriter writer(4); | 33   char buffer[4]; | 
|  | 34   QuicDataWriter writer(4, buffer); | 
| 35 | 35 | 
| 36   EXPECT_DFATAL(EXPECT_FALSE(writer.WriteUInt8ToOffset(5, 4)), | 36   EXPECT_DFATAL(EXPECT_FALSE(writer.WriteUInt8ToOffset(5, 4)), | 
| 37                 "offset: 4 >= capacity: 4"); | 37                 "offset: 4 >= capacity: 4"); | 
| 38 } | 38 } | 
| 39 | 39 | 
| 40 TEST(QuicDataWriterTest, SanityCheckUFloat16Consts) { | 40 TEST(QuicDataWriterTest, SanityCheckUFloat16Consts) { | 
| 41   // Check the arithmetic on the constants - otherwise the values below make | 41   // Check the arithmetic on the constants - otherwise the values below make | 
| 42   // no sense. | 42   // no sense. | 
| 43   EXPECT_EQ(30, kUFloat16MaxExponent); | 43   EXPECT_EQ(30, kUFloat16MaxExponent); | 
| 44   EXPECT_EQ(11, kUFloat16MantissaBits); | 44   EXPECT_EQ(11, kUFloat16MantissaBits); | 
| (...skipping 28 matching lines...) Expand all  Loading... | 
| 73     { 0x20040000000, 0xF801}, { 0x20040000001, 0xF801}, | 73     { 0x20040000000, 0xF801}, { 0x20040000001, 0xF801}, | 
| 74     // Transition into the max value and clamping. | 74     // Transition into the max value and clamping. | 
| 75     { 0x3FF80000000, 0xFFFE}, { 0x3FFBFFFFFFF, 0xFFFE}, | 75     { 0x3FF80000000, 0xFFFE}, { 0x3FFBFFFFFFF, 0xFFFE}, | 
| 76     { 0x3FFC0000000, 0xFFFF}, { 0x3FFC0000001, 0xFFFF}, | 76     { 0x3FFC0000000, 0xFFFF}, { 0x3FFC0000001, 0xFFFF}, | 
| 77     { 0x3FFFFFFFFFF, 0xFFFF}, { 0x40000000000, 0xFFFF}, | 77     { 0x3FFFFFFFFFF, 0xFFFF}, { 0x40000000000, 0xFFFF}, | 
| 78     { 0xFFFFFFFFFFFFFFFF, 0xFFFF}, | 78     { 0xFFFFFFFFFFFFFFFF, 0xFFFF}, | 
| 79   }; | 79   }; | 
| 80   int num_test_cases = sizeof(test_cases) / sizeof(test_cases[0]); | 80   int num_test_cases = sizeof(test_cases) / sizeof(test_cases[0]); | 
| 81 | 81 | 
| 82   for (int i = 0; i < num_test_cases; ++i) { | 82   for (int i = 0; i < num_test_cases; ++i) { | 
| 83     QuicDataWriter writer(2); | 83     char buffer[2]; | 
|  | 84     QuicDataWriter writer(2, buffer); | 
| 84     EXPECT_TRUE(writer.WriteUFloat16(test_cases[i].decoded)); | 85     EXPECT_TRUE(writer.WriteUFloat16(test_cases[i].decoded)); | 
| 85     scoped_ptr<char[]> data(writer.take()); | 86     EXPECT_EQ(test_cases[i].encoded, *reinterpret_cast<uint16*>(writer.data())); | 
| 86     EXPECT_EQ(test_cases[i].encoded, *reinterpret_cast<uint16*>(data.get())); |  | 
| 87   } | 87   } | 
| 88 } | 88 } | 
| 89 | 89 | 
| 90 TEST(QuicDataWriterTest, ReadUFloat16) { | 90 TEST(QuicDataWriterTest, ReadUFloat16) { | 
| 91   struct TestCase { | 91   struct TestCase { | 
| 92     uint64 decoded; | 92     uint64 decoded; | 
| 93     uint16 encoded; | 93     uint16 encoded; | 
| 94   }; | 94   }; | 
| 95   TestCase test_cases[] = { | 95   TestCase test_cases[] = { | 
| 96     // There are fewer decoding test cases because encoding truncates, and | 96     // There are fewer decoding test cases because encoding truncates, and | 
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
| 137     if (i < 4097) | 137     if (i < 4097) | 
| 138       EXPECT_EQ(i, value); | 138       EXPECT_EQ(i, value); | 
| 139     // Check there's monotonic growth. | 139     // Check there's monotonic growth. | 
| 140     EXPECT_LT(previous_value, value); | 140     EXPECT_LT(previous_value, value); | 
| 141     // Check that precision is within 0.5% away from the denormals. | 141     // Check that precision is within 0.5% away from the denormals. | 
| 142     if (i > 2000) | 142     if (i > 2000) | 
| 143       EXPECT_GT(previous_value * 1005, value * 1000); | 143       EXPECT_GT(previous_value * 1005, value * 1000); | 
| 144     // Check we're always within the promised range. | 144     // Check we're always within the promised range. | 
| 145     EXPECT_LT(value, GG_UINT64_C(0x3FFC0000000)); | 145     EXPECT_LT(value, GG_UINT64_C(0x3FFC0000000)); | 
| 146     previous_value = value; | 146     previous_value = value; | 
| 147     QuicDataWriter writer(6); | 147     char buffer[6]; | 
|  | 148     QuicDataWriter writer(6, buffer); | 
| 148     EXPECT_TRUE(writer.WriteUFloat16(value - 1)); | 149     EXPECT_TRUE(writer.WriteUFloat16(value - 1)); | 
| 149     EXPECT_TRUE(writer.WriteUFloat16(value)); | 150     EXPECT_TRUE(writer.WriteUFloat16(value)); | 
| 150     EXPECT_TRUE(writer.WriteUFloat16(value + 1)); | 151     EXPECT_TRUE(writer.WriteUFloat16(value + 1)); | 
| 151     scoped_ptr<char[]> data(writer.take()); |  | 
| 152     // Check minimal decoding (previous decoding has previous encoding). | 152     // Check minimal decoding (previous decoding has previous encoding). | 
| 153     EXPECT_EQ(i-1, *reinterpret_cast<uint16*>(data.get())); | 153     EXPECT_EQ(i - 1, *reinterpret_cast<uint16*>(writer.data())); | 
| 154     // Check roundtrip. | 154     // Check roundtrip. | 
| 155     EXPECT_EQ(i, *reinterpret_cast<uint16*>(data.get() + 2)); | 155     EXPECT_EQ(i, *reinterpret_cast<uint16*>(writer.data() + 2)); | 
| 156     // Check next decoding. | 156     // Check next decoding. | 
| 157     EXPECT_EQ(i < 4096? i+1 : i, *reinterpret_cast<uint16*>(data.get() + 4)); | 157     EXPECT_EQ(i < 4096 ? i + 1 : i, | 
|  | 158               *reinterpret_cast<uint16*>(writer.data() + 4)); | 
| 158   } | 159   } | 
| 159 } | 160 } | 
| 160 | 161 | 
| 161 }  // namespace | 162 }  // namespace | 
| 162 }  // namespace test | 163 }  // namespace test | 
| 163 }  // namespace net | 164 }  // namespace net | 
| OLD | NEW | 
|---|