gem5  v22.0.0.2
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 
35 using namespace gem5;
36 
37 TEST(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 
79 TEST(SerializeTest, ParseParamUint32)
80 {
81  ParseParam<uint32_t> parser;
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 
124 TEST(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 
173 TEST(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 
213 TEST(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  EXPECT_FALSE(parser.parse("255", value));
228  EXPECT_TRUE(parser.parse("-128", value));
229  EXPECT_EQ(char(-128), value);
230 
231  // 16-bit values
232  EXPECT_FALSE(parser.parse("1000", value));
233  EXPECT_FALSE(parser.parse("-1000", value));
234 
235  // 32-bit values
236  EXPECT_FALSE(parser.parse("2147483648", value));
237  EXPECT_FALSE(parser.parse("-1073741824", value));
238 
239  // Doubles
240  EXPECT_FALSE(parser.parse("123456.789", value));
241  EXPECT_FALSE(parser.parse("-123456.789", value));
242  EXPECT_FALSE(parser.parse("9.87654e+06", value));
243 
244  // Characters
245  EXPECT_TRUE(parser.parse("69", value));
246  EXPECT_EQ('E', value);
247  EXPECT_TRUE(parser.parse("97", value));
248  EXPECT_EQ('a', value);
249 
250  // Strings
251  EXPECT_FALSE(parser.parse("Test", value));
252 }
253 
254 TEST(SerializeTest, ParseParamString)
255 {
257  std::string value("");
258 
259  // Zero
260  EXPECT_TRUE(parser.parse("0", value));
261  EXPECT_EQ("0", value);
262 
263  // Booleans
264  EXPECT_TRUE(parser.parse("true", value));
265  EXPECT_EQ("true", value);
266  EXPECT_TRUE(parser.parse("false", value));
267  EXPECT_EQ("false", value);
268 
269  // 8-bit values
270  EXPECT_TRUE(parser.parse("255", value));
271  EXPECT_EQ("255", value);
272  EXPECT_TRUE(parser.parse("-128", value));
273  EXPECT_EQ("-128", value);
274 
275  // 16-bit values
276  EXPECT_TRUE(parser.parse("1000", value));
277  EXPECT_EQ("1000", value);
278  EXPECT_TRUE(parser.parse("-1000", value));
279  EXPECT_EQ("-1000", value);
280 
281  // 32-bit values
282  EXPECT_TRUE(parser.parse("2147483648", value));
283  EXPECT_EQ("2147483648", value);
284  EXPECT_TRUE(parser.parse("-1073741824", value));
285  EXPECT_EQ("-1073741824", value);
286 
287  // Doubles
288  EXPECT_TRUE(parser.parse("123456.789", value));
289  EXPECT_EQ("123456.789", value);
290  EXPECT_TRUE(parser.parse("-123456.789", value));
291  EXPECT_EQ("-123456.789", value);
292  EXPECT_TRUE(parser.parse("9.87654e+06", value));
293  EXPECT_EQ("9.87654e+06", value);
294 
295  // Characters
296  EXPECT_TRUE(parser.parse("69", value));
297  EXPECT_EQ("69", value);
298  EXPECT_TRUE(parser.parse("97", value));
299  EXPECT_EQ("97", value);
300 
301  // Strings
302  EXPECT_TRUE(parser.parse("Test", value));
303  EXPECT_EQ("Test", value);
304 }
305 
306 TEST(SerializeTest, ShowParamInt8)
307 {
308  ShowParam<int8_t> parser;
309  std::stringstream ss;
310 
311  parser.show(ss, 0);
312  EXPECT_EQ("0", ss.str());
313  ss.str("");
314  parser.show(ss, 127);
315  EXPECT_EQ("127", ss.str());
316  ss.str("");
317  parser.show(ss, -128);
318  EXPECT_EQ("-128", ss.str());
319  ss.str("");
320 }
321 
322 TEST(SerializeTest, ShowParamUint32)
323 {
324  ShowParam<uint32_t> parser;
325  std::stringstream ss;
326 
327  parser.show(ss, 0);
328  EXPECT_EQ("0", ss.str());
329  ss.str("");
330  parser.show(ss, 255);
331  EXPECT_EQ("255", ss.str());
332  ss.str("");
333  parser.show(ss, 1000);
334  EXPECT_EQ("1000", ss.str());
335  ss.str("");
336  parser.show(ss, 2147483648);
337  EXPECT_EQ("2147483648", ss.str());
338  ss.str("");
339  parser.show(ss, (double)123456.789);
340  EXPECT_EQ("123456", ss.str());
341  ss.str("");
342  parser.show(ss, 9.87654e+06);
343  EXPECT_EQ("9876540", ss.str());
344  ss.str("");
345 }
346 
351 TEST(SerializeTest, ShowParamDouble)
352 {
353  ShowParam<double> parser;
354  std::stringstream ss;
355 
356  parser.show(ss, 0);
357  EXPECT_EQ("0", ss.str());
358  ss.str("");
359  parser.show(ss, 255);
360  EXPECT_EQ("255", ss.str());
361  ss.str("");
362  parser.show(ss, -1000);
363  EXPECT_EQ("-1000", ss.str());
364  ss.str("");
365  parser.show(ss, 123456.789);
366  EXPECT_EQ("123457", ss.str());
367  ss.str("");
368  parser.show(ss, -123456.789);
369  EXPECT_EQ("-123457", ss.str());
370  ss.str("");
371  parser.show(ss, 1234567.89);
372  EXPECT_EQ("1.23457e+06", ss.str());
373  ss.str("");
374  parser.show(ss, -1234567.89);
375  EXPECT_EQ("-1.23457e+06", ss.str());
376  ss.str("");
377  parser.show(ss, 9.87654e+06);
378  EXPECT_EQ("9.87654e+06", ss.str());
379  ss.str("");
380 }
381 
382 TEST(SerializeTest, ShowParamBool)
383 {
384  ShowParam<bool> parser;
385  std::stringstream ss;
386 
387  parser.show(ss, true);
388  EXPECT_EQ("true", ss.str());
389  ss.str("");
390  parser.show(ss, false);
391  EXPECT_EQ("false", ss.str());
392  ss.str("");
393 }
394 
395 TEST(SerializeTest, ShowParamChar)
396 {
397  ShowParam<char> parser;
398  std::stringstream ss;
399 
400  parser.show(ss, 'E');
401  EXPECT_EQ("69", ss.str()); // int('E')=69
402  ss.str("");
403  parser.show(ss, 'a');
404  EXPECT_EQ("97", ss.str()); // int('a')=97
405  ss.str("");
406 }
407 
408 TEST(SerializeTest, ShowParamString)
409 {
410  ShowParam<std::string> parser;
411  std::stringstream ss;
412 
413  parser.show(ss, "test");
414  EXPECT_EQ("test", ss.str());
415  ss.str("");
416  parser.show(ss, "tEsT");
417  EXPECT_EQ("tEsT", ss.str());
418  ss.str("");
419 }
gem5::ParseParam< std::string >
Definition: serialize_handlers.hh:102
gem5::ArmISA::e
Bitfield< 9 > e
Definition: misc_types.hh:65
gem5::ParseParam< std::string >::parse
static bool parse(const std::string &s, std::string &value)
Definition: serialize_handlers.hh:105
gem5::ShowParam::show
static void show(std::ostream &os, const T &value)
Definition: serialize_handlers.hh:127
gem5::ShowParam< bool >
Definition: serialize_handlers.hh:148
gem5::ShowParam
Definition: serialize_handlers.hh:125
TEST
TEST(SerializeTest, ParseParamInt8)
Definition: serialize_handlers.test.cc:37
gem5::ParseParam< bool >::parse
static bool parse(const std::string &s, bool &value)
Definition: serialize_handlers.hh:95
gem5::ParseParam< bool >
Definition: serialize_handlers.hh:92
ss
std::stringstream ss
Definition: trace.test.cc:45
serialize_handlers.hh
gem5::ShowParam< bool >::show
static void show(std::ostream &os, const bool &value)
Definition: serialize_handlers.hh:151
gem5::ParseParam
Definition: serialize_handlers.hh:78
gem5
Reference material can be found at the JEDEC website: UFS standard http://www.jedec....
Definition: gpu_translation_state.hh:37

Generated on Thu Jul 28 2022 13:32:37 for gem5 by doxygen 1.8.17