iPXE
vsprintf_test.c
Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2012 Michael Brown <mbrown@fensystems.co.uk>.
00003  *
00004  * This program is free software; you can redistribute it and/or
00005  * modify it under the terms of the GNU General Public License as
00006  * published by the Free Software Foundation; either version 2 of the
00007  * License, or any later version.
00008  *
00009  * This program is distributed in the hope that it will be useful, but
00010  * WITHOUT ANY WARRANTY; without even the implied warranty of
00011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012  * General Public License for more details.
00013  *
00014  * You should have received a copy of the GNU General Public License
00015  * along with this program; if not, write to the Free Software
00016  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
00017  * 02110-1301, USA.
00018  *
00019  * You can also choose to distribute this program under the terms of
00020  * the Unmodified Binary Distribution Licence (as given in the file
00021  * COPYING.UBDL), provided that you have satisfied its requirements.
00022  */
00023 
00024 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
00025 
00026 /** @file
00027  *
00028  * vsprintf() self-tests
00029  *
00030  */
00031 
00032 /* Forcibly enable assertions */
00033 #undef NDEBUG
00034 
00035 #include <string.h>
00036 #include <stdio.h>
00037 #include <ipxe/test.h>
00038 
00039 /**
00040  * Report an snprintf() test result
00041  *
00042  * @v len               Buffer length
00043  * @v expected          Expected result
00044  * @v file              Test code file
00045  * @v line              Test code line
00046  * @v format            Format string
00047  * @v ...               Arguments
00048  */
00049 static void snprintf_okx ( size_t len, const char *expected, const char *file,
00050                            unsigned int line, const char *fmt, ... ) {
00051         char actual[len];
00052         size_t actual_len;
00053         va_list args;
00054 
00055         va_start ( args, fmt );
00056         actual_len = vsnprintf ( actual, sizeof ( actual ), fmt, args );
00057         va_end ( args );
00058         okx ( actual_len >= strlen ( expected ), file, line );
00059         okx ( strcmp ( actual, expected ) == 0, file, line );
00060         if ( strcmp ( actual, expected ) != 0 ) {
00061                 DBG ( "SNPRINTF expected \"%s\", got \"%s\"\n",
00062                       expected, actual );
00063         }
00064 }
00065 #define snprintf_ok( len, result, format, ... )                         \
00066         snprintf_okx ( len, result, __FILE__, __LINE__, format,         \
00067                        ##__VA_ARGS__ )
00068 
00069 /**
00070  * Perform vsprintf() self-tests
00071  *
00072  */
00073 static void vsprintf_test_exec ( void ) {
00074 
00075         /* Constant string */
00076         snprintf_ok ( 16, "Testing", "Testing" );
00077 
00078         /* Constant string, truncated to fit */
00079         snprintf_ok ( 5, "Test", "Testing" );
00080 
00081         /* Basic format specifiers */
00082         snprintf_ok ( 16, "%", "%%" );
00083         snprintf_ok ( 16, "ABC", "%c%c%c", 'A', 'B', 'C' );
00084         snprintf_ok ( 16, "abc", "%lc%lc%lc", L'a', L'b', L'c' );
00085         snprintf_ok ( 16, "Hello world", "%s %s", "Hello", "world" );
00086         snprintf_ok ( 16, "Goodbye world", "%ls %s", L"Goodbye", "world" );
00087         snprintf_ok ( 16, "0x1234abcd", "%p", ( ( void * ) 0x1234abcd ) );
00088         snprintf_ok ( 16, "0xa723", "%#x", 0xa723 );
00089         snprintf_ok ( 16, "a723", "%x", 0xa723 );
00090         snprintf_ok ( 16, "0x0000a723", "%#08x", 0xa723 );
00091         snprintf_ok ( 16, "00A723", "%06X", 0xa723 );
00092         snprintf_ok ( 16, "9876abcd", "%lx", 0x9876abcdUL );
00093         snprintf_ok ( 16, "1234 5678", "%04llx %04llx", 0x1234ULL, 0x5678ULL );
00094         snprintf_ok ( 16, "123", "%d", 123 );
00095         snprintf_ok ( 16, "456", "%i", 456 );
00096         snprintf_ok ( 16, " 99", "%3d", 99 );
00097         snprintf_ok ( 16, "099", "%03d", 99 );
00098         snprintf_ok ( 16, "-72", "%d", -72 );
00099         snprintf_ok ( 16, " -72", "%4d", -72 );
00100         snprintf_ok ( 16, "-072", "%04d", -72 );
00101         snprintf_ok ( 16, "4", "%zd", sizeof ( uint32_t ) );
00102         snprintf_ok ( 16, "123456789", "%d", 123456789 );
00103 
00104         /* Realistic combinations */
00105         snprintf_ok ( 64, "DBG 0x1234 thingy at 0x0003f0c0+0x5c\n",
00106                       "DBG %p %s at %#08lx+%#zx\n", ( ( void * ) 0x1234 ),
00107                       "thingy", 0x3f0c0UL, ( ( size_t ) 0x5c ) );
00108         snprintf_ok ( 64, "PCI 00:1f.3", "PCI %02x:%02x.%x", 0x00, 0x1f, 0x03 );
00109         snprintf_ok ( 64, "Region [1000000,3f000000)", "Region [%llx,%llx)",
00110                       0x1000000ULL, 0x3f000000ULL );
00111 
00112         /* Null string (used for debug messages) */
00113         snprintf_ok ( 16, "<NULL>", "%s", ( ( char * ) NULL ) );
00114         snprintf_ok ( 16, "<NULL>", "%ls", ( ( wchar_t * ) NULL ) );
00115 }
00116 
00117 /** vsprintf() self-test */
00118 struct self_test vsprintf_test __self_test = {
00119         .name = "vsprintf",
00120         .exec = vsprintf_test_exec,
00121 };