| 1 | //! The top sorting algorithm; that is, the modified merge sort we keep |
| 2 | //! talking about. |
| 3 | |
| 4 | use crate::{never, ord_t_comparator}; |
| 5 | |
| 6 | /// Test the sort implementation with an empty list |
| 7 | #[test] |
| 8 | fn empty() { |
| 9 | let mut list: Vec<u32> = vec![]; |
| 10 | sort(&mut list); |
| 11 | assert!(list.is_empty()); |
| 12 | } |
| 13 | |
| 14 | /// Test the sort implementation with a single-element list |
| 15 | #[test] |
| 16 | fn single() { |
| 17 | let mut list = vec![42]; |
| 18 | sort(&mut list); |
| 19 | assert!(list[0] == 42); |
| 20 | } |
| 21 | |
| 22 | /// Test the sort implementation with a short unsorted list |
| 23 | #[test] |
| 24 | fn unsorted() { |
| 25 | let mut list = vec![3, 1, 0, 4]; |
| 26 | sort(&mut list); |
| 27 | assert!(list[0] == 0); |
| 28 | assert!(list[1] == 1); |
| 29 | assert!(list[2] == 3); |
| 30 | assert!(list[3] == 4); |
| 31 | } |
| 32 | |
| 33 | /// Test the sort implementation with a short backward list |
| 34 | #[test] |
| 35 | fn reverse() { |
| 36 | let mut list = vec![21, 18, 7, 1]; |
| 37 | sort(&mut list); |
| 38 | assert!(list[0] == 1); |
| 39 | assert!(list[1] == 7); |
| 40 | assert!(list[2] == 18); |
| 41 | assert!(list[3] == 21); |
| 42 | } |
| 43 | |
| 44 | /// Test the sort implementation with a short unsorted list |
| 45 | #[test] |
| 46 | fn sorted() { |
| 47 | let mut list = vec![0, 1, 2, 3]; |
| 48 | sort(&mut list); |
| 49 | assert!(list[0] == 0); |
| 50 | assert!(list[1] == 1); |
| 51 | assert!(list[2] == 2); |
| 52 | assert!(list[3] == 3); |
| 53 | } |
| 54 | |
| 55 | /// Make sure the sort is stable. |
| 56 | #[test] |
| 57 | fn stable_npow2() { |
| 58 | let len = 259; |
| 59 | let mut key1: usize = 0; |
| 60 | let mut key2: usize = 0; |
| 61 | #[derive(Debug)] |
| 62 | struct Item { |
| 63 | key1: usize, |
| 64 | key2: usize, |
| 65 | } |
| 66 | let mut list: Vec<Item> = (0..len) |
| 67 | .map(|_| { |
| 68 | key1 += 1; |
| 69 | key1 %= 5; |
| 70 | key2 += 1; |
| 71 | Item { key1, key2 } |
| 72 | }) |
| 73 | .collect(); |
| 74 | crate::sort_by_gt(&mut list, |a, b| a.key1 > b.key1); |
| 75 | for i in 0..(len - 1) { |
| 76 | assert!(list[i].key1 <= list[i + 1].key1); |
| 77 | if list[i].key1 == list[i + 1].key1 { |
| 78 | assert!(list[i].key2 <= list[i + 1].key2); |
| 79 | } |
| 80 | } |
| 81 | } |
| 82 | /// Make sure the sort is stable. |
| 83 | #[test] |
| 84 | fn stable() { |
| 85 | let len = 256; |
| 86 | let mut key1: usize = 0; |
| 87 | let mut key2: usize = 0; |
| 88 | #[derive(Debug)] |
| 89 | struct Item { |
| 90 | key1: usize, |
| 91 | key2: usize, |
| 92 | } |
| 93 | let mut list: Vec<Item> = (0..len) |
| 94 | .map(|_| { |
| 95 | key1 += 1; |
| 96 | key1 %= 5; |
| 97 | key2 += 1; |
| 98 | Item { key1, key2 } |
| 99 | }) |
| 100 | .collect(); |
| 101 | crate::sort_by_gt(&mut list, |a, b| a.key1 > b.key1); |
| 102 | for i in 0..(len - 1) { |
| 103 | assert!(list[i].key1 <= list[i + 1].key1); |
| 104 | if list[i].key1 == list[i + 1].key1 { |
| 105 | assert!(list[i].key2 <= list[i + 1].key2); |
| 106 | } |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | /// Sort implementation convenience used for tests. |
| 111 | fn sort<T: Ord>(list: &mut [T]) { |
| 112 | super::SortState::new(list, &ord_t_comparator()) |
| 113 | .sort() |
| 114 | .unwrap_or_else(never) |
| 115 | } |