add a test for parser (WIP)

This commit is contained in:
ToruNiina 2018-12-11 21:51:39 +09:00
parent 27b9334f10
commit 0253f49101
5 changed files with 321 additions and 5 deletions

View File

@ -1,15 +1,19 @@
set(TEST_NAMES
test_datetime
test_utility
test_result
test_traits
test_value
test_lex_boolean
test_lex_integer
test_lex_floating
test_lex_datetime
test_lex_string
test_lex_key_comment
test_datetime
test_utility
test_result
test_traits
test_value
test_parse_boolean
test_parse_integer
test_parse_floating
# test_to_toml
# test_from_toml
# test_get

38
tests/test_parse_aux.hpp Normal file
View File

@ -0,0 +1,38 @@
#include <iostream>
#include <iomanip>
#include <algorithm>
#include <toml/region.hpp>
#include <toml/result.hpp>
// some of the parsers returns not only a value but also a region.
#define TOML11_TEST_PARSE_EQUAL(psr, tkn, expct) \
do { \
const std::string token(tkn); \
toml::detail::location<std::string> loc("test", token); \
const auto result = psr(loc); \
BOOST_CHECK(result.is_ok()); \
if(result.is_ok()){ \
BOOST_CHECK(result.unwrap().first == expct); \
} else { \
std::cerr << "parser " << #psr << " failed with input `"; \
std::cerr << token << "`.\n"; \
std::cerr << "reason: " << result.unwrap_err() << '\n'; \
} \
} while(false); \
/**/
#define TOML11_TEST_PARSE_EQUAL_VALUE(tkn, expct) \
do { \
const std::string token(tkn); \
toml::detail::location<std::string> loc("test", token); \
const auto result = toml::detail::parse_value(loc); \
BOOST_CHECK(result.is_ok()); \
if(result.is_ok()){ \
BOOST_CHECK(result.unwrap() == expct); \
} else { \
std::cerr << "parse_value failed with input `"; \
std::cerr << token << "`.\n"; \
std::cerr << "reason: " << result.unwrap_err() << '\n'; \
} \
} while(false); \
/**/

View File

@ -0,0 +1,24 @@
#define BOOST_TEST_MODULE "test_parse_boolean"
#ifdef UNITTEST_FRAMEWORK_LIBRARY_EXIST
#include <boost/test/unit_test.hpp>
#else
#define BOOST_TEST_NO_LIB
#include <boost/test/included/unit_test.hpp>
#endif
#include <toml/parser.hpp>
#include "test_parse_aux.hpp"
using namespace toml;
using namespace detail;
BOOST_AUTO_TEST_CASE(test_boolean)
{
TOML11_TEST_PARSE_EQUAL(parse_boolean, "true", true);
TOML11_TEST_PARSE_EQUAL(parse_boolean, "false", false);
}
BOOST_AUTO_TEST_CASE(test_boolean_value)
{
TOML11_TEST_PARSE_EQUAL_VALUE( "true", toml::value( true));
TOML11_TEST_PARSE_EQUAL_VALUE("false", toml::value(false));
}

View File

@ -0,0 +1,158 @@
#define BOOST_TEST_MODULE "parse_floating_test"
#ifdef UNITTEST_FRAMEWORK_LIBRARY_EXIST
#include <boost/test/unit_test.hpp>
#else
#define BOOST_TEST_NO_LIB
#include <boost/test/included/unit_test.hpp>
#endif
#include <toml/parser.hpp>
#include <cmath>
#include "test_parse_aux.hpp"
using namespace toml;
using namespace detail;
BOOST_AUTO_TEST_CASE(test_fractional)
{
TOML11_TEST_PARSE_EQUAL(parse_floating, "1.0", 1.0);
TOML11_TEST_PARSE_EQUAL(parse_floating, "0.1", 0.1);
TOML11_TEST_PARSE_EQUAL(parse_floating, "0.001", 0.001);
TOML11_TEST_PARSE_EQUAL(parse_floating, "0.100", 0.1);
TOML11_TEST_PARSE_EQUAL(parse_floating, "+3.14", 3.14);
TOML11_TEST_PARSE_EQUAL(parse_floating, "-3.14", -3.14);
TOML11_TEST_PARSE_EQUAL(parse_floating, "3.1415_9265_3589", 3.141592653589);
TOML11_TEST_PARSE_EQUAL(parse_floating, "+3.1415_9265_3589", 3.141592653589);
TOML11_TEST_PARSE_EQUAL(parse_floating, "-3.1415_9265_3589", -3.141592653589);
TOML11_TEST_PARSE_EQUAL(parse_floating, "123_456.789", 123456.789);
TOML11_TEST_PARSE_EQUAL(parse_floating, "+123_456.789", 123456.789);
TOML11_TEST_PARSE_EQUAL(parse_floating, "-123_456.789", -123456.789);
TOML11_TEST_PARSE_EQUAL(parse_floating, "+0.0", 0.0);
TOML11_TEST_PARSE_EQUAL(parse_floating, "-0.0", -0.0);
}
BOOST_AUTO_TEST_CASE(test_fractional_value)
{
TOML11_TEST_PARSE_EQUAL_VALUE("1.0", value( 1.0));
TOML11_TEST_PARSE_EQUAL_VALUE("0.1", value( 0.1));
TOML11_TEST_PARSE_EQUAL_VALUE("0.001", value( 0.001));
TOML11_TEST_PARSE_EQUAL_VALUE("0.100", value( 0.1));
TOML11_TEST_PARSE_EQUAL_VALUE("+3.14", value( 3.14));
TOML11_TEST_PARSE_EQUAL_VALUE("-3.14", value(-3.14));
TOML11_TEST_PARSE_EQUAL_VALUE("3.1415_9265_3589", value( 3.141592653589));
TOML11_TEST_PARSE_EQUAL_VALUE("+3.1415_9265_3589", value( 3.141592653589));
TOML11_TEST_PARSE_EQUAL_VALUE("-3.1415_9265_3589", value(-3.141592653589));
TOML11_TEST_PARSE_EQUAL_VALUE("123_456.789", value( 123456.789));
TOML11_TEST_PARSE_EQUAL_VALUE("+123_456.789", value( 123456.789));
TOML11_TEST_PARSE_EQUAL_VALUE("-123_456.789", value(-123456.789));
TOML11_TEST_PARSE_EQUAL_VALUE("+0.0", value( 0.0));
TOML11_TEST_PARSE_EQUAL_VALUE("-0.0", value(-0.0));
}
BOOST_AUTO_TEST_CASE(test_exponential)
{
TOML11_TEST_PARSE_EQUAL(parse_floating, "1e10", 1e10);
TOML11_TEST_PARSE_EQUAL(parse_floating, "1e+10", 1e10);
TOML11_TEST_PARSE_EQUAL(parse_floating, "1e-10", 1e-10);
TOML11_TEST_PARSE_EQUAL(parse_floating, "+1e10", 1e10);
TOML11_TEST_PARSE_EQUAL(parse_floating, "+1e+10", 1e10);
TOML11_TEST_PARSE_EQUAL(parse_floating, "+1e-10", 1e-10);
TOML11_TEST_PARSE_EQUAL(parse_floating, "-1e10", -1e10);
TOML11_TEST_PARSE_EQUAL(parse_floating, "-1e+10", -1e10);
TOML11_TEST_PARSE_EQUAL(parse_floating, "-1e-10", -1e-10);
TOML11_TEST_PARSE_EQUAL(parse_floating, "123e-10", 123e-10);
TOML11_TEST_PARSE_EQUAL(parse_floating, "1E10", 1e10);
TOML11_TEST_PARSE_EQUAL(parse_floating, "1E+10", 1e10);
TOML11_TEST_PARSE_EQUAL(parse_floating, "1E-10", 1e-10);
TOML11_TEST_PARSE_EQUAL(parse_floating, "123E-10", 123e-10);
TOML11_TEST_PARSE_EQUAL(parse_floating, "1_2_3E-10", 123e-10);
TOML11_TEST_PARSE_EQUAL(parse_floating, "1_2_3E-1_0", 123e-10);
TOML11_TEST_PARSE_EQUAL(parse_floating, "+0e0", 0.0);
TOML11_TEST_PARSE_EQUAL(parse_floating, "-0e0", -0.0);
}
BOOST_AUTO_TEST_CASE(test_exponential_value)
{
TOML11_TEST_PARSE_EQUAL_VALUE("1e10", value(1e10));
TOML11_TEST_PARSE_EQUAL_VALUE("1e+10", value(1e10));
TOML11_TEST_PARSE_EQUAL_VALUE("1e-10", value(1e-10));
TOML11_TEST_PARSE_EQUAL_VALUE("+1e10", value(1e10));
TOML11_TEST_PARSE_EQUAL_VALUE("+1e+10", value(1e10));
TOML11_TEST_PARSE_EQUAL_VALUE("+1e-10", value(1e-10));
TOML11_TEST_PARSE_EQUAL_VALUE("-1e10", value(-1e10));
TOML11_TEST_PARSE_EQUAL_VALUE("-1e+10", value(-1e10));
TOML11_TEST_PARSE_EQUAL_VALUE("-1e-10", value(-1e-10));
TOML11_TEST_PARSE_EQUAL_VALUE("123e-10", value(123e-10));
TOML11_TEST_PARSE_EQUAL_VALUE("1E10", value(1e10));
TOML11_TEST_PARSE_EQUAL_VALUE("1E+10", value(1e10));
TOML11_TEST_PARSE_EQUAL_VALUE("1E-10", value(1e-10));
TOML11_TEST_PARSE_EQUAL_VALUE("123E-10", value(123e-10));
TOML11_TEST_PARSE_EQUAL_VALUE("1_2_3E-10", value(123e-10));
TOML11_TEST_PARSE_EQUAL_VALUE("1_2_3E-1_0", value(123e-10));
TOML11_TEST_PARSE_EQUAL_VALUE("+0e0", value( 0.0));
TOML11_TEST_PARSE_EQUAL_VALUE("-0e0", value(-0.0));
}
BOOST_AUTO_TEST_CASE(test_fe)
{
TOML11_TEST_PARSE_EQUAL(parse_floating, "6.02e23", 6.02e23);
TOML11_TEST_PARSE_EQUAL(parse_floating, "6.02e+23", 6.02e23);
TOML11_TEST_PARSE_EQUAL(parse_floating, "1.112_650_06e-17", 1.11265006e-17);
}
BOOST_AUTO_TEST_CASE(test_fe_vaule)
{
TOML11_TEST_PARSE_EQUAL_VALUE("6.02e23", value(6.02e23));
TOML11_TEST_PARSE_EQUAL_VALUE("6.02e+23", value(6.02e23));
TOML11_TEST_PARSE_EQUAL_VALUE("1.112_650_06e-17", value(1.11265006e-17));
}
BOOST_AUTO_TEST_CASE(test_inf)
{
{
const std::string token("inf");
toml::detail::location<std::string> loc("test", token);
const auto r = parse_floating(loc);
BOOST_CHECK(r.is_ok());
BOOST_CHECK(std::isinf(r.unwrap().first));
BOOST_CHECK(r.unwrap().first > 0.0);
}
{
const std::string token("+inf");
toml::detail::location<std::string> loc("test", token);
const auto r = parse_floating(loc);
BOOST_CHECK(r.is_ok());
BOOST_CHECK(std::isinf(r.unwrap().first));
BOOST_CHECK(r.unwrap().first > 0.0);
}
{
const std::string token("-inf");
toml::detail::location<std::string> loc("test", token);
const auto r = parse_floating(loc);
BOOST_CHECK(r.is_ok());
BOOST_CHECK(std::isinf(r.unwrap().first));
BOOST_CHECK(r.unwrap().first < 0.0);
}
}
BOOST_AUTO_TEST_CASE(test_nan)
{
{
const std::string token("nan");
toml::detail::location<std::string> loc("test", token);
const auto r = parse_floating(loc);
BOOST_CHECK(r.is_ok());
BOOST_CHECK(std::isnan(r.unwrap().first));
}
{
const std::string token("+nan");
toml::detail::location<std::string> loc("test", token);
const auto r = parse_floating(loc);
BOOST_CHECK(r.is_ok());
BOOST_CHECK(std::isnan(r.unwrap().first));
}
{
const std::string token("-nan");
toml::detail::location<std::string> loc("test", token);
const auto r = parse_floating(loc);
BOOST_CHECK(r.is_ok());
BOOST_CHECK(std::isnan(r.unwrap().first));
}
}

View File

@ -0,0 +1,92 @@
#define BOOST_TEST_MODULE "parse_integer_test"
#ifdef UNITTEST_FRAMEWORK_LIBRARY_EXIST
#include <boost/test/unit_test.hpp>
#else
#define BOOST_TEST_NO_LIB
#include <boost/test/included/unit_test.hpp>
#endif
#include <toml/parser.hpp>
#include "test_parse_aux.hpp"
using namespace toml;
using namespace detail;
BOOST_AUTO_TEST_CASE(test_decimal)
{
TOML11_TEST_PARSE_EQUAL(parse_integer, "1234", 1234);
TOML11_TEST_PARSE_EQUAL(parse_integer, "+1234", 1234);
TOML11_TEST_PARSE_EQUAL(parse_integer, "-1234", -1234);
TOML11_TEST_PARSE_EQUAL(parse_integer, "0", 0);
TOML11_TEST_PARSE_EQUAL(parse_integer, "1_2_3_4", 1234);
TOML11_TEST_PARSE_EQUAL(parse_integer, "+1_2_3_4", +1234);
TOML11_TEST_PARSE_EQUAL(parse_integer, "-1_2_3_4", -1234);
TOML11_TEST_PARSE_EQUAL(parse_integer, "123_456_789", 123456789);
}
BOOST_AUTO_TEST_CASE(test_decimal_value)
{
TOML11_TEST_PARSE_EQUAL_VALUE( "1234", toml::value( 1234));
TOML11_TEST_PARSE_EQUAL_VALUE( "+1234", toml::value( 1234));
TOML11_TEST_PARSE_EQUAL_VALUE( "-1234", toml::value( -1234));
TOML11_TEST_PARSE_EQUAL_VALUE( "0", toml::value( 0));
TOML11_TEST_PARSE_EQUAL_VALUE( "1_2_3_4", toml::value( 1234));
TOML11_TEST_PARSE_EQUAL_VALUE( "+1_2_3_4", toml::value( +1234));
TOML11_TEST_PARSE_EQUAL_VALUE( "-1_2_3_4", toml::value( -1234));
TOML11_TEST_PARSE_EQUAL_VALUE("123_456_789", toml::value(123456789));
}
BOOST_AUTO_TEST_CASE(test_hex)
{
TOML11_TEST_PARSE_EQUAL(parse_integer, "0xDEADBEEF", 0xDEADBEEF);
TOML11_TEST_PARSE_EQUAL(parse_integer, "0xdeadbeef", 0xDEADBEEF);
TOML11_TEST_PARSE_EQUAL(parse_integer, "0xDEADbeef", 0xDEADBEEF);
TOML11_TEST_PARSE_EQUAL(parse_integer, "0xDEAD_BEEF", 0xDEADBEEF);
TOML11_TEST_PARSE_EQUAL(parse_integer, "0xdead_beef", 0xDEADBEEF);
TOML11_TEST_PARSE_EQUAL(parse_integer, "0xdead_BEEF", 0xDEADBEEF);
TOML11_TEST_PARSE_EQUAL(parse_integer, "0xFF", 0xFF);
TOML11_TEST_PARSE_EQUAL(parse_integer, "0x00FF", 0xFF);
TOML11_TEST_PARSE_EQUAL(parse_integer, "0x0000FF", 0xFF);
}
BOOST_AUTO_TEST_CASE(test_hex_value)
{
TOML11_TEST_PARSE_EQUAL_VALUE("0xDEADBEEF", value(0xDEADBEEF));
TOML11_TEST_PARSE_EQUAL_VALUE("0xdeadbeef", value(0xDEADBEEF));
TOML11_TEST_PARSE_EQUAL_VALUE("0xDEADbeef", value(0xDEADBEEF));
TOML11_TEST_PARSE_EQUAL_VALUE("0xDEAD_BEEF", value(0xDEADBEEF));
TOML11_TEST_PARSE_EQUAL_VALUE("0xdead_beef", value(0xDEADBEEF));
TOML11_TEST_PARSE_EQUAL_VALUE("0xdead_BEEF", value(0xDEADBEEF));
TOML11_TEST_PARSE_EQUAL_VALUE("0xFF", value(0xFF));
TOML11_TEST_PARSE_EQUAL_VALUE("0x00FF", value(0xFF));
TOML11_TEST_PARSE_EQUAL_VALUE("0x0000FF", value(0xFF));
}
BOOST_AUTO_TEST_CASE(test_oct)
{
TOML11_TEST_PARSE_EQUAL(parse_integer, "0o777", 64*7+8*7+7);
TOML11_TEST_PARSE_EQUAL(parse_integer, "0o7_7_7", 64*7+8*7+7);
TOML11_TEST_PARSE_EQUAL(parse_integer, "0o007", 7);
}
BOOST_AUTO_TEST_CASE(test_oct_value)
{
TOML11_TEST_PARSE_EQUAL_VALUE("0o777", value(64*7+8*7+7));
TOML11_TEST_PARSE_EQUAL_VALUE("0o7_7_7", value(64*7+8*7+7));
TOML11_TEST_PARSE_EQUAL_VALUE("0o007", value(7));
}
BOOST_AUTO_TEST_CASE(test_bin)
{
TOML11_TEST_PARSE_EQUAL(parse_integer, "0b10000", 16);
TOML11_TEST_PARSE_EQUAL(parse_integer, "0b010000", 16);
TOML11_TEST_PARSE_EQUAL(parse_integer, "0b01_00_00", 16);
TOML11_TEST_PARSE_EQUAL(parse_integer, "0b111111", 63);
}
BOOST_AUTO_TEST_CASE(test_bin_value)
{
TOML11_TEST_PARSE_EQUAL_VALUE("0b10000", value(16));
TOML11_TEST_PARSE_EQUAL_VALUE("0b010000", value(16));
TOML11_TEST_PARSE_EQUAL_VALUE("0b01_00_00", value(16));
TOML11_TEST_PARSE_EQUAL_VALUE("0b111111", value(63));
}