Skip to content

Commit 03a3bea

Browse files
committed
tests: add case_insensitive_less test
make case_insensitive_less "transparent" so you can use std::string_view for lookups.
1 parent 6f84009 commit 03a3bea

3 files changed

Lines changed: 23 additions & 6 deletions

File tree

common/cmdlib.cc

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -126,11 +126,6 @@ bool case_insensitive_equal::operator()(const std::string &l, const std::string
126126
return Q_strcasecmp(l.c_str(), r.c_str()) == 0;
127127
}
128128

129-
bool case_insensitive_less::operator()(const std::string &l, const std::string &r) const noexcept
130-
{
131-
return Q_strcasecmp(l.c_str(), r.c_str()) < 0;
132-
}
133-
134129
// membuf
135130

136131
membuf::membuf(void *base, size_t size, std::ios_base::openmode which)

include/common/cmdlib.hh

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,9 @@ struct case_insensitive_equal
5050

5151
struct case_insensitive_less
5252
{
53-
bool operator()(const std::string &l, const std::string &r) const noexcept;
53+
using is_transparent = void;
54+
55+
bool operator()(std::string_view a, std::string_view b) const noexcept { return Q_strcasecmp(a, b) < 0; }
5456
};
5557

5658
/**

tests/test_common.cc

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
#include <cstdint>
99
#include <filesystem>
10+
#include <string_view>
1011
#include <common/bspfile.hh>
1112
#include <common/bspfile_q1.hh>
1213
#include <common/bspfile_q2.hh>
@@ -33,6 +34,25 @@ TEST(common, stringIStartsWith)
3334
EXPECT_FALSE(string_istarts_with("asdf", "ASDFX"));
3435
}
3536

37+
TEST(common, caseInsensitiveLess)
38+
{
39+
using namespace std::string_view_literals;
40+
41+
std::map<std::string, int, case_insensitive_less> testmap;
42+
43+
testmap["abc"] = 1;
44+
testmap["def"] = 2;
45+
testmap["ghi"] = 3;
46+
47+
EXPECT_EQ(3, testmap.size());
48+
49+
EXPECT_EQ(1, testmap.find("ABC"sv)->second);
50+
EXPECT_EQ(2, testmap.find("DEF"sv)->second);
51+
EXPECT_EQ(3, testmap.find("GHI"sv)->second);
52+
53+
EXPECT_EQ(testmap.end(), testmap.find("A"));
54+
}
55+
3656
TEST(common, q1Contents)
3757
{
3858
auto *game_q1 = bspver_q1.game;

0 commit comments

Comments
 (0)