<commit_before>import unittest

from bitfield import BitField


class BaseFunctionality(unittest.TestCase):
    def test_not_mapped_no_len(self):
        test_value = 42

        bf = BitField(test_value)

        self.assertEqual(
            repr(bf),
            '{cls}(x=0x{x:0{len}X}, base=16)'.format(
                cls=bf.__class__.__name__,
                x=int(bf),
                len=len(bf) * 2,
            )
        )

        self.assertEqual(
            dir(bf), ['_bit_size_', '_mapping_', '_mask_', '_value_']
        )

        self.assertEqual(bf._bit_size_, test_value.bit_length())
        self.assertEqual(bf._value_, test_value)
        self.assertIsNone(bf._mapping_)
        self.assertIsNone(bf._mask_)

        self.assertEqual(int(bf), test_value)
        self.assertEqual(bin(bf), bin(test_value))

        self.assertEqual(abs(bf), test_value)
        self.assertGreater(bf, test_value - 1)
        self.assertGreaterEqual(bf, test_value - 1)
        self.assertGreaterEqual(bf, test_value)
        self.assertLess(bf, test_value + 1)
        self.assertLessEqual(bf, test_value + 1)
        self.assertLessEqual(bf, test_value)
        self.assertEqual(bf, test_value)
        self.assertNotEqual(bf, 0)

        self.assertEqual(bool(bf), True)

        bf &= 1
        test_value &= 1

        self.assertEqual(bf, test_value)

        bf |= 1
        test_value |= 1

        self.assertEqual(bf, test_value)

        bf ^= 2
        test_value ^= 2

        self.assertEqual(bf, test_value)

        test_value = 42
        bf = BitField(test_value)

        self.assertEqual(bf & 1, test_value & 1)
        self.assertEqual(bf | 1, test_value | 1)
        self.assertEqual(bf ^ 1, test_value ^ 1)

        bf += 1
        test_value += 1

        self.assertEqual(bf, test_value)

        bf -= 1
        test_value -= 1

        self.assertEqual(bf + 1, test_value + 1)
        self.assertEqual(bf - 1, test_value - 1)

        self.assertEqual(bf * 2, test_value * 2)
        self.assertEqual(bf << 1, test_value << 1)
        self.assertEqual(bf >> 1, test_value >> 1)

        self.assertEqual(bf[0], 0)

        self.assertIsInstance(bf[0: 2], BitField)
        self.assertEqual(bf[0: 2]._bit_size_, 2)

        self.assertIsInstance(bf[: 2], BitField)
        self.assertEqual(bf[: 2]._bit_size_, 2)

        self.assertIsInstance(bf[(0, 2)], BitField)
        self.assertEqual(bf[(0, 2)]._bit_size_, 2)

        self.assertIsInstance(bf[[0, 2]], BitField)
        self.assertEqual(bf[[0, 2]]._bit_size_, 2)

        bf[0] = 1
        self.assertEqual(bf[0], 1)

        bf[1: 3] = 3
        self.assertEqual(bf[1: 3], 3)

        bf[:3] = 0
        self.assertEqual(bf[0: 3], 0)

        with self.assertRaises(ValueError):
            bf -= 100  # negative result

        with self.assertRaises(ValueError):
            _ = bf - 100  # negative result

        with self.assertRaises(IndexError):
            _ = bf[2:1]  # invalid slice

        with self.assertRaises(IndexError):
            _ = bf[None]  # invalid index type

        with self.assertRaises(ValueError):
            bf[0:2] = 10  # bigger, than slice

        with self.assertRaises(ValueError):
            bf[:2] = 10  # bigger, than slice

        with self.assertRaises(ValueError):
            bf[1] = 10  # bigger, than 1 bit

    def test_positive_mapped_no_len(self):
        class MappedBitField(BitField):
            test_index = 0
            test_slc = slice(1, 3)
            test_list_slc = [3, 5]

        mbf = MappedBitField(7)

        self.assertEqual(
            repr(mbf),
            '{cls}(x=0x{x:0{len}X}, base=16)'.format(
                cls=mbf.__class__.__name__,
                x=int(mbf),
                len=len(mbf) * 2,
            )
        )

        self.assertEqual(
            mbf._mapping_,
            {
                'test_index': 0,<commit_msg>Refactor & continue development<commit_after>import unittest

from bitfield import BitField


class BaseFunctionality(unittest.TestCase):
    def test_not_mapped_no_len(self):
        test_value = 42

        bf = BitField(test_value)

        self.assertEqual(
            repr(bf),
            '{cls}(x=0x{x:0{len}X}, base=16)'.format(
                cls=bf.__class__.__name__,
                x=int(bf),
                len=len(bf) * 2,
            )
        )

        self.assertEqual(
            dir(bf), ['_bit_size_', '_mapping_', '_mask_', '_value_']
        )

        self.assertEqual(bf._bit_size_, test_value.bit_length())
        self.assertEqual(bf._value_, test_value)
        self.assertIsNone(bf._mapping_)
        self.assertIsNone(bf._mask_)

        self.assertEqual(int(bf), test_value)
        self.assertEqual(bin(bf), bin(test_value))

        self.assertEqual(abs(bf), test_value)
        self.assertGreater(bf, test_value - 1)
        self.assertGreaterEqual(bf, test_value - 1)
        self.assertGreaterEqual(bf, test_value)
        self.assertLess(bf, test_value + 1)
        self.assertLessEqual(bf, test_value + 1)
        self.assertLessEqual(bf, test_value)
        self.assertEqual(bf, test_value)
        self.assertNotEqual(bf, 0)

        self.assertEqual(bool(bf), True)

        bf &= 1
        test_value &= 1

        self.assertEqual(bf, test_value)

        bf |= 1
        test_value |= 1

        self.assertEqual(bf, test_value)

        bf ^= 2
        test_value ^= 2

        self.assertEqual(bf, test_value)

        test_value = 42
        bf = BitField(test_value)

        self.assertEqual(bf & 1, test_value & 1)
        self.assertEqual(bf | 1, test_value | 1)
        self.assertEqual(bf ^ 1, test_value ^ 1)

        bf += 1
        test_value += 1

        self.assertEqual(bf, test_value)

        bf -= 1
        test_value -= 1

        self.assertEqual(bf + 1, test_value + 1)
        self.assertEqual(bf - 1, test_value - 1)

        self.assertEqual(bf * 2, test_value * 2)
        self.assertEqual(bf << 1, test_value << 1)
        self.assertEqual(bf >> 1, test_value >> 1)

        self.assertEqual(bf[0], 0)

        self.assertIsInstance(bf[0: 2], BitField)
        self.assertEqual(bf[0: 2]._bit_size_, 2)

        self.assertIsInstance(bf[: 2], BitField)
        self.assertEqual(bf[: 2]._bit_size_, 2)

        self.assertIsInstance(bf[(0, 2)], BitField)
        self.assertEqual(bf[(0, 2)]._bit_size_, 2)

        self.assertIsInstance(bf[[0, 2]], BitField)
        self.assertEqual(bf[[0, 2]]._bit_size_, 2)

        bf[0] = 1
        self.assertEqual(bf[0], 1)

        bf[1: 3] = 3
        self.assertEqual(bf[1: 3], 3)

        bf[:3] = 0
        self.assertEqual(bf[0: 3], 0)

        with self.assertRaises(ValueError):
            bf -= 100  # negative result

        with self.assertRaises(ValueError):
            bf - 100  # negative result

        with self.assertRaises(IndexError):
            bf[2:1]  # invalid slice

        with self.assertRaises(IndexError):
            bf[None]  # invalid index type

        with self.assertRaises(ValueError):
            bf[0:2] = 10  # bigger, than slice

        with self.assertRaises(ValueError):
            bf[:2] = 10  # bigger, than slice

        with self.assertRaises(ValueError):
            bf[1] = 10  # bigger, than 1 bit

    def test_positive_mapped_no_len(self):
        class MappedBitField(BitField):
            test_index = 0
            test_slc = slice(1, 3)
            test_list_slc = [3, 5]

        mbf = MappedBitField(7)

        self.assertEqual(
            repr(mbf),
            '{cls}(x=0x{x:0{len}X}, base=16)'.format(
                cls=mbf.__class__.__name__,
                x=int(mbf),
                len=len(mbf) * 2,
            )
        )

        self.assertEqual(
            mbf._mapping_,
            {
                'test_index': 0,
{'fraction_non_alphanumeric': 0.12027579162410623, 'fraction_numerical': 0.027068437180796732, 'mean_word_length': 1.879779411764706, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3268626', 'n_tokens_mistral': 2747, 'n_tokens_neox': 2467, 'n_words': 477}