Prevent app list doodle from being pinch-to-zoomed.
[chromium-blink-merge.git] / net / quic / quic_data_writer_test.cc
bloba7ac2a8ea41461b2eb2443cad5a749452c655fd4
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, SanityCheckUFloat16Consts) {
17 // Check the arithmetic on the constants - otherwise the values below make
18 // no sense.
19 EXPECT_EQ(30, kUFloat16MaxExponent);
20 EXPECT_EQ(11, kUFloat16MantissaBits);
21 EXPECT_EQ(12, kUFloat16MantissaEffectiveBits);
22 EXPECT_EQ(GG_UINT64_C(0x3FFC0000000), kUFloat16MaxValue);
25 TEST(QuicDataWriterTest, WriteUFloat16) {
26 struct TestCase {
27 uint64 decoded;
28 uint16 encoded;
30 TestCase test_cases[] = {
31 // Small numbers represent themselves.
32 { 0, 0 }, { 1, 1 }, { 2, 2 }, { 3, 3 }, { 4, 4 }, { 5, 5 }, { 6, 6 },
33 { 7, 7 }, { 15, 15 }, { 31, 31 }, { 42, 42 }, { 123, 123 }, { 1234, 1234 },
34 // Check transition through 2^11.
35 { 2046, 2046 }, { 2047, 2047 }, { 2048, 2048 }, { 2049, 2049 },
36 // Running out of mantissa at 2^12.
37 { 4094, 4094 }, { 4095, 4095 }, { 4096, 4096 }, { 4097, 4096 },
38 { 4098, 4097 }, { 4099, 4097 }, { 4100, 4098 }, { 4101, 4098 },
39 // Check transition through 2^13.
40 { 8190, 6143 }, { 8191, 6143 }, { 8192, 6144 }, { 8193, 6144 },
41 { 8194, 6144 }, { 8195, 6144 }, { 8196, 6145 }, { 8197, 6145 },
42 // Half-way through the exponents.
43 { 0x7FF8000, 0x87FF }, { 0x7FFFFFF, 0x87FF }, { 0x8000000, 0x8800 },
44 { 0xFFF0000, 0x8FFF }, { 0xFFFFFFF, 0x8FFF }, { 0x10000000, 0x9000 },
45 // Transition into the largest exponent.
46 { 0x1FFFFFFFFFE, 0xF7FF}, { 0x1FFFFFFFFFF, 0xF7FF},
47 { 0x20000000000, 0xF800}, { 0x20000000001, 0xF800},
48 { 0x2003FFFFFFE, 0xF800}, { 0x2003FFFFFFF, 0xF800},
49 { 0x20040000000, 0xF801}, { 0x20040000001, 0xF801},
50 // Transition into the max value and clamping.
51 { 0x3FF80000000, 0xFFFE}, { 0x3FFBFFFFFFF, 0xFFFE},
52 { 0x3FFC0000000, 0xFFFF}, { 0x3FFC0000001, 0xFFFF},
53 { 0x3FFFFFFFFFF, 0xFFFF}, { 0x40000000000, 0xFFFF},
54 { 0xFFFFFFFFFFFFFFFF, 0xFFFF},
56 int num_test_cases = sizeof(test_cases) / sizeof(test_cases[0]);
58 for (int i = 0; i < num_test_cases; ++i) {
59 char buffer[2];
60 QuicDataWriter writer(2, buffer);
61 EXPECT_TRUE(writer.WriteUFloat16(test_cases[i].decoded));
62 EXPECT_EQ(test_cases[i].encoded, *reinterpret_cast<uint16*>(writer.data()));
66 TEST(QuicDataWriterTest, ReadUFloat16) {
67 struct TestCase {
68 uint64 decoded;
69 uint16 encoded;
71 TestCase test_cases[] = {
72 // There are fewer decoding test cases because encoding truncates, and
73 // decoding returns the smallest expansion.
74 // Small numbers represent themselves.
75 { 0, 0 }, { 1, 1 }, { 2, 2 }, { 3, 3 }, { 4, 4 }, { 5, 5 }, { 6, 6 },
76 { 7, 7 }, { 15, 15 }, { 31, 31 }, { 42, 42 }, { 123, 123 }, { 1234, 1234 },
77 // Check transition through 2^11.
78 { 2046, 2046 }, { 2047, 2047 }, { 2048, 2048 }, { 2049, 2049 },
79 // Running out of mantissa at 2^12.
80 { 4094, 4094 }, { 4095, 4095 }, { 4096, 4096 },
81 { 4098, 4097 }, { 4100, 4098 },
82 // Check transition through 2^13.
83 { 8190, 6143 }, { 8192, 6144 }, { 8196, 6145 },
84 // Half-way through the exponents.
85 { 0x7FF8000, 0x87FF }, { 0x8000000, 0x8800 },
86 { 0xFFF0000, 0x8FFF }, { 0x10000000, 0x9000 },
87 // Transition into the largest exponent.
88 { 0x1FFE0000000, 0xF7FF}, { 0x20000000000, 0xF800},
89 { 0x20040000000, 0xF801},
90 // Transition into the max value.
91 { 0x3FF80000000, 0xFFFE}, { 0x3FFC0000000, 0xFFFF},
93 int num_test_cases = sizeof(test_cases) / sizeof(test_cases[0]);
95 for (int i = 0; i < num_test_cases; ++i) {
96 QuicDataReader reader(reinterpret_cast<char*>(&test_cases[i].encoded), 2);
97 uint64 value;
98 EXPECT_TRUE(reader.ReadUFloat16(&value));
99 EXPECT_EQ(test_cases[i].decoded, value);
103 TEST(QuicDataWriterTest, RoundTripUFloat16) {
104 // Just test all 16-bit encoded values. 0 and max already tested above.
105 uint64 previous_value = 0;
106 for (uint16 i = 1; i < 0xFFFF; ++i) {
107 // Read the two bytes.
108 QuicDataReader reader(reinterpret_cast<char*>(&i), 2);
109 uint64 value;
110 // All values must be decodable.
111 EXPECT_TRUE(reader.ReadUFloat16(&value));
112 // Check that small numbers represent themselves
113 if (i < 4097)
114 EXPECT_EQ(i, value);
115 // Check there's monotonic growth.
116 EXPECT_LT(previous_value, value);
117 // Check that precision is within 0.5% away from the denormals.
118 if (i > 2000)
119 EXPECT_GT(previous_value * 1005, value * 1000);
120 // Check we're always within the promised range.
121 EXPECT_LT(value, GG_UINT64_C(0x3FFC0000000));
122 previous_value = value;
123 char buffer[6];
124 QuicDataWriter writer(6, buffer);
125 EXPECT_TRUE(writer.WriteUFloat16(value - 1));
126 EXPECT_TRUE(writer.WriteUFloat16(value));
127 EXPECT_TRUE(writer.WriteUFloat16(value + 1));
128 // Check minimal decoding (previous decoding has previous encoding).
129 EXPECT_EQ(i - 1, *reinterpret_cast<uint16*>(writer.data()));
130 // Check roundtrip.
131 EXPECT_EQ(i, *reinterpret_cast<uint16*>(writer.data() + 2));
132 // Check next decoding.
133 EXPECT_EQ(i < 4096 ? i + 1 : i,
134 *reinterpret_cast<uint16*>(writer.data() + 4));
138 } // namespace
139 } // namespace test
140 } // namespace net