mirror of
https://github.com/ToruNiina/toml11.git
synced 2025-09-17 17:58:09 +08:00
make tests/ and toml/ dir and move codes
This commit is contained in:
76
tests/test.cpp
Normal file
76
tests/test.cpp
Normal file
@@ -0,0 +1,76 @@
|
||||
#include "toml.hpp"
|
||||
#include <map>
|
||||
#include <list>
|
||||
|
||||
int main()
|
||||
{
|
||||
bool b = true;
|
||||
int i=42;
|
||||
double f = 3.14;
|
||||
toml::value v1(i);
|
||||
toml::value v2(b);
|
||||
toml::value v3(f);
|
||||
toml::value v4("hoge");
|
||||
|
||||
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;
|
||||
|
||||
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;
|
||||
}
|
89
tests/test_traits.cpp
Normal file
89
tests/test_traits.cpp
Normal file
@@ -0,0 +1,89 @@
|
||||
#define BOOST_TEST_MODULE "test_traits"
|
||||
#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 <list>
|
||||
#include <forward_list>
|
||||
#include <deque>
|
||||
#include <array>
|
||||
#include <map>
|
||||
#include <set>
|
||||
#include <unordered_map>
|
||||
#include <unordered_set>
|
||||
#include <string>
|
||||
|
||||
struct dummy_type{};
|
||||
|
||||
template<typename T>
|
||||
struct dummy_container
|
||||
{
|
||||
typedef T value_type;
|
||||
typedef value_type* pointer;
|
||||
typedef value_type& reference;
|
||||
typedef value_type const* const_pointer;
|
||||
typedef value_type const& const_reference;
|
||||
typedef pointer iterator;
|
||||
typedef const_pointer const_iterator;
|
||||
};
|
||||
|
||||
typedef std::array<dummy_type, 10> std_array_type;
|
||||
typedef std::map<std::string, dummy_type> std_map_type;
|
||||
typedef std::unordered_map<std::string, dummy_type> std_unordered_map_type;
|
||||
|
||||
BOOST_AUTO_TEST_CASE(test_has_xxx)
|
||||
{
|
||||
BOOST_CHECK(toml::detail::has_iterator<std::list<dummy_type>>::value);
|
||||
BOOST_CHECK(toml::detail::has_iterator<std::forward_list<dummy_type>>::value);
|
||||
BOOST_CHECK(toml::detail::has_iterator<std::deque<dummy_type>>::value);
|
||||
BOOST_CHECK(toml::detail::has_iterator<std::vector<dummy_type>>::value);
|
||||
BOOST_CHECK(toml::detail::has_iterator<std::set<dummy_type>>::value);
|
||||
BOOST_CHECK(toml::detail::has_iterator<std::unordered_set<std::string>>::value);
|
||||
BOOST_CHECK(toml::detail::has_iterator<std_array_type>::value);
|
||||
BOOST_CHECK(toml::detail::has_iterator<std_map_type>::value);
|
||||
BOOST_CHECK(toml::detail::has_iterator<std_unordered_map_type>::value);
|
||||
BOOST_CHECK(toml::detail::has_iterator<dummy_container<dummy_type>>::value);
|
||||
|
||||
BOOST_CHECK(toml::detail::has_value_type<std::list<dummy_type>>::value);
|
||||
BOOST_CHECK(toml::detail::has_value_type<std::forward_list<dummy_type>>::value);
|
||||
BOOST_CHECK(toml::detail::has_value_type<std::deque<dummy_type>>::value);
|
||||
BOOST_CHECK(toml::detail::has_value_type<std::vector<dummy_type>>::value);
|
||||
BOOST_CHECK(toml::detail::has_value_type<std_array_type>::value);
|
||||
BOOST_CHECK(toml::detail::has_value_type<std::set<dummy_type>>::value);
|
||||
BOOST_CHECK(toml::detail::has_value_type<std::unordered_set<std::string>>::value);
|
||||
BOOST_CHECK(toml::detail::has_value_type<std_map_type>::value);
|
||||
BOOST_CHECK(toml::detail::has_value_type<std_unordered_map_type>::value);
|
||||
BOOST_CHECK(toml::detail::has_value_type<dummy_container<dummy_type>>::value);
|
||||
|
||||
BOOST_CHECK(toml::detail::has_key_type<std_map_type>::value);
|
||||
BOOST_CHECK(toml::detail::has_key_type<std_unordered_map_type>::value);
|
||||
BOOST_CHECK(toml::detail::has_mapped_type<std_map_type>::value);
|
||||
BOOST_CHECK(toml::detail::has_mapped_type<std_unordered_map_type>::value);
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(test_is_xxx)
|
||||
{
|
||||
BOOST_CHECK(toml::detail::is_container<std::list<dummy_type>>::value);
|
||||
BOOST_CHECK(toml::detail::is_container<std::forward_list<dummy_type>>::value);
|
||||
BOOST_CHECK(toml::detail::is_container<std::deque<dummy_type>>::value);
|
||||
BOOST_CHECK(toml::detail::is_container<std::vector<dummy_type>>::value);
|
||||
BOOST_CHECK(toml::detail::is_container<std_array_type>::value);
|
||||
BOOST_CHECK(toml::detail::is_container<std::set<dummy_type>>::value);
|
||||
BOOST_CHECK(toml::detail::is_container<std::unordered_set<std::string>>::value);
|
||||
BOOST_CHECK(toml::detail::is_container<std_map_type>::value);
|
||||
BOOST_CHECK(toml::detail::is_container<std_unordered_map_type>::value);
|
||||
BOOST_CHECK(toml::detail::is_container<dummy_container<dummy_type>>::value);
|
||||
|
||||
BOOST_CHECK(toml::detail::is_map<std_map_type>::value);
|
||||
BOOST_CHECK(toml::detail::is_map<std_unordered_map_type>::value);
|
||||
|
||||
BOOST_CHECK(toml::detail::is_key_convertible<std_map_type>::value);
|
||||
BOOST_CHECK(toml::detail::is_key_convertible<std_unordered_map_type>::value);
|
||||
}
|
||||
|
||||
|
||||
|
Reference in New Issue
Block a user