test_enum.cpp
Go to the documentation of this file.
1 /*
2  tests/test_enums.cpp -- enumerations
3 
4  Copyright (c) 2016 Wenzel Jakob <wenzel.jakob@epfl.ch>
5 
6  All rights reserved. Use of this source code is governed by a
7  BSD-style license that can be found in the LICENSE file.
8 */
9 
10 #include "pybind11_tests.h"
11 
12 TEST_SUBMODULE(enums, m) {
13  // test_unscoped_enum
14  enum UnscopedEnum { EOne = 1, ETwo, EThree };
15  py::enum_<UnscopedEnum>(m, "UnscopedEnum", py::arithmetic(), "An unscoped enumeration")
16  .value("EOne", EOne, "Docstring for EOne")
17  .value("ETwo", ETwo, "Docstring for ETwo")
18  .value("EThree", EThree, "Docstring for EThree")
19  .export_values();
20 
21  // test_scoped_enum
22  enum class ScopedEnum { Two = 2, Three };
23  py::enum_<ScopedEnum>(m, "ScopedEnum", py::arithmetic())
24  .value("Two", ScopedEnum::Two)
25  .value("Three", ScopedEnum::Three);
26 
27  m.def("test_scoped_enum", [](ScopedEnum z) {
28  return "ScopedEnum::" + std::string(z == ScopedEnum::Two ? "Two" : "Three");
29  });
30 
31  // test_binary_operators
32  enum Flags { Read = 4, Write = 2, Execute = 1 };
33  py::enum_<Flags>(m, "Flags", py::arithmetic())
34  .value("Read", Flags::Read)
35  .value("Write", Flags::Write)
36  .value("Execute", Flags::Execute)
37  .export_values();
38 
39  // test_implicit_conversion
40  class ClassWithUnscopedEnum {
41  public:
42  enum EMode { EFirstMode = 1, ESecondMode };
43 
44  static EMode test_function(EMode mode) { return mode; }
45  };
46  py::class_<ClassWithUnscopedEnum> exenum_class(m, "ClassWithUnscopedEnum");
47  exenum_class.def_static("test_function", &ClassWithUnscopedEnum::test_function);
48  py::enum_<ClassWithUnscopedEnum::EMode>(exenum_class, "EMode")
49  .value("EFirstMode", ClassWithUnscopedEnum::EFirstMode)
50  .value("ESecondMode", ClassWithUnscopedEnum::ESecondMode)
51  .export_values();
52 
53  // test_enum_to_int
54  m.def("test_enum_to_int", [](int) {});
55  m.def("test_enum_to_uint", [](uint32_t) {});
56  m.def("test_enum_to_long_long", [](long long) {});
57 
58  // test_duplicate_enum_name
59  enum SimpleEnum { ONE, TWO, THREE };
60 
61  m.def("register_bad_enum", [m]() {
62  py::enum_<SimpleEnum>(m, "SimpleEnum")
63  .value("ONE", SimpleEnum::ONE) // NOTE: all value function calls are called with the
64  // same first parameter value
65  .value("ONE", SimpleEnum::TWO)
66  .value("ONE", SimpleEnum::THREE)
67  .export_values();
68  });
69 
70  // test_enum_scalar
71  enum UnscopedUCharEnum : unsigned char {};
72  enum class ScopedShortEnum : short {};
73  enum class ScopedLongEnum : long {};
74  enum UnscopedUInt64Enum : std::uint64_t {};
75  static_assert(
77  std::is_same<py::enum_<UnscopedUCharEnum>::Scalar, unsigned char>,
78  std::is_same<py::enum_<ScopedShortEnum>::Scalar, short>,
79  std::is_same<py::enum_<ScopedLongEnum>::Scalar, long>,
81  "Error during the deduction of enum's scalar type with normal integer underlying");
82 
83  // test_enum_scalar_with_char_underlying
84  enum class ScopedCharEnum : char { Zero, Positive };
85  enum class ScopedWCharEnum : wchar_t { Zero, Positive };
86  enum class ScopedChar32Enum : char32_t { Zero, Positive };
87  enum class ScopedChar16Enum : char16_t { Zero, Positive };
88 
89  // test the scalar of char type enums according to chapter 'Character types'
90  // from https://en.cppreference.com/w/cpp/language/types
91  static_assert(
93  std::is_same<py::enum_<ScopedCharEnum>::Scalar, signed char>, // e.g. gcc on x86
94  std::is_same<py::enum_<ScopedCharEnum>::Scalar, unsigned char> // e.g. arm linux
95  >::value,
96  "char should be cast to either signed char or unsigned char");
97  static_assert(sizeof(py::enum_<ScopedWCharEnum>::Scalar) == 2
98  || sizeof(py::enum_<ScopedWCharEnum>::Scalar) == 4,
99  "wchar_t should be either 16 bits (Windows) or 32 (everywhere else)");
100  static_assert(
104  "char32_t, char16_t (and char8_t)'s size, signedness, and alignment is determined");
105 #if defined(PYBIND11_HAS_U8STRING)
106  enum class ScopedChar8Enum : char8_t { Zero, Positive };
107  static_assert(std::is_same<py::enum_<ScopedChar8Enum>::Scalar, unsigned char>::value);
108 #endif
109 
110  // test_char_underlying_enum
111  py::enum_<ScopedCharEnum>(m, "ScopedCharEnum")
112  .value("Zero", ScopedCharEnum::Zero)
113  .value("Positive", ScopedCharEnum::Positive);
114  py::enum_<ScopedWCharEnum>(m, "ScopedWCharEnum")
115  .value("Zero", ScopedWCharEnum::Zero)
116  .value("Positive", ScopedWCharEnum::Positive);
117  py::enum_<ScopedChar32Enum>(m, "ScopedChar32Enum")
118  .value("Zero", ScopedChar32Enum::Zero)
119  .value("Positive", ScopedChar32Enum::Positive);
120  py::enum_<ScopedChar16Enum>(m, "ScopedChar16Enum")
121  .value("Zero", ScopedChar16Enum::Zero)
122  .value("Positive", ScopedChar16Enum::Positive);
123 
124  // test_bool_underlying_enum
125  enum class ScopedBoolEnum : bool { FALSE, TRUE };
126 
127  // bool is unsigned (std::is_signed returns false) and 1-byte long, so represented with u8
128  static_assert(std::is_same<py::enum_<ScopedBoolEnum>::Scalar, std::uint8_t>::value, "");
129 
130  py::enum_<ScopedBoolEnum>(m, "ScopedBoolEnum")
131  .value("FALSE", ScopedBoolEnum::FALSE)
132  .value("TRUE", ScopedBoolEnum::TRUE);
133 }
Matrix3f m
unsigned char uint8_t
Definition: ms_stdint.h:83
SCALAR Scalar
Definition: bench_gemm.cpp:46
uint16_t uint_least16_t
Definition: ms_stdint.h:104
#define FALSE
Definition: ccolamd.c:751
unsigned __int64 uint64_t
Definition: ms_stdint.h:95
unsigned int uint32_t
Definition: ms_stdint.h:85
TEST_SUBMODULE(enums, m)
Definition: test_enum.cpp:12
uint32_t uint_least32_t
Definition: ms_stdint.h:105
std::is_same< bools< Ts::value..., true >, bools< true, Ts::value... > > all_of
#define TRUE
Definition: ccolamd.c:750
static const DiscreteKey mode(modeKey, 2)
negation< all_of< negation< Ts >... > > any_of


gtsam
Author(s):
autogenerated on Tue Jul 4 2023 02:37:45