tests.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. /* $OpenBSD: tests.c,v 1.4 2017/02/19 00:11:29 djm Exp $ */
  2. /*
  3. * Regress test for the utf8.h *mprintf() API
  4. *
  5. * Written by Ingo Schwarze <schwarze@openbsd.org> in 2016
  6. * and placed in the public domain.
  7. */
  8. #include "includes.h"
  9. #include <locale.h>
  10. #include <stdarg.h>
  11. #include <string.h>
  12. #include <stdio.h>
  13. #include "../test_helper/test_helper.h"
  14. #include "utf8.h"
  15. static void
  16. badarg(void)
  17. {
  18. char buf[16];
  19. int len, width;
  20. width = 1;
  21. TEST_START("utf8_badarg");
  22. len = snmprintf(buf, sizeof(buf), &width, "\377");
  23. ASSERT_INT_EQ(len, -1);
  24. ASSERT_STRING_EQ(buf, "");
  25. ASSERT_INT_EQ(width, 0);
  26. TEST_DONE();
  27. }
  28. static void
  29. one(int utf8, const char *name, const char *mbs, int width,
  30. int wantwidth, int wantlen, const char *wants)
  31. {
  32. char buf[16];
  33. int *wp;
  34. int len;
  35. if (wantlen == -2)
  36. wantlen = strlen(wants);
  37. (void)strlcpy(buf, utf8 ? "utf8_" : "c_", sizeof(buf));
  38. (void)strlcat(buf, name, sizeof(buf));
  39. TEST_START(buf);
  40. wp = wantwidth == -2 ? NULL : &width;
  41. len = snmprintf(buf, sizeof(buf), wp, "%s", mbs);
  42. ASSERT_INT_EQ(len, wantlen);
  43. ASSERT_STRING_EQ(buf, wants);
  44. ASSERT_INT_EQ(width, wantwidth);
  45. TEST_DONE();
  46. }
  47. void
  48. tests(void)
  49. {
  50. char *loc;
  51. TEST_START("utf8_setlocale");
  52. loc = setlocale(LC_CTYPE, "en_US.UTF-8");
  53. ASSERT_PTR_NE(loc, NULL);
  54. TEST_DONE();
  55. badarg();
  56. one(1, "empty", "", 2, 0, 0, "");
  57. one(1, "ascii", "x", -2, -2, -2, "x");
  58. one(1, "newline", "a\nb", -2, -2, -2, "a\nb");
  59. one(1, "cr", "a\rb", -2, -2, -2, "a\rb");
  60. one(1, "tab", "a\tb", -2, -2, -2, "a\tb");
  61. one(1, "esc", "\033x", -2, -2, -2, "\\033x");
  62. one(1, "inv_badbyte", "\377x", -2, -2, -2, "\\377x");
  63. one(1, "inv_nocont", "\341x", -2, -2, -2, "\\341x");
  64. one(1, "inv_nolead", "a\200b", -2, -2, -2, "a\\200b");
  65. one(1, "sz_ascii", "1234567890123456", -2, -2, 16, "123456789012345");
  66. one(1, "sz_esc", "123456789012\033", -2, -2, 16, "123456789012");
  67. one(1, "width_ascii", "123", 2, 2, -1, "12");
  68. one(1, "width_double", "a\343\201\201", 2, 1, -1, "a");
  69. one(1, "double_fit", "a\343\201\201", 3, 3, 4, "a\343\201\201");
  70. one(1, "double_spc", "a\343\201\201", 4, 3, 4, "a\343\201\201");
  71. TEST_START("C_setlocale");
  72. loc = setlocale(LC_CTYPE, "C");
  73. ASSERT_PTR_NE(loc, NULL);
  74. TEST_DONE();
  75. badarg();
  76. one(0, "empty", "", 2, 0, 0, "");
  77. one(0, "ascii", "x", -2, -2, -2, "x");
  78. one(0, "newline", "a\nb", -2, -2, -2, "a\nb");
  79. one(0, "cr", "a\rb", -2, -2, -2, "a\rb");
  80. one(0, "tab", "a\tb", -2, -2, -2, "a\tb");
  81. one(0, "esc", "\033x", -2, -2, -2, "\\033x");
  82. one(0, "inv_badbyte", "\377x", -2, -2, -2, "\\377x");
  83. one(0, "inv_nocont", "\341x", -2, -2, -2, "\\341x");
  84. one(0, "inv_nolead", "a\200b", -2, -2, -2, "a\\200b");
  85. one(0, "sz_ascii", "1234567890123456", -2, -2, 16, "123456789012345");
  86. one(0, "sz_esc", "123456789012\033", -2, -2, 16, "123456789012");
  87. one(0, "width_ascii", "123", 2, 2, -1, "12");
  88. one(0, "width_double", "a\343\201\201", 2, 1, -1, "a");
  89. one(0, "double_fit", "a\343\201\201", 7, 5, -1, "a\\343");
  90. one(0, "double_spc", "a\343\201\201", 13, 13, 13, "a\\343\\201\\201");
  91. }