summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorThomas Haller <thaller@redhat.com>2016-10-08 12:45:58 +0200
committerThomas Haller <thaller@redhat.com>2016-10-08 13:24:09 +0200
commit55129e03ddb424ea8b4511e716961704ee727e70 (patch)
treeb7256ede67322688b3df42dcad7e73ffdb8e993a
parentb39869750cf5ed60da5b983cd8801520d29089db (diff)
downloadNetworkManager-55129e03ddb424ea8b4511e716961704ee727e70.tar.gz
shared: add nm_sprintf_bufa_int() utils
-rw-r--r--libnm-core/tests/test-general.c60
-rw-r--r--shared/nm-utils/nm-macros-internal.h43
2 files changed, 103 insertions, 0 deletions
diff --git a/libnm-core/tests/test-general.c b/libnm-core/tests/test-general.c
index 414c6659b3..f93328a62b 100644
--- a/libnm-core/tests/test-general.c
+++ b/libnm-core/tests/test-general.c
@@ -5430,6 +5430,65 @@ test_nm_set_out (void)
/*****************************************************************************/
+static void
+test_nm_sprintf_bufa_int (void)
+{
+
+#define do_test_nm_sprintf_bufa_int_full(type, n, ns) \
+ G_STMT_START { \
+ type _n = (n); \
+ const char *_s; \
+ \
+ _s = nm_sprintf_bufa_int (_n); \
+ g_assert_cmpstr (_s, ==, ns); \
+ } G_STMT_END
+
+#define do_test_nm_sprintf_bufa_int(type, n) do_test_nm_sprintf_bufa_int_full (type, n, G_STRINGIFY (n))
+
+ do_test_nm_sprintf_bufa_int (guint32, 5);
+
+ do_test_nm_sprintf_bufa_int (signed char, 0);
+ do_test_nm_sprintf_bufa_int (signed char, -5);
+ do_test_nm_sprintf_bufa_int (signed char, 5);
+ do_test_nm_sprintf_bufa_int (signed char, 127);
+ do_test_nm_sprintf_bufa_int (signed char, -128);
+
+ do_test_nm_sprintf_bufa_int (gint32, 0);
+ do_test_nm_sprintf_bufa_int (gint32, -5);
+ do_test_nm_sprintf_bufa_int (gint32, 5);
+ do_test_nm_sprintf_bufa_int (gint32, 2147483647);
+ do_test_nm_sprintf_bufa_int (gint32, -2147483648);
+
+ do_test_nm_sprintf_bufa_int (long, 0);
+ do_test_nm_sprintf_bufa_int (long, -5);
+ do_test_nm_sprintf_bufa_int (long, 5);
+ do_test_nm_sprintf_bufa_int (long, 2147483647);
+ do_test_nm_sprintf_bufa_int (long, -2147483648);
+ do_test_nm_sprintf_bufa_int_full (long, G_MAXLONG, nm_sprintf_bufa (30, "%ld", G_MAXLONG));
+ do_test_nm_sprintf_bufa_int_full (long, G_MINLONG, nm_sprintf_bufa (30, "%ld", G_MINLONG));
+
+ do_test_nm_sprintf_bufa_int (gint64, 0);
+ do_test_nm_sprintf_bufa_int (gint64, -5);
+ do_test_nm_sprintf_bufa_int (gint64, 5);
+ do_test_nm_sprintf_bufa_int (gint64, 2147483647);
+ do_test_nm_sprintf_bufa_int (gint64, -2147483648);
+ do_test_nm_sprintf_bufa_int (gint64, 9223372036854775807);
+ do_test_nm_sprintf_bufa_int (gint64, -9223372036854775807);
+ do_test_nm_sprintf_bufa_int_full (gint64, -9223372036854775808u, "-9223372036854775808");
+ do_test_nm_sprintf_bufa_int_full (gint64, G_MAXINT64, nm_sprintf_bufa (30, "%"G_GINT64_FORMAT, G_MAXINT64));
+ do_test_nm_sprintf_bufa_int_full (gint64, G_MININT64, nm_sprintf_bufa (30, "%"G_GINT64_FORMAT, G_MININT64));
+
+ do_test_nm_sprintf_bufa_int (guint64, 0);
+ do_test_nm_sprintf_bufa_int (guint64, 5);
+ do_test_nm_sprintf_bufa_int (guint64, 2147483647);
+ do_test_nm_sprintf_bufa_int_full (guint64, 18446744073709551615llu, "18446744073709551615");
+ do_test_nm_sprintf_bufa_int_full (guint64, G_MAXUINT64, nm_sprintf_bufa (30, "%"G_GUINT64_FORMAT, G_MAXUINT64));
+
+ do_test_nm_sprintf_bufa_int (const guint64, 0);
+}
+
+/*****************************************************************************/
+
NMTST_DEFINE ();
int main (int argc, char **argv)
@@ -5553,6 +5612,7 @@ int main (int argc, char **argv)
g_test_add_func ("/core/general/_nm_utils_team_config_equal", test_nm_utils_team_config_equal);
g_test_add_func ("/core/general/test_nm_utils_enum", test_nm_utils_enum);
g_test_add_func ("/core/general/nm-set-out", test_nm_set_out);
+ g_test_add_func ("/core/general/nm-sprintf-bufa-int", test_nm_sprintf_bufa_int);
return g_test_run ();
}
diff --git a/shared/nm-utils/nm-macros-internal.h b/shared/nm-utils/nm-macros-internal.h
index 1451053580..c3910189f4 100644
--- a/shared/nm-utils/nm-macros-internal.h
+++ b/shared/nm-utils/nm-macros-internal.h
@@ -615,6 +615,22 @@ nm_decode_version (guint version, guint *major, guint *minor, guint *micro) {
}
/*****************************************************************************/
+/* taken from systemd's DECIMAL_STR_MAX()
+ *
+ * Returns the number of chars needed to format variables of the
+ * specified type as a decimal string. Adds in extra space for a
+ * negative '-' prefix (hence works correctly on signed
+ * types). Includes space for the trailing NUL. */
+#define NM_DECIMAL_STR_MAX(type) \
+ (2+(sizeof(type) <= 1 ? 3 : \
+ sizeof(type) <= 2 ? 5 : \
+ sizeof(type) <= 4 ? 10 : \
+ sizeof(type) <= 8 ? 20 : sizeof(int[-2*(sizeof(type) > 8)])))
+
+#define NM_IS_SIGNED(type) (((type) -1) < 0)
+
+/*****************************************************************************/
+
/* if @str is NULL, return "(null)". Otherwise, allocate a buffer using
* alloca() of size @bufsize and fill it with @str. @str will be quoted with
* single quote, and in case @str is too long, the final quote will be '^'. */
@@ -666,6 +682,33 @@ nm_decode_version (guint version, guint *major, guint *minor, guint *micro) {
_buf; \
})
+#define nm_sprintf_bufa_int(num) \
+ ({ \
+ char *_buf; \
+ typeof (num) _num = (num); \
+ const int _buf_len = NM_DECIMAL_STR_MAX (_num); \
+ \
+ _buf = g_alloca (_buf_len); \
+ if (sizeof (_num) <= sizeof (int)) { \
+ if (NM_IS_SIGNED (typeof (_num))) \
+ g_snprintf (_buf, _buf_len, "%d", (int) _num); \
+ else \
+ g_snprintf (_buf, _buf_len, "%u", (unsigned) _num); \
+ } else if (sizeof (_num) <= sizeof (long)) { \
+ if (NM_IS_SIGNED (typeof (_num))) \
+ g_snprintf (_buf, _buf_len, "%ld", (long int) _num); \
+ else \
+ g_snprintf (_buf, _buf_len, "%lu", (long unsigned) _num); \
+ } else { \
+ G_STATIC_ASSERT (sizeof (_num) <= sizeof (long long)); \
+ if (NM_IS_SIGNED (typeof (_num))) \
+ g_snprintf (_buf, _buf_len, "%lld", (long long int) _num); \
+ else \
+ g_snprintf (_buf, _buf_len, "%llu", (long long unsigned) _num); \
+ } \
+ (const char *) _buf; \
+ })
+
/*****************************************************************************/
/**