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 
9 #include <locale.h>
10 #include <string.h>
11 
12 #include "test_helper.h"
13 
14 #include "utf8.h"
15 
16 static void
17 badarg(void)
18 {
19 	char	 buf[16];
20 	int	 len, width;
21 
22 	width = 1;
23 	TEST_START("utf8_badarg");
24 	len = snmprintf(buf, sizeof(buf), &width, "\377");
25 	ASSERT_INT_EQ(len, -1);
26 	ASSERT_STRING_EQ(buf, "");
27 	ASSERT_INT_EQ(width, 0);
28 	TEST_DONE();
29 }
30 
31 static void
32 one(int utf8, const char *name, const char *mbs, int width,
33     int wantwidth, int wantlen, const char *wants)
34 {
35 	char	 buf[16];
36 	int	*wp;
37 	int	 len;
38 
39 	if (wantlen == -2)
40 		wantlen = strlen(wants);
41 	(void)strlcpy(buf, utf8 ? "utf8_" : "c_", sizeof(buf));
42 	(void)strlcat(buf, name, sizeof(buf));
43 	TEST_START(buf);
44 	wp = wantwidth == -2 ? NULL : &width;
45 	len = snmprintf(buf, sizeof(buf), wp, "%s", mbs);
46 	ASSERT_INT_EQ(len, wantlen);
47 	ASSERT_STRING_EQ(buf, wants);
48 	ASSERT_INT_EQ(width, wantwidth);
49 	TEST_DONE();
50 }
51 
52 void
53 tests(void)
54 {
55 	char	*loc;
56 
57 	TEST_START("utf8_setlocale");
58 	loc = setlocale(LC_CTYPE, "en_US.UTF-8");
59 	ASSERT_PTR_NE(loc, NULL);
60 	TEST_DONE();
61 
62 	badarg();
63 	one(1, "empty", "", 2, 0, 0, "");
64 	one(1, "ascii", "x", -2, -2, -2, "x");
65 	one(1, "newline", "a\nb", -2, -2, -2, "a\nb");
66 	one(1, "cr", "a\rb", -2, -2, -2, "a\rb");
67 	one(1, "tab", "a\tb", -2, -2, -2, "a\tb");
68 	one(1, "esc", "\033x", -2, -2, -2, "\\033x");
69 	one(1, "inv_badbyte", "\377x", -2, -2, -2, "\\377x");
70 	one(1, "inv_nocont", "\341x", -2, -2, -2, "\\341x");
71 	one(1, "inv_nolead", "a\200b", -2, -2, -2, "a\\200b");
72 	one(1, "sz_ascii", "1234567890123456", -2, -2, 16, "123456789012345");
73 	one(1, "sz_esc", "123456789012\033", -2, -2, 16, "123456789012");
74 	one(1, "width_ascii", "123", 2, 2, -1, "12");
75 	one(1, "width_double", "a\343\201\201", 2, 1, -1, "a");
76 	one(1, "double_fit", "a\343\201\201", 3, 3, 4, "a\343\201\201");
77 	one(1, "double_spc", "a\343\201\201", 4, 3, 4, "a\343\201\201");
78 
79 	TEST_START("C_setlocale");
80 	loc = setlocale(LC_CTYPE, "C");
81 	ASSERT_PTR_NE(loc, NULL);
82 	TEST_DONE();
83 
84 	badarg();
85 	one(0, "empty", "", 2, 0, 0, "");
86 	one(0, "ascii", "x", -2, -2, -2, "x");
87 	one(0, "newline", "a\nb", -2, -2, -2, "a\nb");
88 	one(0, "cr", "a\rb", -2, -2, -2, "a\rb");
89 	one(0, "tab", "a\tb", -2, -2, -2, "a\tb");
90 	one(0, "esc", "\033x", -2, -2, -2, "\\033x");
91 	one(0, "inv_badbyte", "\377x", -2, -2, -2, "\\377x");
92 	one(0, "inv_nocont", "\341x", -2, -2, -2, "\\341x");
93 	one(0, "inv_nolead", "a\200b", -2, -2, -2, "a\\200b");
94 	one(0, "sz_ascii", "1234567890123456", -2, -2, 16, "123456789012345");
95 	one(0, "sz_esc", "123456789012\033", -2, -2, 16, "123456789012");
96 	one(0, "width_ascii", "123", 2, 2, -1, "12");
97 	one(0, "width_double", "a\343\201\201", 2, 1, -1, "a");
98 	one(0, "double_fit", "a\343\201\201", 7, 5, -1, "a\\343");
99 	one(0, "double_spc", "a\343\201\201", 13, 13, 13, "a\\343\\201\\201");
100 }
101