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