From f97eacf65baa69a352cbc15688081201c7100749 Mon Sep 17 00:00:00 2001 From: s4016080-ps1nov Date: Fri, 30 Aug 2024 00:13:18 +1000 Subject: [PATCH 1/2] Change package and tag naming --- CMakeLists.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 5bc6115c..4f574f34 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -7,7 +7,7 @@ endif() project(mcpp VERSION ${PROJECT_VERSION}) -set(CMAKE_CXX_STANDARD 17) +set(CMAKE_CXX_STANDARD 17) if(NOT CMAKE_BUILD_TYPE) set(CMAKE_BUILD_TYPE Release) From 5c35ac683e9aa479ab32845d11e74d17759465a7 Mon Sep 17 00:00:00 2001 From: Johnathanchann Date: Sun, 16 Feb 2025 16:24:53 +1100 Subject: [PATCH 2/2] tests --- include/mcpp/util.h | 3 +++ src/util.cpp | 14 ++++++++++++++ test/local_tests.cpp | 21 ++++++++++++++++++++- 3 files changed, 37 insertions(+), 1 deletion(-) diff --git a/include/mcpp/util.h b/include/mcpp/util.h index 4e00ccc4..8b8e84da 100644 --- a/include/mcpp/util.h +++ b/include/mcpp/util.h @@ -75,6 +75,9 @@ struct Coordinate { */ [[nodiscard]] Coordinate clone() const; + + std::size_t operator()(const Coordinate& obj) const; + /** * @brief Outputs the Coordinate object to an ostream. * diff --git a/src/util.cpp b/src/util.cpp index 03d08f03..0e316675 100644 --- a/src/util.cpp +++ b/src/util.cpp @@ -40,6 +40,20 @@ Coordinate Coordinate::operator-(const Coordinate& obj) const { result.z = this->z - obj.z; return result; } +std::size_t Coordinate::operator()(const mcpp::Coordinate& obj) const { + // Minecraft coordinate bounds + int lower = -3e7, upper = 3e7; + size_t base = upper - lower + 1; + + // Convert coordinate attributes to non-negative values + size_t nx = obj.x - lower; + size_t ny = obj.y - lower; + size_t nz = obj.z - lower; + + // Combine and weight coordinate values using the boundary range + return nx * base * base + ny * base + nz; +} + Coordinate Coordinate::clone() const { return Coordinate(this->x, this->y, this->z); diff --git a/test/local_tests.cpp b/test/local_tests.cpp index 86d848b8..8ce4c516 100644 --- a/test/local_tests.cpp +++ b/test/local_tests.cpp @@ -3,6 +3,7 @@ #include "../include/mcpp/block.h" #include "../include/mcpp/util.h" #include "doctest.h" +#include using namespace mcpp; @@ -35,7 +36,25 @@ TEST_CASE("Test Coordinate class") { CHECK_EQ(testCoord, testCoordRHS); } - + SUBCASE("Test hash no collision"){ + std::set hashes; + std::random_device rd; + std::mt19937 gen(rd()); + std::uniform_int_distribution dis(-3e7, 3e7); + std::uniform_int_distribution dis2(-64, 256); + + Coordinate hashing; + for (int i = 0; i < 100; i++) { + Coordinate testCoord(dis(gen), dis2(gen), dis(gen)); + size_t hash = hashing(testCoord); + if (hashes.count(hash)) { + FAIL("Collision"); + } + hashes.insert(hash); + } + CHECK_EQ(hashes.size(), 100); + + } SUBCASE("Test not equals") { Coordinate testCoord(3, 2, 1); Coordinate testCoordRHS(2, 2, 1);