diff options
Diffstat (limited to 'test/test-observers.cc')
| -rw-r--r-- | test/test-observers.cc | 94 |
1 files changed, 94 insertions, 0 deletions
diff --git a/test/test-observers.cc b/test/test-observers.cc new file mode 100644 index 0000000..be1418d --- /dev/null +++ b/test/test-observers.cc @@ -0,0 +1,94 @@ +// -*- mode: c++; c-basic-offset: 2; -*- + +#include "common.hh" +#include "test.hh" + +#include "observers.hh" + +namespace { + +bool test_sanity() { + Observers<int> 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<int> 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<int> 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; +} |
