• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //
2 // Copyright 2021 The ANGLE Project Authors. All rights reserved.
3 // Use of this source code is governed by a BSD-style license that can be
4 // found in the LICENSE file.
5 //
6 // JsonSerializer_unittests-cpp: Unit tests for the JSON based serializer
7 //
8 
9 #if defined(ANGLE_HAS_RAPIDJSON)
10 #    include "JsonSerializer.h"
11 
12 #    include <gtest/gtest.h>
13 
14 class JsonSerializerTest : public ::testing::Test
15 {
16   protected:
17     void SetUp() override;
18     void check(const std::string &expect);
19 
20     angle::JsonSerializer js;
21 };
22 
23 // Test writing one integer value
TEST_F(JsonSerializerTest,NamedIntValue1)24 TEST_F(JsonSerializerTest, NamedIntValue1)
25 {
26     js.addScalar("test1", 1);
27 
28     const std::string expect =
29         R"({
30     "context": {
31         "test1": 1
32     }
33 })";
34     check(expect);
35 }
36 
37 // Test writing one long value
TEST_F(JsonSerializerTest,NamedLongValue)38 TEST_F(JsonSerializerTest, NamedLongValue)
39 {
40     long v = -12;
41     js.addScalar("test1", v);
42 
43     const std::string expect =
44         R"({
45     "context": {
46         "test1": -12
47     }
48 })";
49     check(expect);
50 }
51 
52 // Test writing one unsigned long value
TEST_F(JsonSerializerTest,NamedULongValue)53 TEST_F(JsonSerializerTest, NamedULongValue)
54 {
55     unsigned long v = 12;
56     js.addScalar("test1", v);
57 
58     const std::string expect =
59         R"({
60     "context": {
61         "test1": 12
62     }
63 })";
64     check(expect);
65 }
66 
67 // Test writing another integer value
TEST_F(JsonSerializerTest,NamedIntValue2)68 TEST_F(JsonSerializerTest, NamedIntValue2)
69 {
70     js.addScalar("test2", 2);
71 
72     const std::string expect =
73         R"({
74     "context": {
75         "test2": 2
76     }
77 })";
78 
79     check(expect);
80 }
81 
82 // Test writing one string value
TEST_F(JsonSerializerTest,NamedStringValue)83 TEST_F(JsonSerializerTest, NamedStringValue)
84 {
85     js.addCString("test2", "value");
86 
87     const std::string expect =
88         R"({
89     "context": {
90         "test2": "value"
91     }
92 })";
93 
94     check(expect);
95 }
96 
97 // Test writing one byte array
98 // Since he serialiter is only used for testing we don't store
99 // the whole byte array, but only it's SHA1 checksum
TEST_F(JsonSerializerTest,ByteArrayValue)100 TEST_F(JsonSerializerTest, ByteArrayValue)
101 {
102     const uint8_t value[5] = {10, 0, 0xcc, 0xff, 0xaa};
103     js.addBlob("test2", value, 5);
104 
105     const std::string expect =
106         R"({
107     "context": {
108         "test2-hash": "SHA1:4315724B1AB1EB2C0128E8E9DAD6D76254BA711D",
109         "test2-raw[0-4]": [
110             10,
111             0,
112             204,
113             255,
114             170
115         ]
116     }
117 })";
118 
119     check(expect);
120 }
121 
122 // Test writing one vector of integer values
TEST_F(JsonSerializerTest,IntVectorValue)123 TEST_F(JsonSerializerTest, IntVectorValue)
124 {
125     std::vector<int> v = {0, 1, -1};
126 
127     js.addVector("test2", v);
128 
129     const std::string expect =
130         R"({
131     "context": {
132         "test2": [
133             0,
134             1,
135             -1
136         ]
137     }
138 })";
139 
140     check(expect);
141 }
142 
143 // Test writing one vector of integer values
TEST_F(JsonSerializerTest,IntVectorAsBlobValue)144 TEST_F(JsonSerializerTest, IntVectorAsBlobValue)
145 {
146     std::vector<int> v = {0, 1, -1};
147 
148     js.addVectorAsHash("test2", v);
149     const std::string expect =
150         R"({
151     "context": {
152         "test2-hash": "SHA1:6216A439C16A113E2F1E53AB63FB88877D3597F5",
153         "test2-raw[0-11]": [
154             0,
155             0,
156             0,
157             0,
158             1,
159             0,
160             0,
161             0,
162             255,
163             255,
164             255,
165             255
166         ]
167     }
168 })";
169     check(expect);
170 }
171 
172 // Test unsorted input gets sorted
TEST_F(JsonSerializerTest,SortValues1)173 TEST_F(JsonSerializerTest, SortValues1)
174 {
175     js.addScalar("b", 1.0);
176     js.addScalar("a", 2.0);
177     const std::string expect =
178         R"({
179     "context": {
180         "a": 2.0,
181         "b": 1.0
182     }
183 })";
184     check(expect);
185 }
186 
187 // Test writing one vector of short integer values
TEST_F(JsonSerializerTest,ShortVectorAsBlobValue)188 TEST_F(JsonSerializerTest, ShortVectorAsBlobValue)
189 {
190     std::vector<short> v = {0, 1, -1};
191 
192     js.addVectorAsHash("test2", v);
193     const std::string expect =
194         R"({
195     "context": {
196         "test2-hash": "SHA1:0BA7C0DE700CE0F8018D084B8CF447B150A9465D",
197         "test2-raw[0-5]": [
198             0,
199             0,
200             1,
201             0,
202             255,
203             255
204         ]
205     }
206 })";
207     check(expect);
208 }
209 
210 // Test adding the same key twice
TEST_F(JsonSerializerTest,KeyUsedTwice)211 TEST_F(JsonSerializerTest, KeyUsedTwice)
212 {
213     js.addScalar("a", 1.0);
214     js.addScalar("a", 1.0);
215 
216     const std::string expect =
217         R"({
218     "context": {
219         "a": 1.0,
220         "a": 1.0
221     }
222 })";
223 
224     check(expect);
225 }
226 
227 // Test writing boolean values
TEST_F(JsonSerializerTest,NamedBoolValues)228 TEST_F(JsonSerializerTest, NamedBoolValues)
229 {
230     js.addScalar("test_false", false);
231     js.addScalar("test_true", true);
232 
233     const std::string expect =
234         R"({
235     "context": {
236         "test_false": false,
237         "test_true": true
238     }
239 })";
240 
241     check(expect);
242 }
243 
244 // test writing two values in a sub-group
TEST_F(JsonSerializerTest,GroupedIntValue)245 TEST_F(JsonSerializerTest, GroupedIntValue)
246 {
247     js.startGroup("group");
248     js.addScalar("test1", 1);
249     js.addScalar("test2", 2);
250     js.endGroup();
251 
252     const std::string expect =
253         R"({
254     "context": {
255         "group": {
256             "test1": 1,
257             "test2": 2
258         }
259     }
260 })";
261 
262     check(expect);
263 }
264 
SetUp()265 void JsonSerializerTest::SetUp()
266 {
267     js.startDocument("context");
268 }
269 
check(const std::string & expect)270 void JsonSerializerTest::check(const std::string &expect)
271 {
272     js.endDocument();
273     EXPECT_EQ(js.data(), expect);
274     EXPECT_EQ(js.length(), expect.length());
275     std::vector<uint8_t> expect_as_ubyte(expect.begin(), expect.end());
276     EXPECT_EQ(js.getData(), expect_as_ubyte);
277 }
278 
279 #endif
280