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
3 // found in the LICENSE file.
5 #include "net/quic/quic_data_writer.h"
7 #include "base/memory/scoped_ptr.h"
8 #include "net/quic/quic_data_reader.h"
9 #include "testing/gtest/include/gtest/gtest.h"
15 TEST(QuicDataWriterTest, WriteUInt8ToOffset) {
16 QuicDataWriter writer(4);
18 writer.WriteUInt32(0xfefdfcfb);
19 EXPECT_TRUE(writer.WriteUInt8ToOffset(1, 0));
20 EXPECT_TRUE(writer.WriteUInt8ToOffset(2, 1));
21 EXPECT_TRUE(writer.WriteUInt8ToOffset(3, 2));
22 EXPECT_TRUE(writer.WriteUInt8ToOffset(4, 3));
24 scoped_ptr<char[]> data(writer.take());
26 EXPECT_EQ(1, data[0]);
27 EXPECT_EQ(2, data[1]);
28 EXPECT_EQ(3, data[2]);
29 EXPECT_EQ(4, data[3]);
32 TEST(QuicDataWriterDeathTest, WriteUInt8ToOffset) {
33 QuicDataWriter writer(4);
35 #if !defined(WIN32) && defined(GTEST_HAS_DEATH_TEST)
36 #if !defined(DCHECK_ALWAYS_ON)
37 EXPECT_DEBUG_DEATH(writer.WriteUInt8ToOffset(5, 4), "Check failed");
39 EXPECT_DEATH(writer.WriteUInt8ToOffset(5, 4), "Check failed");
44 TEST(QuicDataWriterTest, SanityCheckUFloat16Consts) {
45 // Check the arithmetic on the constants - otherwise the values below make
47 EXPECT_EQ(30, kUFloat16MaxExponent);
48 EXPECT_EQ(11, kUFloat16MantissaBits);
49 EXPECT_EQ(12, kUFloat16MantissaEffectiveBits);
50 EXPECT_EQ(GG_UINT64_C(0x3FFC0000000), kUFloat16MaxValue);
53 TEST(QuicDataWriterTest, WriteUFloat16) {
58 TestCase test_cases[] = {
59 // Small numbers represent themselves.
60 { 0, 0 }, { 1, 1 }, { 2, 2 }, { 3, 3 }, { 4, 4 }, { 5, 5 }, { 6, 6 },
61 { 7, 7 }, { 15, 15 }, { 31, 31 }, { 42, 42 }, { 123, 123 }, { 1234, 1234 },
62 // Check transition through 2^11.
63 { 2046, 2046 }, { 2047, 2047 }, { 2048, 2048 }, { 2049, 2049 },
64 // Running out of mantissa at 2^12.
65 { 4094, 4094 }, { 4095, 4095 }, { 4096, 4096 }, { 4097, 4096 },
66 { 4098, 4097 }, { 4099, 4097 }, { 4100, 4098 }, { 4101, 4098 },
67 // Check transition through 2^13.
68 { 8190, 6143 }, { 8191, 6143 }, { 8192, 6144 }, { 8193, 6144 },
69 { 8194, 6144 }, { 8195, 6144 }, { 8196, 6145 }, { 8197, 6145 },
70 // Half-way through the exponents.
71 { 0x7FF8000, 0x87FF }, { 0x7FFFFFF, 0x87FF }, { 0x8000000, 0x8800 },
72 { 0xFFF0000, 0x8FFF }, { 0xFFFFFFF, 0x8FFF }, { 0x10000000, 0x9000 },
73 // Transition into the largest exponent.
74 { 0x1FFFFFFFFFE, 0xF7FF}, { 0x1FFFFFFFFFF, 0xF7FF},
75 { 0x20000000000, 0xF800}, { 0x20000000001, 0xF800},
76 { 0x2003FFFFFFE, 0xF800}, { 0x2003FFFFFFF, 0xF800},
77 { 0x20040000000, 0xF801}, { 0x20040000001, 0xF801},
78 // Transition into the max value and clamping.
79 { 0x3FF80000000, 0xFFFE}, { 0x3FFBFFFFFFF, 0xFFFE},
80 { 0x3FFC0000000, 0xFFFF}, { 0x3FFC0000001, 0xFFFF},
81 { 0x3FFFFFFFFFF, 0xFFFF}, { 0x40000000000, 0xFFFF},
82 { 0xFFFFFFFFFFFFFFFF, 0xFFFF},
84 int num_test_cases = sizeof(test_cases) / sizeof(test_cases[0]);
86 for (int i = 0; i < num_test_cases; ++i) {
87 QuicDataWriter writer(2);
88 EXPECT_TRUE(writer.WriteUFloat16(test_cases[i].decoded));
89 scoped_ptr<char[]> data(writer.take());
90 EXPECT_EQ(test_cases[i].encoded, *reinterpret_cast<uint16*>(data.get()));
94 TEST(QuicDataWriterTest, ReadUFloat16) {
99 TestCase test_cases[] = {
100 // There are fewer decoding test cases because encoding truncates, and
101 // decoding returns the smallest expansion.
102 // Small numbers represent themselves.
103 { 0, 0 }, { 1, 1 }, { 2, 2 }, { 3, 3 }, { 4, 4 }, { 5, 5 }, { 6, 6 },
104 { 7, 7 }, { 15, 15 }, { 31, 31 }, { 42, 42 }, { 123, 123 }, { 1234, 1234 },
105 // Check transition through 2^11.
106 { 2046, 2046 }, { 2047, 2047 }, { 2048, 2048 }, { 2049, 2049 },
107 // Running out of mantissa at 2^12.
108 { 4094, 4094 }, { 4095, 4095 }, { 4096, 4096 },
109 { 4098, 4097 }, { 4100, 4098 },
110 // Check transition through 2^13.
111 { 8190, 6143 }, { 8192, 6144 }, { 8196, 6145 },
112 // Half-way through the exponents.
113 { 0x7FF8000, 0x87FF }, { 0x8000000, 0x8800 },
114 { 0xFFF0000, 0x8FFF }, { 0x10000000, 0x9000 },
115 // Transition into the largest exponent.
116 { 0x1FFE0000000, 0xF7FF}, { 0x20000000000, 0xF800},
117 { 0x20040000000, 0xF801},
118 // Transition into the max value.
119 { 0x3FF80000000, 0xFFFE}, { 0x3FFC0000000, 0xFFFF},
121 int num_test_cases = sizeof(test_cases) / sizeof(test_cases[0]);
123 for (int i = 0; i < num_test_cases; ++i) {
124 QuicDataReader reader(reinterpret_cast<char*>(&test_cases[i].encoded), 2);
126 EXPECT_TRUE(reader.ReadUFloat16(&value));
127 EXPECT_EQ(test_cases[i].decoded, value);
131 TEST(QuicDataWriterTest, RoundTripUFloat16) {
132 // Just test all 16-bit encoded values. 0 and max already tested above.
133 uint64 previous_value = 0;
134 for (uint16 i = 1; i < 0xFFFF; ++i) {
135 // Read the two bytes.
136 QuicDataReader reader(reinterpret_cast<char*>(&i), 2);
138 // All values must be decodable.
139 EXPECT_TRUE(reader.ReadUFloat16(&value));
140 // Check that small numbers represent themselves
143 // Check there's monotonic growth.
144 EXPECT_LT(previous_value, value);
145 // Check that precision is within 0.5% away from the denormals.
147 EXPECT_GT(previous_value * 1005, value * 1000);
148 // Check we're always within the promised range.
149 EXPECT_LT(value, GG_UINT64_C(0x3FFC0000000));
150 previous_value = value;
151 QuicDataWriter writer(6);
152 EXPECT_TRUE(writer.WriteUFloat16(value - 1));
153 EXPECT_TRUE(writer.WriteUFloat16(value));
154 EXPECT_TRUE(writer.WriteUFloat16(value + 1));
155 scoped_ptr<char[]> data(writer.take());
156 // Check minimal decoding (previous decoding has previous encoding).
157 EXPECT_EQ(i-1, *reinterpret_cast<uint16*>(data.get()));
159 EXPECT_EQ(i, *reinterpret_cast<uint16*>(data.get() + 2));
160 // Check next decoding.
161 EXPECT_EQ(i < 4096? i+1 : i, *reinterpret_cast<uint16*>(data.get() + 4));