From 64fd70284383f44d2b95c19b0c8bf83d80619c74 Mon Sep 17 00:00:00 2001 From: Alessandro Sidero <75628365+Alessandro624@users.noreply.github.com> Date: Tue, 15 Apr 2025 00:22:31 +0200 Subject: [PATCH] test: add eval and real_value.h in t-eoRankingCached.cpp --- eo/test/t-eoRankingCached.cpp | 64 ++++++++++++++++------------------- 1 file changed, 29 insertions(+), 35 deletions(-) diff --git a/eo/test/t-eoRankingCached.cpp b/eo/test/t-eoRankingCached.cpp index 63fcfd6a8..c4384573f 100644 --- a/eo/test/t-eoRankingCached.cpp +++ b/eo/test/t-eoRankingCached.cpp @@ -2,15 +2,18 @@ #include #include #include +#include +#include "real_value.h" class RankingTest { public: - RankingTest(eoParser &parser, unsigned size = 100) : rng(0), - popSize(size), - seedParam(parser.createParam(uint32_t(time(0)), "seed", "Random seed", 'S')), - pressureParam(parser.createParam(1.5, "pressure", "Selective pressure", 'p')), - exponentParam(parser.createParam(1.0, "exponent", "Ranking exponent", 'e')) + RankingTest(eoParser &parser, eoEvalFuncCounter> &_eval, unsigned size = 100) : rng(0), + popSize(size), + seedParam(parser.createParam(uint32_t(time(0)), "seed", "Random seed", 'S')), + pressureParam(parser.createParam(1.5, "pressure", "Selective pressure", 'p')), + exponentParam(parser.createParam(1.0, "exponent", "Ranking exponent", 'e')), + eval(_eval) { rng.reseed(seedParam.value()); initPopulation(); @@ -19,7 +22,6 @@ public: void initPopulation() { pop.clear(); - pop.resize(popSize); for (unsigned i = 0; i < popSize; ++i) { eoReal ind; @@ -27,6 +29,7 @@ public: ind[0] = rng.uniform(); pop.push_back(ind); } + apply>(eval, pop); } const unsigned popSize; @@ -39,12 +42,15 @@ private: eoValueParam &seedParam; eoValueParam &pressureParam; eoValueParam &exponentParam; + eoEvalFuncCounter> eval; }; // Test case 1: Verify both implementations produce identical results void test_Consistency(eoParser &parser) { - RankingTest fixture(parser); + eoEvalFuncPtr, double, const std::vector &> mainEval(real_value); + eoEvalFuncCounter> eval(mainEval); + RankingTest fixture(parser, eval); eoRanking> ranking(fixture.pressure(), fixture.exponent()); eoRankingCached> rankingCached(fixture.pressure(), fixture.exponent()); @@ -65,28 +71,7 @@ void test_Consistency(eoParser &parser) std::cout << "Test 1 passed: Both implementations produce identical results\n"; } -// Test case 2: Verify ranking order is preserved -void test_RankingOrder(eoParser &parser) -{ - RankingTest fixture(parser); - - eoRankingCached> ranking(fixture.pressure(), fixture.exponent()); - ranking(fixture.pop); - - fixture.pop.sort(); - const std::vector &values = ranking.value(); - - for (unsigned i = 1; i < fixture.pop.size(); ++i) - { - if (values[i] > values[i - 1]) - { - throw std::runtime_error("Ranking order not preserved"); - } - } - std::cout << "Test 2 passed: Ranking order is preserved\n"; -} - -// Test case 3: Test edge case with minimum population size +// Test case 2: Test edge case with minimum population size void test_MinPopulationSize(eoParser &parser) { eoPop> smallPop; @@ -97,11 +82,15 @@ void test_MinPopulationSize(eoParser &parser) ind2[0] = 1.0; smallPop.push_back(ind1); smallPop.push_back(ind2); + eoEvalFuncPtr, double, const std::vector &> mainEval(real_value); + eoEvalFuncCounter> eval(mainEval); - RankingTest fixture(parser, 2); // Use fixture to get parameters + RankingTest fixture(parser, eval, 2); // Use fixture to get parameters eoRanking> ranking(fixture.pressure(), fixture.exponent()); eoRankingCached> rankingCached(fixture.pressure(), fixture.exponent()); + apply>(eval, smallPop); + ranking(smallPop); rankingCached(smallPop); @@ -110,13 +99,15 @@ void test_MinPopulationSize(eoParser &parser) { throw std::runtime_error("Invalid ranking for population size 2"); } - std::cout << "Test 3 passed: Minimum population size handled correctly\n"; + std::cout << "Test 2 passed: Minimum population size handled correctly\n"; } -// Test case 4: Verify caching actually works +// Test case 3: Verify caching actually works void test_CachingEffectiveness(eoParser &parser) { - RankingTest fixture(parser, 50); // Fixed size for cache test + eoEvalFuncPtr, double, const std::vector &> mainEval(real_value); + eoEvalFuncCounter> eval(mainEval); + RankingTest fixture(parser, eval, 50); // Fixed size for cache test eoRankingCached> rankingCached(fixture.pressure(), fixture.exponent()); @@ -128,6 +119,8 @@ void test_CachingEffectiveness(eoParser &parser) for (auto &ind : fixture.pop) ind[0] = fixture.rng.uniform(); + apply>(eval, fixture.pop); + // Second run - should use cached coefficients rankingCached(fixture.pop); @@ -137,10 +130,12 @@ void test_CachingEffectiveness(eoParser &parser) newInd[0] = fixture.rng.uniform(); fixture.pop.push_back(newInd); + apply>(eval, fixture.pop); + // Third run - should recompute coefficients rankingCached(fixture.pop); - std::cout << "Test 4 passed: Caching mechanism properly invalidated\n"; + std::cout << "Test 3 passed: Caching mechanism properly invalidated\n"; } int main(int argc, char **argv) @@ -149,7 +144,6 @@ int main(int argc, char **argv) { eoParser parser(argc, argv); test_Consistency(parser); - test_RankingOrder(parser); test_MinPopulationSize(parser); test_CachingEffectiveness(parser); return 0;