Aleph-w 7.10.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
ah_cpp_compat_test.cc
Go to the documentation of this file.
1/*
2 Aleph_w
3
4 Data structures & Algorithms
5 https://github.com/lrleon/Aleph-w
6
7 This file is part of Aleph-w library
8
9 Copyright (c) 2002-2026 Leandro Rabindranath Leon
10
11 Permission is hereby granted, free of charge, to any person obtaining a copy
12 of this software and associated documentation files (the "Software"), to deal
13 in the Software without restriction, including without limitation the rights
14 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
15 copies of the Software, and to permit persons to whom the Software is
16 furnished to do so, subject to the following conditions:
17
18 The above copyright notice and this permission notice shall be included in all
19 copies or substantial portions of the Software.
20
21 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
24 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
26 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
27 SOFTWARE.
28*/
29
38#include <string>
39
40#include <gtest/gtest.h>
41
42#include <ah-cpp-compat.H>
43
44using Aleph::expected;
45// Not `using Aleph::unexpected`: the Windows UCRT declares a global
46// ::unexpected() (the C++98 std::unexpected), which that declaration would
47// conflict with. The uses below are qualified instead.
48
49namespace
50{
51// A small parse-like function returning an expected, the canonical use case.
52expected<int, std::string> parse_positive(int raw)
53{
54 if (raw <= 0)
55 return Aleph::unexpected<std::string>("must be positive");
56 return raw * 2;
57}
58} // namespace
59
60TEST(AhCppCompat, DetectionMacrosAreDefined)
61{
62 // The four macros must always be defined to 0 or 1.
63 EXPECT_TRUE(ALEPH_HAS_STD_EXPECTED == 0 or ALEPH_HAS_STD_EXPECTED == 1);
64 EXPECT_TRUE(ALEPH_HAS_STD_GENERATOR == 0 or ALEPH_HAS_STD_GENERATOR == 1);
65 EXPECT_TRUE(ALEPH_HAS_STD_FLAT_MAP == 0 or ALEPH_HAS_STD_FLAT_MAP == 1);
66 EXPECT_TRUE(ALEPH_HAS_STD_MDSPAN == 0 or ALEPH_HAS_STD_MDSPAN == 1);
67}
68
69TEST(AhCppCompat, HoldsValue)
70{
72 ASSERT_TRUE(e.has_value());
73 ASSERT_TRUE(static_cast<bool>(e));
74 EXPECT_EQ(e.value(), 41);
75 EXPECT_EQ(*e, 41);
76 EXPECT_EQ(e.value_or(-1), 41);
77}
78
79TEST(AhCppCompat, HoldsError)
80{
82 ASSERT_FALSE(e.has_value());
83 ASSERT_FALSE(static_cast<bool>(e));
84 EXPECT_EQ(e.error(), "boom");
85 EXPECT_EQ(e.value_or(-1), -1);
86}
87
88TEST(AhCppCompat, ValueOnErrorThrows)
89{
91 EXPECT_THROW((void) e.value(), Aleph::bad_expected_access<std::string>);
92}
93
94TEST(AhCppCompat, ParseLikeRoundTrip)
95{
96 auto ok = parse_positive(10);
97 ASSERT_TRUE(ok.has_value());
98 EXPECT_EQ(*ok, 20);
99
100 auto bad = parse_positive(-3);
101 ASSERT_FALSE(bad.has_value());
102 EXPECT_EQ(bad.error(), "must be positive");
103}
104
105TEST(AhCppCompat, Transform)
106{
108 auto doubled = e.transform([](int v) { return v * 2; });
109 ASSERT_TRUE(doubled.has_value());
110 EXPECT_EQ(*doubled, 42);
111
113 auto still_err = err.transform([](int v) { return v * 2; });
114 ASSERT_FALSE(still_err.has_value());
115 EXPECT_EQ(still_err.error(), "e");
116}
117
118TEST(AhCppCompat, AndThen)
119{
121 auto chained = e.and_then(
122 [](int v) -> expected<int, std::string> { return v + 100; });
123 ASSERT_TRUE(chained.has_value());
124 EXPECT_EQ(*chained, 105);
125
127 auto skipped = err.and_then(
128 [](int v) -> expected<int, std::string> { return v + 100; });
129 ASSERT_FALSE(skipped.has_value());
130 EXPECT_EQ(skipped.error(), "x");
131}
132
133TEST(AhCppCompat, OrElse)
134{
136 auto recovered = err.or_else(
137 [](const std::string &) -> expected<int, std::string> { return 7; });
138 ASSERT_TRUE(recovered.has_value());
139 EXPECT_EQ(*recovered, 7);
140}
141
142TEST(AhCppCompat, CopyAndMovePreserveState)
143{
144 expected<std::string, int> v = std::string("hello");
145 auto copy = v; // copy of a value
146 ASSERT_TRUE(copy.has_value());
147 EXPECT_EQ(*copy, "hello");
148
150 auto moved = std::move(e); // move of an error
151 ASSERT_FALSE(moved.has_value());
152 EXPECT_EQ(moved.error(), 404);
153
154 // Assignment that switches the active member (value -> error).
155 copy = Aleph::unexpected<int>(500);
156 ASSERT_FALSE(copy.has_value());
157 EXPECT_EQ(copy.error(), 500);
158}
159
160TEST(AhCppCompat, VoidExpectedBasic)
161{
163 ASSERT_TRUE(ok.has_value());
164 ASSERT_TRUE(static_cast<bool>(ok));
165 EXPECT_NO_THROW(ok.value());
166
168 ASSERT_FALSE(err.has_value());
169 ASSERT_FALSE(static_cast<bool>(err));
170 EXPECT_EQ(err.error(), "fail");
171 EXPECT_ANY_THROW(err.value());
172}
173
174TEST(AhCppCompat, VoidExpectedMonadic)
175{
177 auto chained = ok.and_then([]() -> expected<int, std::string> { return 42; });
178 ASSERT_TRUE(chained.has_value());
179 EXPECT_EQ(*chained, 42);
180
182 auto recovered = err.or_else(
183 [](const std::string &) -> expected<void, std::string> { return {}; });
184 ASSERT_TRUE(recovered.has_value());
185}
186
187namespace
188{
189// Copy assignable, as std::expected (Aleph::expected under C++23) requires
190// of its error type to be copy assigned; only the copy constructor throws.
191struct ThrowOnCopy
192{
193 int val = 0;
194 ThrowOnCopy(int v = 0) : val(v) {}
195 ThrowOnCopy(const ThrowOnCopy &) { throw std::runtime_error("copy threw"); }
196 ThrowOnCopy(ThrowOnCopy &&) noexcept = default;
197 ThrowOnCopy &operator=(const ThrowOnCopy &) = default;
198 ThrowOnCopy &operator=(ThrowOnCopy &&) noexcept = default;
199};
200} // namespace
201
202TEST(AhCppCompat, AssignmentExceptionSafety)
203{
204 expected<int, ThrowOnCopy> original = 100;
205 ASSERT_TRUE(original.has_value());
206
208 ASSERT_FALSE(other.has_value());
209
210 EXPECT_THROW(original = other, std::runtime_error);
211 ASSERT_TRUE(original.has_value());
212 EXPECT_EQ(*original, 100);
213}
C++20/23 compatibility layer: feature detection + Aleph:: polyfills.
#define ALEPH_HAS_STD_FLAT_MAP
#define ALEPH_HAS_STD_EXPECTED
#define ALEPH_HAS_STD_GENERATOR
#define ALEPH_HAS_STD_MDSPAN
Exception thrown by expected::value() when no value is held.
Minimal std::expected-style result type for C++20.
auto or_else(F &&f) const &
Monadic error recovery: handle the error.
constexpr T value_or(U &&default_value) const &
Get the value or a default.
constexpr bool has_value() const noexcept
Check if a value is held.
auto and_then(F &&f) const &
Monadic bind: chain another fallible operation.
constexpr const E & error() const &noexcept
Access the error (const lvalue).
auto transform(F &&f) const &
Monadic map: transform the value.
constexpr const T & value() const &
Access the value with exception on error (const lvalue).
Wrapper for the error value used to initialize an expected.
#define TEST(name)