From 8875b9acdd624d2158f82acf6e2101091ea49f3b Mon Sep 17 00:00:00 2001 From: Minh Vu Date: Sat, 27 Jun 2026 23:05:19 +0200 Subject: [PATCH] Fix per-process cache key equality --- cpp/src/cache/image_cache_per_process.cpp | 2 +- cpp/tests/CMakeLists.txt | 1 + cpp/tests/test_image_cache.cpp | 50 +++++++++++++++++++++++ 3 files changed, 52 insertions(+), 1 deletion(-) create mode 100644 cpp/tests/test_image_cache.cpp diff --git a/cpp/src/cache/image_cache_per_process.cpp b/cpp/src/cache/image_cache_per_process.cpp index e5fdb60b3..6c5db2639 100644 --- a/cpp/src/cache/image_cache_per_process.cpp +++ b/cpp/src/cache/image_cache_per_process.cpp @@ -25,7 +25,7 @@ size_t hash>::operator()( bool equal_to>::operator()( const std::shared_ptr& lhs, const std::shared_ptr& rhs) const { - return lhs->location_hash == rhs->location_hash; + return lhs->file_hash == rhs->file_hash && lhs->location_hash == rhs->location_hash; } } // namespace std diff --git a/cpp/tests/CMakeLists.txt b/cpp/tests/CMakeLists.txt index 4b96412dc..2f9fa0b9c 100644 --- a/cpp/tests/CMakeLists.txt +++ b/cpp/tests/CMakeLists.txt @@ -15,6 +15,7 @@ add_executable(cucim_tests main.cpp test_read_region.cpp test_cufile.cpp + test_image_cache.cpp test_metadata.cpp ) diff --git a/cpp/tests/test_image_cache.cpp b/cpp/tests/test_image_cache.cpp new file mode 100644 index 000000000..0a83b0365 --- /dev/null +++ b/cpp/tests/test_image_cache.cpp @@ -0,0 +1,50 @@ +/* + * SPDX-FileCopyrightText: Copyright (c) 2026, NVIDIA CORPORATION. + * SPDX-License-Identifier: Apache-2.0 + */ + +#include "cucim/cache/image_cache_manager.h" + +#include +#include + +namespace +{ + +std::shared_ptr create_cache_value(cucim::cache::ImageCache& cache, uint8_t byte) +{ + auto* data = static_cast(cache.allocate(1)); + REQUIRE(data != nullptr); + data[0] = byte; + return cache.create_value(data, 1, cucim::io::DeviceType::kCPU); +} + +} // namespace + +TEST_CASE("Per-process image cache should distinguish file hash for matching tile locations", "[test_image_cache.cpp]") +{ + cucim::cache::ImageCacheConfig config{}; + config.type = cucim::cache::CacheType::kPerProcess; + config.capacity = 4; + config.memory_capacity = 1; + config.list_padding = 1; + + auto cache = cucim::cache::ImageCacheManager::create_cache(config, cucim::io::DeviceType::kCPU); + + auto first_key = cache->create_key(0x101, 42); + auto second_key = cache->create_key(0x202, 42); + auto first_value = create_cache_value(*cache, 0x11); + auto second_value = create_cache_value(*cache, 0x22); + + REQUIRE(cache->insert(first_key, first_value)); + REQUIRE(cache->insert(second_key, second_value)); + + auto found_first = cache->find(first_key); + auto found_second = cache->find(second_key); + + REQUIRE(found_first); + REQUIRE(found_second); + REQUIRE(found_first != found_second); + REQUIRE(static_cast(found_first->data)[0] == 0x11); + REQUIRE(static_cast(found_second->data)[0] == 0x22); +}