aboutsummaryrefslogtreecommitdiff
path: root/test
diff options
context:
space:
mode:
Diffstat (limited to 'test')
-rw-r--r--test/bitfield.cc59
-rw-r--r--test/option.cc89
2 files changed, 148 insertions, 0 deletions
diff --git a/test/bitfield.cc b/test/bitfield.cc
new file mode 100644
index 0000000..77bb286
--- /dev/null
+++ b/test/bitfield.cc
@@ -0,0 +1,59 @@
+#include <bitfield.h>
+
+#include <cassert>
+#include <cstdio>
+
+BITFIELD_START(MOOSE, std::uint32_t)
+ FIELD(IO, 4, 7)
+ FIELD(EL, 16, 31)
+BITFIELD_END()
+
+int main() {
+ MOOSE mo(0xab);
+
+ auto io = mo.IO();
+ auto el = mo.EL();
+
+ assert(mo.val() == 0xab);
+ assert(io.val() == 0xa);
+ assert(el.val() == 0);
+
+ io = 0x0;
+ std::printf("reg %08x f %08x\n", mo.val(), io.val());
+ assert(mo.val() == 0x0b);
+ assert(io.val() == 0x0);
+ assert(el.val() == 0);
+
+ el = 0xfeef;
+ std::printf("reg %08x f %08x\n", mo.val(), el.val());
+ assert(mo.val() == 0xfeef000b);
+ assert(io.val() == 0x0);
+ assert(el.val() == 0xfeef);
+
+ mo = 0xcafeu;
+ assert(mo.val() == 0xcafe);
+ assert(io.val() == 0xf);
+ assert(el.val() == 0x0);
+
+ assert(mo.val() == 0xcafe);
+ mo |= 0xa00b0000;
+ assert(mo.val() == 0xa00bcafe);
+ assert(io.val() == 0xf);
+ assert(el.val() == 0xa00b);
+
+ assert(mo.val() == 0xa00bcafe);
+ mo &= 0xffff0000;
+ assert(mo.val() == 0xa00b0000);
+ assert(io.val() == 0x0);
+ assert(el.val() == 0xa00b);
+
+ el |= 0xc0;
+ assert(mo.val() == 0xa0cb0000);
+ assert(io.val() == 0x0);
+ assert(el.val() == 0xa0cb);
+
+ el &= 0xf000;
+ assert(mo.val() == 0xa0000000);
+ assert(io.val() == 0x0);
+ assert(el.val() == 0xa000);
+}
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 <option.h>
+#include <cstdlib>
+#include <cstdio>
+
+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<int> o1;
+ option<int> o2{::option::none{}};
+
+ assert(!o1.has_value());
+ assert(!o2.has_value());
+ }
+
+ // Assume we start test with cnt=0.
+ check_cnt(0);
+
+ {
+ option<Checker> o1(Checker{});
+ // copy construct
+ option<Checker> o2 = o1;
+ // move construct
+ option<Checker> o3 = o1.take();
+
+ assert(!o1.has_value());
+ assert(o2.has_value());
+ assert(o3.has_value());
+
+ // move option
+ option<Checker> 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<Checker> o1;
+ assert(!o1.has_value());
+
+ o1.emplace();
+ assert(o1.has_value());
+ }
+
+ // Expect to finish test with cnt=0.
+ check_cnt(0);
+
+ return 0;
+}