TEST_F(EwmaTest, testInitialValue) { Ewma<FakeClock> ewma(std::chrono::seconds(5), 0); ASSERT_EQ(ewma.estimate(), 0.0); Ewma<FakeClock> ewma2(std::chrono::seconds(5), 50); ASSERT_EQ(ewma2.estimate(), 50.0); }
TEST_F(EwmaTest, testConstantValue) { Ewma<FakeClock> ewma(std::chrono::seconds(5), 0); auto target = 100.0; for (int i = 0; i < 100; i++) { ewma.add(target); FakeClock::advance(std::chrono::seconds(1)); } ASSERT_NEAR(ewma.estimate(), target, target * 0.1 / 100); }
void stats__add_sample(struct stats *self, unsigned long sample) { if (self->nr_samples++ != 0) { self->avg = ewma(self->avg, sample, 9); if (sample > self->max) self->max = sample; if (sample < self->min) self->min = sample; } else self->avg = self->min = self->max = sample; }
int main (void) { struct iw_stat iws; static float qual = 0.00; iw_getstat(&iws); qual = ewma(qual, iws.stat.qual.qual, 0 /* meter decay */ / 100.0); printf("%.2f\n", qual); printf("%d\n", iws.range.max_qual.qual); printf("%0.f%% %.2f %.2f\n", (1e2 * qual)/iws.range.max_qual.qual, iws.dbm.signal, iws.dbm.noise); return 0; }
TEST_F(EwmaTest, testHalfLife) { auto halfLife = std::chrono::seconds(5); auto initial = 100; Ewma<FakeClock> ewma(halfLife, initial); auto previous = ewma.estimate(); auto target2 = 1000.0; FakeClock::advance(halfLife); ewma.add(target2); auto expectedValue = previous + (target2 - previous) / 2; // half of the difference ASSERT_NEAR(ewma.estimate(), expectedValue, expectedValue * 0.1 / 100); }
TEST_F(EwmaTest, testChangeInConstantValue) { Ewma<FakeClock> ewma(std::chrono::seconds(5), 0); auto target = 100.0; for (int i = 0; i < 100; i++) { ewma.add(target); FakeClock::advance(std::chrono::seconds(1)); } auto target2 = 1000.0; for (int i = 0; i < 100; i++) { ewma.add(target2); FakeClock::advance(std::chrono::seconds(1)); } // Should converge to the second target ASSERT_NEAR(ewma.estimate(), target2, target2 * 0.1 / 100); }