printf_unit_test.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. /*
  2. * GRUB -- GRand Unified Bootloader
  3. * Copyright (C) 2011 Free Software Foundation, Inc.
  4. *
  5. * GRUB is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * GRUB is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with GRUB. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #include <stdio.h>
  19. #include <string.h>
  20. #include <grub/test.h>
  21. #include <grub/misc.h>
  22. #define MSG "printf test failed: %s, %s", real, expected
  23. static void
  24. printf_test (void)
  25. {
  26. char real[512];
  27. char expected[512];
  28. char *null = NULL;
  29. grub_snprintf (real, sizeof (real), "%s", null);
  30. snprintf (expected, sizeof (expected), "%s", null);
  31. grub_test_assert (strcmp (real, expected) == 0, MSG);
  32. grub_snprintf (real, sizeof (real), "%10s", null);
  33. snprintf (expected, sizeof (expected), "%10s", null);
  34. grub_test_assert (strcmp (real, expected) == 0, MSG);
  35. grub_snprintf (real, sizeof (real), "%-10s", null);
  36. snprintf (expected, sizeof (expected), "%-10s", null);
  37. grub_test_assert (strcmp (real, expected) == 0, MSG);
  38. grub_snprintf (real, sizeof (real), "%d%%", 10);
  39. snprintf (expected, sizeof (expected), "%d%%", 10);
  40. grub_test_assert (strcmp (real, expected) == 0, MSG);
  41. grub_snprintf (real, sizeof (real), "%d %%", 10);
  42. snprintf (expected, sizeof (expected), "%d %%", 10);
  43. grub_test_assert (strcmp (real, expected) == 0, MSG);
  44. grub_snprintf (real, sizeof (real), "%%");
  45. snprintf (expected, sizeof (expected), "%%");
  46. grub_test_assert (strcmp (real, expected) == 0, MSG);
  47. grub_snprintf (real, sizeof (real), "%d %d %d", 1, 2, 3);
  48. snprintf (expected, sizeof (expected), "%d %d %d", 1, 2, 3);
  49. grub_test_assert (strcmp (real, expected) == 0, MSG);
  50. grub_snprintf (real, sizeof (real), "%3$d %2$d %1$d", 1, 2, 3);
  51. snprintf (expected, sizeof (expected), "%3$d %2$d %1$d", 1, 2, 3);
  52. grub_test_assert (strcmp (real, expected) == 0, MSG);
  53. grub_snprintf (real, sizeof (real), "%d %lld %d", 1, 2LL, 3);
  54. snprintf (expected, sizeof (expected), "%d %lld %d", 1, 2LL, 3);
  55. grub_test_assert (strcmp (real, expected) == 0, MSG);
  56. grub_snprintf (real, sizeof (real), "%3$d %2$lld %1$d", 1, 2LL, 3);
  57. snprintf (expected, sizeof (expected), "%3$d %2$lld %1$d", 1, 2LL, 3);
  58. grub_test_assert (strcmp (real, expected) == 0, MSG);
  59. grub_snprintf (real, sizeof (real), "%%0%dd ", 1);
  60. snprintf (expected, sizeof (expected), "%%0%dd ", 1);
  61. grub_test_assert (strcmp (real, expected) == 0, MSG);
  62. }
  63. GRUB_UNIT_TEST ("printf_unit_test", printf_test);