Bläddra i källkod

[test] Add self-tests for snprintf()

Signed-off-by: Michael Brown <mcb30@ipxe.org>
tags/v1.20.1
Michael Brown 10 år sedan
förälder
incheckning
d105627928
2 ändrade filer med 103 tillägg och 0 borttagningar
  1. 1
    0
      src/tests/tests.c
  2. 102
    0
      src/tests/vsprintf_test.c

+ 1
- 0
src/tests/tests.c Visa fil

@@ -28,6 +28,7 @@ FILE_LICENCE ( GPL2_OR_LATER );
28 28
 /* Drag in all applicable self-tests */
29 29
 REQUIRE_OBJECT ( memcpy_test );
30 30
 REQUIRE_OBJECT ( string_test );
31
+REQUIRE_OBJECT ( vsprintf_test );
31 32
 REQUIRE_OBJECT ( list_test );
32 33
 REQUIRE_OBJECT ( byteswap_test );
33 34
 REQUIRE_OBJECT ( base64_test );

+ 102
- 0
src/tests/vsprintf_test.c Visa fil

@@ -0,0 +1,102 @@
1
+/*
2
+ * Copyright (C) 2012 Michael Brown <mbrown@fensystems.co.uk>.
3
+ *
4
+ * This program is free software; you can redistribute it and/or
5
+ * modify it under the terms of the GNU General Public License as
6
+ * published by the Free Software Foundation; either version 2 of the
7
+ * License, or any later version.
8
+ *
9
+ * This program is distributed in the hope that it will be useful, but
10
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
11
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12
+ * General Public License for more details.
13
+ *
14
+ * You should have received a copy of the GNU General Public License
15
+ * along with this program; if not, write to the Free Software
16
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17
+ * 02110-1301, USA.
18
+ */
19
+
20
+FILE_LICENCE ( GPL2_OR_LATER );
21
+
22
+/** @file
23
+ *
24
+ * vsprintf() self-tests
25
+ *
26
+ */
27
+
28
+/* Forcibly enable assertions */
29
+#undef NDEBUG
30
+
31
+#include <string.h>
32
+#include <stdio.h>
33
+#include <ipxe/test.h>
34
+
35
+/**
36
+ * Report an snprintf() test result
37
+ *
38
+ */
39
+#define snprintf_ok( len, result, format, ... ) do {			\
40
+	char actual[ (len) ];						\
41
+	const char expected[] = result;					\
42
+	size_t actual_len;						\
43
+									\
44
+	actual_len = snprintf ( actual, sizeof ( actual ),		\
45
+				format, ##__VA_ARGS__ );		\
46
+	ok ( actual_len >= strlen ( result ) );				\
47
+	ok ( strcmp ( actual, expected ) == 0 );			\
48
+	if ( strcmp ( actual, expected ) != 0 ) {			\
49
+		DBG ( "SNPRINTF expected \"%s\", got \"%s\"\n",		\
50
+		      expected, actual );				\
51
+	}								\
52
+	} while ( 0 )
53
+
54
+/**
55
+ * Perform vsprintf() self-tests
56
+ *
57
+ */
58
+static void vsprintf_test_exec ( void ) {
59
+
60
+	/* Constant string */
61
+	snprintf_ok ( 16, "Testing", "Testing" );
62
+
63
+	/* Constant string, truncated to fit */
64
+	snprintf_ok ( 5, "Test", "Testing" );
65
+
66
+	/* Basic format specifiers */
67
+	snprintf_ok ( 16, "%", "%%" );
68
+	snprintf_ok ( 16, "ABC", "%c%c%c", 'A', 'B', 'C' );
69
+	snprintf_ok ( 16, "abc", "%lc%lc%lc", L'a', L'b', L'c' );
70
+	snprintf_ok ( 16, "Hello world", "%s %s", "Hello", "world" );
71
+	snprintf_ok ( 16, "Goodbye world", "%ls %s", L"Goodbye", "world" );
72
+	snprintf_ok ( 16, "0x1234abcd", "%p", ( ( void * ) 0x1234abcd ) );
73
+	snprintf_ok ( 16, "0xa723", "%#x", 0xa723 );
74
+	snprintf_ok ( 16, "a723", "%x", 0xa723 );
75
+	snprintf_ok ( 16, "0x0000a723", "%#08x", 0xa723 );
76
+	snprintf_ok ( 16, "00A723", "%06X", 0xa723 );
77
+	snprintf_ok ( 16, "9876abcd", "%lx", 0x9876abcdUL );
78
+	snprintf_ok ( 16, "1234 5678", "%04llx %04llx", 0x1234ULL, 0x5678ULL );
79
+	snprintf_ok ( 16, "123", "%d", 123 );
80
+	snprintf_ok ( 16, "456", "%i", 456 );
81
+	snprintf_ok ( 16, " 99", "%3d", 99 );
82
+	snprintf_ok ( 16, "099", "%03d", 99 );
83
+	snprintf_ok ( 16, "-72", "%d", -72 );
84
+	snprintf_ok ( 16, " -72", "%4d", -72 );
85
+	snprintf_ok ( 16, "-072", "%04d", -72 );
86
+	snprintf_ok ( 16, "4", "%zd", sizeof ( uint32_t ) );
87
+	snprintf_ok ( 16, "123456789", "%d", 123456789 );
88
+
89
+	/* Realistic combinations */
90
+	snprintf_ok ( 64, "DBG 0x1234 thingy at 0x0003f0c0+0x5c\n",
91
+		      "DBG %p %s at %#08lx+%#zx\n", ( ( void * ) 0x1234 ),
92
+		      "thingy", 0x3f0c0UL, ( ( size_t ) 0x5c ) );
93
+	snprintf_ok ( 64, "PCI 00:1f.3", "PCI %02x:%02x.%x", 0x00, 0x1f, 0x03 );
94
+	snprintf_ok ( 64, "Region [1000000,3f000000)", "Region [%llx,%llx)",
95
+		      0x1000000ULL, 0x3f000000ULL );
96
+}
97
+
98
+/** vsprintf() self-test */
99
+struct self_test vsprintf_test __self_test = {
100
+	.name = "vsprintf",
101
+	.exec = vsprintf_test_exec,
102
+};

Laddar…
Avbryt
Spara