Skip to content

Commit 79af20f

Browse files
giacomocavalierilpil
authored andcommitted
Improve sample tests
To make sure that the sample function doesn't take duplicate items from the original list
1 parent a935a2c commit 79af20f

File tree

1 file changed

+17
-19
lines changed

1 file changed

+17
-19
lines changed

test/gleam/list_test.gleam

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -970,36 +970,34 @@ pub fn max_test() {
970970

971971
pub fn sample_test() {
972972
assert list.sample([], 3) == []
973-
974973
assert list.sample([1, 2, 3], 0) == []
975-
976974
assert list.sample([1, 2, 3], -1) == []
977-
978-
assert [1, 2]
979-
|> list.sample(5)
980-
|> list.sort(int.compare)
981-
== [1, 2]
982-
975+
assert list.sort(list.sample([1, 2], 5), int.compare) == [1, 2]
983976
assert list.sample([1], 1) == [1]
984977

985-
let input = list.range(1, 100)
986-
let sample = list.sample(input, 10)
987-
assert list.length(sample) == 10
978+
assert 10
979+
== list.range(1, 100)
980+
|> list.sample(10)
981+
|> list.unique
982+
|> list.length
983+
984+
assert [1, 1, 1, 1, 1]
985+
|> list.sample(3)
986+
|> list.all(fn(x) { x == 1 })
988987

989-
let repeated = [1, 1, 1, 1, 1]
990-
let sample = list.sample(repeated, 3)
991-
assert list.all(sample, fn(x) { x == 1 })
988+
// Some tests on a bigger sample.
989+
let sample =
990+
list.range(1, 1000)
991+
|> list.sample(100)
992992

993-
let input = list.range(1, 1000)
994-
let sample = list.sample(input, 100)
995993
assert sample
996994
|> list.sort(int.compare)
997995
|> list.all(fn(x) { x >= 1 && x <= 1000 })
998996

999-
assert list.length(sample) == 100
997+
assert 100 == list.length(list.unique(sample))
1000998

1001-
let min = list.fold(sample, 1000, int.min)
1002-
let max = list.fold(sample, 1, int.max)
999+
let assert Ok(min) = list.reduce(sample, int.min)
1000+
let assert Ok(max) = list.reduce(sample, int.max)
10031001
assert min >= 1
10041002
assert max <= 1000
10051003
}

0 commit comments

Comments
 (0)