2
0
mirror of https://github.com/boostorg/pfr.git synced 2026-01-19 04:22:13 +00:00
Files
pfr/test/common/global_ops.cpp

122 lines
2.6 KiB
C++

// Copyright (c) 2016 Antony Polukhin
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
#ifdef BOOST_PFR_TEST_FLAT
#include <boost/pfr/flat/global_ops.hpp>
#endif
#ifdef BOOST_PFR_TEST_PRECISE
#include <boost/pfr/precise/global_ops.hpp>
#endif
#include <boost/core/lightweight_test.hpp>
#include <iostream>
#include <typeinfo>
#include <tuple>
#include <sstream>
#include <set>
#include <string>
#include <boost/functional/hash.hpp>
#include <unordered_set>
template <class T>
void test_comparable_struct() {
T s1 {0, 1, "Hello", false, 6,7,8,9,10,11};
T s2 = s1;
T s3 {0, 1, "Hello", false, 6,7,8,9,10,11111};
BOOST_TEST(s1 == s2);
BOOST_TEST(s1 <= s2);
BOOST_TEST(s1 >= s2);
BOOST_TEST(!(s1 != s2));
BOOST_TEST(!(s1 == s3));
BOOST_TEST(s1 != s3);
BOOST_TEST(s1 < s3);
BOOST_TEST(s3 > s2);
BOOST_TEST(s1 <= s3);
BOOST_TEST(s3 >= s2);
std::cout << s1 << std::endl;
T s4;
std::stringstream ss;
ss.exceptions ( std::ios::failbit);
ss << s1;
ss >> s4;
std::cout << s4 << std::endl;
BOOST_TEST(s1 == s4);
int i = 1, j = 2;
BOOST_TEST_NE(i, j);
}
void test_empty_struct() {
struct empty {};
std::cout << empty{};
BOOST_TEST(empty{} == empty{});
}
struct adl_hash {
template <class T>
std::size_t operator()(const T& val) const {
using namespace boost;
return hash_value(val);
}
};
template <class Comparator>
void test_with_contatiners() {
struct testing { bool b1, b2; int i; };
std::set<testing, Comparator > t{
{true, true, 100},
{false, true, 100},
{true, false, 100},
{true, true, 101}
};
BOOST_TEST(t.find({true, true, 100}) != t.end());
BOOST_TEST_EQ(t.count({true, true, 100}), 1u);
std::unordered_set<testing, adl_hash> us(t.begin(), t.end());
BOOST_TEST_EQ(us.size(), t.size());
}
namespace foo {
struct comparable_struct {
int i; short s; char data[50]; bool bl; int a,b,c,d,e,f;
};
}
void test_implicit_conversions() {
std::stringstream ss;
ss << std::true_type{};
BOOST_TEST_EQ(ss.str(), "{}"); // Breaks implicit conversion :(
}
int main() {
test_comparable_struct<foo::comparable_struct>();
struct local_comparable_struct {
int i; short s; char data[50]; bool bl; int a,b,c,d,e,f;
};
test_comparable_struct<local_comparable_struct>();
test_empty_struct();
test_with_contatiners<std::less<>>();
test_with_contatiners<std::greater<>>();
test_implicit_conversions();
return boost::report_errors();
}