Skip to content

Commit

Permalink
port logical tests to test/unit.cpp - refs #15
Browse files Browse the repository at this point in the history
  • Loading branch information
Dane Springmeyer committed May 20, 2014
1 parent 0ef558f commit 7109df9
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 59 deletions.
4 changes: 0 additions & 4 deletions Jamroot
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ exe variant-test
:
<include>$(BOOST_DIR)/include
<include>./
<define>VARIANT_LOGICAL_TESTS
<cxxflags>-std=c++11
<variant>release:<cxxflags>-march=native
;
Expand All @@ -30,7 +29,6 @@ exe binary-visitor-test
:
<include>$(BOOST_DIR)/include
<include>./
<define>VARIANT_LOGICAL_TESTS
<cxxflags>-std=c++11
<variant>release:<cxxflags>-march=native
;
Expand All @@ -44,7 +42,6 @@ exe recursive-wrapper-test
:
<include>$(BOOST_DIR)/include
<include>./
<define>VARIANT_LOGICAL_TESTS
<cxxflags>-std=c++11
<variant>release:<cxxflags>-march=native
;
Expand All @@ -58,7 +55,6 @@ exe unique-ptr-test
:
<include>$(BOOST_DIR)/include
<include>./
<define>VARIANT_LOGICAL_TESTS
<cxxflags>-std=c++11
<variant>release:<cxxflags>-march=native
;
55 changes: 0 additions & 55 deletions test/bench_variant.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -196,61 +196,6 @@ int main (int argc, char** argv)
}
#endif

#ifdef VARIANT_LOGICAL_TESTS

std::cerr << util::detail::type_traits<bool, bool, int, double, std::string>::id << std::endl;
std::cerr << util::detail::type_traits<int, bool, int, double, std::string>::id << std::endl;
std::cerr << util::detail::type_traits<double, bool, int, double, std::string>::id << std::endl;
std::cerr << util::detail::type_traits<std::string, bool, int, double, std::string>::id << std::endl;
std::cerr << util::detail::type_traits<long, bool, int, double, std::string>::id << std::endl;
std::cerr << util::detail::type_traits<std::vector<int>, bool, int, double, std::string>::id << std::endl;

typedef util::variant<bool,int, double, std::string> variant_type;
variant_type v(std::string("test"));
util::apply_visitor(v, print());

v = std::string("ABC");
util::apply_visitor(v, print());

std::vector<variant_type> vec;
vec.emplace_back(std::move(v));
for (auto const& e : vec)
{
util::apply_visitor(e, print());
}

v=std::string("test");
util::apply_visitor(v, print());
v=123.345;
util::apply_visitor(v, print());
variant_type v2(std::string("testing a bit more"));
util::apply_visitor(v2, print());
variant_type v3(444);
util::apply_visitor(v3, print());

std::cerr << sizeof(v) << std::endl;
std::cerr << sizeof(v2) << std::endl;
std::cerr << sizeof(v3) << std::endl;
std::cerr << sizeof(boost::variant<bool,int, double, std::string>) << std::endl;


{
std::cerr << "---------- comparison test" << std::endl;
std::cerr << (variant_type(123) == variant_type(123)) << std::endl;
std::cerr << (variant_type(123) == variant_type(456)) << std::endl;
std::cerr << (variant_type(123) == variant_type(123.0)) << std::endl;
std::cerr << (variant_type(std::string("ABC")) == variant_type(std::string("ABC"))) << std::endl;
}

{
std::cerr << "---------- variant operator<<" << std::endl;
std::cerr << variant_type(123) << std::endl;
std::cerr << variant_type(true) << std::endl;
std::cerr << variant_type(3.14159) << std::endl;
std::cerr << variant_type(std::string("c++11 rock!")) << std::endl;
}

#endif

return EXIT_SUCCESS;
}
27 changes: 27 additions & 0 deletions test/unit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,30 @@
#include <ostream>
#include <memory>

TEST_CASE( "variant type traits", "[variant]" ) {
REQUIRE((util::detail::type_traits<bool, bool, int, double, std::string>::id == 3));
REQUIRE((util::detail::type_traits<int, bool, int, double, std::string>::id == 2));
REQUIRE((util::detail::type_traits<double, bool, int, double, std::string>::id == 1));
REQUIRE((util::detail::type_traits<std::string, bool, int, double, std::string>::id == 0));
REQUIRE((util::detail::type_traits<long, bool, int, double, std::string>::id == util::detail::invalid_value));
REQUIRE((util::detail::type_traits<std::vector<int>, bool, int, double, std::string>::id == util::detail::invalid_value));
}

TEST_CASE( "variant can be moved into vector", "[variant]" ) {
typedef util::variant<bool,std::string> variant_type;
variant_type v(std::string("test"));
std::vector<variant_type> vec;
vec.emplace_back(std::move(v));
REQUIRE(v.get<std::string>() != std::string("test"));
REQUIRE(vec.at(0).get<std::string>() == std::string("test"));
}

TEST_CASE( "variant should support built-in types", "[variant]" ) {
SECTION( "bool" ) {
util::variant<bool> v(true);
REQUIRE(v.valid());
REQUIRE(v.is<bool>());
REQUIRE(sizeof(v) == 16);
REQUIRE(v.get<bool>() == true);
v.set<bool>(false);
REQUIRE(v.get<bool>() == false);
Expand All @@ -25,6 +43,7 @@ TEST_CASE( "variant should support built-in types", "[variant]" ) {
util::variant<value_type> v(nullptr);
REQUIRE(v.valid());
REQUIRE(v.is<value_type>());
REQUIRE(sizeof(v) == 16);
REQUIRE(v.get<value_type>() == nullptr);
// FIXME: does not compile: ./variant.hpp:340:14: error: use of overloaded operator '<<' is ambiguous (with operand types 'std::__1::basic_ostream<char>' and 'const nullptr_t')
// https://github.com/mapbox/variant/issues/14
Expand All @@ -35,13 +54,15 @@ TEST_CASE( "variant should support built-in types", "[variant]" ) {
util::variant<value_type> v(value_type(new std::string("hello")));
REQUIRE(v.valid());
REQUIRE(v.is<value_type>());
REQUIRE(sizeof(v) == 16);
REQUIRE(*v.get<value_type>().get() == *value_type(new std::string("hello")).get());
}
SECTION( "string" ) {
typedef std::string value_type;
util::variant<value_type> v(value_type("hello"));
REQUIRE(v.valid());
REQUIRE(v.is<value_type>());
REQUIRE(sizeof(v) == 32);
REQUIRE(v.get<value_type>() == value_type("hello"));
v.set<value_type>(value_type("there"));
REQUIRE(v.get<value_type>() == value_type("there"));
Expand All @@ -53,6 +74,7 @@ TEST_CASE( "variant should support built-in types", "[variant]" ) {
util::variant<value_type> v(std::numeric_limits<value_type>::max());
REQUIRE(v.valid());
REQUIRE(v.is<value_type>());
REQUIRE(sizeof(v) == 16);
REQUIRE(v.get<value_type>() == std::numeric_limits<value_type>::max());
v.set<value_type>(value_type(0));
REQUIRE(v.get<value_type>() == value_type(0));
Expand All @@ -64,6 +86,7 @@ TEST_CASE( "variant should support built-in types", "[variant]" ) {
util::variant<value_type> v(std::numeric_limits<value_type>::max());
REQUIRE(v.valid());
REQUIRE(v.is<value_type>());
REQUIRE(sizeof(v) == 16);
REQUIRE(v.get<value_type>() == std::numeric_limits<value_type>::max());
v.set<value_type>(0);
REQUIRE(v.get<value_type>() == value_type(0));
Expand All @@ -75,6 +98,7 @@ TEST_CASE( "variant should support built-in types", "[variant]" ) {
util::variant<value_type> v(std::numeric_limits<value_type>::max());
REQUIRE(v.valid());
REQUIRE(v.is<value_type>());
REQUIRE(sizeof(v) == 16);
REQUIRE(v.get<value_type>() == std::numeric_limits<value_type>::max());
v.set<value_type>(0);
REQUIRE(v.get<value_type>() == value_type(0));
Expand All @@ -86,6 +110,7 @@ TEST_CASE( "variant should support built-in types", "[variant]" ) {
util::variant<value_type> v(std::numeric_limits<value_type>::max());
REQUIRE(v.valid());
REQUIRE(v.is<value_type>());
REQUIRE(sizeof(v) == 16);
REQUIRE(v.get<value_type>() == std::numeric_limits<value_type>::max());
v.set<value_type>(0);
REQUIRE(v.get<value_type>() == value_type(0));
Expand All @@ -97,6 +122,7 @@ TEST_CASE( "variant should support built-in types", "[variant]" ) {
util::variant<value_type> v(std::numeric_limits<value_type>::max());
REQUIRE(v.valid());
REQUIRE(v.is<value_type>());
REQUIRE(sizeof(v) == 16);
REQUIRE(v.get<value_type>() == std::numeric_limits<value_type>::max());
v.set<value_type>(0);
REQUIRE(v.get<value_type>() == value_type(0));
Expand Down Expand Up @@ -136,6 +162,7 @@ TEST_CASE( "variant should support custom types", "[variant]" ) {
util::variant<MissionInteger> v(MissionInteger(34838300));
REQUIRE(v.valid());
REQUIRE(v.is<MissionInteger>());
REQUIRE(sizeof(v) == 16);
REQUIRE(v.get<MissionInteger>() == MissionInteger(34838300));
REQUIRE(v.get<MissionInteger>().get() == MissionInteger::value_type(34838300));
// TODO: should both of the set usages below compile?
Expand Down

0 comments on commit 7109df9

Please sign in to comment.