]>
Commit | Line | Data |
---|---|---|
1e59de90 | 1 | // Copyright 2018-2022 Emil Dotchevski and Reverge Studios, Inc. |
20effc67 TL |
2 | |
3 | // Distributed under the Boost Software License, Version 1.0. (See accompanying | |
4 | // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) | |
5 | ||
1e59de90 TL |
6 | #ifdef BOOST_LEAF_TEST_SINGLE_HEADER |
7 | # include "leaf.hpp" | |
8 | #else | |
9 | # include <boost/leaf/error.hpp> | |
10 | #endif | |
11 | ||
20effc67 TL |
12 | #include "lightweight_test.hpp" |
13 | #include <future> | |
14 | #include <vector> | |
15 | #include <algorithm> | |
16 | #include <iterator> | |
17 | ||
18 | namespace leaf = boost::leaf; | |
19 | ||
20 | constexpr int ids_per_thread = 10000; | |
21 | ||
22 | std::vector<int> generate_ids() | |
23 | { | |
24 | std::vector<int> ids; | |
25 | ids.reserve(ids_per_thread); | |
26 | for(int i=0; i!=ids_per_thread-1; ++i) | |
27 | { | |
28 | int id = leaf::leaf_detail::new_id(); | |
29 | BOOST_TEST_NE(id&1, 0); | |
30 | int last = leaf::leaf_detail::current_id(); | |
31 | BOOST_TEST_EQ(last, leaf::leaf_detail::current_id()); | |
32 | BOOST_TEST_NE(last&1, 0); | |
33 | BOOST_TEST_EQ(last, id); | |
34 | ids.push_back(id); | |
35 | } | |
36 | return ids; | |
37 | } | |
38 | ||
39 | int main() | |
40 | { | |
41 | { | |
42 | leaf::error_id e1; | |
43 | leaf::error_id e2; | |
44 | BOOST_TEST(!e1); | |
45 | BOOST_TEST_EQ(e1.value(), 0); | |
46 | BOOST_TEST(!e2); | |
47 | BOOST_TEST_EQ(e2.value(), 0); | |
48 | BOOST_TEST(e1==e2); | |
49 | BOOST_TEST(!(e1!=e2)); | |
50 | BOOST_TEST(!(e1<e2)); | |
51 | BOOST_TEST(!(e2<e1)); | |
52 | } | |
53 | { | |
54 | leaf::error_id e1; | |
55 | leaf::error_id e2 = leaf::new_error(); | |
56 | BOOST_TEST(!e1); | |
57 | BOOST_TEST_EQ(e1.value(), 0); | |
58 | BOOST_TEST(e2); | |
59 | BOOST_TEST_EQ(e2.value(), 1); | |
60 | BOOST_TEST(!(e1==e2)); | |
61 | BOOST_TEST(e1!=e2); | |
62 | BOOST_TEST(e1<e2); | |
63 | BOOST_TEST(!(e2<e1)); | |
64 | } | |
65 | { | |
66 | leaf::error_id e1 = leaf::new_error(); | |
67 | leaf::error_id e2 = leaf::new_error(); | |
68 | BOOST_TEST(e1); | |
69 | BOOST_TEST_EQ(e1.value(), 5); | |
70 | BOOST_TEST(e2); | |
71 | BOOST_TEST_EQ(e2.value(), 9); | |
72 | BOOST_TEST(!(e1==e2)); | |
73 | BOOST_TEST(e1!=e2); | |
74 | BOOST_TEST(e1<e2); | |
75 | BOOST_TEST(!(e2<e1)); | |
76 | } | |
77 | { | |
78 | leaf::error_id e1 = leaf::new_error(); | |
79 | leaf::error_id e2 = e1; | |
80 | BOOST_TEST(e1); | |
81 | BOOST_TEST_EQ(e1.value(), 13); | |
82 | BOOST_TEST(e2); | |
83 | BOOST_TEST_EQ(e2.value(), 13); | |
84 | BOOST_TEST(e1==e2); | |
85 | BOOST_TEST(!(e1!=e2)); | |
86 | BOOST_TEST(!(e1<e2)); | |
87 | BOOST_TEST(!(e2<e1)); | |
88 | } | |
89 | { | |
90 | leaf::error_id e1 = leaf::new_error(); | |
91 | leaf::error_id e2; e2 = e1; | |
92 | BOOST_TEST(e1); | |
93 | BOOST_TEST_EQ(e1.value(), 17); | |
94 | BOOST_TEST(e2); | |
95 | BOOST_TEST_EQ(e2.value(), 17); | |
96 | BOOST_TEST(e1==e2); | |
97 | BOOST_TEST(!(e1!=e2)); | |
98 | BOOST_TEST(!(e1<e2)); | |
99 | BOOST_TEST(!(e2<e1)); | |
100 | } | |
101 | #ifdef BOOST_LEAF_NO_THREADS | |
102 | std::vector<int> all_ids = generate_ids(); | |
103 | #else | |
104 | constexpr int thread_count = 100; | |
105 | using thread_ids = std::future<std::vector<int>>; | |
106 | std::vector<thread_ids> fut; | |
107 | fut.reserve(thread_count); | |
108 | std::generate_n( | |
109 | std::back_inserter(fut), | |
110 | thread_count, | |
111 | [=] | |
112 | { | |
113 | return std::async(std::launch::async, &generate_ids); | |
114 | }); | |
115 | std::vector<int> all_ids; | |
116 | for(auto & f : fut) | |
117 | { | |
118 | auto fv = f.get(); | |
119 | all_ids.insert(all_ids.end(), fv.begin(), fv.end()); | |
120 | } | |
121 | #endif | |
122 | std::sort(all_ids.begin(), all_ids.end()); | |
123 | auto u = std::unique(all_ids.begin(), all_ids.end()); | |
124 | BOOST_TEST(u==all_ids.end()); | |
125 | ||
126 | return boost::report_errors(); | |
127 | } |