Skip to content

Commit

Permalink
builtin: add int_min/2 and int_max/2 tests
Browse files Browse the repository at this point in the history
  • Loading branch information
spytheman committed Nov 18, 2024
1 parent 7a4b3ab commit e72c9d0
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 0 deletions.
14 changes: 14 additions & 0 deletions vlib/builtin/int.v
Original file line number Diff line number Diff line change
Expand Up @@ -616,3 616,17 @@ pub fn (b u8) repeat(count int) string {
fn _Atomic__int_str(x int) string {
return x.str()
}

// int_min returns the smallest `int` of input `a` and `b`.
// Example: assert int_min(2,3) == 2
@[inline]
fn int_min(a int, b int) int {
return if a < b { a } else { b }
}

// int_max returns the largest `int` of input `a` and `b`.
// Example: assert int_max(2,3) == 3
@[inline]
fn int_max(a int, b int) int {
return if a > b { a } else { b }
}
22 changes: 22 additions & 0 deletions vlib/builtin/int_test.v
Original file line number Diff line number Diff line change
Expand Up @@ -246,3 246,25 @@ fn test_repeat() {
assert b.repeat(1) == b.ascii_str()
assert b.repeat(0) == ''
}

fn test_int_min() {
assert int_min(1, 2) == 1
assert int_min(2, 1) == 1
assert int_min(-2, -1) == -2
assert int_min(-1, -2) == -2
assert int_min(0, 5) == 0
assert int_min(5, 0) == 0
assert int_min(-5, 5) == -5
assert int_min(5, -5) == -5
}

fn test_int_max() {
assert int_max(1, 2) == 2
assert int_max(2, 1) == 2
assert int_max(-2, -1) == -1
assert int_max(-1, -2) == -1
assert int_max(0, 5) == 5
assert int_max(5, 0) == 5
assert int_max(-5, 5) == 5
assert int_max(5, -5) == 5
}

0 comments on commit e72c9d0

Please sign in to comment.