Google OR-Tools v9.15
a fast and portable software suite for combinatorial optimization
Loading...
Searching...
No Matches
enums_testing.h
Go to the documentation of this file.
1// Copyright 2010-2025 Google LLC
2// Licensed under the Apache License, Version 2.0 (the "License");
3// you may not use this file except in compliance with the License.
4// You may obtain a copy of the License at
5//
6// http://www.apache.org/licenses/LICENSE-2.0
7//
8// Unless required by applicable law or agreed to in writing, software
9// distributed under the License is distributed on an "AS IS" BASIS,
10// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11// See the License for the specific language governing permissions and
12// limitations under the License.
13
14// Typed tests for enums that use enums.h.
15#ifndef ORTOOLS_MATH_OPT_CPP_ENUMS_TESTING_H_
16#define ORTOOLS_MATH_OPT_CPP_ENUMS_TESTING_H_
17
18#include <limits>
19#include <optional>
20#include <type_traits>
21#include <vector>
22
23#include "absl/numeric/int128.h"
24#include "absl/strings/string_view.h"
25#include "gtest/gtest.h"
26#include "ortools/base/gmock.h"
29
31
32// A type parameterized test suite for testing the correct implementation of
33// Enum<E> for a given enum.
34//
35// Usage:
36//
37// INSTANTIATE_TYPED_TEST_SUITE_P(<E>, EnumTest, <E>);
38//
39template <typename E>
40class EnumTest : public testing::Test {
41 public:
42};
43
45
46TYPED_TEST_P(EnumTest, UnderlyingTypeFits) {
47 // We could static_assert that but using a gUnit test works too.
48 using underlying_type_limits =
49 std::numeric_limits<std::underlying_type_t<TypeParam>>;
50 using Proto = typename Enum<TypeParam>::Proto;
51 CHECK_GE(EnumProto<Proto>::kMin, underlying_type_limits::min());
52 CHECK_LE(EnumProto<Proto>::kMax, underlying_type_limits::max());
53}
54
55TYPED_TEST_P(EnumTest, AllProtoValues) {
56 using Proto = typename Enum<TypeParam>::Proto;
57
58 bool found_unspecified = false;
59 std::vector<TypeParam> found_values;
60 for (int i = EnumProto<Proto>::kMin; i <= EnumProto<Proto>::kMax; ++i) {
62 continue;
63 }
64
65 const auto proto_value = static_cast<Proto>(i);
66 SCOPED_TRACE(proto_value);
67
68 const std::optional<TypeParam> cpp_enum = EnumFromProto(proto_value);
69 if (proto_value == Enum<TypeParam>::kProtoUnspecifiedValue) {
70 found_unspecified = true;
71 ASSERT_FALSE(cpp_enum.has_value());
72 } else {
73 ASSERT_TRUE(cpp_enum.has_value());
74 found_values.push_back(*cpp_enum);
75 }
76
77 const Proto reconverted_proto_value = EnumToProto(cpp_enum);
78 EXPECT_EQ(proto_value, reconverted_proto_value);
79 }
80
81 // We should have found all C++ enum + nullopt when traversing all possible
82 // Proto enums. And the AllValues() of C++ should not contain any additional
83 // value.
84 ASSERT_TRUE(found_unspecified);
85 ASSERT_THAT(found_values, ::testing::UnorderedElementsAreArray(
87}
88
89TYPED_TEST_P(EnumTest, AllCppValuesString) {
90 for (const TypeParam cpp_value : Enum<TypeParam>::AllValues()) {
91 const auto cpp_underlying_value =
92 static_cast<std::underlying_type_t<TypeParam>>(cpp_value);
93 const std::optional<absl::string_view> opt_str_value =
94 EnumToOptString(cpp_value);
95 ASSERT_TRUE(opt_str_value.has_value())
96 << "cpp_value: " << cpp_underlying_value;
98 ::testing::Optional(cpp_value))
99 << "cpp_value: " << cpp_underlying_value;
100 }
101}
102
103REGISTER_TYPED_TEST_SUITE_P(EnumTest, UnderlyingTypeFits, AllProtoValues,
104 AllCppValuesString);
105
106} // namespace operations_research::math_opt
107
108#endif // ORTOOLS_MATH_OPT_CPP_ENUMS_TESTING_H_
EXPECT_THAT(ComputeInfeasibleSubsystem(model, GetParam().solver_type), IsOkAndHolds(IsInfeasible(true, ModelSubset{ .variable_bounds={{x, ModelSubset::Bounds{.lower=false,.upper=true}}},.linear_constraints={ {c, ModelSubset::Bounds{.lower=true,.upper=false}}}})))
std::optional< absl::string_view > EnumToOptString(E value)
Definition enums.h:297
std::optional< typename EnumProto< P >::Cpp > EnumFromProto(P proto_value)
Definition enums.h:281
REGISTER_TYPED_TEST_SUITE_P(EnumTest, UnderlyingTypeFits, AllProtoValues, AllCppValuesString)
TYPED_TEST_P(EnumTest, UnderlyingTypeFits)
std::optional< E > EnumFromString(absl::string_view str)
Definition enums.h:302
Enum< E >::Proto EnumToProto(std::optional< E > value)
Definition enums.h:270
static constexpr ProtoEnumIsValid kIsValid
Definition enums.h:183
static constexpr Proto kProtoUnspecifiedValue
Definition enums.h:143
static absl::Span< const E > AllValues()