diff options
-rw-r--r-- | doc/cap.3 | 60 | ||||
-rw-r--r-- | doc/max.3 | 60 | ||||
-rw-r--r-- | doc/min.3 | 60 | ||||
-rw-r--r-- | src/extlib.c | 18 | ||||
-rw-r--r-- | src/extlib.h | 9 | ||||
-rw-r--r-- | tests/test.c | 39 |
6 files changed, 246 insertions, 0 deletions
diff --git a/doc/cap.3 b/doc/cap.3 new file mode 100644 index 0000000..f5b4efb --- /dev/null +++ b/doc/cap.3 @@ -0,0 +1,60 @@ +'\" t +.\" Copyright 2024 axtlos (axtlos@disroot.org) +.\" +.\" SPDX-License-Identifier: BSD-3-Clause + +.TH cap 3 2024-09-23 "extlib" +.SH NAME +cap, min, max \- limit a float between a min/max value +.SH LIBRARY +extlib extended standard library +.RI ( libextlib ", " \-lextlib ) +.SH SYNOPSIS +.nf +.B #include <extlib.h> +.P +.BI "float min (float v, float min_v);" +.P +.BI "float max (float v, float max_v);" +.P +.BI "float cap (float v, float min_v, float max_v);" +.P +.fi +.SH DESCRIPTION +The +.BR min () +function limits +.I v +to a given minimum value +.IR min_v . +The +.BR max () +function limits +.I v +to a given maximum value +.IR max_v . +The +.BR cap () +function limits +.I v +between a given minimum value +.I min_v +and a maximum value +.IR max_v . +.fi +.SH RETURN VALUE +.BR max () +and +.BR min () +return the passed value +.I v +if it is in above/below the given maximum/minimum. Otherwise the maximum/minimum is returned. +.BR cap () +returns the given value +.I v +if it is between the given maximum and minimum. +Otherwise the maximum is returned if the value is above the maximum +.IR max_v , +the minimum is returned if the value is below the minimum +.IR min_v . +.\" SRC END diff --git a/doc/max.3 b/doc/max.3 new file mode 100644 index 0000000..353b64d --- /dev/null +++ b/doc/max.3 @@ -0,0 +1,60 @@ +'\" t +.\" Copyright 2024 axtlos (axtlos@disroot.org) +.\" +.\" SPDX-License-Identifier: BSD-3-Clause + +.TH max 3 2024-09-23 "extlib" +.SH NAME +cap, min, max \- limit a float between a min/max value +.SH LIBRARY +extlib extended standard library +.RI ( libextlib ", " \-lextlib ) +.SH SYNOPSIS +.nf +.B #include <extlib.h> +.P +.BI "float min (float v, float min_v);" +.P +.BI "float max (float v, float max_v);" +.P +.BI "float cap (float v, float min_v, float max_v);" +.P +.fi +.SH DESCRIPTION +The +.BR min () +function limits +.I v +to a given minimum value +.IR min_v . +The +.BR max () +function limits +.I v +to a given maximum value +.IR max_v . +The +.BR cap () +function limits +.I v +between a given minimum value +.I min_v +and a maximum value +.IR max_v . +.fi +.SH RETURN VALUE +.BR max () +and +.BR min () +return the passed value +.I v +if it is in above/below the given maximum/minimum. Otherwise the maximum/minimum is returned. +.BR cap () +returns the given value +.I v +if it is between the given maximum and minimum. +Otherwise the maximum is returned if the value is above the maximum +.IR max_v , +the minimum is returned if the value is below the minimum +.IR min_v . +.\" SRC END diff --git a/doc/min.3 b/doc/min.3 new file mode 100644 index 0000000..c260207 --- /dev/null +++ b/doc/min.3 @@ -0,0 +1,60 @@ +'\" t +.\" Copyright 2024 axtlos (axtlos@disroot.org) +.\" +.\" SPDX-License-Identifier: BSD-3-Clause + +.TH min 3 2024-09-23 "extlib" +.SH NAME +cap, min, max \- limit a float between a min/max value +.SH LIBRARY +extlib extended standard library +.RI ( libextlib ", " \-lextlib ) +.SH SYNOPSIS +.nf +.B #include <extlib.h> +.P +.BI "float min (float v, float min_v);" +.P +.BI "float max (float v, float max_v);" +.P +.BI "float cap (float v, float min_v, float max_v);" +.P +.fi +.SH DESCRIPTION +The +.BR min () +function limits +.I v +to a given minimum value +.IR min_v . +The +.BR max () +function limits +.I v +to a given maximum value +.IR max_v . +The +.BR cap () +function limits +.I v +between a given minimum value +.I min_v +and a maximum value +.IR max_v . +.fi +.SH RETURN VALUE +.BR max () +and +.BR min () +return the passed value +.I v +if it is in above/below the given maximum/minimum. Otherwise the maximum/minimum is returned. +.BR cap () +returns the given value +.I v +if it is between the given maximum and minimum. +Otherwise the maximum is returned if the value is above the maximum +.IR max_v , +the minimum is returned if the value is below the minimum +.IR min_v . +.\" SRC END diff --git a/src/extlib.c b/src/extlib.c index e1fdb14..108e3e5 100644 --- a/src/extlib.c +++ b/src/extlib.c @@ -96,3 +96,21 @@ rrmdir (char *pathname) return err; return 0; } + +inline float +min (float v, float min_v) +{ + return (v < min_v) ? min_v : v; +} + +inline float +max (float v, float max_v) +{ + return (v > max_v) ? max_v : v; +} + +inline float +cap (float v, float min_v, float max_v) +{ + return min(max(v, max_v), min_v); +} diff --git a/src/extlib.h b/src/extlib.h index c62c374..c36b919 100644 --- a/src/extlib.h +++ b/src/extlib.h @@ -67,3 +67,12 @@ char *replace_str (char *s, char *old, char *replace); /// Join an array of strings into one string char *join_str (char **s, size_t len, char delim); + +/// Limit a value to a maximum +float max (float v, float max_v); + +/// Limit a value to a minimum +float min (float v, float min_v); + +/// Limit a value between min and max +float cap (float v, float min_v, float max_v); diff --git a/tests/test.c b/tests/test.c index 332ebb8..0d1ff7c 100644 --- a/tests/test.c +++ b/tests/test.c @@ -7,6 +7,38 @@ #include "../src/extlib.h" int +test_min_max_cap () +{ + int val = 5; + int max_v = 10; + int min_v = 1; + if (cap(val, min_v, max_v) != 5) + return 1; + if (max(val, max_v) != 5) + return 2; + if (min(val, min_v) != 5) + return 3; + + val = 11; + if (cap(val, min_v, max_v) != 10) + return 4; + if (max(val, max_v) != 10) + return 5; + if (min(val, min_v) != 11) + return 6; + + val = 0; + if (cap(val, min_v, max_v) != 1) + return 7; + if (max(val, max_v) != 0) + return 8; + if (min(val, min_v) != 1) + return 9; + + return 0; +} + +int test_malloc_free_secure (size_t size) { char *mall_test = malloc_secure (size); @@ -178,5 +210,12 @@ main (void) printf ("Test Case 6: test_join_str -- SUCCESS\n\n"); else printf ("Test Case 6: test_join_str -- FAILED %d\n\n", test_6_result); + + printf ("Test Case 7: test_min_max_cap\n"); + int test_7_result = test_min_max_cap (); + if (test_7_result == 0) + printf ("Test Case 7: test_min_max_cap -- SUCCESS\n\n"); + else + printf ("Test Case 7: test_min_max_cap -- FAILED %d\n\n", test_7_result); return 0; } |