Laszlo Ersek
2023-Mar-01 11:40 UTC
[Libguestfs] [libnbd PATCH 0/6] common: catch up with nbdkit
If we compare the "common" subdirectory between nbdkit @ 6b4178d0fdfe ("ci: Temporarily disable perl in MacOS", 2023-02-27) and libnbd @ d05cd8f384a7 ("Version 1.15.11.", 2023-02-28), we find differences. We can categorize these differences along two (orthogonal) axes: - Intentional or unintentional. Intentional differences are for example when one of the libnbd and nbdkit projects has either a customization or a feature that the other project doesn't need or want. (Customizations can be small, such as a divergence inside a header file. Feature granularity is larger: a feature usually corresponds to a full header file.) Unintentional differences come from missing ports, or incomplete ports. - Libnbd not having a change from nbdkit (whether intentionally or not), vs. nbdkit not having a change from libnbd (ditto). I've analyzed each of the differences found. This patch set eliminates those differences where libnbd unintentionally lags behind nbdkit. Later I plan to post the counterpart series for nbdkit (eliminating the differences where nbdkit unintentionally lags behind libnbd). Note that the counterpart series for nbdkit will not target libnbd @ d05cd8f384a7 (that is, libnbd 1.15.11) at once. Instead, it will target libnbd v1.15.9 at first. Then I plan to port my patches between libnbd v1.15.9 and v1.15.11 to nbdkit separately -- that's going to be more work. So this series, and the counterpart for nbdkit, will eliminate past debt, in order to form a common baseline for porting my recent patches from libnbd to nbdkit. The larger LISTEN_FDNAMES series will be resumed even later. Laszlo Eric Blake (1): tests: Avoid odd test behavior under NDEBUG [partial port] Richard W.M. Jones (5): common/include: Add unit tests [byte-swapping, isaligned, iszero] common/include: Add further unit tests [minmax] common: include: Define bswap_16, bswap_32 and bswap_64 [finish port] vector: Add vector_duplicate function common/include: Fix MIN and MAX macros so they can be nested [finish port] .gitignore | 4 + common/include/Makefile.am | 20 +++ common/include/test-byte-swapping.c | 108 ++++++++++++ common/include/test-isaligned.c | 62 +++++++ common/include/test-iszero.c | 66 ++++++++ common/include/test-minmax.c | 172 ++++++++++++++++++++ common/utils/vector.h | 17 ++ configure.ac | 3 + 8 files changed, 452 insertions(+) create mode 100644 common/include/test-byte-swapping.c create mode 100644 common/include/test-isaligned.c create mode 100644 common/include/test-iszero.c create mode 100644 common/include/test-minmax.c base-commit: d05cd8f384a7278187d7e87c4dc2a346e879773b
Laszlo Ersek
2023-Mar-01 11:40 UTC
[Libguestfs] [libnbd PATCH 1/6] common/include: Add unit tests [byte-swapping, isaligned, iszero]
From: "Richard W.M. Jones" <rjones at redhat.com> Partially port nbdkit commit 58e7b0e104d5 ("common/include: Add unit tests.", 2018-12-29) to libnbd. In libnbd, we already use the headers "byte-swapping.h", "isaligned.h" and "iszero.h", but we don't have the matching test cases for them. Porting notes: - nbdkit commit 58e7b0e104d5 also introduces "test-current-dir-name". In libnbd, we don't have "get-current-dir-name.h" for the time being, so the test case is not ported either. - In libnbd, we have "ispowerof2" (both header and test case) from earlier, so porting that part of nbdkit commit 58e7b0e104d5 is not necessary. - In the three test programs 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 at redhat.com> (cherry picked from nbdkit commit 58e7b0e104d57d767d11aacab7d7ab82f0d4a98b) --- common/include/Makefile.am | 15 ++++ common/include/test-byte-swapping.c | 89 ++++++++++++++++++++ common/include/test-isaligned.c | 61 ++++++++++++++ common/include/test-iszero.c | 65 ++++++++++++++ .gitignore | 3 + 5 files changed, 233 insertions(+) diff --git a/common/include/Makefile.am b/common/include/Makefile.am index 8d6b2a88be13..1d7720d9b353 100644 --- a/common/include/Makefile.am +++ b/common/include/Makefile.am @@ -36,8 +36,11 @@ EXTRA_DIST = \ TESTS = \ test-array-size \ + test-byte-swapping \ test-checked-overflow \ + test-isaligned \ test-ispowerof2 \ + test-iszero \ $(NULL) check_PROGRAMS = $(TESTS) @@ -45,10 +48,22 @@ test_array_size_SOURCES = test-array-size.c array-size.h test_array_size_CPPFLAGS = -I$(srcdir) test_array_size_CFLAGS = $(WARNINGS_CFLAGS) +test_byte_swapping_SOURCES = test-byte-swapping.c byte-swapping.h +test_byte_swapping_CPPFLAGS = -I$(srcdir) +test_byte_swapping_CFLAGS = $(WARNINGS_CFLAGS) + test_checked_overflow_SOURCES = test-checked-overflow.c checked-overflow.h test_checked_overflow_CPPFLAGS = -I$(srcdir) test_checked_overflow_CFLAGS = $(WARNINGS_CFLAGS) +test_isaligned_SOURCES = test-isaligned.c isaligned.h +test_isaligned_CPPFLAGS = -I$(srcdir) +test_isaligned_CFLAGS = $(WARNINGS_CFLAGS) + test_ispowerof2_SOURCES = test-ispowerof2.c ispowerof2.h test_ispowerof2_CPPFLAGS = -I$(srcdir) test_ispowerof2_CFLAGS = $(WARNINGS_CFLAGS) + +test_iszero_SOURCES = test-iszero.c iszero.h +test_iszero_CPPFLAGS = -I$(srcdir) +test_iszero_CFLAGS = $(WARNINGS_CFLAGS) diff --git a/common/include/test-byte-swapping.c b/common/include/test-byte-swapping.c new file mode 100644 index 000000000000..fcb83c574bfc --- /dev/null +++ b/common/include/test-byte-swapping.c @@ -0,0 +1,89 @@ +/* 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 <string.h> +#include <assert.h> + +#include "byte-swapping.h" + +/* Little-endian test strings. */ +static uint8_t le16[] = { 0x34, 0x12 }; +static uint8_t le32[] = { 0x78, 0x56, 0x34, 0x12 }; +static uint8_t le64[] = { 0xf0, 0xde, 0xbc, 0x9a, 0x78, 0x56, 0x34, 0x12 }; + +/* Big-endian test strings. */ +static uint8_t be16[] = { 0x12, 0x34 }; +static uint8_t be32[] = { 0x12, 0x34, 0x56, 0x78 }; +static uint8_t be64[] = { 0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0 }; + +int +main (void) +{ + uint16_t i16; + uint32_t i32; + uint64_t i64; + + memcpy (&i16, le16, 2); + assert (le16toh (i16) == 0x1234); + memcpy (&i32, le32, 4); + assert (le32toh (i32) == 0x12345678); + memcpy (&i64, le64, 8); + assert (le64toh (i64) == 0x123456789abcdef0); + + memcpy (&i16, be16, 2); + assert (be16toh (i16) == 0x1234); + memcpy (&i32, be32, 4); + assert (be32toh (i32) == 0x12345678); + memcpy (&i64, be64, 8); + assert (be64toh (i64) == 0x123456789abcdef0); + + i16 = htole16 (0x1234); + assert (memcmp (&i16, le16, 2) == 0); + i32 = htole32 (0x12345678); + assert (memcmp (&i32, le32, 4) == 0); + i64 = htole64 (0x123456789abcdef0); + assert (memcmp (&i64, le64, 8) == 0); + + i16 = htobe16 (0x1234); + assert (memcmp (&i16, be16, 2) == 0); + i32 = htobe32 (0x12345678); + assert (memcmp (&i32, be32, 4) == 0); + i64 = htobe64 (0x123456789abcdef0); + assert (memcmp (&i64, be64, 8) == 0); + + exit (EXIT_SUCCESS); +} diff --git a/common/include/test-isaligned.c b/common/include/test-isaligned.c new file mode 100644 index 000000000000..e3709a8f05bd --- /dev/null +++ b/common/include/test-isaligned.c @@ -0,0 +1,61 @@ +/* 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 <string.h> +#include <assert.h> + +#include "isaligned.h" + +int +main (void) +{ + uint64_t i; + + for (i = 1; i <= 0x80000000; i <<= 1) { + assert (IS_ALIGNED (0, i)); + assert (IS_ALIGNED (i, i)); + assert (IS_ALIGNED (i*2, i)); + assert (IS_ALIGNED (i*3, i)); + assert (IS_ALIGNED (i*4, i)); + if (i > 1) { + assert (! IS_ALIGNED (i-1, i)); + assert (! IS_ALIGNED (i/2, i)); + } + } + + exit (EXIT_SUCCESS); +} diff --git a/common/include/test-iszero.c b/common/include/test-iszero.c new file mode 100644 index 000000000000..6c2836678155 --- /dev/null +++ b/common/include/test-iszero.c @@ -0,0 +1,65 @@ +/* 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 <string.h> +#include <assert.h> + +#include "iszero.h" + +int +main (void) +{ + char *buf; + size_t i, j; + + buf = malloc (256); + if (buf == NULL) { + perror ("malloc"); + exit (EXIT_FAILURE); + } + memset (buf, 0, 256); + + for (j = 0; j <= 16; ++j) { + for (i = 0; i <= 16; ++i) + assert (is_zero (&buf[j], i)); + for (i = 0; i <= 16; ++i) + assert (is_zero (&buf[j], 256-j-i)); + } + + free (buf); + exit (EXIT_SUCCESS); +} diff --git a/.gitignore b/.gitignore index f42737131d8c..1225917034f3 100644 --- a/.gitignore +++ b/.gitignore @@ -34,8 +34,11 @@ Makefile.in /bash-completion/nbdinfo /bash-completion/nbdublk /common/include/test-array-size +/common/include/test-byte-swapping /common/include/test-checked-overflow +/common/include/test-isaligned /common/include/test-ispowerof2 +/common/include/test-iszero /common/utils/test-human-size /common/utils/test-vector /compile
Laszlo Ersek
2023-Mar-01 11:40 UTC
[Libguestfs] [libnbd PATCH 2/6] common/include: Add further unit tests [minmax]
From: "Richard W.M. Jones" <rjones at 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 at 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
Laszlo Ersek
2023-Mar-01 11:40 UTC
[Libguestfs] [libnbd PATCH 3/6] common: include: Define bswap_16, bswap_32 and bswap_64 [finish port]
From: "Richard W.M. Jones" <rjones at redhat.com> Complete porting nbdkit commit 2bc1f3383e00 ("common: include: Define bswap_16, bswap_32 and bswap_64.", 2020-05-30). Libnbd commit def468c2a9e5 ("common/include/byte-swapping.h: Synchronize with nbdkit.", 2020-10-27) had ported nbdkit commit 2bc1f3383e00 earlier, but it contains the following omissions / mistakes: - In the header file, it starts using the WORDS_BIGENDIAN macro, but it does not port the AC_C_BIGENDIAN stanza from nbdkit's "configure.ac". - It does not port the test case extension. Fill those gaps now. Signed-off-by: Laszlo Ersek <lersek at redhat.com> (cherry picked from nbdkit commit 2bc1f3383e00bd5bbe2e1bb911ab56e1e42c4db3) --- configure.ac | 3 +++ common/include/test-byte-swapping.c | 18 ++++++++++++++++++ 2 files changed, 21 insertions(+) diff --git a/configure.ac b/configure.ac index d967277ea308..6919b2782d0a 100644 --- a/configure.ac +++ b/configure.ac @@ -43,6 +43,9 @@ AM_PROG_CC_C_O AX_PTHREAD +dnl Defines WORDS_BIGENDIAN on big endian platforms. +AC_C_BIGENDIAN + dnl Check for C++ (optional, we just use this to test the header dnl can be included from C++ code). AC_PROG_CXX diff --git a/common/include/test-byte-swapping.c b/common/include/test-byte-swapping.c index fcb83c574bfc..741b3d11517e 100644 --- a/common/include/test-byte-swapping.c +++ b/common/include/test-byte-swapping.c @@ -85,5 +85,23 @@ main (void) i64 = htobe64 (0x123456789abcdef0); assert (memcmp (&i64, be64, 8) == 0); + memcpy (&i16, le16, 2); + i16 = bswap_16 (i16); + assert (memcmp (&i16, be16, 2) == 0); + i16 = bswap_16 (i16); + assert (memcmp (&i16, le16, 2) == 0); + + memcpy (&i32, le32, 4); + i32 = bswap_32 (i32); + assert (memcmp (&i32, be32, 4) == 0); + i32 = bswap_32 (i32); + assert (memcmp (&i32, le32, 4) == 0); + + memcpy (&i64, le64, 8); + i64 = bswap_64 (i64); + assert (memcmp (&i64, be64, 8) == 0); + i64 = bswap_64 (i64); + assert (memcmp (&i64, le64, 8) == 0); + exit (EXIT_SUCCESS); }
Laszlo Ersek
2023-Mar-01 11:40 UTC
[Libguestfs] [libnbd PATCH 4/6] tests: Avoid odd test behavior under NDEBUG [partial port]
From: Eric Blake <eblake at redhat.com> Original (nbdkit) commit message: While we support compilation with CFLAGS=-DNDEBUG for the brave user desiring to avoid assertion overhead, this causes the tests to be less powerful (the tests "fail open" for anything where we relied on an assert to validate nbdkit was behaving correctly; and this would be even worse if we had side effects in a test assert, such as we just fixed in d5cedf20). Fortunately, a quick test of './configure CFLAGS=-DNDEBUG' didn't hit any test failures, but it seems better to just ensure that assertions always work in our tests, even when they are turned off for the main binary. Porting notes: This is a partial port. At this point, the test cases we have under common/ actually reflect our common/ header file usage (meaning that, if nbdkit has a test case for a header file we use, then we also have that test case). However, test case *content* remains different at this point; one piece is nbdkit commit 0b6d62668584 ("tests: Avoid odd test behavior under NDEBUG", 2020-07-07). Port those parts of it that modify such test cases that libnbd does have, at this point. Note that the nbdkit commit modifies "test-ispowerof2.c" as well, be we have ported that particular NDEBUG addition earlier (in libnbd commit 6cc512341281, "common/include: Copy test-ispowerof2.c from nbdkit", 2022-08-25). Signed-off-by: Eric Blake <eblake at redhat.com> Signed-off-by: Laszlo Ersek <lersek at redhat.com> (cherry picked from nbdkit commit 0b6d626685846d41ea113a394cb37e321c63e8bf) --- common/include/test-byte-swapping.c | 1 + common/include/test-isaligned.c | 1 + common/include/test-iszero.c | 1 + common/include/test-minmax.c | 1 + 4 files changed, 4 insertions(+) diff --git a/common/include/test-byte-swapping.c b/common/include/test-byte-swapping.c index 741b3d11517e..a3b9b68b785f 100644 --- a/common/include/test-byte-swapping.c +++ b/common/include/test-byte-swapping.c @@ -36,6 +36,7 @@ #include <stdlib.h> #include <stdint.h> #include <string.h> +#undef NDEBUG /* Keep test strong even for nbdkit built without assertions */ #include <assert.h> #include "byte-swapping.h" diff --git a/common/include/test-isaligned.c b/common/include/test-isaligned.c index e3709a8f05bd..4240df6f80c4 100644 --- a/common/include/test-isaligned.c +++ b/common/include/test-isaligned.c @@ -36,6 +36,7 @@ #include <stdlib.h> #include <stdint.h> #include <string.h> +#undef NDEBUG /* Keep test strong even for nbdkit built without assertions */ #include <assert.h> #include "isaligned.h" diff --git a/common/include/test-iszero.c b/common/include/test-iszero.c index 6c2836678155..36769d1305c7 100644 --- a/common/include/test-iszero.c +++ b/common/include/test-iszero.c @@ -36,6 +36,7 @@ #include <stdlib.h> #include <stdint.h> #include <string.h> +#undef NDEBUG /* Keep test strong even for nbdkit built without assertions */ #include <assert.h> #include "iszero.h" diff --git a/common/include/test-minmax.c b/common/include/test-minmax.c index e622cfefe1ff..285f8074e503 100644 --- a/common/include/test-minmax.c +++ b/common/include/test-minmax.c @@ -36,6 +36,7 @@ #include <stdlib.h> #include <stdint.h> #include <limits.h> +#undef NDEBUG /* Keep test strong even for nbdkit built without assertions */ #include <assert.h> #include <float.h>
Laszlo Ersek
2023-Mar-01 11:40 UTC
[Libguestfs] [libnbd PATCH 5/6] vector: Add vector_duplicate function
From: "Richard W.M. Jones" <rjones at redhat.com> Original commit message: Duplicate a vector, optionally in-place. Porting notes: this port of nbdkit commit 78b72746e547 ("vector: Add vector_duplicate function", 2021-08-22) includes: - The renaming of vector fields from nbdkit commit 0b0eece73f04 ("common/utils/vector: Rename `size` to `len`", 2021-11-07); otherwise, the patch wouldn't compile. - The effect of libnbd commit 8fb8ffb53477 ("build: Silence some cppcheck warnings", 2022-11-02), which added "__attribute__((__unused__))" to the other vector APIs. - The effect of libnbd commit b5101fbc59cb ("use space consistently in function and function-like macro invocations", 2023-02-22), which inserted a space between "__attribute__" and "((__unused__))". Signed-off-by: Laszlo Ersek <lersek at redhat.com> (cherry picked from nbdkit commit 78b72746e547150290c2b0f2fe3ad5fe69817904) --- common/utils/vector.h | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/common/utils/vector.h b/common/utils/vector.h index bdba02f7ebee..d0b250db87fc 100644 --- a/common/utils/vector.h +++ b/common/utils/vector.h @@ -181,6 +181,23 @@ (void *) compare); \ } \ \ + /* Make a new vector with the same elements. */ \ + static inline int __attribute__ ((__unused__)) \ + name##_duplicate (name *v, name *copy) \ + { \ + /* Note it's allowed for v and copy to be the same pointer. */ \ + type *vptr = v->ptr; \ + type *newptr; \ + size_t len = v->len * sizeof (type); \ + \ + newptr = malloc (len); \ + if (newptr == NULL) return -1; \ + memcpy (newptr, vptr, len); \ + copy->ptr = newptr; \ + copy->len = copy->cap = v->len; \ + return 0; \ + } \ + \ /* End with duplicate declaration, so callers must supply ';'. */ \ struct name
Laszlo Ersek
2023-Mar-01 11:40 UTC
[Libguestfs] [libnbd PATCH 6/6] common/include: Fix MIN and MAX macros so they can be nested [finish port]
From: "Richard W.M. Jones" <rjones at redhat.com> Original commit message: Thanks: Eric Blake Porting notes: We already have the nesting fix in the "common/include/minmax.h" header file, from nbdkit commit 9f462af12e3b ("common/include: Fix MIN and MAX macros so they can be nested", 2022-02-22), via libnbd commit 2f25695212db5. However, that port didn't include the update to the test case. Do it now. Signed-off-by: Laszlo Ersek <lersek at redhat.com> (cherry picked from nbdkit commit 9f462af12e3b8b9840435a266d7e6e7d45ed70bf) --- common/include/test-minmax.c | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/common/include/test-minmax.c b/common/include/test-minmax.c index 285f8074e503..095c91491acc 100644 --- a/common/include/test-minmax.c +++ b/common/include/test-minmax.c @@ -154,5 +154,19 @@ main (void) SIGNED_TEST (f, -FLT_MAX, FLT_MAX); SIGNED_TEST (d, -DBL_MAX, DBL_MAX); + /* Test that MIN and MAX can be nested. This is really a compile + * test, but we do check the answer. + */ + assert (MIN (MIN (1, 2), 3) == 1); + assert (MAX (MIN (1, 2), 3) == 3); + assert (MIN (MAX (1, 2), 3) == 2); + assert (MAX (MAX (1, 4), 3) == 4); + assert (MIN (3, MIN (1, 2)) == 1); + assert (MAX (3, MIN (1, 2)) == 3); + assert (MIN (3, MAX (1, 2)) == 2); + assert (MAX (3, MAX (1, 4)) == 4); + assert (MIN (MIN (1, MIN (2, 3)), 4) == 1); + assert (MAX (MAX (1, MAX (2, 3)), 4) == 4); + exit (EXIT_SUCCESS); }
Possibly Parallel Threads
- [nbdkit PATCH] RFC tests: Avoid odd test behavior under NDEBUG
- [PATCH libnbd 0/5] copy: Allow human sizes for --queue-size, etc
- [PATCH nbdkit] server: Move size parsing code (nbdkit_parse_size) to common/include
- [PATCH v3 0/4] file: Zero for block devices and older file systems
- [PATCH nbdkit 0/3] Add partitioning plugin.