-
Notifications
You must be signed in to change notification settings - Fork 26
Expand file tree
/
Copy pathtest_comparator.py
More file actions
5582 lines (4518 loc) · 180 KB
/
test_comparator.py
File metadata and controls
5582 lines (4518 loc) · 180 KB
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
import array # Add import for array
import ast
import copy
import dataclasses
import datetime
import decimal
import re
import sys
import uuid
import weakref
from collections import ChainMap, Counter, OrderedDict, UserDict, UserList, UserString, defaultdict, deque, namedtuple
from enum import Enum, Flag, IntFlag, auto
from pathlib import Path
import pydantic
import pytest
from codeflash.either import Failure, Success
from codeflash.models.models import FunctionTestInvocation, InvocationId, TestDiffScope, TestResults, TestType
from codeflash.verification.comparator import (
PYTEST_TEMP_PATH_PATTERN,
PYTHON_TEMPFILE_PATTERN,
_extract_exception_from_message,
_get_wrapped_exception,
_is_temp_path,
_normalize_temp_path,
comparator,
)
from codeflash.verification.equivalence import compare_test_results
def test_basic_python_objects() -> None:
a = 5
b = 5
c = 6
d = None
assert comparator(a, b)
assert not comparator(a, c)
assert not comparator(a, d)
a = 5.0
b = 5.0
c = 6.0
d = None
e = None
assert comparator(a, b)
assert not comparator(a, c)
assert not comparator(a, d)
assert not comparator(d, a)
assert comparator(d, e)
a = "Hello"
b = "Hello"
c = "World"
assert comparator(a, b)
assert not comparator(a, c)
a = [1, 2, 3]
b = [1, 2, 3]
c = [1, 2, 4]
assert comparator(a, b)
assert not comparator(a, c)
a = {"a": 1, "b": 2}
b = {"a": 1, "b": 2}
c = {"a": 1, "b": 3}
d = {"c": 1, "b": 2}
e = {"a": 1, "b": 2, "c": 3}
assert comparator(a, b)
assert not comparator(a, c)
assert not comparator(a, d)
assert not comparator(a, e)
a = (1, 2, "str")
b = (1, 2, "str")
c = (1, 2, "str2")
d = [1, 2, "str"]
assert comparator(a, b)
assert not comparator(a, c)
assert not comparator(a, d)
a = {1, 2, 3}
b = {2, 3, 1}
c = {1, 2, 4}
d = {1, 2, 3, 4}
assert comparator(a, b)
assert not comparator(a, c)
assert not comparator(a, d)
a = (65).to_bytes(1, byteorder="big")
b = (65).to_bytes(1, byteorder="big")
c = (66).to_bytes(1, byteorder="big")
assert comparator(a, b)
assert not comparator(a, c)
a = (65).to_bytes(2, byteorder="little")
b = (65).to_bytes(2, byteorder="big")
assert not comparator(a, b)
a = bytearray([65, 64, 63])
b = bytearray([65, 64, 63])
c = bytearray([65, 64, 62])
assert comparator(a, b)
assert not comparator(a, c)
memoryview_a = memoryview(bytearray([65, 64, 63]))
memoryview_b = memoryview(bytearray([65, 64, 63]))
memoryview_c = memoryview(bytearray([65, 64, 62]))
assert comparator(memoryview_a, memoryview_b)
assert not comparator(memoryview_a, memoryview_c)
a = frozenset([1, 2, 3])
b = frozenset([2, 3, 1])
c = frozenset([1, 2, 4])
d = frozenset([1, 2, 3, 4])
assert comparator(a, b)
assert not comparator(a, c)
assert not comparator(a, d)
a = map
b = pow
c = pow
d = abs
assert comparator(b, c)
assert not comparator(a, b)
assert not comparator(c, d)
a = object()
b = object()
c = abs
assert comparator(a, b)
assert not comparator(a, c)
a = type([])
b = type([])
c = type({})
assert comparator(a, b)
assert not comparator(a, c)
def test_weakref() -> None:
"""Test comparator for weakref.ref objects."""
# Helper class that supports weak references and has comparable __dict__
class Holder:
def __init__(self, value):
self.value = value
# Test weak references to the same object
obj = Holder([1, 2, 3])
ref1 = weakref.ref(obj)
ref2 = weakref.ref(obj)
assert comparator(ref1, ref2)
# Test weak references to equivalent but different objects
obj1 = Holder({"key": "value"})
obj2 = Holder({"key": "value"})
ref1 = weakref.ref(obj1)
ref2 = weakref.ref(obj2)
assert comparator(ref1, ref2)
# Test weak references to different objects
obj1 = Holder([1, 2, 3])
obj2 = Holder([1, 2, 4])
ref1 = weakref.ref(obj1)
ref2 = weakref.ref(obj2)
assert not comparator(ref1, ref2)
# Test weak references with different data
obj1 = Holder([1, 2, 3])
obj2 = Holder([1, 2, 3, 4])
ref1 = weakref.ref(obj1)
ref2 = weakref.ref(obj2)
assert not comparator(ref1, ref2)
# Test dead weak references (both dead)
obj1 = Holder([1, 2, 3])
obj2 = Holder([1, 2, 3])
ref1 = weakref.ref(obj1)
ref2 = weakref.ref(obj2)
del obj1
del obj2
# Both refs are now dead, should be equal
assert comparator(ref1, ref2)
# Test one dead, one alive weak reference
obj1 = Holder([1, 2, 3])
obj2 = Holder([1, 2, 3])
ref1 = weakref.ref(obj1)
ref2 = weakref.ref(obj2)
del obj1
# ref1 is dead, ref2 is alive, should not be equal
assert not comparator(ref1, ref2)
assert not comparator(ref2, ref1)
# Test weak references to nested structures
obj1 = Holder({"nested": [1, 2, {"inner": "value"}]})
obj2 = Holder({"nested": [1, 2, {"inner": "value"}]})
ref1 = weakref.ref(obj1)
ref2 = weakref.ref(obj2)
assert comparator(ref1, ref2)
# Test weak references to nested structures with differences
obj1 = Holder({"nested": [1, 2, {"inner": "value1"}]})
obj2 = Holder({"nested": [1, 2, {"inner": "value2"}]})
ref1 = weakref.ref(obj1)
ref2 = weakref.ref(obj2)
assert not comparator(ref1, ref2)
# Test weak references in a dictionary (simulating __dict__ with weakrefs)
obj1 = Holder([1, 2, 3])
obj2 = Holder([1, 2, 3])
dict1 = {"data": 42, "ref": weakref.ref(obj1)}
dict2 = {"data": 42, "ref": weakref.ref(obj2)}
assert comparator(dict1, dict2)
# Test weak references in a dictionary with different referents
obj1 = Holder([1, 2, 3])
obj2 = Holder([4, 5, 6])
dict1 = {"data": 42, "ref": weakref.ref(obj1)}
dict2 = {"data": 42, "ref": weakref.ref(obj2)}
assert not comparator(dict1, dict2)
# Test weak references in a list
obj1 = Holder({"a": 1})
obj2 = Holder({"a": 1})
list1 = [weakref.ref(obj1), "other"]
list2 = [weakref.ref(obj2), "other"]
assert comparator(list1, list2)
def test_weakref_to_custom_objects() -> None:
"""Test comparator for weakref.ref to custom class instances."""
class MyClass:
def __init__(self, value):
self.value = value
# Test weak references to equivalent custom objects
obj1 = MyClass(42)
obj2 = MyClass(42)
ref1 = weakref.ref(obj1)
ref2 = weakref.ref(obj2)
assert comparator(ref1, ref2)
# Test weak references to different custom objects
obj1 = MyClass(42)
obj2 = MyClass(99)
ref1 = weakref.ref(obj1)
ref2 = weakref.ref(obj2)
assert not comparator(ref1, ref2)
# Test weak references to custom objects with nested data
class Container:
def __init__(self, items):
self.items = items
obj1 = Container([1, 2, 3])
obj2 = Container([1, 2, 3])
ref1 = weakref.ref(obj1)
ref2 = weakref.ref(obj2)
assert comparator(ref1, ref2)
obj1 = Container([1, 2, 3])
obj2 = Container([1, 2, 4])
ref1 = weakref.ref(obj1)
ref2 = weakref.ref(obj2)
assert not comparator(ref1, ref2)
def test_weakref_with_callbacks() -> None:
"""Test that weakrefs with callbacks are compared correctly."""
class Holder:
def __init__(self, value):
self.value = value
callback_called = []
def callback(ref):
callback_called.append(ref)
obj1 = Holder([1, 2, 3])
obj2 = Holder([1, 2, 3])
# Weakrefs with callbacks should still compare based on referents
ref1 = weakref.ref(obj1, callback)
ref2 = weakref.ref(obj2, callback)
assert comparator(ref1, ref2)
obj1 = Holder([1, 2, 3])
obj2 = Holder([4, 5, 6])
ref1 = weakref.ref(obj1, callback)
ref2 = weakref.ref(obj2, callback)
assert not comparator(ref1, ref2)
@pytest.mark.parametrize(
"r1, r2, expected",
[
(range(1, 10), range(1, 10), True), # equal
(range(10), range(1, 10), False), # different start
(range(2, 10), range(1, 10), False),
(range(1, 5), range(1, 10), False), # different stop
(range(1, 20), range(1, 10), False),
(range(1, 10, 1), range(1, 10, 2), False), # different step
(range(1, 10, 3), range(1, 10, 2), False),
(range(-5, 0), range(-5, 0), True), # negative ranges
(range(-10, 0), range(-5, 0), False),
(range(5, 1), range(10, 5), True), # empty ranges
(range(5, 1), range(5, 1), True),
(range(7), range(7), True),
(range(7), range(0, 7, 1), True),
(range(7), range(0, 7, 1), True),
],
)
def test_ranges(r1, r2, expected):
assert comparator(r1, r2) == expected
def test_standard_python_library_objects() -> None:
a = datetime.datetime(2020, 2, 2, 2, 2, 2) # type: ignore
b = datetime.datetime(2020, 2, 2, 2, 2, 2) # type: ignore
c = datetime.datetime(2020, 2, 2, 2, 2, 3) # type: ignore
assert comparator(a, b)
assert not comparator(a, c)
a = datetime.date(2020, 2, 2) # type: ignore
b = datetime.date(2020, 2, 2) # type: ignore
c = datetime.date(2020, 2, 3) # type: ignore
assert comparator(a, b)
assert not comparator(a, c)
a = datetime.timedelta(days=1) # type: ignore
b = datetime.timedelta(days=1) # type: ignore
c = datetime.timedelta(days=2) # type: ignore
assert comparator(a, b)
assert not comparator(a, c)
a = datetime.time(2, 2, 2) # type: ignore
b = datetime.time(2, 2, 2) # type: ignore
c = datetime.time(2, 2, 3) # type: ignore
assert comparator(a, b)
assert not comparator(a, c)
a = datetime.timezone.utc # type: ignore
b = datetime.timezone.utc # type: ignore
c = datetime.timezone(datetime.timedelta(hours=1)) # type: ignore
assert comparator(a, b)
assert not comparator(a, c)
a = decimal.Decimal(3.14) # type: ignore
b = decimal.Decimal(3.14) # type: ignore
c = decimal.Decimal(3.15) # type: ignore
assert comparator(a, b)
assert not comparator(a, c)
class Color(Flag):
RED = auto()
GREEN = auto()
BLUE = auto()
class Color2(Enum):
RED = auto()
GREEN = auto()
BLUE = auto()
a = Color.RED # type: ignore
b = Color.RED # type: ignore
c = Color.GREEN # type: ignore
assert comparator(a, b)
assert not comparator(a, c)
a = Color2.RED # type: ignore
b = Color2.RED # type: ignore
c = Color2.GREEN # type: ignore
assert comparator(a, b)
assert not comparator(a, c)
class Color4(IntFlag):
RED = auto()
GREEN = auto()
BLUE = auto()
a = Color4.RED # type: ignore
b = Color4.RED # type: ignore
c = Color4.GREEN # type: ignore
assert comparator(a, b)
assert not comparator(a, c)
a: re.Pattern = re.compile("a")
b: re.Pattern = re.compile("a")
c: re.Pattern = re.compile("b")
d: re.Pattern = re.compile("a", re.IGNORECASE)
assert comparator(a, b)
assert not comparator(a, c)
assert not comparator(a, d)
arr1 = array.array("i", [1, 2, 3])
arr2 = array.array("i", [1, 2, 3])
arr3 = array.array("i", [4, 5, 6])
arr4 = array.array("f", [1.0, 2.0, 3.0])
assert comparator(arr1, arr2)
assert not comparator(arr1, arr3)
assert not comparator(arr1, arr4)
assert not comparator(arr1, [1, 2, 3])
empty_arr_i1 = array.array("i")
empty_arr_i2 = array.array("i")
empty_arr_f = array.array("f")
assert comparator(empty_arr_i1, empty_arr_i2)
assert not comparator(empty_arr_i1, empty_arr_f)
assert not comparator(empty_arr_i1, arr1)
id1 = uuid.uuid4()
id3 = uuid.uuid4()
assert comparator(id1, id1)
assert not comparator(id1, id3)
def test_itertools_count() -> None:
import itertools
# Equal: same start and step (default step=1)
assert comparator(itertools.count(0), itertools.count(0))
assert comparator(itertools.count(5), itertools.count(5))
assert comparator(itertools.count(0, 1), itertools.count(0, 1))
assert comparator(itertools.count(10, 3), itertools.count(10, 3))
# Equal: negative start and step
assert comparator(itertools.count(-5, -2), itertools.count(-5, -2))
# Equal: float start and step
assert comparator(itertools.count(0.5, 0.1), itertools.count(0.5, 0.1))
# Not equal: different start
assert not comparator(itertools.count(0), itertools.count(1))
assert not comparator(itertools.count(5), itertools.count(10))
# Not equal: different step
assert not comparator(itertools.count(0, 1), itertools.count(0, 2))
assert not comparator(itertools.count(0, 1), itertools.count(0, -1))
# Not equal: different type
assert not comparator(itertools.count(0), 0)
assert not comparator(itertools.count(0), [0, 1, 2])
# Equal after partial consumption (both advanced to the same state)
a = itertools.count(0)
b = itertools.count(0)
next(a)
next(b)
assert comparator(a, b)
# Not equal after different consumption
a = itertools.count(0)
b = itertools.count(0)
next(a)
assert not comparator(a, b)
# Works inside containers
assert comparator([itertools.count(0)], [itertools.count(0)])
assert comparator({"key": itertools.count(5, 2)}, {"key": itertools.count(5, 2)})
assert not comparator([itertools.count(0)], [itertools.count(1)])
def test_itertools_repeat() -> None:
import itertools
# Equal: infinite repeat
assert comparator(itertools.repeat(5), itertools.repeat(5))
assert comparator(itertools.repeat("hello"), itertools.repeat("hello"))
# Equal: bounded repeat
assert comparator(itertools.repeat(5, 3), itertools.repeat(5, 3))
assert comparator(itertools.repeat(None, 10), itertools.repeat(None, 10))
# Not equal: different value
assert not comparator(itertools.repeat(5), itertools.repeat(6))
assert not comparator(itertools.repeat(5, 3), itertools.repeat(6, 3))
# Not equal: different count
assert not comparator(itertools.repeat(5, 3), itertools.repeat(5, 4))
# Not equal: bounded vs infinite
assert not comparator(itertools.repeat(5), itertools.repeat(5, 3))
# Not equal: different type
assert not comparator(itertools.repeat(5), 5)
assert not comparator(itertools.repeat(5), [5])
# Equal after partial consumption
a = itertools.repeat(5, 5)
b = itertools.repeat(5, 5)
next(a)
next(b)
assert comparator(a, b)
# Not equal after different consumption
a = itertools.repeat(5, 5)
b = itertools.repeat(5, 5)
next(a)
assert not comparator(a, b)
# Works inside containers
assert comparator([itertools.repeat(5, 3)], [itertools.repeat(5, 3)])
assert not comparator([itertools.repeat(5, 3)], [itertools.repeat(5, 4)])
def test_itertools_cycle() -> None:
import itertools
# Equal: same sequence
assert comparator(itertools.cycle([1, 2, 3]), itertools.cycle([1, 2, 3]))
assert comparator(itertools.cycle("abc"), itertools.cycle("abc"))
# Not equal: different sequence
assert not comparator(itertools.cycle([1, 2, 3]), itertools.cycle([1, 2, 4]))
assert not comparator(itertools.cycle([1, 2, 3]), itertools.cycle([1, 2]))
# Not equal: different type
assert not comparator(itertools.cycle([1, 2, 3]), [1, 2, 3])
# Equal after same partial consumption
a = itertools.cycle([1, 2, 3])
b = itertools.cycle([1, 2, 3])
next(a)
next(b)
assert comparator(a, b)
# Not equal after different consumption
a = itertools.cycle([1, 2, 3])
b = itertools.cycle([1, 2, 3])
next(a)
assert not comparator(a, b)
# Equal after consuming a full cycle
a = itertools.cycle([1, 2, 3])
b = itertools.cycle([1, 2, 3])
for _ in range(3):
next(a)
next(b)
assert comparator(a, b)
# Equal at same position across different full-cycle counts
a = itertools.cycle([1, 2, 3])
b = itertools.cycle([1, 2, 3])
for _ in range(4):
next(a)
for _ in range(7):
next(b)
# Both at position 1 within the cycle (4%3 == 7%3 == 1)
assert comparator(a, b)
# Works inside containers
assert comparator([itertools.cycle([1, 2])], [itertools.cycle([1, 2])])
assert not comparator([itertools.cycle([1, 2])], [itertools.cycle([1, 3])])
def test_itertools_chain() -> None:
import itertools
assert comparator(itertools.chain([1, 2], [3, 4]), itertools.chain([1, 2], [3, 4]))
assert not comparator(itertools.chain([1, 2], [3, 4]), itertools.chain([1, 2], [3, 5]))
assert comparator(itertools.chain.from_iterable([[1, 2], [3]]), itertools.chain.from_iterable([[1, 2], [3]]))
assert comparator(itertools.chain(), itertools.chain())
assert not comparator(itertools.chain([1]), itertools.chain([1, 2]))
def test_itertools_islice() -> None:
import itertools
assert comparator(itertools.islice(range(10), 5), itertools.islice(range(10), 5))
assert not comparator(itertools.islice(range(10), 5), itertools.islice(range(10), 6))
assert comparator(itertools.islice(range(10), 2, 5), itertools.islice(range(10), 2, 5))
assert not comparator(itertools.islice(range(10), 2, 5), itertools.islice(range(10), 2, 6))
def test_itertools_product() -> None:
import itertools
assert comparator(itertools.product("AB", repeat=2), itertools.product("AB", repeat=2))
assert not comparator(itertools.product("AB", repeat=2), itertools.product("AC", repeat=2))
assert comparator(itertools.product([1, 2], [3, 4]), itertools.product([1, 2], [3, 4]))
assert not comparator(itertools.product([1, 2], [3, 4]), itertools.product([1, 2], [3, 5]))
def test_itertools_permutations_combinations() -> None:
import itertools
assert comparator(itertools.permutations("ABC", 2), itertools.permutations("ABC", 2))
assert not comparator(itertools.permutations("ABC", 2), itertools.permutations("ABD", 2))
assert comparator(itertools.combinations("ABCD", 2), itertools.combinations("ABCD", 2))
assert not comparator(itertools.combinations("ABCD", 2), itertools.combinations("ABCD", 3))
assert comparator(
itertools.combinations_with_replacement("ABC", 2), itertools.combinations_with_replacement("ABC", 2)
)
assert not comparator(
itertools.combinations_with_replacement("ABC", 2), itertools.combinations_with_replacement("ABD", 2)
)
def test_itertools_accumulate() -> None:
import itertools
assert comparator(itertools.accumulate([1, 2, 3, 4]), itertools.accumulate([1, 2, 3, 4]))
assert not comparator(itertools.accumulate([1, 2, 3, 4]), itertools.accumulate([1, 2, 3, 5]))
assert comparator(itertools.accumulate([1, 2, 3], initial=10), itertools.accumulate([1, 2, 3], initial=10))
assert not comparator(itertools.accumulate([1, 2, 3], initial=10), itertools.accumulate([1, 2, 3], initial=0))
def test_itertools_filtering() -> None:
import itertools
# compress
assert comparator(
itertools.compress("ABCDEF", [1, 0, 1, 0, 1, 1]), itertools.compress("ABCDEF", [1, 0, 1, 0, 1, 1])
)
assert not comparator(
itertools.compress("ABCDEF", [1, 0, 1, 0, 1, 1]), itertools.compress("ABCDEF", [1, 1, 1, 0, 1, 1])
)
# dropwhile
assert comparator(
itertools.dropwhile(lambda x: x < 5, [1, 4, 6, 4, 1]), itertools.dropwhile(lambda x: x < 5, [1, 4, 6, 4, 1])
)
assert not comparator(
itertools.dropwhile(lambda x: x < 5, [1, 4, 6, 4, 1]), itertools.dropwhile(lambda x: x < 5, [1, 4, 7, 4, 1])
)
# takewhile
assert comparator(
itertools.takewhile(lambda x: x < 5, [1, 4, 6, 4, 1]), itertools.takewhile(lambda x: x < 5, [1, 4, 6, 4, 1])
)
assert not comparator(
itertools.takewhile(lambda x: x < 5, [1, 4, 6, 4, 1]), itertools.takewhile(lambda x: x < 5, [1, 3, 6, 4, 1])
)
# filterfalse
assert comparator(
itertools.filterfalse(lambda x: x % 2, range(10)), itertools.filterfalse(lambda x: x % 2, range(10))
)
def test_itertools_starmap() -> None:
import itertools
assert comparator(
itertools.starmap(pow, [(2, 3), (3, 2), (10, 0)]), itertools.starmap(pow, [(2, 3), (3, 2), (10, 0)])
)
assert not comparator(itertools.starmap(pow, [(2, 3), (3, 2)]), itertools.starmap(pow, [(2, 3), (3, 3)]))
def test_itertools_zip_longest() -> None:
import itertools
assert comparator(
itertools.zip_longest("AB", "xyz", fillvalue="-"), itertools.zip_longest("AB", "xyz", fillvalue="-")
)
assert not comparator(
itertools.zip_longest("AB", "xyz", fillvalue="-"), itertools.zip_longest("AB", "xyz", fillvalue="*")
)
def test_itertools_groupby() -> None:
import itertools
assert comparator(itertools.groupby("AAABBBCC"), itertools.groupby("AAABBBCC"))
assert not comparator(itertools.groupby("AAABBBCC"), itertools.groupby("AAABBCC"))
assert comparator(itertools.groupby([]), itertools.groupby([]))
# With key function
assert comparator(
itertools.groupby([1, 1, 2, 2, 3], key=lambda x: x), itertools.groupby([1, 1, 2, 2, 3], key=lambda x: x)
)
@pytest.mark.skipif(sys.version_info < (3, 10), reason="itertools.pairwise requires Python 3.10+")
def test_itertools_pairwise() -> None:
import itertools
assert comparator(itertools.pairwise([1, 2, 3, 4]), itertools.pairwise([1, 2, 3, 4]))
assert not comparator(itertools.pairwise([1, 2, 3, 4]), itertools.pairwise([1, 2, 3, 5]))
@pytest.mark.skipif(sys.version_info < (3, 12), reason="itertools.batched requires Python 3.12+")
def test_itertools_batched() -> None:
import itertools
assert comparator(itertools.batched("ABCDEFG", 3), itertools.batched("ABCDEFG", 3))
assert not comparator(itertools.batched("ABCDEFG", 3), itertools.batched("ABCDEFG", 2))
def test_itertools_in_containers() -> None:
import itertools
# Itertools objects nested in dicts/lists
assert comparator(
{"a": itertools.chain([1], [2]), "b": itertools.islice(range(5), 3)},
{"a": itertools.chain([1], [2]), "b": itertools.islice(range(5), 3)},
)
assert not comparator([itertools.product("AB", repeat=2)], [itertools.product("AC", repeat=2)])
# Different itertools types should not match
assert not comparator(itertools.chain([1, 2]), itertools.islice([1, 2], 2))
def test_numpy():
try:
import numpy as np
except ImportError:
pytest.skip()
a = np.array([1, 2, 3])
b = np.array([1, 2, 3])
c = np.array([1, 2, 4])
assert comparator(a, b)
assert not comparator(a, c)
d = np.array([[1, 2], [3, 4]])
e = np.array([[1, 2], [3, 4]])
f = np.array([[1, 2], [3, 5]])
assert comparator(d, e)
assert not comparator(d, f)
assert not comparator(a, d)
g = np.array([1.0, 2.0, 3.0])
assert not comparator(a, g)
h = np.float32(1.0)
i = np.float32(1.0)
assert comparator(h, i)
j = np.float64(1.0)
k = np.float64(1.0)
assert not comparator(h, j)
assert comparator(j, k)
l = np.int32(1)
m = np.int32(1)
assert comparator(l, m)
assert not comparator(l, h)
assert not comparator(l, j)
n = np.int64(1)
o = np.int64(1)
assert not comparator(n, l)
assert comparator(n, o)
p = np.uint32(1)
q = np.uint32(1)
assert comparator(p, q)
assert not comparator(p, l)
r = np.uint64(1)
s = np.uint64(1)
assert not comparator(r, p)
assert comparator(r, s)
t = np.bool_(True)
u = np.bool_(True)
assert comparator(t, u)
assert not comparator(t, r)
v = np.complex64(1.0 + 1.0j)
w = np.complex64(1.0 + 1.0j)
assert comparator(v, w)
assert not comparator(v, t)
x = np.complex128(1.0 + 1.0j)
y = np.complex128(1.0 + 1.0j)
assert not comparator(x, v)
assert comparator(x, y)
# Create numpy array with mixed type object
z = np.array([1, 2, "str"], dtype=np.object_)
aa = np.array([1, 2, "str"], dtype=np.object_)
ab = np.array([1, 2, "str2"], dtype=np.object_)
assert comparator(z, aa)
assert not comparator(z, ab)
ac = np.array([1, 2, "str2"])
ad = np.array([1, 2, "str2"])
assert comparator(ac, ad)
# Test for numpy array with nan and inf
ae = np.array([1, 2, np.nan])
af = np.array([1, 2, np.nan])
ag = np.array([1, 2, np.inf])
ah = np.array([1, 2, np.inf])
ai = np.inf
aj = np.inf
ak = np.nan
al = np.nan
assert comparator(ae, af)
assert comparator(ag, ah)
assert not comparator(ae, ag)
assert not comparator(af, ah)
assert comparator(ai, aj)
assert comparator(ak, al)
assert not comparator(ai, ak)
dt = np.dtype([("name", "S10"), ("age", np.int32)])
a_struct = np.array([("Alice", 25)], dtype=dt)
b_struct = np.array([("Alice", 25)], dtype=dt)
c_struct = np.array([("Bob", 30)], dtype=dt)
a_void = a_struct[0]
b_void = b_struct[0]
c_void = c_struct[0]
assert isinstance(a_void, np.void)
assert comparator(a_void, b_void)
assert not comparator(a_void, c_void)
def test_numpy_random_generator():
try:
import numpy as np
except ImportError:
pytest.skip()
# Test numpy.random.Generator (modern API)
# Same seed should produce equal generators
rng1 = np.random.default_rng(seed=42)
rng2 = np.random.default_rng(seed=42)
assert comparator(rng1, rng2)
# Different seeds should produce non-equal generators
rng3 = np.random.default_rng(seed=123)
assert not comparator(rng1, rng3)
# After generating numbers, state changes
rng4 = np.random.default_rng(seed=42)
rng5 = np.random.default_rng(seed=42)
rng4.random() # Advance state
assert not comparator(rng4, rng5)
# Both advanced by same amount should be equal
rng5.random()
assert comparator(rng4, rng5)
# Test with different bit generators
from numpy.random import MT19937, PCG64
rng_pcg1 = np.random.Generator(PCG64(seed=42))
rng_pcg2 = np.random.Generator(PCG64(seed=42))
assert comparator(rng_pcg1, rng_pcg2)
rng_mt1 = np.random.Generator(MT19937(seed=42))
rng_mt2 = np.random.Generator(MT19937(seed=42))
assert comparator(rng_mt1, rng_mt2)
# Different bit generator types should not be equal
assert not comparator(rng_pcg1, rng_mt1)
def test_numpy_random_state():
try:
import numpy as np
except ImportError:
pytest.skip()
# Test numpy.random.RandomState (legacy API)
# Same seed should produce equal states
rs1 = np.random.RandomState(seed=42)
rs2 = np.random.RandomState(seed=42)
assert comparator(rs1, rs2)
# Different seeds should produce non-equal states
rs3 = np.random.RandomState(seed=123)
assert not comparator(rs1, rs3)
# After generating numbers, state changes
rs4 = np.random.RandomState(seed=42)
rs5 = np.random.RandomState(seed=42)
rs4.random() # Advance state
assert not comparator(rs4, rs5)
# Both advanced by same amount should be equal
rs5.random()
assert comparator(rs4, rs5)
# Test state restoration
rs6 = np.random.RandomState(seed=42)
state = rs6.get_state()
rs6.random() # Advance state
rs7 = np.random.RandomState(seed=42)
rs7.set_state(state)
# rs6 advanced, rs7 restored to original state
assert not comparator(rs6, rs7)
def test_scipy():
try:
import scipy as sp # type: ignore
except ImportError:
pytest.skip()
a = sp.sparse.csr_matrix([[1, 0, 0], [0, 0, 3], [4, 0, 5]])
b = sp.sparse.csr_matrix([[1, 0, 0], [0, 0, 3], [4, 0, 5]])
c = sp.sparse.csr_matrix([[1, 0, 0], [0, 0, 3], [4, 0, 6]])
ca = sp.sparse.csr_matrix([[1, 0, 0, 0], [0, 0, 3, 0], [4, 0, 6, 0]])
assert comparator(a, b)
assert not comparator(a, c)
assert not comparator(c, ca)
d = sp.sparse.csc_matrix([[1, 0, 0], [0, 0, 3], [4, 0, 5]])
e = sp.sparse.csc_matrix([[1, 0, 0], [0, 0, 3], [4, 0, 5]])
f = sp.sparse.csc_matrix([[1, 0, 0], [0, 0, 3], [4, 0, 6]])
fa = sp.sparse.csc_matrix([[1, 0, 0, 0], [0, 0, 3, 0], [4, 0, 6, 0]])
assert comparator(d, e)
assert not comparator(d, f)
assert not comparator(a, d)
assert not comparator(c, f)
assert not comparator(f, fa)
g = sp.sparse.lil_matrix([[1, 0, 0], [0, 0, 3], [4, 0, 5]])
h = sp.sparse.lil_matrix([[1, 0, 0], [0, 0, 3], [4, 0, 5]])
i = sp.sparse.lil_matrix([[1, 0, 0], [0, 0, 3], [4, 0, 6]])
assert comparator(g, h)
assert not comparator(g, i)
assert not comparator(a, g)
j = sp.sparse.dok_matrix([[1, 0, 0], [0, 0, 3], [4, 0, 5]])
k = sp.sparse.dok_matrix([[1, 0, 0], [0, 0, 3], [4, 0, 5]])
l = sp.sparse.dok_matrix([[1, 0, 0], [0, 0, 3], [4, 0, 6]])
assert comparator(j, k)
assert not comparator(j, l)
assert not comparator(a, j)
m = sp.sparse.dia_matrix([[1, 0, 0], [0, 0, 3], [4, 0, 5]])
n = sp.sparse.dia_matrix([[1, 0, 0], [0, 0, 3], [4, 0, 5]])
o = sp.sparse.dia_matrix([[1, 0, 0], [0, 0, 3], [4, 0, 6]])
assert comparator(m, n)
assert not comparator(m, o)
assert not comparator(a, m)
p = sp.sparse.coo_matrix([[1, 0, 0], [0, 0, 3], [4, 0, 5]])
q = sp.sparse.coo_matrix([[1, 0, 0], [0, 0, 3], [4, 0, 5]])
r = sp.sparse.coo_matrix([[1, 0, 0], [0, 0, 3], [4, 0, 6]])
assert comparator(p, q)
assert not comparator(p, r)
assert not comparator(a, p)
s = sp.sparse.bsr_matrix([[1, 0, 0], [0, 0, 3], [4, 0, 5]])
t = sp.sparse.bsr_matrix([[1, 0, 0], [0, 0, 3], [4, 0, 5]])
u = sp.sparse.bsr_matrix([[1, 0, 0], [0, 0, 3], [4, 0, 6]])
assert comparator(s, t)
assert not comparator(s, u)
assert not comparator(a, s)
try:
import numpy as np
row = np.array([0, 3, 1, 0])
col = np.array([0, 3, 1, 2])
data = np.array([4, 5, 7, 9])
v = sp.sparse.coo_array((data, (row, col)), shape=(4, 4)).toarray()
w = sp.sparse.coo_array((data, (row, col)), shape=(4, 4)).toarray()
assert comparator(v, w)
except ImportError:
print("Should run tests with numpy installed to test more thoroughly")
def test_pandas():
try:
import pandas as pd
except ImportError:
pytest.skip()
a = pd.DataFrame({"a": [1, 2, 3], "b": [4, 5, 6]})
b = pd.DataFrame({"a": [1, 2, 3], "b": [4, 5, 6]})
c = pd.DataFrame({"a": [1, 2, 3], "b": [4, 5, 7]})
ca = pd.DataFrame({"a": [1, 2, 3, 4], "b": [4, 5, 6, 7]})
assert comparator(a, b)
assert not comparator(a, c)
assert not comparator(c, ca)
ak = pd.DataFrame(
{"a": [datetime.datetime(2020, 2, 2, 2, 2, 2), datetime.datetime(2020, 2, 2, 2, 2, 2)], "b": [4, 5]}
)
al = pd.DataFrame(
{"a": [datetime.datetime(2020, 2, 2, 2, 2, 2), datetime.datetime(2020, 2, 2, 2, 2, 2)], "b": [4, 5]}
)
am = pd.DataFrame(
{"a": [datetime.datetime(2020, 2, 2, 2, 2, 2), datetime.datetime(2020, 2, 2, 2, 2, 3)], "b": [4, 5]}
)
assert comparator(ak, al)
assert not comparator(ak, am)
d = pd.Series([1, 2, 3])
e = pd.Series([1, 2, 3])
f = pd.Series([1, 2, 4])
assert comparator(d, e)
assert not comparator(d, f)
g = pd.Index([1, 2, 3])
h = pd.Index([1, 2, 3])
i = pd.Index([1, 2, 4])
assert comparator(g, h)
assert not comparator(g, i)
j = pd.MultiIndex.from_tuples([(1, 2), (3, 4)])