Files
scylladb/tests/enum_set_test.cc
Jesse Haber-Kucharsky bbe09a4793 enum_set: Throw on bad mask
`super_enum::valid_is_valid_sequence` determines if the numeric index
corresponding to an enumeration value is valid. This is important,
because it is undefined behavior to cast an invalid index into an
enumeration value.

This function is used to check the validity of the `enum_set` mask when
an `enum_set` is constructed in `enum_set::from_mask`. If the mask has
set bits that correspond to invalid enumeration indicies, then we throw
`bad_enum_set_mask`.
2018-02-14 14:15:59 -05:00

94 lines
2.5 KiB
C++

/*
* Copyright (C) 2018 ScyllaDB
*/
/*
* This file is part of Scylla.
*
* Scylla is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Scylla is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Scylla. If not, see <http://www.gnu.org/licenses/>.
*/
#define BOOST_TEST_MODULE core
#include "enum_set.hh"
#include <boost/test/unit_test.hpp>
enum class fruit { apple = 3, pear = 7, banana = 8 };
using fruit_enum = super_enum<fruit, fruit::apple, fruit::pear, fruit::banana>;
//
// `super_enum`
//
BOOST_AUTO_TEST_CASE(enum_max_sequence) {
BOOST_REQUIRE_EQUAL(fruit_enum::max_sequence, 8);
}
BOOST_AUTO_TEST_CASE(enum_min_sequence) {
BOOST_REQUIRE_EQUAL(fruit_enum::min_sequence, 3);
}
BOOST_AUTO_TEST_CASE(enum_valid_sequence) {
BOOST_REQUIRE(fruit_enum::is_valid_sequence(3));
BOOST_REQUIRE(fruit_enum::is_valid_sequence(7));
BOOST_REQUIRE(fruit_enum::is_valid_sequence(8));
BOOST_REQUIRE(!fruit_enum::is_valid_sequence(0));
BOOST_REQUIRE(!fruit_enum::is_valid_sequence(4));
BOOST_REQUIRE(!fruit_enum::is_valid_sequence(9));
}
//
// `enum_set`
//
using fruit_set = enum_set<fruit_enum>;
BOOST_AUTO_TEST_CASE(set_contains) {
const auto fs = fruit_set::of<fruit::apple, fruit::banana>();
BOOST_REQUIRE(fs.contains(fruit::apple));
BOOST_REQUIRE(!fs.contains(fruit::pear));
}
BOOST_AUTO_TEST_CASE(set_from_mask) {
const auto fs = fruit_set::of<fruit::apple, fruit::banana>();
BOOST_REQUIRE_EQUAL(fs.mask(), fruit_set::from_mask(fs.mask()).mask());
BOOST_REQUIRE_THROW(fruit_set::from_mask(0xdead), bad_enum_set_mask);
}
BOOST_AUTO_TEST_CASE(set_enable) {
auto fs = fruit_set();
fs.set(fruit::apple);
BOOST_REQUIRE(fs.contains(fruit::apple));
}
BOOST_AUTO_TEST_CASE(set_enable_if) {
auto fs = fruit_set();
fs.set_if<fruit::apple>(false);
BOOST_REQUIRE(!fs.contains(fruit::apple));
fs.set_if<fruit::apple>(true);
BOOST_REQUIRE(fs.contains(fruit::apple));
}
BOOST_AUTO_TEST_CASE(set_remove) {
auto fs = fruit_set::of<fruit::pear>();
fs.remove(fruit::pear);
BOOST_REQUIRE(!fs.contains(fruit::pear));
}