libcruft-util/test/stringid.cpp

33 lines
881 B
C++
Raw Normal View History

2015-04-13 16:45:56 +10:00
#include "stringid.hpp"
#include "debug.hpp"
#include "tap.hpp"
#include <cstdlib>
#include <stdexcept>
int
main (int, char**) {
cruft::TAP::logger tap;
2016-01-19 18:31:49 +11:00
cruft::stringid map;
2016-01-19 18:31:49 +11:00
tap.expect_throw<std::out_of_range> ([&] { map.find ("invalid"); }, "find on empty set throws");
auto id1 = map.add ("first");
2016-01-19 18:31:49 +11:00
tap.expect_eq (id1, map.find ("first"), "single entity ID matches");
2016-01-19 18:31:49 +11:00
tap.expect_throw<std::out_of_range> ([&] { map.find ("invalid"); }, "invalid find throws");
auto id2 = map.add ("second");
2016-01-19 18:31:49 +11:00
tap.expect_eq (id1 + 1, id2, "monotonically increasing IDs");
tap.expect_eq (id1, map.find ("first"), "first element still matches");
tap.expect_eq (map["second"], id2, "index lookup invokes no change");
auto id3 = map["third"];
tap.expect (id3 != id1 && id3 != id2, "index creation adds new value");
2016-01-19 18:31:49 +11:00
return tap.status ();
}