bitarray 3.5.1__tar.gz → 3.6.0__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.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: bitarray
3
- Version: 3.5.1
3
+ Version: 3.6.0
4
4
  Summary: efficient arrays of booleans -- C extension
5
5
  Home-page: https://github.com/ilanschnell/bitarray
6
6
  Author: Ilan Schnell
@@ -96,7 +96,7 @@ Once you have installed the package, you may want to test it:
96
96
 
97
97
  $ python -c 'import bitarray; bitarray.test()'
98
98
  bitarray is installed in: /Users/ilan/bitarray/bitarray
99
- bitarray version: 3.5.1
99
+ bitarray version: 3.6.0
100
100
  sys.version: 3.13.5 (main, Jun 16 2025) [Clang 18.1.8]
101
101
  sys.prefix: /Users/ilan/miniforge
102
102
  pointer size: 64 bit
@@ -110,7 +110,7 @@ Once you have installed the package, you may want to test it:
110
110
  .........................................................................
111
111
  ................................................................
112
112
  ----------------------------------------------------------------------
113
- Ran 579 tests in 0.162s
113
+ Ran 589 tests in 0.163s
114
114
 
115
115
  OK
116
116
 
@@ -427,7 +427,7 @@ and can therefore be used as a dictionary key:
427
427
  Reference
428
428
  =========
429
429
 
430
- bitarray version: 3.5.1 -- `change log <https://github.com/ilanschnell/bitarray/blob/master/doc/changelog.rst>`__
430
+ bitarray version: 3.6.0 -- `change log <https://github.com/ilanschnell/bitarray/blob/master/doc/changelog.rst>`__
431
431
 
432
432
  In the following, ``item`` and ``value`` are usually a single bit -
433
433
  an integer 0 or 1.
@@ -928,11 +928,25 @@ This sub-module was added in version 1.2.
928
928
  New in version 1.8
929
929
 
930
930
 
931
+ ``random_k(n, /, k, endian=None)`` -> bitarray
932
+ Return (pseudo-) random bitarray of length ``n`` with ``k`` elements
933
+ set to one. Mathematically equivalent to setting (in a bitarray of
934
+ length ``n``) all bits at indices ``random.sample(range(n), k)`` to one.
935
+ The random bitarrays are reproducible when giving Python's ``random.seed()``
936
+ with a specific seed value.
937
+
938
+ This function requires Python 3.9 or higher, as it depends on the standard
939
+ library function ``random.randbytes()``. Raises ``NotImplementedError``
940
+ when Python version is too low.
941
+
942
+ New in version 3.6
943
+
944
+
931
945
  ``random_p(n, /, p=0.5, endian=None)`` -> bitarray
932
- Return (pseudo-) random bitarray of length ``n``. Each bit has probability ``p``
933
- of being one (independent of any other bits). Mathematically equivalent
934
- to ``bitarray((random() < p for _ in range(n)), endian)``, but much faster
935
- for large ``n``. The random bitarrays are reproducible when giving
946
+ Return (pseudo-) random bitarray of length ``n``, where each bit has
947
+ probability ``p`` of being one (independent of any other bits). Mathematically
948
+ equivalent to ``bitarray((random() < p for _ in range(n)), endian)``, but much
949
+ faster for large ``n``. The random bitarrays are reproducible when giving
936
950
  Python's ``random.seed()`` with a specific seed value.
937
951
 
938
952
  This function requires Python 3.12 or higher, as it depends on the standard
@@ -987,6 +1001,13 @@ This sub-module was added in version 1.2.
987
1001
  iteration is stopped as soon as one mismatch is found.
988
1002
 
989
1003
 
1004
+ ``sum_indices(a, /)`` -> int
1005
+ Return sum of indices of all active bits in bitarray ``a``.
1006
+ This is equivalent to ``sum(i for i, v in enumerate(a) if v)``.
1007
+
1008
+ New in version 3.6
1009
+
1010
+
990
1011
  ``urandom(n, /, endian=None)`` -> bitarray
991
1012
  Return random bitarray of length ``n`` (uses ``os.urandom()``).
992
1013
 
@@ -1016,7 +1037,7 @@ This sub-module was added in version 1.2.
1016
1037
  ``xor_indices(a, /)`` -> int
1017
1038
  Return xor reduced indices of all active bits in bitarray ``a``.
1018
1039
  This is essentially equivalent to
1019
- ``reduce(operator.xor, [i for i, v in enumerate(a) if v])``.
1040
+ ``reduce(operator.xor, (i for i, v in enumerate(a) if v))``.
1020
1041
 
1021
1042
  New in version 3.2
1022
1043
 
@@ -64,7 +64,7 @@ Once you have installed the package, you may want to test it:
64
64
 
65
65
  $ python -c 'import bitarray; bitarray.test()'
66
66
  bitarray is installed in: /Users/ilan/bitarray/bitarray
67
- bitarray version: 3.5.1
67
+ bitarray version: 3.6.0
68
68
  sys.version: 3.13.5 (main, Jun 16 2025) [Clang 18.1.8]
69
69
  sys.prefix: /Users/ilan/miniforge
70
70
  pointer size: 64 bit
@@ -78,7 +78,7 @@ Once you have installed the package, you may want to test it:
78
78
  .........................................................................
79
79
  ................................................................
80
80
  ----------------------------------------------------------------------
81
- Ran 579 tests in 0.162s
81
+ Ran 589 tests in 0.163s
82
82
 
83
83
  OK
84
84
 
@@ -395,7 +395,7 @@ and can therefore be used as a dictionary key:
395
395
  Reference
396
396
  =========
397
397
 
398
- bitarray version: 3.5.1 -- `change log <https://github.com/ilanschnell/bitarray/blob/master/doc/changelog.rst>`__
398
+ bitarray version: 3.6.0 -- `change log <https://github.com/ilanschnell/bitarray/blob/master/doc/changelog.rst>`__
399
399
 
400
400
  In the following, ``item`` and ``value`` are usually a single bit -
401
401
  an integer 0 or 1.
@@ -896,11 +896,25 @@ This sub-module was added in version 1.2.
896
896
  New in version 1.8
897
897
 
898
898
 
899
+ ``random_k(n, /, k, endian=None)`` -> bitarray
900
+ Return (pseudo-) random bitarray of length ``n`` with ``k`` elements
901
+ set to one. Mathematically equivalent to setting (in a bitarray of
902
+ length ``n``) all bits at indices ``random.sample(range(n), k)`` to one.
903
+ The random bitarrays are reproducible when giving Python's ``random.seed()``
904
+ with a specific seed value.
905
+
906
+ This function requires Python 3.9 or higher, as it depends on the standard
907
+ library function ``random.randbytes()``. Raises ``NotImplementedError``
908
+ when Python version is too low.
909
+
910
+ New in version 3.6
911
+
912
+
899
913
  ``random_p(n, /, p=0.5, endian=None)`` -> bitarray
900
- Return (pseudo-) random bitarray of length ``n``. Each bit has probability ``p``
901
- of being one (independent of any other bits). Mathematically equivalent
902
- to ``bitarray((random() < p for _ in range(n)), endian)``, but much faster
903
- for large ``n``. The random bitarrays are reproducible when giving
914
+ Return (pseudo-) random bitarray of length ``n``, where each bit has
915
+ probability ``p`` of being one (independent of any other bits). Mathematically
916
+ equivalent to ``bitarray((random() < p for _ in range(n)), endian)``, but much
917
+ faster for large ``n``. The random bitarrays are reproducible when giving
904
918
  Python's ``random.seed()`` with a specific seed value.
905
919
 
906
920
  This function requires Python 3.12 or higher, as it depends on the standard
@@ -955,6 +969,13 @@ This sub-module was added in version 1.2.
955
969
  iteration is stopped as soon as one mismatch is found.
956
970
 
957
971
 
972
+ ``sum_indices(a, /)`` -> int
973
+ Return sum of indices of all active bits in bitarray ``a``.
974
+ This is equivalent to ``sum(i for i, v in enumerate(a) if v)``.
975
+
976
+ New in version 3.6
977
+
978
+
958
979
  ``urandom(n, /, endian=None)`` -> bitarray
959
980
  Return random bitarray of length ``n`` (uses ``os.urandom()``).
960
981
 
@@ -984,7 +1005,7 @@ This sub-module was added in version 1.2.
984
1005
  ``xor_indices(a, /)`` -> int
985
1006
  Return xor reduced indices of all active bits in bitarray ``a``.
986
1007
  This is essentially equivalent to
987
- ``reduce(operator.xor, [i for i, v in enumerate(a) if v])``.
1008
+ ``reduce(operator.xor, (i for i, v in enumerate(a) if v))``.
988
1009
 
989
1010
  New in version 3.2
990
1011
 
@@ -189,21 +189,6 @@ buffers_overlap(bitarrayobject *self, bitarrayobject *other)
189
189
  #undef PIB
190
190
  }
191
191
 
192
- /* setup translation table, which maps each byte to its reversed:
193
- reverse_trans = {0x00, 0x80, 0x40, 0xc0, 0x20, 0xa0, ..., 0xff} */
194
- static void
195
- setup_reverse_trans(void)
196
- {
197
- int j, k;
198
-
199
- for (k = 0; k < 256; k++) {
200
- reverse_trans[k] = 0x00;
201
- for (j = 0; j < 8; j++)
202
- if (k & 128 >> j)
203
- reverse_trans[k] |= 1 << j;
204
- }
205
- }
206
-
207
192
  /* reverse bits in first n characters of p */
208
193
  static void
209
194
  bytereverse(char *p, Py_ssize_t n)
@@ -221,7 +206,7 @@ bytereverse(char *p, Py_ssize_t n)
221
206
  They operate on little-endian and bit-endian bitarrays respectively.
222
207
  As we shift right, we need to start with the highest address and loop
223
208
  downwards such that lower bytes are still unaltered.
224
- See also examples/shift_r8.c
209
+ See also devel/shift_r8.c
225
210
  */
226
211
  static void
227
212
  shift_r8le(unsigned char *buff, Py_ssize_t n, int k)
@@ -321,7 +306,7 @@ shift_r8(bitarrayobject *self, Py_ssize_t a, Py_ssize_t b, int k)
321
306
  }
322
307
 
323
308
  /* Copy n bits from other (starting at b) onto self (starting at a).
324
- Please see examples/copy_n.py for more details.
309
+ Please see devel/copy_n.py for more details.
325
310
 
326
311
  Notes:
327
312
  - self and other may have opposite bit-endianness
@@ -4214,6 +4199,20 @@ Return tuple containing:\n\
4214
4199
 
4215
4200
  #ifndef NDEBUG
4216
4201
 
4202
+ static PyObject *
4203
+ module_setup_table(PyObject *module, PyObject *obj)
4204
+ {
4205
+ char table[256];
4206
+ Py_UCS4 ch;
4207
+
4208
+ assert(PyUnicode_Check(obj));
4209
+ assert(PyUnicode_GET_LENGTH(obj) == 1);
4210
+ ch = PyUnicode_READ_CHAR(obj, 0);
4211
+
4212
+ setup_table(table, ch);
4213
+ return PyBytes_FromStringAndSize(table, 256);
4214
+ }
4215
+
4217
4216
  /* Return zlw(a) as a new bitarray, rather than an int object.
4218
4217
  This makes testing easier, because the int result would depend
4219
4218
  on the machine byteorder. */
@@ -4250,6 +4249,7 @@ static PyMethodDef module_functions[] = {
4250
4249
 
4251
4250
  #ifndef NDEBUG
4252
4251
  /* functions exposed in debug mode for testing */
4252
+ {"_setup_table", (PyCFunction) module_setup_table, METH_O, 0},
4253
4253
  {"_zlw", (PyCFunction) module_zlw, METH_O, 0},
4254
4254
  #endif
4255
4255
 
@@ -4291,7 +4291,9 @@ PyInit__bitarray(void)
4291
4291
  {
4292
4292
  PyObject *m;
4293
4293
 
4294
- setup_reverse_trans();
4294
+ /* setup translation table, which maps each byte to its reversed:
4295
+ reverse_trans = {0x00, 0x80, 0x40, 0xc0, 0x20, 0xa0, ..., 0xff} */
4296
+ setup_table(reverse_trans, 'r');
4295
4297
 
4296
4298
  if ((m = PyModule_Create(&moduledef)) == NULL)
4297
4299
  return NULL;
@@ -265,20 +265,91 @@ Return parity of bitarray `a`.\n\
265
265
  `parity(a)` is equivalent to `a.count() % 2` but more efficient.");
266
266
 
267
267
 
268
+ static PyObject *
269
+ add_uint64(PyObject *number, uint64_t i)
270
+ {
271
+ PyObject *res, *tmp = PyLong_FromUnsignedLongLong(i);
272
+
273
+ res = PyNumber_Add(number, tmp);
274
+ Py_DECREF(tmp);
275
+ Py_DECREF(number);
276
+ return res;
277
+ }
278
+
279
+ static PyObject *
280
+ sum_indices(PyObject *module, PyObject *obj)
281
+ {
282
+ static char table[256];
283
+ static int setup = -1; /* endianness of table */
284
+ PyObject *res;
285
+ bitarrayobject *a;
286
+ Py_ssize_t nbytes, i;
287
+ uint64_t sm = 0; /* accumulated sum */
288
+
289
+ if (ensure_bitarray(obj) < 0)
290
+ return NULL;
291
+
292
+ res = PyLong_FromLong(0);
293
+ a = (bitarrayobject *) obj;
294
+ nbytes = Py_SIZE(a);
295
+ set_padbits(a);
296
+
297
+ if (setup != a->endian) {
298
+ setup_table(table, IS_LE(a) ? 'a' : 'A');
299
+ setup = a->endian;
300
+ }
301
+
302
+ for (i = 0; i < nbytes; i++) {
303
+ unsigned char c = a->ob_item[i];
304
+ if (!c)
305
+ continue;
306
+ sm += ((uint64_t) i) * ((uint64_t) (8 * popcnt_64(c)));
307
+ sm += table[c];
308
+
309
+ if (sm > ((uint64_t ) 1) << 63) {
310
+ /* Flush accumulated sum into Python number object.
311
+ For ones(n) this will already happen when n > 2**32.
312
+ printf("%llu %30zd\n", sm, i); */
313
+ if ((res = add_uint64(res, sm)) == NULL)
314
+ return NULL;
315
+ sm = 0;
316
+ }
317
+ }
318
+ return add_uint64(res, sm);
319
+ }
320
+
321
+ PyDoc_STRVAR(sum_indices_doc,
322
+ "sum_indices(a, /) -> int\n\
323
+ \n\
324
+ Return sum of indices of all active bits in bitarray `a`.\n\
325
+ This is equivalent to `sum(i for i, v in enumerate(a) if v)`.");
326
+
327
+
268
328
  static PyObject *
269
329
  xor_indices(PyObject *module, PyObject *obj)
270
330
  {
331
+ static char table[256];
332
+ static int setup = -1; /* endianness of table */
271
333
  bitarrayobject *a;
272
- Py_ssize_t res = 0, n, i;
334
+ Py_ssize_t res = 0, nbytes, i;
273
335
 
274
336
  if (ensure_bitarray(obj) < 0)
275
337
  return NULL;
276
338
 
277
339
  a = (bitarrayobject *) obj;
278
- n = a->nbits;
279
- for (i = 1; i < n; i++) {
280
- if (getbit(a, i))
281
- res ^= i;
340
+ nbytes = Py_SIZE(a);
341
+ set_padbits(a);
342
+
343
+ if (setup != a->endian) {
344
+ setup_table(table, IS_LE(a) ? 'x' : 'X');
345
+ setup = a->endian;
346
+ }
347
+
348
+ for (i = 0; i < nbytes; i++) {
349
+ unsigned char c = a->ob_item[i];
350
+ if (parity_64(c))
351
+ res ^= i << 3;
352
+ res ^= table[c];
282
353
  }
283
354
  return PyLong_FromSsize_t(res);
284
355
  }
@@ -288,7 +359,7 @@ PyDoc_STRVAR(xor_indices_doc,
288
359
  \n\
289
360
  Return xor reduced indices of all active bits in bitarray `a`.\n\
290
361
  This is essentially equivalent to\n\
291
- `reduce(operator.xor, [i for i, v in enumerate(a) if v])`.");
362
+ `reduce(operator.xor, (i for i, v in enumerate(a) if v))`.");
292
363
 
293
364
  /* --------------------------- binary functions ------------------------ */
294
365
 
@@ -786,6 +857,7 @@ digit_to_int(int m, char c)
786
857
  static int setup = 0;
787
858
  int i;
788
859
 
860
+ assert(1 <= m && m <= 6);
789
861
  if (m < 5) { /* base 2, 4, 8, 16 */
790
862
  i = hex_to_int(c);
791
863
  return i >> m ? -1 : i;
@@ -2084,6 +2156,9 @@ static PyMethodDef module_functions[] = {
2084
2156
  METH_VARARGS, ones_doc},
2085
2157
  {"count_n", (PyCFunction) count_n, METH_VARARGS, count_n_doc},
2086
2158
  {"parity", (PyCFunction) parity, METH_O, parity_doc},
2159
+ {"sum_indices",
2160
+ (PyCFunction) sum_indices,
2161
+ METH_O, sum_indices_doc},
2087
2162
  {"xor_indices",
2088
2163
  (PyCFunction) xor_indices,
2089
2164
  METH_O, xor_indices_doc},
@@ -4,7 +4,7 @@
4
4
 
5
5
  Author: Ilan Schnell
6
6
  */
7
- #define BITARRAY_VERSION "3.5.1"
7
+ #define BITARRAY_VERSION "3.6.0"
8
8
 
9
9
  #ifdef STDC_HEADERS
10
10
  # include <stddef.h>
@@ -225,6 +225,27 @@ swap_bytes(char *p, Py_ssize_t n)
225
225
  }
226
226
  }
227
227
 
228
+ /* write 256 characters into table for given kernel operation */
229
+ static inline void
230
+ setup_table(char *table, char kop)
231
+ {
232
+ int j, k;
233
+ for (k = 0; k < 256; k++) {
234
+ table[k] = 0;
235
+ for (j = 0; j < 8; j++) {
236
+ if (k & 1 << j)
237
+ switch (kop) {
238
+ case 'a': table[k] += j; break;
239
+ case 'A': table[k] += 7 - j; break;
240
+ case 'x': table[k] ^= j; break;
241
+ case 'X': table[k] ^= 7 - j; break;
242
+ case 'r': table[k] |= 128 >> j; break;
243
+ default: Py_UNREACHABLE();
244
+ }
245
+ }
246
+ }
247
+ }
248
+
228
249
  /* Return distance [0..3] to next aligned pointer.
229
250
  While on modern compilers uint64_t pointers may be misaligned, it may
230
251
  cause problems on older ones. Moreover, it may lead to slowdown (even
@@ -43,9 +43,6 @@ SYSINFO = _sysinfo()
43
43
  PTRSIZE = SYSINFO[0] # pointer size in bytes
44
44
  DEBUG = SYSINFO[6]
45
45
 
46
- if DEBUG:
47
- from bitarray._bitarray import _zlw # type: ignore
48
-
49
46
 
50
47
  def buffer_info(a, key=None):
51
48
  fields = (
@@ -581,217 +578,6 @@ class MetaDataTests(unittest.TestCase):
581
578
  a = bitarray(endian=endian)
582
579
  self.assertEqual(a.endian, endian)
583
580
 
584
- # ------------------------- Internal debug tests ----------------------------
585
-
586
- # Internal functionality exposed for the purpose of testing.
587
- # These classes will only be part of the test suite in debug mode.
588
-
589
- @skipIf(not DEBUG)
590
- class ShiftR8_Tests(unittest.TestCase, Util):
591
-
592
- def test_empty(self):
593
- a = bitarray()
594
- a._shift_r8(0, 0, 3)
595
- self.assertEqual(a, bitarray())
596
-
597
- def test_explicit(self):
598
- x = bitarray('11000100 11111111 11100111 10111111 00001000')
599
- y = bitarray('11000100 00000111 11111111 00111101 00001000')
600
- x._shift_r8(1, 4, 5)
601
- self.assertEqual(x, y)
602
- x._shift_r8(2, 1, 5) # start > stop -- do nothing
603
- self.assertEqual(x, y)
604
- x._shift_r8(0, 5, 0) # shift = 0 -- do nothing
605
- self.assertEqual(x, y)
606
-
607
- x = bitarray('11000100 11110')
608
- y = bitarray('00011000 10011')
609
- x._shift_r8(0, 2, 3)
610
- self.assertEqual(x, y)
611
-
612
- x = bitarray('1100011')
613
- y = bitarray('0110001')
614
- x._shift_r8(0, 1, 1)
615
- self.assertEqual(x, y)
616
-
617
- def test_random(self):
618
- for _ in range(2000):
619
- n = randrange(200)
620
- x = urandom_2(n)
621
- a = randint(0, x.nbytes)
622
- b = randint(a, x.nbytes)
623
- k = randrange(8)
624
- y = x.copy()
625
- y[8 * a : 8 * b] >>= k
626
- s = x.to01()
627
- if a < b:
628
- s = s[:8 * a] + k * "0" + s[8 * a : 8 * b - k] + s[8 * b:]
629
- if 8 * b > n:
630
- s = s[:n]
631
- x._shift_r8(a, b, k)
632
- self.assertEqual(x.to01(), s)
633
- self.assertEqual(x, y)
634
- self.assertEqual(x.endian, y.endian)
635
- self.assertEqual(len(x), n)
636
-
637
- @skipIf(not DEBUG)
638
- class CopyN_Tests(unittest.TestCase, Util):
639
-
640
- def test_explicit(self):
641
- x = bitarray('11000100 11110')
642
- # ^^^^ ^
643
- y = bitarray('0101110001')
644
- # ^^^^^
645
- x._copy_n(4, y, 1, 5)
646
- self.assertEqual(x, bitarray('11001011 11110'))
647
- # ^^^^ ^
648
- x = bitarray('10110111 101', 'little')
649
- y = x.copy()
650
- x._copy_n(3, x, 3, 7) # copy region of x onto x
651
- self.assertEqual(x, y)
652
- x._copy_n(3, bitarray(x, 'big'), 3, 7) # as before but other endian
653
- self.assertEqual(x, y)
654
- x._copy_n(5, bitarray(), 0, 0) # copy empty bitarray onto x
655
- self.assertEqual(x, y)
656
-
657
- def test_example(self):
658
- # example given in examples/copy_n.py
659
- y = bitarray(
660
- '00101110 11111001 01011101 11001011 10110000 01011110 011')
661
- x = bitarray(
662
- '01011101 11100101 01110101 01011001 01110100 10001010 01111011')
663
- x._copy_n(21, y, 6, 31)
664
- self.assertEqual(x, bitarray(
665
- '01011101 11100101 01110101 11110010 10111011 10010111 01101011'))
666
-
667
- def check_copy_n(self, N, M, a, b, n):
668
- x = urandom_2(N)
669
- x_lst = x.tolist()
670
- y = x if M < 0 else urandom_2(M)
671
- y_lst = y.tolist()
672
- x_lst[a:a + n] = y_lst[b:b + n]
673
- x._copy_n(a, y, b, n)
674
- self.assertEqual(x, bitarray(x_lst))
675
- self.assertEqual(len(x), N)
676
- self.check_obj(x)
677
-
678
- if M < 0:
679
- return
680
- self.assertEqual(y, bitarray(y_lst))
681
- self.assertEqual(len(y), M)
682
- self.check_obj(y)
683
-
684
- def test_random(self):
685
- for _ in range(1000):
686
- N = randrange(1000)
687
- n = randint(0, N)
688
- a = randint(0, N - n)
689
- b = randint(0, N - n)
690
- self.check_copy_n(N, -1, a, b, n)
691
-
692
- M = randrange(1000)
693
- n = randint(0, min(N, M))
694
- a = randint(0, N - n)
695
- b = randint(0, M - n)
696
- self.check_copy_n(N, M, a, b, n)
697
-
698
- @staticmethod
699
- def getslice(a, start, slicelength):
700
- # this is the Python eqivalent of __getitem__ for slices with step=1
701
- b = bitarray(slicelength, a.endian)
702
- b._copy_n(0, a, start, slicelength)
703
- return b
704
-
705
- def test_getslice(self):
706
- for a in self.randombitarrays():
707
- a_lst = a.tolist()
708
- n = len(a)
709
- i = randint(0, n)
710
- j = randint(i, n)
711
- b = self.getslice(a, i, j - i)
712
- self.assertEqual(b.tolist(), a_lst[i:j])
713
- self.assertEQUAL(b, a[i:j])
714
-
715
- @skipIf(not DEBUG)
716
- class Overlap_Tests(unittest.TestCase, Util):
717
-
718
- def check_overlap(self, a, b, res):
719
- r1 = a._overlap(b)
720
- r2 = b._overlap(a)
721
- self.assertTrue(r1 is r2 is res)
722
- self.check_obj(a)
723
- self.check_obj(b)
724
-
725
- def test_empty(self):
726
- a = bitarray()
727
- self.check_overlap(a, a, False)
728
- b = bitarray()
729
- self.check_overlap(a, b, False)
730
-
731
- def test_distinct(self):
732
- for a in self.randombitarrays():
733
- # buffers overlaps with itself, unless buffer is NULL
734
- self.check_overlap(a, a, bool(a))
735
- b = a.copy()
736
- self.check_overlap(a, b, False)
737
-
738
- def test_shared(self):
739
- a = bitarray(64)
740
- b = bitarray(buffer=a)
741
- self.check_overlap(b, a, True)
742
-
743
- c = bitarray(buffer=memoryview(a)[2:4])
744
- self.check_overlap(c, a, True)
745
-
746
- d = bitarray(buffer=memoryview(a)[5:])
747
- self.check_overlap(d, c, False)
748
- self.check_overlap(d, b, True)
749
-
750
- e = bitarray(buffer=memoryview(a)[3:3])
751
- self.check_overlap(e, c, False)
752
- self.check_overlap(e, d, False)
753
-
754
- def test_shared_random(self):
755
- n = 100 # buffer size in bytes
756
- a = bitarray(8 * n)
757
- for _ in range(1000):
758
- i1 = randint(0, n)
759
- j1 = randint(i1, n)
760
- b1 = bitarray(buffer=memoryview(a)[i1:j1])
761
-
762
- i2 = randint(0, n)
763
- j2 = randint(i2, n)
764
- b2 = bitarray(buffer=memoryview(a)[i2:j2])
765
-
766
- r1, r2 = range(i1, j1), range(i2, j2)
767
- res = bool(r1) and bool(r2) and (i2 in r1 or i1 in r2)
768
- self.check_overlap(b1, b2, res)
769
-
770
- @skipIf(not DEBUG)
771
- class ZLW_Tests(unittest.TestCase, Util):
772
-
773
- def test_zeros(self):
774
- for n in range(200):
775
- a = zeros(n, self.random_endian())
776
- self.assertEqual(_zlw(a), zeros(64))
777
-
778
- def test_ones(self):
779
- for n in range(200):
780
- a = ones(n, self.random_endian())
781
- b = _zlw(a)
782
- r = n % 64
783
- self.assertEqual(b, ones(r) + zeros(64 - r))
784
-
785
- def test_random(self):
786
- for n in range(200):
787
- a = urandom_2(n)
788
- b = _zlw(a)
789
- self.assertEqual(len(b), 64)
790
- self.assertEqual(a.endian, b.endian)
791
- self.assertEqual(b[63], 0) # last bit is always 0
792
- q, r = divmod(n, 64)
793
- self.assertEqual(b, a[64 * q:] + zeros(64 - r))
794
-
795
581
  # -------------------------- (Number) index tests ---------------------------
796
582
 
797
583
  class GetItemTests(unittest.TestCase, Util):