Merge pull request #9 from BlackMATov/dev

Dev
This commit is contained in:
2020-12-19 04:54:47 +07:00
committed by GitHub
15 changed files with 7757 additions and 350 deletions

View File

@@ -1,4 +0,0 @@
ignore:
- "catch.hpp"
- "catch_main.hpp"
- "*_tests.cpp"

View File

@@ -1,32 +1,12 @@
git:
depth: false
quiet: true
language: cpp language: cpp
matrix: jobs:
include: include:
#
# windows (MSVC 2017)
#
- os: windows
stage: windows
name: debug, MSVC 2017, x86
script: ./scripts/build_debug_x86.bat
- os: windows
stage: windows
name: release, MSVC 2017, x86
script: ./scripts/build_release_x86.bat
- os: windows
stage: windows
name: debug, MSVC 2017, x64
script: ./scripts/build_debug_x64.bat
- os: windows
stage: windows
name: release, MSVC 2017, x64
script: ./scripts/build_release_x64.bat
# #
# linux (g++-7) # linux (g++-7)
# #
@@ -75,7 +55,7 @@ matrix:
dist: xenial dist: xenial
stage: linux stage: linux
name: debug, clang++-5.0 name: debug, clang++-5.0
addons: { apt: { sources: ["ubuntu-toolchain-r-test", "llvm-toolchain-xenial-5.0"], packages: ["g++-7", "clang-5.0"] } } addons: { apt: { sources: ["ubuntu-toolchain-r-test"], packages: ["g++-7", "clang-5.0"] } }
env: CC=clang-5.0 CXX=clang++-5.0 env: CC=clang-5.0 CXX=clang++-5.0
script: ./scripts/build_debug.sh script: ./scripts/build_debug.sh
@@ -83,7 +63,7 @@ matrix:
dist: xenial dist: xenial
stage: linux stage: linux
name: release, clang++-5.0 name: release, clang++-5.0
addons: { apt: { sources: ["ubuntu-toolchain-r-test", "llvm-toolchain-xenial-5.0"], packages: ["g++-7", "clang-5.0"] } } addons: { apt: { sources: ["ubuntu-toolchain-r-test"], packages: ["g++-7", "clang-5.0"] } }
env: CC=clang-5.0 CXX=clang++-5.0 env: CC=clang-5.0 CXX=clang++-5.0
script: ./scripts/build_release.sh script: ./scripts/build_release.sh
@@ -95,7 +75,7 @@ matrix:
dist: xenial dist: xenial
stage: linux stage: linux
name: debug, clang++-6.0 name: debug, clang++-6.0
addons: { apt: { sources: ["ubuntu-toolchain-r-test", "llvm-toolchain-xenial-6.0"], packages: ["g++-7", "clang-6.0"] } } addons: { apt: { sources: ["ubuntu-toolchain-r-test"], packages: ["g++-7", "clang-6.0"] } }
env: CC=clang-6.0 CXX=clang++-6.0 env: CC=clang-6.0 CXX=clang++-6.0
script: ./scripts/build_debug.sh script: ./scripts/build_debug.sh
@@ -103,7 +83,7 @@ matrix:
dist: xenial dist: xenial
stage: linux stage: linux
name: release, clang++-6.0 name: release, clang++-6.0
addons: { apt: { sources: ["ubuntu-toolchain-r-test", "llvm-toolchain-xenial-6.0"], packages: ["g++-7", "clang-6.0"] } } addons: { apt: { sources: ["ubuntu-toolchain-r-test"], packages: ["g++-7", "clang-6.0"] } }
env: CC=clang-6.0 CXX=clang++-6.0 env: CC=clang-6.0 CXX=clang++-6.0
script: ./scripts/build_release.sh script: ./scripts/build_release.sh
@@ -149,11 +129,3 @@ matrix:
name: coverage, xcode10 name: coverage, xcode10
addons: { homebrew: { packages: ["lcov"], update: true } } addons: { homebrew: { packages: ["lcov"], update: true } }
script: ./scripts/upload_coverage.sh script: ./scripts/upload_coverage.sh
before_install:
- if [ "$TRAVIS_OS_NAME" == 'linux' ]; then
mkdir $HOME/cmake;
export PATH="$HOME/cmake/bin:$PATH";
travis_retry wget -q https://cmake.org/files/v3.11/cmake-3.11.4-Linux-x86_64.sh;
sh cmake-3.11.4-Linux-x86_64.sh --prefix=$HOME/cmake --exclude-subdir --skip-license;
fi

369
README.md
View File

@@ -25,6 +25,19 @@
[enum]: https://github.com/BlackMATov/enum.hpp [enum]: https://github.com/BlackMATov/enum.hpp
---
## Content
- [Requirements](#Requirements)
- [Installation](#Installation)
- [Examples](#Examples)
- [API](#API)
- [Alternatives](#Alternatives)
- [License](#License-(MIT))
---
## Requirements ## Requirements
- [gcc](https://www.gnu.org/software/gcc/) **>= 7** - [gcc](https://www.gnu.org/software/gcc/) **>= 7**
@@ -48,15 +61,26 @@ target_link_libraries(your_project_target enum.hpp)
## Examples ## Examples
### Declarations - `enum.hpp`
- [Enum declarations](#Enum-declarations)
- [Traits using](#Traits-using)
- [Generic context](#Generic-context)
- [Adapting external enums](#Adapting-external-enums)
- `enum_bitflags.hpp`
- [Enum bitflags using](#Enum-bitflags-using)
- [Additional bitflags functions](#Additional-bitflags-functions)
### Enum declarations
```cpp ```cpp
// declaration of unscoped enumeration `debug_level` with traits
ENUM_HPP_DECL(debug_level, int, ENUM_HPP_DECL(debug_level, int,
(level_info) (level_info)
(level_warning) (level_warning)
(level_error)) (level_error))
// equivalent to: /* equivalent to:
enum debug_level : int { enum debug_level : int {
level_info, level_info,
@@ -65,18 +89,20 @@ enum debug_level : int {
}; };
struct debug_level_traits { struct debug_level_traits {
/*...*/ ...
}; };*/
``` ```
```cpp ```cpp
// declaration of scoped enumeration `color` with traits
ENUM_HPP_CLASS_DECL(color, unsigned, ENUM_HPP_CLASS_DECL(color, unsigned,
(red = 1 << 0) (red = 1 << 0)
(green = 1 << 1) (green = 1 << 1)
(blue = 1 << 2) (blue = 1 << 2)
(white = red | green | blue)) (white = red | green | blue))
// equivalent to: /* equivalent to:
enum class color : unsigned { enum class color : unsigned {
red = 1 << 0, red = 1 << 0,
@@ -86,8 +112,8 @@ enum class color : unsigned {
}; };
struct color_traits { struct color_traits {
/*...*/ ...
}; };*/
``` ```
### Traits using ### Traits using
@@ -126,7 +152,7 @@ int main() {
static_assert(color_traits::from_index(42) == std::nullopt); static_assert(color_traits::from_index(42) == std::nullopt);
// names // names
for ( auto n : color_traits::names ) { for ( std::string_view n : color_traits::names ) {
std::cout << n << ","; std::cout << n << ",";
} // stdout: red,green,blue, } // stdout: red,green,blue,
@@ -190,61 +216,152 @@ int main() {
} }
``` ```
### Enum bitflags using
```cpp
#include <enum.hpp/enum_bitflags.hpp>
namespace
{
enum class perms : unsigned {
execute = 1 << 0,
write = 1 << 1,
read = 1 << 2,
};
// declares operators for perms enum (~, |, &, ^)
ENUM_HPP_OPERATORS_DECL(perms)
}
int main() {
namespace bf = enum_hpp::bitflags;
// every enum operator returns bitflags<enum> value
bf::bitflags flags = perms::read | perms::write;
// the bitflags class has some member functions for working with bit flags
if ( flags.has(perms::write) ) {
flags.clear(perms::write);
}
// you can passing other the same type bitflags to these functions
flags.set(perms::write | perms::execute);
// or using bit flags with the usual bit operations but type safe
if ( flags & perms::execute ) {
flags &= ~perms::execute; // flags.toggle(perms::execute);
}
// or compare them, why not?
if ( flags == (perms::read | perms::write) ) {
return 0;
}
return 1;
}
```
### Additional bitflags functions
```cpp
#include <enum.hpp/enum_bitflags.hpp>
namespace
{
enum class perms : unsigned {
execute = 1 << 0,
write = 1 << 1,
read = 1 << 2,
};
// declares operators for perms enum (~, |, &, ^)
ENUM_HPP_OPERATORS_DECL(perms)
}
int main() {
namespace bf = enum_hpp::bitflags;
bf::bitflags<perms> flags = perms::read | perms::write;
// bitflags namespace has many free functions
// that can accept both enumerations and bit flags
if ( bf::any_of(flags, perms::write | perms::execute) ) {
// it's writable or executable
}
if ( bf::any_except(flags, perms::write | perms::execute) ) {
// and something else :-)
}
}
```
## API ## API
- `enum.hpp`
- [Enum traits](#Enum-traits)
- [Enum generic functions](#Enum-generic-functions)
- `enum_bitflags.hpp`
- [Enum bitflags](#Enum-bitflags)
- [Enum operators](#Enum-operators)
- [Enum bitflags operators](#Enum-bitflags-operators)
- [Enum bitflags functions](#Enum-bitflags-functions)
### Enum traits ### Enum traits
```cpp ```cpp
// declare enum // declares unscoped enumeration
ENUM_HPP_DECL( ENUM_HPP_DECL(
/*enum_name*/, /*enum_name*/,
/*underlying_type*/, /*underlying_type*/,
/*fields*/) /*fields*/)
// declare enum class // declares scoped enumeration
ENUM_HPP_CLASS_DECL( ENUM_HPP_CLASS_DECL(
/*enum_name*/, /*enum_name*/,
/*underlying_type*/, /*underlying_type*/,
/*fields*/) /*fields*/)
// declare only traits // declares only traits for external enumerations
ENUM_HPP_TRAITS_DECL( ENUM_HPP_TRAITS_DECL(
/*enum_name*/, /*enum_name*/,
/*fields*/) /*fields*/)
// declared enumeration traits
struct /*enum_name*/_traits { struct /*enum_name*/_traits {
using enum_type = /*enum_name*/;
using underlying_type = /*underlying_type*/; using underlying_type = /*underlying_type*/;
static constexpr std::size_t size = /*field_count*/; static constexpr std::size_t size = /*field_count*/;
static constexpr const std::array</*enum_name*/, /*field_count*/ > values = { static constexpr const std::array<enum_type, size> values = {
/*enum values*/ /*enum values*/
}; };
static constexpr const std::array<std::string_view, /*field_count*/> names = { static constexpr const std::array<std::string_view, size> names = {
/*enum names*/ /*enum value names*/
}; };
static constexpr /*underlying_type*/ to_underlying(/*enum_name*/ e) noexcept; static constexpr underlying_type to_underlying(enum_type e) noexcept;
static constexpr std::optional<std::string_view> to_string(/*enum_name*/ e) noexcept; static constexpr std::optional<std::string_view> to_string(enum_type e) noexcept;
static constexpr std::string_view to_string_or_empty(/*enum_name*/ e) noexcept; static constexpr std::string_view to_string_or_empty(enum_type e) noexcept;
static std::string_view to_string_or_throw(/*enum_name*/ e); static std::string_view to_string_or_throw(enum_type e);
static constexpr std::optional</*enum_name*/> from_string(std::string_view name) noexcept; static constexpr std::optional<enum_type> from_string(std::string_view name) noexcept;
static constexpr /*enum_name*/ from_string_or_default(std::string_view name, /*enum_name*/ def) noexcept; static constexpr enum_type from_string_or_default(std::string_view name, enum_type def) noexcept;
static /*enum_name*/ from_string_or_throw(std::string_view name); static enum_type from_string_or_throw(std::string_view name);
static constexpr std::optional<std::size_t> to_index(/*enum_name*/ e) noexcept; static constexpr std::optional<std::size_t> to_index(enum_type e) noexcept;
static constexpr std::size_t to_index_or_invalid(/*enum_name*/ e) noexcept; static constexpr std::size_t to_index_or_invalid(enum_type e) noexcept;
static std::size_t to_index_or_throw(/*enum_name*/ e); static std::size_t to_index_or_throw(enum_type e);
static constexpr std::optional</*enum_name*/> from_index(std::size_t index) noexcept; static constexpr std::optional<enum_type> from_index(std::size_t index) noexcept;
static constexpr /*enum_name*/ from_index_or_default(std::size_t index, /*enum_name*/ def) noexcept; static constexpr enum_type from_index_or_default(std::size_t index, enum_type def) noexcept;
static /*enum_name*/ from_index_or_throw(std::size_t index); static enum_type from_index_or_throw(std::size_t index);
}; };
``` ```
### Generic functions ### Enum generic functions
```cpp ```cpp
// should be in enum namespace // should be in enum namespace
@@ -308,6 +425,202 @@ namespace enum_hpp
} }
``` ```
### Enum bitflags
```cpp
namespace enum_hpp::bitflags
{
template < typename Enum >
class bitflags final {
public:
using enum_type = Enum;
using underlying_type = std::underlying_type_t<Enum>;
bitflags() = default;
bitflags(const bitflags&) = default;
bitflags& operator=(const bitflags&) = default;
constexpr bitflags(enum_type flags);
constexpr explicit bitflags(underlying_type flags);
constexpr void swap(bitflags& other) noexcept;
constexpr explicit operator bool() const noexcept;
constexpr underlying_type as_raw() const noexcept;
constexpr enum_type as_enum() const noexcept;
constexpr bool has(bitflags flags) const noexcept;
constexpr bitflags& set(bitflags flags) noexcept;
constexpr bitflags& toggle(bitflags flags) noexcept;
constexpr bitflags& clear(bitflags flags) noexcept;
};
template < typename Enum >
constexpr void swap(bitflags<Enum>& l, bitflags<Enum>& r) noexcept;
}
namespace std
{
template < typename Enum >
struct hash<enum_hpp::bitflags::bitflags<Enum>> {
size_t operator()(enum_hpp::bitflags::bitflags<Enum> bf) const noexcept;
};
}
```
### Enum operators
```cpp
// declares enumeration operators
ENUM_HPP_OPERATORS_DECL(/*enum_name*/)
// declared enumeration operators
constexpr bitflags<Enum> operator ~ (/*enum_name*/ l) noexcept;
constexpr bitflags<Enum> operator | (/*enum_name*/ l, /*enum_name*/ r) noexcept;
constexpr bitflags<Enum> operator & (/*enum_name*/ l, /*enum_name*/ r) noexcept;
constexpr bitflags<Enum> operator ^ (/*enum_name*/ l, /*enum_name*/ r) noexcept;
```
### Enum bitflags operators
```cpp
namespace enum_hpp::bitflags
{
template < typename Enum >
constexpr bool operator < (Enum l, bitflags<Enum> r) noexcept;
template < typename Enum >
constexpr bool operator < (bitflags<Enum> l, Enum r) noexcept;
template < typename Enum >
constexpr bool operator < (std::underlying_type_t<Enum> l, bitflags<Enum> r) noexcept;
template < typename Enum >
constexpr bool operator < (bitflags<Enum> l, std::underlying_type_t<Enum> r) noexcept;
template < typename Enum >
constexpr bool operator < (bitflags<Enum> l, bitflags<Enum> r) noexcept;
// and also for other comparison operators (<, >, <=, >=, ==, !=)
}
namespace enum_hpp::bitflags
{
template < typename Enum >
constexpr bitflags<Enum> operator ~ (bitflags<Enum> l) noexcept;
template < typename Enum >
constexpr bitflags<Enum> operator | (Enum l, bitflags<Enum> r) noexcept;
template < typename Enum >
constexpr bitflags<Enum> operator | (bitflags<Enum> l, Enum r) noexcept;
template < typename Enum >
constexpr bitflags<Enum> operator | (bitflags<Enum> l, bitflags<Enum> r) noexcept;
template < typename Enum >
constexpr bitflags<Enum>& operator |= (bitflags<Enum>& l, Enum r) noexcept;
template < typename Enum >
constexpr bitflags<Enum>& operator |= (bitflags<Enum>& l, bitflags<Enum> r) noexcept;
// and also for other bitwise logic operators (|, |=, &, &=, ^, ^=)
}
```
### Enum bitflags functions
```cpp
namespace enum_hpp::bitflags
{
// any
template < enum Enum >
constexpr bool any(Enum flags) noexcept;
template < typename Enum >
constexpr bool any(bitflags<Enum> flags) noexcept;
// none
template < enum Enum >
constexpr bool none(Enum flags) noexcept;
template < typename Enum >
constexpr bool none(bitflags<Enum> flags) noexcept;
// all_of
template < enum Enum >
constexpr bool all_of(Enum flags, Enum mask) noexcept;
template < typename Enum >
constexpr bool all_of(Enum flags, bitflags<Enum> mask) noexcept;
template < typename Enum >
constexpr bool all_of(bitflags<Enum> flags, Enum mask) noexcept;
template < typename Enum >
constexpr bool all_of(bitflags<Enum> flags, bitflags<Enum> mask) noexcept;
// any_of
template < enum Enum >
constexpr bool any_of(Enum flags, Enum mask) noexcept;
template < typename Enum >
constexpr bool any_of(Enum flags, bitflags<Enum> mask) noexcept;
template < typename Enum >
constexpr bool any_of(bitflags<Enum> flags, Enum mask) noexcept;
template < typename Enum >
constexpr bool any_of(bitflags<Enum> flags, bitflags<Enum> mask) noexcept;
// none_of
template < enum Enum >
constexpr bool none_of(Enum flags, Enum mask) noexcept;
template < typename Enum >
constexpr bool none_of(Enum flags, bitflags<Enum> mask) noexcept;
template < typename Enum >
constexpr bool none_of(bitflags<Enum> flags, Enum mask) noexcept;
template < typename Enum >
constexpr bool none_of(bitflags<Enum> flags, bitflags<Enum> mask) noexcept;
// any_except
template < enum Enum >
constexpr bool any_except(Enum flags, Enum mask) noexcept;
template < typename Enum >
constexpr bool any_except(Enum flags, bitflags<Enum> mask) noexcept;
template < typename Enum >
constexpr bool any_except(bitflags<Enum> flags, Enum mask) noexcept;
template < typename Enum >
constexpr bool any_except(bitflags<Enum> flags, bitflags<Enum> mask) noexcept;
// none_except
template < enum Enum >
constexpr bool none_except(Enum flags, Enum mask) noexcept;
template < typename Enum >
constexpr bool none_except(Enum flags, bitflags<Enum> mask) noexcept;
template < typename Enum >
constexpr bool none_except(bitflags<Enum> flags, Enum mask) noexcept;
template < typename Enum >
constexpr bool none_except(bitflags<Enum> flags, bitflags<Enum> mask) noexcept;
}
```
## Alternatives ## Alternatives
[Better Enums](https://github.com/aantron/better-enums) [Better Enums](https://github.com/aantron/better-enums)

View File

@@ -7,11 +7,17 @@
#pragma once #pragma once
#include <array> #include <array>
#include <utility> #include <cstddef>
#include <cstdlib>
#include <optional> #include <optional>
#include <stdexcept> #include <stdexcept>
#include <string_view> #include <string_view>
#include <type_traits> #include <type_traits>
#include <utility>
#if !defined(__cpp_exceptions) && !defined(__EXCEPTIONS) && !defined(_CPPUNWIND)
# define ENUM_HPP_NO_EXCEPTIONS
#endif
namespace enum_hpp namespace enum_hpp
{ {
@@ -116,6 +122,15 @@ namespace enum_hpp
namespace enum_hpp::detail namespace enum_hpp::detail
{ {
inline void throw_exception_with [[noreturn]] (const char* what) {
#ifndef ENUM_HPP_NO_EXCEPTIONS
throw ::enum_hpp::exception(what);
#else
(void)what;
std::abort();
#endif
}
template < typename Enum > template < typename Enum >
struct ignore_assign final { struct ignore_assign final {
Enum value; Enum value;
@@ -233,10 +248,11 @@ namespace enum_hpp::detail
ENUM_HPP_GENERATE_FIELDS(Fields)\ ENUM_HPP_GENERATE_FIELDS(Fields)\
};\ };\
public:\ public:\
using underlying_type = std::underlying_type_t<Enum>;\ using enum_type = Enum;\
using underlying_type = std::underlying_type_t<enum_type>;\
static constexpr std::size_t size = ENUM_HPP_PP_SEQ_SIZE(Fields);\ static constexpr std::size_t size = ENUM_HPP_PP_SEQ_SIZE(Fields);\
\ \
static constexpr const std::array<Enum, size> values = {\ static constexpr const std::array<enum_type, size> values = {\
{ ENUM_HPP_GENERATE_VALUES(Enum, Fields) }\ { ENUM_HPP_GENERATE_VALUES(Enum, Fields) }\
};\ };\
\ \
@@ -244,30 +260,30 @@ namespace enum_hpp::detail
{ ENUM_HPP_GENERATE_NAMES(Fields) }\ { ENUM_HPP_GENERATE_NAMES(Fields) }\
};\ };\
public:\ public:\
static constexpr underlying_type to_underlying(Enum e) noexcept {\ static constexpr underlying_type to_underlying(enum_type e) noexcept {\
return static_cast<underlying_type>(e);\ return static_cast<underlying_type>(e);\
}\ }\
\ \
static constexpr std::optional<std::string_view> to_string(Enum e) noexcept {\ static constexpr std::optional<std::string_view> to_string(enum_type e) noexcept {\
switch ( e ) {\ switch ( e ) {\
ENUM_HPP_GENERATE_VALUE_TO_NAME_CASES(Enum, Fields)\ ENUM_HPP_GENERATE_VALUE_TO_NAME_CASES(Enum, Fields)\
default: return std::nullopt;\ default: return std::nullopt;\
}\ }\
}\ }\
static constexpr std::string_view to_string_or_empty(Enum e) noexcept {\ static constexpr std::string_view to_string_or_empty(enum_type e) noexcept {\
if ( auto s = to_string(e) ) {\ if ( auto s = to_string(e) ) {\
return *s;\ return *s;\
}\ }\
return ::enum_hpp::empty_string;\ return ::enum_hpp::empty_string;\
}\ }\
static std::string_view to_string_or_throw(Enum e) {\ static std::string_view to_string_or_throw(enum_type e) {\
if ( auto s = to_string(e) ) {\ if ( auto s = to_string(e) ) {\
return *s;\ return *s;\
}\ }\
throw ::enum_hpp::exception(#Enum "_traits::to_string_or_throw(): invalid argument");\ ::enum_hpp::detail::throw_exception_with(#Enum "_traits::to_string_or_throw(): invalid argument");\
}\ }\
\ \
static constexpr std::optional<Enum> from_string(std::string_view name) noexcept {\ static constexpr std::optional<enum_type> from_string(std::string_view name) noexcept {\
for ( std::size_t i = 0; i < size; ++i) {\ for ( std::size_t i = 0; i < size; ++i) {\
if ( name == names[i] ) {\ if ( name == names[i] ) {\
return values[i];\ return values[i];\
@@ -275,56 +291,56 @@ namespace enum_hpp::detail
}\ }\
return std::nullopt;\ return std::nullopt;\
}\ }\
static constexpr Enum from_string_or_default(std::string_view name, Enum def) noexcept {\ static constexpr enum_type from_string_or_default(std::string_view name, enum_type def) noexcept {\
if ( auto e = from_string(name) ) {\ if ( auto e = from_string(name) ) {\
return *e;\ return *e;\
}\ }\
return def;\ return def;\
}\ }\
static Enum from_string_or_throw(std::string_view name) {\ static enum_type from_string_or_throw(std::string_view name) {\
if ( auto e = from_string(name) ) {\ if ( auto e = from_string(name) ) {\
return *e;\ return *e;\
}\ }\
throw ::enum_hpp::exception(#Enum "_traits::from_string_or_throw(): invalid argument");\ ::enum_hpp::detail::throw_exception_with(#Enum "_traits::from_string_or_throw(): invalid argument");\
}\ }\
\ \
static constexpr std::optional<std::size_t> to_index(Enum e) noexcept {\ static constexpr std::optional<std::size_t> to_index(enum_type e) noexcept {\
switch ( e ) {\ switch ( e ) {\
ENUM_HPP_GENERATE_VALUE_TO_INDEX_CASES(Enum, Fields)\ ENUM_HPP_GENERATE_VALUE_TO_INDEX_CASES(Enum, Fields)\
default: return std::nullopt;\ default: return std::nullopt;\
}\ }\
}\ }\
\ \
static constexpr std::size_t to_index_or_invalid(Enum e) noexcept {\ static constexpr std::size_t to_index_or_invalid(enum_type e) noexcept {\
if ( auto i = to_index(e) ) {\ if ( auto i = to_index(e) ) {\
return *i;\ return *i;\
}\ }\
return ::enum_hpp::invalid_index;\ return ::enum_hpp::invalid_index;\
}\ }\
static std::size_t to_index_or_throw(Enum e) {\ static std::size_t to_index_or_throw(enum_type e) {\
if ( auto i = to_index(e) ) {\ if ( auto i = to_index(e) ) {\
return *i;\ return *i;\
}\ }\
throw ::enum_hpp::exception(#Enum "_traits::to_index_or_throw(): invalid argument");\ ::enum_hpp::detail::throw_exception_with(#Enum "_traits::to_index_or_throw(): invalid argument");\
}\ }\
\ \
static constexpr std::optional<Enum> from_index(std::size_t index) noexcept {\ static constexpr std::optional<enum_type> from_index(std::size_t index) noexcept {\
if ( index < size ) {\ if ( index < size ) {\
return values[index];\ return values[index];\
}\ }\
return std::nullopt;\ return std::nullopt;\
}\ }\
static constexpr Enum from_index_or_default(std::size_t index, Enum def) noexcept {\ static constexpr enum_type from_index_or_default(std::size_t index, enum_type def) noexcept {\
if ( auto e = from_index(index) ) {\ if ( auto e = from_index(index) ) {\
return *e;\ return *e;\
}\ }\
return def;\ return def;\
}\ }\
static Enum from_index_or_throw(std::size_t index) {\ static enum_type from_index_or_throw(std::size_t index) {\
if ( auto e = from_index(index) ) {\ if ( auto e = from_index(index) ) {\
return *e;\ return *e;\
}\ }\
throw ::enum_hpp::exception(#Enum "_traits::from_index_or_throw(): invalid argument");\ ::enum_hpp::detail::throw_exception_with(#Enum "_traits::from_index_or_throw(): invalid argument");\
}\ }\
}; };
@@ -333,7 +349,9 @@ namespace enum_hpp::detail
// //
#define ENUM_HPP_REGISTER_TRAITS(Enum)\ #define ENUM_HPP_REGISTER_TRAITS(Enum)\
[[maybe_unused]] Enum##_traits enum_hpp_adl_find_registered_traits(Enum) noexcept; constexpr Enum##_traits enum_hpp_adl_find_registered_traits [[maybe_unused]] (Enum) noexcept {\
return Enum##_traits{};\
}
// ----------------------------------------------------------------------------- // -----------------------------------------------------------------------------
// //

View File

@@ -0,0 +1,329 @@
/*******************************************************************************
* This file is part of the "https://github.com/blackmatov/enum.hpp"
* For conditions of distribution and use, see copyright notice in LICENSE.md
* Copyright (C) 2019-2020, by Matvey Cherevko (blackmatov@gmail.com)
******************************************************************************/
#pragma once
#include <functional>
#include <type_traits>
#include <utility>
namespace enum_hpp::bitflags
{
template < typename Enum >
class bitflags final {
static_assert(std::is_enum_v<Enum>);
public:
using enum_type = Enum;
using underlying_type = std::underlying_type_t<Enum>;
bitflags() = default;
bitflags(const bitflags&) = default;
bitflags& operator=(const bitflags&) = default;
constexpr bitflags(enum_type flags)
: flags_(static_cast<underlying_type>(flags)) {}
constexpr explicit bitflags(underlying_type flags)
: flags_(flags) {}
constexpr void swap(bitflags& other) noexcept {
using std::swap;
swap(flags_, other.flags_);
}
constexpr explicit operator bool() const noexcept {
return !!flags_;
}
constexpr underlying_type as_raw() const noexcept {
return flags_;
}
constexpr enum_type as_enum() const noexcept {
return static_cast<enum_type>(flags_);
}
constexpr bool has(bitflags flags) const noexcept {
return flags.flags_ == (flags_ & flags.flags_);
}
constexpr bitflags& set(bitflags flags) noexcept {
flags_ |= flags.flags_;
return *this;
}
constexpr bitflags& toggle(bitflags flags) noexcept {
flags_ ^= flags.flags_;
return *this;
};
constexpr bitflags& clear(bitflags flags) noexcept {
flags_ &= ~flags.flags_;
return *this;
}
private:
underlying_type flags_{};
};
template < typename Enum >
constexpr void swap(bitflags<Enum>& l, bitflags<Enum>& r) noexcept {
l.swap(r);
}
}
namespace std
{
template < typename Enum >
struct hash<enum_hpp::bitflags::bitflags<Enum>> {
size_t operator()(enum_hpp::bitflags::bitflags<Enum> bf) const noexcept {
return hash<Enum>{}(bf.as_enum());
}
};
}
namespace enum_hpp::bitflags
{
#define ENUM_HPP_DEFINE_BINARY_OPERATOR(op)\
template < typename Enum >\
constexpr bool operator op(Enum l, bitflags<Enum> r) noexcept {\
return l op r.as_enum();\
}\
template < typename Enum >\
constexpr bool operator op(bitflags<Enum> l, Enum r) noexcept {\
return l.as_enum() op r;\
}\
template < typename Enum >\
constexpr bool operator op(std::underlying_type_t<Enum> l, bitflags<Enum> r) noexcept {\
return l op r.as_raw();\
}\
template < typename Enum >\
constexpr bool operator op(bitflags<Enum> l, std::underlying_type_t<Enum> r) noexcept {\
return l.as_raw() op r;\
}\
template < typename Enum >\
constexpr bool operator op(bitflags<Enum> l, bitflags<Enum> r) noexcept {\
return l.as_raw() op r.as_raw();\
}
ENUM_HPP_DEFINE_BINARY_OPERATOR(<)
ENUM_HPP_DEFINE_BINARY_OPERATOR(>)
ENUM_HPP_DEFINE_BINARY_OPERATOR(<=)
ENUM_HPP_DEFINE_BINARY_OPERATOR(>=)
ENUM_HPP_DEFINE_BINARY_OPERATOR(==)
ENUM_HPP_DEFINE_BINARY_OPERATOR(!=)
#undef ENUM_HPP_DEFINE_BINARY_OPERATOR
}
namespace enum_hpp::bitflags
{
template < typename Enum >
constexpr bitflags<Enum> operator~(bitflags<Enum> l) noexcept {
return static_cast<Enum>(~l.as_raw());
}
#define ENUM_HPP_DEFINE_BINARY_OPERATOR(op)\
template < typename Enum >\
constexpr bitflags<Enum> operator op (Enum l, bitflags<Enum> r) noexcept {\
return bitflags{l} op r;\
}\
template < typename Enum >\
constexpr bitflags<Enum> operator op (bitflags<Enum> l, Enum r) noexcept {\
return l op bitflags{r};\
}\
template < typename Enum >\
constexpr bitflags<Enum> operator op (bitflags<Enum> l, bitflags<Enum> r) noexcept {\
return static_cast<Enum>(l.as_raw() op r.as_raw());\
}\
template < typename Enum >\
constexpr bitflags<Enum>& operator op##= (bitflags<Enum>& l, Enum r) noexcept {\
return l = l op bitflags{r};\
}\
template < typename Enum >\
constexpr bitflags<Enum>& operator op##= (bitflags<Enum>& l, bitflags<Enum> r) noexcept {\
return l = l op r;\
}
ENUM_HPP_DEFINE_BINARY_OPERATOR(|)
ENUM_HPP_DEFINE_BINARY_OPERATOR(&)
ENUM_HPP_DEFINE_BINARY_OPERATOR(^)
#undef ENUM_HPP_DEFINE_BINARY_OPERATOR
}
namespace enum_hpp::bitflags
{
//
// any
//
template < typename Enum
, std::enable_if_t<std::is_enum_v<Enum>, int> = 0 >
constexpr bool any(Enum flags) noexcept {
return any(bitflags{flags});
}
template < typename Enum >
constexpr bool any(bitflags<Enum> flags) noexcept {
return 0 != flags.as_raw();
}
//
// none
//
template < typename Enum
, std::enable_if_t<std::is_enum_v<Enum>, int> = 0 >
constexpr bool none(Enum flags) noexcept {
return none(bitflags{flags});
}
template < typename Enum >
constexpr bool none(bitflags<Enum> flags) noexcept {
return 0 == flags.as_raw();
}
//
// all_of
//
template < typename Enum
, std::enable_if_t<std::is_enum_v<Enum>, int> = 0 >
constexpr bool all_of(Enum flags, Enum mask) noexcept {
return all_of(bitflags{flags}, bitflags{mask});
}
template < typename Enum >
constexpr bool all_of(Enum flags, bitflags<Enum> mask) noexcept {
return all_of(bitflags{flags}, mask);
}
template < typename Enum >
constexpr bool all_of(bitflags<Enum> flags, Enum mask) noexcept {
return all_of(flags, bitflags{mask});
}
template < typename Enum >
constexpr bool all_of(bitflags<Enum> flags, bitflags<Enum> mask) noexcept {
return (flags.as_raw() & mask.as_raw()) == mask.as_raw();
}
//
// any_of
//
template < typename Enum
, std::enable_if_t<std::is_enum_v<Enum>, int> = 0 >
constexpr bool any_of(Enum flags, Enum mask) noexcept {
return any_of(bitflags{flags}, bitflags{mask});
}
template < typename Enum >
constexpr bool any_of(Enum flags, bitflags<Enum> mask) noexcept {
return any_of(bitflags{flags}, mask);
}
template < typename Enum >
constexpr bool any_of(bitflags<Enum> flags, Enum mask) noexcept {
return any_of(flags, bitflags{mask});
}
template < typename Enum >
constexpr bool any_of(bitflags<Enum> flags, bitflags<Enum> mask) noexcept {
return mask.as_raw() == 0
|| (flags.as_raw() & mask.as_raw()) != 0;
}
//
// none_of
//
template < typename Enum
, std::enable_if_t<std::is_enum_v<Enum>, int> = 0 >
constexpr bool none_of(Enum flags, Enum mask) noexcept {
return none_of(bitflags{flags}, bitflags{mask});
}
template < typename Enum >
constexpr bool none_of(Enum flags, bitflags<Enum> mask) noexcept {
return none_of(bitflags{flags}, mask);
}
template < typename Enum >
constexpr bool none_of(bitflags<Enum> flags, Enum mask) noexcept {
return none_of(flags, bitflags{mask});
}
template < typename Enum >
constexpr bool none_of(bitflags<Enum> flags, bitflags<Enum> mask) noexcept {
return mask.as_raw() != 0
&& (flags.as_raw() & mask.as_raw()) == 0;
}
//
// any_except
//
template < typename Enum
, std::enable_if_t<std::is_enum_v<Enum>, int> = 0 >
constexpr bool any_except(Enum flags, Enum mask) noexcept {
return any_except(bitflags{flags}, bitflags{mask});
}
template < typename Enum >
constexpr bool any_except(Enum flags, bitflags<Enum> mask) noexcept {
return any_except(bitflags{flags}, mask);
}
template < typename Enum >
constexpr bool any_except(bitflags<Enum> flags, Enum mask) noexcept {
return any_except(flags, bitflags{mask});
}
template < typename Enum >
constexpr bool any_except(bitflags<Enum> flags, bitflags<Enum> mask) noexcept {
return any_of(flags, ~mask);
}
//
// none_except
//
template < typename Enum
, std::enable_if_t<std::is_enum_v<Enum>, int> = 0 >
constexpr bool none_except(Enum flags, Enum mask) noexcept {
return none_except(bitflags{flags}, bitflags{mask});
}
template < typename Enum >
constexpr bool none_except(Enum flags, bitflags<Enum> mask) noexcept {
return none_except(bitflags{flags}, mask);
}
template < typename Enum >
constexpr bool none_except(bitflags<Enum> flags, Enum mask) noexcept {
return none_except(flags, bitflags{mask});
}
template < typename Enum >
constexpr bool none_except(bitflags<Enum> flags, bitflags<Enum> mask) noexcept {
return none_of(flags, ~mask);
}
}
//
// ENUM_HPP_OPERATORS_DECL
//
#define ENUM_HPP_OPERATORS_DECL(Enum)\
constexpr ::enum_hpp::bitflags::bitflags<Enum> operator~ [[maybe_unused]] (Enum l) noexcept {\
return ~::enum_hpp::bitflags::bitflags(l);\
}\
constexpr ::enum_hpp::bitflags::bitflags<Enum> operator| [[maybe_unused]] (Enum l, Enum r) noexcept {\
return ::enum_hpp::bitflags::bitflags(l) | ::enum_hpp::bitflags::bitflags(r);\
}\
constexpr ::enum_hpp::bitflags::bitflags<Enum> operator& [[maybe_unused]] (Enum l, Enum r) noexcept {\
return ::enum_hpp::bitflags::bitflags(l) & ::enum_hpp::bitflags::bitflags(r);\
}\
constexpr ::enum_hpp::bitflags::bitflags<Enum> operator^ [[maybe_unused]] (Enum l, Enum r) noexcept {\
return ::enum_hpp::bitflags::bitflags(l) ^ ::enum_hpp::bitflags::bitflags(r);\
}

View File

@@ -11,7 +11,7 @@ lcov -d . -z
ctest --verbose ctest --verbose
lcov -d . -c -o "coverage.info" lcov -d . -c -o "coverage.info"
lcov -r "coverage.info" "*/usr/*" "*/catch.hpp" "*/catch_main.cpp" "*_tests.cpp" "*_examples.cpp" -o "coverage.info" lcov -r "coverage.info" "*/usr/*" "*/untests/*" -o "coverage.info"
lcov -l "coverage.info" lcov -l "coverage.info"
bash <(curl -s https://codecov.io/bash) || echo "Codecov did not collect coverage reports" bash <(curl -s https://codecov.io/bash) -f "coverage.info" || echo "Codecov did not collect coverage reports"

View File

@@ -1,6 +1,3 @@
# 3.11 version is required for `FetchContent`
cmake_minimum_required(VERSION 3.11 FATAL_ERROR)
project(enum.hpp.untests) project(enum.hpp.untests)
# #
@@ -19,7 +16,7 @@ endif()
# executable # executable
# #
file(GLOB UNTESTS_SOURCES "*.cpp" "*.hpp") file(GLOB_RECURSE UNTESTS_SOURCES "*.cpp" "*.hpp")
add_executable(${PROJECT_NAME} ${UNTESTS_SOURCES}) add_executable(${PROJECT_NAME} ${UNTESTS_SOURCES})
target_link_libraries(${PROJECT_NAME} enum.hpp) target_link_libraries(${PROJECT_NAME} enum.hpp)
@@ -32,20 +29,3 @@ target_compile_options(${PROJECT_NAME}
-Wall -Wextra -Wpedantic>) -Wall -Wextra -Wpedantic>)
add_test(${PROJECT_NAME} ${PROJECT_NAME}) add_test(${PROJECT_NAME} ${PROJECT_NAME})
#
# catchorg/catch2
#
include(FetchContent)
FetchContent_Declare(
catchorg_catch2
GIT_REPOSITORY https://github.com/catchorg/catch2
GIT_TAG v2.13.2)
FetchContent_GetProperties(catchorg_catch2)
if(NOT catchorg_catch2_POPULATED)
FetchContent_Populate(catchorg_catch2)
target_include_directories(${PROJECT_NAME}
PRIVATE ${catchorg_catch2_SOURCE_DIR}/single_include)
endif()

View File

@@ -1,9 +0,0 @@
/*******************************************************************************
* This file is part of the "https://github.com/blackmatov/enum.hpp"
* For conditions of distribution and use, see copyright notice in LICENSE.md
* Copyright (C) 2019-2020, by Matvey Cherevko (blackmatov@gmail.com)
******************************************************************************/
#define CATCH_CONFIG_MAIN
#define CATCH_CONFIG_FAST_COMPILE
#include <catch2/catch.hpp>

View File

@@ -0,0 +1,2 @@
#define DOCTEST_CONFIG_IMPLEMENT_WITH_MAIN
#include "doctest.h"

6260
untests/doctest/doctest.h Normal file

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,11 @@
#pragma once
#include "doctest.h"
#define STATIC_CHECK(...)\
static_assert(__VA_ARGS__, #__VA_ARGS__);\
CHECK(__VA_ARGS__);
#define STATIC_CHECK_FALSE(...)\
static_assert(!(__VA_ARGS__), "!(" #__VA_ARGS__ ")");\
CHECK(!(__VA_ARGS__));

View File

@@ -0,0 +1,49 @@
/*******************************************************************************
* This file is part of the "https://github.com/blackmatov/enum.hpp"
* For conditions of distribution and use, see copyright notice in LICENSE.md
* Copyright (C) 2019-2020, by Matvey Cherevko (blackmatov@gmail.com)
******************************************************************************/
#include <enum.hpp/enum_bitflags.hpp>
#include "doctest/doctest.hpp"
#include <string>
#include <iostream>
namespace
{
enum class perms : unsigned {
execute = 1 << 0,
write = 1 << 1,
read = 1 << 2,
};
ENUM_HPP_OPERATORS_DECL(perms)
}
TEST_CASE("bitflags_examples") {
SUBCASE("Enum operators using") {
namespace bf = enum_hpp::bitflags;
bf::bitflags<perms> flags = perms::read | perms::write;
if ( flags.has(perms::write) ) {
flags.clear(perms::write);
}
flags.set(perms::write | perms::execute);
if ( flags & perms::execute ) {
flags ^= perms::execute; // flags.toggle(perms::execute);
}
CHECK(flags == (perms::read | perms::write));
}
SUBCASE("Additional bitflags functions") {
namespace bf = enum_hpp::bitflags;
bf::bitflags<perms> flags = perms::read | perms::write;
CHECK(bf::any_of(flags, perms::write | perms::execute));
CHECK(bf::any_except(flags, perms::write | perms::execute));
}
}

View File

@@ -0,0 +1,426 @@
/*******************************************************************************
* This file is part of the "https://github.com/blackmatov/enum.hpp"
* For conditions of distribution and use, see copyright notice in LICENSE.md
* Copyright (C) 2019-2020, by Matvey Cherevko (blackmatov@gmail.com)
******************************************************************************/
#include <enum.hpp/enum_bitflags.hpp>
#include "doctest/doctest.hpp"
#include <cstdint>
#include <type_traits>
namespace
{
enum class access : std::uint8_t {
none = 0,
read = 1 << 0,
write = 1 << 1,
execute = 1 << 2,
read_write = read | write,
all = read_write | execute
};
ENUM_HPP_OPERATORS_DECL(access)
}
TEST_CASE("enum_bitflags") {
namespace bf = enum_hpp::bitflags;
SUBCASE("ctors") {
{
constexpr bf::bitflags<access> f;
STATIC_CHECK(!f);
STATIC_CHECK(f.as_raw() == 0x0);
STATIC_CHECK(f.as_enum() == access::none);
}
{
constexpr bf::bitflags f = access::read_write;
STATIC_CHECK(!!f);
STATIC_CHECK(f.as_raw() == 0x3);
STATIC_CHECK(f.as_enum() == access::read_write);
}
{
constexpr bf::bitflags<access> f{0x3};
STATIC_CHECK(!!f);
STATIC_CHECK(f.as_raw() == 0x3);
STATIC_CHECK(f.as_enum() == access::read_write);
}
{
constexpr bf::bitflags f = access::read | access::write;
constexpr bf::bitflags g = f;
STATIC_CHECK(g.as_raw() == 0x3);
}
{
constexpr bf::bitflags f = access::read | access::write;
bf::bitflags<access> g;
g = f;
CHECK(g.as_raw() == 0x3);
}
}
SUBCASE("swap") {
{
bf::bitflags f = access::read | access::write;
bf::bitflags g = access::write | access::execute;
f.swap(g);
CHECK(f == (access::write | access::execute));
CHECK(g == (access::read | access::write));
}
{
bf::bitflags f = access::read | access::write;
bf::bitflags g = access::write | access::execute;
swap(f, g);
CHECK(f == (access::write | access::execute));
CHECK(g == (access::read | access::write));
}
}
SUBCASE("hash") {
std::hash<bf::bitflags<access>> hasher1;
std::hash<bf::bitflags<access>> hasher2;
CHECK(hasher1(access::read) == hasher2(access::read));
CHECK(hasher1(access::read) != hasher2(access::read_write));
}
SUBCASE("enum_operators") {
STATIC_CHECK((~access::read) == bf::bitflags<access>(0xFE));
STATIC_CHECK((access::read | access::write) == bf::bitflags<access>(0x3));
STATIC_CHECK((access::read_write & access::write) == bf::bitflags<access>(0x2));
STATIC_CHECK((access::read_write ^ access::write) == bf::bitflags<access>(0x1));
}
SUBCASE("logic_operators") {
STATIC_CHECK(0x1 < bf::bitflags{access::write});
STATIC_CHECK(access::read < bf::bitflags{access::write});
STATIC_CHECK(bf::bitflags{access::read} < 0x2);
STATIC_CHECK(bf::bitflags{access::read} < access::write);
STATIC_CHECK(bf::bitflags{access::read} < bf::bitflags{access::write});
STATIC_CHECK_FALSE(0x2 < bf::bitflags{access::read});
STATIC_CHECK_FALSE(access::write < bf::bitflags{access::read});
STATIC_CHECK_FALSE(bf::bitflags{access::write} < 0x1);
STATIC_CHECK_FALSE(bf::bitflags{access::write} < access::read);
STATIC_CHECK_FALSE(bf::bitflags{access::write} < bf::bitflags{access::read});
STATIC_CHECK(0x1 <= bf::bitflags{access::write});
STATIC_CHECK(access::read <= bf::bitflags{access::write});
STATIC_CHECK(bf::bitflags{access::read} <= 0x2);
STATIC_CHECK(bf::bitflags{access::read} <= access::write);
STATIC_CHECK(bf::bitflags{access::read} <= bf::bitflags{access::write});
STATIC_CHECK_FALSE(0x2 <= bf::bitflags{access::read});
STATIC_CHECK_FALSE(access::write <= bf::bitflags{access::read});
STATIC_CHECK_FALSE(bf::bitflags{access::write} <= 0x1);
STATIC_CHECK_FALSE(bf::bitflags{access::write} <= access::read);
STATIC_CHECK_FALSE(bf::bitflags{access::write} <= bf::bitflags{access::read});
STATIC_CHECK(0x2 <= bf::bitflags{access::write});
STATIC_CHECK(access::write <= bf::bitflags{access::write});
STATIC_CHECK(bf::bitflags{access::write} <= 0x2);
STATIC_CHECK(bf::bitflags{access::write} <= access::write);
STATIC_CHECK(bf::bitflags{access::write} <= bf::bitflags{access::write});
//
STATIC_CHECK_FALSE(0x1 > bf::bitflags{access::write});
STATIC_CHECK_FALSE(access::read > bf::bitflags{access::write});
STATIC_CHECK_FALSE(bf::bitflags{access::read} > 0x2);
STATIC_CHECK_FALSE(bf::bitflags{access::read} > access::write);
STATIC_CHECK_FALSE(bf::bitflags{access::read} > bf::bitflags{access::write});
STATIC_CHECK(0x2 > bf::bitflags{access::read});
STATIC_CHECK(access::write > bf::bitflags{access::read});
STATIC_CHECK(bf::bitflags{access::write} > 0x1);
STATIC_CHECK(bf::bitflags{access::write} > access::read);
STATIC_CHECK(bf::bitflags{access::write} > bf::bitflags{access::read});
STATIC_CHECK_FALSE(0x1 >= bf::bitflags{access::write});
STATIC_CHECK_FALSE(access::read >= bf::bitflags{access::write});
STATIC_CHECK_FALSE(bf::bitflags{access::read} >= 0x2);
STATIC_CHECK_FALSE(bf::bitflags{access::read} >= access::write);
STATIC_CHECK_FALSE(bf::bitflags{access::read} >= bf::bitflags{access::write});
STATIC_CHECK(0x2 >= bf::bitflags{access::read});
STATIC_CHECK(access::write >= bf::bitflags{access::read});
STATIC_CHECK(bf::bitflags{access::write} >= 0x1);
STATIC_CHECK(bf::bitflags{access::write} >= access::read);
STATIC_CHECK(bf::bitflags{access::write} >= bf::bitflags{access::read});
STATIC_CHECK(0x2 >= bf::bitflags{access::write});
STATIC_CHECK(access::write >= bf::bitflags{access::write});
STATIC_CHECK(bf::bitflags{access::write} >= 0x2);
STATIC_CHECK(bf::bitflags{access::write} >= access::write);
STATIC_CHECK(bf::bitflags{access::write} >= bf::bitflags{access::write});
//
STATIC_CHECK(0x1 == bf::bitflags{access::read});
STATIC_CHECK(access::read == bf::bitflags{access::read});
STATIC_CHECK(bf::bitflags{access::read} == 0x1);
STATIC_CHECK(bf::bitflags{access::read} == access::read);
STATIC_CHECK(bf::bitflags{access::read} == bf::bitflags{access::read});
STATIC_CHECK_FALSE(0x1 == bf::bitflags{access::write});
STATIC_CHECK_FALSE(access::read == bf::bitflags{access::write});
STATIC_CHECK_FALSE(bf::bitflags{access::read} == 0x2);
STATIC_CHECK_FALSE(bf::bitflags{access::read} == access::write);
STATIC_CHECK_FALSE(bf::bitflags{access::read} == bf::bitflags{access::write});
STATIC_CHECK(0x1 != bf::bitflags{access::write});
STATIC_CHECK(access::read != bf::bitflags{access::write});
STATIC_CHECK(bf::bitflags{access::read} != 0x2);
STATIC_CHECK(bf::bitflags{access::read} != access::write);
STATIC_CHECK(bf::bitflags{access::read} != bf::bitflags{access::write});
STATIC_CHECK_FALSE(0x1 != bf::bitflags{access::read});
STATIC_CHECK_FALSE(access::read != bf::bitflags{access::read});
STATIC_CHECK_FALSE(bf::bitflags{access::read} != 0x1);
STATIC_CHECK_FALSE(bf::bitflags{access::read} != access::read);
STATIC_CHECK_FALSE(bf::bitflags{access::read} != bf::bitflags{access::read});
}
SUBCASE("bitflags_operators") {
STATIC_CHECK(0xFE == ~bf::bitflags{access::read});
STATIC_CHECK(~bf::bitflags{access::read} == 0xFE);
STATIC_CHECK(~bf::bitflags{access::read} == bf::bitflags<access>(0xFE));
STATIC_CHECK((access::write | bf::bitflags{access::read}) == 0x3);
STATIC_CHECK((bf::bitflags{access::read} | access::write) == 0x3);
STATIC_CHECK((bf::bitflags{access::read} | bf::bitflags{access::write}) == 0x3);
STATIC_CHECK((access::write & bf::bitflags{access::read_write}) == 0x2);
STATIC_CHECK((bf::bitflags{access::read_write} & access::write) == 0x2);
STATIC_CHECK((bf::bitflags{access::read_write} & bf::bitflags{access::write}) == 0x2);
STATIC_CHECK((access::write ^ bf::bitflags{access::read_write}) == 0x1);
STATIC_CHECK((bf::bitflags{access::read_write} ^ access::write) == 0x1);
STATIC_CHECK((bf::bitflags{access::read_write} ^ bf::bitflags{access::write}) == 0x1);
{
bf::bitflags f{access::read};
f |= access::write;
CHECK(f == 0x3);
}
{
bf::bitflags f{access::read};
f |= bf::bitflags{access::write};
CHECK(f == 0x3);
}
{
bf::bitflags f{access::read_write};
f &= access::write;
CHECK(f == 0x2);
}
{
bf::bitflags f{access::read_write};
f &= bf::bitflags{access::write};
CHECK(f == 0x2);
}
{
bf::bitflags f{access::read_write};
f ^= access::write;
CHECK(f == 0x1);
}
{
bf::bitflags f{access::read_write};
f ^= bf::bitflags{access::write};
CHECK(f == 0x1);
}
}
SUBCASE("has") {
constexpr bf::bitflags f = access::read_write;
STATIC_CHECK(f.has(access::read));
STATIC_CHECK(f.has(access::read_write));
STATIC_CHECK(f.has(access::read | access::write));
STATIC_CHECK_FALSE(f.has(access::execute));
STATIC_CHECK_FALSE(f.has(access::read | access::execute));
}
SUBCASE("set") {
bf::bitflags<access> f;
CHECK(&f == &f.set(access::read));
CHECK(f.has(access::read));
CHECK(&f == &f.set(access::read | access::write));
CHECK(f.has(access::read | access::write));
CHECK_FALSE(f.has(access::execute));
}
SUBCASE("toggle") {
bf::bitflags f = access::read_write;
CHECK(&f == &f.toggle(access::read));
CHECK_FALSE(f.has(access::read));
CHECK(f.has(access::write));
CHECK_FALSE(f.has(access::execute));
CHECK(&f == &f.toggle(access::read | access::write));
CHECK(f.has(access::read));
CHECK_FALSE(f.has(access::write));
CHECK_FALSE(f.has(access::execute));
CHECK(&f == &f.toggle(access::execute));
CHECK(f.has(access::read));
CHECK_FALSE(f.has(access::write));
CHECK(f.has(access::execute));
}
SUBCASE("clear") {
{
bf::bitflags f = access::all;
CHECK(&f == &f.clear(access::read));
CHECK_FALSE(f.has(access::read));
CHECK(f.has(access::write));
CHECK(f.has(access::execute));
CHECK(&f == &f.clear(access::read | access::execute));
CHECK_FALSE(f.has(access::read));
CHECK(f.has(access::write));
CHECK_FALSE(f.has(access::execute));
}
{
bf::bitflags f = access::all;
CHECK(&f == &f.clear(access::write | access::execute));
CHECK(f.has(access::read));
CHECK_FALSE(f.has(access::write));
CHECK_FALSE(f.has(access::execute));
CHECK(&f == &f.clear(access::read | access::execute));
CHECK_FALSE(f.has(access::read));
CHECK_FALSE(f.has(access::write));
CHECK_FALSE(f.has(access::execute));
}
}
SUBCASE("any") {
STATIC_CHECK_FALSE(bf::any(access::none));
STATIC_CHECK(bf::any(access::read));
STATIC_CHECK(bf::any(access::write));
STATIC_CHECK(bf::any(access::read_write));
}
SUBCASE("none") {
STATIC_CHECK(bf::none(access::none));
STATIC_CHECK_FALSE(bf::none(access::read));
STATIC_CHECK_FALSE(bf::none(access::write));
STATIC_CHECK_FALSE(bf::none(access::read_write));
}
SUBCASE("all_of") {
STATIC_CHECK(bf::all_of(access::none, access::none));
STATIC_CHECK_FALSE(bf::all_of(access::none, access::read));
STATIC_CHECK_FALSE(bf::all_of(access::none, access::write));
STATIC_CHECK_FALSE(bf::all_of(access::none, access::read_write));
STATIC_CHECK(bf::all_of(access::read, access::none));
STATIC_CHECK(bf::all_of(access::read, access::read));
STATIC_CHECK_FALSE(bf::all_of(access::read, access::write));
STATIC_CHECK_FALSE(bf::all_of(access::read, access::read_write));
STATIC_CHECK(bf::all_of(access::write, access::none));
STATIC_CHECK_FALSE(bf::all_of(access::write, access::read));
STATIC_CHECK(bf::all_of(access::write, access::write));
STATIC_CHECK_FALSE(bf::all_of(access::write, access::read_write));
STATIC_CHECK(bf::all_of(access::read_write, access::none));
STATIC_CHECK(bf::all_of(access::read_write, access::read));
STATIC_CHECK(bf::all_of(access::read_write, access::write));
STATIC_CHECK(bf::all_of(access::read_write, access::read_write));
}
SUBCASE("any_of") {
STATIC_CHECK(bf::any_of(access::none, access::none));
STATIC_CHECK_FALSE(bf::any_of(access::none, access::read));
STATIC_CHECK_FALSE(bf::any_of(access::none, access::write));
STATIC_CHECK_FALSE(bf::any_of(access::none, access::read_write));
STATIC_CHECK(bf::any_of(access::read, access::none));
STATIC_CHECK(bf::any_of(access::read, access::read));
STATIC_CHECK_FALSE(bf::any_of(access::read, access::write));
STATIC_CHECK(bf::any_of(access::read, access::read_write));
STATIC_CHECK(bf::any_of(access::write, access::none));
STATIC_CHECK_FALSE(bf::any_of(access::write, access::read));
STATIC_CHECK(bf::any_of(access::write, access::write));
STATIC_CHECK(bf::any_of(access::write, access::read_write));
STATIC_CHECK(bf::any_of(access::read_write, access::none));
STATIC_CHECK(bf::any_of(access::read_write, access::read));
STATIC_CHECK(bf::any_of(access::read_write, access::write));
STATIC_CHECK(bf::any_of(access::read_write, access::read_write));
}
SUBCASE("none_of") {
STATIC_CHECK_FALSE(bf::none_of(access::none, access::none));
STATIC_CHECK(bf::none_of(access::none, access::read));
STATIC_CHECK(bf::none_of(access::none, access::write));
STATIC_CHECK(bf::none_of(access::none, access::read_write));
STATIC_CHECK_FALSE(bf::none_of(access::read, access::none));
STATIC_CHECK_FALSE(bf::none_of(access::read, access::read));
STATIC_CHECK(bf::none_of(access::read, access::write));
STATIC_CHECK_FALSE(bf::none_of(access::read, access::read_write));
STATIC_CHECK_FALSE(bf::none_of(access::write, access::none));
STATIC_CHECK(bf::none_of(access::write, access::read));
STATIC_CHECK_FALSE(bf::none_of(access::write, access::write));
STATIC_CHECK_FALSE(bf::none_of(access::write, access::read_write));
STATIC_CHECK_FALSE(bf::none_of(access::read_write, access::none));
STATIC_CHECK_FALSE(bf::none_of(access::read_write, access::read));
STATIC_CHECK_FALSE(bf::none_of(access::read_write, access::write));
STATIC_CHECK_FALSE(bf::none_of(access::read_write, access::read_write));
}
SUBCASE("any_except") {
STATIC_CHECK_FALSE(bf::any_except(access::none, access::none));
STATIC_CHECK_FALSE(bf::any_except(access::none, access::read));
STATIC_CHECK_FALSE(bf::any_except(access::none, access::write));
STATIC_CHECK_FALSE(bf::any_except(access::none, access::read_write));
STATIC_CHECK(bf::any_except(access::read, access::none));
STATIC_CHECK_FALSE(bf::any_except(access::read, access::read));
STATIC_CHECK(bf::any_except(access::read, access::write));
STATIC_CHECK_FALSE(bf::any_except(access::read, access::read_write));
STATIC_CHECK(bf::any_except(access::write, access::none));
STATIC_CHECK(bf::any_except(access::write, access::read));
STATIC_CHECK_FALSE(bf::any_except(access::write, access::write));
STATIC_CHECK_FALSE(bf::any_except(access::write, access::read_write));
STATIC_CHECK(bf::any_except(access::read_write, access::none));
STATIC_CHECK(bf::any_except(access::read_write, access::read));
STATIC_CHECK(bf::any_except(access::read_write, access::write));
STATIC_CHECK_FALSE(bf::any_except(access::read_write, access::read_write));
}
SUBCASE("none_except") {
STATIC_CHECK(bf::none_except(access::none, access::none));
STATIC_CHECK(bf::none_except(access::none, access::read));
STATIC_CHECK(bf::none_except(access::none, access::write));
STATIC_CHECK(bf::none_except(access::none, access::read_write));
STATIC_CHECK_FALSE(bf::none_except(access::read, access::none));
STATIC_CHECK(bf::none_except(access::read, access::read));
STATIC_CHECK_FALSE(bf::none_except(access::read, access::write));
STATIC_CHECK(bf::none_except(access::read, access::read_write));
STATIC_CHECK_FALSE(bf::none_except(access::write, access::none));
STATIC_CHECK_FALSE(bf::none_except(access::write, access::read));
STATIC_CHECK(bf::none_except(access::write, access::write));
STATIC_CHECK(bf::none_except(access::write, access::read_write));
STATIC_CHECK_FALSE(bf::none_except(access::read_write, access::none));
STATIC_CHECK_FALSE(bf::none_except(access::read_write, access::read));
STATIC_CHECK_FALSE(bf::none_except(access::read_write, access::write));
STATIC_CHECK(bf::none_except(access::read_write, access::read_write));
}
}

View File

@@ -4,11 +4,10 @@
* Copyright (C) 2019-2020, by Matvey Cherevko (blackmatov@gmail.com) * Copyright (C) 2019-2020, by Matvey Cherevko (blackmatov@gmail.com)
******************************************************************************/ ******************************************************************************/
#define CATCH_CONFIG_FAST_COMPILE
#include <catch2/catch.hpp>
#include <enum.hpp/enum.hpp> #include <enum.hpp/enum.hpp>
#include "doctest/doctest.hpp"
#include <string> #include <string>
#include <iostream> #include <iostream>
@@ -23,8 +22,35 @@ namespace
ENUM_HPP_REGISTER_TRAITS(color) ENUM_HPP_REGISTER_TRAITS(color)
} }
namespace some_namespace
{
ENUM_HPP_CLASS_DECL(color, unsigned,
(red = 0xFF0000)
(green = 0x00FF00)
(blue = 0x0000FF)
(white = red | green | blue))
ENUM_HPP_REGISTER_TRAITS(color)
}
namespace external_ns
{
enum class external_enum : unsigned short {
a = 10,
b,
c = a + b
};
ENUM_HPP_TRAITS_DECL(external_enum,
(a)
(b)
(c))
ENUM_HPP_REGISTER_TRAITS(external_enum)
}
TEST_CASE("examples") { TEST_CASE("examples") {
SECTION("traits_using") { SUBCASE("traits using") {
// size // size
static_assert(color_traits::size == 4); static_assert(color_traits::size == 4);
@@ -48,16 +74,24 @@ TEST_CASE("examples") {
static_assert(color_traits::from_index(42) == std::nullopt); static_assert(color_traits::from_index(42) == std::nullopt);
// names // names
for ( auto n : color_traits::names ) { for ( std::string_view n : color_traits::names ) {
std::cout << n << ","; std::cout << n << ",";
} // stdout: red,green,blue, } // stdout: red,green,blue,
} }
SECTION("generic_context") { SUBCASE("generic context") {
using color = some_namespace::color;
// to string // to string
static_assert(enum_hpp::to_string(color::red) == "red"); static_assert(enum_hpp::to_string(color::red) == "red");
// from string // from string
static_assert(enum_hpp::from_string<color>("red") == color::red); static_assert(enum_hpp::from_string<color>("red") == color::red);
} }
SUBCASE("external enums") {
using ee = external_ns::external_enum;
static_assert(enum_hpp::to_string(ee::a) == "a");
static_assert(enum_hpp::from_string<ee>("c") == ee::c);
}
} }

View File

@@ -4,16 +4,14 @@
* Copyright (C) 2019-2020, by Matvey Cherevko (blackmatov@gmail.com) * Copyright (C) 2019-2020, by Matvey Cherevko (blackmatov@gmail.com)
******************************************************************************/ ******************************************************************************/
#define CATCH_CONFIG_FAST_COMPILE
#include <catch2/catch.hpp>
#include <enum.hpp/enum.hpp> #include <enum.hpp/enum.hpp>
#include <iterator> #include "doctest/doctest.hpp"
#include <string_view>
#include <type_traits>
using namespace std::string_view_literals; #include <iostream>
#include <iterator>
#include <string>
#include <type_traits>
namespace namespace
{ {
@@ -78,337 +76,365 @@ namespace some_namespace
TEST_CASE("enum") { TEST_CASE("enum") {
namespace sn = some_namespace; namespace sn = some_namespace;
SECTION("declaration") { SUBCASE("declaration") {
{ {
STATIC_REQUIRE(std::is_same_v< STATIC_CHECK(std::is_same_v<
std::underlying_type_t<sn::color>, std::underlying_type_t<sn::color>,
unsigned>); unsigned>);
STATIC_REQUIRE(std::is_same_v< STATIC_CHECK(std::is_same_v<
sn::color_traits::enum_type,
sn::color>);
STATIC_CHECK(std::is_same_v<
sn::color_traits::underlying_type, sn::color_traits::underlying_type,
enum_hpp::underlying_type<sn::color>>); enum_hpp::underlying_type<sn::color>>);
STATIC_REQUIRE(std::is_same_v< STATIC_CHECK(std::is_same_v<
sn::color_traits, sn::color_traits,
enum_hpp::traits_t<sn::color>>); enum_hpp::traits_t<sn::color>>);
REQUIRE(enum_to_underlying(sn::color::red) == 2u); CHECK(enum_to_underlying(sn::color::red) == 2u);
REQUIRE(enum_to_underlying(sn::color::green) == 3u); CHECK(enum_to_underlying(sn::color::green) == 3u);
REQUIRE(enum_to_underlying(sn::color::blue) == 6u); CHECK(enum_to_underlying(sn::color::blue) == 6u);
} }
{ {
STATIC_REQUIRE(std::is_same_v< STATIC_CHECK(std::is_same_v<
std::underlying_type_t<sn::render::mask>, std::underlying_type_t<sn::render::mask>,
unsigned char>); unsigned char>);
REQUIRE(enum_to_underlying(sn::render::mask::none) == 0u); CHECK(enum_to_underlying(sn::render::mask::none) == 0u);
REQUIRE(enum_to_underlying(sn::render::mask::color) == 1u); CHECK(enum_to_underlying(sn::render::mask::color) == 1u);
REQUIRE(enum_to_underlying(sn::render::mask::alpha) == 2u); CHECK(enum_to_underlying(sn::render::mask::alpha) == 2u);
REQUIRE(enum_to_underlying(sn::render::mask::all) == 3u); CHECK(enum_to_underlying(sn::render::mask::all) == 3u);
} }
{ {
STATIC_REQUIRE(std::is_same_v< STATIC_CHECK(std::is_same_v<
std::underlying_type_t<sn::numbers>, std::underlying_type_t<sn::numbers>,
int>); int>);
STATIC_REQUIRE(std::is_same_v< STATIC_CHECK(std::is_same_v<
sn::numbers_traits::underlying_type, sn::numbers_traits::underlying_type,
enum_hpp::underlying_type<sn::numbers>>); enum_hpp::underlying_type<sn::numbers>>);
STATIC_REQUIRE(std::is_same_v< STATIC_CHECK(std::is_same_v<
sn::numbers_traits, sn::numbers_traits,
enum_hpp::traits_t<sn::numbers>>); enum_hpp::traits_t<sn::numbers>>);
REQUIRE(enum_to_underlying(sn::_0) == 0); CHECK(enum_to_underlying(sn::_0) == 0);
REQUIRE(enum_to_underlying(sn::_100) == 100); CHECK(enum_to_underlying(sn::_100) == 100);
REQUIRE(enum_to_underlying(sn::_240) == 240); CHECK(enum_to_underlying(sn::_240) == 240);
} }
} }
SECTION("size") { SUBCASE("size") {
{ {
STATIC_REQUIRE(sn::color_traits::size == 3u); STATIC_CHECK(sn::color_traits::size == 3u);
STATIC_REQUIRE(sn::color_traits::size == std::size(sn::color_traits::names)); STATIC_CHECK(sn::color_traits::size == std::size(sn::color_traits::names));
STATIC_REQUIRE(sn::color_traits::size == std::size(sn::color_traits::values)); STATIC_CHECK(sn::color_traits::size == std::size(sn::color_traits::values));
STATIC_REQUIRE(enum_hpp::size<sn::color>() == 3u); STATIC_CHECK(enum_hpp::size<sn::color>() == 3u);
STATIC_REQUIRE(enum_hpp::traits_t<sn::color>::size == 3u); STATIC_CHECK(enum_hpp::traits_t<sn::color>::size == 3u);
STATIC_REQUIRE(std::size(enum_hpp::names<sn::color>()) == 3u); STATIC_CHECK(std::size(enum_hpp::names<sn::color>()) == 3u);
STATIC_REQUIRE(std::size(enum_hpp::values<sn::color>()) == 3u); STATIC_CHECK(std::size(enum_hpp::values<sn::color>()) == 3u);
} }
{ {
STATIC_REQUIRE(sn::render::mask_traits::size == 4u); STATIC_CHECK(sn::render::mask_traits::size == 4u);
STATIC_REQUIRE(sn::render::mask_traits::size == std::size(sn::render::mask_traits::names)); STATIC_CHECK(sn::render::mask_traits::size == std::size(sn::render::mask_traits::names));
STATIC_REQUIRE(sn::render::mask_traits::size == std::size(sn::render::mask_traits::values)); STATIC_CHECK(sn::render::mask_traits::size == std::size(sn::render::mask_traits::values));
} }
{ {
STATIC_REQUIRE(sn::numbers_traits::size == 241u); STATIC_CHECK(sn::numbers_traits::size == 241u);
STATIC_REQUIRE(sn::numbers_traits::size == std::size(sn::numbers_traits::names)); STATIC_CHECK(sn::numbers_traits::size == std::size(sn::numbers_traits::names));
STATIC_REQUIRE(sn::numbers_traits::size == std::size(sn::numbers_traits::values)); STATIC_CHECK(sn::numbers_traits::size == std::size(sn::numbers_traits::values));
STATIC_REQUIRE(enum_hpp::size<sn::numbers>() == 241u); STATIC_CHECK(enum_hpp::size<sn::numbers>() == 241u);
STATIC_REQUIRE(enum_hpp::traits_t<sn::numbers>::size == 241u); STATIC_CHECK(enum_hpp::traits_t<sn::numbers>::size == 241u);
STATIC_REQUIRE(std::size(enum_hpp::names<sn::numbers>()) == 241u); STATIC_CHECK(std::size(enum_hpp::names<sn::numbers>()) == 241u);
STATIC_REQUIRE(std::size(enum_hpp::values<sn::numbers>()) == 241u); STATIC_CHECK(std::size(enum_hpp::values<sn::numbers>()) == 241u);
STATIC_REQUIRE(enum_hpp::size<sn::render::mask>() == 4u); STATIC_CHECK(enum_hpp::size<sn::render::mask>() == 4u);
STATIC_REQUIRE(enum_hpp::traits_t<sn::render::mask>::size == 4u); STATIC_CHECK(enum_hpp::traits_t<sn::render::mask>::size == 4u);
STATIC_REQUIRE(std::size(enum_hpp::names<sn::render::mask>()) == 4u); STATIC_CHECK(std::size(enum_hpp::names<sn::render::mask>()) == 4u);
STATIC_REQUIRE(std::size(enum_hpp::values<sn::render::mask>()) == 4u); STATIC_CHECK(std::size(enum_hpp::values<sn::render::mask>()) == 4u);
} }
} }
SECTION("to_underlying") { SUBCASE("to_underlying") {
{ {
STATIC_REQUIRE(sn::color_traits::to_underlying(sn::color::red) == enum_to_underlying(sn::color::red)); STATIC_CHECK(sn::color_traits::to_underlying(sn::color::red) == enum_to_underlying(sn::color::red));
STATIC_REQUIRE(sn::color_traits::to_underlying(sn::color::green) == enum_to_underlying(sn::color::green)); STATIC_CHECK(sn::color_traits::to_underlying(sn::color::green) == enum_to_underlying(sn::color::green));
STATIC_REQUIRE(sn::color_traits::to_underlying(sn::color::blue) == enum_to_underlying(sn::color::blue)); STATIC_CHECK(sn::color_traits::to_underlying(sn::color::blue) == enum_to_underlying(sn::color::blue));
STATIC_REQUIRE(sn::color_traits::to_underlying(sn::color(42)) == 42); STATIC_CHECK(sn::color_traits::to_underlying(sn::color(42)) == 42);
STATIC_REQUIRE(enum_hpp::to_underlying(sn::color::red) == enum_to_underlying(sn::color::red)); STATIC_CHECK(enum_hpp::to_underlying(sn::color::red) == enum_to_underlying(sn::color::red));
STATIC_REQUIRE(enum_hpp::to_underlying(sn::color::green) == enum_to_underlying(sn::color::green)); STATIC_CHECK(enum_hpp::to_underlying(sn::color::green) == enum_to_underlying(sn::color::green));
STATIC_REQUIRE(enum_hpp::to_underlying(sn::color::blue) == enum_to_underlying(sn::color::blue)); STATIC_CHECK(enum_hpp::to_underlying(sn::color::blue) == enum_to_underlying(sn::color::blue));
STATIC_REQUIRE(enum_hpp::to_underlying(sn::color(42)) == 42); STATIC_CHECK(enum_hpp::to_underlying(sn::color(42)) == 42);
} }
{ {
STATIC_REQUIRE(sn::render::mask_traits::to_underlying(sn::render::mask::none) == enum_to_underlying(sn::render::mask::none)); STATIC_CHECK(sn::render::mask_traits::to_underlying(sn::render::mask::none) == enum_to_underlying(sn::render::mask::none));
STATIC_REQUIRE(sn::render::mask_traits::to_underlying(sn::render::mask::color) == enum_to_underlying(sn::render::mask::color)); STATIC_CHECK(sn::render::mask_traits::to_underlying(sn::render::mask::color) == enum_to_underlying(sn::render::mask::color));
STATIC_REQUIRE(sn::render::mask_traits::to_underlying(sn::render::mask::alpha) == enum_to_underlying(sn::render::mask::alpha)); STATIC_CHECK(sn::render::mask_traits::to_underlying(sn::render::mask::alpha) == enum_to_underlying(sn::render::mask::alpha));
STATIC_REQUIRE(sn::render::mask_traits::to_underlying(sn::render::mask::all) == enum_to_underlying(sn::render::mask::all)); STATIC_CHECK(sn::render::mask_traits::to_underlying(sn::render::mask::all) == enum_to_underlying(sn::render::mask::all));
} }
{ {
STATIC_REQUIRE(sn::numbers_traits::to_underlying(sn::_0) == enum_to_underlying(sn::_0)); STATIC_CHECK(sn::numbers_traits::to_underlying(sn::_0) == enum_to_underlying(sn::_0));
STATIC_REQUIRE(sn::numbers_traits::to_underlying(sn::_180) == enum_to_underlying(sn::_180)); STATIC_CHECK(sn::numbers_traits::to_underlying(sn::_180) == enum_to_underlying(sn::_180));
STATIC_REQUIRE(sn::numbers_traits::to_underlying(sn::_240) == enum_to_underlying(sn::_240)); STATIC_CHECK(sn::numbers_traits::to_underlying(sn::_240) == enum_to_underlying(sn::_240));
STATIC_REQUIRE(sn::numbers_traits::to_underlying(sn::numbers(100500)) == 100500); STATIC_CHECK(sn::numbers_traits::to_underlying(sn::numbers(100500)) == 100500);
STATIC_REQUIRE(enum_hpp::to_underlying(sn::_0) == enum_to_underlying(sn::_0)); STATIC_CHECK(enum_hpp::to_underlying(sn::_0) == enum_to_underlying(sn::_0));
STATIC_REQUIRE(enum_hpp::to_underlying(sn::_180) == enum_to_underlying(sn::_180)); STATIC_CHECK(enum_hpp::to_underlying(sn::_180) == enum_to_underlying(sn::_180));
STATIC_REQUIRE(enum_hpp::to_underlying(sn::_240) == enum_to_underlying(sn::_240)); STATIC_CHECK(enum_hpp::to_underlying(sn::_240) == enum_to_underlying(sn::_240));
STATIC_REQUIRE(enum_hpp::to_underlying(sn::numbers(100500)) == 100500); STATIC_CHECK(enum_hpp::to_underlying(sn::numbers(100500)) == 100500);
} }
} }
SECTION("to_string") { SUBCASE("to_string") {
{ {
STATIC_REQUIRE(sn::color_traits::to_string(sn::color::red) == "red"); STATIC_CHECK(sn::color_traits::to_string(sn::color::red) == "red");
STATIC_REQUIRE(sn::color_traits::to_string(sn::color::green) == "green"); STATIC_CHECK(sn::color_traits::to_string(sn::color::green) == "green");
STATIC_REQUIRE(sn::color_traits::to_string(sn::color::blue) == "blue"); STATIC_CHECK(sn::color_traits::to_string(sn::color::blue) == "blue");
STATIC_REQUIRE(sn::color_traits::to_string_or_empty(sn::color::red) == "red"); STATIC_CHECK(sn::color_traits::to_string_or_empty(sn::color::red) == "red");
STATIC_REQUIRE(sn::color_traits::to_string_or_empty(sn::color::green) == "green"); STATIC_CHECK(sn::color_traits::to_string_or_empty(sn::color::green) == "green");
STATIC_REQUIRE(sn::color_traits::to_string_or_empty(sn::color::blue) == "blue"); STATIC_CHECK(sn::color_traits::to_string_or_empty(sn::color::blue) == "blue");
REQUIRE(sn::color_traits::to_string_or_throw(sn::color::red) == "red"); CHECK(sn::color_traits::to_string_or_throw(sn::color::red) == "red");
REQUIRE(sn::color_traits::to_string_or_throw(sn::color::green) == "green"); CHECK(sn::color_traits::to_string_or_throw(sn::color::green) == "green");
REQUIRE(sn::color_traits::to_string_or_throw(sn::color::blue) == "blue"); CHECK(sn::color_traits::to_string_or_throw(sn::color::blue) == "blue");
STATIC_REQUIRE_FALSE(sn::color_traits::to_string(sn::color(42))); STATIC_CHECK_FALSE(sn::color_traits::to_string(sn::color(42)));
STATIC_REQUIRE(sn::color_traits::to_string_or_empty(sn::color(42)) == ""); STATIC_CHECK(sn::color_traits::to_string_or_empty(sn::color(42)) == "");
REQUIRE_THROWS_AS(sn::color_traits::to_string_or_throw(sn::color(42)), enum_hpp::exception); #ifndef ENUM_HPP_NO_EXCEPTIONS
CHECK_THROWS_AS(sn::color_traits::to_string_or_throw(sn::color(42)), enum_hpp::exception);
#endif
STATIC_REQUIRE(enum_hpp::to_string(sn::color::green) == "green"); STATIC_CHECK(enum_hpp::to_string(sn::color::green) == "green");
STATIC_REQUIRE(enum_hpp::to_string_or_empty(sn::color::green) == "green"); STATIC_CHECK(enum_hpp::to_string_or_empty(sn::color::green) == "green");
REQUIRE(enum_hpp::to_string_or_throw(sn::color::green) == "green"); CHECK(enum_hpp::to_string_or_throw(sn::color::green) == "green");
REQUIRE_THROWS_AS(enum_hpp::to_string_or_throw(sn::color(42)), enum_hpp::exception); #ifndef ENUM_HPP_NO_EXCEPTIONS
CHECK_THROWS_AS(enum_hpp::to_string_or_throw(sn::color(42)), enum_hpp::exception);
#endif
} }
{ {
STATIC_REQUIRE(sn::render::mask_traits::to_string(sn::render::mask::none) == "none"); STATIC_CHECK(sn::render::mask_traits::to_string(sn::render::mask::none) == "none");
STATIC_REQUIRE(sn::render::mask_traits::to_string(sn::render::mask::color) == "color"); STATIC_CHECK(sn::render::mask_traits::to_string(sn::render::mask::color) == "color");
STATIC_REQUIRE(sn::render::mask_traits::to_string(sn::render::mask::alpha) == "alpha"); STATIC_CHECK(sn::render::mask_traits::to_string(sn::render::mask::alpha) == "alpha");
STATIC_REQUIRE(sn::render::mask_traits::to_string(sn::render::mask::all) == "all"); STATIC_CHECK(sn::render::mask_traits::to_string(sn::render::mask::all) == "all");
STATIC_REQUIRE(sn::render::mask_traits::to_string_or_empty(sn::render::mask::none) == "none"); STATIC_CHECK(sn::render::mask_traits::to_string_or_empty(sn::render::mask::none) == "none");
STATIC_REQUIRE(sn::render::mask_traits::to_string_or_empty(sn::render::mask::color) == "color"); STATIC_CHECK(sn::render::mask_traits::to_string_or_empty(sn::render::mask::color) == "color");
STATIC_REQUIRE(sn::render::mask_traits::to_string_or_empty(sn::render::mask::alpha) == "alpha"); STATIC_CHECK(sn::render::mask_traits::to_string_or_empty(sn::render::mask::alpha) == "alpha");
STATIC_REQUIRE(sn::render::mask_traits::to_string_or_empty(sn::render::mask::all) == "all"); STATIC_CHECK(sn::render::mask_traits::to_string_or_empty(sn::render::mask::all) == "all");
REQUIRE(sn::render::mask_traits::to_string_or_throw(sn::render::mask::none) == "none"); CHECK(sn::render::mask_traits::to_string_or_throw(sn::render::mask::none) == "none");
REQUIRE(sn::render::mask_traits::to_string_or_throw(sn::render::mask::color) == "color"); CHECK(sn::render::mask_traits::to_string_or_throw(sn::render::mask::color) == "color");
REQUIRE(sn::render::mask_traits::to_string_or_throw(sn::render::mask::alpha) == "alpha"); CHECK(sn::render::mask_traits::to_string_or_throw(sn::render::mask::alpha) == "alpha");
REQUIRE(sn::render::mask_traits::to_string_or_throw(sn::render::mask::all) == "all"); CHECK(sn::render::mask_traits::to_string_or_throw(sn::render::mask::all) == "all");
} }
{ {
STATIC_REQUIRE(sn::numbers_traits::to_string(sn::_0) == "_0"); STATIC_CHECK(sn::numbers_traits::to_string(sn::_0) == "_0");
STATIC_REQUIRE(sn::numbers_traits::to_string(sn::_180) == "_180"); STATIC_CHECK(sn::numbers_traits::to_string(sn::_180) == "_180");
STATIC_REQUIRE(sn::numbers_traits::to_string(sn::_240) == "_240"); STATIC_CHECK(sn::numbers_traits::to_string(sn::_240) == "_240");
STATIC_REQUIRE(sn::numbers_traits::to_string_or_empty(sn::_0) == "_0"); STATIC_CHECK(sn::numbers_traits::to_string_or_empty(sn::_0) == "_0");
STATIC_REQUIRE(sn::numbers_traits::to_string_or_empty(sn::_180) == "_180"); STATIC_CHECK(sn::numbers_traits::to_string_or_empty(sn::_180) == "_180");
STATIC_REQUIRE(sn::numbers_traits::to_string_or_empty(sn::_240) == "_240"); STATIC_CHECK(sn::numbers_traits::to_string_or_empty(sn::_240) == "_240");
REQUIRE(sn::numbers_traits::to_string_or_throw(sn::_0) == "_0"); CHECK(sn::numbers_traits::to_string_or_throw(sn::_0) == "_0");
REQUIRE(sn::numbers_traits::to_string_or_throw(sn::_180) == "_180"); CHECK(sn::numbers_traits::to_string_or_throw(sn::_180) == "_180");
REQUIRE(sn::numbers_traits::to_string_or_throw(sn::_240) == "_240"); CHECK(sn::numbers_traits::to_string_or_throw(sn::_240) == "_240");
STATIC_REQUIRE_FALSE(sn::numbers_traits::to_string(sn::numbers(100500))); STATIC_CHECK_FALSE(sn::numbers_traits::to_string(sn::numbers(100500)));
STATIC_REQUIRE(sn::numbers_traits::to_string_or_empty(sn::numbers(100500)) == ""); STATIC_CHECK(sn::numbers_traits::to_string_or_empty(sn::numbers(100500)) == "");
REQUIRE_THROWS_AS(sn::numbers_traits::to_string_or_throw(sn::numbers(100500)), enum_hpp::exception); #ifndef ENUM_HPP_NO_EXCEPTIONS
CHECK_THROWS_AS(sn::numbers_traits::to_string_or_throw(sn::numbers(100500)), enum_hpp::exception);
#endif
STATIC_REQUIRE(enum_hpp::to_string(sn::_180) == "_180"); STATIC_CHECK(enum_hpp::to_string(sn::_180) == "_180");
STATIC_REQUIRE(enum_hpp::to_string_or_empty(sn::_180) == "_180"); STATIC_CHECK(enum_hpp::to_string_or_empty(sn::_180) == "_180");
REQUIRE(enum_hpp::to_string_or_throw(sn::_180) == "_180"); CHECK(enum_hpp::to_string_or_throw(sn::_180) == "_180");
REQUIRE_THROWS_AS(enum_hpp::to_string_or_throw(sn::numbers(100500)), enum_hpp::exception); #ifndef ENUM_HPP_NO_EXCEPTIONS
CHECK_THROWS_AS(enum_hpp::to_string_or_throw(sn::numbers(100500)), enum_hpp::exception);
#endif
} }
} }
SECTION("from_string") { SUBCASE("from_string") {
{ {
STATIC_REQUIRE(sn::color_traits::from_string("red") == sn::color::red); STATIC_CHECK(sn::color_traits::from_string("red") == sn::color::red);
STATIC_REQUIRE(sn::color_traits::from_string("green") == sn::color::green); STATIC_CHECK(sn::color_traits::from_string("green") == sn::color::green);
STATIC_REQUIRE(sn::color_traits::from_string("blue") == sn::color::blue); STATIC_CHECK(sn::color_traits::from_string("blue") == sn::color::blue);
STATIC_REQUIRE(sn::color_traits::from_string_or_default("red", sn::color::green) == sn::color::red); STATIC_CHECK(sn::color_traits::from_string_or_default("red", sn::color::green) == sn::color::red);
STATIC_REQUIRE(sn::color_traits::from_string_or_default("green", sn::color::red) == sn::color::green); STATIC_CHECK(sn::color_traits::from_string_or_default("green", sn::color::red) == sn::color::green);
STATIC_REQUIRE(sn::color_traits::from_string_or_default("blue", sn::color::red) == sn::color::blue); STATIC_CHECK(sn::color_traits::from_string_or_default("blue", sn::color::red) == sn::color::blue);
REQUIRE(sn::color_traits::from_string_or_throw("red") == sn::color::red); CHECK(sn::color_traits::from_string_or_throw("red") == sn::color::red);
REQUIRE(sn::color_traits::from_string_or_throw("green") == sn::color::green); CHECK(sn::color_traits::from_string_or_throw("green") == sn::color::green);
REQUIRE(sn::color_traits::from_string_or_throw("blue") == sn::color::blue); CHECK(sn::color_traits::from_string_or_throw("blue") == sn::color::blue);
STATIC_REQUIRE_FALSE(sn::color_traits::from_string("42")); STATIC_CHECK_FALSE(sn::color_traits::from_string("42"));
STATIC_REQUIRE(sn::color_traits::from_string_or_default("42", sn::color::red) == sn::color::red); STATIC_CHECK(sn::color_traits::from_string_or_default("42", sn::color::red) == sn::color::red);
REQUIRE_THROWS_AS(sn::color_traits::from_string_or_throw("42"), enum_hpp::exception); #ifndef ENUM_HPP_NO_EXCEPTIONS
CHECK_THROWS_AS(sn::color_traits::from_string_or_throw("42"), enum_hpp::exception);
#endif
STATIC_REQUIRE(enum_hpp::from_string<sn::color>("green") == sn::color::green); STATIC_CHECK(enum_hpp::from_string<sn::color>("green") == sn::color::green);
STATIC_REQUIRE(enum_hpp::from_string_or_default("green", sn::color::red) == sn::color::green); STATIC_CHECK(enum_hpp::from_string_or_default("green", sn::color::red) == sn::color::green);
REQUIRE(enum_hpp::from_string_or_throw<sn::color>("green") == sn::color::green); CHECK(enum_hpp::from_string_or_throw<sn::color>("green") == sn::color::green);
REQUIRE_THROWS_AS(enum_hpp::from_string_or_throw<sn::color>("42"), enum_hpp::exception); #ifndef ENUM_HPP_NO_EXCEPTIONS
CHECK_THROWS_AS(enum_hpp::from_string_or_throw<sn::color>("42"), enum_hpp::exception);
#endif
} }
{ {
STATIC_REQUIRE(sn::render::mask_traits::from_string("none") == sn::render::mask::none); STATIC_CHECK(sn::render::mask_traits::from_string("none") == sn::render::mask::none);
STATIC_REQUIRE(sn::render::mask_traits::from_string("color") == sn::render::mask::color); STATIC_CHECK(sn::render::mask_traits::from_string("color") == sn::render::mask::color);
STATIC_REQUIRE(sn::render::mask_traits::from_string("alpha") == sn::render::mask::alpha); STATIC_CHECK(sn::render::mask_traits::from_string("alpha") == sn::render::mask::alpha);
STATIC_REQUIRE(sn::render::mask_traits::from_string("all") == sn::render::mask::all); STATIC_CHECK(sn::render::mask_traits::from_string("all") == sn::render::mask::all);
STATIC_REQUIRE_FALSE(sn::render::mask_traits::from_string("42")); STATIC_CHECK_FALSE(sn::render::mask_traits::from_string("42"));
} }
{ {
STATIC_REQUIRE(sn::numbers_traits::from_string("_10") == sn::_10); STATIC_CHECK(sn::numbers_traits::from_string("_10") == sn::_10);
STATIC_REQUIRE(sn::numbers_traits::from_string("_240") == sn::_240); STATIC_CHECK(sn::numbers_traits::from_string("_240") == sn::_240);
STATIC_REQUIRE_FALSE(sn::numbers_traits::from_string("error")); STATIC_CHECK_FALSE(sn::numbers_traits::from_string("error"));
STATIC_REQUIRE(enum_hpp::from_string<sn::numbers>("_10") == sn::_10); STATIC_CHECK(enum_hpp::from_string<sn::numbers>("_10") == sn::_10);
STATIC_REQUIRE(enum_hpp::from_string<sn::numbers>("_240") == sn::_240); STATIC_CHECK(enum_hpp::from_string<sn::numbers>("_240") == sn::_240);
STATIC_REQUIRE_FALSE(enum_hpp::from_string<sn::numbers>("error")); STATIC_CHECK_FALSE(enum_hpp::from_string<sn::numbers>("error"));
} }
} }
SECTION("to_index") { SUBCASE("to_index") {
{ {
STATIC_REQUIRE(sn::color_traits::to_index(sn::color::red) == 0u); STATIC_CHECK(sn::color_traits::to_index(sn::color::red) == 0u);
STATIC_REQUIRE(sn::color_traits::to_index(sn::color::green) == 1u); STATIC_CHECK(sn::color_traits::to_index(sn::color::green) == 1u);
STATIC_REQUIRE(sn::color_traits::to_index(sn::color::blue) == 2u); STATIC_CHECK(sn::color_traits::to_index(sn::color::blue) == 2u);
STATIC_REQUIRE(sn::color_traits::to_index_or_invalid(sn::color::red) == 0u); STATIC_CHECK(sn::color_traits::to_index_or_invalid(sn::color::red) == 0u);
STATIC_REQUIRE(sn::color_traits::to_index_or_invalid(sn::color::green) == 1u); STATIC_CHECK(sn::color_traits::to_index_or_invalid(sn::color::green) == 1u);
STATIC_REQUIRE(sn::color_traits::to_index_or_invalid(sn::color::blue) == 2u); STATIC_CHECK(sn::color_traits::to_index_or_invalid(sn::color::blue) == 2u);
REQUIRE(sn::color_traits::to_index_or_throw(sn::color::red) == 0u); CHECK(sn::color_traits::to_index_or_throw(sn::color::red) == 0u);
REQUIRE(sn::color_traits::to_index_or_throw(sn::color::green) == 1u); CHECK(sn::color_traits::to_index_or_throw(sn::color::green) == 1u);
REQUIRE(sn::color_traits::to_index_or_throw(sn::color::blue) == 2u); CHECK(sn::color_traits::to_index_or_throw(sn::color::blue) == 2u);
STATIC_REQUIRE_FALSE(sn::color_traits::to_index(sn::color(42))); STATIC_CHECK_FALSE(sn::color_traits::to_index(sn::color(42)));
STATIC_REQUIRE(sn::color_traits::to_index_or_invalid(sn::color(42)) == enum_hpp::invalid_index); STATIC_CHECK(sn::color_traits::to_index_or_invalid(sn::color(42)) == enum_hpp::invalid_index);
REQUIRE_THROWS_AS(sn::color_traits::to_index_or_throw(sn::color(42)), enum_hpp::exception); #ifndef ENUM_HPP_NO_EXCEPTIONS
CHECK_THROWS_AS(sn::color_traits::to_index_or_throw(sn::color(42)), enum_hpp::exception);
#endif
STATIC_REQUIRE(enum_hpp::to_index(sn::color::green) == 1u); STATIC_CHECK(enum_hpp::to_index(sn::color::green) == 1u);
STATIC_REQUIRE(enum_hpp::to_index_or_invalid(sn::color::green) == 1u); STATIC_CHECK(enum_hpp::to_index_or_invalid(sn::color::green) == 1u);
REQUIRE(enum_hpp::to_index_or_throw(sn::color::green) == 1u); CHECK(enum_hpp::to_index_or_throw(sn::color::green) == 1u);
REQUIRE_THROWS_AS(enum_hpp::to_index_or_throw(sn::color(42)), enum_hpp::exception); #ifndef ENUM_HPP_NO_EXCEPTIONS
CHECK_THROWS_AS(enum_hpp::to_index_or_throw(sn::color(42)), enum_hpp::exception);
#endif
} }
{ {
STATIC_REQUIRE(sn::render::mask_traits::to_index(sn::render::mask::none) == 0u); STATIC_CHECK(sn::render::mask_traits::to_index(sn::render::mask::none) == 0u);
STATIC_REQUIRE(sn::render::mask_traits::to_index(sn::render::mask::color) == 1u); STATIC_CHECK(sn::render::mask_traits::to_index(sn::render::mask::color) == 1u);
STATIC_REQUIRE(sn::render::mask_traits::to_index(sn::render::mask::alpha) == 2u); STATIC_CHECK(sn::render::mask_traits::to_index(sn::render::mask::alpha) == 2u);
STATIC_REQUIRE(sn::render::mask_traits::to_index(sn::render::mask::all) == 3u); STATIC_CHECK(sn::render::mask_traits::to_index(sn::render::mask::all) == 3u);
STATIC_REQUIRE(sn::render::mask_traits::to_index_or_invalid(sn::render::mask::none) == 0u); STATIC_CHECK(sn::render::mask_traits::to_index_or_invalid(sn::render::mask::none) == 0u);
STATIC_REQUIRE(sn::render::mask_traits::to_index_or_invalid(sn::render::mask::color) == 1u); STATIC_CHECK(sn::render::mask_traits::to_index_or_invalid(sn::render::mask::color) == 1u);
STATIC_REQUIRE(sn::render::mask_traits::to_index_or_invalid(sn::render::mask::alpha) == 2u); STATIC_CHECK(sn::render::mask_traits::to_index_or_invalid(sn::render::mask::alpha) == 2u);
STATIC_REQUIRE(sn::render::mask_traits::to_index_or_invalid(sn::render::mask::all) == 3u); STATIC_CHECK(sn::render::mask_traits::to_index_or_invalid(sn::render::mask::all) == 3u);
REQUIRE(sn::render::mask_traits::to_index_or_throw(sn::render::mask::none) == 0u); CHECK(sn::render::mask_traits::to_index_or_throw(sn::render::mask::none) == 0u);
REQUIRE(sn::render::mask_traits::to_index_or_throw(sn::render::mask::color) == 1u); CHECK(sn::render::mask_traits::to_index_or_throw(sn::render::mask::color) == 1u);
REQUIRE(sn::render::mask_traits::to_index_or_throw(sn::render::mask::alpha) == 2u); CHECK(sn::render::mask_traits::to_index_or_throw(sn::render::mask::alpha) == 2u);
REQUIRE(sn::render::mask_traits::to_index_or_throw(sn::render::mask::all) == 3u); CHECK(sn::render::mask_traits::to_index_or_throw(sn::render::mask::all) == 3u);
} }
{ {
STATIC_REQUIRE(sn::numbers_traits::to_index(sn::_0) == 0u); STATIC_CHECK(sn::numbers_traits::to_index(sn::_0) == 0u);
STATIC_REQUIRE(sn::numbers_traits::to_index(sn::_180) == 180u); STATIC_CHECK(sn::numbers_traits::to_index(sn::_180) == 180u);
STATIC_REQUIRE(sn::numbers_traits::to_index(sn::_240) == 240u); STATIC_CHECK(sn::numbers_traits::to_index(sn::_240) == 240u);
STATIC_REQUIRE(sn::numbers_traits::to_index_or_invalid(sn::_0) == 0u); STATIC_CHECK(sn::numbers_traits::to_index_or_invalid(sn::_0) == 0u);
STATIC_REQUIRE(sn::numbers_traits::to_index_or_invalid(sn::_180) == 180u); STATIC_CHECK(sn::numbers_traits::to_index_or_invalid(sn::_180) == 180u);
STATIC_REQUIRE(sn::numbers_traits::to_index_or_invalid(sn::_240) == 240u); STATIC_CHECK(sn::numbers_traits::to_index_or_invalid(sn::_240) == 240u);
REQUIRE(sn::numbers_traits::to_index_or_throw(sn::_0) == 0u); CHECK(sn::numbers_traits::to_index_or_throw(sn::_0) == 0u);
REQUIRE(sn::numbers_traits::to_index_or_throw(sn::_180) == 180u); CHECK(sn::numbers_traits::to_index_or_throw(sn::_180) == 180u);
REQUIRE(sn::numbers_traits::to_index_or_throw(sn::_240) == 240u); CHECK(sn::numbers_traits::to_index_or_throw(sn::_240) == 240u);
STATIC_REQUIRE_FALSE(sn::numbers_traits::to_index(sn::numbers(100500))); STATIC_CHECK_FALSE(sn::numbers_traits::to_index(sn::numbers(100500)));
STATIC_REQUIRE(sn::numbers_traits::to_index_or_invalid(sn::numbers(100500)) == enum_hpp::invalid_index); STATIC_CHECK(sn::numbers_traits::to_index_or_invalid(sn::numbers(100500)) == enum_hpp::invalid_index);
REQUIRE_THROWS_AS(sn::numbers_traits::to_index_or_throw(sn::numbers(100500)), enum_hpp::exception); #ifndef ENUM_HPP_NO_EXCEPTIONS
CHECK_THROWS_AS(sn::numbers_traits::to_index_or_throw(sn::numbers(100500)), enum_hpp::exception);
#endif
STATIC_REQUIRE(enum_hpp::to_index(sn::_180) == 180u); STATIC_CHECK(enum_hpp::to_index(sn::_180) == 180u);
STATIC_REQUIRE(enum_hpp::to_index_or_invalid(sn::_180) == 180u); STATIC_CHECK(enum_hpp::to_index_or_invalid(sn::_180) == 180u);
REQUIRE(enum_hpp::to_index_or_throw(sn::_180) == 180u); CHECK(enum_hpp::to_index_or_throw(sn::_180) == 180u);
REQUIRE_THROWS_AS(enum_hpp::to_index_or_throw(sn::numbers(100500)), enum_hpp::exception); #ifndef ENUM_HPP_NO_EXCEPTIONS
CHECK_THROWS_AS(enum_hpp::to_index_or_throw(sn::numbers(100500)), enum_hpp::exception);
#endif
} }
} }
SECTION("from_index") { SUBCASE("from_index") {
{ {
STATIC_REQUIRE(sn::color_traits::from_index(0) == sn::color::red); STATIC_CHECK(sn::color_traits::from_index(0) == sn::color::red);
STATIC_REQUIRE(sn::color_traits::from_index(1) == sn::color::green); STATIC_CHECK(sn::color_traits::from_index(1) == sn::color::green);
STATIC_REQUIRE(sn::color_traits::from_index(2) == sn::color::blue); STATIC_CHECK(sn::color_traits::from_index(2) == sn::color::blue);
STATIC_REQUIRE(sn::color_traits::from_index_or_default(0, sn::color::green) == sn::color::red); STATIC_CHECK(sn::color_traits::from_index_or_default(0, sn::color::green) == sn::color::red);
STATIC_REQUIRE(sn::color_traits::from_index_or_default(1, sn::color::red) == sn::color::green); STATIC_CHECK(sn::color_traits::from_index_or_default(1, sn::color::red) == sn::color::green);
STATIC_REQUIRE(sn::color_traits::from_index_or_default(2, sn::color::red) == sn::color::blue); STATIC_CHECK(sn::color_traits::from_index_or_default(2, sn::color::red) == sn::color::blue);
REQUIRE(sn::color_traits::from_index_or_throw(0) == sn::color::red); CHECK(sn::color_traits::from_index_or_throw(0) == sn::color::red);
REQUIRE(sn::color_traits::from_index_or_throw(1) == sn::color::green); CHECK(sn::color_traits::from_index_or_throw(1) == sn::color::green);
REQUIRE(sn::color_traits::from_index_or_throw(2) == sn::color::blue); CHECK(sn::color_traits::from_index_or_throw(2) == sn::color::blue);
STATIC_REQUIRE_FALSE(sn::color_traits::from_index(42)); STATIC_CHECK_FALSE(sn::color_traits::from_index(42));
STATIC_REQUIRE(sn::color_traits::from_index_or_default(42, sn::color::red) == sn::color::red); STATIC_CHECK(sn::color_traits::from_index_or_default(42, sn::color::red) == sn::color::red);
REQUIRE_THROWS_AS(sn::color_traits::from_index_or_throw(42), enum_hpp::exception); #ifndef ENUM_HPP_NO_EXCEPTIONS
CHECK_THROWS_AS(sn::color_traits::from_index_or_throw(42), enum_hpp::exception);
#endif
STATIC_REQUIRE(enum_hpp::from_index<sn::color>(1) == sn::color::green); STATIC_CHECK(enum_hpp::from_index<sn::color>(1) == sn::color::green);
STATIC_REQUIRE(enum_hpp::from_index_or_default(1, sn::color::red) == sn::color::green); STATIC_CHECK(enum_hpp::from_index_or_default(1, sn::color::red) == sn::color::green);
REQUIRE(enum_hpp::from_index_or_throw<sn::color>(1) == sn::color::green); CHECK(enum_hpp::from_index_or_throw<sn::color>(1) == sn::color::green);
REQUIRE_THROWS_AS(enum_hpp::from_index_or_throw<sn::color>(42), enum_hpp::exception); #ifndef ENUM_HPP_NO_EXCEPTIONS
CHECK_THROWS_AS(enum_hpp::from_index_or_throw<sn::color>(42), enum_hpp::exception);
#endif
} }
{ {
STATIC_REQUIRE(sn::render::mask_traits::from_index(0) == sn::render::mask::none); STATIC_CHECK(sn::render::mask_traits::from_index(0) == sn::render::mask::none);
STATIC_REQUIRE(sn::render::mask_traits::from_index(1) == sn::render::mask::color); STATIC_CHECK(sn::render::mask_traits::from_index(1) == sn::render::mask::color);
STATIC_REQUIRE(sn::render::mask_traits::from_index(2) == sn::render::mask::alpha); STATIC_CHECK(sn::render::mask_traits::from_index(2) == sn::render::mask::alpha);
STATIC_REQUIRE(sn::render::mask_traits::from_index(3) == sn::render::mask::all); STATIC_CHECK(sn::render::mask_traits::from_index(3) == sn::render::mask::all);
STATIC_REQUIRE_FALSE(sn::render::mask_traits::from_index(42)); STATIC_CHECK_FALSE(sn::render::mask_traits::from_index(42));
} }
{ {
STATIC_REQUIRE(sn::numbers_traits::from_index(10) == sn::_10); STATIC_CHECK(sn::numbers_traits::from_index(10) == sn::_10);
STATIC_REQUIRE(sn::numbers_traits::from_index(240) == sn::_240); STATIC_CHECK(sn::numbers_traits::from_index(240) == sn::_240);
STATIC_REQUIRE_FALSE(sn::numbers_traits::from_index(100500)); STATIC_CHECK_FALSE(sn::numbers_traits::from_index(100500));
STATIC_REQUIRE(enum_hpp::from_index<sn::numbers>(10) == sn::_10); STATIC_CHECK(enum_hpp::from_index<sn::numbers>(10) == sn::_10);
STATIC_REQUIRE(enum_hpp::from_index<sn::numbers>(240) == sn::_240); STATIC_CHECK(enum_hpp::from_index<sn::numbers>(240) == sn::_240);
STATIC_REQUIRE_FALSE(enum_hpp::from_index<sn::numbers>(100500)); STATIC_CHECK_FALSE(enum_hpp::from_index<sn::numbers>(100500));
} }
} }
} }
TEST_CASE("external_enum") { TEST_CASE("external_enum") {
using ee = some_namespace::exns::external_enum; using ee = some_namespace::exns::external_enum;
STATIC_REQUIRE(std::is_same_v<enum_hpp::underlying_type<ee>, unsigned short>); STATIC_CHECK(std::is_same_v<enum_hpp::underlying_type<ee>, unsigned short>);
STATIC_REQUIRE(enum_hpp::size<ee>() == 3); STATIC_CHECK(enum_hpp::size<ee>() == 3);
STATIC_REQUIRE(enum_hpp::names<ee>()[0] == "a"); STATIC_CHECK(enum_hpp::names<ee>()[0] == "a");
STATIC_REQUIRE(enum_hpp::names<ee>()[2] == "c"); STATIC_CHECK(enum_hpp::names<ee>()[2] == "c");
STATIC_REQUIRE(enum_hpp::values<ee>()[0] == ee::a); STATIC_CHECK(enum_hpp::values<ee>()[0] == ee::a);
STATIC_REQUIRE(enum_hpp::values<ee>()[2] == ee::c); STATIC_CHECK(enum_hpp::values<ee>()[2] == ee::c);
STATIC_REQUIRE(enum_hpp::to_string(ee::c) == "c"); STATIC_CHECK(enum_hpp::to_string(ee::c) == "c");
STATIC_REQUIRE(enum_hpp::from_string<ee>("b") == ee::b); STATIC_CHECK(enum_hpp::from_string<ee>("b") == ee::b);
} }