Skip to content

Commit

Permalink
Merge branch 'standard-normal-form' into refref
Browse files Browse the repository at this point in the history
  • Loading branch information
steve-downey committed Aug 23, 2024
2 parents 8a75835 + d52f8c5 commit ddc5f3d
Show file tree
Hide file tree
Showing 9 changed files with 83 additions and 34 deletions.
2 changes: 1 addition & 1 deletion src/Beman/Optional26/optional.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// src/Beman/Optional26/optional.cpp -*-C++-*-
// src/Beman/Optional26/optional.cpp -*-C++-*-
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception

#include <Beman/Optional26/optional.hpp>
3 changes: 2 additions & 1 deletion src/Beman/Optional26/tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ target_sources(
optional_ref_monadic.t.cpp
optional_refref.t.cpp
optional_refref_monadic.t.cpp
test_types.cpp)
test_types.cpp
test_utilities.cpp)

target_link_libraries(
beman_optional26_test
Expand Down
2 changes: 0 additions & 2 deletions src/Beman/Optional26/tests/optional.t.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@

#include <gtest/gtest.h>

TEST(OptionalTest, TestGTest) { ASSERT_EQ(1, 1); }

TEST(OptionalTest, Constructors) {
beman::optional26::optional<int> i1;
beman::optional26::optional<int> i2{beman::optional26::nullopt};
Expand Down
27 changes: 6 additions & 21 deletions src/Beman/Optional26/tests/optional_constexpr.t.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,14 @@

#include <Beman/Optional26/optional.hpp>
#include <Beman/Optional26/tests/test_types.hpp>
#include <Beman/Optional26/tests/test_utilities.hpp>

#include <functional>
#include <ranges>
#include <tuple>

#include <gtest/gtest.h>

TEST(OptionalConstexprTest, TestGTest) { ASSERT_EQ(1, 1); }


TEST(OptionalConstexprTest, Constructors) {
constexpr beman::optional26::optional<int> i1;
Expand Down Expand Up @@ -220,7 +219,7 @@ consteval bool testConstexprInPlace() {
return retval;
}

auto consteval constify(auto expr) { return (expr); }
using beman::optional26::tests::constify;

TEST(OptionalConstexprTest, InPlace) {
EXPECT_TRUE(constify(testConstexprInPlace()));
Expand Down Expand Up @@ -268,11 +267,6 @@ TEST(OptionalConstexprTest, Nullopt) {
EXPECT_TRUE(!std::is_default_constructible<beman::optional26::nullopt_t>::value);
}

struct move_detector {
move_detector() = default;
move_detector(move_detector&& rhs) { rhs.been_moved = true; }
bool been_moved = false;
};

TEST(OptionalConstexprTest, Observers) {
constexpr beman::optional26::optional<int> o1 = 42;
Expand All @@ -292,19 +286,6 @@ TEST(OptionalConstexprTest, Observers) {

}

namespace {
class Point
{
int x_;
int y_;
public:
constexpr Point() : x_(0), y_(0) {}
constexpr Point(int x, int y) : x_(x), y_(y) {}
auto operator<=>(const Point&) const = default;
bool operator==(const Point&) const = default;
};

}
TEST(OptionalConstexprTest, RelationalOps) {
constexpr beman::optional26::optional<int> o1{4};
constexpr beman::optional26::optional<int> o2{42};
Expand Down Expand Up @@ -389,6 +370,8 @@ TEST(OptionalConstexprTest, RelationalOps) {
}
}

using beman::optional26::tests::Point;

constexpr Point p4{2, 3};
constexpr Point p5{3, 4};

Expand Down Expand Up @@ -796,6 +779,8 @@ consteval bool testComparisons() {
}
}

using beman::optional26::tests::Point;

constexpr Point p4{2, 3};
constexpr Point p5{3, 4};

Expand Down
44 changes: 35 additions & 9 deletions src/Beman/Optional26/tests/optional_ref.t.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,6 @@

#include <gtest/gtest.h>

TEST(OptionalRefTest, TestGTest) { ASSERT_EQ(1, 1); }


TEST(OptionalRefTest, Constructors) {
beman::optional26::optional<int&> i1;
beman::optional26::optional<int&> i2{beman::optional26::nullopt};
Expand Down Expand Up @@ -405,12 +402,6 @@ TEST(OptionalRefTest, Nullopt) {
EXPECT_TRUE(!std::is_default_constructible<beman::optional26::nullopt_t>::value);
}

struct move_detector {
move_detector() = default;
move_detector(move_detector&& rhs) { rhs.been_moved = true; }
bool been_moved = false;
};

TEST(OptionalRefTest, Observers) {
int var = 42;
beman::optional26::optional<int&> o1 = var;
Expand Down Expand Up @@ -623,3 +614,38 @@ TEST(OptionalRefTest, AssignFromOptional) {
// optional_base_const_ref = [](){return beman::optional26::optional<derived>(derived(3, 4));}();
// TODO: Add to "fail-to-compile" tests when they exist
}

TEST(OptionalRefTest, ConstructFromOptional) {
int var = 42;
beman::optional26::optional<int&> o1 = beman::optional26::nullopt;
beman::optional26::optional<int&> o2{var};


using beman::optional26::tests::base;
using beman::optional26::tests::derived;

base b{1};
derived d(1, 2);
beman::optional26::optional<base&> empty_base;
beman::optional26::optional<base&> engaged_base{b};

beman::optional26::optional<derived&> empty_derived_ref;
beman::optional26::optional<derived&> engaged_derived_ref{d};

beman::optional26::optional<base&> optional_base_ref{empty_derived_ref};
EXPECT_FALSE(optional_base_ref.has_value());

beman::optional26::optional<base&> optional_base_ref2{engaged_derived_ref};
EXPECT_TRUE(optional_base_ref2.has_value());

beman::optional26::optional<derived> empty_derived;
beman::optional26::optional<derived> engaged_derived{d};

static_assert(std::is_constructible_v<const base&, derived>);

beman::optional26::optional<const base&> optional_base_const_ref{empty_derived};
EXPECT_FALSE(optional_base_const_ref.has_value());

beman::optional26::optional<const base&> optional_base_const_ref2{engaged_derived};
EXPECT_TRUE(optional_base_const_ref2.has_value());
}
3 changes: 3 additions & 0 deletions src/Beman/Optional26/tests/test_types.cpp
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
// src/Beman/Optional26/test/test_types.cpp -*-C++-*-
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception

#include <Beman/Optional26/tests/test_types.hpp>
17 changes: 17 additions & 0 deletions src/Beman/Optional26/tests/test_types.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,23 @@ struct derived : public base {
auto operator<=>(const derived&) const = default;
};

struct move_detector {
move_detector() = default;
move_detector(move_detector&& rhs) { rhs.been_moved = true; }
bool been_moved = false;
};

class Point {
int x_;
int y_;

public:
constexpr Point() : x_(0), y_(0) {}
constexpr Point(int x, int y) : x_(x), y_(y) {}
auto operator<=>(const Point&) const = default;
bool operator==(const Point&) const = default;
};

} // namespace beman::optional26::tests

#endif // BEMAN_OPTIONAL26_TESTS_TEST_TYPES_HPP
4 changes: 4 additions & 0 deletions src/Beman/Optional26/tests/test_utilities.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
// src/Beman/Optional26/test/test_utilities.cpp -*-C++-*-
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception

#include <Beman/Optional26/tests/test_utilities.hpp>
15 changes: 15 additions & 0 deletions src/Beman/Optional26/tests/test_utilities.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// src/Beman/Optional26/tests/test_utilities.hpp -*-C++-*-
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception

#ifndef BEMAN_OPTIONAL26_TESTS_TEST_UTILITIES_HPP
#define BEMAN_OPTIONAL26_TESTS_TEST_UTILITIES_HPP

namespace beman::optional26::tests {
/***
* Evaluate and return an expression in a consteval context for testing
* constexpr correctness.
*/
auto consteval constify(auto expr) { return (expr); }
} // namespace beman::optional26::tests

#endif

0 comments on commit ddc5f3d

Please sign in to comment.