Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

vsprintf_test.c 4.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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. * You can also choose to distribute this program under the terms of
  20. * the Unmodified Binary Distribution Licence (as given in the file
  21. * COPYING.UBDL), provided that you have satisfied its requirements.
  22. */
  23. FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
  24. /** @file
  25. *
  26. * vsprintf() self-tests
  27. *
  28. */
  29. /* Forcibly enable assertions */
  30. #undef NDEBUG
  31. #include <string.h>
  32. #include <stdio.h>
  33. #include <ipxe/test.h>
  34. /**
  35. * Report an snprintf() test result
  36. *
  37. * @v len Buffer length
  38. * @v expected Expected result
  39. * @v file Test code file
  40. * @v line Test code line
  41. * @v format Format string
  42. * @v ... Arguments
  43. */
  44. static void snprintf_okx ( size_t len, const char *expected, const char *file,
  45. unsigned int line, const char *fmt, ... ) {
  46. char actual[len];
  47. size_t actual_len;
  48. va_list args;
  49. va_start ( args, fmt );
  50. actual_len = vsnprintf ( actual, sizeof ( actual ), fmt, args );
  51. va_end ( args );
  52. okx ( actual_len >= strlen ( expected ), file, line );
  53. okx ( strcmp ( actual, expected ) == 0, file, line );
  54. if ( strcmp ( actual, expected ) != 0 ) {
  55. DBG ( "SNPRINTF expected \"%s\", got \"%s\"\n",
  56. expected, actual );
  57. }
  58. }
  59. #define snprintf_ok( len, result, format, ... ) \
  60. snprintf_okx ( len, result, __FILE__, __LINE__, format, \
  61. ##__VA_ARGS__ )
  62. /**
  63. * Perform vsprintf() self-tests
  64. *
  65. */
  66. static void vsprintf_test_exec ( void ) {
  67. /* Constant string */
  68. snprintf_ok ( 16, "Testing", "Testing" );
  69. /* Constant string, truncated to fit */
  70. snprintf_ok ( 5, "Test", "Testing" );
  71. /* Basic format specifiers */
  72. snprintf_ok ( 16, "%", "%%" );
  73. snprintf_ok ( 16, "ABC", "%c%c%c", 'A', 'B', 'C' );
  74. snprintf_ok ( 16, "abc", "%lc%lc%lc", L'a', L'b', L'c' );
  75. snprintf_ok ( 16, "Hello world", "%s %s", "Hello", "world" );
  76. snprintf_ok ( 16, "Goodbye world", "%ls %s", L"Goodbye", "world" );
  77. snprintf_ok ( 16, "0x1234abcd", "%p", ( ( void * ) 0x1234abcd ) );
  78. snprintf_ok ( 16, "0xa723", "%#x", 0xa723 );
  79. snprintf_ok ( 16, "a723", "%x", 0xa723 );
  80. snprintf_ok ( 16, "0x0000a723", "%#08x", 0xa723 );
  81. snprintf_ok ( 16, "00A723", "%06X", 0xa723 );
  82. snprintf_ok ( 16, "9876abcd", "%lx", 0x9876abcdUL );
  83. snprintf_ok ( 16, "1234 5678", "%04llx %04llx", 0x1234ULL, 0x5678ULL );
  84. snprintf_ok ( 16, "123", "%d", 123 );
  85. snprintf_ok ( 16, "456", "%i", 456 );
  86. snprintf_ok ( 16, " 99", "%3d", 99 );
  87. snprintf_ok ( 16, "099", "%03d", 99 );
  88. snprintf_ok ( 16, "-72", "%d", -72 );
  89. snprintf_ok ( 16, " -72", "%4d", -72 );
  90. snprintf_ok ( 16, "-072", "%04d", -72 );
  91. snprintf_ok ( 16, "4", "%zd", sizeof ( uint32_t ) );
  92. snprintf_ok ( 16, "123456789", "%d", 123456789 );
  93. /* Realistic combinations */
  94. snprintf_ok ( 64, "DBG 0x1234 thingy at 0x0003f0c0+0x5c\n",
  95. "DBG %p %s at %#08lx+%#zx\n", ( ( void * ) 0x1234 ),
  96. "thingy", 0x3f0c0UL, ( ( size_t ) 0x5c ) );
  97. snprintf_ok ( 64, "PCI 00:1f.3", "PCI %02x:%02x.%x", 0x00, 0x1f, 0x03 );
  98. snprintf_ok ( 64, "Region [1000000,3f000000)", "Region [%llx,%llx)",
  99. 0x1000000ULL, 0x3f000000ULL );
  100. /* Null string (used for debug messages) */
  101. snprintf_ok ( 16, "<NULL>", "%s", ( ( char * ) NULL ) );
  102. snprintf_ok ( 16, "<NULL>", "%ls", ( ( wchar_t * ) NULL ) );
  103. }
  104. /** vsprintf() self-test */
  105. struct self_test vsprintf_test __self_test = {
  106. .name = "vsprintf",
  107. .exec = vsprintf_test_exec,
  108. };