File: expect.cpp

package info (click to toggle)
boost-ext-ut 2.3.1-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 620 kB
  • sloc: cpp: 6,773; makefile: 13; sh: 10
file content (123 lines) | stat: -rw-r--r-- 3,571 bytes parent folder | download
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
//
// Copyright (c) 2019-2020 Kris Jusiak (kris at jusiak dot net)
//
// 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)
//
#include <array>
#include <boost/ut.hpp>
#include <memory>
#include <string_view>
#include <vector>

constexpr auto sum = [](auto... args) { return (0 + ... + args); };

struct dummy_struct {};
int main() {
  using namespace boost::ut;

  "operators"_test = [] {
    expect(0_i == sum());
    expect(2_i != sum(1, 2));
    expect(sum(1) >= 0_i);
    expect(sum(1) <= 1_i);
  };

  "message"_test = [] { expect(3_i == sum(1, 2)) << "wrong sum"; };

  "expressions"_test = [] {
    expect(0_i == sum() and 42_i == sum(40, 2));
    expect(1_i == sum() or 0_i == sum());
    expect(1_i == sum() or (sum() != 0_i or sum(1) > 0_i)) << "compound";
  };

  "that"_test = [] {
    expect(that % 0 == sum());
    expect(that % 42 == sum(40, 2) and that % (1 + 2) == sum(1, 2));
    expect(that % 1 != 2 or 2_i > 3);
  };

  "eq/neq/gt/ge/lt/le"_test = [] {
    // type_traits::is_stream_insertable_v constraint check

    static_assert(type_traits::is_stream_insertable_v<int>);
    static_assert(!type_traits::is_stream_insertable_v<dummy_struct>);

    // it seems it produces nice error information
    // leaving this as easy way to check failing compilation in case of doubt
    // expect(eq(dummy_struct{}, sum(40, 2)));
    //  gcc
    // expect.cpp:46:14: error: no matching function for call to ‘eq(dummy_struct, int)’
    //    46 |     expect(eq(dummy_struct{}, sum(40, 2)));
    //       |            ~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~
    // clang
    // expect.cpp:51:12: error: no matching function for call to 'eq'
    //    51 |     expect(eq(dummy_struct{}, sum(40, 2)));
    //       |            ^~

    expect(eq(42, sum(40, 2)));
    expect(neq(1, 2));
    expect(eq(sum(1), 1) and neq(sum(1, 2), 2));
    expect(eq(1, 1) and that % 1 == 1 and 1_i == 1);
  };

  "floating points"_test = [] {
    expect(42.1_d == 42.101) << "epsilon=0.1";
    expect(42.10_d == 42.101) << "epsilon=0.01";
    expect(42.10000001 == 42.1_d) << "epsilon=0.1";
  };

  "strings"_test = [] {
    using namespace std::literals::string_view_literals;
    using namespace std::literals::string_literals;

    expect("str"s == "str"s);
    expect("str1"s != "str2"s);

    expect("str"sv == "str"sv);
    expect("str1"sv != "str2"sv);

    expect("str"sv == "str"s);
    expect("str1"sv != "str2"s);
    expect("str"s == "str"sv);
    expect("str1"s != "str2"sv);
  };

  "types"_test = [] {
    expect(type<int> == type<int>);
    expect(type<float> != type<double>);

    [[maybe_unused]] const auto i = 0;
    expect(type<const int> == type<decltype(i)>);
    expect(type<decltype(i)> != type<int>);
  };

  "containers"_test = [] {
    std::vector v1{1, 2, 3};
    std::vector v2{1, 2, 3};
    expect(v1 == v2);
    expect(std::vector{"a", "b"} != std::vector{"c"});
    expect(std::array{true, false} == std::array{true, false});
  };

  "constant"_test = [] {
    constexpr auto compile_time_v = 42;
    auto run_time_v = 99;
    // clang-format off
    expect(constant<42_i == compile_time_v> and run_time_v == 99_i);
    // clang-format on
  };

  "convertible"_test = [] {
    expect(bool(std::make_unique<int>()));
    expect(not bool(std::unique_ptr<int>{}));
  };

  "boolean"_test = [] {
    expect("true"_b);
    expect("true"_b or not "true"_b);
    expect((not "true"_b) != "true"_b);
    expect("has value"_b == "value is set"_b);
  };
}