iPXE
vsprintf_test.c File Reference

vsprintf() self-tests More...

#include <string.h>
#include <stdio.h>
#include <ipxe/test.h>

Go to the source code of this file.

Macros

#define snprintf_ok(len, result, format, ...)

Functions

 FILE_LICENCE (GPL2_OR_LATER_OR_UBDL)
static void snprintf_okx (size_t len, const char *expected, const char *file, unsigned int line, const char *fmt,...)
 Report an snprintf() test result.
static void vsprintf_test_exec (void)
 Perform vsprintf() self-tests.

Variables

struct self_test vsprintf_test __self_test
 vsprintf() self-test

Detailed Description

vsprintf() self-tests

Definition in file vsprintf_test.c.

Macro Definition Documentation

◆ snprintf_ok

#define snprintf_ok ( len,
result,
format,
... )
Value:
snprintf_okx ( len, result, __FILE__, __LINE__, format, \
##__VA_ARGS__ )
uint16_t result
Definition hyperv.h:33
ring len
Length.
Definition dwmac.h:226
static void snprintf_okx(size_t len, const char *expected, const char *file, unsigned int line, const char *fmt,...)
Report an snprintf() test result.
int const char * format
Definition xfer.h:105

Definition at line 65 of file vsprintf_test.c.

65#define snprintf_ok( len, result, format, ... ) \
66 snprintf_okx ( len, result, __FILE__, __LINE__, format, \
67 ##__VA_ARGS__ )

Referenced by vsprintf_test_exec().

Function Documentation

◆ FILE_LICENCE()

FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL )

◆ snprintf_okx()

void snprintf_okx ( size_t len,
const char * expected,
const char * file,
unsigned int line,
const char * fmt,
... )
static

Report an snprintf() test result.

Parameters
lenBuffer length
expectedExpected result
fileTest code file
lineTest code line
formatFormat string
...Arguments

Definition at line 49 of file vsprintf_test.c.

50 {
51 char actual[len];
52 size_t actual_len;
53 va_list args;
54
55 va_start ( args, fmt );
56 actual_len = vsnprintf ( actual, sizeof ( actual ), fmt, args );
57 va_end ( args );
58 okx ( actual_len >= strlen ( expected ), file, line );
59 okx ( strcmp ( actual, expected ) == 0, file, line );
60 if ( strcmp ( actual, expected ) != 0 ) {
61 DBG ( "SNPRINTF expected \"%s\", got \"%s\"\n",
62 expected, actual );
63 }
64}
#define DBG(...)
Print a debugging message.
Definition compiler.h:498
#define va_end(ap)
Definition stdarg.h:10
#define va_start(ap, last)
Definition stdarg.h:8
__builtin_va_list va_list
Definition stdarg.h:7
int strcmp(const char *first, const char *second)
Compare strings.
Definition string.c:174
size_t strlen(const char *src)
Get length of string.
Definition string.c:244
#define okx(success, file, line)
Report test result.
Definition test.h:44
int vsnprintf(char *buf, size_t size, const char *fmt, va_list args)
Write a formatted string to a buffer.
Definition vsprintf.c:352
int ssize_t const char * fmt
Definition vsprintf.h:73

References DBG, fmt, len, okx, strcmp(), strlen(), va_end, va_start, and vsnprintf().

◆ vsprintf_test_exec()

void vsprintf_test_exec ( void )
static

Perform vsprintf() self-tests.

Definition at line 73 of file vsprintf_test.c.

73 {
74
75 /* Constant string */
76 snprintf_ok ( 16, "Testing", "Testing" );
77
78 /* Constant string, truncated to fit */
79 snprintf_ok ( 5, "Test", "Testing" );
80
81 /* Basic format specifiers */
82 snprintf_ok ( 16, "%", "%%" );
83 snprintf_ok ( 16, "ABC", "%c%c%c", 'A', 'B', 'C' );
84 snprintf_ok ( 16, "abc", "%lc%lc%lc", L'a', L'b', L'c' );
85 snprintf_ok ( 16, "Hello world", "%s %s", "Hello", "world" );
86 snprintf_ok ( 16, "Goodbye world", "%ls %s", L"Goodbye", "world" );
87 snprintf_ok ( 16, "0x1234abcd", "%p", ( ( void * ) 0x1234abcd ) );
88 snprintf_ok ( 16, "0xa723", "%#x", 0xa723 );
89 snprintf_ok ( 16, "a723", "%x", 0xa723 );
90 snprintf_ok ( 16, "0x0000a723", "%#08x", 0xa723 );
91 snprintf_ok ( 16, "00A723", "%06X", 0xa723 );
92 snprintf_ok ( 16, "9876abcd", "%lx", 0x9876abcdUL );
93 snprintf_ok ( 16, "1234 5678", "%04llx %04llx", 0x1234ULL, 0x5678ULL );
94 snprintf_ok ( 16, "123", "%d", 123 );
95 snprintf_ok ( 16, "456", "%i", 456 );
96 snprintf_ok ( 16, " 99", "%3d", 99 );
97 snprintf_ok ( 16, "099", "%03d", 99 );
98 snprintf_ok ( 16, "-72", "%d", -72 );
99 snprintf_ok ( 16, " -72", "%4d", -72 );
100 snprintf_ok ( 16, "-072", "%04d", -72 );
101 snprintf_ok ( 16, "4", "%zd", sizeof ( uint32_t ) );
102 snprintf_ok ( 16, "123456789", "%d", 123456789 );
103
104 /* Realistic combinations */
105 snprintf_ok ( 64, "DBG 0x1234 thingy at 0x0003f0c0+0x5c\n",
106 "DBG %p %s at %#08lx+%#zx\n", ( ( void * ) 0x1234 ),
107 "thingy", 0x3f0c0UL, ( ( size_t ) 0x5c ) );
108 snprintf_ok ( 64, "PCI 00:1f.3", "PCI %02x:%02x.%x", 0x00, 0x1f, 0x03 );
109 snprintf_ok ( 64, "Region [1000000,3f000000)", "Region [%llx,%llx)",
110 0x1000000ULL, 0x3f000000ULL );
111
112 /* Null string (used for debug messages) */
113 snprintf_ok ( 16, "<NULL>", "%s", ( ( char * ) NULL ) );
114 snprintf_ok ( 16, "<NULL>", "%ls", ( ( wchar_t * ) NULL ) );
115}
#define NULL
NULL pointer (VOID *)
Definition Base.h:322
unsigned int uint32_t
Definition stdint.h:12
#define snprintf_ok(len, result, format,...)

References NULL, and snprintf_ok.

Variable Documentation

◆ __self_test

struct self_test vsprintf_test __self_test
Initial value:
= {
.name = "vsprintf",
}
static void vsprintf_test_exec(void)
Perform vsprintf() self-tests.

vsprintf() self-test

Definition at line 118 of file vsprintf_test.c.

118 {
119 .name = "vsprintf",
120 .exec = vsprintf_test_exec,
121};