Commit eb3efdf5 authored by Guillem Jover's avatar Guillem Jover

libdpkg: Add new clamp macro

parent fb2172fe
......@@ -115,6 +115,19 @@
#endif
#endif
/**
* @def clamp
*
* Returns a normalized value within the low and high limits.
*
* @param v The value to clamp.
* @param l The low limit.
* @param h The high limit.
*/
#ifndef clamp
#define clamp(v, l, h) ((v) > (h) ? (h) : ((v) < (l) ? (l) : (v)))
#endif
/** @} */
#endif /* LIBDPKG_MACROS_H */
......@@ -2,7 +2,7 @@
* libdpkg - Debian packaging suite library routines
* t-macros.c - test C support macros
*
* Copyright © 2009 Guillem Jover <guillem@debian.org>
* Copyright © 2009,2012 Guillem Jover <guillem@debian.org>
*
* This is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
......@@ -36,4 +36,9 @@ test(void)
test_pass(max(30, 10) == 30);
test_pass(max(0, 10) == 10);
test_pass(max(-10, 0) == 0);
test_pass(clamp(0, 0, 0) == 0);
test_pass(clamp(0, -10, 10) == 0);
test_pass(clamp(20, -10, 10) == 10);
test_pass(clamp(-20, -10, 10) == -10);
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment