|
| 1 | +module main |
| 2 | + |
| 3 | +const empty = []int{cap: 0} |
| 4 | + |
| 5 | +fn test_keep_on_empty_list_returns_empty_list() { |
| 6 | + predicate := fn (x int) bool { |
| 7 | + return true |
| 8 | + } |
| 9 | + assert keep(empty, predicate) == empty |
| 10 | +} |
| 11 | + |
| 12 | +fn test_keeps_everything() { |
| 13 | + array := [1, 3, 5] |
| 14 | + predicate := fn (x int) bool { |
| 15 | + return true |
| 16 | + } |
| 17 | + expect := [1, 3, 5] |
| 18 | + assert keep(array, predicate) == expect |
| 19 | +} |
| 20 | + |
| 21 | +fn test_keeps_nothing() { |
| 22 | + array := [1, 3, 5] |
| 23 | + predicate := fn (x int) bool { |
| 24 | + return false |
| 25 | + } |
| 26 | + assert keep(array, predicate) == empty |
| 27 | +} |
| 28 | + |
| 29 | +fn test_keeps_first_and_last() { |
| 30 | + array := [1, 2, 3] |
| 31 | + predicate := fn (x int) bool { |
| 32 | + return x % 2 == 1 |
| 33 | + } |
| 34 | + expect := [1, 3] |
| 35 | + assert keep(array, predicate) == expect |
| 36 | +} |
| 37 | + |
| 38 | +fn test_keeps_neither_first_nor_last() { |
| 39 | + array := [1, 2, 3] |
| 40 | + predicate := fn (x int) bool { |
| 41 | + return x % 2 == 0 |
| 42 | + } |
| 43 | + expect := [2] |
| 44 | + assert keep(array, predicate) == expect |
| 45 | +} |
| 46 | + |
| 47 | +fn test_keeps_strings() { |
| 48 | + array := ['apple', 'zebra', 'banana', 'zombies', 'cherimoya', 'zealot'] |
| 49 | + predicate := fn (x string) bool { |
| 50 | + return x.starts_with('z') |
| 51 | + } |
| 52 | + expect := ['zebra', 'zombies', 'zealot'] |
| 53 | + assert keep(array, predicate) == expect |
| 54 | +} |
| 55 | + |
| 56 | +fn test_keeps_lists() { |
| 57 | + array := [ |
| 58 | + [1, 2, 3], |
| 59 | + [5, 5, 5], |
| 60 | + [5, 1, 2], |
| 61 | + [2, 1, 2], |
| 62 | + [1, 5, 2], |
| 63 | + [2, 2, 1], |
| 64 | + [1, 2, 5], |
| 65 | + ] |
| 66 | + predicate := fn (x []int) bool { |
| 67 | + return x.contains(5) |
| 68 | + } |
| 69 | + expect := [ |
| 70 | + [5, 5, 5], |
| 71 | + [5, 1, 2], |
| 72 | + [1, 5, 2], |
| 73 | + [1, 2, 5], |
| 74 | + ] |
| 75 | + assert keep(array, predicate) == expect |
| 76 | +} |
| 77 | + |
| 78 | +fn test_discard_on_empty_list_returns_empty_list() { |
| 79 | + predicate := fn (x int) bool { |
| 80 | + return true |
| 81 | + } |
| 82 | + assert discard(empty, predicate) == empty |
| 83 | +} |
| 84 | + |
| 85 | +fn test_discards_everything() { |
| 86 | + array := [1, 3, 5] |
| 87 | + predicate := fn (x int) bool { |
| 88 | + return true |
| 89 | + } |
| 90 | + assert discard(array, predicate) == empty |
| 91 | +} |
| 92 | + |
| 93 | +fn test_discards_nothing() { |
| 94 | + array := [1, 3, 5] |
| 95 | + predicate := fn (x int) bool { |
| 96 | + return false |
| 97 | + } |
| 98 | + expect := [1, 3, 5] |
| 99 | + assert discard(array, predicate) == expect |
| 100 | +} |
| 101 | + |
| 102 | +fn test_discards_first_and_last() { |
| 103 | + array := [1, 2, 3] |
| 104 | + predicate := fn (x int) bool { |
| 105 | + return x % 2 == 1 |
| 106 | + } |
| 107 | + expect := [2] |
| 108 | + assert discard(array, predicate) == expect |
| 109 | +} |
| 110 | + |
| 111 | +fn test_discards_neither_first_nor_last() { |
| 112 | + array := [1, 2, 3] |
| 113 | + predicate := fn (x int) bool { |
| 114 | + return x % 2 == 0 |
| 115 | + } |
| 116 | + expect := [1, 3] |
| 117 | + assert discard(array, predicate) == expect |
| 118 | +} |
| 119 | + |
| 120 | +fn test_discards_strings() { |
| 121 | + array := ['apple', 'zebra', 'banana', 'zombies', 'cherimoya', 'zealot'] |
| 122 | + predicate := fn (x string) bool { |
| 123 | + return x.starts_with('z') |
| 124 | + } |
| 125 | + expect := ['apple', 'banana', 'cherimoya'] |
| 126 | + assert discard(array, predicate) == expect |
| 127 | +} |
| 128 | + |
| 129 | +fn test_discards_lists() { |
| 130 | + array := [ |
| 131 | + [1, 2, 3], |
| 132 | + [5, 5, 5], |
| 133 | + [5, 1, 2], |
| 134 | + [2, 1, 2], |
| 135 | + [1, 5, 2], |
| 136 | + [2, 2, 1], |
| 137 | + [1, 2, 5], |
| 138 | + ] |
| 139 | + predicate := fn (x []int) bool { |
| 140 | + return x.contains(5) |
| 141 | + } |
| 142 | + expect := [ |
| 143 | + [1, 2, 3], |
| 144 | + [2, 1, 2], |
| 145 | + [2, 2, 1], |
| 146 | + ] |
| 147 | + assert discard(array, predicate) == expect |
| 148 | +} |
0 commit comments