Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

vsprintf_test.c 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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. FILE_LICENCE ( GPL2_OR_LATER );
  20. /** @file
  21. *
  22. * vsprintf() self-tests
  23. *
  24. */
  25. /* Forcibly enable assertions */
  26. #undef NDEBUG
  27. #include <string.h>
  28. #include <stdio.h>
  29. #include <ipxe/test.h>
  30. /**
  31. * Report an snprintf() test result
  32. *
  33. */
  34. #define snprintf_ok( len, result, format, ... ) do { \
  35. char actual[ (len) ]; \
  36. const char expected[] = result; \
  37. size_t actual_len; \
  38. \
  39. actual_len = snprintf ( actual, sizeof ( actual ), \
  40. format, ##__VA_ARGS__ ); \
  41. ok ( actual_len >= strlen ( result ) ); \
  42. ok ( strcmp ( actual, expected ) == 0 ); \
  43. if ( strcmp ( actual, expected ) != 0 ) { \
  44. DBG ( "SNPRINTF expected \"%s\", got \"%s\"\n", \
  45. expected, actual ); \
  46. } \
  47. } while ( 0 )
  48. /**
  49. * Perform vsprintf() self-tests
  50. *
  51. */
  52. static void vsprintf_test_exec ( void ) {
  53. /* Constant string */
  54. snprintf_ok ( 16, "Testing", "Testing" );
  55. /* Constant string, truncated to fit */
  56. snprintf_ok ( 5, "Test", "Testing" );
  57. /* Basic format specifiers */
  58. snprintf_ok ( 16, "%", "%%" );
  59. snprintf_ok ( 16, "ABC", "%c%c%c", 'A', 'B', 'C' );
  60. snprintf_ok ( 16, "abc", "%lc%lc%lc", L'a', L'b', L'c' );
  61. snprintf_ok ( 16, "Hello world", "%s %s", "Hello", "world" );
  62. snprintf_ok ( 16, "Goodbye world", "%ls %s", L"Goodbye", "world" );
  63. snprintf_ok ( 16, "0x1234abcd", "%p", ( ( void * ) 0x1234abcd ) );
  64. snprintf_ok ( 16, "0xa723", "%#x", 0xa723 );
  65. snprintf_ok ( 16, "a723", "%x", 0xa723 );
  66. snprintf_ok ( 16, "0x0000a723", "%#08x", 0xa723 );
  67. snprintf_ok ( 16, "00A723", "%06X", 0xa723 );
  68. snprintf_ok ( 16, "9876abcd", "%lx", 0x9876abcdUL );
  69. snprintf_ok ( 16, "1234 5678", "%04llx %04llx", 0x1234ULL, 0x5678ULL );
  70. snprintf_ok ( 16, "123", "%d", 123 );
  71. snprintf_ok ( 16, "456", "%i", 456 );
  72. snprintf_ok ( 16, " 99", "%3d", 99 );
  73. snprintf_ok ( 16, "099", "%03d", 99 );
  74. snprintf_ok ( 16, "-72", "%d", -72 );
  75. snprintf_ok ( 16, " -72", "%4d", -72 );
  76. snprintf_ok ( 16, "-072", "%04d", -72 );
  77. snprintf_ok ( 16, "4", "%zd", sizeof ( uint32_t ) );
  78. snprintf_ok ( 16, "123456789", "%d", 123456789 );
  79. /* Realistic combinations */
  80. snprintf_ok ( 64, "DBG 0x1234 thingy at 0x0003f0c0+0x5c\n",
  81. "DBG %p %s at %#08lx+%#zx\n", ( ( void * ) 0x1234 ),
  82. "thingy", 0x3f0c0UL, ( ( size_t ) 0x5c ) );
  83. snprintf_ok ( 64, "PCI 00:1f.3", "PCI %02x:%02x.%x", 0x00, 0x1f, 0x03 );
  84. snprintf_ok ( 64, "Region [1000000,3f000000)", "Region [%llx,%llx)",
  85. 0x1000000ULL, 0x3f000000ULL );
  86. }
  87. /** vsprintf() self-test */
  88. struct self_test vsprintf_test __self_test = {
  89. .name = "vsprintf",
  90. .exec = vsprintf_test_exec,
  91. };