2016-08-12 22:57:24 +00:00
|
|
|
/*
|
|
|
|
tests/test_enums.cpp -- enumerations
|
|
|
|
|
|
|
|
Copyright (c) 2016 Wenzel Jakob <wenzel.jakob@epfl.ch>
|
|
|
|
|
|
|
|
All rights reserved. Use of this source code is governed by a
|
|
|
|
BSD-style license that can be found in the LICENSE file.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "pybind11_tests.h"
|
|
|
|
|
Update all remaining tests to new test styles
This udpates all the remaining tests to the new test suite code and
comment styles started in #898. For the most part, the test coverage
here is unchanged, with a few minor exceptions as noted below.
- test_constants_and_functions: this adds more overload tests with
overloads with different number of arguments for more comprehensive
overload_cast testing. The test style conversion broke the overload
tests under MSVC 2015, prompting the additional tests while looking
for a workaround.
- test_eigen: this dropped the unused functions `get_cm_corners` and
`get_cm_corners_const`--these same tests were duplicates of the same
things provided (and used) via ReturnTester methods.
- test_opaque_types: this test had a hidden dependence on ExampleMandA
which is now fixed by using the global UserType which suffices for the
relevant test.
- test_methods_and_attributes: this required some additions to UserType
to make it usable as a replacement for the test's previous SimpleType:
UserType gained a value mutator, and the `value` property is not
mutable (it was previously readonly). Some overload tests were also
added to better test overload_cast (as described above).
- test_numpy_array: removed the untemplated mutate_data/mutate_data_t:
the templated versions with an empty parameter pack expand to the same
thing.
- test_stl: this was already mostly in the new style; this just tweaks
things a bit, localizing a class, and adding some missing
`// test_whatever` comments.
- test_virtual_functions: like `test_stl`, this was mostly in the new
test style already, but needed some `// test_whatever` comments.
This commit also moves the inherited virtual example code to the end
of the file, after the main set of tests (since it is less important
than the other tests, and rather length); it also got renamed to
`test_inherited_virtuals` (from `test_inheriting_repeat`) because it
tests both inherited virtual approaches, not just the repeat approach.
2017-07-25 20:47:36 +00:00
|
|
|
TEST_SUBMODULE(enums, m) {
|
|
|
|
// test_unscoped_enum
|
2022-02-10 20:17:07 +00:00
|
|
|
enum UnscopedEnum { EOne = 1, ETwo, EThree };
|
2017-11-16 21:24:36 +00:00
|
|
|
py::enum_<UnscopedEnum>(m, "UnscopedEnum", py::arithmetic(), "An unscoped enumeration")
|
|
|
|
.value("EOne", EOne, "Docstring for EOne")
|
|
|
|
.value("ETwo", ETwo, "Docstring for ETwo")
|
2019-09-19 16:23:27 +00:00
|
|
|
.value("EThree", EThree, "Docstring for EThree")
|
2016-08-12 22:57:24 +00:00
|
|
|
.export_values();
|
|
|
|
|
Update all remaining tests to new test styles
This udpates all the remaining tests to the new test suite code and
comment styles started in #898. For the most part, the test coverage
here is unchanged, with a few minor exceptions as noted below.
- test_constants_and_functions: this adds more overload tests with
overloads with different number of arguments for more comprehensive
overload_cast testing. The test style conversion broke the overload
tests under MSVC 2015, prompting the additional tests while looking
for a workaround.
- test_eigen: this dropped the unused functions `get_cm_corners` and
`get_cm_corners_const`--these same tests were duplicates of the same
things provided (and used) via ReturnTester methods.
- test_opaque_types: this test had a hidden dependence on ExampleMandA
which is now fixed by using the global UserType which suffices for the
relevant test.
- test_methods_and_attributes: this required some additions to UserType
to make it usable as a replacement for the test's previous SimpleType:
UserType gained a value mutator, and the `value` property is not
mutable (it was previously readonly). Some overload tests were also
added to better test overload_cast (as described above).
- test_numpy_array: removed the untemplated mutate_data/mutate_data_t:
the templated versions with an empty parameter pack expand to the same
thing.
- test_stl: this was already mostly in the new style; this just tweaks
things a bit, localizing a class, and adding some missing
`// test_whatever` comments.
- test_virtual_functions: like `test_stl`, this was mostly in the new
test style already, but needed some `// test_whatever` comments.
This commit also moves the inherited virtual example code to the end
of the file, after the main set of tests (since it is less important
than the other tests, and rather length); it also got renamed to
`test_inherited_virtuals` (from `test_inheriting_repeat`) because it
tests both inherited virtual approaches, not just the repeat approach.
2017-07-25 20:47:36 +00:00
|
|
|
// test_scoped_enum
|
2022-02-10 20:17:07 +00:00
|
|
|
enum class ScopedEnum { Two = 2, Three };
|
2016-11-17 22:24:47 +00:00
|
|
|
py::enum_<ScopedEnum>(m, "ScopedEnum", py::arithmetic())
|
2016-08-12 22:57:24 +00:00
|
|
|
.value("Two", ScopedEnum::Two)
|
2016-11-17 22:24:47 +00:00
|
|
|
.value("Three", ScopedEnum::Three);
|
2016-08-12 22:57:24 +00:00
|
|
|
|
Update all remaining tests to new test styles
This udpates all the remaining tests to the new test suite code and
comment styles started in #898. For the most part, the test coverage
here is unchanged, with a few minor exceptions as noted below.
- test_constants_and_functions: this adds more overload tests with
overloads with different number of arguments for more comprehensive
overload_cast testing. The test style conversion broke the overload
tests under MSVC 2015, prompting the additional tests while looking
for a workaround.
- test_eigen: this dropped the unused functions `get_cm_corners` and
`get_cm_corners_const`--these same tests were duplicates of the same
things provided (and used) via ReturnTester methods.
- test_opaque_types: this test had a hidden dependence on ExampleMandA
which is now fixed by using the global UserType which suffices for the
relevant test.
- test_methods_and_attributes: this required some additions to UserType
to make it usable as a replacement for the test's previous SimpleType:
UserType gained a value mutator, and the `value` property is not
mutable (it was previously readonly). Some overload tests were also
added to better test overload_cast (as described above).
- test_numpy_array: removed the untemplated mutate_data/mutate_data_t:
the templated versions with an empty parameter pack expand to the same
thing.
- test_stl: this was already mostly in the new style; this just tweaks
things a bit, localizing a class, and adding some missing
`// test_whatever` comments.
- test_virtual_functions: like `test_stl`, this was mostly in the new
test style already, but needed some `// test_whatever` comments.
This commit also moves the inherited virtual example code to the end
of the file, after the main set of tests (since it is less important
than the other tests, and rather length); it also got renamed to
`test_inherited_virtuals` (from `test_inheriting_repeat`) because it
tests both inherited virtual approaches, not just the repeat approach.
2017-07-25 20:47:36 +00:00
|
|
|
m.def("test_scoped_enum", [](ScopedEnum z) {
|
|
|
|
return "ScopedEnum::" + std::string(z == ScopedEnum::Two ? "Two" : "Three");
|
|
|
|
});
|
|
|
|
|
|
|
|
// test_binary_operators
|
2022-02-10 20:17:07 +00:00
|
|
|
enum Flags { Read = 4, Write = 2, Execute = 1 };
|
2016-11-17 22:24:47 +00:00
|
|
|
py::enum_<Flags>(m, "Flags", py::arithmetic())
|
2016-11-16 16:28:11 +00:00
|
|
|
.value("Read", Flags::Read)
|
|
|
|
.value("Write", Flags::Write)
|
|
|
|
.value("Execute", Flags::Execute)
|
|
|
|
.export_values();
|
|
|
|
|
Update all remaining tests to new test styles
This udpates all the remaining tests to the new test suite code and
comment styles started in #898. For the most part, the test coverage
here is unchanged, with a few minor exceptions as noted below.
- test_constants_and_functions: this adds more overload tests with
overloads with different number of arguments for more comprehensive
overload_cast testing. The test style conversion broke the overload
tests under MSVC 2015, prompting the additional tests while looking
for a workaround.
- test_eigen: this dropped the unused functions `get_cm_corners` and
`get_cm_corners_const`--these same tests were duplicates of the same
things provided (and used) via ReturnTester methods.
- test_opaque_types: this test had a hidden dependence on ExampleMandA
which is now fixed by using the global UserType which suffices for the
relevant test.
- test_methods_and_attributes: this required some additions to UserType
to make it usable as a replacement for the test's previous SimpleType:
UserType gained a value mutator, and the `value` property is not
mutable (it was previously readonly). Some overload tests were also
added to better test overload_cast (as described above).
- test_numpy_array: removed the untemplated mutate_data/mutate_data_t:
the templated versions with an empty parameter pack expand to the same
thing.
- test_stl: this was already mostly in the new style; this just tweaks
things a bit, localizing a class, and adding some missing
`// test_whatever` comments.
- test_virtual_functions: like `test_stl`, this was mostly in the new
test style already, but needed some `// test_whatever` comments.
This commit also moves the inherited virtual example code to the end
of the file, after the main set of tests (since it is less important
than the other tests, and rather length); it also got renamed to
`test_inherited_virtuals` (from `test_inheriting_repeat`) because it
tests both inherited virtual approaches, not just the repeat approach.
2017-07-25 20:47:36 +00:00
|
|
|
// test_implicit_conversion
|
|
|
|
class ClassWithUnscopedEnum {
|
|
|
|
public:
|
2022-02-10 20:17:07 +00:00
|
|
|
enum EMode { EFirstMode = 1, ESecondMode };
|
|
|
|
|
|
|
|
static EMode test_function(EMode mode) { return mode; }
|
Update all remaining tests to new test styles
This udpates all the remaining tests to the new test suite code and
comment styles started in #898. For the most part, the test coverage
here is unchanged, with a few minor exceptions as noted below.
- test_constants_and_functions: this adds more overload tests with
overloads with different number of arguments for more comprehensive
overload_cast testing. The test style conversion broke the overload
tests under MSVC 2015, prompting the additional tests while looking
for a workaround.
- test_eigen: this dropped the unused functions `get_cm_corners` and
`get_cm_corners_const`--these same tests were duplicates of the same
things provided (and used) via ReturnTester methods.
- test_opaque_types: this test had a hidden dependence on ExampleMandA
which is now fixed by using the global UserType which suffices for the
relevant test.
- test_methods_and_attributes: this required some additions to UserType
to make it usable as a replacement for the test's previous SimpleType:
UserType gained a value mutator, and the `value` property is not
mutable (it was previously readonly). Some overload tests were also
added to better test overload_cast (as described above).
- test_numpy_array: removed the untemplated mutate_data/mutate_data_t:
the templated versions with an empty parameter pack expand to the same
thing.
- test_stl: this was already mostly in the new style; this just tweaks
things a bit, localizing a class, and adding some missing
`// test_whatever` comments.
- test_virtual_functions: like `test_stl`, this was mostly in the new
test style already, but needed some `// test_whatever` comments.
This commit also moves the inherited virtual example code to the end
of the file, after the main set of tests (since it is less important
than the other tests, and rather length); it also got renamed to
`test_inherited_virtuals` (from `test_inheriting_repeat`) because it
tests both inherited virtual approaches, not just the repeat approach.
2017-07-25 20:47:36 +00:00
|
|
|
};
|
2016-08-12 22:57:24 +00:00
|
|
|
py::class_<ClassWithUnscopedEnum> exenum_class(m, "ClassWithUnscopedEnum");
|
|
|
|
exenum_class.def_static("test_function", &ClassWithUnscopedEnum::test_function);
|
|
|
|
py::enum_<ClassWithUnscopedEnum::EMode>(exenum_class, "EMode")
|
|
|
|
.value("EFirstMode", ClassWithUnscopedEnum::EFirstMode)
|
|
|
|
.value("ESecondMode", ClassWithUnscopedEnum::ESecondMode)
|
|
|
|
.export_values();
|
2017-04-28 12:46:52 +00:00
|
|
|
|
Update all remaining tests to new test styles
This udpates all the remaining tests to the new test suite code and
comment styles started in #898. For the most part, the test coverage
here is unchanged, with a few minor exceptions as noted below.
- test_constants_and_functions: this adds more overload tests with
overloads with different number of arguments for more comprehensive
overload_cast testing. The test style conversion broke the overload
tests under MSVC 2015, prompting the additional tests while looking
for a workaround.
- test_eigen: this dropped the unused functions `get_cm_corners` and
`get_cm_corners_const`--these same tests were duplicates of the same
things provided (and used) via ReturnTester methods.
- test_opaque_types: this test had a hidden dependence on ExampleMandA
which is now fixed by using the global UserType which suffices for the
relevant test.
- test_methods_and_attributes: this required some additions to UserType
to make it usable as a replacement for the test's previous SimpleType:
UserType gained a value mutator, and the `value` property is not
mutable (it was previously readonly). Some overload tests were also
added to better test overload_cast (as described above).
- test_numpy_array: removed the untemplated mutate_data/mutate_data_t:
the templated versions with an empty parameter pack expand to the same
thing.
- test_stl: this was already mostly in the new style; this just tweaks
things a bit, localizing a class, and adding some missing
`// test_whatever` comments.
- test_virtual_functions: like `test_stl`, this was mostly in the new
test style already, but needed some `// test_whatever` comments.
This commit also moves the inherited virtual example code to the end
of the file, after the main set of tests (since it is less important
than the other tests, and rather length); it also got renamed to
`test_inherited_virtuals` (from `test_inheriting_repeat`) because it
tests both inherited virtual approaches, not just the repeat approach.
2017-07-25 20:47:36 +00:00
|
|
|
// test_enum_to_int
|
2022-02-10 20:17:07 +00:00
|
|
|
m.def("test_enum_to_int", [](int) {});
|
|
|
|
m.def("test_enum_to_uint", [](uint32_t) {});
|
|
|
|
m.def("test_enum_to_long_long", [](long long) {});
|
2018-09-11 08:59:56 +00:00
|
|
|
|
2024-02-14 18:46:30 +00:00
|
|
|
// Trying to pass an enum to a function that accepts a float should
|
|
|
|
// trigger a type error
|
|
|
|
m.def("test_enum_to_float", [](double) {});
|
|
|
|
|
|
|
|
// When performing overload resolution, calling f(0, ScopedEnum.TWO)
|
|
|
|
// should select f(float, ScopedEnum), NOT f(float, float)
|
|
|
|
m.def("test_enum_overload_resolution", [](double, double) { return "f(float, float)"; });
|
|
|
|
m.def("test_enum_overload_resolution",
|
|
|
|
[](double, ScopedEnum) { return "f(float, ScopedEnum)"; });
|
|
|
|
|
2018-09-11 08:59:56 +00:00
|
|
|
// test_duplicate_enum_name
|
2022-02-10 20:17:07 +00:00
|
|
|
enum SimpleEnum { ONE, TWO, THREE };
|
2018-09-11 08:59:56 +00:00
|
|
|
|
|
|
|
m.def("register_bad_enum", [m]() {
|
|
|
|
py::enum_<SimpleEnum>(m, "SimpleEnum")
|
2022-02-10 20:17:07 +00:00
|
|
|
.value("ONE", SimpleEnum::ONE) // NOTE: all value function calls are called with the
|
|
|
|
// same first parameter value
|
2018-09-11 08:59:56 +00:00
|
|
|
.value("ONE", SimpleEnum::TWO)
|
|
|
|
.value("ONE", SimpleEnum::THREE)
|
|
|
|
.export_values();
|
|
|
|
});
|
2021-08-26 21:34:24 +00:00
|
|
|
|
|
|
|
// test_enum_scalar
|
|
|
|
enum UnscopedUCharEnum : unsigned char {};
|
|
|
|
enum class ScopedShortEnum : short {};
|
|
|
|
enum class ScopedLongEnum : long {};
|
|
|
|
enum UnscopedUInt64Enum : std::uint64_t {};
|
2022-02-10 20:17:07 +00:00
|
|
|
static_assert(
|
|
|
|
py::detail::all_of<
|
|
|
|
std::is_same<py::enum_<UnscopedUCharEnum>::Scalar, unsigned char>,
|
|
|
|
std::is_same<py::enum_<ScopedShortEnum>::Scalar, short>,
|
|
|
|
std::is_same<py::enum_<ScopedLongEnum>::Scalar, long>,
|
|
|
|
std::is_same<py::enum_<UnscopedUInt64Enum>::Scalar, std::uint64_t>>::value,
|
|
|
|
"Error during the deduction of enum's scalar type with normal integer underlying");
|
2021-08-26 21:34:24 +00:00
|
|
|
|
|
|
|
// test_enum_scalar_with_char_underlying
|
2022-02-10 20:17:07 +00:00
|
|
|
enum class ScopedCharEnum : char { Zero, Positive };
|
|
|
|
enum class ScopedWCharEnum : wchar_t { Zero, Positive };
|
2021-08-26 21:34:24 +00:00
|
|
|
enum class ScopedChar32Enum : char32_t { Zero, Positive };
|
|
|
|
enum class ScopedChar16Enum : char16_t { Zero, Positive };
|
|
|
|
|
|
|
|
// test the scalar of char type enums according to chapter 'Character types'
|
|
|
|
// from https://en.cppreference.com/w/cpp/language/types
|
|
|
|
static_assert(
|
2022-02-10 20:17:07 +00:00
|
|
|
py::detail::any_of<
|
|
|
|
std::is_same<py::enum_<ScopedCharEnum>::Scalar, signed char>, // e.g. gcc on x86
|
|
|
|
std::is_same<py::enum_<ScopedCharEnum>::Scalar, unsigned char> // e.g. arm linux
|
|
|
|
>::value,
|
|
|
|
"char should be cast to either signed char or unsigned char");
|
|
|
|
static_assert(sizeof(py::enum_<ScopedWCharEnum>::Scalar) == 2
|
|
|
|
|| sizeof(py::enum_<ScopedWCharEnum>::Scalar) == 4,
|
|
|
|
"wchar_t should be either 16 bits (Windows) or 32 (everywhere else)");
|
|
|
|
static_assert(
|
|
|
|
py::detail::all_of<
|
|
|
|
std::is_same<py::enum_<ScopedChar32Enum>::Scalar, std::uint_least32_t>,
|
|
|
|
std::is_same<py::enum_<ScopedChar16Enum>::Scalar, std::uint_least16_t>>::value,
|
|
|
|
"char32_t, char16_t (and char8_t)'s size, signedness, and alignment is determined");
|
2021-08-26 21:34:24 +00:00
|
|
|
#if defined(PYBIND11_HAS_U8STRING)
|
|
|
|
enum class ScopedChar8Enum : char8_t { Zero, Positive };
|
|
|
|
static_assert(std::is_same<py::enum_<ScopedChar8Enum>::Scalar, unsigned char>::value);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// test_char_underlying_enum
|
|
|
|
py::enum_<ScopedCharEnum>(m, "ScopedCharEnum")
|
|
|
|
.value("Zero", ScopedCharEnum::Zero)
|
|
|
|
.value("Positive", ScopedCharEnum::Positive);
|
|
|
|
py::enum_<ScopedWCharEnum>(m, "ScopedWCharEnum")
|
|
|
|
.value("Zero", ScopedWCharEnum::Zero)
|
|
|
|
.value("Positive", ScopedWCharEnum::Positive);
|
|
|
|
py::enum_<ScopedChar32Enum>(m, "ScopedChar32Enum")
|
|
|
|
.value("Zero", ScopedChar32Enum::Zero)
|
|
|
|
.value("Positive", ScopedChar32Enum::Positive);
|
|
|
|
py::enum_<ScopedChar16Enum>(m, "ScopedChar16Enum")
|
|
|
|
.value("Zero", ScopedChar16Enum::Zero)
|
|
|
|
.value("Positive", ScopedChar16Enum::Positive);
|
|
|
|
|
|
|
|
// test_bool_underlying_enum
|
|
|
|
enum class ScopedBoolEnum : bool { FALSE, TRUE };
|
|
|
|
|
|
|
|
// bool is unsigned (std::is_signed returns false) and 1-byte long, so represented with u8
|
|
|
|
static_assert(std::is_same<py::enum_<ScopedBoolEnum>::Scalar, std::uint8_t>::value, "");
|
|
|
|
|
|
|
|
py::enum_<ScopedBoolEnum>(m, "ScopedBoolEnum")
|
|
|
|
.value("FALSE", ScopedBoolEnum::FALSE)
|
|
|
|
.value("TRUE", ScopedBoolEnum::TRUE);
|
Update all remaining tests to new test styles
This udpates all the remaining tests to the new test suite code and
comment styles started in #898. For the most part, the test coverage
here is unchanged, with a few minor exceptions as noted below.
- test_constants_and_functions: this adds more overload tests with
overloads with different number of arguments for more comprehensive
overload_cast testing. The test style conversion broke the overload
tests under MSVC 2015, prompting the additional tests while looking
for a workaround.
- test_eigen: this dropped the unused functions `get_cm_corners` and
`get_cm_corners_const`--these same tests were duplicates of the same
things provided (and used) via ReturnTester methods.
- test_opaque_types: this test had a hidden dependence on ExampleMandA
which is now fixed by using the global UserType which suffices for the
relevant test.
- test_methods_and_attributes: this required some additions to UserType
to make it usable as a replacement for the test's previous SimpleType:
UserType gained a value mutator, and the `value` property is not
mutable (it was previously readonly). Some overload tests were also
added to better test overload_cast (as described above).
- test_numpy_array: removed the untemplated mutate_data/mutate_data_t:
the templated versions with an empty parameter pack expand to the same
thing.
- test_stl: this was already mostly in the new style; this just tweaks
things a bit, localizing a class, and adding some missing
`// test_whatever` comments.
- test_virtual_functions: like `test_stl`, this was mostly in the new
test style already, but needed some `// test_whatever` comments.
This commit also moves the inherited virtual example code to the end
of the file, after the main set of tests (since it is less important
than the other tests, and rather length); it also got renamed to
`test_inherited_virtuals` (from `test_inheriting_repeat`) because it
tests both inherited virtual approaches, not just the repeat approach.
2017-07-25 20:47:36 +00:00
|
|
|
}
|