From: "Richard W.M. Jones" <rjones(a)redhat.com>
Partially port nbdkit commit b55d85696172 ("common/include: Add further
unit tests.", 2019-01-09) to libnbd. In libnbd, we already use the header
"minmax.h", but we don't have the matching test case for it.
Porting notes:
- nbdkit commit b55d85696172 also introduces "test-nextnonzero". In
libnbd, we don't have "nextnonzero.h" for the time being, so the test
case is not ported either.
- In the test program being ported, update the (C) notice. Extend the year
range to 2023, plus remove the "All rights reserved" line (to be
consistent with the other test cases in libnbd).
Signed-off-by: Laszlo Ersek <lersek(a)redhat.com>
(cherry picked from nbdkit commit b55d85696172f9a54e7a5688897821d5f72237b4)
---
common/include/Makefile.am | 5 +
common/include/test-minmax.c | 157 ++++++++++++++++++++
.gitignore | 1 +
3 files changed, 163 insertions(+)
diff --git a/common/include/Makefile.am b/common/include/Makefile.am
index 1d7720d9b353..8767818c2b3b 100644
--- a/common/include/Makefile.am
+++ b/common/include/Makefile.am
@@ -41,6 +41,7 @@ TESTS = \
test-isaligned \
test-ispowerof2 \
test-iszero \
+ test-minmax \
$(NULL)
check_PROGRAMS = $(TESTS)
@@ -67,3 +68,7 @@ test_ispowerof2_CFLAGS = $(WARNINGS_CFLAGS)
test_iszero_SOURCES = test-iszero.c iszero.h
test_iszero_CPPFLAGS = -I$(srcdir)
test_iszero_CFLAGS = $(WARNINGS_CFLAGS)
+
+test_minmax_SOURCES = test-minmax.c minmax.h
+test_minmax_CPPFLAGS = -I$(srcdir)
+test_minmax_CFLAGS = $(WARNINGS_CFLAGS)
diff --git a/common/include/test-minmax.c b/common/include/test-minmax.c
new file mode 100644
index 000000000000..e622cfefe1ff
--- /dev/null
+++ b/common/include/test-minmax.c
@@ -0,0 +1,157 @@
+/* nbdkit
+ * Copyright (C) 2018-2023 Red Hat Inc.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are
+ * met:
+ *
+ * * Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ *
+ * * Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * * Neither the name of Red Hat nor the names of its contributors may be
+ * used to endorse or promote products derived from this software without
+ * specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY RED HAT AND CONTRIBUTORS ''AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
+ * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
+ * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL RED HAT OR
+ * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+ * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
+ * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
+ * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
+ * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
+ * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+
+#include <config.h>
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <stdint.h>
+#include <limits.h>
+#include <assert.h>
+#include <float.h>
+
+#include "minmax.h"
+
+#define SIGNED_TEST(var, min, max) \
+ MIN_SIGNED_TEST (var, min, max) \
+ MAX_SIGNED_TEST (var, min, max)
+
+#define MIN_SIGNED_TEST(var, min, max) \
+ var = 0; \
+ assert (MIN (0, var) == 0); \
+ var = 1; \
+ assert (MIN (0, var) == 0); \
+ var = -1; \
+ assert (MIN (0, var) == -1); \
+ var = 1; \
+ assert (MIN (var, 0) == 0); \
+ var = 1; \
+ assert (MIN (var, 1) == 1); \
+ var = -1; \
+ assert (MIN (var, 0) == -1); \
+ var = min; \
+ assert (MIN (var, min) == min); \
+ var = max; \
+ assert (MIN (var, max) == max); \
+ var = min; \
+ assert (MIN (var, max) == min); \
+ assert (MIN (0, min) == min);
+
+#define MAX_SIGNED_TEST(var, min, max) \
+ var = 0; \
+ assert (MAX (0, var) == 0); \
+ var = 1; \
+ assert (MAX (0, var) == 1); \
+ var = -1; \
+ assert (MAX (0, var) == 0); \
+ var = 1; \
+ assert (MAX (var, 0) == 1); \
+ var = 1; \
+ assert (MAX (var, 1) == 1); \
+ var = -1; \
+ assert (MAX (var, 0) == 0); \
+ var = min; \
+ assert (MAX (var, min) == min); \
+ var = max; \
+ assert (MAX (var, max) == max); \
+ var = min; \
+ assert (MAX (var, max) == max); \
+ assert (MAX (0, min) == 0);
+
+#define UNSIGNED_TEST(var, max) \
+ MIN_UNSIGNED_TEST (var, max) \
+ MAX_UNSIGNED_TEST (var, max)
+
+#define MIN_UNSIGNED_TEST(var, max) \
+ var = 0; \
+ assert (MIN (0, var) == 0); \
+ var = 1; \
+ assert (MIN (0, var) == 0); \
+ var = 1; \
+ assert (MIN (var, 0) == 0); \
+ var = 1; \
+ assert (MIN (var, 1) == 1); \
+ var = max; \
+ assert (MIN (var, max) == max);
+
+#define MAX_UNSIGNED_TEST(var, max) \
+ var = 0; \
+ assert (MAX (0, var) == 0); \
+ var = 1; \
+ assert (MAX (0, var) == 1); \
+ var = 1; \
+ assert (MAX (var, 0) == 1); \
+ var = 1; \
+ assert (MAX (var, 1) == 1); \
+ var = max; \
+ assert (MAX (var, max) == max);
+
+int
+main (void)
+{
+ signed char sc;
+ int i;
+ int8_t i8;
+ int16_t i16;
+ int32_t i32;
+ int64_t i64;
+ unsigned char uc;
+ unsigned u;
+ uint8_t u8;
+ uint16_t u16;
+ uint32_t u32;
+ uint64_t u64;
+ float f;
+ double d;
+
+ SIGNED_TEST (sc, SCHAR_MIN, SCHAR_MAX);
+ SIGNED_TEST (i, INT_MIN, INT_MAX);
+ SIGNED_TEST (i8, INT8_MIN, INT8_MAX);
+ SIGNED_TEST (i16, INT16_MIN, INT16_MAX);
+ SIGNED_TEST (i32, INT32_MIN, INT32_MAX);
+ SIGNED_TEST (i64, INT64_MIN, INT64_MAX);
+
+ UNSIGNED_TEST (uc, UCHAR_MAX);
+ UNSIGNED_TEST (u, UINT_MAX);
+ UNSIGNED_TEST (u8, UINT8_MAX);
+ UNSIGNED_TEST (u16, UINT16_MAX);
+ UNSIGNED_TEST (u32, UINT32_MAX);
+ UNSIGNED_TEST (u64, UINT64_MAX);
+
+ /* Note that FLT_MIN and DBL_MIN are the closest positive normalized
+ * numbers to 0.0, not the min.
+ */
+ SIGNED_TEST (f, -FLT_MAX, FLT_MAX);
+ SIGNED_TEST (d, -DBL_MAX, DBL_MAX);
+
+ exit (EXIT_SUCCESS);
+}
diff --git a/.gitignore b/.gitignore
index 1225917034f3..4ebd96519c29 100644
--- a/.gitignore
+++ b/.gitignore
@@ -39,6 +39,7 @@ Makefile.in
/common/include/test-isaligned
/common/include/test-ispowerof2
/common/include/test-iszero
+/common/include/test-minmax
/common/utils/test-human-size
/common/utils/test-vector
/compile