-
-
Notifications
You must be signed in to change notification settings - Fork 650
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
Printable enum support Co-authored-by: Joshua Kriegshauser <Joshuakr@nvidia.com>
- Loading branch information
1 parent
301a104
commit 961a542
Showing
9 changed files
with
495 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
#include <doctest/doctest.h> | ||
|
||
#include "header.h" | ||
|
||
DOCTEST_MAKE_STD_HEADERS_CLEAN_FROM_WARNINGS_ON_WALL_BEGIN | ||
#include <cstdint> | ||
#include <sstream> | ||
DOCTEST_MAKE_STD_HEADERS_CLEAN_FROM_WARNINGS_ON_WALL_END | ||
|
||
namespace | ||
{ | ||
|
||
enum StandardEnum | ||
{ | ||
Zero, | ||
One, | ||
Two, | ||
}; | ||
|
||
enum TypedEnum : int64_t | ||
{ | ||
TypedZero, | ||
TypedOne, | ||
TypedTwo, | ||
}; | ||
|
||
enum class EnumClassC : char | ||
{ | ||
Zero = '0', | ||
One = '1', | ||
Two = '2', | ||
}; | ||
|
||
enum class EnumClassSC : signed char | ||
{ | ||
Zero = '0', | ||
One = '1', | ||
Two = '2', | ||
}; | ||
|
||
enum class EnumClassUC : unsigned char | ||
{ | ||
Zero = '0', | ||
One = '1', | ||
Two = '2', | ||
}; | ||
|
||
enum class EnumClassU8 : uint8_t | ||
{ | ||
Zero, | ||
One, | ||
Two, | ||
}; | ||
|
||
template<class E, class T = typename std::underlying_type<E>::type> | ||
T printable(E val) | ||
{ | ||
return T(val); | ||
} | ||
|
||
} | ||
|
||
TEST_CASE("enum 1") | ||
{ | ||
std::ostringstream ostr; | ||
ostr << Zero << One << Two; | ||
ostr << TypedZero << TypedOne << TypedTwo; | ||
static_assert(std::is_enum<EnumClassSC>::value, ""); | ||
ostr << printable(EnumClassSC::Zero) << printable(EnumClassSC::One) << printable(EnumClassSC::Two); | ||
|
||
CHECK_EQ(Zero, 0); | ||
CHECK_EQ(One, 1); | ||
CHECK_EQ(Two, 2); | ||
|
||
CHECK_EQ(TypedZero, 0); | ||
CHECK_EQ(TypedOne, 1); | ||
CHECK_EQ(TypedTwo, 2); | ||
|
||
CHECK_EQ(EnumClassSC::Zero, EnumClassSC::Zero); | ||
CHECK_EQ(EnumClassSC::One, EnumClassSC::One); | ||
CHECK_EQ(EnumClassSC::Two, EnumClassSC::Two); | ||
} | ||
|
||
TEST_CASE("enum 2" * doctest::should_fail()) | ||
{ | ||
CHECK_EQ(Zero, 1); | ||
CHECK_EQ(One, 2); | ||
CHECK_EQ(Two, 3); | ||
|
||
CHECK_EQ(TypedZero, 1); | ||
CHECK_EQ(TypedOne, 2); | ||
CHECK_EQ(TypedTwo, 3); | ||
|
||
CHECK_EQ(EnumClassC::Zero, EnumClassC::One); | ||
CHECK_EQ(EnumClassC::One, EnumClassC::Two); | ||
CHECK_EQ(EnumClassC::Two, EnumClassC::Zero); | ||
|
||
CHECK_EQ(EnumClassSC::Zero, EnumClassSC::One); | ||
CHECK_EQ(EnumClassSC::One, EnumClassSC::Two); | ||
CHECK_EQ(EnumClassSC::Two, EnumClassSC::Zero); | ||
|
||
CHECK_EQ(EnumClassUC::Zero, EnumClassUC::One); | ||
CHECK_EQ(EnumClassUC::One, EnumClassUC::Two); | ||
CHECK_EQ(EnumClassUC::Two, EnumClassUC::Zero); | ||
|
||
CHECK_EQ(EnumClassU8::Zero, EnumClassU8::One); | ||
CHECK_EQ(EnumClassU8::One, EnumClassU8::Two); | ||
CHECK_EQ(EnumClassU8::Two, EnumClassU8::Zero); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
[doctest] run with "--help" for options | ||
=============================================================================== | ||
enums.cpp(0): | ||
TEST CASE: enum 2 | ||
|
||
enums.cpp(0): ERROR: CHECK_EQ( Zero, 1 ) is NOT correct! | ||
values: CHECK_EQ( 0, 1 ) | ||
|
||
enums.cpp(0): ERROR: CHECK_EQ( One, 2 ) is NOT correct! | ||
values: CHECK_EQ( 1, 2 ) | ||
|
||
enums.cpp(0): ERROR: CHECK_EQ( Two, 3 ) is NOT correct! | ||
values: CHECK_EQ( 2, 3 ) | ||
|
||
enums.cpp(0): ERROR: CHECK_EQ( TypedZero, 1 ) is NOT correct! | ||
values: CHECK_EQ( 0, 1 ) | ||
|
||
enums.cpp(0): ERROR: CHECK_EQ( TypedOne, 2 ) is NOT correct! | ||
values: CHECK_EQ( 1, 2 ) | ||
|
||
enums.cpp(0): ERROR: CHECK_EQ( TypedTwo, 3 ) is NOT correct! | ||
values: CHECK_EQ( 2, 3 ) | ||
|
||
enums.cpp(0): ERROR: CHECK_EQ( EnumClassC::Zero, EnumClassC::One ) is NOT correct! | ||
values: CHECK_EQ( 48, 49 ) | ||
|
||
enums.cpp(0): ERROR: CHECK_EQ( EnumClassC::One, EnumClassC::Two ) is NOT correct! | ||
values: CHECK_EQ( 49, 50 ) | ||
|
||
enums.cpp(0): ERROR: CHECK_EQ( EnumClassC::Two, EnumClassC::Zero ) is NOT correct! | ||
values: CHECK_EQ( 50, 48 ) | ||
|
||
enums.cpp(0): ERROR: CHECK_EQ( EnumClassSC::Zero, EnumClassSC::One ) is NOT correct! | ||
values: CHECK_EQ( 48, 49 ) | ||
|
||
enums.cpp(0): ERROR: CHECK_EQ( EnumClassSC::One, EnumClassSC::Two ) is NOT correct! | ||
values: CHECK_EQ( 49, 50 ) | ||
|
||
enums.cpp(0): ERROR: CHECK_EQ( EnumClassSC::Two, EnumClassSC::Zero ) is NOT correct! | ||
values: CHECK_EQ( 50, 48 ) | ||
|
||
enums.cpp(0): ERROR: CHECK_EQ( EnumClassUC::Zero, EnumClassUC::One ) is NOT correct! | ||
values: CHECK_EQ( 48, 49 ) | ||
|
||
enums.cpp(0): ERROR: CHECK_EQ( EnumClassUC::One, EnumClassUC::Two ) is NOT correct! | ||
values: CHECK_EQ( 49, 50 ) | ||
|
||
enums.cpp(0): ERROR: CHECK_EQ( EnumClassUC::Two, EnumClassUC::Zero ) is NOT correct! | ||
values: CHECK_EQ( 50, 48 ) | ||
|
||
enums.cpp(0): ERROR: CHECK_EQ( EnumClassU8::Zero, EnumClassU8::One ) is NOT correct! | ||
values: CHECK_EQ( 0, 1 ) | ||
|
||
enums.cpp(0): ERROR: CHECK_EQ( EnumClassU8::One, EnumClassU8::Two ) is NOT correct! | ||
values: CHECK_EQ( 1, 2 ) | ||
|
||
enums.cpp(0): ERROR: CHECK_EQ( EnumClassU8::Two, EnumClassU8::Zero ) is NOT correct! | ||
values: CHECK_EQ( 2, 0 ) | ||
|
||
Failed as expected so marking it as not failed | ||
=============================================================================== | ||
[doctest] test cases: 2 | 2 passed | 0 failed | | ||
[doctest] assertions: 27 | 9 passed | 18 failed | | ||
[doctest] Status: SUCCESS! | ||
Program code. |
Oops, something went wrong.