From: Andre Muezerie <andremue@linux.microsoft.com>
To: andremue@linux.microsoft.com
Cc: dev@dpdk.org, mb@smartsharesystems.com
Subject: [PATCH v2 5/5] app/test: add tests for portable version of __builtin_add_overflow
Date: Fri, 3 Jan 2025 12:39:41 -0800 [thread overview]
Message-ID: <1735936781-24199-6-git-send-email-andremue@linux.microsoft.com> (raw)
In-Reply-To: <1735936781-24199-1-git-send-email-andremue@linux.microsoft.com>
__builtin_add_overflow is gcc specific. There's a need for a portable
version that can also be used with other compilers.
This patch adds tests for these new portable functions, to confirm
they behave the same way across different compilers.
Signed-off-by: Andre Muezerie <andremue@linux.microsoft.com>
---
app/test/meson.build | 1 +
app/test/test_math.c | 170 +++++++++++++++++++++++++++++++++++++++++++
2 files changed, 171 insertions(+)
create mode 100644 app/test/test_math.c
diff --git a/app/test/meson.build b/app/test/meson.build
index d5cb6a7f7a..49e13c5505 100644
--- a/app/test/meson.build
+++ b/app/test/meson.build
@@ -118,6 +118,7 @@ source_file_deps = {
'test_lpm_perf.c': ['net', 'lpm'],
'test_malloc.c': [],
'test_malloc_perf.c': [],
+ 'test_math.c': [],
'test_mbuf.c': ['net'],
'test_mcslock.c': [],
'test_member.c': ['member', 'net'],
diff --git a/app/test/test_math.c b/app/test/test_math.c
new file mode 100644
index 0000000000..5b53eba71d
--- /dev/null
+++ b/app/test/test_math.c
@@ -0,0 +1,170 @@
+/* SPDX-License-Identifier: BSD-3-Clause
+ * Copyright (C) 2025 Microsoft Corporation
+ */
+
+#include <rte_math.h>
+#include <rte_debug.h>
+
+#include "test.h"
+#include <inttypes.h>
+
+/* Check condition and return if true. */
+#define TEST_MATH_RETURN_IF_ERROR(X) \
+do { \
+ if (X) { \
+ return -1; \
+ } \
+} while (0)
+
+RTE_LOG_REGISTER(math_logtype_test, test.math, INFO);
+
+static int
+verify_add_overflow8(uint8_t a, uint8_t b,
+ uint8_t expected_res, uint8_t expected_overflow)
+{
+ uint8_t res;
+ uint8_t overflow = rte_add_overflow(a, b, &res);
+ RTE_TEST_ASSERT_EQUAL(res, expected_res,
+ "ERROR: rte_add_overflow(0x%x, 0x%x) returned result 0x%x,"
+ " but 0x%x was expected.", a, b, res, expected_res);
+ RTE_TEST_ASSERT_EQUAL(overflow, expected_overflow,
+ "ERROR: rte_add_overflow(0x%x, 0x%x) returned overflow 0x%x,"
+ " but 0x%x was expected.", a, b, overflow, expected_overflow);
+
+ return 0;
+}
+
+static int
+verify_add_overflow16(uint16_t a, uint16_t b,
+ uint16_t expected_res, uint8_t expected_overflow)
+{
+ uint16_t res;
+ uint8_t overflow = rte_add_overflow(a, b, &res);
+ RTE_TEST_ASSERT_EQUAL(res, expected_res,
+ "ERROR: rte_add_overflow(0x%x, 0x%x) returned result 0x%x,"
+ " but 0x%x was expected.", a, b, res, expected_res);
+ RTE_TEST_ASSERT_EQUAL(overflow, expected_overflow,
+ "ERROR: rte_add_overflow(0x%x, 0x%x) returned overflow 0x%x,"
+ " but 0x%x was expected.", a, b, overflow, expected_overflow);
+
+ return 0;
+}
+
+static int
+verify_add_overflow32(uint32_t a, uint32_t b,
+ uint32_t expected_res, uint8_t expected_overflow)
+{
+ uint32_t res;
+ uint8_t overflow = rte_add_overflow(a, b, &res);
+ RTE_TEST_ASSERT_EQUAL(res, expected_res,
+ "ERROR: rte_add_overflow(0x%x, 0x%x) returned result 0x%x,"
+ " but 0x%x was expected.", a, b, res, expected_res);
+ RTE_TEST_ASSERT_EQUAL(overflow, expected_overflow,
+ "ERROR: rte_add_overflow(0x%x, 0x%x) returned overflow 0x%x,"
+ " but 0x%x was expected.", a, b, overflow, expected_overflow);
+
+ return 0;
+}
+
+static int
+verify_add_overflow64(uint64_t a, uint64_t b,
+ uint64_t expected_res, uint8_t expected_overflow)
+{
+ uint64_t res;
+ uint8_t overflow = rte_add_overflow(a, b, &res);
+ RTE_TEST_ASSERT_EQUAL(res, expected_res,
+ "ERROR: rte_add_overflow(0x%" PRIx64 ", 0x%" PRIx64 ") returned"
+ " result 0x%" PRIx64 ", but 0x%" PRIx64 " was expected.",
+ a, b, res, expected_res);
+ RTE_TEST_ASSERT_EQUAL(res, expected_res,
+ "ERROR: rte_add_overflow(0x%" PRIx64 ", 0x%" PRIx64 ") returned"
+ " overflow 0x%x, but 0x%x was expected.",
+ a, b, overflow, expected_overflow);
+
+ return 0;
+}
+
+static int
+test_add_overflow8(void)
+{
+ TEST_MATH_RETURN_IF_ERROR(verify_add_overflow8(0, 0, 0, 0));
+ TEST_MATH_RETURN_IF_ERROR(verify_add_overflow8(0, 1, 1, 0));
+ TEST_MATH_RETURN_IF_ERROR(verify_add_overflow8(0, 0xFF, 0xFF, 0));
+ TEST_MATH_RETURN_IF_ERROR(verify_add_overflow8(1, 0xFF, 0, 1));
+ TEST_MATH_RETURN_IF_ERROR(verify_add_overflow8(2, 0xFF, 1, 1));
+ TEST_MATH_RETURN_IF_ERROR(verify_add_overflow8(4, 0xFE, 2, 1));
+ TEST_MATH_RETURN_IF_ERROR(verify_add_overflow8(0xFF, 0xFF, 0xFE, 1));
+
+ return 0;
+}
+
+static int
+test_add_overflow16(void)
+{
+ TEST_MATH_RETURN_IF_ERROR(verify_add_overflow16(0, 0, 0, 0));
+ TEST_MATH_RETURN_IF_ERROR(verify_add_overflow16(0, 1, 1, 0));
+ TEST_MATH_RETURN_IF_ERROR(verify_add_overflow16(0, 0xFFFF, 0xFFFF, 0));
+ TEST_MATH_RETURN_IF_ERROR(verify_add_overflow16(1, 0xFFFF, 0, 1));
+ TEST_MATH_RETURN_IF_ERROR(verify_add_overflow16(2, 0xFFFF, 1, 1));
+ TEST_MATH_RETURN_IF_ERROR(verify_add_overflow16(4, 0xFFFE, 2, 1));
+ TEST_MATH_RETURN_IF_ERROR(verify_add_overflow16(
+ 0xFFFF, 0xFFFF, 0xFFFE, 1));
+
+ return 0;
+}
+
+static int
+test_add_overflow32(void)
+{
+ TEST_MATH_RETURN_IF_ERROR(verify_add_overflow32(0, 0, 0, 0));
+ TEST_MATH_RETURN_IF_ERROR(verify_add_overflow32(0, 1, 1, 0));
+ TEST_MATH_RETURN_IF_ERROR(verify_add_overflow32(
+ 0, 0xFFFFFFFF, 0xFFFFFFFF, 0));
+ TEST_MATH_RETURN_IF_ERROR(verify_add_overflow32(1, 0xFFFFFFFF, 0, 1));
+ TEST_MATH_RETURN_IF_ERROR(verify_add_overflow32(2, 0xFFFFFFFF, 1, 1));
+ TEST_MATH_RETURN_IF_ERROR(verify_add_overflow32(4, 0xFFFFFFFE, 2, 1));
+ TEST_MATH_RETURN_IF_ERROR(verify_add_overflow32(
+ 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFE, 1));
+
+ return 0;
+}
+
+static int
+test_add_overflow64(void)
+{
+ TEST_MATH_RETURN_IF_ERROR(verify_add_overflow64(0, 0, 0, 0));
+ TEST_MATH_RETURN_IF_ERROR(verify_add_overflow64(0, 1, 1, 0));
+ TEST_MATH_RETURN_IF_ERROR(verify_add_overflow64(
+ 0, 0xFFFFFFFFFFFFFFFF, 0xFFFFFFFFFFFFFFFF, 0));
+ TEST_MATH_RETURN_IF_ERROR(verify_add_overflow64(
+ 1, 0xFFFFFFFFFFFFFFFF, 0, 1));
+ TEST_MATH_RETURN_IF_ERROR(verify_add_overflow64(
+ 2, 0xFFFFFFFFFFFFFFFF, 1, 1));
+ TEST_MATH_RETURN_IF_ERROR(verify_add_overflow64(
+ 4, 0xFFFFFFFFFFFFFFFE, 2, 1));
+ TEST_MATH_RETURN_IF_ERROR(verify_add_overflow64(
+ 0xFFFFFFFFFFFFFFFF, 0xFFFFFFFFFFFFFFFF, 0xFFFFFFFFFFFFFFFE, 1));
+
+ return 0;
+}
+
+static struct unit_test_suite math_test_suite = {
+ .suite_name = "math autotest",
+ .setup = NULL,
+ .teardown = NULL,
+ .unit_test_cases = {
+ TEST_CASE(test_add_overflow8),
+ TEST_CASE(test_add_overflow16),
+ TEST_CASE(test_add_overflow32),
+ TEST_CASE(test_add_overflow64),
+ TEST_CASES_END()
+ }
+};
+
+static int
+test_math(void)
+{
+ return unit_test_suite_runner(&math_test_suite);
+}
+
+REGISTER_FAST_TEST(math_autotest, true, true, test_math);
--
2.47.0.vfs.0.3
prev parent reply other threads:[~2025-01-03 20:40 UTC|newest]
Thread overview: 21+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-01-02 22:32 [PATCH 0/5] add " Andre Muezerie
2025-01-02 22:32 ` [PATCH 1/5] maintainers: " Andre Muezerie
2025-01-02 22:32 ` [PATCH 2/5] lib/eal: " Andre Muezerie
2025-01-03 8:19 ` Morten Brørup
2025-01-03 20:38 ` Andre Muezerie
2025-01-02 22:32 ` [PATCH 3/5] doc/api: " Andre Muezerie
2025-01-02 22:32 ` [PATCH 4/5] drivers/net: use " Andre Muezerie
2025-01-02 22:32 ` [PATCH 5/5] app/test: add tests for portable versions " Andre Muezerie
2025-01-02 23:51 ` [PATCH 0/5] add portable version " Stephen Hemminger
2025-01-03 0:15 ` Andre Muezerie
2025-01-03 0:41 ` Andre Muezerie
2025-01-03 20:39 ` [PATCH v2 " Andre Muezerie
2025-01-03 20:39 ` [PATCH v2 1/5] maintainers: " Andre Muezerie
2025-01-03 20:39 ` [PATCH v2 2/5] lib/eal: " Andre Muezerie
2025-01-06 11:07 ` Bruce Richardson
2025-01-06 11:21 ` Morten Brørup
2025-01-06 11:34 ` Bruce Richardson
2025-01-06 11:58 ` Morten Brørup
2025-01-03 20:39 ` [PATCH v2 3/5] doc/api: " Andre Muezerie
2025-01-03 20:39 ` [PATCH v2 4/5] drivers/net: use " Andre Muezerie
2025-01-03 20:39 ` Andre Muezerie [this message]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=1735936781-24199-6-git-send-email-andremue@linux.microsoft.com \
--to=andremue@linux.microsoft.com \
--cc=dev@dpdk.org \
--cc=mb@smartsharesystems.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).