aboutsummaryrefslogtreecommitdiff
path: root/test/bitfield.cc
diff options
context:
space:
mode:
authorJohannes Stoelp <johannes.stoelp@gmail.com>2023-07-02 22:33:19 +0200
committerJohannes Stoelp <johannes.stoelp@gmail.com>2023-07-02 22:33:19 +0200
commitd9743696562fc85643e016d778ee994f0b9296db (patch)
tree4cf08da0eb8569e227a412a6da45d1c476ffff78 /test/bitfield.cc
downloadcpp-utils-d9743696562fc85643e016d778ee994f0b9296db.tar.gz
cpp-utils-d9743696562fc85643e016d778ee994f0b9296db.zip
add initial state of bitfield and option
Diffstat (limited to 'test/bitfield.cc')
-rw-r--r--test/bitfield.cc59
1 files changed, 59 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);
+}