fix(cache): Cache properly clears

Cache now clears properly when setting molar abundances
This commit is contained in:
2025-12-07 07:52:08 -05:00
parent 75ba6c8456
commit 51c6a97b47
2 changed files with 21 additions and 2 deletions

View File

@@ -604,6 +604,8 @@ namespace fourdst::composition {
LOG_ERROR(getLogger(), "Molar abundance must be non-negative for symbol {}. Currently it is {}.", species.name(), molar_abundance);
throw exceptions::InvalidCompositionError("Molar abundance must be non-negative, got " + std::to_string(molar_abundance) + " for symbol " + std::string(species.name()) + ".");
}
m_cache.clear();
m_molarAbundances.at(species) = molar_abundance;
}

View File

@@ -451,14 +451,14 @@ TEST_F(compositionTest, hash) {
TEST_F(compositionTest, hashCaching) {
using namespace fourdst::atomic;
std::unordered_map<Species, double> abundances ={
const std::unordered_map<Species, double> abundances ={
{H_1, 0.702},
{He_4, 0.06},
{C_12, 0.001},
{N_14, 0.0005},
{O_16, 0.22},
};
fourdst::composition::Composition a(abundances);
const fourdst::composition::Composition a(abundances);
const auto first_hash_clock_start = std::chrono::high_resolution_clock::now();
(void)a.hash();
@@ -471,4 +471,21 @@ TEST_F(compositionTest, hashCaching) {
const auto second_hash_duration = std::chrono::duration_cast<std::chrono::nanoseconds>(second_hash_clock_end - second_hash_clock_start).count();
EXPECT_LT(second_hash_duration, first_hash_duration);
}
TEST_F(compositionTest, hashStaleing) {
using namespace fourdst::atomic;
const std::unordered_map<Species, double> abundances ={
{H_1, 0.702},
{He_4, 0.06},
{C_12, 0.001},
{N_14, 0.0005},
{O_16, 0.22},
};
fourdst::composition::Composition a(abundances);
const std::size_t hash1 = a.hash();
a.setMolarAbundance("C-12", 0.002);
const std::size_t hash2 = a.hash();
EXPECT_NE(hash1, hash2);
}