10d4c52358a1af421705c54bd8a9fdd8a30558a2eAlexander Gutkin// Copyright 2005 The RE2 Authors. All Rights Reserved. 20d4c52358a1af421705c54bd8a9fdd8a30558a2eAlexander Gutkin// Use of this source code is governed by a BSD-style 30d4c52358a1af421705c54bd8a9fdd8a30558a2eAlexander Gutkin// license that can be found in the LICENSE file. 40d4c52358a1af421705c54bd8a9fdd8a30558a2eAlexander Gutkin 50d4c52358a1af421705c54bd8a9fdd8a30558a2eAlexander Gutkin// This tests to make sure numbers are parsed from strings 60d4c52358a1af421705c54bd8a9fdd8a30558a2eAlexander Gutkin// correctly. 70d4c52358a1af421705c54bd8a9fdd8a30558a2eAlexander Gutkin// Todo: Expand the test to validate strings parsed to the other types 80d4c52358a1af421705c54bd8a9fdd8a30558a2eAlexander Gutkin// supported by RE2::Arg class 90d4c52358a1af421705c54bd8a9fdd8a30558a2eAlexander Gutkin 100d4c52358a1af421705c54bd8a9fdd8a30558a2eAlexander Gutkin#include "util/test.h" 110d4c52358a1af421705c54bd8a9fdd8a30558a2eAlexander Gutkin#include "re2/re2.h" 120d4c52358a1af421705c54bd8a9fdd8a30558a2eAlexander Gutkin 130d4c52358a1af421705c54bd8a9fdd8a30558a2eAlexander Gutkinnamespace re2 { 140d4c52358a1af421705c54bd8a9fdd8a30558a2eAlexander Gutkin 150d4c52358a1af421705c54bd8a9fdd8a30558a2eAlexander Gutkinstruct SuccessTable { 160d4c52358a1af421705c54bd8a9fdd8a30558a2eAlexander Gutkin const char * value_string; 170d4c52358a1af421705c54bd8a9fdd8a30558a2eAlexander Gutkin int64 value; 180d4c52358a1af421705c54bd8a9fdd8a30558a2eAlexander Gutkin bool success[6]; 190d4c52358a1af421705c54bd8a9fdd8a30558a2eAlexander Gutkin}; 200d4c52358a1af421705c54bd8a9fdd8a30558a2eAlexander Gutkin 210d4c52358a1af421705c54bd8a9fdd8a30558a2eAlexander Gutkin// Test boundary cases for different integral sizes. 220d4c52358a1af421705c54bd8a9fdd8a30558a2eAlexander Gutkin// Specifically I want to make sure that values outside the boundries 230d4c52358a1af421705c54bd8a9fdd8a30558a2eAlexander Gutkin// of an integral type will fail and that negative numbers will fail 240d4c52358a1af421705c54bd8a9fdd8a30558a2eAlexander Gutkin// for unsigned types. The following table contains the boundaries for 250d4c52358a1af421705c54bd8a9fdd8a30558a2eAlexander Gutkin// the various integral types and has entries for whether or not each 260d4c52358a1af421705c54bd8a9fdd8a30558a2eAlexander Gutkin// type can contain the given value. 270d4c52358a1af421705c54bd8a9fdd8a30558a2eAlexander Gutkinconst SuccessTable kSuccessTable[] = { 280d4c52358a1af421705c54bd8a9fdd8a30558a2eAlexander Gutkin// string integer value short ushort int uint int64 uint64 290d4c52358a1af421705c54bd8a9fdd8a30558a2eAlexander Gutkin// 0 to 2^7-1 300d4c52358a1af421705c54bd8a9fdd8a30558a2eAlexander Gutkin{ "0", 0, { true, true, true, true, true, true }}, 310d4c52358a1af421705c54bd8a9fdd8a30558a2eAlexander Gutkin{ "127", 127, { true, true, true, true, true, true }}, 320d4c52358a1af421705c54bd8a9fdd8a30558a2eAlexander Gutkin 330d4c52358a1af421705c54bd8a9fdd8a30558a2eAlexander Gutkin// -1 to -2^7 340d4c52358a1af421705c54bd8a9fdd8a30558a2eAlexander Gutkin{ "-1", -1, { true, false, true, false, true, false }}, 350d4c52358a1af421705c54bd8a9fdd8a30558a2eAlexander Gutkin{ "-128", -128, { true, false, true, false, true, false }}, 360d4c52358a1af421705c54bd8a9fdd8a30558a2eAlexander Gutkin 370d4c52358a1af421705c54bd8a9fdd8a30558a2eAlexander Gutkin// 2^7 to 2^8-1 380d4c52358a1af421705c54bd8a9fdd8a30558a2eAlexander Gutkin{ "128", 128, { true, true, true, true, true, true }}, 390d4c52358a1af421705c54bd8a9fdd8a30558a2eAlexander Gutkin{ "255", 255, { true, true, true, true, true, true }}, 400d4c52358a1af421705c54bd8a9fdd8a30558a2eAlexander Gutkin 410d4c52358a1af421705c54bd8a9fdd8a30558a2eAlexander Gutkin// 2^8 to 2^15-1 420d4c52358a1af421705c54bd8a9fdd8a30558a2eAlexander Gutkin{ "256", 256, { true, true, true, true, true, true }}, 430d4c52358a1af421705c54bd8a9fdd8a30558a2eAlexander Gutkin{ "32767", 32767, { true, true, true, true, true, true }}, 440d4c52358a1af421705c54bd8a9fdd8a30558a2eAlexander Gutkin 450d4c52358a1af421705c54bd8a9fdd8a30558a2eAlexander Gutkin// -2^7-1 to -2^15 460d4c52358a1af421705c54bd8a9fdd8a30558a2eAlexander Gutkin{ "-129", -129, { true, false, true, false, true, false }}, 470d4c52358a1af421705c54bd8a9fdd8a30558a2eAlexander Gutkin{ "-32768", -32768, { true, false, true, false, true, false }}, 480d4c52358a1af421705c54bd8a9fdd8a30558a2eAlexander Gutkin 490d4c52358a1af421705c54bd8a9fdd8a30558a2eAlexander Gutkin// 2^15 to 2^16-1 500d4c52358a1af421705c54bd8a9fdd8a30558a2eAlexander Gutkin{ "32768", 32768, { false, true, true, true, true, true }}, 510d4c52358a1af421705c54bd8a9fdd8a30558a2eAlexander Gutkin{ "65535", 65535, { false, true, true, true, true, true }}, 520d4c52358a1af421705c54bd8a9fdd8a30558a2eAlexander Gutkin 530d4c52358a1af421705c54bd8a9fdd8a30558a2eAlexander Gutkin// 2^16 to 2^31-1 540d4c52358a1af421705c54bd8a9fdd8a30558a2eAlexander Gutkin{ "65536", 65536, { false, false, true, true, true, true }}, 550d4c52358a1af421705c54bd8a9fdd8a30558a2eAlexander Gutkin{ "2147483647", 2147483647, { false, false, true, true, true, true }}, 560d4c52358a1af421705c54bd8a9fdd8a30558a2eAlexander Gutkin 570d4c52358a1af421705c54bd8a9fdd8a30558a2eAlexander Gutkin// -2^15-1 to -2^31 580d4c52358a1af421705c54bd8a9fdd8a30558a2eAlexander Gutkin{ "-32769", -32769, { false, false, true, false, true, false }}, 590d4c52358a1af421705c54bd8a9fdd8a30558a2eAlexander Gutkin{ "-2147483648", 600d4c52358a1af421705c54bd8a9fdd8a30558a2eAlexander Gutkin static_cast<int64>(0xFFFFFFFF80000000LL), 610d4c52358a1af421705c54bd8a9fdd8a30558a2eAlexander Gutkin{ false, false, true, false, true, false }}, 620d4c52358a1af421705c54bd8a9fdd8a30558a2eAlexander Gutkin 630d4c52358a1af421705c54bd8a9fdd8a30558a2eAlexander Gutkin// 2^31 to 2^32-1 640d4c52358a1af421705c54bd8a9fdd8a30558a2eAlexander Gutkin{ "2147483648", 2147483648U, { false, false, false, true, true, true }}, 650d4c52358a1af421705c54bd8a9fdd8a30558a2eAlexander Gutkin{ "4294967295", 4294967295U, { false, false, false, true, true, true }}, 660d4c52358a1af421705c54bd8a9fdd8a30558a2eAlexander Gutkin 670d4c52358a1af421705c54bd8a9fdd8a30558a2eAlexander Gutkin// 2^32 to 2^63-1 680d4c52358a1af421705c54bd8a9fdd8a30558a2eAlexander Gutkin{ "4294967296", 4294967296LL, { false, false, false, false, true, true }}, 690d4c52358a1af421705c54bd8a9fdd8a30558a2eAlexander Gutkin{ "9223372036854775807", 700d4c52358a1af421705c54bd8a9fdd8a30558a2eAlexander Gutkin 9223372036854775807LL, { false, false, false, false, true, true }}, 710d4c52358a1af421705c54bd8a9fdd8a30558a2eAlexander Gutkin 720d4c52358a1af421705c54bd8a9fdd8a30558a2eAlexander Gutkin// -2^31-1 to -2^63 730d4c52358a1af421705c54bd8a9fdd8a30558a2eAlexander Gutkin{ "-2147483649", -2147483649LL, { false, false, false, false, true, false }}, 740d4c52358a1af421705c54bd8a9fdd8a30558a2eAlexander Gutkin{ "-9223372036854775808", static_cast<int64>(0x8000000000000000LL), 750d4c52358a1af421705c54bd8a9fdd8a30558a2eAlexander Gutkin { false, false, false, false, true, false }}, 760d4c52358a1af421705c54bd8a9fdd8a30558a2eAlexander Gutkin 770d4c52358a1af421705c54bd8a9fdd8a30558a2eAlexander Gutkin// 2^63 to 2^64-1 780d4c52358a1af421705c54bd8a9fdd8a30558a2eAlexander Gutkin{ "9223372036854775808", static_cast<int64>(9223372036854775808ULL), 790d4c52358a1af421705c54bd8a9fdd8a30558a2eAlexander Gutkin { false, false, false, false, false, true }}, 800d4c52358a1af421705c54bd8a9fdd8a30558a2eAlexander Gutkin{ "18446744073709551615", static_cast<int64>(18446744073709551615ULL), 810d4c52358a1af421705c54bd8a9fdd8a30558a2eAlexander Gutkin { false, false, false, false, false, true }}, 820d4c52358a1af421705c54bd8a9fdd8a30558a2eAlexander Gutkin 830d4c52358a1af421705c54bd8a9fdd8a30558a2eAlexander Gutkin// >= 2^64 840d4c52358a1af421705c54bd8a9fdd8a30558a2eAlexander Gutkin{ "18446744073709551616", 0, { false, false, false, false, false, false }}, 850d4c52358a1af421705c54bd8a9fdd8a30558a2eAlexander Gutkin}; 860d4c52358a1af421705c54bd8a9fdd8a30558a2eAlexander Gutkin 870d4c52358a1af421705c54bd8a9fdd8a30558a2eAlexander Gutkinconst int kNumStrings = ARRAYSIZE(kSuccessTable); 880d4c52358a1af421705c54bd8a9fdd8a30558a2eAlexander Gutkin 890d4c52358a1af421705c54bd8a9fdd8a30558a2eAlexander Gutkin// It's ugly to use a macro, but we apparently can't use the ASSERT_TRUE_M 900d4c52358a1af421705c54bd8a9fdd8a30558a2eAlexander Gutkin// macro outside of a TEST block and this seems to be the only way to 910d4c52358a1af421705c54bd8a9fdd8a30558a2eAlexander Gutkin// avoid code duplication. I can also pull off a couple nice tricks 920d4c52358a1af421705c54bd8a9fdd8a30558a2eAlexander Gutkin// using concatenation for the type I'm checking against. 930d4c52358a1af421705c54bd8a9fdd8a30558a2eAlexander Gutkin#define PARSE_FOR_TYPE(type, column) { \ 940d4c52358a1af421705c54bd8a9fdd8a30558a2eAlexander Gutkin type r; \ 950d4c52358a1af421705c54bd8a9fdd8a30558a2eAlexander Gutkin for ( int i = 0; i < kNumStrings; ++i ) { \ 960d4c52358a1af421705c54bd8a9fdd8a30558a2eAlexander Gutkin RE2::Arg arg(&r); \ 970d4c52358a1af421705c54bd8a9fdd8a30558a2eAlexander Gutkin const char* const p = kSuccessTable[i].value_string; \ 980d4c52358a1af421705c54bd8a9fdd8a30558a2eAlexander Gutkin bool retval = arg.Parse(p, strlen(p)); \ 990d4c52358a1af421705c54bd8a9fdd8a30558a2eAlexander Gutkin bool success = kSuccessTable[i].success[column]; \ 1000d4c52358a1af421705c54bd8a9fdd8a30558a2eAlexander Gutkin ASSERT_TRUE_M(retval == success, \ 1010d4c52358a1af421705c54bd8a9fdd8a30558a2eAlexander Gutkin StringPrintf("Parsing '%s' for type " #type " should return %d", \ 1020d4c52358a1af421705c54bd8a9fdd8a30558a2eAlexander Gutkin p, success).c_str()); \ 1030d4c52358a1af421705c54bd8a9fdd8a30558a2eAlexander Gutkin if ( success ) { \ 1040d4c52358a1af421705c54bd8a9fdd8a30558a2eAlexander Gutkin ASSERT_EQUALS(r, kSuccessTable[i].value); \ 1050d4c52358a1af421705c54bd8a9fdd8a30558a2eAlexander Gutkin } \ 1060d4c52358a1af421705c54bd8a9fdd8a30558a2eAlexander Gutkin } \ 1070d4c52358a1af421705c54bd8a9fdd8a30558a2eAlexander Gutkin} 1080d4c52358a1af421705c54bd8a9fdd8a30558a2eAlexander Gutkin 1090d4c52358a1af421705c54bd8a9fdd8a30558a2eAlexander GutkinTEST(REArgTest, Int16Test) { 1100d4c52358a1af421705c54bd8a9fdd8a30558a2eAlexander Gutkin PARSE_FOR_TYPE(int16, 0); 1110d4c52358a1af421705c54bd8a9fdd8a30558a2eAlexander Gutkin} 1120d4c52358a1af421705c54bd8a9fdd8a30558a2eAlexander Gutkin 1130d4c52358a1af421705c54bd8a9fdd8a30558a2eAlexander GutkinTEST(REArgTest, Uint16Test) { 1140d4c52358a1af421705c54bd8a9fdd8a30558a2eAlexander Gutkin PARSE_FOR_TYPE(uint16, 1); 1150d4c52358a1af421705c54bd8a9fdd8a30558a2eAlexander Gutkin} 1160d4c52358a1af421705c54bd8a9fdd8a30558a2eAlexander Gutkin 1170d4c52358a1af421705c54bd8a9fdd8a30558a2eAlexander GutkinTEST(REArgTest, IntTest) { 1180d4c52358a1af421705c54bd8a9fdd8a30558a2eAlexander Gutkin PARSE_FOR_TYPE(int, 2); 1190d4c52358a1af421705c54bd8a9fdd8a30558a2eAlexander Gutkin} 1200d4c52358a1af421705c54bd8a9fdd8a30558a2eAlexander Gutkin 1210d4c52358a1af421705c54bd8a9fdd8a30558a2eAlexander GutkinTEST(REArgTest, UInt32Test) { 1220d4c52358a1af421705c54bd8a9fdd8a30558a2eAlexander Gutkin PARSE_FOR_TYPE(uint32, 3); 1230d4c52358a1af421705c54bd8a9fdd8a30558a2eAlexander Gutkin} 1240d4c52358a1af421705c54bd8a9fdd8a30558a2eAlexander Gutkin 1250d4c52358a1af421705c54bd8a9fdd8a30558a2eAlexander GutkinTEST(REArgTest, Iint64Test) { 1260d4c52358a1af421705c54bd8a9fdd8a30558a2eAlexander Gutkin PARSE_FOR_TYPE(int64, 4); 1270d4c52358a1af421705c54bd8a9fdd8a30558a2eAlexander Gutkin} 1280d4c52358a1af421705c54bd8a9fdd8a30558a2eAlexander Gutkin 1290d4c52358a1af421705c54bd8a9fdd8a30558a2eAlexander GutkinTEST(REArgTest, Uint64Test) { 1300d4c52358a1af421705c54bd8a9fdd8a30558a2eAlexander Gutkin PARSE_FOR_TYPE(uint64, 5); 1310d4c52358a1af421705c54bd8a9fdd8a30558a2eAlexander Gutkin} 1320d4c52358a1af421705c54bd8a9fdd8a30558a2eAlexander Gutkin 1330d4c52358a1af421705c54bd8a9fdd8a30558a2eAlexander Gutkin} // namespace re2 134