Skip to content

Commit b14396f

Browse files
committed
New utils CLAMP function
Also includes associted unittest.
1 parent 08d01f3 commit b14396f

File tree

2 files changed

+12
-0
lines changed

2 files changed

+12
-0
lines changed

include/utils.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,3 +97,6 @@
9797
#define ARCH_ENABLE_INTERRUPTS() (void)0
9898
#define ARCH_DISABLE_INTERRUPTS() (void)0
9999
#endif
100+
101+
// Clamp value to lower & upper bounds
102+
#define CLAMP(v, l, u) (v < l) ? l : (v >= u) ? u : v

src/unittests/kernel/utils_test.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,14 @@
44
#include <unittest/unittest.h>
55
#include <types.h>
66

7+
TEST (utils, clamp)
8+
{
9+
EQ_SCALAR (CLAMP (0, 0, 0), 0);
10+
EQ_SCALAR (CLAMP (-10, 1, 2), 1);
11+
EQ_SCALAR (CLAMP (10, 1, 2), 2);
12+
END();
13+
}
14+
715
TEST (utils, bit_set)
816
{
917
EQ_SCALAR (BIT_SET (0x10, 0), 0b00010001);
@@ -196,4 +204,5 @@ int main() {
196204
bytes_to_pageframes_floor();
197205
bit_set();
198206
bit_clear();
207+
clamp();
199208
}

0 commit comments

Comments
 (0)