Flutter Engine
The Flutter Engine
datastream_test.cc
Go to the documentation of this file.
1// Copyright (c) 2020, the Dart project authors. Please see the AUTHORS file
2// for details. All rights reserved. Use of this source code is governed by a
3// BSD-style license that can be found in the LICENSE file.
4
5#include "vm/datastream.h"
6
7#include "platform/assert.h"
8#include "vm/unit_test.h"
9
10namespace dart {
11
12// As a baseline, testing encodings of all numbers with bit size <= 16, as
13// that's both a reasonable amount of numbers to iterate over and that way we
14// test all 1-byte, all 2-byte, and some 3-byte encodings.
15static constexpr intptr_t kUnsignedEnd = kMaxUint16;
16static constexpr intptr_t kSignedStart = kMinInt16;
17static constexpr intptr_t kSignedEnd = kMaxInt16;
18
19// Testing some numbers with first, second, and/or third MSBs set.
20#define DEFINE_LARGE_CONSTANTS(T) \
21 using Unsigned = typename std::make_unsigned<T>::type; \
22 constexpr T all_ones = static_cast<T>(-1); /* 111... */ \
23 constexpr T min = \
24 static_cast<T>(static_cast<Unsigned>(1) \
25 << (kBitsPerByte * sizeof(T) - 1)); /* 100... */ \
26 constexpr T max = \
27 static_cast<T>(static_cast<Unsigned>(min) - 1); /* 011... */ \
28 constexpr T half_min = min / 2; /* 110... */ \
29 constexpr T half_max = max / 2; /* 001... */
30
31TEST_CASE(BaseWriteStream_Write) {
32 MallocWriteStream writer(1 * KB);
33 for (intptr_t i = kSignedStart; i < kSignedEnd; i++) {
34 writer.Write(i);
35 }
36 DEFINE_LARGE_CONSTANTS(intptr_t);
37 writer.Write(all_ones);
38 writer.Write(min);
39 writer.Write(max);
40 writer.Write(half_min);
41 writer.Write(half_max);
42 ReadStream reader(writer.buffer(), writer.bytes_written());
43 for (intptr_t i = kSignedStart; i < kSignedEnd; i++) {
44 const intptr_t r = reader.Read<intptr_t>();
45 EXPECT_EQ(i, r);
46 }
47 const intptr_t read_all_ones = reader.Read<intptr_t>();
48 EXPECT_EQ(all_ones, read_all_ones);
49 const intptr_t read_min = reader.Read<intptr_t>();
50 EXPECT_EQ(min, read_min);
51 const intptr_t read_max = reader.Read<intptr_t>();
52 EXPECT_EQ(max, read_max);
53 const intptr_t read_half_min = reader.Read<intptr_t>();
54 EXPECT_EQ(half_min, read_half_min);
55 const intptr_t read_half_max = reader.Read<intptr_t>();
56 EXPECT_EQ(half_max, read_half_max);
57}
58
59TEST_CASE(BaseWriteStream_WriteUnsigned) {
60 MallocWriteStream writer(1 * KB);
61 for (uintptr_t i = 0; i < kUnsignedEnd; i++) {
62 writer.WriteUnsigned(i);
63 }
64 DEFINE_LARGE_CONSTANTS(uintptr_t);
65 writer.WriteUnsigned(all_ones);
66 writer.WriteUnsigned(min);
67 writer.WriteUnsigned(max);
68 writer.WriteUnsigned(half_min);
69 writer.WriteUnsigned(half_max);
70 ReadStream reader(writer.buffer(), writer.bytes_written());
71 for (uintptr_t i = 0; i < kUnsignedEnd; i++) {
72 const uintptr_t r = reader.ReadUnsigned<uintptr_t>();
73 EXPECT_EQ(i, r);
74 }
75 const uintptr_t read_all_ones = reader.ReadUnsigned<uintptr_t>();
76 EXPECT_EQ(all_ones, read_all_ones);
77 const uintptr_t read_min = reader.ReadUnsigned<uintptr_t>();
78 EXPECT_EQ(min, read_min);
79 const uintptr_t read_max = reader.ReadUnsigned<uintptr_t>();
80 EXPECT_EQ(max, read_max);
81 const uintptr_t read_half_min = reader.ReadUnsigned<uintptr_t>();
82 EXPECT_EQ(half_min, read_half_min);
83 const uintptr_t read_half_max = reader.ReadUnsigned<uintptr_t>();
84 EXPECT_EQ(half_max, read_half_max);
85}
86
87template <typename T>
88void TestRaw() {
89 MallocWriteStream writer(1 * KB);
90 for (T i = kSignedStart; i < kSignedEnd; i++) {
91 writer.Write(i);
92 }
94 writer.Write(all_ones);
95 writer.Write(min);
96 writer.Write(max);
97 writer.Write(half_min);
98 writer.Write(half_max);
99 ReadStream reader(writer.buffer(), writer.bytes_written());
100 using Raw = ReadStream::Raw<sizeof(T), T>;
101 for (T i = kSignedStart; i < kSignedEnd; i++) {
102 const T r = Raw::Read(&reader);
103 EXPECT_EQ(i, r);
104 }
105 const T read_all_ones = Raw::Read(&reader);
106 EXPECT_EQ(all_ones, read_all_ones);
107 const T read_min = Raw::Read(&reader);
108 EXPECT_EQ(min, read_min);
109 const T read_max = Raw::Read(&reader);
110 EXPECT_EQ(max, read_max);
111 const T read_half_min = Raw::Read(&reader);
112 EXPECT_EQ(half_min, read_half_min);
113 const T read_half_max = Raw::Read(&reader);
114 EXPECT_EQ(half_max, read_half_max);
115}
116
117TEST_CASE(ReadStream_Read16) {
118 TestRaw<int16_t>();
119}
120
121TEST_CASE(ReadStream_Read32) {
122 TestRaw<int32_t>();
123}
124
125TEST_CASE(ReadStream_Read64) {
126 TestRaw<int64_t>();
127}
128
129TEST_CASE(BaseWriteStream_WriteLEB128) {
130 MallocWriteStream writer(1 * KB);
131 for (uintptr_t i = 0; i < kUnsignedEnd; i++) {
132 writer.WriteLEB128(i);
133 }
134 DEFINE_LARGE_CONSTANTS(uintptr_t);
135 writer.WriteLEB128(all_ones);
136 writer.WriteLEB128(min);
137 writer.WriteLEB128(max);
138 writer.WriteLEB128(half_min);
139 writer.WriteLEB128(half_max);
140 ReadStream reader(writer.buffer(), writer.bytes_written());
141 for (uintptr_t i = 0; i < kUnsignedEnd; i++) {
142 const uintptr_t r = reader.ReadLEB128();
143 EXPECT_EQ(i, r);
144 }
145 const uintptr_t read_all_ones = reader.ReadLEB128();
146 EXPECT_EQ(all_ones, read_all_ones);
147 const uintptr_t read_min = reader.ReadLEB128();
148 EXPECT_EQ(min, read_min);
149 const uintptr_t read_max = reader.ReadLEB128();
150 EXPECT_EQ(max, read_max);
151 const uintptr_t read_half_min = reader.ReadLEB128();
152 EXPECT_EQ(half_min, read_half_min);
153 const uintptr_t read_half_max = reader.ReadLEB128();
154 EXPECT_EQ(half_max, read_half_max);
155}
156
157TEST_CASE(BaseWriteStream_WriteSLEB128) {
158 MallocWriteStream writer(1 * KB);
159 for (intptr_t i = kSignedStart; i < kSignedEnd; i++) {
160 writer.WriteSLEB128(i);
161 }
162 DEFINE_LARGE_CONSTANTS(intptr_t);
163 writer.WriteSLEB128(all_ones);
164 writer.WriteSLEB128(min);
165 writer.WriteSLEB128(max);
166 writer.WriteSLEB128(half_min);
167 writer.WriteSLEB128(half_max);
168 ReadStream reader(writer.buffer(), writer.bytes_written());
169 for (intptr_t i = kSignedStart; i < kSignedEnd; i++) {
170 const intptr_t r = reader.ReadSLEB128();
171 EXPECT_EQ(i, r);
172 }
173 const intptr_t read_all_ones = reader.ReadSLEB128();
174 EXPECT_EQ(all_ones, read_all_ones);
175 const intptr_t read_min = reader.ReadSLEB128();
176 EXPECT_EQ(min, read_min);
177 const intptr_t read_max = reader.ReadSLEB128();
178 EXPECT_EQ(max, read_max);
179 const intptr_t read_half_min = reader.ReadSLEB128();
180 EXPECT_EQ(half_min, read_half_min);
181 const intptr_t read_half_max = reader.ReadSLEB128();
182 EXPECT_EQ(half_max, read_half_max);
183}
184
185} // namespace dart
C::only_if_signed< T, void > WriteSLEB128(T value)
Definition: datastream.h:514
void WriteUnsigned(T value)
Definition: datastream.h:400
DART_FORCE_INLINE intptr_t bytes_written() const
Definition: datastream.h:338
void Write(T value)
Definition: datastream.h:463
C::only_if_unsigned< T, void > WriteLEB128(T value)
Definition: datastream.h:489
uint8_t * buffer() const
Definition: datastream.h:615
C::only_if_unsigned< T, T > ReadSLEB128()
Definition: datastream.h:194
C::only_if_unsigned< T, T > ReadLEB128()
Definition: datastream.h:173
#define DEFINE_LARGE_CONSTANTS(T)
static float max(float r, float g, float b)
Definition: hsl.cpp:49
static float min(float r, float g, float b)
Definition: hsl.cpp:48
SK_API bool Read(SkStreamSeekable *src, SkDocumentPage *dstArray, int dstArrayCount, const SkDeserialProcs *=nullptr)
Definition: dart_vm.cc:33
constexpr int16_t kMaxInt16
Definition: globals.h:480
static constexpr intptr_t kSignedEnd
void TestRaw()
constexpr intptr_t KB
Definition: globals.h:528
TEST_CASE(DirectoryCurrent)
static constexpr intptr_t kSignedStart
constexpr int16_t kMinInt16
Definition: globals.h:479
static constexpr intptr_t kUnsignedEnd
constexpr uint16_t kMaxUint16
Definition: globals.h:481
#define T
Definition: precompiler.cc:65