summaryrefslogtreecommitdiff
path: root/regress/unittests/utf8/tests.c
diff options
context:
space:
mode:
Diffstat (limited to 'regress/unittests/utf8/tests.c')
-rw-r--r--regress/unittests/utf8/tests.c63
1 files changed, 63 insertions, 0 deletions
diff --git a/regress/unittests/utf8/tests.c b/regress/unittests/utf8/tests.c
new file mode 100644
index 000000000..d18cadc5d
--- /dev/null
+++ b/regress/unittests/utf8/tests.c
@@ -0,0 +1,63 @@
1/* $OpenBSD: tests.c,v 1.1 2016/05/26 19:14:25 schwarze 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
9#include <locale.h>
10#include <string.h>
11
12#include "test_helper.h"
13
14#include "utf8.h"
15
16void one(const char *, const char *, int, int, int, const char *);
17
18void
19one(const char *name, const char *mbs, int width,
20 int wantwidth, int wantlen, const char *wants)
21{
22 char buf[16];
23 int *wp;
24 int len;
25
26 if (wantlen == -2)
27 wantlen = strlen(wants);
28 (void)strlcpy(buf, "utf8_", sizeof(buf));
29 (void)strlcat(buf, name, sizeof(buf));
30 TEST_START(buf);
31 wp = wantwidth == -2 ? NULL : &width;
32 len = snmprintf(buf, sizeof(buf), wp, "%s", mbs);
33 ASSERT_INT_EQ(len, wantlen);
34 ASSERT_STRING_EQ(buf, wants);
35 ASSERT_INT_EQ(width, wantwidth);
36 TEST_DONE();
37}
38
39void
40tests(void)
41{
42 char *loc;
43
44 TEST_START("utf8_setlocale");
45 loc = setlocale(LC_CTYPE, "en_US.UTF-8");
46 ASSERT_PTR_NE(loc, NULL);
47 TEST_DONE();
48
49 one("ascii", "x", -2, -2, -2, "x");
50 one("newline", "a\nb", -2, -2, -2, "a\nb");
51 one("cr", "a\rb", -2, -2, -2, "a\rb");
52 one("tab", "a\tb", -2, -2, -2, "a\tb");
53 one("esc", "\033x", -2, -2, -2, "\\033x");
54 one("inv_badbyte", "\377x", -2, -2, -2, "\\377x");
55 one("inv_nocont", "\341x", -2, -2, -2, "\\341x");
56 one("inv_nolead", "a\200b", -2, -2, -2, "a\\200b");
57 one("sz_ascii", "1234567890123456", -2, -2, 16, "123456789012345");
58 one("sz_esc", "123456789012\033", -2, -2, 16, "123456789012");
59 one("width_ascii", "123", 2, 2, -1, "12");
60 one("width_double", "a\343\201\201", 2, 1, -1, "a");
61 one("double_fit", "a\343\201\201", 3, 3, 4, "a\343\201\201");
62 one("double_spc", "a\343\201\201", 4, 3, 4, "a\343\201\201");
63}