add test for values

This commit is contained in:
ToruNiina
2017-04-20 00:08:41 +09:00
parent 34b0176083
commit 0b13f01668
2 changed files with 125 additions and 69 deletions

View File

@@ -1,6 +1,6 @@
set(TEST_NAMES
test_traits
# test_value
test_value
)
add_definitions("-Wall -Wpedantic")

View File

@@ -1,76 +1,132 @@
#include "toml.hpp"
#define BOOST_TEST_MODULE "test_value"
#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/toml.hpp>
#include <map>
#include <list>
int main()
BOOST_AUTO_TEST_CASE(test_value_exact_constructor)
{
bool b = true;
int i=42;
double f = 3.14;
toml::value v1(i);
toml::value v2(b);
toml::Boolean b(true);
toml::Integer i(42);
toml::Float f(3.14);
toml::String s("hoge");
toml::Datetime d(std::chrono::system_clock::now());
toml::Array a;
a.emplace_back(2);
a.emplace_back(7);
a.emplace_back(1);
a.emplace_back(8);
a.emplace_back(2);
toml::Table t;
t.emplace("val1", true);
t.emplace("val2", 42);
t.emplace("val3", 3.14);
t.emplace("val4", "piyo");
toml::value v1(b);
toml::value v2(i);
toml::value v3(f);
toml::value v4("hoge");
toml::value v4(s);
toml::value v5(d);
toml::value v6(a);
toml::value v7(t);
std::cout << "v1: " << v1.type() << " = " << v1.cast<toml::value_t::Integer>() << std::endl;
std::cout << "v2: " << v2.type() << " = " << v2.cast<toml::value_t::Boolean>() << std::endl;
std::cout << "v3: " << v3.type() << " = " << v3.cast<toml::value_t::Float>() << std::endl;
std::cout << "v4: " << v4.type() << " = " << v4.cast<toml::value_t::String>() << std::endl;
v1.cast<toml::value_t::Integer>() += 1;
std::cout << "v1: " << v1.type() << " = " << v1.cast<toml::value_t::Integer>() << std::endl;
v4.cast<toml::value_t::String>() += " piyo";
std::cout << "v4: " << v4.type() << " = " << v4.cast<toml::value_t::String>() << std::endl;
BOOST_CHECK_EQUAL(v1.type(), toml::value_t::Boolean);
BOOST_CHECK_EQUAL(v2.type(), toml::value_t::Integer);
BOOST_CHECK_EQUAL(v3.type(), toml::value_t::Float);
BOOST_CHECK_EQUAL(v4.type(), toml::value_t::String);
BOOST_CHECK_EQUAL(v5.type(), toml::value_t::Datetime);
BOOST_CHECK_EQUAL(v6.type(), toml::value_t::Array);
BOOST_CHECK_EQUAL(v7.type(), toml::value_t::Table);
v1 = f;
std::cout << "v1: " << v1.type() << " = " << v1.cast<toml::value_t::Float>() << std::endl;
toml::Table tab;
tab["v1"] = v1;
tab["v2"] = v2;
tab["v3"] = v3;
tab["v4"] = v4;
toml::Array arr;
arr.emplace_back(3);
arr.emplace_back(1);
arr.emplace_back(4);
arr.emplace_back(1);
arr.emplace_back(5);
toml::value v5(tab);
std::cout << "v5: " << v5.type() << " = {" << std::endl;
const auto& v5t = v5.cast<toml::value_t::Table>();
std::cout << "v5t[v1] = " << v5t.at("v1").cast<toml::value_t::Float>() << std::endl;
std::cout << "v5t[v2] = " << v5t.at("v2").cast<toml::value_t::Boolean>() << std::endl;
std::cout << "v5t[v3] = " << v5t.at("v3").cast<toml::value_t::Float>() << std::endl;
std::cout << "v5t[v4] = " << v5t.at("v4").cast<toml::value_t::String>() << std::endl;
std::cout << "}" << std::endl;
toml::value v6(arr);
std::cout << "v6: " << v6.type() << " = ";
for(auto&& item : v6.cast<toml::value_t::Array>())
{
std::cout << item.cast<toml::value_t::Integer>() << ", ";
}
std::cout << std::endl;
std::string piyo("piyo");
std::swap(v4.cast<toml::value_t::String>(), piyo);
std::cout << "v4: " << v4.type() << " = " << v4.cast<toml::value_t::String>() << std::endl;
toml::value v7(v4);
std::cout << "v7: " << v7.type() << " = " << v7.cast<toml::value_t::String>() << std::endl;
toml::value v8(std::move(v4));
std::cout << "v8: " << v8.type() << " = " << v8.cast<toml::value_t::String>() << std::endl;
std::cout << toml::is_castable<int, toml::value_t::Integer>::value << std::endl;
std::cout << toml::is_castable<std::vector<int>, toml::value_t::Array>::value << std::endl;
std::cout << toml::is_castable<int[5], toml::value_t::Array>::value << std::endl;
std::cout << toml::is_castable<std::map<std::string, int>, toml::value_t::Table>::value << std::endl;
std::cout << toml::is_castable<int, toml::value_t::Table>::value << std::endl;
std::cout << toml::is_castable<std::map<int, std::string>, toml::value_t::Table>::value << std::endl;
std::cout << toml::is_castable<std::list<int>, toml::value_t::Array>::value << std::endl;
return 0;
BOOST_CHECK_EQUAL(v1.cast<toml::value_t::Boolean >(), b);
BOOST_CHECK_EQUAL(v2.cast<toml::value_t::Integer >(), i);
BOOST_CHECK_EQUAL(v3.cast<toml::value_t::Float >(), f);
BOOST_CHECK_EQUAL(v4.cast<toml::value_t::String >(), s);
const auto& ar = v6.cast<toml::value_t::Array>();
BOOST_CHECK_EQUAL(ar.at(0).cast<toml::value_t::Integer>(), a.at(0).cast<toml::value_t::Integer>());
BOOST_CHECK_EQUAL(ar.at(1).cast<toml::value_t::Integer>(), a.at(1).cast<toml::value_t::Integer>());
BOOST_CHECK_EQUAL(ar.at(2).cast<toml::value_t::Integer>(), a.at(2).cast<toml::value_t::Integer>());
BOOST_CHECK_EQUAL(ar.at(3).cast<toml::value_t::Integer>(), a.at(3).cast<toml::value_t::Integer>());
BOOST_CHECK_EQUAL(ar.at(4).cast<toml::value_t::Integer>(), a.at(4).cast<toml::value_t::Integer>());
}
BOOST_AUTO_TEST_CASE(test_value_convertible_constructor)
{
int i(42);
float f(3.14);
const char* s = "hoge";
toml::value v1(i);
toml::value v2(f);
toml::value v3(s);
BOOST_CHECK_EQUAL(v1.type(), toml::value_t::Integer);
BOOST_CHECK_EQUAL(v2.type(), toml::value_t::Float);
BOOST_CHECK_EQUAL(v3.type(), toml::value_t::String);
BOOST_CHECK_EQUAL(v1.cast<toml::value_t::Integer >(), i);
BOOST_CHECK_EQUAL(v2.cast<toml::value_t::Float >(), f);
BOOST_CHECK_EQUAL(v3.cast<toml::value_t::String >(), s);
}
BOOST_AUTO_TEST_CASE(test_value_copy_move_constructor)
{
toml::Array a;
toml::Table t;
toml::value v1(true);
toml::value v2(42);
toml::value v3(3.14);
toml::value v4("hoge");
toml::value v5(std::chrono::system_clock::now());
toml::value v6(a);
toml::value v7(t);
toml::value u1(v1);
toml::value u2(v2);
toml::value u3(v3);
toml::value u4(v4);
toml::value u5(v5);
toml::value u6(v6);
toml::value u7(v7);
BOOST_CHECK_EQUAL(u1.type(), toml::value_t::Boolean);
BOOST_CHECK_EQUAL(u2.type(), toml::value_t::Integer);
BOOST_CHECK_EQUAL(u3.type(), toml::value_t::Float);
BOOST_CHECK_EQUAL(u4.type(), toml::value_t::String);
BOOST_CHECK_EQUAL(u5.type(), toml::value_t::Datetime);
BOOST_CHECK_EQUAL(u6.type(), toml::value_t::Array);
BOOST_CHECK_EQUAL(u7.type(), toml::value_t::Table);
BOOST_CHECK_EQUAL(u1.cast<toml::value_t::Boolean >(), true);
BOOST_CHECK_EQUAL(u2.cast<toml::value_t::Integer >(), 42);
BOOST_CHECK_EQUAL(u3.cast<toml::value_t::Float >(), 3.14);
BOOST_CHECK_EQUAL(u4.cast<toml::value_t::String >(), "hoge");
toml::value w1(std::move(v1));
toml::value w2(std::move(v2));
toml::value w3(std::move(v3));
toml::value w4(std::move(v4));
toml::value w5(std::move(v5));
toml::value w6(std::move(v6));
toml::value w7(std::move(v7));
BOOST_CHECK_EQUAL(w1.type(), toml::value_t::Boolean);
BOOST_CHECK_EQUAL(w2.type(), toml::value_t::Integer);
BOOST_CHECK_EQUAL(w3.type(), toml::value_t::Float);
BOOST_CHECK_EQUAL(w4.type(), toml::value_t::String);
BOOST_CHECK_EQUAL(w5.type(), toml::value_t::Datetime);
BOOST_CHECK_EQUAL(w6.type(), toml::value_t::Array);
BOOST_CHECK_EQUAL(w7.type(), toml::value_t::Table);
BOOST_CHECK_EQUAL(w1.cast<toml::value_t::Boolean >(), true);
BOOST_CHECK_EQUAL(w2.cast<toml::value_t::Integer >(), 42);
BOOST_CHECK_EQUAL(w3.cast<toml::value_t::Float >(), 3.14);
BOOST_CHECK_EQUAL(w4.cast<toml::value_t::String >(), "hoge");
}