Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 10:17:53

0001 /*
0002     tests/test_enums.cpp -- enumerations
0003 
0004     Copyright (c) 2016 Wenzel Jakob <wenzel.jakob@epfl.ch>
0005 
0006     All rights reserved. Use of this source code is governed by a
0007     BSD-style license that can be found in the LICENSE file.
0008 */
0009 
0010 #include "pybind11_tests.h"
0011 
0012 TEST_SUBMODULE(enums, m) {
0013     // test_unscoped_enum
0014     enum UnscopedEnum { EOne = 1, ETwo, EThree };
0015     py::enum_<UnscopedEnum>(m, "UnscopedEnum", py::arithmetic(), "An unscoped enumeration")
0016         .value("EOne", EOne, "Docstring for EOne")
0017         .value("ETwo", ETwo, "Docstring for ETwo")
0018         .value("EThree", EThree, "Docstring for EThree")
0019         .export_values();
0020 
0021     // test_scoped_enum
0022     enum class ScopedEnum { Two = 2, Three };
0023     py::enum_<ScopedEnum>(m, "ScopedEnum", py::arithmetic())
0024         .value("Two", ScopedEnum::Two)
0025         .value("Three", ScopedEnum::Three);
0026 
0027     m.def("test_scoped_enum", [](ScopedEnum z) {
0028         return "ScopedEnum::" + std::string(z == ScopedEnum::Two ? "Two" : "Three");
0029     });
0030 
0031     // test_binary_operators
0032     enum Flags { Read = 4, Write = 2, Execute = 1 };
0033     py::enum_<Flags>(m, "Flags", py::arithmetic())
0034         .value("Read", Flags::Read)
0035         .value("Write", Flags::Write)
0036         .value("Execute", Flags::Execute)
0037         .export_values();
0038 
0039     // test_implicit_conversion
0040     class ClassWithUnscopedEnum {
0041     public:
0042         enum EMode { EFirstMode = 1, ESecondMode };
0043 
0044         static EMode test_function(EMode mode) { return mode; }
0045     };
0046     py::class_<ClassWithUnscopedEnum> exenum_class(m, "ClassWithUnscopedEnum");
0047     exenum_class.def_static("test_function", &ClassWithUnscopedEnum::test_function);
0048     py::enum_<ClassWithUnscopedEnum::EMode>(exenum_class, "EMode")
0049         .value("EFirstMode", ClassWithUnscopedEnum::EFirstMode)
0050         .value("ESecondMode", ClassWithUnscopedEnum::ESecondMode)
0051         .export_values();
0052 
0053     // test_enum_to_int
0054     m.def("test_enum_to_int", [](int) {});
0055     m.def("test_enum_to_uint", [](uint32_t) {});
0056     m.def("test_enum_to_long_long", [](long long) {});
0057 
0058     // test_duplicate_enum_name
0059     enum SimpleEnum { ONE, TWO, THREE };
0060 
0061     m.def("register_bad_enum", [m]() {
0062         py::enum_<SimpleEnum>(m, "SimpleEnum")
0063             .value("ONE", SimpleEnum::ONE) // NOTE: all value function calls are called with the
0064                                            // same first parameter value
0065             .value("ONE", SimpleEnum::TWO)
0066             .value("ONE", SimpleEnum::THREE)
0067             .export_values();
0068     });
0069 
0070     // test_enum_scalar
0071     enum UnscopedUCharEnum : unsigned char {};
0072     enum class ScopedShortEnum : short {};
0073     enum class ScopedLongEnum : long {};
0074     enum UnscopedUInt64Enum : std::uint64_t {};
0075     static_assert(
0076         py::detail::all_of<
0077             std::is_same<py::enum_<UnscopedUCharEnum>::Scalar, unsigned char>,
0078             std::is_same<py::enum_<ScopedShortEnum>::Scalar, short>,
0079             std::is_same<py::enum_<ScopedLongEnum>::Scalar, long>,
0080             std::is_same<py::enum_<UnscopedUInt64Enum>::Scalar, std::uint64_t>>::value,
0081         "Error during the deduction of enum's scalar type with normal integer underlying");
0082 
0083     // test_enum_scalar_with_char_underlying
0084     enum class ScopedCharEnum : char { Zero, Positive };
0085     enum class ScopedWCharEnum : wchar_t { Zero, Positive };
0086     enum class ScopedChar32Enum : char32_t { Zero, Positive };
0087     enum class ScopedChar16Enum : char16_t { Zero, Positive };
0088 
0089     // test the scalar of char type enums according to chapter 'Character types'
0090     // from https://en.cppreference.com/w/cpp/language/types
0091     static_assert(
0092         py::detail::any_of<
0093             std::is_same<py::enum_<ScopedCharEnum>::Scalar, signed char>,  // e.g. gcc on x86
0094             std::is_same<py::enum_<ScopedCharEnum>::Scalar, unsigned char> // e.g. arm linux
0095             >::value,
0096         "char should be cast to either signed char or unsigned char");
0097     static_assert(sizeof(py::enum_<ScopedWCharEnum>::Scalar) == 2
0098                       || sizeof(py::enum_<ScopedWCharEnum>::Scalar) == 4,
0099                   "wchar_t should be either 16 bits (Windows) or 32 (everywhere else)");
0100     static_assert(
0101         py::detail::all_of<
0102             std::is_same<py::enum_<ScopedChar32Enum>::Scalar, std::uint_least32_t>,
0103             std::is_same<py::enum_<ScopedChar16Enum>::Scalar, std::uint_least16_t>>::value,
0104         "char32_t, char16_t (and char8_t)'s size, signedness, and alignment is determined");
0105 #if defined(PYBIND11_HAS_U8STRING)
0106     enum class ScopedChar8Enum : char8_t { Zero, Positive };
0107     static_assert(std::is_same<py::enum_<ScopedChar8Enum>::Scalar, unsigned char>::value);
0108 #endif
0109 
0110     // test_char_underlying_enum
0111     py::enum_<ScopedCharEnum>(m, "ScopedCharEnum")
0112         .value("Zero", ScopedCharEnum::Zero)
0113         .value("Positive", ScopedCharEnum::Positive);
0114     py::enum_<ScopedWCharEnum>(m, "ScopedWCharEnum")
0115         .value("Zero", ScopedWCharEnum::Zero)
0116         .value("Positive", ScopedWCharEnum::Positive);
0117     py::enum_<ScopedChar32Enum>(m, "ScopedChar32Enum")
0118         .value("Zero", ScopedChar32Enum::Zero)
0119         .value("Positive", ScopedChar32Enum::Positive);
0120     py::enum_<ScopedChar16Enum>(m, "ScopedChar16Enum")
0121         .value("Zero", ScopedChar16Enum::Zero)
0122         .value("Positive", ScopedChar16Enum::Positive);
0123 
0124     // test_bool_underlying_enum
0125     enum class ScopedBoolEnum : bool { FALSE, TRUE };
0126 
0127     // bool is unsigned (std::is_signed returns false) and 1-byte long, so represented with u8
0128     static_assert(std::is_same<py::enum_<ScopedBoolEnum>::Scalar, std::uint8_t>::value, "");
0129 
0130     py::enum_<ScopedBoolEnum>(m, "ScopedBoolEnum")
0131         .value("FALSE", ScopedBoolEnum::FALSE)
0132         .value("TRUE", ScopedBoolEnum::TRUE);
0133 }