// SPDX-License-Identifier: MIT // SPDX-FileCopyrightText: 2026 Martino Ferrari #include "harness.hpp" #include PLOT_TEST(ring_wraps_and_keeps_latest) { plot::data::RingBuffer r(8); // capacity rounded up to a power of two for (int i = 0; i < 20; ++i) r.push(f32(i)); PLOT_CHECK(r.size() == 8); PLOT_CHECK_NEAR(r[0], 12.0); // oldest retained PLOT_CHECK_NEAR(r[7], 19.0); // newest } PLOT_TEST(ring_clear_resets) { plot::data::RingBuffer r(8); for (int i = 0; i < 5; ++i) r.push(f32(i)); r.clear(); PLOT_CHECK(r.size() == 0); r.push(42.0f); PLOT_CHECK(r.size() == 1); PLOT_CHECK_NEAR(r[0], 42.0); } PLOT_TEST(ring_capacity_rounds_up_to_a_power_of_two) { // The capacity is rounded up so slot addressing stays a mask, and capacity() // reports the rounded value -- the one callers must size for (R4). PLOT_CHECK(plot::data::RingBuffer(100).capacity() == 128); PLOT_CHECK(plot::data::RingBuffer(1).capacity() == 1); PLOT_CHECK(plot::data::RingBuffer(0).capacity() == 1); PLOT_CHECK(plot::data::RingBuffer(64).capacity() == 64); // already a power plot::data::RingBuffer r(100); for (int i = 0; i < 1000; ++i) r.push(f32(i)); PLOT_CHECK(r.size() == 128); // size() saturates at the rounded capacity PLOT_CHECK_NEAR(r[0], 872.0); // oldest retained PLOT_CHECK_NEAR(r[127], 999.0); // newest }