forked from rust-lang/rust
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtests.rs
1240 lines (1069 loc) · 43.3 KB
/
tests.rs
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
use std::cell::Cell;
use std::cmp::Ordering;
use std::fmt::Debug;
use std::panic::{self, AssertUnwindSafe};
use std::rc::Rc;
use std::{env, fs};
use crate::sort::ffi_types::{F128, FFIOneKibiByte};
use crate::sort::{Sort, known_good_stable_sort, patterns};
#[cfg(miri)]
const TEST_LENGTHS: &[usize] = &[2, 3, 4, 7, 10, 15, 20, 24, 33, 50, 100, 171, 300];
// node.js gives out of memory error to use with length 1_100_000
#[cfg(all(not(miri), target_os = "emscripten"))]
const TEST_LENGTHS: &[usize] = &[
2, 3, 4, 5, 6, 7, 8, 9, 10, 15, 16, 17, 20, 24, 30, 32, 33, 35, 50, 100, 200, 500, 1_000,
2_048, 5_000, 10_000, 100_000,
];
#[cfg(all(not(miri), not(target_os = "emscripten")))]
const TEST_LENGTHS: &[usize] = &[
2, 3, 4, 5, 6, 7, 8, 9, 10, 15, 16, 17, 20, 24, 30, 32, 33, 35, 50, 100, 200, 500, 1_000,
2_048, 5_000, 10_000, 100_000, 1_100_000,
];
fn check_is_sorted<T: Ord + Clone + Debug, S: Sort>(v: &mut [T]) {
let seed = patterns::get_or_init_rand_seed();
let is_small_test = v.len() <= 100;
let v_orig = v.to_vec();
<S as Sort>::sort(v);
assert_eq!(v.len(), v_orig.len());
for window in v.windows(2) {
if window[0] > window[1] {
let mut known_good_sorted_vec = v_orig.clone();
known_good_stable_sort::sort(known_good_sorted_vec.as_mut_slice());
if is_small_test {
eprintln!("Original: {:?}", v_orig);
eprintln!("Expected: {:?}", known_good_sorted_vec);
eprintln!("Got: {:?}", v);
} else {
if env::var("WRITE_LARGE_FAILURE").is_ok() {
// Large arrays output them as files.
let original_name = format!("original_{}.txt", seed);
let std_name = format!("known_good_sorted_{}.txt", seed);
let testsort_name = format!("{}_sorted_{}.txt", S::name(), seed);
fs::write(&original_name, format!("{:?}", v_orig)).unwrap();
fs::write(&std_name, format!("{:?}", known_good_sorted_vec)).unwrap();
fs::write(&testsort_name, format!("{:?}", v)).unwrap();
eprintln!(
"Failed comparison, see files {original_name}, {std_name}, and {testsort_name}"
);
} else {
eprintln!(
"Failed comparison, re-run with WRITE_LARGE_FAILURE env var set, to get output."
);
}
}
panic!("Test assertion failed!")
}
}
}
fn test_is_sorted<T: Ord + Clone + Debug, S: Sort>(
test_len: usize,
map_fn: impl Fn(i32) -> T,
pattern_fn: impl Fn(usize) -> Vec<i32>,
) {
let mut test_data: Vec<T> = pattern_fn(test_len).into_iter().map(map_fn).collect();
check_is_sorted::<T, S>(test_data.as_mut_slice());
}
trait DynTrait: Debug {
fn get_val(&self) -> i32;
}
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord)]
struct DynValA {
value: i32,
}
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord)]
struct DynValB {
value: u64,
}
impl DynTrait for DynValA {
fn get_val(&self) -> i32 {
self.value
}
}
impl DynTrait for DynValB {
fn get_val(&self) -> i32 {
let bytes = self.value.to_ne_bytes();
i32::from_ne_bytes([bytes[0], bytes[1], bytes[6], bytes[7]])
}
}
impl PartialOrd for dyn DynTrait {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
Some(self.cmp(other))
}
}
impl Ord for dyn DynTrait {
fn cmp(&self, other: &Self) -> Ordering {
self.get_val().cmp(&other.get_val())
}
}
impl PartialEq for dyn DynTrait {
fn eq(&self, other: &Self) -> bool {
self.get_val() == other.get_val()
}
}
impl Eq for dyn DynTrait {}
fn shift_i32_to_u32(val: i32) -> u32 {
(val as i64 + (i32::MAX as i64 + 1)) as u32
}
fn reverse_shift_i32_to_u32(val: u32) -> i32 {
(val as i64 - (i32::MAX as i64 + 1)) as i32
}
fn extend_i32_to_u64(val: i32) -> u64 {
// Extends the value into the 64 bit range,
// while preserving input order.
(shift_i32_to_u32(val) as u64) * i32::MAX as u64
}
fn extend_i32_to_u128(val: i32) -> u128 {
// Extends the value into the 64 bit range,
// while preserving input order.
(shift_i32_to_u32(val) as u128) * i64::MAX as u128
}
fn dyn_trait_from_i32(val: i32) -> Rc<dyn DynTrait> {
if val % 2 == 0 {
Rc::new(DynValA { value: val })
} else {
Rc::new(DynValB { value: extend_i32_to_u64(val) })
}
}
fn i32_from_i32(val: i32) -> i32 {
val
}
fn i32_from_i32_ref(val: &i32) -> i32 {
*val
}
fn string_from_i32(val: i32) -> String {
format!("{:010}", shift_i32_to_u32(val))
}
fn i32_from_string(val: &String) -> i32 {
reverse_shift_i32_to_u32(val.parse::<u32>().unwrap())
}
fn cell_i32_from_i32(val: i32) -> Cell<i32> {
Cell::new(val)
}
fn i32_from_cell_i32(val: &Cell<i32>) -> i32 {
val.get()
}
fn calc_comps_required<T, S: Sort>(v: &mut [T], mut cmp_fn: impl FnMut(&T, &T) -> Ordering) -> u32 {
let mut comp_counter = 0u32;
<S as Sort>::sort_by(v, |a, b| {
comp_counter += 1;
cmp_fn(a, b)
});
comp_counter
}
#[derive(PartialEq, Eq, Debug, Clone)]
#[repr(C)]
struct CompCount {
val: i32,
comp_count: Cell<u32>,
}
impl CompCount {
fn new(val: i32) -> Self {
Self { val, comp_count: Cell::new(0) }
}
}
/// Generates $base_name_pattern_name_impl functions calling the test_fns for all test_len.
macro_rules! gen_sort_test_fns {
(
$base_name:ident,
$test_fn:expr,
$test_lengths:expr,
[$(($pattern_name:ident, $pattern_fn:expr)),* $(,)?] $(,)?
) => {
$(fn ${concat($base_name, _, $pattern_name, _impl)}<S: Sort>() {
for test_len in $test_lengths {
$test_fn(*test_len, $pattern_fn);
}
})*
};
}
/// Generates $base_name_pattern_name_impl functions calling the test_fns for all test_len,
/// with a default set of patterns that can be extended by the caller.
macro_rules! gen_sort_test_fns_with_default_patterns {
(
$base_name:ident,
$test_fn:expr,
$test_lengths:expr,
[$(($pattern_name:ident, $pattern_fn:expr)),* $(,)?] $(,)?
) => {
gen_sort_test_fns!(
$base_name,
$test_fn,
$test_lengths,
[
(random, patterns::random),
(random_z1, |len| patterns::random_zipf(len, 1.0)),
(random_d2, |len| patterns::random_uniform(len, 0..2)),
(random_d20, |len| patterns::random_uniform(len, 0..16)),
(random_s95, |len| patterns::random_sorted(len, 95.0)),
(ascending, patterns::ascending),
(descending, patterns::descending),
(saw_mixed, |len| patterns::saw_mixed(
len,
((len as f64).log2().round()) as usize
)),
$(($pattern_name, $pattern_fn),)*
]
);
};
}
/// Generates $base_name_type_pattern_name_impl functions calling the test_fns for all test_len for
/// three types that cover the core specialization differences in the sort implementations, with a
/// default set of patterns that can be extended by the caller.
macro_rules! gen_sort_test_fns_with_default_patterns_3_ty {
(
$base_name:ident,
$test_fn:ident,
[$(($pattern_name:ident, $pattern_fn:expr)),* $(,)?] $(,)?
) => {
gen_sort_test_fns_with_default_patterns!(
${concat($base_name, _i32)},
|len, pattern_fn| $test_fn::<i32, S>(len, i32_from_i32, i32_from_i32_ref, pattern_fn),
&TEST_LENGTHS[..TEST_LENGTHS.len() - 2],
[$(($pattern_name, $pattern_fn),)*],
);
gen_sort_test_fns_with_default_patterns!(
${concat($base_name, _cell_i32)},
|len, pattern_fn| $test_fn::<Cell<i32>, S>(len, cell_i32_from_i32, i32_from_cell_i32, pattern_fn),
&TEST_LENGTHS[..TEST_LENGTHS.len() - 3],
[$(($pattern_name, $pattern_fn),)*],
);
gen_sort_test_fns_with_default_patterns!(
${concat($base_name, _string)},
|len, pattern_fn| $test_fn::<String, S>(len, string_from_i32, i32_from_string, pattern_fn),
&TEST_LENGTHS[..TEST_LENGTHS.len() - 3],
[$(($pattern_name, $pattern_fn),)*],
);
};
}
// --- TESTS ---
pub fn basic_impl<S: Sort>() {
check_is_sorted::<i32, S>(&mut []);
check_is_sorted::<(), S>(&mut []);
check_is_sorted::<(), S>(&mut [()]);
check_is_sorted::<(), S>(&mut [(), ()]);
check_is_sorted::<(), S>(&mut [(), (), ()]);
check_is_sorted::<i32, S>(&mut []);
check_is_sorted::<i32, S>(&mut [77]);
check_is_sorted::<i32, S>(&mut [2, 3]);
check_is_sorted::<i32, S>(&mut [2, 3, 6]);
check_is_sorted::<i32, S>(&mut [2, 3, 99, 6]);
check_is_sorted::<i32, S>(&mut [2, 7709, 400, 90932]);
check_is_sorted::<i32, S>(&mut [15, -1, 3, -1, -3, -1, 7]);
}
fn fixed_seed_impl<S: Sort>() {
let fixed_seed_a = patterns::get_or_init_rand_seed();
let fixed_seed_b = patterns::get_or_init_rand_seed();
assert_eq!(fixed_seed_a, fixed_seed_b);
}
fn fixed_seed_rand_vec_prefix_impl<S: Sort>() {
let vec_rand_len_5 = patterns::random(5);
let vec_rand_len_7 = patterns::random(7);
assert_eq!(vec_rand_len_5, vec_rand_len_7[..5]);
}
fn int_edge_impl<S: Sort>() {
// Ensure that the sort can handle integer edge cases.
check_is_sorted::<i32, S>(&mut [i32::MIN, i32::MAX]);
check_is_sorted::<i32, S>(&mut [i32::MAX, i32::MIN]);
check_is_sorted::<i32, S>(&mut [i32::MIN, 3]);
check_is_sorted::<i32, S>(&mut [i32::MIN, -3]);
check_is_sorted::<i32, S>(&mut [i32::MIN, -3, i32::MAX]);
check_is_sorted::<i32, S>(&mut [i32::MIN, -3, i32::MAX, i32::MIN, 5]);
check_is_sorted::<i32, S>(&mut [i32::MAX, 3, i32::MIN, 5, i32::MIN, -3, 60, 200, 50, 7, 10]);
check_is_sorted::<u64, S>(&mut [u64::MIN, u64::MAX]);
check_is_sorted::<u64, S>(&mut [u64::MAX, u64::MIN]);
check_is_sorted::<u64, S>(&mut [u64::MIN, 3]);
check_is_sorted::<u64, S>(&mut [u64::MIN, u64::MAX - 3]);
check_is_sorted::<u64, S>(&mut [u64::MIN, u64::MAX - 3, u64::MAX]);
check_is_sorted::<u64, S>(&mut [u64::MIN, u64::MAX - 3, u64::MAX, u64::MIN, 5]);
check_is_sorted::<u64, S>(&mut [
u64::MAX,
3,
u64::MIN,
5,
u64::MIN,
u64::MAX - 3,
60,
200,
50,
7,
10,
]);
let mut large = patterns::random(TEST_LENGTHS[TEST_LENGTHS.len() - 2]);
large.push(i32::MAX);
large.push(i32::MIN);
large.push(i32::MAX);
check_is_sorted::<i32, S>(&mut large);
}
fn sort_vs_sort_by_impl<S: Sort>() {
// Ensure that sort and sort_by produce the same result.
let mut input_normal = [800, 3, -801, 5, -801, -3, 60, 200, 50, 7, 10];
let expected = [-801, -801, -3, 3, 5, 7, 10, 50, 60, 200, 800];
let mut input_sort_by = input_normal.to_vec();
<S as Sort>::sort(&mut input_normal);
<S as Sort>::sort_by(&mut input_sort_by, |a, b| a.cmp(b));
assert_eq!(input_normal, expected);
assert_eq!(input_sort_by, expected);
}
gen_sort_test_fns_with_default_patterns!(
correct_i32,
|len, pattern_fn| test_is_sorted::<i32, S>(len, |val| val, pattern_fn),
TEST_LENGTHS,
[
(random_d4, |len| patterns::random_uniform(len, 0..4)),
(random_d8, |len| patterns::random_uniform(len, 0..8)),
(random_d311, |len| patterns::random_uniform(len, 0..311)),
(random_d1024, |len| patterns::random_uniform(len, 0..1024)),
(random_z1_03, |len| patterns::random_zipf(len, 1.03)),
(random_z2, |len| patterns::random_zipf(len, 2.0)),
(random_s50, |len| patterns::random_sorted(len, 50.0)),
(narrow, |len| patterns::random_uniform(
len,
0..=(((len as f64).log2().round()) as i32) * 100
)),
(all_equal, patterns::all_equal),
(saw_mixed_range, |len| patterns::saw_mixed_range(len, 20..50)),
(pipe_organ, patterns::pipe_organ),
]
);
gen_sort_test_fns_with_default_patterns!(
correct_u64,
|len, pattern_fn| test_is_sorted::<u64, S>(len, extend_i32_to_u64, pattern_fn),
TEST_LENGTHS,
[]
);
gen_sort_test_fns_with_default_patterns!(
correct_u128,
|len, pattern_fn| test_is_sorted::<u128, S>(len, extend_i32_to_u128, pattern_fn),
&TEST_LENGTHS[..TEST_LENGTHS.len() - 2],
[]
);
gen_sort_test_fns_with_default_patterns!(
correct_cell_i32,
|len, pattern_fn| test_is_sorted::<Cell<i32>, S>(len, Cell::new, pattern_fn),
&TEST_LENGTHS[..TEST_LENGTHS.len() - 2],
[]
);
gen_sort_test_fns_with_default_patterns!(
correct_string,
|len, pattern_fn| test_is_sorted::<String, S>(
len,
|val| format!("{:010}", shift_i32_to_u32(val)),
pattern_fn
),
&TEST_LENGTHS[..TEST_LENGTHS.len() - 2],
[]
);
gen_sort_test_fns_with_default_patterns!(
correct_f128,
|len, pattern_fn| test_is_sorted::<F128, S>(len, F128::new, pattern_fn),
&TEST_LENGTHS[..TEST_LENGTHS.len() - 2],
[]
);
gen_sort_test_fns_with_default_patterns!(
correct_1k,
|len, pattern_fn| test_is_sorted::<FFIOneKibiByte, S>(len, FFIOneKibiByte::new, pattern_fn),
&TEST_LENGTHS[..TEST_LENGTHS.len() - 2],
[]
);
// Dyn values are fat pointers, something the implementation might have overlooked.
gen_sort_test_fns_with_default_patterns!(
correct_dyn_val,
|len, pattern_fn| test_is_sorted::<Rc<dyn DynTrait>, S>(len, dyn_trait_from_i32, pattern_fn),
&TEST_LENGTHS[..TEST_LENGTHS.len() - 2],
[]
);
fn stability_legacy_impl<S: Sort>() {
// This non pattern variant has proven to catch some bugs the pattern version of this function
// doesn't catch, so it remains in conjunction with the other one.
if <S as Sort>::name().contains("unstable") {
// It would be great to mark the test as skipped, but that isn't possible as of now.
return;
}
let large_range = if cfg!(miri) { 100..110 } else { 3000..3010 };
let rounds = if cfg!(miri) { 1 } else { 10 };
let rand_vals = patterns::random_uniform(5_000, 0..=9);
let mut rand_idx = 0;
for len in (2..55).chain(large_range) {
for _ in 0..rounds {
let mut counts = [0; 10];
// create a vector like [(6, 1), (5, 1), (6, 2), ...],
// where the first item of each tuple is random, but
// the second item represents which occurrence of that
// number this element is, i.e., the second elements
// will occur in sorted order.
let orig: Vec<_> = (0..len)
.map(|_| {
let n = rand_vals[rand_idx];
rand_idx += 1;
if rand_idx >= rand_vals.len() {
rand_idx = 0;
}
counts[n as usize] += 1;
i32_tup_as_u64((n, counts[n as usize]))
})
.collect();
let mut v = orig.clone();
// Only sort on the first element, so an unstable sort
// may mix up the counts.
<S as Sort>::sort_by(&mut v, |a_packed, b_packed| {
let a = i32_tup_from_u64(*a_packed).0;
let b = i32_tup_from_u64(*b_packed).0;
a.cmp(&b)
});
// This comparison includes the count (the second item
// of the tuple), so elements with equal first items
// will need to be ordered with increasing
// counts... i.e., exactly asserting that this sort is
// stable.
assert!(v.windows(2).all(|w| i32_tup_from_u64(w[0]) <= i32_tup_from_u64(w[1])));
}
}
// For cpp_sorts that only support u64 we can pack the two i32 inside a u64.
fn i32_tup_as_u64(val: (i32, i32)) -> u64 {
let a_bytes = val.0.to_le_bytes();
let b_bytes = val.1.to_le_bytes();
u64::from_le_bytes([a_bytes, b_bytes].concat().try_into().unwrap())
}
fn i32_tup_from_u64(val: u64) -> (i32, i32) {
let bytes = val.to_le_bytes();
let a = i32::from_le_bytes(bytes[0..4].try_into().unwrap());
let b = i32::from_le_bytes(bytes[4..8].try_into().unwrap());
(a, b)
}
}
fn stability_with_patterns<T: Ord + Clone, S: Sort>(
len: usize,
type_into_fn: impl Fn(i32) -> T,
_type_from_fn: impl Fn(&T) -> i32,
pattern_fn: fn(usize) -> Vec<i32>,
) {
if <S as Sort>::name().contains("unstable") {
// It would be great to mark the test as skipped, but that isn't possible as of now.
return;
}
let pattern = pattern_fn(len);
let mut counts = [0i32; 128];
// create a vector like [(6, 1), (5, 1), (6, 2), ...],
// where the first item of each tuple is random, but
// the second item represents which occurrence of that
// number this element is, i.e., the second elements
// will occur in sorted order.
let orig: Vec<_> = pattern
.iter()
.map(|val| {
let n = val.saturating_abs() % counts.len() as i32;
counts[n as usize] += 1;
(type_into_fn(n), counts[n as usize])
})
.collect();
let mut v = orig.clone();
// Only sort on the first element, so an unstable sort
// may mix up the counts.
<S as Sort>::sort(&mut v);
// This comparison includes the count (the second item
// of the tuple), so elements with equal first items
// will need to be ordered with increasing
// counts... i.e., exactly asserting that this sort is
// stable.
assert!(v.windows(2).all(|w| w[0] <= w[1]));
}
gen_sort_test_fns_with_default_patterns_3_ty!(stability, stability_with_patterns, []);
fn observable_is_less<S: Sort>(len: usize, pattern_fn: fn(usize) -> Vec<i32>) {
// This test, tests that every is_less is actually observable. Ie. this can go wrong if a hole
// is created using temporary memory and, the whole is used as comparison but not copied back.
//
// If this is not upheld a custom type + comparison function could yield UB in otherwise safe
// code. Eg T == Mutex<Option<Box<str>>> which replaces the pointer with none in the comparison
// function, which would not be observed in the original slice and would lead to a double free.
let pattern = pattern_fn(len);
let mut test_input = pattern.into_iter().map(|val| CompCount::new(val)).collect::<Vec<_>>();
let mut comp_count_global = 0;
<S as Sort>::sort_by(&mut test_input, |a, b| {
a.comp_count.replace(a.comp_count.get() + 1);
b.comp_count.replace(b.comp_count.get() + 1);
comp_count_global += 1;
a.val.cmp(&b.val)
});
let total_inner: u64 = test_input.iter().map(|c| c.comp_count.get() as u64).sum();
assert_eq!(total_inner, comp_count_global * 2);
}
gen_sort_test_fns_with_default_patterns!(
observable_is_less,
observable_is_less::<S>,
&TEST_LENGTHS[..TEST_LENGTHS.len() - 2],
[]
);
fn panic_retain_orig_set<T: Ord + Clone, S: Sort>(
len: usize,
type_into_fn: impl Fn(i32) -> T + Copy,
type_from_fn: impl Fn(&T) -> i32,
pattern_fn: fn(usize) -> Vec<i32>,
) {
let mut test_data: Vec<T> = pattern_fn(len).into_iter().map(type_into_fn).collect();
let sum_before: i64 = test_data.iter().map(|x| type_from_fn(x) as i64).sum();
// Calculate a specific comparison that should panic.
// Ensure that it can be any of the possible comparisons and that it always panics.
let required_comps = calc_comps_required::<T, S>(&mut test_data.clone(), |a, b| a.cmp(b));
let panic_threshold = patterns::random_uniform(1, 1..=required_comps as i32)[0] as usize - 1;
let mut comp_counter = 0;
let res = panic::catch_unwind(AssertUnwindSafe(|| {
<S as Sort>::sort_by(&mut test_data, |a, b| {
if comp_counter == panic_threshold {
// Make the panic dependent on the test len and some random factor. We want to
// make sure that panicking may also happen when comparing elements a second
// time.
panic!();
}
comp_counter += 1;
a.cmp(b)
});
}));
assert!(res.is_err());
// If the sum before and after don't match, it means the set of elements hasn't remained the
// same.
let sum_after: i64 = test_data.iter().map(|x| type_from_fn(x) as i64).sum();
assert_eq!(sum_before, sum_after);
}
gen_sort_test_fns_with_default_patterns_3_ty!(panic_retain_orig_set, panic_retain_orig_set, []);
fn panic_observable_is_less<S: Sort>(len: usize, pattern_fn: fn(usize) -> Vec<i32>) {
// This test, tests that every is_less is actually observable. Ie. this can go wrong if a hole
// is created using temporary memory and, the whole is used as comparison but not copied back.
// This property must also hold if the user provided comparison panics.
//
// If this is not upheld a custom type + comparison function could yield UB in otherwise safe
// code. Eg T == Mutex<Option<Box<str>>> which replaces the pointer with none in the comparison
// function, which would not be observed in the original slice and would lead to a double free.
let mut test_input =
pattern_fn(len).into_iter().map(|val| CompCount::new(val)).collect::<Vec<_>>();
let sum_before: i64 = test_input.iter().map(|x| x.val as i64).sum();
// Calculate a specific comparison that should panic.
// Ensure that it can be any of the possible comparisons and that it always panics.
let required_comps =
calc_comps_required::<CompCount, S>(&mut test_input.clone(), |a, b| a.val.cmp(&b.val));
let panic_threshold = patterns::random_uniform(1, 1..=required_comps as i32)[0] as u64 - 1;
let mut comp_count_global = 0;
let res = panic::catch_unwind(AssertUnwindSafe(|| {
<S as Sort>::sort_by(&mut test_input, |a, b| {
if comp_count_global == panic_threshold {
// Make the panic dependent on the test len and some random factor. We want to
// make sure that panicking may also happen when comparing elements a second
// time.
panic!();
}
a.comp_count.replace(a.comp_count.get() + 1);
b.comp_count.replace(b.comp_count.get() + 1);
comp_count_global += 1;
a.val.cmp(&b.val)
});
}));
assert!(res.is_err());
let total_inner: u64 = test_input.iter().map(|c| c.comp_count.get() as u64).sum();
assert_eq!(total_inner, comp_count_global * 2);
// If the sum before and after don't match, it means the set of elements hasn't remained the
// same.
let sum_after: i64 = test_input.iter().map(|x| x.val as i64).sum();
assert_eq!(sum_before, sum_after);
}
gen_sort_test_fns_with_default_patterns!(
panic_observable_is_less,
panic_observable_is_less::<S>,
&TEST_LENGTHS[..TEST_LENGTHS.len() - 2],
[]
);
fn deterministic<T: Ord + Clone + Debug, S: Sort>(
len: usize,
type_into_fn: impl Fn(i32) -> T + Copy,
type_from_fn: impl Fn(&T) -> i32,
pattern_fn: fn(usize) -> Vec<i32>,
) {
// A property similar to stability is deterministic output order. If the entire value is used as
// the comparison key a lack of determinism has no effect. But if only a part of the value is
// used as comparison key, a lack of determinism can manifest itself in the order of values
// considered equal by the comparison predicate.
//
// This test only tests that results are deterministic across runs, it does not test determinism
// on different platforms and with different toolchains.
let mut test_input =
pattern_fn(len).into_iter().map(|val| type_into_fn(val)).collect::<Vec<_>>();
let mut test_input_clone = test_input.clone();
let comparison_fn = |a: &T, b: &T| {
let a_i32 = type_from_fn(a);
let b_i32 = type_from_fn(b);
let a_i32_key_space_reduced = a_i32 % 10_000;
let b_i32_key_space_reduced = b_i32 % 10_000;
a_i32_key_space_reduced.cmp(&b_i32_key_space_reduced)
};
<S as Sort>::sort_by(&mut test_input, comparison_fn);
<S as Sort>::sort_by(&mut test_input_clone, comparison_fn);
assert_eq!(test_input, test_input_clone);
}
gen_sort_test_fns_with_default_patterns_3_ty!(deterministic, deterministic, []);
fn self_cmp<T: Ord + Clone + Debug, S: Sort>(
len: usize,
type_into_fn: impl Fn(i32) -> T + Copy,
_type_from_fn: impl Fn(&T) -> i32,
pattern_fn: fn(usize) -> Vec<i32>,
) {
// It's possible for comparisons to run into problems if the values of `a` and `b` passed into
// the comparison function are the same reference. So this tests that they never are.
let mut test_input =
pattern_fn(len).into_iter().map(|val| type_into_fn(val)).collect::<Vec<_>>();
let comparison_fn = |a: &T, b: &T| {
assert_ne!(a as *const T as usize, b as *const T as usize);
a.cmp(b)
};
<S as Sort>::sort_by(&mut test_input, comparison_fn);
// Check that the output is actually sorted and wasn't stopped by the assert.
for window in test_input.windows(2) {
assert!(window[0] <= window[1]);
}
}
gen_sort_test_fns_with_default_patterns_3_ty!(self_cmp, self_cmp, []);
fn violate_ord_retain_orig_set<T: Ord, S: Sort>(
len: usize,
type_into_fn: impl Fn(i32) -> T + Copy,
type_from_fn: impl Fn(&T) -> i32,
pattern_fn: fn(usize) -> Vec<i32>,
) {
// A user may implement Ord incorrectly for a type or violate it by calling sort_by with a
// comparison function that violates Ord with the orderings it returns. Even under such
// circumstances the input must retain its original set of elements.
// Ord implies a strict total order see https://en.wikipedia.org/wiki/Total_order.
// Generating random numbers with miri is quite expensive.
let random_orderings_len = if cfg!(miri) { 200 } else { 10_000 };
// Make sure we get a good distribution of random orderings, that are repeatable with the seed.
// Just using random_uniform with the same len and range will always yield the same value.
let random_orderings = patterns::random_uniform(random_orderings_len, 0..2);
let get_random_0_1_or_2 = |random_idx: &mut usize| {
let ridx = *random_idx;
*random_idx += 1;
if ridx + 1 == random_orderings.len() {
*random_idx = 0;
}
random_orderings[ridx] as usize
};
let mut random_idx_a = 0;
let mut random_idx_b = 0;
let mut random_idx_c = 0;
let mut last_element_a = -1;
let mut last_element_b = -1;
let mut rand_counter_b = 0;
let mut rand_counter_c = 0;
let mut streak_counter_a = 0;
let mut streak_counter_b = 0;
// Examples, a = 3, b = 5, c = 9.
// Correct Ord -> 10010 | is_less(a, b) is_less(a, a) is_less(b, a) is_less(a, c) is_less(c, a)
let mut invalid_ord_comp_functions: Vec<Box<dyn FnMut(&T, &T) -> Ordering>> = vec![
Box::new(|_a, _b| -> Ordering {
// random
// Eg. is_less(3, 5) == true, is_less(3, 5) == false
let idx = get_random_0_1_or_2(&mut random_idx_a);
[Ordering::Less, Ordering::Equal, Ordering::Greater][idx]
}),
Box::new(|_a, _b| -> Ordering {
// everything is less -> 11111
Ordering::Less
}),
Box::new(|_a, _b| -> Ordering {
// everything is equal -> 00000
Ordering::Equal
}),
Box::new(|_a, _b| -> Ordering {
// everything is greater -> 00000
// Eg. is_less(3, 5) == false, is_less(5, 3) == false, is_less(3, 3) == false
Ordering::Greater
}),
Box::new(|a, b| -> Ordering {
// equal means less else greater -> 01000
if a == b { Ordering::Less } else { Ordering::Greater }
}),
Box::new(|a, b| -> Ordering {
// Transitive breaker. remember last element -> 10001
let lea = last_element_a;
let leb = last_element_b;
let a_as_i32 = type_from_fn(a);
let b_as_i32 = type_from_fn(b);
last_element_a = a_as_i32;
last_element_b = b_as_i32;
if a_as_i32 == lea && b_as_i32 != leb { b.cmp(a) } else { a.cmp(b) }
}),
Box::new(|a, b| -> Ordering {
// Sampled random 1% of comparisons are reversed.
rand_counter_b += get_random_0_1_or_2(&mut random_idx_b);
if rand_counter_b >= 100 {
rand_counter_b = 0;
b.cmp(a)
} else {
a.cmp(b)
}
}),
Box::new(|a, b| -> Ordering {
// Sampled random 33% of comparisons are reversed.
rand_counter_c += get_random_0_1_or_2(&mut random_idx_c);
if rand_counter_c >= 3 {
rand_counter_c = 0;
b.cmp(a)
} else {
a.cmp(b)
}
}),
Box::new(|a, b| -> Ordering {
// STREAK_LEN comparisons yield a.cmp(b) then STREAK_LEN comparisons less. This can
// discover bugs that neither, random Ord, or just Less or Greater can find. Because it
// can push a pointer further than expected. Random Ord will average out how far a
// comparison based pointer travels. Just Less or Greater will be caught by pattern
// analysis and never enter interesting code.
const STREAK_LEN: usize = 50;
streak_counter_a += 1;
if streak_counter_a <= STREAK_LEN {
a.cmp(b)
} else {
if streak_counter_a == STREAK_LEN * 2 {
streak_counter_a = 0;
}
Ordering::Less
}
}),
Box::new(|a, b| -> Ordering {
// See above.
const STREAK_LEN: usize = 50;
streak_counter_b += 1;
if streak_counter_b <= STREAK_LEN {
a.cmp(b)
} else {
if streak_counter_b == STREAK_LEN * 2 {
streak_counter_b = 0;
}
Ordering::Greater
}
}),
];
for comp_func in &mut invalid_ord_comp_functions {
let mut test_data: Vec<T> = pattern_fn(len).into_iter().map(type_into_fn).collect();
let sum_before: i64 = test_data.iter().map(|x| type_from_fn(x) as i64).sum();
// It's ok to panic on Ord violation or to complete.
// In both cases the original elements must still be present.
let _ = panic::catch_unwind(AssertUnwindSafe(|| {
<S as Sort>::sort_by(&mut test_data, &mut *comp_func);
}));
// If the sum before and after don't match, it means the set of elements hasn't remained the
// same.
let sum_after: i64 = test_data.iter().map(|x| type_from_fn(x) as i64).sum();
assert_eq!(sum_before, sum_after);
if cfg!(miri) {
// This test is prohibitively expensive in miri, so only run one of the comparison
// functions. This test is not expected to yield direct UB, but rather surface potential
// UB by showing that the sum is different now.
break;
}
}
}
gen_sort_test_fns_with_default_patterns_3_ty!(
violate_ord_retain_orig_set,
violate_ord_retain_orig_set,
[]
);
macro_rules! instantiate_sort_test_inner {
($sort_impl:ty, miri_yes, $test_fn_name:ident) => {
#[test]
fn $test_fn_name() {
$crate::sort::tests::$test_fn_name::<$sort_impl>();
}
};
($sort_impl:ty, miri_no, $test_fn_name:ident) => {
#[test]
#[cfg_attr(miri, ignore)]
fn $test_fn_name() {
$crate::sort::tests::$test_fn_name::<$sort_impl>();
}
};
}
// Using this construct allows us to get warnings for unused test functions.
macro_rules! define_instantiate_sort_tests {
($([$miri_use:ident, $test_fn_name:ident]),*,) => {
$(pub fn $test_fn_name<S: Sort>() {
${concat($test_fn_name, _impl)}::<S>();
})*
macro_rules! instantiate_sort_tests_gen {
($sort_impl:ty) => {
$(
instantiate_sort_test_inner!(
$sort_impl,
$miri_use,
$test_fn_name
);
)*
}
}
};
}
// Some tests are not tested with miri to avoid prohibitively long test times. This leaves coverage
// holes, but the way they are selected should make for relatively small holes. Many properties that
// can lead to UB are tested directly, for example that the original set of elements is retained
// even when a panic occurs or Ord is implemented incorrectly.
define_instantiate_sort_tests!(
[miri_yes, basic],
[miri_yes, fixed_seed],
[miri_yes, fixed_seed_rand_vec_prefix],
[miri_yes, int_edge],
[miri_yes, sort_vs_sort_by],
[miri_yes, correct_i32_random],
[miri_yes, correct_i32_random_z1],
[miri_yes, correct_i32_random_d2],
[miri_yes, correct_i32_random_d20],
[miri_yes, correct_i32_random_s95],
[miri_yes, correct_i32_ascending],
[miri_yes, correct_i32_descending],
[miri_yes, correct_i32_saw_mixed],
[miri_no, correct_i32_random_d4],
[miri_no, correct_i32_random_d8],
[miri_no, correct_i32_random_d311],
[miri_no, correct_i32_random_d1024],
[miri_no, correct_i32_random_z1_03],
[miri_no, correct_i32_random_z2],
[miri_no, correct_i32_random_s50],
[miri_no, correct_i32_narrow],
[miri_no, correct_i32_all_equal],
[miri_no, correct_i32_saw_mixed_range],
[miri_yes, correct_i32_pipe_organ],
[miri_no, correct_u64_random],
[miri_yes, correct_u64_random_z1],
[miri_no, correct_u64_random_d2],
[miri_no, correct_u64_random_d20],
[miri_no, correct_u64_random_s95],
[miri_no, correct_u64_ascending],
[miri_no, correct_u64_descending],
[miri_no, correct_u64_saw_mixed],
[miri_no, correct_u128_random],
[miri_yes, correct_u128_random_z1],
[miri_no, correct_u128_random_d2],
[miri_no, correct_u128_random_d20],