#!/usr/bin/env python

# THIS FILE WAS GENERATED BY A LLM.
# The tests help improve coverage but are not hand crafted.

import sys
sys.path.insert(0, "..")
import pytest
from tibs import Tibs, Mutibs

class TestTibsCreation:
    def test_from_bin_simple(self):
        assert Tibs.from_bin("101") == "0b101"

    def test_from_bin_with_prefix(self):
        assert Tibs.from_bin("0b110") == "0b110"

    def test_from_oct_simple(self):
        assert Tibs.from_oct("75") == "0o75"

    def test_from_oct_with_prefix(self):
        assert Tibs.from_oct("0o64") == "0o64"

    def test_from_hex_simple(self):
        assert Tibs.from_hex("af") == "0xaf"

    def test_from_hex_with_prefix(self):
        assert Tibs.from_hex("0xbe") == "0xbe"

    def test_from_u_basic(self):
        assert Tibs.from_u(42, 8) == "0b00101010"

    def test_from_i_positive(self):
        assert Tibs.from_i(42, 8) == "0b00101010"

    def test_from_i_negative(self):
        assert Tibs.from_i(-42, 8) == "0b11010110"

    def test_from_f16(self):
        assert len(Tibs.from_f(3.14, 16)) == 16

    def test_from_f32(self):
        assert len(Tibs.from_f(3.14, 32)) == 32

    def test_from_f64(self):
        assert len(Tibs.from_f(3.14, 64)) == 64

    def test_from_bytes(self):
        assert Tibs.from_bytes(b"abc") == "0x616263"

    def test_from_string_bin(self):
        assert Tibs.from_string("0b101") == "0b101"

    def test_from_string_hex(self):
        assert Tibs.from_string("0xabc") == "0xabc"

    def test_from_bools(self):
        assert Tibs.from_bools([True, False, 1, 0]) == "0b1010"

    def test_from_zeros(self):
        assert Tibs.from_zeros(5) == "0b00000"

    def test_from_ones(self):
        assert Tibs.from_ones(5) == "0b11111"

    def test_from_random(self):
        assert len(Tibs.from_random(100)) == 100

    def test_from_joined(self):
        assert Tibs.from_joined(["0b11", "0o7", "0xf"]) == "0b111111111"

    def test_constructor_delegation(self):
        assert Tibs("0b101") == Tibs.from_string("0b101")
        assert Tibs(b"abc") == Tibs.from_bytes(b"abc")
        assert Tibs([True, False]) == Tibs.from_bools([True, False])

    def test_from_slice(self):
        a = Tibs("0b101100111000")
        b = a[2:10]
        assert b == "0b11001110"
        c = Tibs(b)
        assert c == "0b11001110"

class TestMutibsCreation:
    def test_from_bin_simple(self):
        assert Mutibs.from_bin("101") == "0b101"

    def test_from_oct_simple(self):
        assert Mutibs.from_oct("75") == "0o75"

    def test_from_hex_simple(self):
        assert Mutibs.from_hex("af") == "0xaf"

    def test_from_u_basic(self):
        assert Mutibs.from_u(42, 8) == "0b00101010"

    def test_from_i_positive(self):
        assert Mutibs.from_i(42, 8) == "0b00101010"

    def test_from_f32(self):
        assert len(Mutibs.from_f(3.14, 32)) == 32

    def test_from_bytes(self):
        assert Mutibs.from_bytes(b"abc") == "0x616263"

    def test_from_string_bin(self):
        assert Mutibs.from_string("0b101") == "0b101"

    def test_from_bools(self):
        assert Mutibs.from_bools([True, False, 1, 0]) == "0b1010"

    def test_from_zeros(self):
        assert Mutibs.from_zeros(5) == "0b00000"

    def test_from_ones(self):
        assert Mutibs.from_ones(5) == "0b11111"

    def test_from_random(self):
        assert len(Mutibs.from_random(100)) == 100

    def test_from_joined(self):
        assert Mutibs.from_joined(["0b11", "0o7", "0xf"]) == "0b111111111"

    def test_constructor_delegation(self):
        assert Mutibs("0b101") == Mutibs.from_string("0b101")
        assert Mutibs(b"abc") == Mutibs.from_bytes(b"abc")
        assert Mutibs([True, False]) == Mutibs.from_bools([True, False])

    def test_from_slice(self):
        a = Tibs("0b101100111000")
        b = a[2:10]
        assert b == "0b11001110"
        c = Mutibs(b)
        assert c == "0b11001110"

class TestTibsMethods:
    def test_eq(self):
        assert Tibs("0xf") == "0b1111"
        assert not (Tibs("0xf") == "0b1110")

    def test_str(self):
        assert str(Tibs("0b101")) == "0b101"

    def test_repr(self):
        assert repr(Tibs("0b101")) == "Tibs('0b101')"

class TestMutibsMethods:
    def test_eq(self):
        assert Mutibs("0xf") == "0b1111"
        assert not (Mutibs("0xf") == "0b1110")

    def test_str(self):
        assert str(Mutibs("0b101")) == "0b101"

    def test_repr(self):
        assert repr(Mutibs("0b101")) == "Mutibs('0b101')"

    def test_set_index(self):
        m = Mutibs("0b000")
        m[1] = 1
        assert m == "0b010"

    def test_set_slice(self):
        m = Mutibs("0b00000")
        m[1:4] = "0b111"
        assert m == "0b01110"

    def test_ixor(self):
        m = Mutibs("0b1100")
        m ^= "0b1010"
        assert m == "0b0110"

    def test_ior(self):
        m = Mutibs("0b1100")
        m |= "0b1010"
        assert m == "0b1110"

    def test_iand(self):
        m = Mutibs("0b1100")
        m &= "0b1010"
        assert m == "0b1000"

    def test_set_from_sequence(self):
        m = Mutibs("0b00000")
        m.set(True, [1, 3])
        assert m == "0b01010"

    def test_set_from_slice(self):
        m = Mutibs("0b00000000")
        m[1:7:2] = [1, 1, 1]
        assert m == "0b01010100"

class TestIterators:
    def test_bool_iterator(self):
        t = Tibs("0b101")
        assert list(t) == [True, False, True]

    def test_find_all_iterator(self):
        t = Tibs("0b101101")
        assert list(t.find_all("0b101")) == [0, 3]

    def test_chunks_iterator(self):
        t = Tibs("0b101101")
        chunks = list(t.chunks(3))
        assert len(chunks) == 2
        assert chunks[0] == "0b101"
        assert chunks[1] == "0b101"

class TestErrorHandling:
    def test_from_u_errors(self):
        with pytest.raises(OverflowError):
            Tibs.from_u(256, 8) # Overflows
        with pytest.raises(OverflowError):
            Tibs.from_u(-1, 8) # can't convert negative int to unsigned
        with pytest.raises(ValueError):
            Tibs.from_u(1, 200) # Too long

    def test_from_i_errors(self):
        with pytest.raises(OverflowError):
            Tibs.from_i(128, 8) # Overflows (signed)
        with pytest.raises(OverflowError):
            Tibs.from_i(-129, 8) # Overflows (signed)

    def test_from_f_errors(self):
        with pytest.raises(ValueError):
            Tibs.from_f(1.0, 12) # Invalid length

    def test_from_bytes_errors(self):
        with pytest.raises(ValueError):
            Tibs.from_bytes(b"abc", offset=30)
        with pytest.raises(ValueError):
            Tibs.from_bytes(b"abc", length=30)

    def test_adding_nonsense(self):
        with pytest.raises(ValueError):
            Mutibs.from_bools([0]) + "hello"

    def test_slice_assignment_errors(self):
        a = Mutibs('0b000')
        with pytest.raises(IndexError):
            a[5] = 1
        with pytest.raises(IndexError):
            del a[5]

class TestAdvancedFeatures:
    def test_rol(self):
        s = Mutibs("0b0001")
        s.rol(1)
        assert s == "0b0010"

    def test_ror(self):
        s = Mutibs("0b1000")
        s.ror(1)
        assert s == "0b0100"

    def test_byte_swap(self):
        s = Mutibs("0x0123456789abcdef")
        s.byte_swap()
        assert s == "0xefcdab8967452301"

    def test_to_f(self):
        f = 3.14159
        t16 = Tibs.from_f(f, 16)
        assert t16.to_f() != f # Should be different
        t32 = Tibs.from_f(f, 32)
        assert abs(t32.to_f() - f) < 1e-6
        t64 = Tibs.from_f(f, 64)
        assert t64.to_f() == f

    def test_to_u_i(self):
        assert Tibs("0b11111111").to_u() == 255
        assert Tibs("0b11111111").to_i() == -1
        assert Tibs("0b01111111").to_i() == 127

    def test_find_all_byte_aligned(self):
        a = Tibs("0x00ff00ff")
        assert list(a.find_all("0xff", byte_aligned=True)) == [8, 24]

class TestComplexInteractions:
    def test_overlapping_replace(self):
        s = Mutibs("0b101010")
        s.replace("0b10", "0b1010")
        assert s == "0b101010101010"

    def test_find_all_overlapping(self):
        s = Tibs("0b1010101")
        assert list(s.find_all("0b101")) == [0, 2, 4]

    def test_chunks_with_remainder(self):
        s = Tibs.from_zeros(10)
        chunks = list(s.chunks(3))
        assert len(chunks) == 4
        assert len(chunks[0]) == 3
        assert len(chunks[1]) == 3
        assert len(chunks[2]) == 3
        assert len(chunks[3]) == 1

    def test_set_slice_with_self_overlap(self):
        m = Mutibs("0b11110000")
        m[2:6] = m[0:4]
        assert m == "0b11111100"

    def test_int_representation_edges(self):
        assert Tibs.from_u(255, 8).to_i() == -1
        assert Tibs.from_i(-128, 8).to_u() == 128
        with pytest.raises(ValueError):
            Tibs("0b" + "1" * 129).to_u()
        with pytest.raises(ValueError):
            Tibs("0b" + "1" * 129).to_i()

    def test_mixed_type_operations(self):
        m = Mutibs("0b1010")
        t = Tibs("0b0101")
        m |= t
        assert m == "0b1111"

    def test_chained_mutations(self):
        m = Mutibs("0x1234")
        m.rol(4)
        m.invert()
        m.ror(8)
        assert m == "0xbedc"

    def test_immutable_slice_mutation_check(self):
        t = Tibs("0x12345678")
        s = t[8:24]
        m = s.to_mutibs()
        m.invert()
        assert t == "0x12345678"
        assert s != m
        assert s == "0x3456"

    def test_from_joined_mixed_types(self):
        t = Tibs("0b11")
        m = Mutibs("0b00")
        result = Tibs.from_joined([t, m, "0xff", [1,0,1]])
        assert result == "0b110011111111101"

    def test_delete_and_insert(self):
        m = Mutibs("0xabcdef")
        del m[8:16]
        assert m == "0xabef"
        m.insert(8, "0b11110000")
        assert m == "0xabf0ef"

    def test_replace_with_empty_string(self):
        s = Mutibs("0b101010")
        s.replace("0b10", "")
        assert s == "0b"

class TestSliceOperations:
    def test_slice_eq(self):
        a = Tibs("0b101100111000")
        b = a[2:10]
        c = Tibs("0b11001110")
        assert b == c

    def test_slice_logical_or(self):
        a = Tibs("0b00001111")
        b = Tibs("0b10101010")
        s_a = a[0:8]
        s_b = b[0:8]
        assert s_a | s_b == "0b10101111"

    def test_slice_logical_and(self):
        a = Tibs("0b00001111")
        b = Tibs("0b10101010")
        s_a = a[0:8]
        s_b = b[0:8]
        assert s_a & s_b == "0b00001010"

    def test_slice_logical_xor(self):
        a = Tibs("0b00001111")
        b = Tibs("0b10101010")
        s_a = a[0:8]
        s_b = b[0:8]
        assert s_a ^ s_b == "0b10100101"

    def test_slice_logical_or_offset(self):
        a = Tibs("0b100001111")  # 9 bits
        b = Tibs("0b010101010")  # 9 bits
        s_a = a[1:9]  # offset = 1
        s_b = b[1:9]  # offset = 1
        assert s_a | s_b == "0b10101111"

    def test_slice_logical_and_offset(self):
        a = Tibs("0b100001111")
        b = Tibs("0b010101010")
        s_a = a[1:9]
        s_b = b[1:9]
        assert s_a & s_b == "0b00001010"

    def test_find_slice(self):
        a = Tibs("0b101100111000101100")
        b = Tibs("0b11001110")
        c = a[2:10]
        assert c == b
        assert a.find(c) == 2

    def test_find_all_slice(self):
        a = Tibs("0b110011100011001110")
        b = Tibs("0b11001110")
        c = a[0:8]
        assert c == b
        assert list(a.find_all(c)) == [0, 10]

    def test_join_slice(self):
        a = Tibs("0b101100111000")
        b = a[2:10]
        c = Tibs.from_joined([b, b])
        assert c == "0b1100111011001110"

    def test_mutibs_from_slice(self):
        a = Tibs("0b101100111000")
        b = a[2:10]
        m = Mutibs(b)
        m.invert()
        assert m != b
        assert a == "0b101100111000"

    def test_logical_op_stepped_slice(self):
        a = Tibs("0b00110011")
        b = Tibs("0b01010101")
        c = a[::2]  # 0101
        d = b[::2]  # 0000
        assert c | d == "0b0101"
        assert c & d == "0b0000"
        assert c ^ d == "0b0101"

    def test_mixed_tibs_mutibs_slice_ops(self):
        a = Tibs("0b11110000")
        b = Mutibs("0b10101010")
        s_a = a[0:8]
        s_b = b[0:8]
        assert s_a & s_b == "0b10100000"

    def test_mutibs_slice_is_immutable_copy(self):
        a = Mutibs("0b00000000")
        b = a[2:6]
        b[1] = 1
        # The original is unchanged
        assert a == "0b00000000"

    def test_slice_logical_xor_offset(self):
        a = Tibs("0b100001111")
        b = Tibs("0b010101010")
        s_a = a[1:9]
        s_b = b[1:9]
        assert s_a ^ s_b == "0b10100101"