bitarray 3.7.0__tar.gz → 3.7.1__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {bitarray-3.7.0 → bitarray-3.7.1}/PKG-INFO +17 -83
- {bitarray-3.7.0 → bitarray-3.7.1}/README.rst +16 -82
- {bitarray-3.7.0 → bitarray-3.7.1}/bitarray/__init__.pyi +3 -0
- {bitarray-3.7.0 → bitarray-3.7.1}/bitarray/_util.c +13 -1
- {bitarray-3.7.0 → bitarray-3.7.1}/bitarray/bitarray.h +1 -1
- {bitarray-3.7.0 → bitarray-3.7.1}/bitarray/test_bitarray.py +8 -5
- {bitarray-3.7.0 → bitarray-3.7.1}/bitarray/test_util.py +147 -145
- {bitarray-3.7.0 → bitarray-3.7.1}/bitarray.egg-info/PKG-INFO +17 -83
- {bitarray-3.7.0 → bitarray-3.7.1}/LICENSE +0 -0
- {bitarray-3.7.0 → bitarray-3.7.1}/bitarray/__init__.py +0 -0
- {bitarray-3.7.0 → bitarray-3.7.1}/bitarray/_bitarray.c +0 -0
- {bitarray-3.7.0 → bitarray-3.7.1}/bitarray/py.typed +0 -0
- {bitarray-3.7.0 → bitarray-3.7.1}/bitarray/pythoncapi_compat.h +0 -0
- {bitarray-3.7.0 → bitarray-3.7.1}/bitarray/test_281.pickle +0 -0
- {bitarray-3.7.0 → bitarray-3.7.1}/bitarray/util.py +0 -0
- {bitarray-3.7.0 → bitarray-3.7.1}/bitarray/util.pyi +0 -0
- {bitarray-3.7.0 → bitarray-3.7.1}/bitarray.egg-info/SOURCES.txt +0 -0
- {bitarray-3.7.0 → bitarray-3.7.1}/bitarray.egg-info/dependency_links.txt +0 -0
- {bitarray-3.7.0 → bitarray-3.7.1}/bitarray.egg-info/not-zip-safe +0 -0
- {bitarray-3.7.0 → bitarray-3.7.1}/bitarray.egg-info/top_level.txt +0 -0
- {bitarray-3.7.0 → bitarray-3.7.1}/pyproject.toml +0 -0
- {bitarray-3.7.0 → bitarray-3.7.1}/setup.cfg +0 -0
- {bitarray-3.7.0 → bitarray-3.7.1}/setup.py +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: bitarray
|
|
3
|
-
Version: 3.7.
|
|
3
|
+
Version: 3.7.1
|
|
4
4
|
Summary: efficient arrays of booleans -- C extension
|
|
5
5
|
Home-page: https://github.com/ilanschnell/bitarray
|
|
6
6
|
Author: Ilan Schnell
|
|
@@ -89,7 +89,7 @@ Once you have installed the package, you may want to test it:
|
|
|
89
89
|
|
|
90
90
|
$ python -c 'import bitarray; bitarray.test()'
|
|
91
91
|
bitarray is installed in: /Users/ilan/bitarray/bitarray
|
|
92
|
-
bitarray version: 3.7.
|
|
92
|
+
bitarray version: 3.7.1
|
|
93
93
|
sys.version: 3.13.5 (main, Jun 16 2025) [Clang 18.1.8]
|
|
94
94
|
sys.prefix: /Users/ilan/miniforge
|
|
95
95
|
pointer size: 64 bit
|
|
@@ -104,7 +104,7 @@ Once you have installed the package, you may want to test it:
|
|
|
104
104
|
.........................................................................
|
|
105
105
|
................................................................
|
|
106
106
|
----------------------------------------------------------------------
|
|
107
|
-
Ran
|
|
107
|
+
Ran 597 tests in 0.165s
|
|
108
108
|
|
|
109
109
|
OK
|
|
110
110
|
|
|
@@ -256,85 +256,15 @@ shift (``>>``) always shifts towards higher indices.
|
|
|
256
256
|
Bit-endianness
|
|
257
257
|
--------------
|
|
258
258
|
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
When dealing with the machine representation of bitarray objects, it is
|
|
269
|
-
recommended to always explicitly specify the endianness.
|
|
270
|
-
|
|
271
|
-
By default, bitarrays use big-endian representation:
|
|
272
|
-
|
|
273
|
-
.. code-block:: python
|
|
274
|
-
|
|
275
|
-
>>> a = bitarray(b'A')
|
|
276
|
-
>>> a.endian
|
|
277
|
-
'big'
|
|
278
|
-
>>> a
|
|
279
|
-
bitarray('01000001')
|
|
280
|
-
>>> a[6] = 1
|
|
281
|
-
>>> a.tobytes()
|
|
282
|
-
b'C'
|
|
283
|
-
|
|
284
|
-
Big-endian means that the most-significant bit comes first.
|
|
285
|
-
Here, ``a[0]`` is the lowest address (index) and most significant bit,
|
|
286
|
-
and ``a[7]`` is the highest address and least significant bit.
|
|
287
|
-
|
|
288
|
-
When creating a new bitarray object, the endianness can always be
|
|
289
|
-
specified explicitly:
|
|
290
|
-
|
|
291
|
-
.. code-block:: python
|
|
292
|
-
|
|
293
|
-
>>> a = bitarray(b'A', endian='little')
|
|
294
|
-
>>> a
|
|
295
|
-
bitarray('10000010')
|
|
296
|
-
>>> a.endian
|
|
297
|
-
'little'
|
|
298
|
-
|
|
299
|
-
Here, the low-bit comes first because little-endian means that increasing
|
|
300
|
-
numeric significance corresponds to an increasing address.
|
|
301
|
-
So ``a[0]`` is the lowest address and least significant bit,
|
|
302
|
-
and ``a[7]`` is the highest address and most significant bit.
|
|
303
|
-
|
|
304
|
-
The bit-endianness is a property of the bitarray object.
|
|
305
|
-
The endianness cannot be changed once a bitarray object has been created.
|
|
306
|
-
When comparing bitarray objects, the endianness (and hence the machine
|
|
307
|
-
representation) is irrelevant; what matters is the mapping from indices
|
|
308
|
-
to bits:
|
|
309
|
-
|
|
310
|
-
.. code-block:: python
|
|
311
|
-
|
|
312
|
-
>>> bitarray('11001', endian='big') == bitarray('11001', endian='little')
|
|
313
|
-
True
|
|
314
|
-
>>> a = bitarray(b'\x01', endian='little')
|
|
315
|
-
>>> b = bitarray(b'\x80', endian='big')
|
|
316
|
-
>>> a == b
|
|
317
|
-
True
|
|
318
|
-
>>> a.tobytes() == b.tobytes()
|
|
319
|
-
False
|
|
320
|
-
|
|
321
|
-
Bitwise operations (``|``, ``^``, ``&=``, ``|=``, ``^=``, ``~``) are
|
|
322
|
-
implemented efficiently using the corresponding byte operations in C, i.e. the
|
|
323
|
-
operators act on the machine representation of the bitarray objects.
|
|
324
|
-
Therefore, it is not possible to perform bitwise operators on bitarrays
|
|
325
|
-
with different endianness.
|
|
326
|
-
|
|
327
|
-
As mentioned above, the endianness can not be changed once an object is
|
|
328
|
-
created. However, you can create a new bitarray with different endianness:
|
|
329
|
-
|
|
330
|
-
.. code-block:: python
|
|
331
|
-
|
|
332
|
-
>>> a = bitarray('111000', endian='little')
|
|
333
|
-
>>> b = bitarray(a, endian='big')
|
|
334
|
-
>>> b
|
|
335
|
-
bitarray('111000')
|
|
336
|
-
>>> a == b
|
|
337
|
-
True
|
|
259
|
+
For many purposes the bit-endianness is not of any relevance to the end user
|
|
260
|
+
and can be regarded as an implementation detail of bitarray objects.
|
|
261
|
+
However, there are use cases when the bit-endianness becomes important.
|
|
262
|
+
These use cases involve explicitly reading and writing the bitarray buffer
|
|
263
|
+
using ``.tobytes()``, ``.frombytes()``, ``.tofile()`` or ``.fromfile()``,
|
|
264
|
+
importing and exporting buffers. Also, a number of utility functions
|
|
265
|
+
in ``bitarray.util`` will return different results depending on
|
|
266
|
+
bit-endianness, such as ``ba2hex()`` or ``ba2int``.
|
|
267
|
+
To better understand this topic, please read `bit-endianness <https://github.com/ilanschnell/bitarray/blob/master/doc/endianness.rst>`__.
|
|
338
268
|
|
|
339
269
|
|
|
340
270
|
Buffer protocol
|
|
@@ -421,7 +351,7 @@ and can therefore be used as a dictionary key:
|
|
|
421
351
|
Reference
|
|
422
352
|
=========
|
|
423
353
|
|
|
424
|
-
bitarray version: 3.7.
|
|
354
|
+
bitarray version: 3.7.1 -- `change log <https://github.com/ilanschnell/bitarray/blob/master/doc/changelog.rst>`__
|
|
425
355
|
|
|
426
356
|
In the following, ``item`` and ``value`` are usually a single bit -
|
|
427
357
|
an integer 0 or 1.
|
|
@@ -874,6 +804,10 @@ This sub-module was added in version 1.2.
|
|
|
874
804
|
When ``odd=True``, only odd prime numbers are represented in the resulting
|
|
875
805
|
bitarray ``a``, and ``a[i]`` corresponds to ``2*i+1`` being prime or not.
|
|
876
806
|
|
|
807
|
+
Apart from working with prime numbers, this function is useful for
|
|
808
|
+
testing, as it provides a simple way to create a well-defined bitarray
|
|
809
|
+
of any length.
|
|
810
|
+
|
|
877
811
|
New in version 3.7
|
|
878
812
|
|
|
879
813
|
|
|
@@ -57,7 +57,7 @@ Once you have installed the package, you may want to test it:
|
|
|
57
57
|
|
|
58
58
|
$ python -c 'import bitarray; bitarray.test()'
|
|
59
59
|
bitarray is installed in: /Users/ilan/bitarray/bitarray
|
|
60
|
-
bitarray version: 3.7.
|
|
60
|
+
bitarray version: 3.7.1
|
|
61
61
|
sys.version: 3.13.5 (main, Jun 16 2025) [Clang 18.1.8]
|
|
62
62
|
sys.prefix: /Users/ilan/miniforge
|
|
63
63
|
pointer size: 64 bit
|
|
@@ -72,7 +72,7 @@ Once you have installed the package, you may want to test it:
|
|
|
72
72
|
.........................................................................
|
|
73
73
|
................................................................
|
|
74
74
|
----------------------------------------------------------------------
|
|
75
|
-
Ran
|
|
75
|
+
Ran 597 tests in 0.165s
|
|
76
76
|
|
|
77
77
|
OK
|
|
78
78
|
|
|
@@ -224,85 +224,15 @@ shift (``>>``) always shifts towards higher indices.
|
|
|
224
224
|
Bit-endianness
|
|
225
225
|
--------------
|
|
226
226
|
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
When dealing with the machine representation of bitarray objects, it is
|
|
237
|
-
recommended to always explicitly specify the endianness.
|
|
238
|
-
|
|
239
|
-
By default, bitarrays use big-endian representation:
|
|
240
|
-
|
|
241
|
-
.. code-block:: python
|
|
242
|
-
|
|
243
|
-
>>> a = bitarray(b'A')
|
|
244
|
-
>>> a.endian
|
|
245
|
-
'big'
|
|
246
|
-
>>> a
|
|
247
|
-
bitarray('01000001')
|
|
248
|
-
>>> a[6] = 1
|
|
249
|
-
>>> a.tobytes()
|
|
250
|
-
b'C'
|
|
251
|
-
|
|
252
|
-
Big-endian means that the most-significant bit comes first.
|
|
253
|
-
Here, ``a[0]`` is the lowest address (index) and most significant bit,
|
|
254
|
-
and ``a[7]`` is the highest address and least significant bit.
|
|
255
|
-
|
|
256
|
-
When creating a new bitarray object, the endianness can always be
|
|
257
|
-
specified explicitly:
|
|
258
|
-
|
|
259
|
-
.. code-block:: python
|
|
260
|
-
|
|
261
|
-
>>> a = bitarray(b'A', endian='little')
|
|
262
|
-
>>> a
|
|
263
|
-
bitarray('10000010')
|
|
264
|
-
>>> a.endian
|
|
265
|
-
'little'
|
|
266
|
-
|
|
267
|
-
Here, the low-bit comes first because little-endian means that increasing
|
|
268
|
-
numeric significance corresponds to an increasing address.
|
|
269
|
-
So ``a[0]`` is the lowest address and least significant bit,
|
|
270
|
-
and ``a[7]`` is the highest address and most significant bit.
|
|
271
|
-
|
|
272
|
-
The bit-endianness is a property of the bitarray object.
|
|
273
|
-
The endianness cannot be changed once a bitarray object has been created.
|
|
274
|
-
When comparing bitarray objects, the endianness (and hence the machine
|
|
275
|
-
representation) is irrelevant; what matters is the mapping from indices
|
|
276
|
-
to bits:
|
|
277
|
-
|
|
278
|
-
.. code-block:: python
|
|
279
|
-
|
|
280
|
-
>>> bitarray('11001', endian='big') == bitarray('11001', endian='little')
|
|
281
|
-
True
|
|
282
|
-
>>> a = bitarray(b'\x01', endian='little')
|
|
283
|
-
>>> b = bitarray(b'\x80', endian='big')
|
|
284
|
-
>>> a == b
|
|
285
|
-
True
|
|
286
|
-
>>> a.tobytes() == b.tobytes()
|
|
287
|
-
False
|
|
288
|
-
|
|
289
|
-
Bitwise operations (``|``, ``^``, ``&=``, ``|=``, ``^=``, ``~``) are
|
|
290
|
-
implemented efficiently using the corresponding byte operations in C, i.e. the
|
|
291
|
-
operators act on the machine representation of the bitarray objects.
|
|
292
|
-
Therefore, it is not possible to perform bitwise operators on bitarrays
|
|
293
|
-
with different endianness.
|
|
294
|
-
|
|
295
|
-
As mentioned above, the endianness can not be changed once an object is
|
|
296
|
-
created. However, you can create a new bitarray with different endianness:
|
|
297
|
-
|
|
298
|
-
.. code-block:: python
|
|
299
|
-
|
|
300
|
-
>>> a = bitarray('111000', endian='little')
|
|
301
|
-
>>> b = bitarray(a, endian='big')
|
|
302
|
-
>>> b
|
|
303
|
-
bitarray('111000')
|
|
304
|
-
>>> a == b
|
|
305
|
-
True
|
|
227
|
+
For many purposes the bit-endianness is not of any relevance to the end user
|
|
228
|
+
and can be regarded as an implementation detail of bitarray objects.
|
|
229
|
+
However, there are use cases when the bit-endianness becomes important.
|
|
230
|
+
These use cases involve explicitly reading and writing the bitarray buffer
|
|
231
|
+
using ``.tobytes()``, ``.frombytes()``, ``.tofile()`` or ``.fromfile()``,
|
|
232
|
+
importing and exporting buffers. Also, a number of utility functions
|
|
233
|
+
in ``bitarray.util`` will return different results depending on
|
|
234
|
+
bit-endianness, such as ``ba2hex()`` or ``ba2int``.
|
|
235
|
+
To better understand this topic, please read `bit-endianness <https://github.com/ilanschnell/bitarray/blob/master/doc/endianness.rst>`__.
|
|
306
236
|
|
|
307
237
|
|
|
308
238
|
Buffer protocol
|
|
@@ -389,7 +319,7 @@ and can therefore be used as a dictionary key:
|
|
|
389
319
|
Reference
|
|
390
320
|
=========
|
|
391
321
|
|
|
392
|
-
bitarray version: 3.7.
|
|
322
|
+
bitarray version: 3.7.1 -- `change log <https://github.com/ilanschnell/bitarray/blob/master/doc/changelog.rst>`__
|
|
393
323
|
|
|
394
324
|
In the following, ``item`` and ``value`` are usually a single bit -
|
|
395
325
|
an integer 0 or 1.
|
|
@@ -842,6 +772,10 @@ This sub-module was added in version 1.2.
|
|
|
842
772
|
When ``odd=True``, only odd prime numbers are represented in the resulting
|
|
843
773
|
bitarray ``a``, and ``a[i]`` corresponds to ``2*i+1`` being prime or not.
|
|
844
774
|
|
|
775
|
+
Apart from working with prime numbers, this function is useful for
|
|
776
|
+
testing, as it provides a simple way to create a well-defined bitarray
|
|
777
|
+
of any length.
|
|
778
|
+
|
|
845
779
|
New in version 3.7
|
|
846
780
|
|
|
847
781
|
|
|
@@ -117,6 +117,9 @@ class bitarray:
|
|
|
117
117
|
def __delitem__(self,
|
|
118
118
|
i: Union[int, slice, bitarray, Sequence]) -> None: ...
|
|
119
119
|
|
|
120
|
+
def __buffer__(self, flags: int, /) -> memoryview: ...
|
|
121
|
+
def __release_buffer__(self, buffer: memoryview, /) -> None: ...
|
|
122
|
+
|
|
120
123
|
def __add__(self, other: bitarray) -> bitarray: ...
|
|
121
124
|
def __iadd__(self, other: bitarray) -> bitarray: ...
|
|
122
125
|
def __mul__(self, n: int) -> bitarray: ...
|
|
@@ -837,7 +837,7 @@ static const char base64_alphabet[] =
|
|
|
837
837
|
|
|
838
838
|
/* Given the length of the base m in [1..6] and a character c, return
|
|
839
839
|
its index in the base 2**m alphabet , or -1 if when c is not included.
|
|
840
|
-
Note: i >> m
|
|
840
|
+
Note: i >> m is true when i is not in range(0, 2**m) */
|
|
841
841
|
static int
|
|
842
842
|
digit_to_int(int m, char c)
|
|
843
843
|
{
|
|
@@ -2136,6 +2136,17 @@ module_cfw(PyObject *module, PyObject *args) /* count_from_word() */
|
|
|
2136
2136
|
return PyLong_FromSsize_t(count_from_word(a, i));
|
|
2137
2137
|
}
|
|
2138
2138
|
|
|
2139
|
+
static PyObject *
|
|
2140
|
+
module_d2i(PyObject *module, PyObject *args)
|
|
2141
|
+
{
|
|
2142
|
+
int m;
|
|
2143
|
+
char c;
|
|
2144
|
+
|
|
2145
|
+
if (!PyArg_ParseTuple(args, "ic", &m, &c))
|
|
2146
|
+
return NULL;
|
|
2147
|
+
return PyLong_FromLong(digit_to_int(m, c));
|
|
2148
|
+
}
|
|
2149
|
+
|
|
2139
2150
|
static PyObject *
|
|
2140
2151
|
module_read_n(PyObject *module, PyObject *args)
|
|
2141
2152
|
{
|
|
@@ -2211,6 +2222,7 @@ static PyMethodDef module_functions[] = {
|
|
|
2211
2222
|
{"_setup_table", (PyCFunction) module_setup_table, METH_O, 0},
|
|
2212
2223
|
{"_zlw", (PyCFunction) module_zlw, METH_O, 0},
|
|
2213
2224
|
{"_cfw", (PyCFunction) module_cfw, METH_VARARGS, 0},
|
|
2225
|
+
{"_d2i", (PyCFunction) module_d2i, METH_VARARGS, 0},
|
|
2214
2226
|
{"_read_n", (PyCFunction) module_read_n, METH_VARARGS, 0},
|
|
2215
2227
|
{"_write_n", (PyCFunction) module_write_n, METH_VARARGS, 0},
|
|
2216
2228
|
{"_sc_rts", (PyCFunction) module_sc_rts, METH_O, 0},
|
|
@@ -1,3 +1,7 @@
|
|
|
1
|
+
# Copyright (c) 2008 - 2025, Ilan Schnell; All Rights Reserved
|
|
2
|
+
# bitarray is published under the PSF license.
|
|
3
|
+
#
|
|
4
|
+
# Author: Ilan Schnell
|
|
1
5
|
"""
|
|
2
6
|
Tests for bitarray
|
|
3
7
|
|
|
@@ -14,6 +18,7 @@ import shutil
|
|
|
14
18
|
import tempfile
|
|
15
19
|
from io import BytesIO, UnsupportedOperation
|
|
16
20
|
from random import choice, getrandbits, randrange, randint, shuffle
|
|
21
|
+
from string import whitespace
|
|
17
22
|
|
|
18
23
|
# imports needed inside tests
|
|
19
24
|
import array
|
|
@@ -57,8 +62,6 @@ def urandom_2(n, endian="<RAND>"):
|
|
|
57
62
|
del a[n:]
|
|
58
63
|
return a
|
|
59
64
|
|
|
60
|
-
WHITESPACE = ' \n\r\t\v'
|
|
61
|
-
|
|
62
65
|
|
|
63
66
|
class Util(object):
|
|
64
67
|
|
|
@@ -400,10 +403,10 @@ class CreateObjectTests(unittest.TestCase, Util):
|
|
|
400
403
|
self.assertRaises(ValueError, bitarray, '0\U00010348') # UCS4
|
|
401
404
|
|
|
402
405
|
def test_string01_whitespace(self):
|
|
403
|
-
a = bitarray(
|
|
406
|
+
a = bitarray(whitespace)
|
|
404
407
|
self.assertEqual(a, bitarray())
|
|
405
408
|
|
|
406
|
-
for c in
|
|
409
|
+
for c in whitespace:
|
|
407
410
|
a = bitarray(c + '1101110001')
|
|
408
411
|
self.assertEqual(a, bitarray('1101110001'))
|
|
409
412
|
|
|
@@ -2614,7 +2617,7 @@ class ExtendTests(unittest.TestCase, Util):
|
|
|
2614
2617
|
|
|
2615
2618
|
def test_string01_whitespace(self):
|
|
2616
2619
|
a = bitarray()
|
|
2617
|
-
a.extend(
|
|
2620
|
+
a.extend(whitespace)
|
|
2618
2621
|
self.assertEqual(len(a), 0)
|
|
2619
2622
|
a.extend('0 1\n0\r1\t0\v1_')
|
|
2620
2623
|
self.assertEqual(a, bitarray('010101'))
|
|
@@ -1,3 +1,7 @@
|
|
|
1
|
+
# Copyright (c) 2019 - 2025, Ilan Schnell; All Rights Reserved
|
|
2
|
+
# bitarray is published under the PSF license.
|
|
3
|
+
#
|
|
4
|
+
# Author: Ilan Schnell
|
|
1
5
|
"""
|
|
2
6
|
Tests for bitarray.util module
|
|
3
7
|
"""
|
|
@@ -16,15 +20,14 @@ import tempfile
|
|
|
16
20
|
import unittest
|
|
17
21
|
from io import StringIO
|
|
18
22
|
from functools import reduce
|
|
19
|
-
from string import hexdigits
|
|
20
23
|
from random import (choice, getrandbits, randrange, randint, random, sample,
|
|
21
24
|
seed)
|
|
25
|
+
from string import hexdigits, whitespace
|
|
22
26
|
from collections import Counter
|
|
23
27
|
|
|
24
28
|
from bitarray import (bitarray, frozenbitarray, decodetree, bits2bytes,
|
|
25
29
|
_set_default_endian)
|
|
26
|
-
from bitarray.test_bitarray import
|
|
27
|
-
PTRSIZE, WHITESPACE)
|
|
30
|
+
from bitarray.test_bitarray import Util, skipIf, is_pypy, urandom_2, PTRSIZE
|
|
28
31
|
|
|
29
32
|
from bitarray.util import (
|
|
30
33
|
zeros, ones, urandom, random_k, random_p, pprint, strip, count_n,
|
|
@@ -39,7 +42,7 @@ from bitarray.util import (
|
|
|
39
42
|
|
|
40
43
|
from bitarray.util import _Random, _ssqi # type: ignore
|
|
41
44
|
|
|
42
|
-
#
|
|
45
|
+
# --------------------------- zeros() ones() -----------------------------
|
|
43
46
|
|
|
44
47
|
class ZerosOnesTests(unittest.TestCase):
|
|
45
48
|
|
|
@@ -79,7 +82,7 @@ class ZerosOnesTests(unittest.TestCase):
|
|
|
79
82
|
# endian wrong string
|
|
80
83
|
self.assertRaises(ValueError, f, 0, 'foo')
|
|
81
84
|
|
|
82
|
-
#
|
|
85
|
+
# ----------------------------- urandom() ---------------------------------
|
|
83
86
|
|
|
84
87
|
class URandomTests(unittest.TestCase):
|
|
85
88
|
|
|
@@ -115,7 +118,7 @@ class URandomTests(unittest.TestCase):
|
|
|
115
118
|
# see if population is within expectation
|
|
116
119
|
self.assertTrue(abs(a.count() - 5_000_000) <= 15_811)
|
|
117
120
|
|
|
118
|
-
# ----------------------------
|
|
121
|
+
# ---------------------------- random_k() ---------------------------------
|
|
119
122
|
|
|
120
123
|
HAVE_RANDBYTES = sys.version_info[:2] >= (3, 9)
|
|
121
124
|
|
|
@@ -279,7 +282,7 @@ class Random_K_Tests(unittest.TestCase):
|
|
|
279
282
|
a = r.combine_half(seq)
|
|
280
283
|
self.assertTrue(abs(a.count() - mean) < 5_000)
|
|
281
284
|
|
|
282
|
-
# ----------------------------
|
|
285
|
+
# ---------------------------- random_p() ---------------------------------
|
|
283
286
|
|
|
284
287
|
HAVE_BINOMIALVARIATE = sys.version_info[:2] >= (3, 12)
|
|
285
288
|
|
|
@@ -371,7 +374,7 @@ class Random_P_Tests(unittest.TestCase):
|
|
|
371
374
|
limit = 1.0 / (r.K + 1) # lower limit for p
|
|
372
375
|
self.assertTrue(r.SMALL_P > limit)
|
|
373
376
|
|
|
374
|
-
#
|
|
377
|
+
# ---------------------------- gen_primes() -------------------------------
|
|
375
378
|
|
|
376
379
|
class PrimeTests(unittest.TestCase):
|
|
377
380
|
|
|
@@ -453,10 +456,10 @@ class PrimeTests(unittest.TestCase):
|
|
|
453
456
|
self.assertEqual(sum_indices(a, 2), sum_sqr_p)
|
|
454
457
|
b = gen_primes(n // 2, odd=1)
|
|
455
458
|
self.assertEqual(len(b), n // 2)
|
|
456
|
-
self.assertEqual(b.count() + 1, count)
|
|
459
|
+
self.assertEqual(b.count() + 1, count) # +1 because of prime 2
|
|
457
460
|
self.assertEqual(b, a[1::2])
|
|
458
461
|
|
|
459
|
-
#
|
|
462
|
+
# ----------------------------- pprint() ----------------------------------
|
|
460
463
|
|
|
461
464
|
class PPrintTests(unittest.TestCase):
|
|
462
465
|
|
|
@@ -517,7 +520,7 @@ class PPrintTests(unittest.TestCase):
|
|
|
517
520
|
def test_file(self):
|
|
518
521
|
tmpdir = tempfile.mkdtemp()
|
|
519
522
|
tmpfile = os.path.join(tmpdir, 'testfile')
|
|
520
|
-
a =
|
|
523
|
+
a = urandom_2(1000)
|
|
521
524
|
try:
|
|
522
525
|
with open(tmpfile, 'w') as fo:
|
|
523
526
|
pprint(a, fo)
|
|
@@ -527,7 +530,7 @@ class PPrintTests(unittest.TestCase):
|
|
|
527
530
|
finally:
|
|
528
531
|
shutil.rmtree(tmpdir)
|
|
529
532
|
|
|
530
|
-
#
|
|
533
|
+
# ----------------------------- strip() -----------------------------------
|
|
531
534
|
|
|
532
535
|
class StripTests(unittest.TestCase, Util):
|
|
533
536
|
|
|
@@ -594,7 +597,7 @@ class StripTests(unittest.TestCase, Util):
|
|
|
594
597
|
self.assertEqual(strip(a, 'both'), bitarray('1'))
|
|
595
598
|
self.assertEqual(len(a), n)
|
|
596
599
|
|
|
597
|
-
#
|
|
600
|
+
# ----------------------------- count_n() ---------------------------------
|
|
598
601
|
|
|
599
602
|
class CountN_Tests(unittest.TestCase, Util):
|
|
600
603
|
|
|
@@ -707,6 +710,15 @@ class CountN_Tests(unittest.TestCase, Util):
|
|
|
707
710
|
msg = "n = 2 exceeds total count (a.count(1) = 1)"
|
|
708
711
|
self.assertRaisesMessage(ValueError, msg, count_n, a, 2)
|
|
709
712
|
|
|
713
|
+
def test_primes(self):
|
|
714
|
+
a = gen_primes(10_000)
|
|
715
|
+
# there are 1229 primes below 10,000
|
|
716
|
+
self.assertEqual(a.count(), 1229)
|
|
717
|
+
for n, p in [( 10, 29), # the 10th prime number is 29
|
|
718
|
+
( 100, 541), # the 100th prime number is 541
|
|
719
|
+
(1000, 7919)]: # the 1000th prime number is 7919
|
|
720
|
+
self.assertEqual(count_n(a, n) - 1, p)
|
|
721
|
+
|
|
710
722
|
def test_large(self):
|
|
711
723
|
for _ in range(100):
|
|
712
724
|
N = randint(100_000, 250_000)
|
|
@@ -811,7 +823,7 @@ class BitwiseCountTests(unittest.TestCase, Util):
|
|
|
811
823
|
self.assertEqual(all(a ^ b), count_xor(a, b) == n)
|
|
812
824
|
self.assertEqual(all(a ^ b), a == ~b)
|
|
813
825
|
|
|
814
|
-
#
|
|
826
|
+
# --------------------------- any_and() -----------------------------------
|
|
815
827
|
|
|
816
828
|
class BitwiseAnyTests(unittest.TestCase, Util):
|
|
817
829
|
|
|
@@ -894,7 +906,7 @@ class BitwiseAnyTests(unittest.TestCase, Util):
|
|
|
894
906
|
a[i] = 1
|
|
895
907
|
self.assertEqual(b[i], any_and(a, b))
|
|
896
908
|
|
|
897
|
-
#
|
|
909
|
+
# ---------------------------- subset() -----------------------------------
|
|
898
910
|
|
|
899
911
|
class SubsetTests(unittest.TestCase, Util):
|
|
900
912
|
|
|
@@ -935,7 +947,7 @@ class SubsetTests(unittest.TestCase, Util):
|
|
|
935
947
|
for a in self.randombitarrays(start=1):
|
|
936
948
|
b = a.copy()
|
|
937
949
|
# we set one random bit in b to 1, so a is always a subset of b
|
|
938
|
-
b[randrange(len(a))]
|
|
950
|
+
b[randrange(len(a))] = 1
|
|
939
951
|
self.check(a, b, True)
|
|
940
952
|
# but b is only a subset when they are equal
|
|
941
953
|
self.check(b, a, a == b)
|
|
@@ -943,9 +955,9 @@ class SubsetTests(unittest.TestCase, Util):
|
|
|
943
955
|
a.setall(1)
|
|
944
956
|
self.check(b, a, True)
|
|
945
957
|
|
|
946
|
-
#
|
|
958
|
+
# ------------------------- correspond_all() ------------------------------
|
|
947
959
|
|
|
948
|
-
class CorrespondAllTests(unittest.TestCase
|
|
960
|
+
class CorrespondAllTests(unittest.TestCase):
|
|
949
961
|
|
|
950
962
|
def test_basic(self):
|
|
951
963
|
a = frozenbitarray('0101')
|
|
@@ -967,8 +979,9 @@ class CorrespondAllTests(unittest.TestCase, Util):
|
|
|
967
979
|
self.assertEqual(correspond_all(bitarray(a), bitarray(b)), res)
|
|
968
980
|
|
|
969
981
|
def test_random(self):
|
|
970
|
-
for
|
|
971
|
-
n =
|
|
982
|
+
for _ in range(100):
|
|
983
|
+
n = randrange(3000)
|
|
984
|
+
a = urandom_2(n)
|
|
972
985
|
b = urandom(n, a.endian)
|
|
973
986
|
res = correspond_all(a, b)
|
|
974
987
|
self.assertEqual(res[0], count_and(~a, ~b))
|
|
@@ -980,10 +993,10 @@ class CorrespondAllTests(unittest.TestCase, Util):
|
|
|
980
993
|
self.assertEqual(res[1] + res[2], count_xor(a, b))
|
|
981
994
|
self.assertEqual(sum(res), n)
|
|
982
995
|
|
|
983
|
-
#
|
|
996
|
+
# ----------------------------- byteswap() --------------------------------
|
|
984
997
|
|
|
985
998
|
@skipIf(is_pypy)
|
|
986
|
-
class
|
|
999
|
+
class ByteSwapTests(unittest.TestCase):
|
|
987
1000
|
|
|
988
1001
|
def test_basic_bytearray(self):
|
|
989
1002
|
a = bytearray(b"ABCD")
|
|
@@ -1078,9 +1091,9 @@ class ByteswapTests(unittest.TestCase, Util):
|
|
|
1078
1091
|
a.bytereverse()
|
|
1079
1092
|
self.assertEqual(a, b[::-1])
|
|
1080
1093
|
|
|
1081
|
-
#
|
|
1094
|
+
# ------------------------------ parity() ---------------------------------
|
|
1082
1095
|
|
|
1083
|
-
class ParityTests(unittest.TestCase
|
|
1096
|
+
class ParityTests(unittest.TestCase):
|
|
1084
1097
|
|
|
1085
1098
|
def test_explitcit(self):
|
|
1086
1099
|
for s, res in [('', 0), ('1', 1), ('0010011', 1), ('10100110', 0)]:
|
|
@@ -1093,10 +1106,14 @@ class ParityTests(unittest.TestCase, Util):
|
|
|
1093
1106
|
self.assertEqual(parity(ones(n)), n % 2)
|
|
1094
1107
|
|
|
1095
1108
|
def test_random(self):
|
|
1096
|
-
|
|
1109
|
+
endian = choice(["little", "big"])
|
|
1110
|
+
a = bitarray(endian=endian)
|
|
1097
1111
|
par = 0
|
|
1098
|
-
for
|
|
1112
|
+
for i in range(2000):
|
|
1099
1113
|
self.assertEqual(parity(a), par)
|
|
1114
|
+
self.assertEqual(par, a.count() % 2)
|
|
1115
|
+
self.assertEqual(a.endian, endian)
|
|
1116
|
+
self.assertEqual(len(a), i)
|
|
1100
1117
|
v = getrandbits(1)
|
|
1101
1118
|
a.append(v)
|
|
1102
1119
|
par ^= v
|
|
@@ -1107,13 +1124,7 @@ class ParityTests(unittest.TestCase, Util):
|
|
|
1107
1124
|
self.assertRaises(TypeError, parity)
|
|
1108
1125
|
self.assertRaises(TypeError, parity, bitarray("110"), 1)
|
|
1109
1126
|
|
|
1110
|
-
|
|
1111
|
-
for a in self.randombitarrays():
|
|
1112
|
-
b = a.copy()
|
|
1113
|
-
self.assertEqual(parity(a), a.count() % 2)
|
|
1114
|
-
self.assertEqual(a, b)
|
|
1115
|
-
|
|
1116
|
-
# ---------------------------------------------------------------------------
|
|
1127
|
+
# ---------------------------- sum_indices() ------------------------------
|
|
1117
1128
|
|
|
1118
1129
|
class SumIndicesUtil(unittest.TestCase):
|
|
1119
1130
|
|
|
@@ -1361,25 +1372,25 @@ class IntervalsTests(unittest.TestCase, Util):
|
|
|
1361
1372
|
v = not v
|
|
1362
1373
|
self.assertEqual(a, b)
|
|
1363
1374
|
|
|
1364
|
-
#
|
|
1375
|
+
# -------------------------- ba2hex() hex2ba() ---------------------------
|
|
1365
1376
|
|
|
1366
1377
|
class HexlifyTests(unittest.TestCase, Util):
|
|
1367
1378
|
|
|
1368
|
-
def
|
|
1369
|
-
|
|
1370
|
-
|
|
1371
|
-
|
|
1372
|
-
|
|
1373
|
-
|
|
1374
|
-
|
|
1375
|
-
|
|
1376
|
-
|
|
1377
|
-
|
|
1378
|
-
|
|
1379
|
-
|
|
1380
|
-
|
|
1381
|
-
|
|
1382
|
-
|
|
1379
|
+
def test_explicit(self):
|
|
1380
|
+
data = [ # little big
|
|
1381
|
+
('', '', ''),
|
|
1382
|
+
('1000', '1', '8'),
|
|
1383
|
+
('0101 0110', 'a6', '56'),
|
|
1384
|
+
('0100 1001 1101', '29b', '49d'),
|
|
1385
|
+
('0000 1100 1110 1111', '037f', '0cef'),
|
|
1386
|
+
]
|
|
1387
|
+
for bs, hex_le, hex_be in data:
|
|
1388
|
+
a_be = bitarray(bs, 'big')
|
|
1389
|
+
a_le = bitarray(bs, 'little')
|
|
1390
|
+
self.assertEQUAL(hex2ba(hex_be, 'big'), a_be)
|
|
1391
|
+
self.assertEQUAL(hex2ba(hex_le, 'little'), a_le)
|
|
1392
|
+
self.assertEqual(ba2hex(a_be), hex_be)
|
|
1393
|
+
self.assertEqual(ba2hex(a_le), hex_le)
|
|
1383
1394
|
|
|
1384
1395
|
def test_ba2hex_group(self):
|
|
1385
1396
|
a = bitarray('1000 0000 0101 1111', 'little')
|
|
@@ -1393,34 +1404,25 @@ class HexlifyTests(unittest.TestCase, Util):
|
|
|
1393
1404
|
self.assertEqual(ba2hex(a, 3, sep=", "), "10a, f")
|
|
1394
1405
|
|
|
1395
1406
|
def test_ba2hex_errors(self):
|
|
1407
|
+
self.assertRaises(TypeError, ba2hex)
|
|
1408
|
+
self.assertRaises(TypeError, ba2hex, None)
|
|
1409
|
+
self.assertRaises(TypeError, ba2hex, '101')
|
|
1410
|
+
|
|
1411
|
+
# length not multiple of 4
|
|
1412
|
+
self.assertRaises(ValueError, ba2hex, bitarray('10'))
|
|
1413
|
+
|
|
1396
1414
|
a = bitarray('1000 0000 0101 1111', 'little')
|
|
1397
1415
|
self.assertRaises(ValueError, ba2hex, a, -1)
|
|
1398
1416
|
self.assertRaises(ValueError, ba2hex, a, group=-1)
|
|
1417
|
+
# sep not str
|
|
1399
1418
|
self.assertRaises(TypeError, ba2hex, a, 1, b" ")
|
|
1400
1419
|
# embedded null character in sep
|
|
1401
|
-
self.assertRaises(ValueError, ba2hex, a, 2, "\0")
|
|
1402
|
-
|
|
1403
|
-
def test_hex2ba(self):
|
|
1404
|
-
_set_default_endian('big')
|
|
1405
|
-
self.assertEqual(hex2ba(''), bitarray())
|
|
1406
|
-
for c in 'e', 'E', b'e', b'E', bytearray(b'e'), bytearray(b'E'):
|
|
1407
|
-
a = hex2ba(c)
|
|
1408
|
-
self.assertEqual(a.to01(), '1110')
|
|
1409
|
-
self.assertEqual(a.endian, 'big')
|
|
1410
|
-
self.assertEqual(type(a), bitarray)
|
|
1411
|
-
self.assertEQUAL(hex2ba('01'), bitarray('0000 0001', 'big'))
|
|
1412
|
-
self.assertEQUAL(hex2ba('08', 'little'),
|
|
1413
|
-
bitarray('0000 0001', 'little'))
|
|
1414
|
-
self.assertEQUAL(hex2ba('aD'), bitarray('1010 1101', 'big'))
|
|
1415
|
-
self.assertEQUAL(hex2ba('10aF'),
|
|
1416
|
-
bitarray('0001 0000 1010 1111', 'big'))
|
|
1417
|
-
self.assertEQUAL(hex2ba(b'10 aF', 'little'),
|
|
1418
|
-
bitarray('1000 0000 0101 1111', 'little'))
|
|
1420
|
+
self.assertRaises(ValueError, ba2hex, a, 2, " \0")
|
|
1419
1421
|
|
|
1420
1422
|
def test_hex2ba_whitespace(self):
|
|
1421
1423
|
_set_default_endian('big')
|
|
1422
|
-
self.assertEqual(hex2ba("F1
|
|
1423
|
-
bitarray("11110001
|
|
1424
|
+
self.assertEqual(hex2ba("F1 FA %s f3 c0" % whitespace),
|
|
1425
|
+
bitarray("11110001 11111010 11110011 11000000"))
|
|
1424
1426
|
self.assertEQUAL(hex2ba(b' a F ', 'big'),
|
|
1425
1427
|
bitarray('1010 1111', 'big'))
|
|
1426
1428
|
self.assertEQUAL(hex2ba(860 * " " + '0 1D' + 590 * " ", 'little'),
|
|
@@ -1428,6 +1430,8 @@ class HexlifyTests(unittest.TestCase, Util):
|
|
|
1428
1430
|
|
|
1429
1431
|
def test_hex2ba_errors(self):
|
|
1430
1432
|
self.assertRaises(TypeError, hex2ba, 0)
|
|
1433
|
+
self.assertRaises(TypeError, hex2ba, "F", 1)
|
|
1434
|
+
self.assertRaises(ValueError, hex2ba, "F", "foo")
|
|
1431
1435
|
|
|
1432
1436
|
for s in '01a7g89', '0\u20ac', '0 \0', b'\x00':
|
|
1433
1437
|
self.assertRaises(ValueError, hex2ba, s)
|
|
@@ -1436,44 +1440,35 @@ class HexlifyTests(unittest.TestCase, Util):
|
|
|
1436
1440
|
msg = "invalid digit found for base16, got 'g' (0x67)"
|
|
1437
1441
|
self.assertRaisesMessage(ValueError, msg, hex2ba, s, 'big')
|
|
1438
1442
|
|
|
1439
|
-
def
|
|
1440
|
-
|
|
1441
|
-
(
|
|
1442
|
-
(
|
|
1443
|
-
(
|
|
1444
|
-
(
|
|
1445
|
-
('1000 1100 1110 1111' , '137f', '8cef'),
|
|
1446
|
-
('1000 1100 1110 1111 0100', '137f2', '8cef4'),
|
|
1447
|
-
]
|
|
1448
|
-
for bs, hex_le, hex_be in data:
|
|
1449
|
-
a_be = bitarray(bs, 'big')
|
|
1450
|
-
a_le = bitarray(bs, 'little')
|
|
1451
|
-
self.assertEQUAL(hex2ba(hex_be, 'big'), a_be)
|
|
1452
|
-
self.assertEQUAL(hex2ba(hex_le, 'little'), a_le)
|
|
1453
|
-
self.assertEqual(ba2hex(a_be), hex_be)
|
|
1454
|
-
self.assertEqual(ba2hex(a_le), hex_le)
|
|
1443
|
+
def test_hex2ba_types(self):
|
|
1444
|
+
for c in 'e', 'E', b'e', b'E', bytearray(b'e'), bytearray(b'E'):
|
|
1445
|
+
a = hex2ba(c, "big")
|
|
1446
|
+
self.assertEqual(a.to01(), '1110')
|
|
1447
|
+
self.assertEqual(a.endian, 'big')
|
|
1448
|
+
self.assertEqual(type(a), bitarray)
|
|
1455
1449
|
|
|
1456
1450
|
def test_random(self):
|
|
1457
1451
|
for _ in range(100):
|
|
1458
|
-
|
|
1459
|
-
|
|
1460
|
-
|
|
1461
|
-
|
|
1452
|
+
default_endian = self.random_endian()
|
|
1453
|
+
_set_default_endian(default_endian)
|
|
1454
|
+
endian = choice(["little", "big", None])
|
|
1455
|
+
a = urandom_2(4 * randrange(100), endian)
|
|
1456
|
+
s = ba2hex(a, group=randrange(10), sep=choice(whitespace))
|
|
1457
|
+
b = hex2ba(s, endian)
|
|
1458
|
+
self.assertEqual(b.endian, endian or default_endian)
|
|
1459
|
+
self.assertEqual(a, b)
|
|
1462
1460
|
self.check_obj(b)
|
|
1463
1461
|
|
|
1464
1462
|
def test_hexdigits(self):
|
|
1465
|
-
|
|
1466
|
-
|
|
1467
|
-
|
|
1468
|
-
|
|
1469
|
-
self.assertEqual(a.endian, default_endian)
|
|
1470
|
-
self.assertEqual(type(a), bitarray)
|
|
1471
|
-
self.check_obj(a)
|
|
1463
|
+
a = hex2ba(hexdigits)
|
|
1464
|
+
self.assertEqual(len(a), 4 * len(hexdigits))
|
|
1465
|
+
self.assertEqual(type(a), bitarray)
|
|
1466
|
+
self.check_obj(a)
|
|
1472
1467
|
|
|
1473
|
-
|
|
1474
|
-
|
|
1475
|
-
|
|
1476
|
-
|
|
1468
|
+
t = ba2hex(a)
|
|
1469
|
+
self.assertEqual(t, hexdigits.lower())
|
|
1470
|
+
self.assertEqual(type(t), str)
|
|
1471
|
+
self.assertEQUAL(a, hex2ba(t))
|
|
1477
1472
|
|
|
1478
1473
|
def test_binascii(self):
|
|
1479
1474
|
a = urandom(80, 'big')
|
|
@@ -1482,21 +1477,32 @@ class HexlifyTests(unittest.TestCase, Util):
|
|
|
1482
1477
|
b = bitarray(binascii.unhexlify(s), endian='big')
|
|
1483
1478
|
self.assertEQUAL(hex2ba(s, 'big'), b)
|
|
1484
1479
|
|
|
1485
|
-
#
|
|
1480
|
+
# -------------------------- ba2base() base2ba() -------------------------
|
|
1486
1481
|
|
|
1487
1482
|
class BaseTests(unittest.TestCase, Util):
|
|
1488
1483
|
|
|
1489
|
-
def
|
|
1490
|
-
|
|
1491
|
-
|
|
1492
|
-
|
|
1484
|
+
def test_explicit(self):
|
|
1485
|
+
data = [ # n little big
|
|
1486
|
+
('', 2, '', ''),
|
|
1487
|
+
('1 0 1', 2, '101', '101'),
|
|
1488
|
+
('11 01 00', 4, '320', '310'),
|
|
1489
|
+
('111 001', 8, '74', '71'),
|
|
1490
|
+
('1111 0001', 16, 'f8', 'f1'),
|
|
1491
|
+
('11111 00001', 32, '7Q', '7B'),
|
|
1492
|
+
('111111 000001', 64, '/g', '/B'),
|
|
1493
|
+
]
|
|
1494
|
+
for bs, n, s_le, s_be in data:
|
|
1495
|
+
a_le = bitarray(bs, 'little')
|
|
1496
|
+
a_be = bitarray(bs, 'big')
|
|
1497
|
+
self.assertEQUAL(base2ba(n, s_le, 'little'), a_le)
|
|
1498
|
+
self.assertEQUAL(base2ba(n, s_be, 'big'), a_be)
|
|
1499
|
+
self.assertEqual(ba2base(n, a_le), s_le)
|
|
1500
|
+
self.assertEqual(ba2base(n, a_be), s_be)
|
|
1493
1501
|
|
|
1494
|
-
def
|
|
1495
|
-
|
|
1496
|
-
|
|
1497
|
-
a
|
|
1498
|
-
self.assertEqual(a.to01(), '1110')
|
|
1499
|
-
self.assertEqual(a.endian, 'big')
|
|
1502
|
+
def test_base2ba_types(self):
|
|
1503
|
+
for c in '7', b'7', bytearray(b'7'):
|
|
1504
|
+
a = base2ba(32, c)
|
|
1505
|
+
self.assertEqual(a.to01(), '11111')
|
|
1500
1506
|
self.assertEqual(type(a), bitarray)
|
|
1501
1507
|
|
|
1502
1508
|
def test_base2ba_whitespace(self):
|
|
@@ -1505,12 +1511,12 @@ class BaseTests(unittest.TestCase, Util):
|
|
|
1505
1511
|
self.assertEqual(base2ba(32, "7 A"), bitarray("11111 00000"))
|
|
1506
1512
|
self.assertEqual(base2ba(64, b"A /"), bitarray("000000 111111"))
|
|
1507
1513
|
for n in 2, 4, 8, 16, 32, 64:
|
|
1508
|
-
a = base2ba(n,
|
|
1514
|
+
a = base2ba(n, whitespace)
|
|
1509
1515
|
self.assertEqual(a, bitarray())
|
|
1510
1516
|
a = urandom(60)
|
|
1511
1517
|
c = list(ba2base(n, a))
|
|
1512
1518
|
for _ in range(randrange(80)):
|
|
1513
|
-
c.insert(randint(0, len(c)), choice(
|
|
1519
|
+
c.insert(randint(0, len(c)), choice(whitespace))
|
|
1514
1520
|
s = ''.join(c)
|
|
1515
1521
|
self.assertEqual(base2ba(n, s), a)
|
|
1516
1522
|
|
|
@@ -1534,24 +1540,6 @@ class BaseTests(unittest.TestCase, Util):
|
|
|
1534
1540
|
self.assertEqual(type(s), str)
|
|
1535
1541
|
self.assertEqual(s, res)
|
|
1536
1542
|
|
|
1537
|
-
def test_explicit(self):
|
|
1538
|
-
data = [ # n little big
|
|
1539
|
-
('', 2, '', ''),
|
|
1540
|
-
('1 0 1', 2, '101', '101'),
|
|
1541
|
-
('11 01 00', 4, '320', '310'),
|
|
1542
|
-
('111 001', 8, '74', '71'),
|
|
1543
|
-
('1111 0001', 16, 'f8', 'f1'),
|
|
1544
|
-
('11111 00001', 32, '7Q', '7B'),
|
|
1545
|
-
('111111 000001', 64, '/g', '/B'),
|
|
1546
|
-
]
|
|
1547
|
-
for bs, n, s_le, s_be in data:
|
|
1548
|
-
a_le = bitarray(bs, 'little')
|
|
1549
|
-
a_be = bitarray(bs, 'big')
|
|
1550
|
-
self.assertEQUAL(base2ba(n, s_le, 'little'), a_le)
|
|
1551
|
-
self.assertEQUAL(base2ba(n, s_be, 'big'), a_be)
|
|
1552
|
-
self.assertEqual(ba2base(n, a_le), s_le)
|
|
1553
|
-
self.assertEqual(ba2base(n, a_be), s_be)
|
|
1554
|
-
|
|
1555
1543
|
def test_empty(self):
|
|
1556
1544
|
for n in 2, 4, 8, 16, 32, 64:
|
|
1557
1545
|
a = base2ba(n, '')
|
|
@@ -1577,6 +1565,9 @@ class BaseTests(unittest.TestCase, Util):
|
|
|
1577
1565
|
self.assertRaises(TypeError, base2ba, None, '')
|
|
1578
1566
|
self.assertRaises(TypeError, ba2base, 16.0, a)
|
|
1579
1567
|
self.assertRaises(TypeError, base2ba, 16.0, '')
|
|
1568
|
+
self.assertRaises(TypeError, ba2base, 32, None)
|
|
1569
|
+
self.assertRaises(TypeError, base2ba, 32, None)
|
|
1570
|
+
|
|
1580
1571
|
for values, msg in [
|
|
1581
1572
|
([-1023, -16, -1, 0, 3, 5, 31, 48, 63, 129, 511, 4123],
|
|
1582
1573
|
"base must be a power of 2"),
|
|
@@ -1586,8 +1577,10 @@ class BaseTests(unittest.TestCase, Util):
|
|
|
1586
1577
|
self.assertRaisesMessage(ValueError, msg, ba2base, i, a)
|
|
1587
1578
|
self.assertRaisesMessage(ValueError, msg, base2ba, i, '')
|
|
1588
1579
|
|
|
1589
|
-
|
|
1590
|
-
|
|
1580
|
+
a = bitarray(29)
|
|
1581
|
+
for m in range(2, 7):
|
|
1582
|
+
msg = "bitarray length 29 not multiple of %d" % m
|
|
1583
|
+
self.assertRaisesMessage(ValueError, msg, ba2base, 1 << m, a)
|
|
1591
1584
|
|
|
1592
1585
|
def test_hexadecimal(self):
|
|
1593
1586
|
a = base2ba(16, 'F61', 'big')
|
|
@@ -1607,6 +1600,7 @@ class BaseTests(unittest.TestCase, Util):
|
|
|
1607
1600
|
a = base2ba(32, s, 'big')
|
|
1608
1601
|
self.assertEqual(a.tobytes(), msg)
|
|
1609
1602
|
self.assertEqual(ba2base(32, a), s)
|
|
1603
|
+
self.assertEqual(base64.b32decode(s), msg)
|
|
1610
1604
|
|
|
1611
1605
|
def test_base64(self):
|
|
1612
1606
|
msg = os.urandom(randint(10, 100) * 3)
|
|
@@ -1614,6 +1608,7 @@ class BaseTests(unittest.TestCase, Util):
|
|
|
1614
1608
|
a = base2ba(64, s, 'big')
|
|
1615
1609
|
self.assertEqual(a.tobytes(), msg)
|
|
1616
1610
|
self.assertEqual(ba2base(64, a), s)
|
|
1611
|
+
self.assertEqual(base64.standard_b64decode(s), msg)
|
|
1617
1612
|
|
|
1618
1613
|
def test_primes(self):
|
|
1619
1614
|
primes = gen_primes(60, odd=True)
|
|
@@ -1638,6 +1633,7 @@ class BaseTests(unittest.TestCase, Util):
|
|
|
1638
1633
|
self.assertEqual(s, rep)
|
|
1639
1634
|
b = base2ba(n, rep, endian)
|
|
1640
1635
|
self.assertEqual(b, a)
|
|
1636
|
+
self.assertEqual(type(b), bitarray)
|
|
1641
1637
|
self.assertEqual(b.endian, endian)
|
|
1642
1638
|
|
|
1643
1639
|
alphabets = [
|
|
@@ -1646,6 +1642,7 @@ class BaseTests(unittest.TestCase, Util):
|
|
|
1646
1642
|
(2, 4, '0123'),
|
|
1647
1643
|
(3, 8, '01234567'),
|
|
1648
1644
|
(4, 16, '0123456789abcdef'),
|
|
1645
|
+
(4, 16, '0123456789ABCDEF'),
|
|
1649
1646
|
(5, 32, 'ABCDEFGHIJKLMNOPQRSTUVWXYZ234567'),
|
|
1650
1647
|
(6, 64, 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdef'
|
|
1651
1648
|
'ghijklmnopqrstuvwxyz0123456789+/'),
|
|
@@ -1658,6 +1655,8 @@ class BaseTests(unittest.TestCase, Util):
|
|
|
1658
1655
|
for i, c in enumerate(alphabet):
|
|
1659
1656
|
endian = self.random_endian()
|
|
1660
1657
|
self.assertEqual(ba2int(base2ba(n, c, endian)), i)
|
|
1658
|
+
if m == 4 and c in "ABCDEF":
|
|
1659
|
+
c = chr(ord(c) + 32)
|
|
1661
1660
|
self.assertEqual(ba2base(n, int2ba(i, m, endian)), c)
|
|
1662
1661
|
|
|
1663
1662
|
def test_not_alphabets(self):
|
|
@@ -1666,7 +1665,7 @@ class BaseTests(unittest.TestCase, Util):
|
|
|
1666
1665
|
c = chr(i)
|
|
1667
1666
|
if c in alphabet or c.isspace():
|
|
1668
1667
|
continue
|
|
1669
|
-
if n == 16 and c in
|
|
1668
|
+
if n == 16 and c in hexdigits:
|
|
1670
1669
|
continue
|
|
1671
1670
|
self.assertRaises(ValueError, base2ba, n, c)
|
|
1672
1671
|
|
|
@@ -1674,9 +1673,12 @@ class BaseTests(unittest.TestCase, Util):
|
|
|
1674
1673
|
for _ in range(100):
|
|
1675
1674
|
m = randint(1, 6)
|
|
1676
1675
|
a = urandom_2(m * randrange(100))
|
|
1677
|
-
self.assertEqual(len(a) % m, 0)
|
|
1678
1676
|
n = 1 << m
|
|
1679
1677
|
s = ba2base(n, a, group=randrange(10), sep=randrange(5) * " ")
|
|
1678
|
+
if m == 4 and getrandbits(1):
|
|
1679
|
+
s = s.upper()
|
|
1680
|
+
if getrandbits(1):
|
|
1681
|
+
s = s.encode()
|
|
1680
1682
|
b = base2ba(n, s, a.endian)
|
|
1681
1683
|
self.assertEQUAL(a, b)
|
|
1682
1684
|
self.check_obj(b)
|
|
@@ -2335,9 +2337,9 @@ class MixedTests(unittest.TestCase, Util):
|
|
|
2335
2337
|
self.assertEqual(s[:2], '0b')
|
|
2336
2338
|
a = bitarray(s[2:], 'big')
|
|
2337
2339
|
self.assertEqual(ba2int(a), i)
|
|
2338
|
-
t =
|
|
2339
|
-
self.assertEqual(t, s)
|
|
2340
|
-
self.assertEqual(
|
|
2340
|
+
t = a.to01()
|
|
2341
|
+
self.assertEqual(t, s[2:])
|
|
2342
|
+
self.assertEqual(int(t, 2), i)
|
|
2341
2343
|
|
|
2342
2344
|
def test_oct(self):
|
|
2343
2345
|
for _ in range(20):
|
|
@@ -2346,9 +2348,9 @@ class MixedTests(unittest.TestCase, Util):
|
|
|
2346
2348
|
self.assertEqual(s[:2], '0o')
|
|
2347
2349
|
a = base2ba(8, s[2:], 'big')
|
|
2348
2350
|
self.assertEqual(ba2int(a), i)
|
|
2349
|
-
t =
|
|
2350
|
-
self.assertEqual(t, s)
|
|
2351
|
-
self.assertEqual(
|
|
2351
|
+
t = ba2base(8, a)
|
|
2352
|
+
self.assertEqual(t, s[2:])
|
|
2353
|
+
self.assertEqual(int(t, 8), i)
|
|
2352
2354
|
|
|
2353
2355
|
def test_hex(self):
|
|
2354
2356
|
for _ in range(20):
|
|
@@ -2357,9 +2359,9 @@ class MixedTests(unittest.TestCase, Util):
|
|
|
2357
2359
|
self.assertEqual(s[:2], '0x')
|
|
2358
2360
|
a = hex2ba(s[2:], 'big')
|
|
2359
2361
|
self.assertEqual(ba2int(a), i)
|
|
2360
|
-
t =
|
|
2361
|
-
self.assertEqual(t, s)
|
|
2362
|
-
self.assertEqual(
|
|
2362
|
+
t = ba2hex(a)
|
|
2363
|
+
self.assertEqual(t, s[2:])
|
|
2364
|
+
self.assertEqual(int(t, 16), i)
|
|
2363
2365
|
|
|
2364
2366
|
def test_bitwise(self):
|
|
2365
2367
|
for a in self.randombitarrays(start=1):
|
|
@@ -2407,7 +2409,7 @@ class MixedTests(unittest.TestCase, Util):
|
|
|
2407
2409
|
c <<= n
|
|
2408
2410
|
self.assertEqual(ba2int(c), i << n)
|
|
2409
2411
|
|
|
2410
|
-
#
|
|
2412
|
+
# ---------------------- serialize() deserialize() -----------------------
|
|
2411
2413
|
|
|
2412
2414
|
class SerializationTests(unittest.TestCase, Util):
|
|
2413
2415
|
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: bitarray
|
|
3
|
-
Version: 3.7.
|
|
3
|
+
Version: 3.7.1
|
|
4
4
|
Summary: efficient arrays of booleans -- C extension
|
|
5
5
|
Home-page: https://github.com/ilanschnell/bitarray
|
|
6
6
|
Author: Ilan Schnell
|
|
@@ -89,7 +89,7 @@ Once you have installed the package, you may want to test it:
|
|
|
89
89
|
|
|
90
90
|
$ python -c 'import bitarray; bitarray.test()'
|
|
91
91
|
bitarray is installed in: /Users/ilan/bitarray/bitarray
|
|
92
|
-
bitarray version: 3.7.
|
|
92
|
+
bitarray version: 3.7.1
|
|
93
93
|
sys.version: 3.13.5 (main, Jun 16 2025) [Clang 18.1.8]
|
|
94
94
|
sys.prefix: /Users/ilan/miniforge
|
|
95
95
|
pointer size: 64 bit
|
|
@@ -104,7 +104,7 @@ Once you have installed the package, you may want to test it:
|
|
|
104
104
|
.........................................................................
|
|
105
105
|
................................................................
|
|
106
106
|
----------------------------------------------------------------------
|
|
107
|
-
Ran
|
|
107
|
+
Ran 597 tests in 0.165s
|
|
108
108
|
|
|
109
109
|
OK
|
|
110
110
|
|
|
@@ -256,85 +256,15 @@ shift (``>>``) always shifts towards higher indices.
|
|
|
256
256
|
Bit-endianness
|
|
257
257
|
--------------
|
|
258
258
|
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
When dealing with the machine representation of bitarray objects, it is
|
|
269
|
-
recommended to always explicitly specify the endianness.
|
|
270
|
-
|
|
271
|
-
By default, bitarrays use big-endian representation:
|
|
272
|
-
|
|
273
|
-
.. code-block:: python
|
|
274
|
-
|
|
275
|
-
>>> a = bitarray(b'A')
|
|
276
|
-
>>> a.endian
|
|
277
|
-
'big'
|
|
278
|
-
>>> a
|
|
279
|
-
bitarray('01000001')
|
|
280
|
-
>>> a[6] = 1
|
|
281
|
-
>>> a.tobytes()
|
|
282
|
-
b'C'
|
|
283
|
-
|
|
284
|
-
Big-endian means that the most-significant bit comes first.
|
|
285
|
-
Here, ``a[0]`` is the lowest address (index) and most significant bit,
|
|
286
|
-
and ``a[7]`` is the highest address and least significant bit.
|
|
287
|
-
|
|
288
|
-
When creating a new bitarray object, the endianness can always be
|
|
289
|
-
specified explicitly:
|
|
290
|
-
|
|
291
|
-
.. code-block:: python
|
|
292
|
-
|
|
293
|
-
>>> a = bitarray(b'A', endian='little')
|
|
294
|
-
>>> a
|
|
295
|
-
bitarray('10000010')
|
|
296
|
-
>>> a.endian
|
|
297
|
-
'little'
|
|
298
|
-
|
|
299
|
-
Here, the low-bit comes first because little-endian means that increasing
|
|
300
|
-
numeric significance corresponds to an increasing address.
|
|
301
|
-
So ``a[0]`` is the lowest address and least significant bit,
|
|
302
|
-
and ``a[7]`` is the highest address and most significant bit.
|
|
303
|
-
|
|
304
|
-
The bit-endianness is a property of the bitarray object.
|
|
305
|
-
The endianness cannot be changed once a bitarray object has been created.
|
|
306
|
-
When comparing bitarray objects, the endianness (and hence the machine
|
|
307
|
-
representation) is irrelevant; what matters is the mapping from indices
|
|
308
|
-
to bits:
|
|
309
|
-
|
|
310
|
-
.. code-block:: python
|
|
311
|
-
|
|
312
|
-
>>> bitarray('11001', endian='big') == bitarray('11001', endian='little')
|
|
313
|
-
True
|
|
314
|
-
>>> a = bitarray(b'\x01', endian='little')
|
|
315
|
-
>>> b = bitarray(b'\x80', endian='big')
|
|
316
|
-
>>> a == b
|
|
317
|
-
True
|
|
318
|
-
>>> a.tobytes() == b.tobytes()
|
|
319
|
-
False
|
|
320
|
-
|
|
321
|
-
Bitwise operations (``|``, ``^``, ``&=``, ``|=``, ``^=``, ``~``) are
|
|
322
|
-
implemented efficiently using the corresponding byte operations in C, i.e. the
|
|
323
|
-
operators act on the machine representation of the bitarray objects.
|
|
324
|
-
Therefore, it is not possible to perform bitwise operators on bitarrays
|
|
325
|
-
with different endianness.
|
|
326
|
-
|
|
327
|
-
As mentioned above, the endianness can not be changed once an object is
|
|
328
|
-
created. However, you can create a new bitarray with different endianness:
|
|
329
|
-
|
|
330
|
-
.. code-block:: python
|
|
331
|
-
|
|
332
|
-
>>> a = bitarray('111000', endian='little')
|
|
333
|
-
>>> b = bitarray(a, endian='big')
|
|
334
|
-
>>> b
|
|
335
|
-
bitarray('111000')
|
|
336
|
-
>>> a == b
|
|
337
|
-
True
|
|
259
|
+
For many purposes the bit-endianness is not of any relevance to the end user
|
|
260
|
+
and can be regarded as an implementation detail of bitarray objects.
|
|
261
|
+
However, there are use cases when the bit-endianness becomes important.
|
|
262
|
+
These use cases involve explicitly reading and writing the bitarray buffer
|
|
263
|
+
using ``.tobytes()``, ``.frombytes()``, ``.tofile()`` or ``.fromfile()``,
|
|
264
|
+
importing and exporting buffers. Also, a number of utility functions
|
|
265
|
+
in ``bitarray.util`` will return different results depending on
|
|
266
|
+
bit-endianness, such as ``ba2hex()`` or ``ba2int``.
|
|
267
|
+
To better understand this topic, please read `bit-endianness <https://github.com/ilanschnell/bitarray/blob/master/doc/endianness.rst>`__.
|
|
338
268
|
|
|
339
269
|
|
|
340
270
|
Buffer protocol
|
|
@@ -421,7 +351,7 @@ and can therefore be used as a dictionary key:
|
|
|
421
351
|
Reference
|
|
422
352
|
=========
|
|
423
353
|
|
|
424
|
-
bitarray version: 3.7.
|
|
354
|
+
bitarray version: 3.7.1 -- `change log <https://github.com/ilanschnell/bitarray/blob/master/doc/changelog.rst>`__
|
|
425
355
|
|
|
426
356
|
In the following, ``item`` and ``value`` are usually a single bit -
|
|
427
357
|
an integer 0 or 1.
|
|
@@ -874,6 +804,10 @@ This sub-module was added in version 1.2.
|
|
|
874
804
|
When ``odd=True``, only odd prime numbers are represented in the resulting
|
|
875
805
|
bitarray ``a``, and ``a[i]`` corresponds to ``2*i+1`` being prime or not.
|
|
876
806
|
|
|
807
|
+
Apart from working with prime numbers, this function is useful for
|
|
808
|
+
testing, as it provides a simple way to create a well-defined bitarray
|
|
809
|
+
of any length.
|
|
810
|
+
|
|
877
811
|
New in version 3.7
|
|
878
812
|
|
|
879
813
|
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|