Skip to content

Commit

Permalink
type security to enumerated string, extra test of non-class enum
Browse files Browse the repository at this point in the history
  • Loading branch information
rboston628 committed Oct 13, 2023
1 parent c1daec5 commit 94ad11d
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 3 deletions.
4 changes: 4 additions & 0 deletions Framework/Kernel/inc/MantidKernel/EnumeratedString.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include <boost/algorithm/string.hpp>
#include <sstream>
#include <string>
#include <type_traits>
#include <vector>

namespace Mantid {
Expand Down Expand Up @@ -38,6 +39,9 @@ class EnumeratedString {
*
* @tparam an optional pointer to a statically defined string comparator.
*/

static_assert(std::is_enum_v<E>); // cause a compiler error if E is not an enum

public:
EnumeratedString() { ensureCompatibleSize(); }

Expand Down
31 changes: 28 additions & 3 deletions Framework/Kernel/test/EnumeratedStringTest.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,12 @@ class EnumeratedStringTest : public CxxTest::TestSuite {

// test constructor from enumerator
COOLGUY dude1(CoolGuys::Fred);
TS_ASSERT_EQUALS(dude1, "Frederic");
TS_ASSERT_EQUALS(dude1, CoolGuys(0));
TS_ASSERT_EQUALS(dude1, CoolGuys::Fred);
TS_ASSERT_EQUALS(dude1, coolGuyNames[0]);
TS_ASSERT_DIFFERS(dude1, CoolGuys::Bill);
TS_ASSERT_DIFFERS(dude1, "Jeremy");

// test constructor from string name
COOLGUY dude2(coolGuyNames[1]);
Expand Down Expand Up @@ -211,7 +213,7 @@ class EnumeratedStringTest : public CxxTest::TestSuite {
// try removing enum_count -- should give a compiler error
enum class Letters : size_t { a, b, enum_count };
static const std::vector<std::string> letters{"a", "b"};
enum Graphia : size_t { alpha, beta, enum_count };
enum class Graphia : size_t { alpha, beta, enum_count };
static const std::vector<std::string> graphia{"alpha", "beta"};
EnumeratedString<Letters, &letters> l1("a");
EnumeratedString<Graphia, &graphia> l2("alpha");
Expand All @@ -232,8 +234,8 @@ class EnumeratedStringTest : public CxxTest::TestSuite {
void testSwitchAndIf() {
g_log.notice("\ntestSwitchAndIf...");

const size_t index = 3;
CAKE tasty = Cakes(index), scrumptious = Cakes(index);
const char index = 3;
CAKE tasty = Cakes(index);

// test enumerated string against string
if (tasty == cakeNames[index]) {
Expand Down Expand Up @@ -262,6 +264,29 @@ class EnumeratedStringTest : public CxxTest::TestSuite {
}
}

void testUnderlyingType() {
g_log.notice("\ntestUnderlyingType...");

// use a non-class enum to compare against chars
enum LetterA : char { a, enum_count };
static const std::vector<std::string> letterA = {"a"};
typedef EnumeratedString<LetterA, &letterA> LETTERA;
LETTERA a1 = a; // note that a is in namespace, vars cannot be named this
TS_ASSERT_EQUALS(a1, a); // compare against enum
TS_ASSERT_EQUALS(a1, "a"); // compares against string
TS_ASSERT_EQUALS(a1, 0); // compare against literal
TS_ASSERT_EQUALS(a1, '\x00'); // char literal at 0
TS_ASSERT_DIFFERS(a1, 'a'); // Letters::a corresponds to 0, not to 'a'= 97
TS_ASSERT_DIFFERS(a1, '\x01'); // char literal at 1
TS_ASSERT_DIFFERS(a1, 1);
switch (a1) {
case '\x00':
break;
default:
TS_FAIL("EnumeratedString in 'SWITCH' failed to match to underlying type");
}
}

void testCaseInsensitiveNameComparison() {
g_log.notice("\ntestCaseInsensitiveNameComparison...");

Expand Down

0 comments on commit 94ad11d

Please sign in to comment.