Skip to content

Commit

Permalink
util: add tests for list_of_keys.
Browse files Browse the repository at this point in the history
The main case we wanted to cover, std::unordered_map with multiple
entries, requires a non-ideal test function, because it isn't
deterministic by design.
  • Loading branch information
ericonr committed Dec 12, 2023
1 parent 5810124 commit 89467de
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 0 deletions.
46 changes: 46 additions & 0 deletions util/tests/list-of-keys-test.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#include <tuple>

#include <catch2/catch_test_macros.hpp>

#include "util.h"

TEST_CASE("std::unordered_map with multiple entries", "[list-of-keys]")
{
std::unordered_map<std::string_view, int> map = {
{"first", 0},
{"second", 0},
};

auto s = list_of_keys(map);
auto rv = s == "first, second" || s == "second, first";
REQUIRE(rv == true);
}

TEST_CASE("std::unordered_map with single entry", "[list-of-keys]")
{
std::unordered_map<std::string_view, int> map = {
{"first", 0},
};

REQUIRE(list_of_keys(map) == "first");
}

TEST_CASE("std::vector with multiple entries", "[list-of-keys]")
{
std::vector<std::string> vec = {
"first",
"second",
"third",
};

REQUIRE(list_of_keys(vec) == "first, second, third");
}

TEST_CASE("std::vector with single entry", "[list-of-keys]")
{
std::vector<std::string> vec = {
"first",
};

REQUIRE(list_of_keys(vec) == "first");
}
3 changes: 3 additions & 0 deletions util/tests/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,8 @@ test_util_lib = static_library('test-util', test_util_src, dependencies: [utilit
copy_test = executable('copy-test', 'copy-test.cc', link_with: [test_util_lib], dependencies: [thread_dep, utilities])
test('copy-test', copy_test)

list_of_keys_test = executable('list-of-keys-test', 'list-of-keys-test.cc', dependencies: [utilities, catch2])
test('list-of-keys-test', list_of_keys_test)

fixed_test = executable('fixed-test', 'fixed-test.cc', dependencies: [utilities, catch2])
test('fixed-test', fixed_test)

0 comments on commit 89467de

Please sign in to comment.