From d9743696562fc85643e016d778ee994f0b9296db Mon Sep 17 00:00:00 2001 From: Johannes Stoelp Date: Sun, 2 Jul 2023 22:33:19 +0200 Subject: add initial state of bitfield and option --- test/option.cc | 89 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 89 insertions(+) create mode 100644 test/option.cc (limited to 'test/option.cc') diff --git a/test/option.cc b/test/option.cc new file mode 100644 index 0000000..8697089 --- /dev/null +++ b/test/option.cc @@ -0,0 +1,89 @@ +#include +#include +#include + +struct Checker { + static unsigned cnt; + + Checker() { + ++cnt; + } + Checker(const Checker&) { + ++cnt; + } + Checker(Checker&&) { + ++cnt; + } + ~Checker() { + --cnt; + } +}; + +unsigned Checker::cnt = 0; + +int main() { + using option::option; + + auto check_cnt = [](unsigned expect) { + if (expect != Checker::cnt) { + std::printf("Checker: expect=%u cnt=%u\n", expect, Checker::cnt); + std::abort(); + } + + }; + + { + option o1; + option o2{::option::none{}}; + + assert(!o1.has_value()); + assert(!o2.has_value()); + } + + // Assume we start test with cnt=0. + check_cnt(0); + + { + option o1(Checker{}); + // copy construct + option o2 = o1; + // move construct + option o3 = o1.take(); + + assert(!o1.has_value()); + assert(o2.has_value()); + assert(o3.has_value()); + + // move option + option o4 = std::move(o2); + + assert(!o2.has_value()); + assert(o4.has_value()); + + // take reference to inner + auto x = o3.value(); + // take ownership of inner + auto y = o4.take(); + + assert(!o1.has_value()); + assert(!o2.has_value()); + assert(o3.has_value()); + assert(!o4.has_value()); + } + + // Expect to finish test with cnt=0. + check_cnt(0); + + { + option o1; + assert(!o1.has_value()); + + o1.emplace(); + assert(o1.has_value()); + } + + // Expect to finish test with cnt=0. + check_cnt(0); + + return 0; +} -- cgit v1.2.3