mirror of
https://github.com/vlang/v.git
synced 2025-09-15 07:22:27 +03:00
rand.util: add sample_r and sample_nr (#8539)
This commit is contained in:
parent
a976876211
commit
c6552d7780
2 changed files with 61 additions and 0 deletions
31
vlib/rand/util/util_test.v
Normal file
31
vlib/rand/util/util_test.v
Normal file
|
@ -0,0 +1,31 @@
|
|||
import rand.util
|
||||
|
||||
fn test_sample_nr() {
|
||||
lengths := [1, 3, 4, 5, 6, 7]
|
||||
a := ['one', 'two', 'three', 'four', 'five', 'six', 'seven']
|
||||
for length in lengths {
|
||||
b := util.sample_nr(a, length)
|
||||
assert b.len == length
|
||||
for element in b {
|
||||
assert element in a
|
||||
// make sure every element occurs once
|
||||
mut count := 0
|
||||
for e in b {
|
||||
if e == element {
|
||||
count++
|
||||
}
|
||||
}
|
||||
assert count == 1
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn test_sample_r() {
|
||||
k := 20
|
||||
a := ['heads', 'tails']
|
||||
b := util.sample_r(a, k)
|
||||
assert b.len == k
|
||||
for element in b {
|
||||
assert element in a
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue