Files
Library/tests/standalone/Bitset/Bitset.cpp
T
faumarayandClaude Sonnet 4.6 58ff7b38a0 Add 20+ new modules with tests: Pipeline, Matrix, Tokenizer, ScopeGuard, Bitset, Optional, Counted, Curry, TypeMap, Bench, DisjointSet, Histogram, StopWatch, and more
Also fixes Diff/Memo/PriorityQueue from previous session: LineDiff is now
an alias for Edit<string> so count_op/apply work on line diffs; Memoized
uses std::apply instead of type-based get to handle duplicate arg types.

874/877 tests pass (3 are platform-gated plugin tests).

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-05-14 23:49:09 +03:00

199 lines
5.5 KiB
C++

#include <Void/Bitset/Bitset.hpp>
#include <Void/Testing/Assert.hpp>
#include <Void/Testing/Test.hpp>
#include <string>
using namespace Void::Bitset;
// ── basic operations ──────────────────────────────────────────────────────────
Void::Testing::Test bitset_default_all_clear()
{
const Bitset<8> b;
Void::Testing::expect(b.none());
Void::Testing::expect_eq(b.count(), std::size_t{0});
}
Void::Testing::Test bitset_set_and_test()
{
Bitset<16> b;
b.set(0);
b.set(7);
b.set(15);
Void::Testing::expect(b.test(0));
Void::Testing::expect(b.test(7));
Void::Testing::expect(b.test(15));
Void::Testing::expect(!b.test(1));
Void::Testing::expect_eq(b.count(), std::size_t{3});
}
Void::Testing::Test bitset_reset_clears_bit()
{
Bitset<8> b;
b.set(3);
Void::Testing::expect(b.test(3));
b.reset(3);
Void::Testing::expect(!b.test(3));
}
Void::Testing::Test bitset_flip()
{
Bitset<8> b;
b.set(5);
b.flip(5);
Void::Testing::expect(!b.test(5));
b.flip(5);
Void::Testing::expect(b.test(5));
}
Void::Testing::Test bitset_set_all_and_reset_all()
{
Bitset<64> b;
b.set_all();
Void::Testing::expect(b.all());
Void::Testing::expect_eq(b.count(), std::size_t{64});
b.reset_all();
Void::Testing::expect(b.none());
}
Void::Testing::Test bitset_flip_all()
{
Bitset<8> b;
b.set(0); b.set(2); b.set(4);
b.flip_all();
Void::Testing::expect(!b.test(0));
Void::Testing::expect(b.test(1));
Void::Testing::expect_eq(b.count(), std::size_t{5});
}
Void::Testing::Test bitset_out_of_range_throws()
{
Bitset<8> b;
bool threw = false;
try { b.set(8); } catch (...) { threw = true; }
Void::Testing::expect(threw);
}
// ── multi-word (N > 64) ───────────────────────────────────────────────────────
Void::Testing::Test bitset_large_set_and_test()
{
Bitset<128> b;
b.set(63);
b.set(64);
b.set(127);
Void::Testing::expect(b.test(63));
Void::Testing::expect(b.test(64));
Void::Testing::expect(b.test(127));
Void::Testing::expect_eq(b.count(), std::size_t{3});
}
// ── bitwise ops ───────────────────────────────────────────────────────────────
Void::Testing::Test bitset_and()
{
Bitset<8> a, b;
a.set(0); a.set(1);
b.set(1); b.set(2);
const auto c = a & b;
Void::Testing::expect(!c.test(0));
Void::Testing::expect(c.test(1));
Void::Testing::expect(!c.test(2));
}
Void::Testing::Test bitset_or()
{
Bitset<8> a, b;
a.set(0); b.set(1);
const auto c = a | b;
Void::Testing::expect(c.test(0));
Void::Testing::expect(c.test(1));
}
Void::Testing::Test bitset_xor()
{
Bitset<8> a, b;
a.set(0); a.set(1);
b.set(1); b.set(2);
const auto c = a ^ b;
Void::Testing::expect(c.test(0));
Void::Testing::expect(!c.test(1));
Void::Testing::expect(c.test(2));
}
Void::Testing::Test bitset_not()
{
Bitset<8> a;
a.set(0);
const auto b = ~a;
Void::Testing::expect(!b.test(0));
Void::Testing::expect(b.test(1));
Void::Testing::expect_eq(b.count(), std::size_t{7});
}
// ── shift ─────────────────────────────────────────────────────────────────────
Void::Testing::Test bitset_shift_left()
{
Bitset<8> b;
b.set(0);
const auto s = b << 3;
Void::Testing::expect(!s.test(0));
Void::Testing::expect(s.test(3));
}
Void::Testing::Test bitset_shift_right()
{
Bitset<8> b;
b.set(5);
const auto s = b >> 2;
Void::Testing::expect(!s.test(5));
Void::Testing::expect(s.test(3));
}
// ── to_string ─────────────────────────────────────────────────────────────────
Void::Testing::Test bitset_to_string()
{
Bitset<8> b;
b.set(0); // LSB → rightmost in string
const auto s = b.to_string();
Void::Testing::expect_eq(s.size(), std::size_t{8});
Void::Testing::expect_eq(s[7], '1'); // bit 0 is rightmost
Void::Testing::expect_eq(s[0], '0');
}
Void::Testing::Test bitset_to_string_all_set()
{
Bitset<4> b;
b.set_all();
Void::Testing::expect_eq(b.to_string(), std::string{"1111"});
}
// ── find_first ────────────────────────────────────────────────────────────────
Void::Testing::Test bitset_find_first_set()
{
Bitset<64> b;
b.set(5);
b.set(10);
Void::Testing::expect_eq(b.find_first(), std::size_t{5});
}
Void::Testing::Test bitset_find_first_none()
{
const Bitset<64> b;
Void::Testing::expect_eq(b.find_first(), std::size_t{64}); // sentinel
}
// ── equality ──────────────────────────────────────────────────────────────────
Void::Testing::Test bitset_equality()
{
Bitset<8> a, b, c;
a.set(3); b.set(3); c.set(4);
Void::Testing::expect(a == b);
Void::Testing::expect(a != c);
}