Skip to content

Commit

Permalink
Add hashmap tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Julian LALU committed Dec 20, 2024
1 parent 4cc9734 commit a97f1ed
Show file tree
Hide file tree
Showing 2 changed files with 202 additions and 92 deletions.
1 change: 1 addition & 0 deletions interface/core/memory/memory.h
Original file line number Diff line number Diff line change
Expand Up @@ -500,6 +500,7 @@ namespace hud
/**
* Sets the first `size` bytes of the block of memory pointed to by `destination` to the specified `value`.
* This safe version ensures that the operation is not removed by the compiler due to optimization.
* `destination` is volatile to prevent memset to be remove by the compiler optimisation
* @param destination Pointer to the memory buffer. Must not be null.
* @param size Number of bytes to set to the specified `value`.
* @param value The value to be set in the buffer.
Expand Down
293 changes: 201 additions & 92 deletions test/hashmap/hashmap_insert.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -307,107 +307,216 @@ GTEST_TEST(hashmap, add_by_copy_construct_bitwise_copy_constructible_type)

static_assert(hud::is_bitwise_copy_constructible_v<key_type, value_type>);

const auto test = []()
// With reallocation
{
hashmap_type map;
const auto test = []()
{
hashmap_type map;

const auto it_1st = map.add(1, 11);
const auto it_1st = map.add(1, 11);

const auto first_element_result = std::tuple {
*it_1st,
map.count(),
map.max_count(),
map.allocator().allocation_count(),
map.allocator().free_count(),
};
const auto first_element_result = std::tuple {
*it_1st,
map.count(),
map.max_count(),
map.allocator().allocation_count(),
map.allocator().free_count(),
};

const auto it_same = map.add(1, 00);
// Add same element
const auto same_element_result = std::tuple {
*it_same,
map.count(),
map.max_count(),
map.allocator().allocation_count(),
map.allocator().free_count(),
};
const auto it_same = map.add(1, 00);
// Add same element
const auto same_element_result = std::tuple {
*it_same,
map.count(),
map.max_count(),
map.allocator().allocation_count(),
map.allocator().free_count(),
};

const auto it_2nd = map.add(2, 22);
// Add 2nd element
const auto second_element_result = std::tuple {
*it_2nd,
map.count(),
map.max_count(),
map.allocator().allocation_count(),
map.allocator().free_count(),
};
const auto it_2nd = map.add(2, 22);
// Add 2nd element
const auto second_element_result = std::tuple {
*it_2nd,
map.count(),
map.max_count(),
map.allocator().allocation_count(),
map.allocator().free_count(),
};

return std::tuple {
first_element_result,
same_element_result,
second_element_result
return std::tuple {
first_element_result,
same_element_result,
second_element_result
};
};
};

// Non Constant
{
const auto result = test();

// First element is correctly added
const auto first_element_result = std::get<0>(result);
hud_assert_eq(std::get<0>(first_element_result).key(), 1u);
hud_assert_eq(std::get<0>(first_element_result).value(), 11u);
hud_assert_eq(std::get<1>(first_element_result), 1u);
hud_assert_eq(std::get<2>(first_element_result), 1u);
hud_assert_eq(std::get<3>(first_element_result), 1u);
hud_assert_eq(std::get<4>(first_element_result), 0u);

// Same element
const auto same_element_result = std::get<1>(result);
hud_assert_eq(std::get<0>(same_element_result).key(), 1u);
hud_assert_eq(std::get<0>(same_element_result).value(), 11u);
hud_assert_eq(std::get<1>(same_element_result), 1u);
hud_assert_eq(std::get<2>(same_element_result), 1u);
hud_assert_eq(std::get<3>(same_element_result), 1u);
hud_assert_eq(std::get<4>(same_element_result), 0u);

// 2nd element
const auto second_element_result = std::get<2>(result);
hud_assert_eq(std::get<0>(second_element_result).key(), 2u);
hud_assert_eq(std::get<0>(second_element_result).value(), 22u);
hud_assert_eq(std::get<1>(second_element_result), 2u);
hud_assert_eq(std::get<2>(second_element_result), 3u);
hud_assert_eq(std::get<3>(second_element_result), 2u);
hud_assert_eq(std::get<4>(second_element_result), 1u);
// Non Constant
{
const auto result = test();

// First element is correctly added
const auto first_element_result = std::get<0>(result);
hud_assert_eq(std::get<0>(first_element_result).key(), 1u);
hud_assert_eq(std::get<0>(first_element_result).value(), 11u);
hud_assert_eq(std::get<1>(first_element_result), 1u);
hud_assert_eq(std::get<2>(first_element_result), 1u);
hud_assert_eq(std::get<3>(first_element_result), 1u);
hud_assert_eq(std::get<4>(first_element_result), 0u);

// Same element
const auto same_element_result = std::get<1>(result);
hud_assert_eq(std::get<0>(same_element_result).key(), 1u);
hud_assert_eq(std::get<0>(same_element_result).value(), 11u);
hud_assert_eq(std::get<1>(same_element_result), 1u);
hud_assert_eq(std::get<2>(same_element_result), 1u);
hud_assert_eq(std::get<3>(same_element_result), 1u);
hud_assert_eq(std::get<4>(same_element_result), 0u);

// 2nd element
const auto second_element_result = std::get<2>(result);
hud_assert_eq(std::get<0>(second_element_result).key(), 2u);
hud_assert_eq(std::get<0>(second_element_result).value(), 22u);
hud_assert_eq(std::get<1>(second_element_result), 2u);
hud_assert_eq(std::get<2>(second_element_result), 3u);
hud_assert_eq(std::get<3>(second_element_result), 2u);
hud_assert_eq(std::get<4>(second_element_result), 1u);
}
// Constant
{
constexpr auto result = test();

// First element is correctly added
const auto first_element_result = std::get<0>(result);
hud_assert_eq(std::get<0>(first_element_result).key(), 1u);
hud_assert_eq(std::get<0>(first_element_result).value(), 11u);
hud_assert_eq(std::get<1>(first_element_result), 1u);
hud_assert_eq(std::get<2>(first_element_result), 1u);
hud_assert_eq(std::get<3>(first_element_result), 2u);
hud_assert_eq(std::get<4>(first_element_result), 0u);

// Same element
const auto same_element_result = std::get<1>(result);
hud_assert_eq(std::get<0>(same_element_result).key(), 1u);
hud_assert_eq(std::get<0>(same_element_result).value(), 11u);
hud_assert_eq(std::get<1>(same_element_result), 1u);
hud_assert_eq(std::get<2>(same_element_result), 1u);
hud_assert_eq(std::get<3>(same_element_result), 2u);
hud_assert_eq(std::get<4>(same_element_result), 0u);

// 2nd element
const auto second_element_result = std::get<2>(result);
hud_assert_eq(std::get<0>(second_element_result).key(), 2u);
hud_assert_eq(std::get<0>(second_element_result).value(), 22u);
hud_assert_eq(std::get<1>(second_element_result), 2u);
hud_assert_eq(std::get<2>(second_element_result), 3u);
hud_assert_eq(std::get<3>(second_element_result), 4u);
hud_assert_eq(std::get<4>(second_element_result), 2u);
}
}
// Constant
// Without reallocation
{
constexpr auto result = test();

// First element is correctly added
const auto first_element_result = std::get<0>(result);
hud_assert_eq(std::get<0>(first_element_result).key(), 1u);
hud_assert_eq(std::get<0>(first_element_result).value(), 11u);
hud_assert_eq(std::get<1>(first_element_result), 1u);
hud_assert_eq(std::get<2>(first_element_result), 1u);
hud_assert_eq(std::get<3>(first_element_result), 2u);
hud_assert_eq(std::get<4>(first_element_result), 0u);

// Same element
const auto same_element_result = std::get<1>(result);
hud_assert_eq(std::get<0>(same_element_result).key(), 1u);
hud_assert_eq(std::get<0>(same_element_result).value(), 11u);
hud_assert_eq(std::get<1>(same_element_result), 1u);
hud_assert_eq(std::get<2>(same_element_result), 1u);
hud_assert_eq(std::get<3>(same_element_result), 2u);
hud_assert_eq(std::get<4>(same_element_result), 0u);

// 2nd element
const auto second_element_result = std::get<2>(result);
hud_assert_eq(std::get<0>(second_element_result).key(), 2u);
hud_assert_eq(std::get<0>(second_element_result).value(), 22u);
hud_assert_eq(std::get<1>(second_element_result), 2u);
hud_assert_eq(std::get<2>(second_element_result), 3u);
hud_assert_eq(std::get<3>(second_element_result), 4u);
hud_assert_eq(std::get<4>(second_element_result), 2u);
const auto test = []()
{
hashmap_type map;
map.reserve(3);
const auto it_1st = map.add(1, 11);

const auto first_element_result = std::tuple {
*it_1st,
map.count(),
map.max_count(),
map.allocator().allocation_count(),
map.allocator().free_count(),
};

const auto it_same = map.add(1, 00);
// Add same element
const auto same_element_result = std::tuple {
*it_same,
map.count(),
map.max_count(),
map.allocator().allocation_count(),
map.allocator().free_count(),
};

const auto it_2nd = map.add(2, 22);
// Add 2nd element
const auto second_element_result = std::tuple {
*it_2nd,
map.count(),
map.max_count(),
map.allocator().allocation_count(),
map.allocator().free_count(),
};

return std::tuple {
first_element_result,
same_element_result,
second_element_result
};
};

// Non Constant
{
const auto result = test();

// First element is correctly added
const auto first_element_result = std::get<0>(result);
hud_assert_eq(std::get<0>(first_element_result).key(), 1u);
hud_assert_eq(std::get<0>(first_element_result).value(), 11u);
hud_assert_eq(std::get<1>(first_element_result), 1u);
hud_assert_eq(std::get<2>(first_element_result), 3u);
hud_assert_eq(std::get<3>(first_element_result), 1u);
hud_assert_eq(std::get<4>(first_element_result), 0u);

// Same element
const auto same_element_result = std::get<1>(result);
hud_assert_eq(std::get<0>(same_element_result).key(), 1u);
hud_assert_eq(std::get<0>(same_element_result).value(), 11u);
hud_assert_eq(std::get<1>(same_element_result), 1u);
hud_assert_eq(std::get<2>(same_element_result), 3u);
hud_assert_eq(std::get<3>(same_element_result), 1u);
hud_assert_eq(std::get<4>(same_element_result), 0u);

// 2nd element
const auto second_element_result = std::get<2>(result);
hud_assert_eq(std::get<0>(second_element_result).key(), 2u);
hud_assert_eq(std::get<0>(second_element_result).value(), 22u);
hud_assert_eq(std::get<1>(second_element_result), 2u);
hud_assert_eq(std::get<2>(second_element_result), 3u);
hud_assert_eq(std::get<3>(second_element_result), 2u);
hud_assert_eq(std::get<4>(second_element_result), 1u);
}
// Constant
{
constexpr auto result = test();

// First element is correctly added
const auto first_element_result = std::get<0>(result);
hud_assert_eq(std::get<0>(first_element_result).key(), 1u);
hud_assert_eq(std::get<0>(first_element_result).value(), 11u);
hud_assert_eq(std::get<1>(first_element_result), 1u);
hud_assert_eq(std::get<2>(first_element_result), 3u);
hud_assert_eq(std::get<3>(first_element_result), 2u);
hud_assert_eq(std::get<4>(first_element_result), 0u);

// Same element
const auto same_element_result = std::get<1>(result);
hud_assert_eq(std::get<0>(same_element_result).key(), 1u);
hud_assert_eq(std::get<0>(same_element_result).value(), 11u);
hud_assert_eq(std::get<1>(same_element_result), 1u);
hud_assert_eq(std::get<2>(same_element_result), 3u);
hud_assert_eq(std::get<3>(same_element_result), 2u);
hud_assert_eq(std::get<4>(same_element_result), 0u);

// 2nd element
const auto second_element_result = std::get<2>(result);
hud_assert_eq(std::get<0>(second_element_result).key(), 2u);
hud_assert_eq(std::get<0>(second_element_result).value(), 22u);
hud_assert_eq(std::get<1>(second_element_result), 2u);
hud_assert_eq(std::get<2>(second_element_result), 3u);
hud_assert_eq(std::get<3>(second_element_result), 4u);
hud_assert_eq(std::get<4>(second_element_result), 2u);
}
}
}

0 comments on commit a97f1ed

Please sign in to comment.