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