All of lore.kernel.org
 help / color / mirror / Atom feed
From: Wei Gao via ltp <ltp@lists.linux.it>
To: ltp@lists.linux.it
Subject: [LTP] [PATCH v9 1/2] lib: Extend tst_assert_ulong() with enum flags
Date: Mon, 31 Aug 2026 08:16:33 +0000	[thread overview]
Message-ID: <20260831081638.19539-2-wegao@suse.com> (raw)
In-Reply-To: <20260831081638.19539-1-wegao@suse.com>

Introduce enum tst_assert_flags (including TST_ASSERT_NONE,
TST_ASSERT_SATURATED_INT, and TST_ASSERT_TRUNC_32BIT) to
tst_assert_ulong(). This enables type-safe handling of 32-bit
compat-mode truncation and clamping behaviors on 64-bit kernels
when verifying sysfs/procfs limits.

Signed-off-by: Wei Gao <wegao@suse.com>
---
 include/tst_assert.h | 36 +++++++++++++++++++++++++++++++-----
 lib/tst_assert.c     | 26 ++++++++++++++++++++------
 2 files changed, 51 insertions(+), 11 deletions(-)

diff --git a/include/tst_assert.h b/include/tst_assert.h
index dcb62dfea..9154b7249 100644
--- a/include/tst_assert.h
+++ b/include/tst_assert.h
@@ -21,14 +21,40 @@ void tst_assert_int(const char *file, const int lineno,
 #define TST_ASSERT_FILE_INT(path, prefix, val) \
 	tst_assert_file_int(__FILE__, __LINE__, path, prefix, val)
 
-/*
- * Same as tst_assert_int() but for unsigned long.
+/**
+ * enum tst_assert_flags - Flags for tst_assert_ulong().
+ *
+ * @TST_ASSERT_NONE: No flags, default direct comparison.
+ * @TST_ASSERT_SATURATED_INT: Clamps the value at %INT_MAX if it exceeds it.
+ * @TST_ASSERT_TRUNC_32BIT: Keeps only the low 32 bits of the read value,
+ *                          truncating any higher bits.
+ */
+enum tst_assert_flags {
+	TST_ASSERT_NONE          = 0x00,
+	TST_ASSERT_SATURATED_INT = 0x01,
+	TST_ASSERT_TRUNC_32BIT   = 0x02,
+};
+
+/**
+ * tst_assert_ulong() - Assert that an unsigned long value in a file matches.
+ * @file: The source file of the assertion (usually __FILE__).
+ * @lineno: The source line number of the assertion (usually __LINE__).
+ * @path: Path to the sysfs or procfs file to read from.
+ * @val: The expected unsigned long value to compare against.
+ * @flags: Bitwise flags controlling how the read value is processed.
+ *         See &enum tst_assert_flags.
+ *
+ * This function reads an integer value from the file specified by @path
+ * and compares it with @val. It allows handling of 32-bit compat mode
+ * truncation/clamping on 64-bit systems via @flags.
  */
 void tst_assert_ulong(const char *file, const int lineno,
-                      const char *path, unsigned long val);
+                      const char *path, unsigned long val,
+                      enum tst_assert_flags flags);
 
-#define TST_ASSERT_ULONG(path, val) \
-	tst_assert_ulong(__FILE__, __LINE__, path, val)
+#define TST_ASSERT_ULONG(path, val, ...) \
+	tst_assert_ulong(__FILE__, __LINE__, path, val, \
+		TST_2_(dummy, ##__VA_ARGS__, TST_ASSERT_NONE))
 
 /*
  * Asserts that integer value stored in the prefix field of file pointed by path
diff --git a/lib/tst_assert.c b/lib/tst_assert.c
index b68bd5d39..04241d31b 100644
--- a/lib/tst_assert.c
+++ b/lib/tst_assert.c
@@ -23,18 +23,32 @@ void tst_assert_int(const char *file, const int lineno, const char *path, int va
 	tst_res_(file, lineno, TFAIL, "%s != %d got %d", path, val, sys_val);
 }
 
-void tst_assert_ulong(const char *file, const int lineno, const char *path, unsigned long val)
+void tst_assert_ulong(const char *file, const int lineno, const char *path,
+		      unsigned long val, enum tst_assert_flags flags)
 {
-	unsigned long sys_val;
-
-	safe_file_scanf(file, lineno, NULL, path, "%lu", &sys_val);
+	unsigned long long sys_val_64;
+	unsigned long expected_val;
+
+	safe_file_scanf(file, lineno, NULL, path, "%llu", &sys_val_64);
+
+	if (flags & TST_ASSERT_SATURATED_INT) {
+		if (sys_val_64 > (unsigned long long)INT_MAX)
+			expected_val = (unsigned long)INT_MAX;
+		else
+			expected_val = (unsigned long)sys_val_64;
+	} else if (flags & TST_ASSERT_TRUNC_32BIT) {
+		expected_val = (unsigned long)(sys_val_64 & 0xFFFFFFFFULL);
+	} else {
+		expected_val = (unsigned long)sys_val_64;
+	}
 
-	if (val == sys_val) {
+	if (val == expected_val) {
 		tst_res_(file, lineno, TPASS, "%s = %lu", path, val);
 		return;
 	}
 
-	tst_res_(file, lineno, TFAIL, "%s != %lu got %lu", path, val, sys_val);
+	tst_res_(file, lineno, TFAIL, "%s != %lu got %lu (raw: %llu)",
+		path, val, expected_val, sys_val_64);
 }
 
 void tst_assert_file_int(const char *file, const int lineno, const char *path, const char *prefix, int val)
-- 
2.55.0


-- 
Mailing list info: https://lists.linux.it/listinfo/ltp

  reply	other threads:[~2026-08-31  8:17 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-08-31  8:16 [LTP] [PATCH v9 0/2] shmctl03: Fix 32-bit compat mode failure Wei Gao via ltp
2026-08-31  8:16 ` Wei Gao via ltp [this message]
2026-08-31  9:00   ` [LTP] lib: Extend tst_assert_ulong() with enum flags linuxtestproject.agent
2026-09-02  9:04   ` [LTP] [PATCH v9 1/2] " Petr Vorel
2026-08-31  8:16 ` [LTP] [PATCH v9 2/2] shmctl03: Fix 32-bit compat mode failure Wei Gao via ltp
2026-09-02  9:06   ` Petr Vorel
2026-09-02  9:18   ` Petr Vorel

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=20260831081638.19539-2-wegao@suse.com \
    --to=ltp@lists.linux.it \
    --cc=wegao@suse.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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.