Skip to content

Commit 7a149c9

Browse files
author
Julian LALU
committed
Fix hashmap tests
1 parent 8f67d3c commit 7a149c9

File tree

1 file changed

+62
-13
lines changed

1 file changed

+62
-13
lines changed

test/hashmap/hashmap_indexed_operator.cpp

Lines changed: 62 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,20 @@ GTEST_TEST(hashmap, indexed_operator_trivial_same_type)
44
{
55
const auto test = []()
66
{
7-
hud::hashmap<const char *, const char *> map;
8-
const char key_0[] = "key";
9-
const char value_0[] = "value";
10-
const char value_1[] = "value_1";
11-
// Should add default value ""
12-
map[key_0];
13-
const bool ok_default = hud::cstring::equals(map.find(key_0)->value(), "");
14-
// Should add "key_0", "value_0"
15-
map[key_0] = value_0;
16-
const bool ok = map.find(key_0)->value() = value_0;
17-
// Should replace value of "key_0" to "value_1"
18-
map[key_0] = value_1;
19-
const bool ok_replace = map.find(key_0)->value() = value_1;
7+
using key_type = u32;
8+
using value_type = u64;
9+
hud::hashmap<key_type, value_type> map;
10+
11+
const key_type key = 123;
12+
// Should add default value 0
13+
map[key];
14+
const bool ok_default = map.find(key)->value() == 0;
15+
// Should add key, 0
16+
map[key] = value_type {1};
17+
const bool ok = map.find(key)->value() == 1;
18+
// Should replace value of key to 2
19+
map[key] = value_type {2};
20+
const bool ok_replace = map.find(key)->value() == 2;
2021

2122
return std::tuple {
2223
ok_default,
@@ -40,3 +41,51 @@ GTEST_TEST(hashmap, indexed_operator_trivial_same_type)
4041
hud_assert_true(std::get<2>(result));
4142
}
4243
}
44+
45+
GTEST_TEST(hashmap, indexed_operator_trivial_different_type)
46+
{
47+
const auto test = []()
48+
{
49+
using key_type = u32;
50+
using value_type = u64;
51+
52+
using other_key_type = i16;
53+
using other_value_type = i8;
54+
static_assert(!hud::is_same_v<key_type, other_key_type>);
55+
static_assert(!hud::is_same_v<value_type, other_value_type>);
56+
57+
hud::hashmap<key_type, value_type> map;
58+
const other_key_type key = 123;
59+
60+
// Should add default value 0
61+
map[key];
62+
const bool ok_default = map.find(key)->value() == 0;
63+
// Should add key, 0
64+
map[key] = other_value_type {1};
65+
const bool ok = map.find(key)->value() == 1;
66+
// Should replace value of key to 2
67+
map[key] = other_value_type {2};
68+
const bool ok_replace = map.find(key)->value() == 2;
69+
70+
return std::tuple {
71+
ok_default,
72+
ok,
73+
ok_replace
74+
};
75+
};
76+
77+
// Non constant
78+
{
79+
const auto result = test();
80+
hud_assert_true(std::get<0>(result));
81+
hud_assert_true(std::get<1>(result));
82+
hud_assert_true(std::get<2>(result));
83+
}
84+
// Constant
85+
{
86+
constexpr auto result = test();
87+
hud_assert_true(std::get<0>(result));
88+
hud_assert_true(std::get<1>(result));
89+
hud_assert_true(std::get<2>(result));
90+
}
91+
}

0 commit comments

Comments
 (0)