gem5 v24.0.0.0
Loading...
Searching...
No Matches
serialize_handlers.test.cc
Go to the documentation of this file.
1/*
2 * Copyright 2021 Daniel R. Carvalho
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
6 * met: redistributions of source code must retain the above copyright
7 * notice, this list of conditions and the following disclaimer;
8 * redistributions in binary form must reproduce the above copyright
9 * notice, this list of conditions and the following disclaimer in the
10 * documentation and/or other materials provided with the distribution;
11 * neither the name of the copyright holders nor the names of its
12 * contributors may be used to endorse or promote products derived from
13 * this software without specific prior written permission.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
18 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
19 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
21 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27
28#include <gtest/gtest.h>
29
30#include <cstdint>
31#include <string>
32
34
35using namespace gem5;
36
37TEST(SerializeTest, ParseParamInt8)
38{
39 ParseParam<int8_t> parser;
40 int8_t value(0);
41
42 // Zero
43 EXPECT_TRUE(parser.parse("0", value));
44 EXPECT_EQ(0, value);
45
46 // Booleans
47 EXPECT_FALSE(parser.parse("true", value));
48 EXPECT_FALSE(parser.parse("false", value));
49
50 // 8-bit values
51 EXPECT_FALSE(parser.parse("255", value));
52 EXPECT_TRUE(parser.parse("-128", value));
53 EXPECT_EQ(-128, value);
54
55 // 16-bit values
56 EXPECT_FALSE(parser.parse("1000", value));
57 EXPECT_FALSE(parser.parse("-1000", value));
58
59 // 32-bit values
60 EXPECT_FALSE(parser.parse("2147483648", value));
61 EXPECT_FALSE(parser.parse("-1073741824", value));
62
63 // Doubles (scientific numbers should not be converted to integers
64 // correctly)
65 EXPECT_FALSE(parser.parse("123456.789", value));
66 EXPECT_FALSE(parser.parse("-123456.789", value));
67 EXPECT_FALSE(parser.parse("9.87654e+06", value));
68
69 // Characters
70 EXPECT_TRUE(parser.parse("69", value));
71 EXPECT_EQ(69, value);
72 EXPECT_TRUE(parser.parse("97", value));
73 EXPECT_EQ(97, value);
74
75 // Strings
76 EXPECT_FALSE(parser.parse("Test", value));
77}
78
79TEST(SerializeTest, ParseParamUint32)
80{
82 uint32_t value(0);
83
84 // Zero
85 EXPECT_TRUE(parser.parse("0", value));
86 EXPECT_EQ(0, value);
87
88 // Booleans
89 EXPECT_FALSE(parser.parse("true", value));
90 EXPECT_FALSE(parser.parse("false", value));
91
92 // 8-bit values
93 EXPECT_TRUE(parser.parse("255", value));
94 EXPECT_EQ(255, value);
95 EXPECT_FALSE(parser.parse("-128", value));
96
97 // 16-bit values
98 EXPECT_TRUE(parser.parse("1000", value));
99 EXPECT_EQ(1000, value);
100 EXPECT_FALSE(parser.parse("-1000", value));
101
102 // 32-bit values
103 EXPECT_TRUE(parser.parse("2147483648", value));
104 EXPECT_EQ(2147483648, value);
105 EXPECT_FALSE(parser.parse("-1073741824", value));
106
107 // Doubles (scientific numbers should not be converted to integers
108 // correctly)
109 EXPECT_TRUE(parser.parse("123456.789", value));
110 EXPECT_EQ(123456, value);
111 EXPECT_FALSE(parser.parse("-123456.789", value));
112 EXPECT_FALSE(parser.parse("9.87654e+06", value));
113
114 // Characters
115 EXPECT_TRUE(parser.parse("69", value));
116 EXPECT_EQ(69, value);
117 EXPECT_TRUE(parser.parse("97", value));
118 EXPECT_EQ(97, value);
119
120 // Strings
121 EXPECT_FALSE(parser.parse("Test", value));
122}
123
124TEST(SerializeTest, ParseParamDouble)
125{
126 ParseParam<double> parser;
127 double value(0.0);
128
129 // Zero
130 EXPECT_TRUE(parser.parse("0", value));
131 EXPECT_EQ(0.0, value);
132
133 // Booleans
134 EXPECT_FALSE(parser.parse("true", value));
135 EXPECT_FALSE(parser.parse("false", value));
136
137 // 8-bit values
138 EXPECT_TRUE(parser.parse("255", value));
139 EXPECT_EQ(255.0, value);
140 EXPECT_TRUE(parser.parse("-128", value));
141 EXPECT_EQ(-128.0, value);
142
143 // 16-bit values
144 EXPECT_TRUE(parser.parse("1000", value));
145 EXPECT_EQ(1000.0, value);
146 EXPECT_TRUE(parser.parse("-1000", value));
147 EXPECT_EQ(-1000.0, value);
148
149 // 32-bit values
150 EXPECT_TRUE(parser.parse("2147483648", value));
151 EXPECT_EQ(2147483648.0, value);
152 EXPECT_TRUE(parser.parse("-1073741824", value));
153 EXPECT_EQ(-1073741824.0, value);
154
155 // Doubles
156 EXPECT_TRUE(parser.parse("123456.789", value));
157 EXPECT_EQ(123456.789, value);
158 EXPECT_TRUE(parser.parse("-123456.789", value));
159 EXPECT_EQ(-123456.789, value);
160 EXPECT_TRUE(parser.parse("9.87654e+06", value));
161 EXPECT_EQ(9.87654e+06, value);
162
163 // Characters
164 EXPECT_TRUE(parser.parse("69", value));
165 EXPECT_EQ(69.0, value);
166 EXPECT_TRUE(parser.parse("97", value));
167 EXPECT_EQ(97.0, value);
168
169 // Strings
170 EXPECT_FALSE(parser.parse("Test", value));
171}
172
173TEST(SerializeTest, ParseParamBool)
174{
175 ParseParam<bool> parser;
176 bool value(false);
177
178 // Zero
179 EXPECT_FALSE(parser.parse("0", value));
180
181 // Booleans
182 EXPECT_TRUE(parser.parse("true", value));
183 EXPECT_EQ(true, value);
184 EXPECT_TRUE(parser.parse("false", value));
185 EXPECT_EQ(false, value);
186
187 // 8-bit values
188 EXPECT_FALSE(parser.parse("255", value));
189 EXPECT_FALSE(parser.parse("-128", value));
190
191 // 16-bit values
192 EXPECT_FALSE(parser.parse("1000", value));
193 EXPECT_FALSE(parser.parse("-1000", value));
194
195 // 32-bit values
196 EXPECT_FALSE(parser.parse("2147483648", value));
197 EXPECT_FALSE(parser.parse("-1073741824", value));
198
199 // Doubles
200 EXPECT_FALSE(parser.parse("123456.789", value));
201 EXPECT_FALSE(parser.parse("-123456.789", value));
202 EXPECT_FALSE(parser.parse("9.87654e+06", value));
203
204 // Characters
205 EXPECT_FALSE(parser.parse("69", value));
206 EXPECT_FALSE(parser.parse("97", value));
207
208 // Strings
209 EXPECT_FALSE(parser.parse("Test", value));
210}
211
213TEST(SerializeTest, ParseParamChar)
214{
215 ParseParam<char> parser;
216 char value;
217
218 // Zero
219 EXPECT_TRUE(parser.parse("48", value));
220 EXPECT_EQ('0', value);
221
222 // Booleans
223 EXPECT_FALSE(parser.parse("true", value));
224 EXPECT_FALSE(parser.parse("false", value));
225
226 // 8-bit values
227 if constexpr (std::is_signed_v<char>) {
228 EXPECT_FALSE(parser.parse("255", value));
229 EXPECT_TRUE(parser.parse("-128", value));
230 EXPECT_EQ(char(-128), value);
231 } else {
232 EXPECT_FALSE(parser.parse("256", value));
233 EXPECT_TRUE(parser.parse("255", value));
234 EXPECT_EQ(char(255), value);
235 }
236
237 // 16-bit values
238 EXPECT_FALSE(parser.parse("1000", value));
239 EXPECT_FALSE(parser.parse("-1000", value));
240
241 // 32-bit values
242 EXPECT_FALSE(parser.parse("2147483648", value));
243 EXPECT_FALSE(parser.parse("-1073741824", value));
244
245 // Doubles
246 EXPECT_FALSE(parser.parse("123456.789", value));
247 EXPECT_FALSE(parser.parse("-123456.789", value));
248 EXPECT_FALSE(parser.parse("9.87654e+06", value));
249
250 // Characters
251 EXPECT_TRUE(parser.parse("69", value));
252 EXPECT_EQ('E', value);
253 EXPECT_TRUE(parser.parse("97", value));
254 EXPECT_EQ('a', value);
255
256 // Strings
257 EXPECT_FALSE(parser.parse("Test", value));
258}
259
260TEST(SerializeTest, ParseParamString)
261{
263 std::string value("");
264
265 // Zero
266 EXPECT_TRUE(parser.parse("0", value));
267 EXPECT_EQ("0", value);
268
269 // Booleans
270 EXPECT_TRUE(parser.parse("true", value));
271 EXPECT_EQ("true", value);
272 EXPECT_TRUE(parser.parse("false", value));
273 EXPECT_EQ("false", value);
274
275 // 8-bit values
276 EXPECT_TRUE(parser.parse("255", value));
277 EXPECT_EQ("255", value);
278 EXPECT_TRUE(parser.parse("-128", value));
279 EXPECT_EQ("-128", value);
280
281 // 16-bit values
282 EXPECT_TRUE(parser.parse("1000", value));
283 EXPECT_EQ("1000", value);
284 EXPECT_TRUE(parser.parse("-1000", value));
285 EXPECT_EQ("-1000", value);
286
287 // 32-bit values
288 EXPECT_TRUE(parser.parse("2147483648", value));
289 EXPECT_EQ("2147483648", value);
290 EXPECT_TRUE(parser.parse("-1073741824", value));
291 EXPECT_EQ("-1073741824", value);
292
293 // Doubles
294 EXPECT_TRUE(parser.parse("123456.789", value));
295 EXPECT_EQ("123456.789", value);
296 EXPECT_TRUE(parser.parse("-123456.789", value));
297 EXPECT_EQ("-123456.789", value);
298 EXPECT_TRUE(parser.parse("9.87654e+06", value));
299 EXPECT_EQ("9.87654e+06", value);
300
301 // Characters
302 EXPECT_TRUE(parser.parse("69", value));
303 EXPECT_EQ("69", value);
304 EXPECT_TRUE(parser.parse("97", value));
305 EXPECT_EQ("97", value);
306
307 // Strings
308 EXPECT_TRUE(parser.parse("Test", value));
309 EXPECT_EQ("Test", value);
310}
311
312TEST(SerializeTest, ShowParamInt8)
313{
314 ShowParam<int8_t> parser;
315 std::stringstream ss;
316
317 parser.show(ss, 0);
318 EXPECT_EQ("0", ss.str());
319 ss.str("");
320 parser.show(ss, 127);
321 EXPECT_EQ("127", ss.str());
322 ss.str("");
323 parser.show(ss, -128);
324 EXPECT_EQ("-128", ss.str());
325 ss.str("");
326}
327
328TEST(SerializeTest, ShowParamUint32)
329{
330 ShowParam<uint32_t> parser;
331 std::stringstream ss;
332
333 parser.show(ss, 0);
334 EXPECT_EQ("0", ss.str());
335 ss.str("");
336 parser.show(ss, 255);
337 EXPECT_EQ("255", ss.str());
338 ss.str("");
339 parser.show(ss, 1000);
340 EXPECT_EQ("1000", ss.str());
341 ss.str("");
342 parser.show(ss, 2147483648);
343 EXPECT_EQ("2147483648", ss.str());
344 ss.str("");
345 parser.show(ss, (double)123456.789);
346 EXPECT_EQ("123456", ss.str());
347 ss.str("");
348 parser.show(ss, 9.87654e+06);
349 EXPECT_EQ("9876540", ss.str());
350 ss.str("");
351}
352
357TEST(SerializeTest, ShowParamDouble)
358{
359 ShowParam<double> parser;
360 std::stringstream ss;
361
362 parser.show(ss, 0);
363 EXPECT_EQ("0", ss.str());
364 ss.str("");
365 parser.show(ss, 255);
366 EXPECT_EQ("255", ss.str());
367 ss.str("");
368 parser.show(ss, -1000);
369 EXPECT_EQ("-1000", ss.str());
370 ss.str("");
371 parser.show(ss, 123456.789);
372 EXPECT_EQ("123457", ss.str());
373 ss.str("");
374 parser.show(ss, -123456.789);
375 EXPECT_EQ("-123457", ss.str());
376 ss.str("");
377 parser.show(ss, 1234567.89);
378 EXPECT_EQ("1.23457e+06", ss.str());
379 ss.str("");
380 parser.show(ss, -1234567.89);
381 EXPECT_EQ("-1.23457e+06", ss.str());
382 ss.str("");
383 parser.show(ss, 9.87654e+06);
384 EXPECT_EQ("9.87654e+06", ss.str());
385 ss.str("");
386}
387
388TEST(SerializeTest, ShowParamBool)
389{
390 ShowParam<bool> parser;
391 std::stringstream ss;
392
393 parser.show(ss, true);
394 EXPECT_EQ("true", ss.str());
395 ss.str("");
396 parser.show(ss, false);
397 EXPECT_EQ("false", ss.str());
398 ss.str("");
399}
400
401TEST(SerializeTest, ShowParamChar)
402{
403 ShowParam<char> parser;
404 std::stringstream ss;
405
406 parser.show(ss, 'E');
407 EXPECT_EQ("69", ss.str()); // int('E')=69
408 ss.str("");
409 parser.show(ss, 'a');
410 EXPECT_EQ("97", ss.str()); // int('a')=97
411 ss.str("");
412}
413
414TEST(SerializeTest, ShowParamString)
415{
417 std::stringstream ss;
418
419 parser.show(ss, "test");
420 EXPECT_EQ("test", ss.str());
421 ss.str("");
422 parser.show(ss, "tEsT");
423 EXPECT_EQ("tEsT", ss.str());
424 ss.str("");
425}
Bitfield< 21 > ss
Definition misc_types.hh:60
Copyright (c) 2024 - Pranith Kumar Copyright (c) 2020 Inria All rights reserved.
Definition binary32.hh:36
TEST(SerializeTest, ParseParamInt8)
static void show(std::ostream &os, const T &value)

Generated on Tue Jun 18 2024 16:24:06 for gem5 by doxygen 1.11.0