summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorThomas Haller <thaller@redhat.com>2020-10-21 16:49:46 +0200
committerThomas Haller <thaller@redhat.com>2020-10-23 17:11:53 +0200
commitdd7ce063fb46e1a8e17e5faf70c7cda70924e1e9 (patch)
tree5e7e3aae10ae396c2f5eaaecb05a9a336d71f8c1
parentc947c51651ad3a5a1914f368c4b3ca948f63e122 (diff)
tests: add nmtst_get_rand_one_case_in() helper
-rw-r--r--shared/nm-utils/nm-test-utils.h19
1 files changed, 16 insertions, 3 deletions
diff --git a/shared/nm-utils/nm-test-utils.h b/shared/nm-utils/nm-test-utils.h
index 9d4848829..431397f0b 100644
--- a/shared/nm-utils/nm-test-utils.h
+++ b/shared/nm-utils/nm-test-utils.h
@@ -900,14 +900,16 @@ nmtst_get_rand_uint64(void)
static inline guint
nmtst_get_rand_uint(void)
{
- G_STATIC_ASSERT_EXPR(sizeof(guint32) == sizeof(guint));
- return nmtst_get_rand_uint32();
+ G_STATIC_ASSERT_EXPR((sizeof(guint) == sizeof(guint32) || (sizeof(guint) == sizeof(guint64))));
+ if (sizeof(guint32) == sizeof(guint))
+ return nmtst_get_rand_uint32();
+ return nmtst_get_rand_uint64();
}
static inline gsize
nmtst_get_rand_size(void)
{
- G_STATIC_ASSERT_EXPR(sizeof(gsize) == sizeof(guint32) || sizeof(gsize) == sizeof(guint64));
+ G_STATIC_ASSERT_EXPR((sizeof(gsize) == sizeof(guint32) || (sizeof(gsize) == sizeof(guint64))));
if (sizeof(gsize) == sizeof(guint32))
return nmtst_get_rand_uint32();
return nmtst_get_rand_uint64();
@@ -919,6 +921,17 @@ nmtst_get_rand_bool(void)
return nmtst_get_rand_uint32() % 2;
}
+static inline gboolean
+nmtst_get_rand_one_case_in(guint32 num)
+{
+ /* num=1 doesn't make much sense, because it will always return %TRUE.
+ * Still accept it, it might be that @num is calculated, so 1 might be
+ * a valid edge case. */
+ g_assert(num > 0);
+
+ return (nmtst_get_rand_uint32() % num) == 0;
+}
+
static inline gpointer
nmtst_rand_buf(GRand *rand, gpointer buffer, gsize buffer_length)
{