add exception tests

This commit is contained in:
Shunsuke Kanda 2021-07-11 13:06:23 +09:00
parent efa105dee6
commit 70414b611b
2 changed files with 38 additions and 10 deletions

View file

@ -12,12 +12,16 @@
#ifdef BC_VECTOR_7 #ifdef BC_VECTOR_7
using bc_vector_type = xcdat::bc_vector_7; using bc_vector_type = xcdat::bc_vector_7;
#define BC_NAME "xcdat::bc_vector_7"
#elif BC_VECTOR_8 #elif BC_VECTOR_8
using bc_vector_type = xcdat::bc_vector_8; using bc_vector_type = xcdat::bc_vector_8;
#define BC_NAME "xcdat::bc_vector_8"
#elif BC_VECTOR_15 #elif BC_VECTOR_15
using bc_vector_type = xcdat::bc_vector_15; using bc_vector_type = xcdat::bc_vector_15;
#define BC_NAME "xcdat::bc_vector_15"
#elif BC_VECTOR_16 #elif BC_VECTOR_16
using bc_vector_type = xcdat::bc_vector_16; using bc_vector_type = xcdat::bc_vector_16;
#define BC_NAME "xcdat::bc_vector_16"
#endif #endif
struct bc_unit { struct bc_unit {
@ -66,14 +70,14 @@ void test_bc_vector(const std::vector<bc_unit>& bc_units, const std::vector<bool
} }
} }
TEST_CASE("Test bc_vector 10K in [0,10K)") { TEST_CASE("Test " BC_NAME " 10K in [0,10K)") {
const std::uint64_t size = 10000; const std::uint64_t size = 10000;
auto bc_units = make_random_units(size, size - 1); auto bc_units = make_random_units(size, size - 1);
auto leaves = xcdat::test::make_random_bits(size, 0.2); auto leaves = xcdat::test::make_random_bits(size, 0.2);
test_bc_vector(bc_units, leaves); test_bc_vector(bc_units, leaves);
} }
TEST_CASE("Test bc_vector 10K in [0,UINT64_MAX)") { TEST_CASE("Test " BC_NAME " 10K in [0,UINT64_MAX)") {
const std::uint64_t size = 10000; const std::uint64_t size = 10000;
auto bc_units = make_random_units(size, UINT64_MAX); auto bc_units = make_random_units(size, UINT64_MAX);
auto leaves = xcdat::test::make_random_bits(size, 0.2); auto leaves = xcdat::test::make_random_bits(size, 0.2);

View file

@ -12,12 +12,16 @@
#ifdef TRIE_7 #ifdef TRIE_7
using trie_type = xcdat::trie_7_type; using trie_type = xcdat::trie_7_type;
#define TRIE_NAME "xcdat::trie_7_type"
#elif TRIE_8 #elif TRIE_8
using trie_type = xcdat::trie_8_type; using trie_type = xcdat::trie_8_type;
#define TRIE_NAME "xcdat::trie_8_type"
#elif TRIE_15 #elif TRIE_15
using trie_type = xcdat::trie_15_type; using trie_type = xcdat::trie_15_type;
#define TRIE_NAME "xcdat::trie_15_type"
#elif TRIE_16 #elif TRIE_16
using trie_type = xcdat::trie_16_type; using trie_type = xcdat::trie_16_type;
#define TRIE_NAME "xcdat::trie_16_type"
#endif #endif
std::vector<std::string> load_strings(const std::string& filepath, char delim = '\n') { std::vector<std::string> load_strings(const std::string& filepath, char delim = '\n') {
@ -148,7 +152,7 @@ void test_io(const trie_type& trie, const std::vector<std::string>& keys, const
std::remove(tmp_filepath); std::remove(tmp_filepath);
} }
TEST_CASE("Test trie_type (tiny)") { TEST_CASE("Test " TRIE_NAME " (tiny)") {
std::vector<std::string> keys = { std::vector<std::string> keys = {
"AirPods", "AirTag", "Mac", "MacBook", "MacBook_Air", "MacBook_Pro", "AirPods", "AirTag", "Mac", "MacBook", "MacBook_Air", "MacBook_Pro",
"Mac_Mini", "Mac_Pro", "iMac", "iPad", "iPhone", "iPhone_SE", "Mac_Mini", "Mac_Pro", "iMac", "iPad", "iPhone", "iPhone_SE",
@ -195,7 +199,27 @@ TEST_CASE("Test trie_type (tiny)") {
test_io(trie, keys, others); test_io(trie, keys, others);
} }
TEST_CASE("Test trie_type (real)") { TEST_CASE("Test " TRIE_NAME " (unsort)") {
std::vector<std::string> keys = {
"AirPods", "AirTag", "Mac", "MacBook", "MacBook_Pro", "MacBook_Air",
"Mac_Mini", "Mac_Pro", "iMac", "iPad", "iPhone", "iPhone_SE",
};
auto func = [&]() { auto trie = trie_type(keys); };
REQUIRE_THROWS_AS(func(), const xcdat::exception&);
}
TEST_CASE("Test " TRIE_NAME " (not unique)") {
std::vector<std::string> keys = {
"AirPods", "AirTag", "Mac", "MacBook", "MacBook", "MacBook_Pro",
"Mac_Mini", "Mac_Pro", "iMac", "iPad", "iPhone", "iPhone_SE",
};
auto func = [&]() { auto trie = trie_type(keys); };
REQUIRE_THROWS_AS(func(), const xcdat::exception&);
}
TEST_CASE("Test " TRIE_NAME " (real)") {
auto keys = xcdat::test::to_unique_vec(load_strings("keys.txt")); auto keys = xcdat::test::to_unique_vec(load_strings("keys.txt"));
auto others = xcdat::test::extract_keys(keys); auto others = xcdat::test::extract_keys(keys);
auto queries = xcdat::test::sample_keys(keys, 100); auto queries = xcdat::test::sample_keys(keys, 100);
@ -210,7 +234,7 @@ TEST_CASE("Test trie_type (real)") {
test_io(trie, keys, others); test_io(trie, keys, others);
} }
TEST_CASE("Test trie_type (random 10K, A--B)") { TEST_CASE("Test " TRIE_NAME " (random 10K, A--B)") {
auto keys = xcdat::test::to_unique_vec(xcdat::test::make_random_keys(10000, 1, 30, 'A', 'B')); auto keys = xcdat::test::to_unique_vec(xcdat::test::make_random_keys(10000, 1, 30, 'A', 'B'));
auto others = xcdat::test::extract_keys(keys); auto others = xcdat::test::extract_keys(keys);
auto queries = xcdat::test::sample_keys(keys, 100); auto queries = xcdat::test::sample_keys(keys, 100);
@ -225,7 +249,7 @@ TEST_CASE("Test trie_type (random 10K, A--B)") {
test_io(trie, keys, others); test_io(trie, keys, others);
} }
TEST_CASE("Test trie_type (random 10K, A--Z)") { TEST_CASE("Test " TRIE_NAME " (random 10K, A--Z)") {
auto keys = xcdat::test::to_unique_vec(xcdat::test::make_random_keys(10000, 1, 30, 'A', 'Z')); auto keys = xcdat::test::to_unique_vec(xcdat::test::make_random_keys(10000, 1, 30, 'A', 'Z'));
auto others = xcdat::test::extract_keys(keys); auto others = xcdat::test::extract_keys(keys);
auto queries = xcdat::test::sample_keys(keys, 100); auto queries = xcdat::test::sample_keys(keys, 100);
@ -240,7 +264,7 @@ TEST_CASE("Test trie_type (random 10K, A--Z)") {
test_io(trie, keys, others); test_io(trie, keys, others);
} }
TEST_CASE("Test trie_type (random 10K, 0x00--0xFF)") { TEST_CASE("Test " TRIE_NAME " (random 10K, 0x00--0xFF)") {
auto keys = xcdat::test::to_unique_vec(xcdat::test::make_random_keys(10000, 1, 30, INT8_MIN, INT8_MAX)); auto keys = xcdat::test::to_unique_vec(xcdat::test::make_random_keys(10000, 1, 30, INT8_MIN, INT8_MAX));
auto others = xcdat::test::extract_keys(keys); auto others = xcdat::test::extract_keys(keys);
auto queries = xcdat::test::sample_keys(keys, 100); auto queries = xcdat::test::sample_keys(keys, 100);
@ -256,7 +280,7 @@ TEST_CASE("Test trie_type (random 10K, 0x00--0xFF)") {
} }
#ifdef NDEBUG #ifdef NDEBUG
TEST_CASE("Test trie_type (random 100K, A--B)") { TEST_CASE("Test " TRIE_NAME " (random 100K, A--B)") {
auto keys = xcdat::test::to_unique_vec(xcdat::test::make_random_keys(100000, 1, 30, 'A', 'B')); auto keys = xcdat::test::to_unique_vec(xcdat::test::make_random_keys(100000, 1, 30, 'A', 'B'));
auto others = xcdat::test::extract_keys(keys); auto others = xcdat::test::extract_keys(keys);
auto queries = xcdat::test::sample_keys(keys, 1000); auto queries = xcdat::test::sample_keys(keys, 1000);
@ -271,7 +295,7 @@ TEST_CASE("Test trie_type (random 100K, A--B)") {
test_io(trie, keys, others); test_io(trie, keys, others);
} }
TEST_CASE("Test trie_type (random 100K, A--Z)") { TEST_CASE("Test " TRIE_NAME " (random 100K, A--Z)") {
auto keys = xcdat::test::to_unique_vec(xcdat::test::make_random_keys(100000, 1, 30, 'A', 'Z')); auto keys = xcdat::test::to_unique_vec(xcdat::test::make_random_keys(100000, 1, 30, 'A', 'Z'));
auto others = xcdat::test::extract_keys(keys); auto others = xcdat::test::extract_keys(keys);
auto queries = xcdat::test::sample_keys(keys, 1000); auto queries = xcdat::test::sample_keys(keys, 1000);
@ -286,7 +310,7 @@ TEST_CASE("Test trie_type (random 100K, A--Z)") {
test_io(trie, keys, others); test_io(trie, keys, others);
} }
TEST_CASE("Test trie_type (random 100K, 0x00--0xFF)") { TEST_CASE("Test " TRIE_NAME " (random 100K, 0x00--0xFF)") {
auto keys = xcdat::test::to_unique_vec(xcdat::test::make_random_keys(100000, 1, 30, INT8_MIN, INT8_MAX)); auto keys = xcdat::test::to_unique_vec(xcdat::test::make_random_keys(100000, 1, 30, INT8_MIN, INT8_MAX));
auto others = xcdat::test::extract_keys(keys); auto others = xcdat::test::extract_keys(keys);
auto queries = xcdat::test::sample_keys(keys, 1000); auto queries = xcdat::test::sample_keys(keys, 1000);