-
Notifications
You must be signed in to change notification settings - Fork 30
/
test.py
executable file
·1784 lines (1509 loc) · 64.6 KB
/
test.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#! /usr/bin/env python3
from __future__ import annotations
import pytest
import os
import re
import sys
import array
import pickle
import random
import operator
import unittest
import functools
from typing import TYPE_CHECKING
from collections.abc import Set, Callable, Iterable, Iterator
import hypothesis.strategies as st
from hypothesis import given, assume, errors, settings, Verbosity, HealthCheck
import pyroaring
settings.register_profile("ci", settings(
max_examples=100, deadline=None))
settings.register_profile("dev", settings(max_examples=10, deadline=None))
settings.register_profile("debug", settings(
max_examples=10, verbosity=Verbosity.verbose, deadline=None))
try:
env = os.getenv('HYPOTHESIS_PROFILE', 'dev')
settings.load_profile(env)
except errors.InvalidArgument:
sys.exit(f'Unknown hypothesis profile: {env}')
bitsize = os.getenv('ROARING_BITSIZE', '32')
if bitsize not in ('32', '64'):
sys.exit(f'Unknown bit size: {bitsize}')
is_32_bits = (bitsize=="32")
if is_32_bits:
from pyroaring import BitMap, FrozenBitMap, AbstractBitMap
else:
from pyroaring import BitMap64 as BitMap, FrozenBitMap64 as FrozenBitMap, AbstractBitMap64 as AbstractBitMap # type: ignore[assignment]
# Note: we could not find a way to type-check both the 32-bit and the 64-bit implementations using a same file.
# Out of simplcity, we therefore decided to only type-check the 32-bit version.
# To type-check the 64-bit version, remove the above if statement to only keep the else part
# (i.e. directly import BitMap64 as BitMap etc.)
uint18 = st.integers(min_value=0, max_value=2**18)
uint32 = st.integers(min_value=0, max_value=2**32 - 1)
uint64 = st.integers(min_value=0, max_value=2**64 - 1)
large_uint64 = st.integers(min_value=2**32, max_value=2**64 - 1)
integer = st.integers(min_value=0, max_value=2**31 - 1)
int64 = st.integers(min_value=-2**63, max_value=2**63 - 1)
range_max_size = 2**18
range_big_step = uint18.flatmap(lambda n:
st.builds(range, st.just(n),
st.integers(
min_value=n + 1, max_value=n + range_max_size),
st.integers(min_value=2**8, max_value=range_max_size // 8)))
range_small_step = uint18.flatmap(lambda n:
st.builds(range, st.just(n),
st.integers(
min_value=n + 1, max_value=n + range_max_size),
st.integers(min_value=1, max_value=2**8)))
range_power2_step = uint18.flatmap(lambda n:
st.builds(range, st.just(n),
st.integers(
min_value=n + 1, max_value=n + range_max_size),
st.integers(min_value=0, max_value=8).flatmap(
lambda n: st.just(2**n),
)))
range_huge_interval = uint18.flatmap(lambda n:
st.builds(range, st.just(n),
st.integers(
min_value=n+2**52, max_value=n+2**63),
st.integers(min_value=2**49, max_value=2**63)))
# Build a list of values of the form a * 2**16 + b with b in [-2,+2]
# In other words, numbers that are close (or equal) to a multiple of 2**16
multiple_2p16 = st.sets(
st.builds(
int.__add__, st.builds(
int.__mul__,
st.integers(min_value=1, max_value=2**32),
st.just(2**16)
),
st.integers(min_value=-2, max_value=+2)
),
max_size=100)
hyp_range = range_big_step | range_small_step | range_power2_step | st.sampled_from(
[range(0, 0)]) # last one is an empty range
if not is_32_bits:
hyp_range = hyp_range | range_huge_interval | multiple_2p16
# would be great to build a true random set, but it takes too long and hypothesis does a timeout...
hyp_set: st.SearchStrategy[set[int]] = st.builds(set, hyp_range)
if is_32_bits:
hyp_array = st.builds(lambda x: array.array('I', x), hyp_range)
else:
hyp_array = st.builds(lambda x: array.array('Q', x), hyp_range)
hyp_collection = hyp_range | hyp_set | hyp_array
hyp_many_collections = st.lists(hyp_collection, min_size=1, max_size=20)
bitmap_cls = st.sampled_from([BitMap, FrozenBitMap])
if TYPE_CHECKING:
from typing_extensions import TypeAlias
HypCollection: TypeAlias = range | set[int] | array.array[int] | list[int]
EitherBitMap = BitMap | FrozenBitMap
EitherSet = set | frozenset # type: ignore[type-arg]
class Util:
comparison_set = random.sample(
range(2**8), 100) + random.sample(range(2**31 - 1), 50)
def compare_with_set(self, bitmap: AbstractBitMap, expected_set: set[int]) -> None:
assert len(bitmap) == len(expected_set)
assert bool(bitmap) == bool(expected_set)
assert set(bitmap) == expected_set
assert sorted(list(bitmap)) == sorted(list(expected_set))
assert BitMap(expected_set, copy_on_write=bitmap.copy_on_write) == bitmap
for value in self.comparison_set:
if value in expected_set:
assert value in bitmap
else:
assert value not in bitmap
@staticmethod
def bitmap_sample(bitmap: AbstractBitMap, size: int) -> list[int]:
indices = random.sample(range(len(bitmap)), size)
return [bitmap[i] for i in indices]
def assert_is_not(self, bitmap1: AbstractBitMap, bitmap2: AbstractBitMap) -> None:
if isinstance(bitmap1, BitMap):
if bitmap1:
bitmap1.remove(bitmap1[0])
else:
bitmap1.add(27)
elif isinstance(bitmap2, BitMap):
if bitmap2:
bitmap2.remove(bitmap1[0])
else:
bitmap2.add(27)
else: # The two are non-mutable, cannot do anything...
return
if bitmap1 == bitmap2:
pytest.fail(
'The two bitmaps are identical (modifying one also modifies the other).')
class TestBasic(Util):
@given(hyp_collection, st.booleans())
@settings(deadline=None)
def test_basic(self, values: HypCollection, cow: bool) -> None:
bitmap = BitMap(copy_on_write=cow)
if is_32_bits:
assert bitmap.copy_on_write == cow
expected_set: set[int] = set()
self.compare_with_set(bitmap, expected_set)
values = list(values)
random.shuffle(values)
size = len(values)
for value in values[:size // 2]:
bitmap.add(value)
expected_set.add(value)
self.compare_with_set(bitmap, expected_set)
for value in values[size // 2:]:
bitmap.add(value)
with pytest.raises(KeyError):
bitmap.add_checked(value)
expected_set.add(value)
self.compare_with_set(bitmap, expected_set)
for value in values[:size // 2]:
bitmap.remove(value)
expected_set.remove(value)
with pytest.raises(KeyError):
bitmap.remove(value)
self.compare_with_set(bitmap, expected_set)
for value in values[size // 2:]:
bitmap.discard(value)
# check that we can discard element not in the bitmap
bitmap.discard(value)
expected_set.discard(value)
self.compare_with_set(bitmap, expected_set)
@given(bitmap_cls, bitmap_cls, hyp_collection, st.booleans())
def test_bitmap_equality(
self,
cls1: type[EitherBitMap],
cls2: type[EitherBitMap],
values: HypCollection,
cow: bool,
) -> None:
bitmap1 = cls1(values, copy_on_write=cow)
bitmap2 = cls2(values, copy_on_write=cow)
assert bitmap1 == bitmap2
@given(bitmap_cls, bitmap_cls, hyp_collection, hyp_collection, st.booleans())
def test_bitmap_unequality(
self,
cls1: type[EitherBitMap],
cls2: type[EitherBitMap],
values1: HypCollection,
values2: HypCollection,
cow: bool,
) -> None:
assume(set(values1) != set(values2))
bitmap1 = cls1(values1, copy_on_write=cow)
bitmap2 = cls2(values2, copy_on_write=cow)
assert bitmap1 != bitmap2
@given(bitmap_cls, hyp_collection, st.booleans())
def test_constructor_values(
self,
cls: type[EitherBitMap],
values: HypCollection,
cow: bool,
) -> None:
bitmap = cls(values, copy_on_write=cow)
expected_set = set(values)
self.compare_with_set(bitmap, expected_set)
@given(bitmap_cls, bitmap_cls, hyp_collection, uint32, st.booleans(), st.booleans())
def test_constructor_copy(
self,
cls1: type[EitherBitMap],
cls2: type[EitherBitMap],
values: HypCollection,
other_value: int,
cow1: bool,
cow2: bool,
) -> None:
bitmap1 = cls1(values, copy_on_write=cow1)
# should be robust even if cow2 != cow1
bitmap2 = cls2(bitmap1, copy_on_write=cow2)
assert bitmap1 == bitmap2
self.assert_is_not(bitmap1, bitmap2)
@given(hyp_collection, hyp_collection, st.booleans())
def test_update(self, initial_values: HypCollection, new_values: HypCollection, cow: bool) -> None:
bm = BitMap(initial_values, cow)
expected = BitMap(bm)
bm.update(new_values)
expected |= BitMap(new_values, copy_on_write=cow)
assert bm == expected
@given(hyp_collection, hyp_collection, st.booleans())
def test_intersection_update(self, initial_values: HypCollection, new_values: HypCollection, cow: bool) -> None:
bm = BitMap(initial_values, cow)
expected = BitMap(bm)
bm.intersection_update(new_values)
expected &= BitMap(new_values, copy_on_write=cow)
assert bm == expected
def wrong_op(self, op: Callable[[BitMap, int], object]) -> None:
bitmap = BitMap()
with pytest.raises(OverflowError):
op(bitmap, -3)
with pytest.raises(OverflowError):
if is_32_bits:
op(bitmap, 2**33)
else:
op(bitmap, 2**65)
with pytest.raises(TypeError):
op(bitmap, 'bla') # type: ignore[arg-type]
def test_wrong_add(self) -> None:
self.wrong_op(lambda bitmap, value: bitmap.add(value))
def test_wrong_contain(self) -> None:
self.wrong_op(lambda bitmap, value: bitmap.__contains__(value))
@given(bitmap_cls)
def test_wrong_constructor_values(self, cls: type[EitherBitMap]) -> None:
with pytest.raises(TypeError): # this should fire a type error!
cls([3, 'bla', 3, 42]) # type: ignore[list-item]
bad_range = range(-3, 0)
with pytest.raises(OverflowError):
cls(bad_range)
@given(bitmap_cls, hyp_collection, st.booleans())
def test_to_array(
self,
cls: type[EitherBitMap],
values: HypCollection,
cow: bool,
) -> None:
bitmap = cls(values, copy_on_write=cow)
result = bitmap.to_array()
if is_32_bits:
expected = array.array('I', sorted(values))
else:
expected = array.array('Q', sorted(values))
assert result == expected
@given(bitmap_cls, st.booleans(), st.integers(min_value=0, max_value=100))
def test_constructor_generator(self, cls: type[EitherBitMap], cow: bool, size: int) -> None:
def generator(n: int) -> Iterator[int]:
for i in range(n):
yield i
bitmap = cls(generator(size), copy_on_write=cow)
assert bitmap == cls(range(size), copy_on_write=cow)
def slice_arg(n: int) -> st.SearchStrategy[int]:
return st.integers(min_value=-n, max_value=n)
class TestSelectRank(Util):
@given(bitmap_cls, hyp_collection, st.booleans())
def test_simple_select(
self,
cls: type[EitherBitMap],
values: HypCollection,
cow: bool,
) -> None:
bitmap = cls(values, copy_on_write=cow)
values = list(bitmap) # enforce sorted order
for i in range(-len(values), len(values)):
assert bitmap[i] == values[i]
@given(bitmap_cls, hyp_collection, uint32, st.booleans())
def test_wrong_selection(
self,
cls: type[EitherBitMap],
values: HypCollection,
n: int,
cow: bool,
) -> None:
bitmap = cls(values, cow)
with pytest.raises(IndexError):
bitmap[len(values)]
with pytest.raises(IndexError):
bitmap[n + len(values)]
with pytest.raises(IndexError):
bitmap[-len(values) - 1]
with pytest.raises(IndexError):
bitmap[-n - len(values) - 1]
def check_slice(
self,
cls: type[EitherBitMap],
values: HypCollection,
start: int | None,
stop: int | None,
step: int | None,
cow: bool,
) -> None:
bitmap = cls(values, copy_on_write=cow)
values = list(bitmap) # enforce sorted order
expected = values[start:stop:step]
expected.sort()
observed = list(bitmap[start:stop:step])
assert expected == observed
@given(bitmap_cls, hyp_collection, slice_arg(2**12), slice_arg(2**12), slice_arg(2**5), st.booleans())
def test_slice_select_non_empty(
self,
cls: type[EitherBitMap],
values: HypCollection,
start: int,
stop: int,
step: int,
cow: bool,
) -> None:
assume(step != 0)
assume(len(range(start, stop, step)) > 0)
self.check_slice(cls, values, start, stop, step, cow)
@given(bitmap_cls, hyp_collection, slice_arg(2**12), slice_arg(2**12), slice_arg(2**5), st.booleans())
def test_slice_select_empty(
self,
cls: type[EitherBitMap],
values: HypCollection,
start: int,
stop: int,
step: int,
cow: bool,
) -> None:
assume(step != 0)
assume(len(range(start, stop, step)) == 0)
self.check_slice(cls, values, start, stop, step, cow)
@given(bitmap_cls, hyp_collection, slice_arg(2**12) | st.none(), slice_arg(2**12) | st.none(), slice_arg(2**5) | st.none(), st.booleans())
def test_slice_select_none(
self,
cls: type[EitherBitMap],
values: HypCollection,
start: int | None,
stop: int | None,
step: int | None,
cow: bool,
) -> None:
assume(step != 0)
self.check_slice(cls, values, start, stop, step, cow)
@given(bitmap_cls, hyp_collection, st.booleans())
def test_simple_rank(
self,
cls: type[EitherBitMap],
values: HypCollection,
cow: bool,
) -> None:
bitmap = cls(values, copy_on_write=cow)
for i, value in enumerate(sorted(values)):
assert bitmap.rank(value) == i + 1
@given(bitmap_cls, hyp_collection, uint18, st.booleans())
def test_general_rank(
self,
cls: type[EitherBitMap],
values: HypCollection,
element: int,
cow: bool,
) -> None:
bitmap = cls(values, copy_on_write=cow)
observed_rank = bitmap.rank(element)
expected_rank = len([n for n in set(values) if n <= element])
assert expected_rank == observed_rank
@given(bitmap_cls, hyp_collection, st.booleans())
def test_min(
self,
cls: type[EitherBitMap],
values: HypCollection,
cow: bool,
) -> None:
assume(len(values) > 0)
bitmap = cls(values, copy_on_write=cow)
assert bitmap.min() == min(values)
@given(bitmap_cls)
def test_wrong_min(self, cls: type[EitherBitMap]) -> None:
bitmap = cls()
with pytest.raises(ValueError):
bitmap.min()
@given(bitmap_cls, hyp_collection, st.booleans())
def test_max(
self,
cls: type[EitherBitMap],
values: HypCollection,
cow: bool,
) -> None:
assume(len(values) > 0)
bitmap = cls(values, copy_on_write=cow)
assert bitmap.max() == max(values)
@given(bitmap_cls)
def test_wrong_max(self, cls: type[EitherBitMap]) -> None:
bitmap = cls()
with pytest.raises(ValueError):
bitmap.max()
@given(bitmap_cls, hyp_collection, uint32, st.booleans())
def test_next_set_bit(
self,
cls: type[EitherBitMap],
values: HypCollection,
other_value: int,
cow: bool,
) -> None:
assume(len(values) > 0)
bitmap = cls(values, copy_on_write=cow)
try:
expected = next(i for i in sorted(values) if i >= other_value)
assert bitmap.next_set_bit(other_value) == expected
except StopIteration:
with pytest.raises(ValueError):
bitmap.next_set_bit(other_value)
@given(bitmap_cls)
def test_wrong_next_set_bit(self, cls: type[EitherBitMap]) -> None:
bitmap = cls()
with pytest.raises(ValueError):
bitmap.next_set_bit(0)
class TestBinaryOperations(Util):
set1: Set[int]
set2: Set[int]
@given(bitmap_cls, bitmap_cls, hyp_collection, hyp_collection, st.booleans())
def test_binary_op(
self,
cls1: type[EitherBitMap],
cls2: type[EitherBitMap],
values1: HypCollection,
values2: HypCollection,
cow: bool,
) -> None:
for op in [operator.or_, operator.and_, operator.xor, operator.sub]:
self.set1 = set(values1)
self.set2 = set(values2)
self.bitmap1 = cls1(values1, cow)
self.bitmap2 = cls2(values2, cow)
old_bitmap1 = cls1(self.bitmap1)
old_bitmap2 = cls2(self.bitmap2)
result_set = op(self.set1, self.set2)
result_bitmap = op(self.bitmap1, self.bitmap2)
assert self.bitmap1 == old_bitmap1
assert self.bitmap2 == old_bitmap2
self.compare_with_set(result_bitmap, result_set)
assert type(self.bitmap1) == type(result_bitmap)
@given(bitmap_cls, hyp_collection, hyp_collection, st.booleans())
def test_binary_op_inplace(
self,
cls2: type[EitherBitMap],
values1: HypCollection,
values2: HypCollection,
cow: bool,
) -> None:
for op in [operator.ior, operator.iand, operator.ixor, operator.isub]:
self.set1 = set(values1)
self.set2 = set(values2)
self.bitmap1 = BitMap(values1, cow)
original = self.bitmap1
self.bitmap2 = cls2(values2, cow)
old_bitmap2 = cls2(self.bitmap2)
op(self.set1, self.set2)
op(self.bitmap1, self.bitmap2)
assert original is self.bitmap1
assert self.bitmap2 == old_bitmap2
self.compare_with_set(self.bitmap1, self.set1)
@given(hyp_collection, st.booleans())
def test_binary_op_inplace_self(
self,
values: HypCollection,
cow: bool,
) -> None:
for op in [operator.ior, operator.iand, operator.ixor, operator.isub]:
self.set = set(values)
self.bitmap = BitMap(values, cow)
original = self.bitmap
op(self.set, self.set)
op(self.bitmap, self.bitmap)
assert original is self.bitmap
self.compare_with_set(self.bitmap, self.set)
@given(bitmap_cls, hyp_collection, hyp_collection, st.booleans())
def test_binary_op_inplace_frozen(
self,
cls2: type[EitherBitMap],
values1: HypCollection,
values2: HypCollection,
cow: bool,
) -> None:
for op in [operator.ior, operator.iand, operator.ixor, operator.isub]:
self.set1 = frozenset(values1)
self.set2 = frozenset(values2)
self.bitmap1 = FrozenBitMap(values1, cow)
old_bitmap1 = FrozenBitMap(self.bitmap1)
self.bitmap2 = cls2(values2, cow)
old_bitmap2 = cls2(self.bitmap2)
new_set = op(self.set1, self.set2)
new_bitmap = op(self.bitmap1, self.bitmap2)
assert self.bitmap1 == old_bitmap1
assert self.bitmap2 == old_bitmap2
self.compare_with_set(new_bitmap, new_set)
class TestComparison(Util):
@given(bitmap_cls, bitmap_cls, hyp_collection, hyp_collection, st.booleans())
def test_comparison(
self,
cls1: type[EitherBitMap],
cls2: type[EitherBitMap],
values1: HypCollection,
values2: HypCollection,
cow: bool,
) -> None:
for op in [operator.le, operator.ge, operator.lt, operator.gt, operator.eq, operator.ne]:
self.set1 = set(values1)
self.set2 = set(values2)
self.bitmap1 = cls1(values1, copy_on_write=cow)
self.bitmap2 = cls2(values2, copy_on_write=cow)
assert op(self.bitmap1, self.bitmap1) == \
op(self.set1, self.set1)
assert op(self.bitmap1, self.bitmap2) == \
op(self.set1, self.set2)
assert op(self.bitmap1 | self.bitmap2, self.bitmap2) == \
op(self.set1 | self.set2, self.set2)
assert op(self.set1, self.set1 | self.set2) == \
op(self.set1, self.set1 | self.set2)
@given(bitmap_cls, hyp_collection, st.booleans())
def test_comparison_other_objects(self, cls: type[EitherBitMap], values: HypCollection, cow: bool) -> None:
for op in [operator.le, operator.ge, operator.lt, operator.gt]:
bm = cls(values, copy_on_write=cow)
with pytest.raises(TypeError):
op(bm, 42)
with pytest.raises(TypeError):
op(bm, None)
@given(bitmap_cls, bitmap_cls, hyp_collection, hyp_collection, st.booleans())
def test_intersect(
self,
cls1: type[EitherBitMap],
cls2: type[EitherBitMap],
values1: HypCollection,
values2: HypCollection,
cow: bool,
) -> None:
bm1 = cls1(values1, copy_on_write=cow)
bm2 = cls2(values2, copy_on_write=cow)
assert (bm1.intersect(bm2)) == (len(bm1 & bm2) > 0)
@given(bitmap_cls, hyp_collection, st.booleans())
def test_eq_other_objects(self, cls: type[EitherBitMap], values: HypCollection, cow: bool) -> None:
bm = cls(values, copy_on_write=cow)
assert not bm == 42
assert cls.__eq__(bm, 42) is NotImplemented
assert not bm == None# noqa: E711
assert cls.__eq__(bm, None) is NotImplemented
@given(bitmap_cls, hyp_collection, st.booleans())
def test_ne_other_objects(self, cls: type[EitherBitMap], values: HypCollection, cow: bool) -> None:
bm = cls(values, copy_on_write=cow)
assert bm != 42
assert cls.__ne__(bm, 42) is NotImplemented
assert bm != None# noqa: E711
assert cls.__ne__(bm, None) is NotImplemented
class TestRange(Util):
@given(bitmap_cls, hyp_collection, st.booleans(), uint32, uint32)
def test_contains_range_arbitrary(
self,
cls: type[EitherBitMap],
values: HypCollection,
cow: bool,
start: int,
end: int,
) -> None:
bm = cls(values)
expected = (cls(range(start, end)) <= bm)
assert expected == bm.contains_range(start, end)
@given(bitmap_cls, st.booleans(), uint32, uint32)
def test_contains_range(self, cls: type[EitherBitMap], cow: bool, start: int, end: int) -> None:
assume(start < end)
assert cls(range(start, end)).contains_range(start, end)
assert cls(range(start, end)).contains_range(start, end - 1)
assert not cls(range(start, end - 1)).contains_range(start, end)
assert cls(range(start, end)).contains_range(start + 1, end)
assert not cls(range(start + 1, end)).contains_range(start, end)
r = range(start, end)
try:
middle = r[len(r) // 2] # on 32bits systems, this call might fail when len(r) is too large
except OverflowError:
if sys.maxsize > 2**32:
raise
else:
return
bm = cls(range(start, end)) - cls([middle])
assert not bm.contains_range(start, end)
assert bm.contains_range(start, middle)
assert bm.contains_range(middle + 1, end)
@given(hyp_collection, st.booleans(), uint32, uint32)
def test_add_remove_range(self, values: HypCollection, cow: bool, start: int, end: int) -> None:
assume(start < end)
bm = BitMap(values, copy_on_write=cow)
# Empty range
original = BitMap(bm)
bm.add_range(end, start)
assert bm == original
bm.remove_range(end, start)
assert bm == original
# Adding the range
bm.add_range(start, end)
assert bm.contains_range(start, end)
assert bm.intersection_cardinality(BitMap(range(start, end), copy_on_write=cow)) == end - start
# Empty range (again)
original = BitMap(bm)
bm.remove_range(end, start)
assert bm == original
assert bm.intersection_cardinality(BitMap(range(start, end), copy_on_write=cow)) == end - start
# Removing the range
bm.remove_range(start, end)
assert not bm.contains_range(start, end)
assert bm.intersection_cardinality(BitMap(range(start, end), copy_on_write=cow)) == 0
@pytest.mark.skipif(not is_32_bits, reason="build a too large bitmap with 64 bits, blows up memory")
@given(hyp_collection, st.booleans(), large_uint64, large_uint64)
def test_large_values(self, values: HypCollection, cow: bool, start: int, end: int) -> None:
bm = BitMap(values, copy_on_write=cow)
original = BitMap(bm)
bm.add_range(start, end)
assert bm == original
bm.remove_range(start, end)
assert bm == original
assert bm.contains_range(start, end)
class TestCardinality(Util):
@given(bitmap_cls, bitmap_cls, hyp_collection, hyp_collection, st.booleans())
def test_cardinality(
self,
cls1: type[EitherBitMap],
cls2: type[EitherBitMap],
values1: HypCollection,
values2: HypCollection,
cow: bool,
) -> None:
for real_op, estimated_op in [
(operator.or_, cls1.union_cardinality),
(operator.and_, cls1.intersection_cardinality),
(operator.sub, cls1.difference_cardinality),
(operator.xor, cls1.symmetric_difference_cardinality),
]:
self.bitmap1 = cls1(values1, copy_on_write=cow)
self.bitmap2 = cls2(values2, copy_on_write=cow)
real_value = len(real_op(self.bitmap1, self.bitmap2))
estimated_value = estimated_op(self.bitmap1, self.bitmap2)
assert real_value == estimated_value
@given(bitmap_cls, bitmap_cls, hyp_collection, hyp_collection, st.booleans())
def test_jaccard_index(
self,
cls1: type[EitherBitMap],
cls2: type[EitherBitMap],
values1: HypCollection,
values2: HypCollection,
cow: bool,
) -> None:
assume(len(values1) > 0 or len(values2) > 0)
self.bitmap1 = cls1(values1, copy_on_write=cow)
self.bitmap2 = cls2(values2, copy_on_write=cow)
real_value = float(len(self.bitmap1 & self.bitmap2)) / \
float(max(1, len(self.bitmap1 | self.bitmap2)))
estimated_value = self.bitmap1.jaccard_index(self.bitmap2)
assert real_value == pytest.approx(estimated_value)
@given(bitmap_cls, hyp_collection, uint32, uint32)
def test_range_cardinality(
self,
cls: type[EitherBitMap],
values: HypCollection,
a: int,
b: int,
) -> None:
bm = cls(values)
start, end = sorted([a, b])
# make an intersection with the relevant range to test against
test_bm = bm.intersection(BitMap(range(start, end)))
assert len(test_bm) == bm.range_cardinality(start, end)
class TestManyOperations(Util):
all_bitmaps: Iterable[AbstractBitMap]
@given(hyp_collection, hyp_many_collections, st.booleans())
def test_update(
self,
initial_values: HypCollection,
all_values: list[HypCollection],
cow: bool,
) -> None:
self.initial_bitmap = BitMap(initial_values, copy_on_write=cow)
self.all_bitmaps = [BitMap(values, copy_on_write=cow)
for values in all_values]
self.initial_bitmap.update(*all_values)
expected_result = functools.reduce(
lambda x, y: x | y, self.all_bitmaps + [self.initial_bitmap])
assert expected_result == self.initial_bitmap
assert type(expected_result) == type(self.initial_bitmap)
@given(hyp_collection, hyp_many_collections, st.booleans())
def test_intersection_update(
self,
initial_values: HypCollection,
all_values: list[HypCollection],
cow: bool,
) -> None:
self.initial_bitmap = BitMap(initial_values, copy_on_write=cow)
self.all_bitmaps = [BitMap(values, copy_on_write=cow)
for values in all_values]
self.initial_bitmap.intersection_update(*all_values)
expected_result = functools.reduce(
lambda x, y: x & y, self.all_bitmaps + [self.initial_bitmap])
assert expected_result == self.initial_bitmap
assert type(expected_result) == type(self.initial_bitmap)
@given(bitmap_cls, st.data(), hyp_many_collections, st.booleans())
def test_union(
self,
cls: type[EitherBitMap],
data: st.DataObject,
all_values: list[HypCollection],
cow: bool,
) -> None:
classes = [data.draw(bitmap_cls) for _ in range(len(all_values))]
self.all_bitmaps = [classes[i](values, copy_on_write=cow)
for i, values in enumerate(all_values)]
result = cls.union(*self.all_bitmaps)
expected_result = functools.reduce(
lambda x, y: x | y, self.all_bitmaps)
assert expected_result == result
@given(bitmap_cls, st.data(), hyp_many_collections, st.booleans())
def test_intersection(
self,
cls: type[EitherBitMap],
data: st.DataObject,
all_values: list[HypCollection],
cow: bool,
) -> None:
classes = [data.draw(bitmap_cls) for _ in range(len(all_values))]
self.all_bitmaps = [classes[i](values, copy_on_write=cow)
for i, values in enumerate(all_values)]
result = cls.intersection(*self.all_bitmaps)
expected_result = functools.reduce(
lambda x, y: x & y, self.all_bitmaps)
assert expected_result == result
@given(bitmap_cls, st.data(), hyp_many_collections, st.booleans())
def test_difference(
self,
cls: type[EitherBitMap],
data: st.DataObject,
all_values: list[HypCollection],
cow: bool,
) -> None:
classes = [data.draw(bitmap_cls) for _ in range(len(all_values))]
self.all_bitmaps = [classes[i](values, copy_on_write=cow)
for i, values in enumerate(all_values)]
result = cls.difference(*self.all_bitmaps)
expected_result = functools.reduce(
lambda x, y: x - y, self.all_bitmaps)
assert expected_result == result
class TestSerialization(Util):
@given(bitmap_cls, bitmap_cls, hyp_collection)
def test_serialization(
self,
cls1: type[EitherBitMap],
cls2: type[EitherBitMap],
values: HypCollection,
) -> None:
old_bm = cls1(values)
buff = old_bm.serialize()
new_bm = cls2.deserialize(buff)
assert old_bm == new_bm
assert isinstance(new_bm, cls2)
self.assert_is_not(old_bm, new_bm)
@given(bitmap_cls, hyp_collection, st.integers(min_value=2, max_value=pickle.HIGHEST_PROTOCOL))
def test_pickle_protocol(
self,
cls: type[EitherBitMap],
values: HypCollection,
protocol: int,
) -> None:
old_bm = cls(values)
pickled = pickle.dumps(old_bm, protocol=protocol)
new_bm = pickle.loads(pickled)
assert old_bm == new_bm
self.assert_is_not(old_bm, new_bm)
class TestStatistics(Util):
@given(bitmap_cls, hyp_collection, st.booleans())
def test_basic_properties(
self,
cls: type[EitherBitMap],
values: HypCollection,
cow: bool,
) -> None:
bitmap = cls(values, copy_on_write=cow)
stats = bitmap.get_statistics()
assert stats['n_values_array_containers'] + stats['n_values_bitset_containers'] \
+ stats['n_values_run_containers'] == len(bitmap)
assert stats['n_bytes_array_containers'] == \
2 * stats['n_values_array_containers']
assert stats['n_bytes_bitset_containers'] == \
2**13 * stats['n_bitset_containers']
if len(values) > 0:
assert stats['min_value'] == bitmap[0]
assert stats['max_value'] == bitmap[len(bitmap) - 1]
assert stats['cardinality'] == len(bitmap)
@given(bitmap_cls)
def test_implementation_properties_array(self, cls: type[EitherBitMap]) -> None:
values = range(2**16 - 10, 2**16 + 10, 2)
stats = cls(values).get_statistics()
assert stats['n_array_containers'] == 2
assert stats['n_bitset_containers'] == 0
assert stats['n_run_containers'] == 0
assert stats['n_values_array_containers'] == len(values)
assert stats['n_values_bitset_containers'] == 0
assert stats['n_values_run_containers'] == 0
@given(bitmap_cls)
def test_implementation_properties_bitset(self, cls: type[EitherBitMap]) -> None:
values = range(2**0, 2**17, 2)
stats = cls(values).get_statistics()
assert stats['n_array_containers'] == 0
assert stats['n_bitset_containers'] == 2
assert stats['n_run_containers'] == 0
assert stats['n_values_array_containers'] == 0
assert stats['n_values_bitset_containers'] == len(values)
assert stats['n_values_run_containers'] == 0
@given(bitmap_cls)
def test_implementation_properties_run(self, cls: type[EitherBitMap]) -> None:
values = range(2**0, 2**17, 1)
stats = cls(values).get_statistics()
assert stats['n_array_containers'] == 0
assert stats['n_bitset_containers'] == 0
assert stats['n_run_containers'] == 2
assert stats['n_values_array_containers'] == 0
assert stats['n_values_bitset_containers'] == 0
assert stats['n_values_run_containers'] == len(values)
assert stats['n_bytes_run_containers'] == 12
class TestFlip(Util):
def check_flip(self, bm_before: AbstractBitMap, bm_after: AbstractBitMap, start: int, end: int) -> None:
size = 100
iter_range = random.sample(
range(start, end), min(size, len(range(start, end))))
iter_before = self.bitmap_sample(bm_before, min(size, len(bm_before)))
iter_after = self.bitmap_sample(bm_after, min(size, len(bm_after)))
for elt in iter_range:
if elt in bm_before:
assert elt not in bm_after
else:
assert elt in bm_after
for elt in iter_before:
if not (start <= elt < end):
assert elt in bm_after
for elt in iter_after:
if not (start <= elt < end):
assert elt in bm_before
@given(bitmap_cls, hyp_collection, integer, integer, st.booleans())
def test_flip_empty(
self,
cls: type[EitherBitMap],
values: HypCollection,
start: int,
end: int,
cow: bool,
) -> None:
assume(start >= end)
bm_before = cls(values, copy_on_write=cow)
bm_copy = cls(bm_before)
bm_after = bm_before.flip(start, end)
assert bm_before == bm_copy
assert bm_before == bm_after
@given(bitmap_cls, hyp_collection, integer, integer, st.booleans())
def test_flip(
self,
cls: type[EitherBitMap],
values: HypCollection,
start: int,
end: int,
cow: bool,
) -> None:
assume(start < end)
bm_before = cls(values, copy_on_write=cow)
bm_copy = cls(bm_before)
bm_after = bm_before.flip(start, end)
assert bm_before == bm_copy
self.check_flip(bm_before, bm_after, start, end)
@given(hyp_collection, integer, integer, st.booleans())
def test_flip_inplace_empty(