Enhance number parsing tests

This commit is contained in:
Jason Turner 2017-02-01 09:07:40 -08:00
parent 3e04210027
commit d8d7bc79b7
2 changed files with 23 additions and 11 deletions

View File

@ -757,7 +757,7 @@ namespace chaiscript
try {
auto u = std::stoull(val,nullptr,base);
if (u >= std::numeric_limits<unsigned long>::min() && u <= std::numeric_limits<unsigned long>::max()) {
if (!longlong_ && u >= std::numeric_limits<unsigned long>::min() && u <= std::numeric_limits<unsigned long>::max()) {
return const_var(static_cast<unsigned long>(u));
} else {
return const_var(static_cast<unsigned long long>(u));

View File

@ -4,13 +4,25 @@
#define TEST_LITERAL(v) test_literal(v, #v)
#define TEST_LITERAL_SIGNED(v) test_literal(v, #v, true)
template<typename T>
bool test_literal(T val, const std::string &str)
bool test_literal(T val, const std::string &str, bool use_boxed_number = false)
{
std::cout << '(' << str << ") Comparing : C++ '" << val;
chaiscript::ChaiScript_Basic chai(create_chaiscript_stdlib(),create_chaiscript_parser());
T val2 = chai.eval<T>(str);
// Note, after applying the `-` it's possible that chaiscript has internally converted
// between two equivalently sized types (ie, unsigned long and unsigned long long on a 64bit system)
// so we're going to allow some leeway with the signed tests
T val2 = [&](){
if (!use_boxed_number) {
return chai.eval<T>(str);
} else {
return chai.eval<chaiscript::Boxed_Number>(str).get_as<T>();
}
}();
std::cout << "' chai '" << val2 << "'\n";
return val == val2;
}
@ -222,20 +234,20 @@ int main()
&& TEST_LITERAL(16777215ull)
&& TEST_LITERAL(4294967295ull)
&& TEST_LITERAL(-255ull)
&& TEST_LITERAL(-65535ull)
&& TEST_LITERAL(-16777215ull)
&& TEST_LITERAL(-4294967295ull)
&& TEST_LITERAL_SIGNED(-255ull)
&& TEST_LITERAL_SIGNED(-65535ull)
&& TEST_LITERAL_SIGNED(-16777215ull)
&& TEST_LITERAL_SIGNED(-4294967295ull)
&& TEST_LITERAL(255ll)
&& TEST_LITERAL(65535ll)
&& TEST_LITERAL(16777215ll)
&& TEST_LITERAL(4294967295ll)
&& TEST_LITERAL(-255ll)
&& TEST_LITERAL(-65535ll)
&& TEST_LITERAL(-16777215ll)
&& TEST_LITERAL(-4294967295ll)
&& TEST_LITERAL_SIGNED(-255ll)
&& TEST_LITERAL_SIGNED(-65535ll)
&& TEST_LITERAL_SIGNED(-16777215ll)
&& TEST_LITERAL_SIGNED(-4294967295ll)
)