1 // Copyright (c) 2015-2016 The Khronos Group Inc.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 #include <limits>
16 #include <string>
17 #include <vector>
18
19 #include "gmock/gmock.h"
20 #include "source/util/parse_number.h"
21 #include "spirv-tools/libspirv.h"
22
23 namespace spvtools {
24 namespace utils {
25 namespace {
26
27 using testing::Eq;
28 using testing::IsNull;
29 using testing::NotNull;
30
TEST(ParseNarrowSignedIntegers,Sample)31 TEST(ParseNarrowSignedIntegers, Sample) {
32 int16_t i16;
33
34 EXPECT_FALSE(ParseNumber(nullptr, &i16));
35 EXPECT_FALSE(ParseNumber("", &i16));
36 EXPECT_FALSE(ParseNumber("0=", &i16));
37
38 EXPECT_TRUE(ParseNumber("0", &i16));
39 EXPECT_EQ(0, i16);
40 EXPECT_TRUE(ParseNumber("32767", &i16));
41 EXPECT_EQ(32767, i16);
42 EXPECT_TRUE(ParseNumber("-32768", &i16));
43 EXPECT_EQ(-32768, i16);
44 EXPECT_TRUE(ParseNumber("-0", &i16));
45 EXPECT_EQ(0, i16);
46
47 // These are out of range, so they should return an error.
48 // The error code depends on whether this is an optional value.
49 EXPECT_FALSE(ParseNumber("32768", &i16));
50 EXPECT_FALSE(ParseNumber("65535", &i16));
51
52 // Check hex parsing.
53 EXPECT_TRUE(ParseNumber("0x7fff", &i16));
54 EXPECT_EQ(32767, i16);
55 // This is out of range.
56 EXPECT_FALSE(ParseNumber("0xffff", &i16));
57 }
58
TEST(ParseNarrowUnsignedIntegers,Sample)59 TEST(ParseNarrowUnsignedIntegers, Sample) {
60 uint16_t u16;
61
62 EXPECT_FALSE(ParseNumber(nullptr, &u16));
63 EXPECT_FALSE(ParseNumber("", &u16));
64 EXPECT_FALSE(ParseNumber("0=", &u16));
65
66 EXPECT_TRUE(ParseNumber("0", &u16));
67 EXPECT_EQ(0, u16);
68 EXPECT_TRUE(ParseNumber("65535", &u16));
69 EXPECT_EQ(65535, u16);
70 EXPECT_FALSE(ParseNumber("65536", &u16));
71
72 // We don't care about -0 since it's rejected at a higher level.
73 EXPECT_FALSE(ParseNumber("-1", &u16));
74 EXPECT_TRUE(ParseNumber("0xffff", &u16));
75 EXPECT_EQ(0xffff, u16);
76 EXPECT_FALSE(ParseNumber("0x10000", &u16));
77 }
78
TEST(ParseSignedIntegers,Sample)79 TEST(ParseSignedIntegers, Sample) {
80 int32_t i32;
81
82 // Invalid parse.
83 EXPECT_FALSE(ParseNumber(nullptr, &i32));
84 EXPECT_FALSE(ParseNumber("", &i32));
85 EXPECT_FALSE(ParseNumber("0=", &i32));
86
87 // Decimal values.
88 EXPECT_TRUE(ParseNumber("0", &i32));
89 EXPECT_EQ(0, i32);
90 EXPECT_TRUE(ParseNumber("2147483647", &i32));
91 EXPECT_EQ(std::numeric_limits<int32_t>::max(), i32);
92 EXPECT_FALSE(ParseNumber("2147483648", &i32));
93 EXPECT_TRUE(ParseNumber("-0", &i32));
94 EXPECT_EQ(0, i32);
95 EXPECT_TRUE(ParseNumber("-1", &i32));
96 EXPECT_EQ(-1, i32);
97 EXPECT_TRUE(ParseNumber("-2147483648", &i32));
98 EXPECT_EQ(std::numeric_limits<int32_t>::min(), i32);
99
100 // Hex values.
101 EXPECT_TRUE(ParseNumber("0x7fffffff", &i32));
102 EXPECT_EQ(std::numeric_limits<int32_t>::max(), i32);
103 EXPECT_FALSE(ParseNumber("0x80000000", &i32));
104 EXPECT_TRUE(ParseNumber("-0x000", &i32));
105 EXPECT_EQ(0, i32);
106 EXPECT_TRUE(ParseNumber("-0x001", &i32));
107 EXPECT_EQ(-1, i32);
108 EXPECT_TRUE(ParseNumber("-0x80000000", &i32));
109 EXPECT_EQ(std::numeric_limits<int32_t>::min(), i32);
110 }
111
TEST(ParseUnsignedIntegers,Sample)112 TEST(ParseUnsignedIntegers, Sample) {
113 uint32_t u32;
114
115 // Invalid parse.
116 EXPECT_FALSE(ParseNumber(nullptr, &u32));
117 EXPECT_FALSE(ParseNumber("", &u32));
118 EXPECT_FALSE(ParseNumber("0=", &u32));
119
120 // Valid values.
121 EXPECT_TRUE(ParseNumber("0", &u32));
122 EXPECT_EQ(0u, u32);
123 EXPECT_TRUE(ParseNumber("4294967295", &u32));
124 EXPECT_EQ(std::numeric_limits<uint32_t>::max(), u32);
125 EXPECT_FALSE(ParseNumber("4294967296", &u32));
126
127 // Hex values.
128 EXPECT_TRUE(ParseNumber("0xffffffff", &u32));
129 EXPECT_EQ(std::numeric_limits<uint32_t>::max(), u32);
130
131 // We don't care about -0 since it's rejected at a higher level.
132 EXPECT_FALSE(ParseNumber("-1", &u32));
133 }
134
TEST(ParseWideSignedIntegers,Sample)135 TEST(ParseWideSignedIntegers, Sample) {
136 int64_t i64;
137 EXPECT_FALSE(ParseNumber(nullptr, &i64));
138 EXPECT_FALSE(ParseNumber("", &i64));
139 EXPECT_FALSE(ParseNumber("0=", &i64));
140 EXPECT_TRUE(ParseNumber("0", &i64));
141 EXPECT_EQ(0, i64);
142 EXPECT_TRUE(ParseNumber("0x7fffffffffffffff", &i64));
143 EXPECT_EQ(0x7fffffffffffffff, i64);
144 EXPECT_TRUE(ParseNumber("-0", &i64));
145 EXPECT_EQ(0, i64);
146 EXPECT_TRUE(ParseNumber("-1", &i64));
147 EXPECT_EQ(-1, i64);
148 }
149
TEST(ParseWideUnsignedIntegers,Sample)150 TEST(ParseWideUnsignedIntegers, Sample) {
151 uint64_t u64;
152 EXPECT_FALSE(ParseNumber(nullptr, &u64));
153 EXPECT_FALSE(ParseNumber("", &u64));
154 EXPECT_FALSE(ParseNumber("0=", &u64));
155 EXPECT_TRUE(ParseNumber("0", &u64));
156 EXPECT_EQ(0u, u64);
157 EXPECT_TRUE(ParseNumber("0xffffffffffffffff", &u64));
158 EXPECT_EQ(0xffffffffffffffffULL, u64);
159
160 // We don't care about -0 since it's rejected at a higher level.
161 EXPECT_FALSE(ParseNumber("-1", &u64));
162 }
163
TEST(ParseFloat,Sample)164 TEST(ParseFloat, Sample) {
165 float f;
166
167 EXPECT_FALSE(ParseNumber(nullptr, &f));
168 EXPECT_FALSE(ParseNumber("", &f));
169 EXPECT_FALSE(ParseNumber("0=", &f));
170
171 // These values are exactly representatble.
172 EXPECT_TRUE(ParseNumber("0", &f));
173 EXPECT_EQ(0.0f, f);
174 EXPECT_TRUE(ParseNumber("42", &f));
175 EXPECT_EQ(42.0f, f);
176 EXPECT_TRUE(ParseNumber("2.5", &f));
177 EXPECT_EQ(2.5f, f);
178 EXPECT_TRUE(ParseNumber("-32.5", &f));
179 EXPECT_EQ(-32.5f, f);
180 EXPECT_TRUE(ParseNumber("1e38", &f));
181 EXPECT_EQ(1e38f, f);
182 EXPECT_TRUE(ParseNumber("-1e38", &f));
183 EXPECT_EQ(-1e38f, f);
184 }
185
TEST(ParseFloat,Overflow)186 TEST(ParseFloat, Overflow) {
187 // The assembler parses using HexFloat<FloatProxy<float>>. Make
188 // sure that succeeds for in-range values, and fails for out of
189 // range values. When it does overflow, the value is set to the
190 // nearest finite value, matching C++11 behavior for operator>>
191 // on floating point.
192 HexFloat<FloatProxy<float>> f(0.0f);
193
194 EXPECT_TRUE(ParseNumber("1e38", &f));
195 EXPECT_EQ(1e38f, f.value().getAsFloat());
196 EXPECT_TRUE(ParseNumber("-1e38", &f));
197 EXPECT_EQ(-1e38f, f.value().getAsFloat());
198 EXPECT_FALSE(ParseNumber("1e40", &f));
199 EXPECT_FALSE(ParseNumber("-1e40", &f));
200 EXPECT_FALSE(ParseNumber("1e400", &f));
201 EXPECT_FALSE(ParseNumber("-1e400", &f));
202 }
203
TEST(ParseDouble,Sample)204 TEST(ParseDouble, Sample) {
205 double f;
206
207 EXPECT_FALSE(ParseNumber(nullptr, &f));
208 EXPECT_FALSE(ParseNumber("", &f));
209 EXPECT_FALSE(ParseNumber("0=", &f));
210
211 // These values are exactly representatble.
212 EXPECT_TRUE(ParseNumber("0", &f));
213 EXPECT_EQ(0.0, f);
214 EXPECT_TRUE(ParseNumber("42", &f));
215 EXPECT_EQ(42.0, f);
216 EXPECT_TRUE(ParseNumber("2.5", &f));
217 EXPECT_EQ(2.5, f);
218 EXPECT_TRUE(ParseNumber("-32.5", &f));
219 EXPECT_EQ(-32.5, f);
220 EXPECT_TRUE(ParseNumber("1e38", &f));
221 EXPECT_EQ(1e38, f);
222 EXPECT_TRUE(ParseNumber("-1e38", &f));
223 EXPECT_EQ(-1e38, f);
224 // These are out of range for 32-bit float, but in range for 64-bit float.
225 EXPECT_TRUE(ParseNumber("1e40", &f));
226 EXPECT_EQ(1e40, f);
227 EXPECT_TRUE(ParseNumber("-1e40", &f));
228 EXPECT_EQ(-1e40, f);
229 }
230
TEST(ParseDouble,Overflow)231 TEST(ParseDouble, Overflow) {
232 // The assembler parses using HexFloat<FloatProxy<double>>. Make
233 // sure that succeeds for in-range values, and fails for out of
234 // range values. When it does overflow, the value is set to the
235 // nearest finite value, matching C++11 behavior for operator>>
236 // on floating point.
237 HexFloat<FloatProxy<double>> f(0.0);
238
239 EXPECT_TRUE(ParseNumber("1e38", &f));
240 EXPECT_EQ(1e38, f.value().getAsFloat());
241 EXPECT_TRUE(ParseNumber("-1e38", &f));
242 EXPECT_EQ(-1e38, f.value().getAsFloat());
243 EXPECT_TRUE(ParseNumber("1e40", &f));
244 EXPECT_EQ(1e40, f.value().getAsFloat());
245 EXPECT_TRUE(ParseNumber("-1e40", &f));
246 EXPECT_EQ(-1e40, f.value().getAsFloat());
247 EXPECT_FALSE(ParseNumber("1e400", &f));
248 EXPECT_FALSE(ParseNumber("-1e400", &f));
249 }
250
TEST(ParseFloat16,Overflow)251 TEST(ParseFloat16, Overflow) {
252 // The assembler parses using HexFloat<FloatProxy<Float16>>. Make
253 // sure that succeeds for in-range values, and fails for out of
254 // range values. When it does overflow, the value is set to the
255 // nearest finite value, matching C++11 behavior for operator>>
256 // on floating point.
257 HexFloat<FloatProxy<Float16>> f(0);
258
259 EXPECT_FALSE(ParseNumber(nullptr, &f));
260 EXPECT_TRUE(ParseNumber("-0.0", &f));
261 EXPECT_EQ(uint16_t{0x8000}, f.value().getAsFloat().get_value());
262 EXPECT_TRUE(ParseNumber("1.0", &f));
263 EXPECT_EQ(uint16_t{0x3c00}, f.value().getAsFloat().get_value());
264
265 // Overflows 16-bit but not 32-bit
266 EXPECT_FALSE(ParseNumber("1e38", &f));
267 EXPECT_FALSE(ParseNumber("-1e38", &f));
268
269 // Overflows 32-bit but not 64-bit
270 EXPECT_FALSE(ParseNumber("1e40", &f));
271 EXPECT_FALSE(ParseNumber("-1e40", &f));
272
273 // Overflows 64-bit
274 EXPECT_FALSE(ParseNumber("1e400", &f));
275 EXPECT_FALSE(ParseNumber("-1e400", &f));
276 }
277
AssertEmitFunc(uint32_t)278 void AssertEmitFunc(uint32_t) {
279 ASSERT_FALSE(true)
280 << "Should not call emit() function when the number can not be parsed.";
281 return;
282 }
283
TEST(ParseAndEncodeNarrowSignedIntegers,Invalid)284 TEST(ParseAndEncodeNarrowSignedIntegers, Invalid) {
285 // The error message should be overwritten after each parsing call.
286 EncodeNumberStatus rc = EncodeNumberStatus::kSuccess;
287 std::string err_msg;
288 NumberType type = {16, SPV_NUMBER_SIGNED_INT};
289
290 rc = ParseAndEncodeIntegerNumber(nullptr, type, AssertEmitFunc, &err_msg);
291 EXPECT_EQ(EncodeNumberStatus::kInvalidText, rc);
292 EXPECT_EQ("The given text is a nullptr", err_msg);
293 rc = ParseAndEncodeIntegerNumber("", type, AssertEmitFunc, &err_msg);
294 EXPECT_EQ(EncodeNumberStatus::kInvalidText, rc);
295 EXPECT_EQ("Invalid unsigned integer literal: ", err_msg);
296 rc = ParseAndEncodeIntegerNumber("=", type, AssertEmitFunc, &err_msg);
297 EXPECT_EQ(EncodeNumberStatus::kInvalidText, rc);
298 EXPECT_EQ("Invalid unsigned integer literal: =", err_msg);
299 rc = ParseAndEncodeIntegerNumber("-", type, AssertEmitFunc, &err_msg);
300 EXPECT_EQ(EncodeNumberStatus::kInvalidText, rc);
301 EXPECT_EQ("Invalid signed integer literal: -", err_msg);
302 rc = ParseAndEncodeIntegerNumber("0=", type, AssertEmitFunc, &err_msg);
303 EXPECT_EQ(EncodeNumberStatus::kInvalidText, rc);
304 EXPECT_EQ("Invalid unsigned integer literal: 0=", err_msg);
305 }
306
TEST(ParseAndEncodeNarrowSignedIntegers,Overflow)307 TEST(ParseAndEncodeNarrowSignedIntegers, Overflow) {
308 // The error message should be overwritten after each parsing call.
309 EncodeNumberStatus rc = EncodeNumberStatus::kSuccess;
310 std::string err_msg;
311 NumberType type = {16, SPV_NUMBER_SIGNED_INT};
312
313 rc = ParseAndEncodeIntegerNumber("32768", type, AssertEmitFunc, &err_msg);
314 EXPECT_EQ(EncodeNumberStatus::kInvalidText, rc);
315 EXPECT_EQ("Integer 32768 does not fit in a 16-bit signed integer", err_msg);
316 rc = ParseAndEncodeIntegerNumber("-32769", type, AssertEmitFunc, &err_msg);
317 EXPECT_EQ(EncodeNumberStatus::kInvalidText, rc);
318 EXPECT_EQ("Integer -32769 does not fit in a 16-bit signed integer", err_msg);
319 }
320
TEST(ParseAndEncodeNarrowSignedIntegers,Success)321 TEST(ParseAndEncodeNarrowSignedIntegers, Success) {
322 // Don't care the error message in this case.
323 EncodeNumberStatus rc = EncodeNumberStatus::kInvalidText;
324 NumberType type = {16, SPV_NUMBER_SIGNED_INT};
325
326 // Zero, maximum, and minimum value
327 rc = ParseAndEncodeIntegerNumber(
328 "0", type, [](uint32_t word) { EXPECT_EQ(0u, word); }, nullptr);
329 EXPECT_EQ(EncodeNumberStatus::kSuccess, rc);
330 rc = ParseAndEncodeIntegerNumber(
331 "-0", type, [](uint32_t word) { EXPECT_EQ(0u, word); }, nullptr);
332 EXPECT_EQ(EncodeNumberStatus::kSuccess, rc);
333 rc = ParseAndEncodeIntegerNumber(
334 "32767", type, [](uint32_t word) { EXPECT_EQ(0x00007fffu, word); },
335 nullptr);
336 EXPECT_EQ(EncodeNumberStatus::kSuccess, rc);
337 rc = ParseAndEncodeIntegerNumber(
338 "-32768", type, [](uint32_t word) { EXPECT_EQ(0xffff8000u, word); },
339 nullptr);
340 EXPECT_EQ(EncodeNumberStatus::kSuccess, rc);
341
342 // Hex parsing
343 rc = ParseAndEncodeIntegerNumber(
344 "0x7fff", type, [](uint32_t word) { EXPECT_EQ(0x00007fffu, word); },
345 nullptr);
346 EXPECT_EQ(EncodeNumberStatus::kSuccess, rc);
347 rc = ParseAndEncodeIntegerNumber(
348 "0xffff", type, [](uint32_t word) { EXPECT_EQ(0xffffffffu, word); },
349 nullptr);
350 EXPECT_EQ(EncodeNumberStatus::kSuccess, rc);
351 }
352
TEST(ParseAndEncodeNarrowUnsignedIntegers,Invalid)353 TEST(ParseAndEncodeNarrowUnsignedIntegers, Invalid) {
354 // The error message should be overwritten after each parsing call.
355 EncodeNumberStatus rc = EncodeNumberStatus::kSuccess;
356 std::string err_msg;
357 NumberType type = {16, SPV_NUMBER_UNSIGNED_INT};
358
359 rc = ParseAndEncodeIntegerNumber(nullptr, type, AssertEmitFunc, &err_msg);
360 EXPECT_EQ(EncodeNumberStatus::kInvalidText, rc);
361 EXPECT_EQ("The given text is a nullptr", err_msg);
362 rc = ParseAndEncodeIntegerNumber("", type, AssertEmitFunc, &err_msg);
363 EXPECT_EQ(EncodeNumberStatus::kInvalidText, rc);
364 EXPECT_EQ("Invalid unsigned integer literal: ", err_msg);
365 rc = ParseAndEncodeIntegerNumber("=", type, AssertEmitFunc, &err_msg);
366 EXPECT_EQ(EncodeNumberStatus::kInvalidText, rc);
367 EXPECT_EQ("Invalid unsigned integer literal: =", err_msg);
368 rc = ParseAndEncodeIntegerNumber("-", type, AssertEmitFunc, &err_msg);
369 EXPECT_EQ(EncodeNumberStatus::kInvalidUsage, rc);
370 EXPECT_EQ("Cannot put a negative number in an unsigned literal", err_msg);
371 rc = ParseAndEncodeIntegerNumber("0=", type, AssertEmitFunc, &err_msg);
372 EXPECT_EQ(EncodeNumberStatus::kInvalidText, rc);
373 EXPECT_EQ("Invalid unsigned integer literal: 0=", err_msg);
374 rc = ParseAndEncodeIntegerNumber("-0", type, AssertEmitFunc, &err_msg);
375 EXPECT_EQ(EncodeNumberStatus::kInvalidUsage, rc);
376 EXPECT_EQ("Cannot put a negative number in an unsigned literal", err_msg);
377 rc = ParseAndEncodeIntegerNumber("-1", type, AssertEmitFunc, &err_msg);
378 EXPECT_EQ(EncodeNumberStatus::kInvalidUsage, rc);
379 EXPECT_EQ("Cannot put a negative number in an unsigned literal", err_msg);
380 }
381
TEST(ParseAndEncodeNarrowUnsignedIntegers,Overflow)382 TEST(ParseAndEncodeNarrowUnsignedIntegers, Overflow) {
383 // The error message should be overwritten after each parsing call.
384 EncodeNumberStatus rc = EncodeNumberStatus::kSuccess;
385 std::string err_msg("random content");
386 NumberType type = {16, SPV_NUMBER_UNSIGNED_INT};
387
388 // Overflow
389 rc = ParseAndEncodeIntegerNumber("65536", type, AssertEmitFunc, &err_msg);
390 EXPECT_EQ(EncodeNumberStatus::kInvalidText, rc);
391 EXPECT_EQ("Integer 65536 does not fit in a 16-bit unsigned integer", err_msg);
392 }
393
TEST(ParseAndEncodeNarrowUnsignedIntegers,Success)394 TEST(ParseAndEncodeNarrowUnsignedIntegers, Success) {
395 // Don't care the error message in this case.
396 EncodeNumberStatus rc = EncodeNumberStatus::kInvalidText;
397 NumberType type = {16, SPV_NUMBER_UNSIGNED_INT};
398
399 // Zero, maximum, and minimum value
400 rc = ParseAndEncodeIntegerNumber(
401 "0", type, [](uint32_t word) { EXPECT_EQ(0u, word); }, nullptr);
402 EXPECT_EQ(EncodeNumberStatus::kSuccess, rc);
403 rc = ParseAndEncodeIntegerNumber(
404 "65535", type, [](uint32_t word) { EXPECT_EQ(0x0000ffffu, word); },
405 nullptr);
406 EXPECT_EQ(EncodeNumberStatus::kSuccess, rc);
407
408 // Hex parsing
409 rc = ParseAndEncodeIntegerNumber(
410 "0xffff", type, [](uint32_t word) { EXPECT_EQ(0x0000ffffu, word); },
411 nullptr);
412 EXPECT_EQ(EncodeNumberStatus::kSuccess, rc);
413 }
414
TEST(ParseAndEncodeSignedIntegers,Invalid)415 TEST(ParseAndEncodeSignedIntegers, Invalid) {
416 // The error message should be overwritten after each parsing call.
417 EncodeNumberStatus rc = EncodeNumberStatus::kSuccess;
418 std::string err_msg;
419 NumberType type = {32, SPV_NUMBER_SIGNED_INT};
420
421 rc = ParseAndEncodeIntegerNumber(nullptr, type, AssertEmitFunc, &err_msg);
422 EXPECT_EQ(EncodeNumberStatus::kInvalidText, rc);
423 EXPECT_EQ("The given text is a nullptr", err_msg);
424 rc = ParseAndEncodeIntegerNumber("", type, AssertEmitFunc, &err_msg);
425 EXPECT_EQ(EncodeNumberStatus::kInvalidText, rc);
426 EXPECT_EQ("Invalid unsigned integer literal: ", err_msg);
427 rc = ParseAndEncodeIntegerNumber("=", type, AssertEmitFunc, &err_msg);
428 EXPECT_EQ(EncodeNumberStatus::kInvalidText, rc);
429 EXPECT_EQ("Invalid unsigned integer literal: =", err_msg);
430 rc = ParseAndEncodeIntegerNumber("-", type, AssertEmitFunc, &err_msg);
431 EXPECT_EQ(EncodeNumberStatus::kInvalidText, rc);
432 EXPECT_EQ("Invalid signed integer literal: -", err_msg);
433 rc = ParseAndEncodeIntegerNumber("0=", type, AssertEmitFunc, &err_msg);
434 EXPECT_EQ(EncodeNumberStatus::kInvalidText, rc);
435 EXPECT_EQ("Invalid unsigned integer literal: 0=", err_msg);
436 }
437
TEST(ParseAndEncodeSignedIntegers,Overflow)438 TEST(ParseAndEncodeSignedIntegers, Overflow) {
439 // The error message should be overwritten after each parsing call.
440 EncodeNumberStatus rc = EncodeNumberStatus::kSuccess;
441 std::string err_msg;
442 NumberType type = {32, SPV_NUMBER_SIGNED_INT};
443
444 rc =
445 ParseAndEncodeIntegerNumber("2147483648", type, AssertEmitFunc, &err_msg);
446 EXPECT_EQ(EncodeNumberStatus::kInvalidText, rc);
447 EXPECT_EQ("Integer 2147483648 does not fit in a 32-bit signed integer",
448 err_msg);
449 rc = ParseAndEncodeIntegerNumber("-2147483649", type, AssertEmitFunc,
450 &err_msg);
451 EXPECT_EQ(EncodeNumberStatus::kInvalidText, rc);
452 EXPECT_EQ("Integer -2147483649 does not fit in a 32-bit signed integer",
453 err_msg);
454 }
455
TEST(ParseAndEncodeSignedIntegers,Success)456 TEST(ParseAndEncodeSignedIntegers, Success) {
457 // Don't care the error message in this case.
458 EncodeNumberStatus rc = EncodeNumberStatus::kInvalidText;
459 NumberType type = {32, SPV_NUMBER_SIGNED_INT};
460
461 // Zero, maximum, and minimum value
462 rc = ParseAndEncodeIntegerNumber(
463 "0", type, [](uint32_t word) { EXPECT_EQ(0u, word); }, nullptr);
464 EXPECT_EQ(EncodeNumberStatus::kSuccess, rc);
465 rc = ParseAndEncodeIntegerNumber(
466 "-0", type, [](uint32_t word) { EXPECT_EQ(0u, word); }, nullptr);
467 EXPECT_EQ(EncodeNumberStatus::kSuccess, rc);
468 rc = ParseAndEncodeIntegerNumber(
469 "2147483647", type, [](uint32_t word) { EXPECT_EQ(0x7fffffffu, word); },
470 nullptr);
471 EXPECT_EQ(EncodeNumberStatus::kSuccess, rc);
472 rc = ParseAndEncodeIntegerNumber(
473 "-2147483648", type, [](uint32_t word) { EXPECT_EQ(0x80000000u, word); },
474 nullptr);
475 EXPECT_EQ(EncodeNumberStatus::kSuccess, rc);
476
477 // Hex parsing
478 rc = ParseAndEncodeIntegerNumber(
479 "0x7fffffff", type, [](uint32_t word) { EXPECT_EQ(0x7fffffffu, word); },
480 nullptr);
481 EXPECT_EQ(EncodeNumberStatus::kSuccess, rc);
482 rc = ParseAndEncodeIntegerNumber(
483 "0xffffffff", type, [](uint32_t word) { EXPECT_EQ(0xffffffffu, word); },
484 nullptr);
485 EXPECT_EQ(EncodeNumberStatus::kSuccess, rc);
486 }
487
TEST(ParseAndEncodeUnsignedIntegers,Invalid)488 TEST(ParseAndEncodeUnsignedIntegers, Invalid) {
489 // The error message should be overwritten after each parsing call.
490 EncodeNumberStatus rc = EncodeNumberStatus::kSuccess;
491 std::string err_msg;
492 NumberType type = {32, SPV_NUMBER_UNSIGNED_INT};
493
494 rc = ParseAndEncodeIntegerNumber(nullptr, type, AssertEmitFunc, &err_msg);
495 EXPECT_EQ(EncodeNumberStatus::kInvalidText, rc);
496 EXPECT_EQ("The given text is a nullptr", err_msg);
497 rc = ParseAndEncodeIntegerNumber("", type, AssertEmitFunc, &err_msg);
498 EXPECT_EQ(EncodeNumberStatus::kInvalidText, rc);
499 EXPECT_EQ("Invalid unsigned integer literal: ", err_msg);
500 rc = ParseAndEncodeIntegerNumber("=", type, AssertEmitFunc, &err_msg);
501 EXPECT_EQ(EncodeNumberStatus::kInvalidText, rc);
502 EXPECT_EQ("Invalid unsigned integer literal: =", err_msg);
503 rc = ParseAndEncodeIntegerNumber("-", type, AssertEmitFunc, &err_msg);
504 EXPECT_EQ(EncodeNumberStatus::kInvalidUsage, rc);
505 EXPECT_EQ("Cannot put a negative number in an unsigned literal", err_msg);
506 rc = ParseAndEncodeIntegerNumber("0=", type, AssertEmitFunc, &err_msg);
507 EXPECT_EQ(EncodeNumberStatus::kInvalidText, rc);
508 EXPECT_EQ("Invalid unsigned integer literal: 0=", err_msg);
509 rc = ParseAndEncodeIntegerNumber("-0", type, AssertEmitFunc, &err_msg);
510 EXPECT_EQ(EncodeNumberStatus::kInvalidUsage, rc);
511 EXPECT_EQ("Cannot put a negative number in an unsigned literal", err_msg);
512 rc = ParseAndEncodeIntegerNumber("-1", type, AssertEmitFunc, &err_msg);
513 EXPECT_EQ(EncodeNumberStatus::kInvalidUsage, rc);
514 EXPECT_EQ("Cannot put a negative number in an unsigned literal", err_msg);
515 }
516
TEST(ParseAndEncodeUnsignedIntegers,Overflow)517 TEST(ParseAndEncodeUnsignedIntegers, Overflow) {
518 // The error message should be overwritten after each parsing call.
519 EncodeNumberStatus rc = EncodeNumberStatus::kSuccess;
520 std::string err_msg("random content");
521 NumberType type = {32, SPV_NUMBER_UNSIGNED_INT};
522
523 // Overflow
524 rc =
525 ParseAndEncodeIntegerNumber("4294967296", type, AssertEmitFunc, &err_msg);
526 EXPECT_EQ(EncodeNumberStatus::kInvalidText, rc);
527 EXPECT_EQ("Integer 4294967296 does not fit in a 32-bit unsigned integer",
528 err_msg);
529 }
530
TEST(ParseAndEncodeUnsignedIntegers,Success)531 TEST(ParseAndEncodeUnsignedIntegers, Success) {
532 // Don't care the error message in this case.
533 EncodeNumberStatus rc = EncodeNumberStatus::kInvalidText;
534 NumberType type = {32, SPV_NUMBER_UNSIGNED_INT};
535
536 // Zero, maximum, and minimum value
537 rc = ParseAndEncodeIntegerNumber(
538 "0", type, [](uint32_t word) { EXPECT_EQ(0u, word); }, nullptr);
539 EXPECT_EQ(EncodeNumberStatus::kSuccess, rc);
540 rc = ParseAndEncodeIntegerNumber(
541 "4294967295", type, [](uint32_t word) { EXPECT_EQ(0xffffffffu, word); },
542 nullptr);
543 EXPECT_EQ(EncodeNumberStatus::kSuccess, rc);
544
545 // Hex parsing
546 rc = ParseAndEncodeIntegerNumber(
547 "0xffffffff", type, [](uint32_t word) { EXPECT_EQ(0xffffffffu, word); },
548 nullptr);
549 EXPECT_EQ(EncodeNumberStatus::kSuccess, rc);
550 }
551
TEST(ParseAndEncodeWideSignedIntegers,Invalid)552 TEST(ParseAndEncodeWideSignedIntegers, Invalid) {
553 // The error message should be overwritten after each parsing call.
554 EncodeNumberStatus rc = EncodeNumberStatus::kSuccess;
555 std::string err_msg;
556 NumberType type = {64, SPV_NUMBER_SIGNED_INT};
557
558 rc = ParseAndEncodeIntegerNumber(nullptr, type, AssertEmitFunc, &err_msg);
559 EXPECT_EQ(EncodeNumberStatus::kInvalidText, rc);
560 EXPECT_EQ("The given text is a nullptr", err_msg);
561 rc = ParseAndEncodeIntegerNumber("", type, AssertEmitFunc, &err_msg);
562 EXPECT_EQ(EncodeNumberStatus::kInvalidText, rc);
563 EXPECT_EQ("Invalid unsigned integer literal: ", err_msg);
564 rc = ParseAndEncodeIntegerNumber("=", type, AssertEmitFunc, &err_msg);
565 EXPECT_EQ(EncodeNumberStatus::kInvalidText, rc);
566 EXPECT_EQ("Invalid unsigned integer literal: =", err_msg);
567 rc = ParseAndEncodeIntegerNumber("-", type, AssertEmitFunc, &err_msg);
568 EXPECT_EQ(EncodeNumberStatus::kInvalidText, rc);
569 EXPECT_EQ("Invalid signed integer literal: -", err_msg);
570 rc = ParseAndEncodeIntegerNumber("0=", type, AssertEmitFunc, &err_msg);
571 EXPECT_EQ(EncodeNumberStatus::kInvalidText, rc);
572 EXPECT_EQ("Invalid unsigned integer literal: 0=", err_msg);
573 }
574
TEST(ParseAndEncodeWideSignedIntegers,Overflow)575 TEST(ParseAndEncodeWideSignedIntegers, Overflow) {
576 // The error message should be overwritten after each parsing call.
577 EncodeNumberStatus rc = EncodeNumberStatus::kSuccess;
578 std::string err_msg;
579 NumberType type = {64, SPV_NUMBER_SIGNED_INT};
580
581 rc = ParseAndEncodeIntegerNumber("9223372036854775808", type, AssertEmitFunc,
582 &err_msg);
583 EXPECT_EQ(EncodeNumberStatus::kInvalidText, rc);
584 EXPECT_EQ(
585 "Integer 9223372036854775808 does not fit in a 64-bit signed integer",
586 err_msg);
587 rc = ParseAndEncodeIntegerNumber("-9223372036854775809", type, AssertEmitFunc,
588 &err_msg);
589 EXPECT_EQ(EncodeNumberStatus::kInvalidText, rc);
590 EXPECT_EQ("Invalid signed integer literal: -9223372036854775809", err_msg);
591 }
592
TEST(ParseAndEncodeWideSignedIntegers,Success)593 TEST(ParseAndEncodeWideSignedIntegers, Success) {
594 // Don't care the error message in this case.
595 EncodeNumberStatus rc = EncodeNumberStatus::kInvalidText;
596 NumberType type = {64, SPV_NUMBER_SIGNED_INT};
597 std::vector<uint32_t> word_buffer;
598 auto emit = [&word_buffer](uint32_t word) {
599 if (word_buffer.size() == 2) word_buffer.clear();
600 word_buffer.push_back(word);
601 };
602
603 // Zero, maximum, and minimum value
604 rc = ParseAndEncodeIntegerNumber("0", type, emit, nullptr);
605 EXPECT_EQ(EncodeNumberStatus::kSuccess, rc);
606 EXPECT_THAT(word_buffer, Eq(std::vector<uint32_t>{0u, 0u}));
607 rc = ParseAndEncodeIntegerNumber("-0", type, emit, nullptr);
608 EXPECT_EQ(EncodeNumberStatus::kSuccess, rc);
609 EXPECT_THAT(word_buffer, Eq(std::vector<uint32_t>{0u, 0u}));
610 rc = ParseAndEncodeIntegerNumber("9223372036854775807", type, emit, nullptr);
611 EXPECT_EQ(EncodeNumberStatus::kSuccess, rc);
612 EXPECT_THAT(word_buffer, Eq(std::vector<uint32_t>{0xffffffffu, 0x7fffffffu}));
613 rc = ParseAndEncodeIntegerNumber("-9223372036854775808", type, emit, nullptr);
614 EXPECT_EQ(EncodeNumberStatus::kSuccess, rc);
615 EXPECT_THAT(word_buffer, Eq(std::vector<uint32_t>{0u, 0x80000000u}));
616 rc = ParseAndEncodeIntegerNumber("-1", type, emit, nullptr);
617 EXPECT_EQ(EncodeNumberStatus::kSuccess, rc);
618 EXPECT_THAT(word_buffer, Eq(std::vector<uint32_t>{0xffffffffu, 0xffffffffu}));
619
620 // Hex parsing
621 rc = ParseAndEncodeIntegerNumber("0x7fffffffffffffff", type, emit, nullptr);
622 EXPECT_EQ(EncodeNumberStatus::kSuccess, rc);
623 EXPECT_THAT(word_buffer, Eq(std::vector<uint32_t>{0xffffffffu, 0x7fffffffu}));
624 rc = ParseAndEncodeIntegerNumber("0xffffffffffffffff", type, emit, nullptr);
625 EXPECT_EQ(EncodeNumberStatus::kSuccess, rc);
626 EXPECT_THAT(word_buffer, Eq(std::vector<uint32_t>{0xffffffffu, 0xffffffffu}));
627 }
628
TEST(ParseAndEncodeWideUnsignedIntegers,Invalid)629 TEST(ParseAndEncodeWideUnsignedIntegers, Invalid) {
630 // The error message should be overwritten after each parsing call.
631 EncodeNumberStatus rc = EncodeNumberStatus::kSuccess;
632 std::string err_msg;
633 NumberType type = {64, SPV_NUMBER_UNSIGNED_INT};
634
635 // Invalid
636 rc = ParseAndEncodeIntegerNumber(nullptr, type, AssertEmitFunc, &err_msg);
637 EXPECT_EQ(EncodeNumberStatus::kInvalidText, rc);
638 EXPECT_EQ("The given text is a nullptr", err_msg);
639 rc = ParseAndEncodeIntegerNumber("", type, AssertEmitFunc, &err_msg);
640 EXPECT_EQ(EncodeNumberStatus::kInvalidText, rc);
641 EXPECT_EQ("Invalid unsigned integer literal: ", err_msg);
642 rc = ParseAndEncodeIntegerNumber("=", type, AssertEmitFunc, &err_msg);
643 EXPECT_EQ(EncodeNumberStatus::kInvalidText, rc);
644 EXPECT_EQ("Invalid unsigned integer literal: =", err_msg);
645 rc = ParseAndEncodeIntegerNumber("-", type, AssertEmitFunc, &err_msg);
646 EXPECT_EQ(EncodeNumberStatus::kInvalidUsage, rc);
647 EXPECT_EQ("Cannot put a negative number in an unsigned literal", err_msg);
648 rc = ParseAndEncodeIntegerNumber("0=", type, AssertEmitFunc, &err_msg);
649 EXPECT_EQ(EncodeNumberStatus::kInvalidText, rc);
650 EXPECT_EQ("Invalid unsigned integer literal: 0=", err_msg);
651 rc = ParseAndEncodeIntegerNumber("-0", type, AssertEmitFunc, &err_msg);
652 EXPECT_EQ(EncodeNumberStatus::kInvalidUsage, rc);
653 EXPECT_EQ("Cannot put a negative number in an unsigned literal", err_msg);
654 rc = ParseAndEncodeIntegerNumber("-1", type, AssertEmitFunc, &err_msg);
655 EXPECT_EQ(EncodeNumberStatus::kInvalidUsage, rc);
656 EXPECT_EQ("Cannot put a negative number in an unsigned literal", err_msg);
657 }
658
TEST(ParseAndEncodeWideUnsignedIntegers,Overflow)659 TEST(ParseAndEncodeWideUnsignedIntegers, Overflow) {
660 // The error message should be overwritten after each parsing call.
661 EncodeNumberStatus rc = EncodeNumberStatus::kSuccess;
662 std::string err_msg;
663 NumberType type = {64, SPV_NUMBER_UNSIGNED_INT};
664
665 // Overflow
666 rc = ParseAndEncodeIntegerNumber("18446744073709551616", type, AssertEmitFunc,
667 &err_msg);
668 EXPECT_EQ(EncodeNumberStatus::kInvalidText, rc);
669 EXPECT_EQ("Invalid unsigned integer literal: 18446744073709551616", err_msg);
670 }
671
TEST(ParseAndEncodeWideUnsignedIntegers,Success)672 TEST(ParseAndEncodeWideUnsignedIntegers, Success) {
673 // Don't care the error message in this case.
674 EncodeNumberStatus rc = EncodeNumberStatus::kInvalidText;
675 NumberType type = {64, SPV_NUMBER_UNSIGNED_INT};
676 std::vector<uint32_t> word_buffer;
677 auto emit = [&word_buffer](uint32_t word) {
678 if (word_buffer.size() == 2) word_buffer.clear();
679 word_buffer.push_back(word);
680 };
681
682 // Zero, maximum, and minimum value
683 rc = ParseAndEncodeIntegerNumber("0", type, emit, nullptr);
684 EXPECT_EQ(EncodeNumberStatus::kSuccess, rc);
685 EXPECT_THAT(word_buffer, Eq(std::vector<uint32_t>{0u, 0u}));
686 rc = ParseAndEncodeIntegerNumber("18446744073709551615", type, emit, nullptr);
687 EXPECT_EQ(EncodeNumberStatus::kSuccess, rc);
688 EXPECT_THAT(word_buffer, Eq(std::vector<uint32_t>{0xffffffffu, 0xffffffffu}));
689
690 // Hex parsing
691 rc = ParseAndEncodeIntegerNumber("0xffffffffffffffff", type, emit, nullptr);
692 EXPECT_EQ(EncodeNumberStatus::kSuccess, rc);
693 EXPECT_THAT(word_buffer, Eq(std::vector<uint32_t>{0xffffffffu, 0xffffffffu}));
694 }
695
TEST(ParseAndEncodeIntegerNumber,TypeNone)696 TEST(ParseAndEncodeIntegerNumber, TypeNone) {
697 EncodeNumberStatus rc = EncodeNumberStatus::kSuccess;
698 std::string err_msg;
699 NumberType type = {32, SPV_NUMBER_NONE};
700
701 rc = ParseAndEncodeIntegerNumber(
702 "0.0", type, [](uint32_t word) { EXPECT_EQ(0x0u, word); }, &err_msg);
703 EXPECT_EQ(EncodeNumberStatus::kInvalidUsage, rc);
704 EXPECT_EQ("The expected type is not a integer type", err_msg);
705 }
706
TEST(ParseAndEncodeIntegerNumber,InvalidCaseWithoutErrorMessageString)707 TEST(ParseAndEncodeIntegerNumber, InvalidCaseWithoutErrorMessageString) {
708 EncodeNumberStatus rc = EncodeNumberStatus::kSuccess;
709 NumberType type = {32, SPV_NUMBER_SIGNED_INT};
710
711 rc = ParseAndEncodeIntegerNumber("invalid", type, AssertEmitFunc, nullptr);
712 EXPECT_EQ(EncodeNumberStatus::kInvalidText, rc);
713 }
714
TEST(ParseAndEncodeIntegerNumber,DoNotTouchErrorMessageStringOnSuccess)715 TEST(ParseAndEncodeIntegerNumber, DoNotTouchErrorMessageStringOnSuccess) {
716 EncodeNumberStatus rc = EncodeNumberStatus::kInvalidText;
717 std::string err_msg("random content");
718 NumberType type = {32, SPV_NUMBER_SIGNED_INT};
719
720 rc = ParseAndEncodeIntegerNumber(
721 "100", type, [](uint32_t word) { EXPECT_EQ(100u, word); }, &err_msg);
722 EXPECT_EQ(EncodeNumberStatus::kSuccess, rc);
723 EXPECT_EQ("random content", err_msg);
724 }
725
TEST(ParseAndEncodeFloat,Sample)726 TEST(ParseAndEncodeFloat, Sample) {
727 EncodeNumberStatus rc = EncodeNumberStatus::kSuccess;
728 std::string err_msg;
729 NumberType type = {32, SPV_NUMBER_FLOATING};
730
731 // Invalid
732 rc = ParseAndEncodeFloatingPointNumber("", type, AssertEmitFunc, &err_msg);
733 EXPECT_EQ(EncodeNumberStatus::kInvalidText, rc);
734 EXPECT_EQ("Invalid 32-bit float literal: ", err_msg);
735 rc = ParseAndEncodeFloatingPointNumber("0=", type, AssertEmitFunc, &err_msg);
736 EXPECT_EQ(EncodeNumberStatus::kInvalidText, rc);
737 EXPECT_EQ("Invalid 32-bit float literal: 0=", err_msg);
738
739 // Representative samples
740 rc = ParseAndEncodeFloatingPointNumber(
741 "0.0", type, [](uint32_t word) { EXPECT_EQ(0x0u, word); }, nullptr);
742 EXPECT_EQ(EncodeNumberStatus::kSuccess, rc);
743 rc = ParseAndEncodeFloatingPointNumber(
744 "-0.0", type, [](uint32_t word) { EXPECT_EQ(0x80000000u, word); },
745 nullptr);
746 EXPECT_EQ(EncodeNumberStatus::kSuccess, rc);
747 rc = ParseAndEncodeFloatingPointNumber(
748 "42", type, [](uint32_t word) { EXPECT_EQ(0x42280000u, word); }, nullptr);
749 EXPECT_EQ(EncodeNumberStatus::kSuccess, rc);
750 rc = ParseAndEncodeFloatingPointNumber(
751 "2.5", type, [](uint32_t word) { EXPECT_EQ(0x40200000u, word); },
752 nullptr);
753 EXPECT_EQ(EncodeNumberStatus::kSuccess, rc);
754 rc = ParseAndEncodeFloatingPointNumber(
755 "-32.5", type, [](uint32_t word) { EXPECT_EQ(0xc2020000u, word); },
756 nullptr);
757 EXPECT_EQ(EncodeNumberStatus::kSuccess, rc);
758 rc = ParseAndEncodeFloatingPointNumber(
759 "1e38", type, [](uint32_t word) { EXPECT_EQ(0x7e967699u, word); },
760 nullptr);
761 EXPECT_EQ(EncodeNumberStatus::kSuccess, rc);
762 rc = ParseAndEncodeFloatingPointNumber(
763 "-1e38", type, [](uint32_t word) { EXPECT_EQ(0xfe967699u, word); },
764 nullptr);
765 EXPECT_EQ(EncodeNumberStatus::kSuccess, rc);
766
767 // Overflow
768 rc =
769 ParseAndEncodeFloatingPointNumber("1e40", type, AssertEmitFunc, &err_msg);
770 EXPECT_EQ(EncodeNumberStatus::kInvalidText, rc);
771 EXPECT_EQ("Invalid 32-bit float literal: 1e40", err_msg);
772 rc = ParseAndEncodeFloatingPointNumber("-1e40", type, AssertEmitFunc,
773 &err_msg);
774 EXPECT_EQ(EncodeNumberStatus::kInvalidText, rc);
775 EXPECT_EQ("Invalid 32-bit float literal: -1e40", err_msg);
776 rc = ParseAndEncodeFloatingPointNumber("1e400", type, AssertEmitFunc,
777 &err_msg);
778 EXPECT_EQ(EncodeNumberStatus::kInvalidText, rc);
779 EXPECT_EQ("Invalid 32-bit float literal: 1e400", err_msg);
780 rc = ParseAndEncodeFloatingPointNumber("-1e400", type, AssertEmitFunc,
781 &err_msg);
782 EXPECT_EQ(EncodeNumberStatus::kInvalidText, rc);
783 EXPECT_EQ("Invalid 32-bit float literal: -1e400", err_msg);
784 }
785
TEST(ParseAndEncodeDouble,Sample)786 TEST(ParseAndEncodeDouble, Sample) {
787 EncodeNumberStatus rc = EncodeNumberStatus::kSuccess;
788 std::string err_msg;
789 NumberType type = {64, SPV_NUMBER_FLOATING};
790 std::vector<uint32_t> word_buffer;
791 auto emit = [&word_buffer](uint32_t word) {
792 if (word_buffer.size() == 2) word_buffer.clear();
793 word_buffer.push_back(word);
794 };
795
796 // Invalid
797 rc = ParseAndEncodeFloatingPointNumber("", type, AssertEmitFunc, &err_msg);
798 EXPECT_EQ(EncodeNumberStatus::kInvalidText, rc);
799 EXPECT_EQ("Invalid 64-bit float literal: ", err_msg);
800 rc = ParseAndEncodeFloatingPointNumber("0=", type, AssertEmitFunc, &err_msg);
801 EXPECT_EQ(EncodeNumberStatus::kInvalidText, rc);
802 EXPECT_EQ("Invalid 64-bit float literal: 0=", err_msg);
803
804 // Representative samples
805 rc = ParseAndEncodeFloatingPointNumber("0.0", type, emit, nullptr);
806 EXPECT_EQ(EncodeNumberStatus::kSuccess, rc);
807 EXPECT_THAT(word_buffer, Eq(std::vector<uint32_t>{0u, 0u}));
808 rc = ParseAndEncodeFloatingPointNumber("-0.0", type, emit, nullptr);
809 EXPECT_EQ(EncodeNumberStatus::kSuccess, rc);
810 EXPECT_THAT(word_buffer, Eq(std::vector<uint32_t>{0u, 0x80000000u}));
811 rc = ParseAndEncodeFloatingPointNumber("42", type, emit, nullptr);
812 EXPECT_EQ(EncodeNumberStatus::kSuccess, rc);
813 EXPECT_THAT(word_buffer, Eq(std::vector<uint32_t>{0u, 0x40450000u}));
814 rc = ParseAndEncodeFloatingPointNumber("2.5", type, emit, nullptr);
815 EXPECT_EQ(EncodeNumberStatus::kSuccess, rc);
816 EXPECT_THAT(word_buffer, Eq(std::vector<uint32_t>{0u, 0x40040000u}));
817 rc = ParseAndEncodeFloatingPointNumber("32.5", type, emit, nullptr);
818 EXPECT_EQ(EncodeNumberStatus::kSuccess, rc);
819 EXPECT_THAT(word_buffer, Eq(std::vector<uint32_t>{0u, 0x40404000u}));
820 rc = ParseAndEncodeFloatingPointNumber("1e38", type, emit, nullptr);
821 EXPECT_EQ(EncodeNumberStatus::kSuccess, rc);
822 EXPECT_THAT(word_buffer, Eq(std::vector<uint32_t>{0x2a16a1b1u, 0x47d2ced3u}));
823 rc = ParseAndEncodeFloatingPointNumber("-1e38", type, emit, nullptr);
824 EXPECT_EQ(EncodeNumberStatus::kSuccess, rc);
825 EXPECT_THAT(word_buffer, Eq(std::vector<uint32_t>{0x2a16a1b1u, 0xc7d2ced3u}));
826 rc = ParseAndEncodeFloatingPointNumber("1e40", type, emit, nullptr);
827 EXPECT_EQ(EncodeNumberStatus::kSuccess, rc);
828 EXPECT_THAT(word_buffer, Eq(std::vector<uint32_t>{0xf1c35ca5u, 0x483d6329u}));
829 rc = ParseAndEncodeFloatingPointNumber("-1e40", type, emit, nullptr);
830 EXPECT_EQ(EncodeNumberStatus::kSuccess, rc);
831 EXPECT_THAT(word_buffer, Eq(std::vector<uint32_t>{0xf1c35ca5u, 0xc83d6329u}));
832
833 // Overflow
834 rc = ParseAndEncodeFloatingPointNumber("1e400", type, AssertEmitFunc,
835 &err_msg);
836 EXPECT_EQ(EncodeNumberStatus::kInvalidText, rc);
837 EXPECT_EQ("Invalid 64-bit float literal: 1e400", err_msg);
838 rc = ParseAndEncodeFloatingPointNumber("-1e400", type, AssertEmitFunc,
839 &err_msg);
840 EXPECT_EQ(EncodeNumberStatus::kInvalidText, rc);
841 EXPECT_EQ("Invalid 64-bit float literal: -1e400", err_msg);
842 }
843
TEST(ParseAndEncodeFloat16,Sample)844 TEST(ParseAndEncodeFloat16, Sample) {
845 EncodeNumberStatus rc = EncodeNumberStatus::kSuccess;
846 std::string err_msg;
847 NumberType type = {16, SPV_NUMBER_FLOATING};
848
849 // Invalid
850 rc = ParseAndEncodeFloatingPointNumber("", type, AssertEmitFunc, &err_msg);
851 EXPECT_EQ(EncodeNumberStatus::kInvalidText, rc);
852 EXPECT_EQ("Invalid 16-bit float literal: ", err_msg);
853 rc = ParseAndEncodeFloatingPointNumber("0=", type, AssertEmitFunc, &err_msg);
854 EXPECT_EQ(EncodeNumberStatus::kInvalidText, rc);
855 EXPECT_EQ("Invalid 16-bit float literal: 0=", err_msg);
856
857 // Representative samples
858 rc = ParseAndEncodeFloatingPointNumber(
859 "0.0", type, [](uint32_t word) { EXPECT_EQ(0x0u, word); }, nullptr);
860 EXPECT_EQ(EncodeNumberStatus::kSuccess, rc);
861 rc = ParseAndEncodeFloatingPointNumber(
862 "-0.0", type, [](uint32_t word) { EXPECT_EQ(0x8000u, word); }, nullptr);
863 EXPECT_EQ(EncodeNumberStatus::kSuccess, rc);
864 rc = ParseAndEncodeFloatingPointNumber(
865 "1.0", type, [](uint32_t word) { EXPECT_EQ(0x3c00u, word); }, nullptr);
866 EXPECT_EQ(EncodeNumberStatus::kSuccess, rc);
867 rc = ParseAndEncodeFloatingPointNumber(
868 "2.5", type, [](uint32_t word) { EXPECT_EQ(0x4100u, word); }, nullptr);
869 EXPECT_EQ(EncodeNumberStatus::kSuccess, rc);
870 rc = ParseAndEncodeFloatingPointNumber(
871 "32.5", type, [](uint32_t word) { EXPECT_EQ(0x5010u, word); }, nullptr);
872 EXPECT_EQ(EncodeNumberStatus::kSuccess, rc);
873
874 // Overflow
875 rc =
876 ParseAndEncodeFloatingPointNumber("1e38", type, AssertEmitFunc, &err_msg);
877 EXPECT_EQ(EncodeNumberStatus::kInvalidText, rc);
878 EXPECT_EQ("Invalid 16-bit float literal: 1e38", err_msg);
879 rc = ParseAndEncodeFloatingPointNumber("-1e38", type, AssertEmitFunc,
880 &err_msg);
881 EXPECT_EQ(EncodeNumberStatus::kInvalidText, rc);
882 EXPECT_EQ("Invalid 16-bit float literal: -1e38", err_msg);
883 rc =
884 ParseAndEncodeFloatingPointNumber("1e40", type, AssertEmitFunc, &err_msg);
885 EXPECT_EQ(EncodeNumberStatus::kInvalidText, rc);
886 EXPECT_EQ("Invalid 16-bit float literal: 1e40", err_msg);
887 rc = ParseAndEncodeFloatingPointNumber("-1e40", type, AssertEmitFunc,
888 &err_msg);
889 EXPECT_EQ(EncodeNumberStatus::kInvalidText, rc);
890 EXPECT_EQ("Invalid 16-bit float literal: -1e40", err_msg);
891 rc = ParseAndEncodeFloatingPointNumber("1e400", type, AssertEmitFunc,
892 &err_msg);
893 EXPECT_EQ(EncodeNumberStatus::kInvalidText, rc);
894 EXPECT_EQ("Invalid 16-bit float literal: 1e400", err_msg);
895 rc = ParseAndEncodeFloatingPointNumber("-1e400", type, AssertEmitFunc,
896 &err_msg);
897 EXPECT_EQ(EncodeNumberStatus::kInvalidText, rc);
898 EXPECT_EQ("Invalid 16-bit float literal: -1e400", err_msg);
899 }
900
TEST(ParseAndEncodeFloatingPointNumber,TypeNone)901 TEST(ParseAndEncodeFloatingPointNumber, TypeNone) {
902 EncodeNumberStatus rc = EncodeNumberStatus::kSuccess;
903 std::string err_msg;
904 NumberType type = {32, SPV_NUMBER_NONE};
905
906 rc = ParseAndEncodeFloatingPointNumber(
907 "0.0", type, [](uint32_t word) { EXPECT_EQ(0x0u, word); }, &err_msg);
908 EXPECT_EQ(EncodeNumberStatus::kInvalidUsage, rc);
909 EXPECT_EQ("The expected type is not a float type", err_msg);
910 }
911
TEST(ParseAndEncodeFloatingPointNumber,InvalidCaseWithoutErrorMessageString)912 TEST(ParseAndEncodeFloatingPointNumber, InvalidCaseWithoutErrorMessageString) {
913 EncodeNumberStatus rc = EncodeNumberStatus::kSuccess;
914 NumberType type = {32, SPV_NUMBER_FLOATING};
915
916 rc = ParseAndEncodeFloatingPointNumber("invalid", type, AssertEmitFunc,
917 nullptr);
918 EXPECT_EQ(EncodeNumberStatus::kInvalidText, rc);
919 }
920
TEST(ParseAndEncodeFloatingPointNumber,DoNotTouchErrorMessageStringOnSuccess)921 TEST(ParseAndEncodeFloatingPointNumber, DoNotTouchErrorMessageStringOnSuccess) {
922 EncodeNumberStatus rc = EncodeNumberStatus::kInvalidText;
923 std::string err_msg("random content");
924 NumberType type = {32, SPV_NUMBER_FLOATING};
925
926 rc = ParseAndEncodeFloatingPointNumber(
927 "0.0", type, [](uint32_t word) { EXPECT_EQ(0x0u, word); }, &err_msg);
928 EXPECT_EQ(EncodeNumberStatus::kSuccess, rc);
929 EXPECT_EQ("random content", err_msg);
930 }
931
TEST(ParseAndEncodeNumber,Sample)932 TEST(ParseAndEncodeNumber, Sample) {
933 EncodeNumberStatus rc = EncodeNumberStatus::kSuccess;
934 std::string err_msg;
935 NumberType type = {32, SPV_NUMBER_SIGNED_INT};
936
937 // Invalid with error message string
938 rc = ParseAndEncodeNumber("something wrong", type, AssertEmitFunc, &err_msg);
939 EXPECT_EQ(EncodeNumberStatus::kInvalidText, rc);
940 EXPECT_EQ("Invalid unsigned integer literal: something wrong", err_msg);
941
942 // Invalid without error message string
943 rc = ParseAndEncodeNumber("something wrong", type, AssertEmitFunc, nullptr);
944 EXPECT_EQ(EncodeNumberStatus::kInvalidText, rc);
945
946 // Signed integer, should not touch the error message string.
947 err_msg = "random content";
948 rc = ParseAndEncodeNumber("-1", type,
949 [](uint32_t word) { EXPECT_EQ(0xffffffffu, word); },
950 &err_msg);
951 EXPECT_EQ(EncodeNumberStatus::kSuccess, rc);
952 EXPECT_EQ("random content", err_msg);
953
954 // Unsigned integer
955 type = {32, SPV_NUMBER_UNSIGNED_INT};
956 rc = ParseAndEncodeNumber(
957 "1", type, [](uint32_t word) { EXPECT_EQ(1u, word); }, nullptr);
958 EXPECT_EQ(EncodeNumberStatus::kSuccess, rc);
959
960 // Float
961 type = {32, SPV_NUMBER_FLOATING};
962 rc = ParseAndEncodeNumber("-1.0", type,
963 [](uint32_t word) { EXPECT_EQ(0xbf800000, word); },
964 nullptr);
965 EXPECT_EQ(EncodeNumberStatus::kSuccess, rc);
966 }
967
968 } // namespace
969 } // namespace utils
970 } // namespace spvtools
971