1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

arrays: add binary_search, lower_bound, and upper_bound array operations (#12045)

This commit is contained in:
05st
2021-10-03 00:14:39 -05:00
committed by GitHub
parent 117091452b
commit f282e64fe3
2 changed files with 87 additions and 0 deletions

View File

@@ -194,3 +194,27 @@ fn test_concat_string() {
assert concat(a, ...b) == ['1', '2', '3', '3', '2', '1']
}
fn test_binary_search() ? {
a := [1, 3, 3, 4, 5, 6, 7, 8, 10]
assert binary_search(a, 3) ? == 1
assert (binary_search(a, 0) or { -1 }) == -1
}
fn test_lower_bound() ? {
a := [1, 3, 3, 4, 5, 6, 7, 8, 10]
b := []int{}
c := [1, 2, 3]
assert lower_bound(a, 2) ? == 3
assert (lower_bound(b, 4) or { -1 }) == -1
assert lower_bound(c, 3) ? == 3
}
fn test_upper_bound() ? {
a := [1, 3, 3, 4, 5, 6, 7, 8, 10]
b := []int{}
c := [1, 2, 3]
assert upper_bound(a, 9) ? == 8
assert (upper_bound(b, 4) or { -1 }) == -1
assert upper_bound(c, 2) ? == 2
}