#include #include #include using namespace Void::Flags; enum class Perm : unsigned { Read = 1, Write = 2, Exec = 4 }; // ── construction ────────────────────────────────────────────────────────────── Void::Testing::Test flags_default_is_empty() { const Flags f; Void::Testing::expect(f.none()); Void::Testing::expect(!f.any()); } Void::Testing::Test flags_single_bit() { const Flags f{Perm::Read}; Void::Testing::expect(f.test(Perm::Read)); Void::Testing::expect(!f.test(Perm::Write)); } Void::Testing::Test flags_initializer_list() { const Flags f{Perm::Read, Perm::Write}; Void::Testing::expect(f.test(Perm::Read)); Void::Testing::expect(f.test(Perm::Write)); Void::Testing::expect(!f.test(Perm::Exec)); } Void::Testing::Test flags_make_flags() { const auto f = make_flags({Perm::Read, Perm::Exec}); Void::Testing::expect(f.test(Perm::Read)); Void::Testing::expect(!f.test(Perm::Write)); Void::Testing::expect(f.test(Perm::Exec)); } // ── query ───────────────────────────────────────────────────────────────────── Void::Testing::Test flags_all() { const Flags f{Perm::Read, Perm::Write}; Void::Testing::expect(f.all(Flags{Perm::Read})); Void::Testing::expect(f.all(Flags{Perm::Read, Perm::Write})); Void::Testing::expect(!f.all(Flags{Perm::Read, Perm::Write, Perm::Exec})); } Void::Testing::Test flags_any_of() { const Flags f{Perm::Read}; Void::Testing::expect(f.any_of(Flags{Perm::Read, Perm::Exec})); Void::Testing::expect(!f.any_of(Flags{Perm::Write, Perm::Exec})); } // ── mutation ────────────────────────────────────────────────────────────────── Void::Testing::Test flags_set_unset() { Flags f; f.set(Perm::Read); Void::Testing::expect(f.test(Perm::Read)); f.unset(Perm::Read); Void::Testing::expect(!f.test(Perm::Read)); } Void::Testing::Test flags_toggle() { Flags f{Perm::Write}; f.toggle(Perm::Write); Void::Testing::expect(!f.test(Perm::Write)); f.toggle(Perm::Write); Void::Testing::expect(f.test(Perm::Write)); } Void::Testing::Test flags_clear() { Flags f{Perm::Read, Perm::Write, Perm::Exec}; f.clear(); Void::Testing::expect(f.none()); } // ── operators ───────────────────────────────────────────────────────────────── Void::Testing::Test flags_or_operator() { const auto f = Flags{Perm::Read} | Perm::Write; Void::Testing::expect(f.test(Perm::Read)); Void::Testing::expect(f.test(Perm::Write)); } Void::Testing::Test flags_and_operator() { const Flags a{Perm::Read, Perm::Write}; const Flags b{Perm::Write, Perm::Exec}; const auto c = a & b; Void::Testing::expect(!c.test(Perm::Read)); Void::Testing::expect(c.test(Perm::Write)); Void::Testing::expect(!c.test(Perm::Exec)); } Void::Testing::Test flags_xor_operator() { const Flags a{Perm::Read, Perm::Write}; const Flags b{Perm::Write, Perm::Exec}; const auto c = a ^ b; Void::Testing::expect(c.test(Perm::Read)); Void::Testing::expect(!c.test(Perm::Write)); Void::Testing::expect(c.test(Perm::Exec)); } Void::Testing::Test flags_or_two_enums() { const auto f = Perm::Read | Perm::Exec; Void::Testing::expect(f.test(Perm::Read)); Void::Testing::expect(!f.test(Perm::Write)); Void::Testing::expect(f.test(Perm::Exec)); } Void::Testing::Test flags_equality() { const Flags a{Perm::Read, Perm::Write}; const Flags b{Perm::Read, Perm::Write}; const Flags c{Perm::Read}; Void::Testing::expect(a == b); Void::Testing::expect(!(a == c)); } Void::Testing::Test flags_bool_conversion() { const Flags empty; const Flags nonempty{Perm::Read}; Void::Testing::expect(!static_cast(empty)); Void::Testing::expect(static_cast(nonempty)); } Void::Testing::Test flags_raw_value() { const Flags f{Perm::Read, Perm::Exec}; Void::Testing::expect_eq(f.raw(), unsigned{5}); }