summaryrefslogtreecommitdiffstats
path: root/src/tests
diff options
context:
space:
mode:
authorMichael Brown2013-08-12 17:34:10 +0200
committerMichael Brown2013-08-12 18:02:50 +0200
commitd105627928b90c2031894e1771098848cc74a7c0 (patch)
tree2fa4d2c4bc150b6acb5d1aae9cf1c9e861729507 /src/tests
parent[settings] Expose CPUID instruction via settings mechanism (diff)
downloadipxe-d105627928b90c2031894e1771098848cc74a7c0.tar.gz
ipxe-d105627928b90c2031894e1771098848cc74a7c0.tar.xz
ipxe-d105627928b90c2031894e1771098848cc74a7c0.zip
[test] Add self-tests for snprintf()
Signed-off-by: Michael Brown <mcb30@ipxe.org>
Diffstat (limited to 'src/tests')
-rw-r--r--src/tests/tests.c1
-rw-r--r--src/tests/vsprintf_test.c102
2 files changed, 103 insertions, 0 deletions
diff --git a/src/tests/tests.c b/src/tests/tests.c
index 7c99a527..f965e6e3 100644
--- a/src/tests/tests.c
+++ b/src/tests/tests.c
@@ -28,6 +28,7 @@ FILE_LICENCE ( GPL2_OR_LATER );
/* Drag in all applicable self-tests */
REQUIRE_OBJECT ( memcpy_test );
REQUIRE_OBJECT ( string_test );
+REQUIRE_OBJECT ( vsprintf_test );
REQUIRE_OBJECT ( list_test );
REQUIRE_OBJECT ( byteswap_test );
REQUIRE_OBJECT ( base64_test );
diff --git a/src/tests/vsprintf_test.c b/src/tests/vsprintf_test.c
new file mode 100644
index 00000000..11512ec8
--- /dev/null
+++ b/src/tests/vsprintf_test.c
@@ -0,0 +1,102 @@
+/*
+ * Copyright (C) 2012 Michael Brown <mbrown@fensystems.co.uk>.
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License as
+ * published by the Free Software Foundation; either version 2 of the
+ * License, or any later version.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+ * 02110-1301, USA.
+ */
+
+FILE_LICENCE ( GPL2_OR_LATER );
+
+/** @file
+ *
+ * vsprintf() self-tests
+ *
+ */
+
+/* Forcibly enable assertions */
+#undef NDEBUG
+
+#include <string.h>
+#include <stdio.h>
+#include <ipxe/test.h>
+
+/**
+ * Report an snprintf() test result
+ *
+ */
+#define snprintf_ok( len, result, format, ... ) do { \
+ char actual[ (len) ]; \
+ const char expected[] = result; \
+ size_t actual_len; \
+ \
+ actual_len = snprintf ( actual, sizeof ( actual ), \
+ format, ##__VA_ARGS__ ); \
+ ok ( actual_len >= strlen ( result ) ); \
+ ok ( strcmp ( actual, expected ) == 0 ); \
+ if ( strcmp ( actual, expected ) != 0 ) { \
+ DBG ( "SNPRINTF expected \"%s\", got \"%s\"\n", \
+ expected, actual ); \
+ } \
+ } while ( 0 )
+
+/**
+ * Perform vsprintf() self-tests
+ *
+ */
+static void vsprintf_test_exec ( void ) {
+
+ /* Constant string */
+ snprintf_ok ( 16, "Testing", "Testing" );
+
+ /* Constant string, truncated to fit */
+ snprintf_ok ( 5, "Test", "Testing" );
+
+ /* Basic format specifiers */
+ snprintf_ok ( 16, "%", "%%" );
+ snprintf_ok ( 16, "ABC", "%c%c%c", 'A', 'B', 'C' );
+ snprintf_ok ( 16, "abc", "%lc%lc%lc", L'a', L'b', L'c' );
+ snprintf_ok ( 16, "Hello world", "%s %s", "Hello", "world" );
+ snprintf_ok ( 16, "Goodbye world", "%ls %s", L"Goodbye", "world" );
+ snprintf_ok ( 16, "0x1234abcd", "%p", ( ( void * ) 0x1234abcd ) );
+ snprintf_ok ( 16, "0xa723", "%#x", 0xa723 );
+ snprintf_ok ( 16, "a723", "%x", 0xa723 );
+ snprintf_ok ( 16, "0x0000a723", "%#08x", 0xa723 );
+ snprintf_ok ( 16, "00A723", "%06X", 0xa723 );
+ snprintf_ok ( 16, "9876abcd", "%lx", 0x9876abcdUL );
+ snprintf_ok ( 16, "1234 5678", "%04llx %04llx", 0x1234ULL, 0x5678ULL );
+ snprintf_ok ( 16, "123", "%d", 123 );
+ snprintf_ok ( 16, "456", "%i", 456 );
+ snprintf_ok ( 16, " 99", "%3d", 99 );
+ snprintf_ok ( 16, "099", "%03d", 99 );
+ snprintf_ok ( 16, "-72", "%d", -72 );
+ snprintf_ok ( 16, " -72", "%4d", -72 );
+ snprintf_ok ( 16, "-072", "%04d", -72 );
+ snprintf_ok ( 16, "4", "%zd", sizeof ( uint32_t ) );
+ snprintf_ok ( 16, "123456789", "%d", 123456789 );
+
+ /* Realistic combinations */
+ snprintf_ok ( 64, "DBG 0x1234 thingy at 0x0003f0c0+0x5c\n",
+ "DBG %p %s at %#08lx+%#zx\n", ( ( void * ) 0x1234 ),
+ "thingy", 0x3f0c0UL, ( ( size_t ) 0x5c ) );
+ snprintf_ok ( 64, "PCI 00:1f.3", "PCI %02x:%02x.%x", 0x00, 0x1f, 0x03 );
+ snprintf_ok ( 64, "Region [1000000,3f000000)", "Region [%llx,%llx)",
+ 0x1000000ULL, 0x3f000000ULL );
+}
+
+/** vsprintf() self-test */
+struct self_test vsprintf_test __self_test = {
+ .name = "vsprintf",
+ .exec = vsprintf_test_exec,
+};