diff --git a/message-queue/cpp/src/algorithm.cpp b/message-queue/cpp/src/algorithm.cpp index dab6c4e..b5de1d2 100644 --- a/message-queue/cpp/src/algorithm.cpp +++ b/message-queue/cpp/src/algorithm.cpp @@ -22,7 +22,7 @@ optional FindMaximum( i = pow(2, i-1) + 1; for(auto n : ranges::views::iota(i)) { - auto opt = sample(i); + auto opt = sample(n); if (opt.has_value()) { auto rs = opt.value(); if (prev.has_value() and rs.MessagesPerSecond <= prev.value().MessagesPerSecond) diff --git a/message-queue/cpp/src/algorithm_unittest.cpp b/message-queue/cpp/src/algorithm_unittest.cpp index ea1d856..ec41c6f 100644 --- a/message-queue/cpp/src/algorithm_unittest.cpp +++ b/message-queue/cpp/src/algorithm_unittest.cpp @@ -1,7 +1,55 @@ #include +#include +#include + +#include "algorithm.hpp" +#include "primitives.hpp" + +using namespace std; + +class Sampler { +public: + list calls; + optional operator()(int n) { + cout << "called with n=" << n << endl; + calls.push_back(n); + auto rs = Results(); + switch(n) { + case 1: + rs.Add(WorkerResult(1, 1, chrono::duration(1))); + break; + case 2: + rs.Add(WorkerResult(1, 2, chrono::duration(1))); + break; + case 4: + rs.Add(WorkerResult(1, 3, chrono::duration(1))); + break; + case 8: + rs.Add(WorkerResult(1, 2, chrono::duration(1))); + break; + case 5: + rs.Add(WorkerResult(1, 4, chrono::duration(1))); + break; + case 6: + rs.Add(WorkerResult(1, 3, chrono::duration(1))); + break; + default: + stringstream ss; + ss << "Unexpected N=" << n; + throw runtime_error(ss.str()); + } + + return rs; + } +}; + namespace { TEST(FindMaximumTest, Test) { - EXPECT_TRUE(true); + Sampler s; + auto rs = FindMaximum(ref(s)); + ASSERT_EQ(rs.value().MessagesPerSecond, 4); + list expected = {1, 2, 4, 8, 5, 6}; + ASSERT_EQ(expected, s.calls); } }