Prechádzať zdrojové kódy

libdpkg: Add new clamp macro

Guillem Jover 14 rokov pred
rodič
commit
eb3efdf553
2 zmenil súbory, kde vykonal 19 pridanie a 1 odobranie
  1. 13 0
      lib/dpkg/macros.h
  2. 6 1
      lib/dpkg/test/t-macros.c

+ 13 - 0
lib/dpkg/macros.h

@@ -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 */

+ 6 - 1
lib/dpkg/test/t-macros.c

@@ -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);
 }