// -*- mode: c++; c-basic-offset: 2; -*- #include "common.hh" #include "test.hh" #include "observers.hh" namespace { bool test_sanity() { Observers obs; ASSERT_EQ(false, obs.notify().has_next()); obs.insert(1); auto it = obs.notify(); ASSERT_EQ(true, it.has_next()); ASSERT_EQ(1, it.next()); ASSERT_EQ(false, it.has_next()); obs.erase(2); it = obs.notify(); ASSERT_EQ(true, it.has_next()); ASSERT_EQ(1, it.next()); obs.erase(1); ASSERT_EQ(false, obs.notify().has_next()); return true; } bool test_insert() { Observers obs; auto it = obs.notify(); ASSERT_EQ(false, it.has_next()); obs.insert(1); ASSERT_EQ(true, it.has_next()); ASSERT_EQ(1, it.next()); obs.insert(2); ASSERT_EQ(false, it.has_next()); it = obs.notify(); ASSERT_EQ(true, it.has_next()); int other; switch (it.next()) { case 1: other = 2; break; case 2: other = 1; break; default: ASSERT_TRUE(false); } ASSERT_EQ(true, it.has_next()); ASSERT_EQ(other, it.next()); return true; } bool test_erase() { Observers obs; auto it = obs.notify(); ASSERT_EQ(false, it.has_next()); obs.insert(1); ASSERT_EQ(true, it.has_next()); ASSERT_EQ(1, it.next()); ASSERT_EQ(false, it.has_next()); obs.erase(1); auto it2 = obs.notify(); ASSERT_EQ(false, it2.has_next()); it = it2; obs.insert(4); obs.insert(3); obs.insert(2); it = obs.notify(); ASSERT_EQ(true, it.has_next()); ASSERT_EQ(2, it.next()); obs.erase(2); it2 = obs.notify(); ASSERT_EQ(true, it2.has_next()); ASSERT_EQ(3, it2.next()); obs.erase(3); ASSERT_EQ(true, it.has_next()); ASSERT_EQ(4, it.next()); ASSERT_EQ(true, it2.has_next()); ASSERT_EQ(4, it2.next()); return true; } } // namespace int main(void) { BEFORE; RUN(test_sanity()); RUN(test_insert()); RUN(test_erase()); AFTER; }