Lots of random cleanups, mostly for native_theme_win.cc:
[chromium-blink-merge.git] / net / quic / quic_data_writer_test.cc
blob07346c78891a2c598e9a52441d2d181166e77b83
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 "net/test/gtest_util.h"
10 #include "testing/gtest/include/gtest/gtest.h"
12 namespace net {
13 namespace test {
14 namespace {
16 TEST(QuicDataWriterTest, WriteUInt8ToOffset) {
17 QuicDataWriter writer(4);
19 writer.WriteUInt32(0xfefdfcfb);
20 EXPECT_TRUE(writer.WriteUInt8ToOffset(1, 0));
21 EXPECT_TRUE(writer.WriteUInt8ToOffset(2, 1));
22 EXPECT_TRUE(writer.WriteUInt8ToOffset(3, 2));
23 EXPECT_TRUE(writer.WriteUInt8ToOffset(4, 3));
25 scoped_ptr<char[]> data(writer.take());
27 EXPECT_EQ(1, data[0]);
28 EXPECT_EQ(2, data[1]);
29 EXPECT_EQ(3, data[2]);
30 EXPECT_EQ(4, data[3]);
33 TEST(QuicDataWriterDeathTest, WriteUInt8ToOffset) {
34 QuicDataWriter writer(4);
36 EXPECT_DFATAL(EXPECT_FALSE(writer.WriteUInt8ToOffset(5, 4)),
37 "offset: 4 >= capacity: 4");
40 TEST(QuicDataWriterTest, SanityCheckUFloat16Consts) {
41 // Check the arithmetic on the constants - otherwise the values below make
42 // no sense.
43 EXPECT_EQ(30, kUFloat16MaxExponent);
44 EXPECT_EQ(11, kUFloat16MantissaBits);
45 EXPECT_EQ(12, kUFloat16MantissaEffectiveBits);
46 EXPECT_EQ(GG_UINT64_C(0x3FFC0000000), kUFloat16MaxValue);
49 TEST(QuicDataWriterTest, WriteUFloat16) {
50 struct TestCase {
51 uint64 decoded;
52 uint16 encoded;
54 TestCase test_cases[] = {
55 // Small numbers represent themselves.
56 { 0, 0 }, { 1, 1 }, { 2, 2 }, { 3, 3 }, { 4, 4 }, { 5, 5 }, { 6, 6 },
57 { 7, 7 }, { 15, 15 }, { 31, 31 }, { 42, 42 }, { 123, 123 }, { 1234, 1234 },
58 // Check transition through 2^11.
59 { 2046, 2046 }, { 2047, 2047 }, { 2048, 2048 }, { 2049, 2049 },
60 // Running out of mantissa at 2^12.
61 { 4094, 4094 }, { 4095, 4095 }, { 4096, 4096 }, { 4097, 4096 },
62 { 4098, 4097 }, { 4099, 4097 }, { 4100, 4098 }, { 4101, 4098 },
63 // Check transition through 2^13.
64 { 8190, 6143 }, { 8191, 6143 }, { 8192, 6144 }, { 8193, 6144 },
65 { 8194, 6144 }, { 8195, 6144 }, { 8196, 6145 }, { 8197, 6145 },
66 // Half-way through the exponents.
67 { 0x7FF8000, 0x87FF }, { 0x7FFFFFF, 0x87FF }, { 0x8000000, 0x8800 },
68 { 0xFFF0000, 0x8FFF }, { 0xFFFFFFF, 0x8FFF }, { 0x10000000, 0x9000 },
69 // Transition into the largest exponent.
70 { 0x1FFFFFFFFFE, 0xF7FF}, { 0x1FFFFFFFFFF, 0xF7FF},
71 { 0x20000000000, 0xF800}, { 0x20000000001, 0xF800},
72 { 0x2003FFFFFFE, 0xF800}, { 0x2003FFFFFFF, 0xF800},
73 { 0x20040000000, 0xF801}, { 0x20040000001, 0xF801},
74 // Transition into the max value and clamping.
75 { 0x3FF80000000, 0xFFFE}, { 0x3FFBFFFFFFF, 0xFFFE},
76 { 0x3FFC0000000, 0xFFFF}, { 0x3FFC0000001, 0xFFFF},
77 { 0x3FFFFFFFFFF, 0xFFFF}, { 0x40000000000, 0xFFFF},
78 { 0xFFFFFFFFFFFFFFFF, 0xFFFF},
80 int num_test_cases = sizeof(test_cases) / sizeof(test_cases[0]);
82 for (int i = 0; i < num_test_cases; ++i) {
83 QuicDataWriter writer(2);
84 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*>(data.get()));
90 TEST(QuicDataWriterTest, ReadUFloat16) {
91 struct TestCase {
92 uint64 decoded;
93 uint16 encoded;
95 TestCase test_cases[] = {
96 // There are fewer decoding test cases because encoding truncates, and
97 // decoding returns the smallest expansion.
98 // Small numbers represent themselves.
99 { 0, 0 }, { 1, 1 }, { 2, 2 }, { 3, 3 }, { 4, 4 }, { 5, 5 }, { 6, 6 },
100 { 7, 7 }, { 15, 15 }, { 31, 31 }, { 42, 42 }, { 123, 123 }, { 1234, 1234 },
101 // Check transition through 2^11.
102 { 2046, 2046 }, { 2047, 2047 }, { 2048, 2048 }, { 2049, 2049 },
103 // Running out of mantissa at 2^12.
104 { 4094, 4094 }, { 4095, 4095 }, { 4096, 4096 },
105 { 4098, 4097 }, { 4100, 4098 },
106 // Check transition through 2^13.
107 { 8190, 6143 }, { 8192, 6144 }, { 8196, 6145 },
108 // Half-way through the exponents.
109 { 0x7FF8000, 0x87FF }, { 0x8000000, 0x8800 },
110 { 0xFFF0000, 0x8FFF }, { 0x10000000, 0x9000 },
111 // Transition into the largest exponent.
112 { 0x1FFE0000000, 0xF7FF}, { 0x20000000000, 0xF800},
113 { 0x20040000000, 0xF801},
114 // Transition into the max value.
115 { 0x3FF80000000, 0xFFFE}, { 0x3FFC0000000, 0xFFFF},
117 int num_test_cases = sizeof(test_cases) / sizeof(test_cases[0]);
119 for (int i = 0; i < num_test_cases; ++i) {
120 QuicDataReader reader(reinterpret_cast<char*>(&test_cases[i].encoded), 2);
121 uint64 value;
122 EXPECT_TRUE(reader.ReadUFloat16(&value));
123 EXPECT_EQ(test_cases[i].decoded, value);
127 TEST(QuicDataWriterTest, RoundTripUFloat16) {
128 // Just test all 16-bit encoded values. 0 and max already tested above.
129 uint64 previous_value = 0;
130 for (uint16 i = 1; i < 0xFFFF; ++i) {
131 // Read the two bytes.
132 QuicDataReader reader(reinterpret_cast<char*>(&i), 2);
133 uint64 value;
134 // All values must be decodable.
135 EXPECT_TRUE(reader.ReadUFloat16(&value));
136 // Check that small numbers represent themselves
137 if (i < 4097)
138 EXPECT_EQ(i, value);
139 // Check there's monotonic growth.
140 EXPECT_LT(previous_value, value);
141 // Check that precision is within 0.5% away from the denormals.
142 if (i > 2000)
143 EXPECT_GT(previous_value * 1005, value * 1000);
144 // Check we're always within the promised range.
145 EXPECT_LT(value, GG_UINT64_C(0x3FFC0000000));
146 previous_value = value;
147 QuicDataWriter writer(6);
148 EXPECT_TRUE(writer.WriteUFloat16(value - 1));
149 EXPECT_TRUE(writer.WriteUFloat16(value));
150 EXPECT_TRUE(writer.WriteUFloat16(value + 1));
151 scoped_ptr<char[]> data(writer.take());
152 // Check minimal decoding (previous decoding has previous encoding).
153 EXPECT_EQ(i-1, *reinterpret_cast<uint16*>(data.get()));
154 // Check roundtrip.
155 EXPECT_EQ(i, *reinterpret_cast<uint16*>(data.get() + 2));
156 // Check next decoding.
157 EXPECT_EQ(i < 4096? i+1 : i, *reinterpret_cast<uint16*>(data.get() + 4));
161 } // namespace
162 } // namespace test
163 } // namespace net