1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
|
// Copyright (C) 2016-2020 Jonathan Müller <jonathanmueller.dev@gmail.com>
// This file is subject to the license terms in the LICENSE file
// found in the top-level directory of this distribution.
#include <type_safe/boolean.hpp>
#include <catch.hpp>
#include <sstream>
using namespace type_safe;
#ifndef TYPE_SAFE_TEST_NO_STATIC_ASSERT
static_assert(std::is_pod<boolean>::value, "");
// conversion checks
static_assert(std::is_constructible<boolean, bool>::value, "");
static_assert(!std::is_constructible<boolean, int>::value, "");
static_assert(std::is_assignable<boolean, bool>::value, "");
static_assert(!std::is_assignable<boolean, int>::value, "");
#endif
TEST_CASE("boolean")
{
SECTION("constructor")
{
boolean b1(true);
REQUIRE(static_cast<bool>(b1));
boolean b2(false);
REQUIRE(!static_cast<bool>(b2));
}
SECTION("assignment")
{
boolean b1(true);
b1 = false;
REQUIRE(!static_cast<bool>(b1));
b1 = true;
REQUIRE(static_cast<bool>(b1));
}
SECTION("negate")
{
boolean b1(true);
REQUIRE(!b1 == false);
boolean b2(false);
REQUIRE(!b2 == true);
}
SECTION("comparison")
{
boolean b1(true);
REQUIRE(b1 == true);
REQUIRE(true == b1);
REQUIRE(b1 != false);
REQUIRE(false != b1);
REQUIRE(b1 == boolean(true));
REQUIRE(b1 != boolean(false));
boolean b2(false);
REQUIRE(b2 == false);
REQUIRE(false == b2);
REQUIRE(b2 != true);
REQUIRE(true != b2);
REQUIRE(b2 == boolean(false));
REQUIRE(b2 != boolean(true));
}
SECTION("i/o")
{
std::ostringstream out;
std::istringstream in("0");
boolean b(true);
out << b;
REQUIRE(out.str() == "1");
in >> b;
REQUIRE(!static_cast<bool>(b));
}
}
|