Aleph-w 3.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;
46
47namespace
48{
49// A small parse-like function returning an expected, the canonical use case.
50expected<int, std::string> parse_positive(int raw)
51{
52 if (raw <= 0)
53 return unexpected<std::string>("must be positive");
54 return raw * 2;
55}
56} // namespace
57
58TEST(AhCppCompat, DetectionMacrosAreDefined)
59{
60 // The four macros must always be defined to 0 or 1.
61 EXPECT_TRUE(ALEPH_HAS_STD_EXPECTED == 0 or ALEPH_HAS_STD_EXPECTED == 1);
62 EXPECT_TRUE(ALEPH_HAS_STD_GENERATOR == 0 or ALEPH_HAS_STD_GENERATOR == 1);
63 EXPECT_TRUE(ALEPH_HAS_STD_FLAT_MAP == 0 or ALEPH_HAS_STD_FLAT_MAP == 1);
64 EXPECT_TRUE(ALEPH_HAS_STD_MDSPAN == 0 or ALEPH_HAS_STD_MDSPAN == 1);
65}
66
67TEST(AhCppCompat, HoldsValue)
68{
70 ASSERT_TRUE(e.has_value());
71 ASSERT_TRUE(static_cast<bool>(e));
72 EXPECT_EQ(e.value(), 41);
73 EXPECT_EQ(*e, 41);
74 EXPECT_EQ(e.value_or(-1), 41);
75}
76
77TEST(AhCppCompat, HoldsError)
78{
80 ASSERT_FALSE(e.has_value());
81 ASSERT_FALSE(static_cast<bool>(e));
82 EXPECT_EQ(e.error(), "boom");
83 EXPECT_EQ(e.value_or(-1), -1);
84}
85
86TEST(AhCppCompat, ValueOnErrorThrows)
87{
89 EXPECT_THROW((void) e.value(), Aleph::bad_expected_access<std::string>);
90}
91
92TEST(AhCppCompat, ParseLikeRoundTrip)
93{
94 auto ok = parse_positive(10);
95 ASSERT_TRUE(ok.has_value());
96 EXPECT_EQ(*ok, 20);
97
98 auto bad = parse_positive(-3);
99 ASSERT_FALSE(bad.has_value());
100 EXPECT_EQ(bad.error(), "must be positive");
101}
102
103TEST(AhCppCompat, Transform)
104{
106 auto doubled = e.transform([](int v) { return v * 2; });
107 ASSERT_TRUE(doubled.has_value());
108 EXPECT_EQ(*doubled, 42);
109
111 auto still_err = err.transform([](int v) { return v * 2; });
112 ASSERT_FALSE(still_err.has_value());
113 EXPECT_EQ(still_err.error(), "e");
114}
115
116TEST(AhCppCompat, AndThen)
117{
119 auto chained = e.and_then(
120 [](int v) -> expected<int, std::string> { return v + 100; });
121 ASSERT_TRUE(chained.has_value());
122 EXPECT_EQ(*chained, 105);
123
125 auto skipped = err.and_then(
126 [](int v) -> expected<int, std::string> { return v + 100; });
127 ASSERT_FALSE(skipped.has_value());
128 EXPECT_EQ(skipped.error(), "x");
129}
130
131TEST(AhCppCompat, OrElse)
132{
134 auto recovered = err.or_else(
135 [](const std::string &) -> expected<int, std::string> { return 7; });
136 ASSERT_TRUE(recovered.has_value());
137 EXPECT_EQ(*recovered, 7);
138}
139
140TEST(AhCppCompat, CopyAndMovePreserveState)
141{
142 expected<std::string, int> v = std::string("hello");
143 auto copy = v; // copy of a value
144 ASSERT_TRUE(copy.has_value());
145 EXPECT_EQ(*copy, "hello");
146
148 auto moved = std::move(e); // move of an error
149 ASSERT_FALSE(moved.has_value());
150 EXPECT_EQ(moved.error(), 404);
151
152 // Assignment that switches the active member (value -> error).
153 copy = unexpected<int>(500);
154 ASSERT_FALSE(copy.has_value());
155 EXPECT_EQ(copy.error(), 500);
156}
157
158TEST(AhCppCompat, VoidExpectedBasic)
159{
161 ASSERT_TRUE(ok.has_value());
162 ASSERT_TRUE(static_cast<bool>(ok));
163 EXPECT_NO_THROW(ok.value());
164
166 ASSERT_FALSE(err.has_value());
167 ASSERT_FALSE(static_cast<bool>(err));
168 EXPECT_EQ(err.error(), "fail");
169 EXPECT_ANY_THROW(err.value());
170}
171
172TEST(AhCppCompat, VoidExpectedMonadic)
173{
175 auto chained = ok.and_then([]() -> expected<int, std::string> { return 42; });
176 ASSERT_TRUE(chained.has_value());
177 EXPECT_EQ(*chained, 42);
178
180 auto recovered = err.or_else(
181 [](const std::string &) -> expected<void, std::string> { return {}; });
182 ASSERT_TRUE(recovered.has_value());
183}
184
185namespace
186{
187struct ThrowOnCopy
188{
189 int val = 0;
190 ThrowOnCopy(int v = 0) : val(v) {}
191 ThrowOnCopy(const ThrowOnCopy &) { throw std::runtime_error("copy threw"); }
192 ThrowOnCopy(ThrowOnCopy &&) noexcept = default;
193 ThrowOnCopy &operator=(const ThrowOnCopy &) = delete;
194 ThrowOnCopy &operator=(ThrowOnCopy &&) noexcept = default;
195};
196} // namespace
197
198TEST(AhCppCompat, AssignmentExceptionSafety)
199{
200 expected<int, ThrowOnCopy> original = 100;
201 ASSERT_TRUE(original.has_value());
202
204 ASSERT_FALSE(other.has_value());
205
206 EXPECT_THROW(original = other, std::runtime_error);
207 ASSERT_TRUE(original.has_value());
208 EXPECT_EQ(*original, 100);
209}
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)