1d5499190Sdjm@openbsd.org /*	$OpenBSD: tests.c,v 1.4 2017/02/19 00:11:29 djm Exp $ */
2016881ebSschwarze@openbsd.org /*
3016881ebSschwarze@openbsd.org  * Regress test for the utf8.h *mprintf() API
4016881ebSschwarze@openbsd.org  *
5016881ebSschwarze@openbsd.org  * Written by Ingo Schwarze <schwarze@openbsd.org> in 2016
6016881ebSschwarze@openbsd.org  * and placed in the public domain.
7016881ebSschwarze@openbsd.org  */
8016881ebSschwarze@openbsd.org 
96ee4f1c0SDamien Miller #include "includes.h"
106ee4f1c0SDamien Miller 
11016881ebSschwarze@openbsd.org #include <locale.h>
12*f61f29afSDamien Miller #include <stdarg.h>
13016881ebSschwarze@openbsd.org #include <string.h>
14*f61f29afSDamien Miller #include <stdio.h>
15016881ebSschwarze@openbsd.org 
166ee4f1c0SDamien Miller #include "../test_helper/test_helper.h"
17016881ebSschwarze@openbsd.org 
18016881ebSschwarze@openbsd.org #include "utf8.h"
19016881ebSschwarze@openbsd.org 
20d5499190Sdjm@openbsd.org static void
badarg(void)2175f0844bSschwarze@openbsd.org badarg(void)
2275f0844bSschwarze@openbsd.org {
2375f0844bSschwarze@openbsd.org 	char	 buf[16];
2475f0844bSschwarze@openbsd.org 	int	 len, width;
2575f0844bSschwarze@openbsd.org 
2675f0844bSschwarze@openbsd.org 	width = 1;
2775f0844bSschwarze@openbsd.org 	TEST_START("utf8_badarg");
2875f0844bSschwarze@openbsd.org 	len = snmprintf(buf, sizeof(buf), &width, "\377");
2975f0844bSschwarze@openbsd.org 	ASSERT_INT_EQ(len, -1);
3075f0844bSschwarze@openbsd.org 	ASSERT_STRING_EQ(buf, "");
3175f0844bSschwarze@openbsd.org 	ASSERT_INT_EQ(width, 0);
3275f0844bSschwarze@openbsd.org 	TEST_DONE();
3375f0844bSschwarze@openbsd.org }
3475f0844bSschwarze@openbsd.org 
35d5499190Sdjm@openbsd.org static void
one(int utf8,const char * name,const char * mbs,int width,int wantwidth,int wantlen,const char * wants)36d5499190Sdjm@openbsd.org one(int utf8, const char *name, const char *mbs, int width,
37016881ebSschwarze@openbsd.org     int wantwidth, int wantlen, const char *wants)
38016881ebSschwarze@openbsd.org {
39016881ebSschwarze@openbsd.org 	char	 buf[16];
40016881ebSschwarze@openbsd.org 	int	*wp;
41016881ebSschwarze@openbsd.org 	int	 len;
42016881ebSschwarze@openbsd.org 
43016881ebSschwarze@openbsd.org 	if (wantlen == -2)
44016881ebSschwarze@openbsd.org 		wantlen = strlen(wants);
45d5499190Sdjm@openbsd.org 	(void)strlcpy(buf, utf8 ? "utf8_" : "c_", sizeof(buf));
46016881ebSschwarze@openbsd.org 	(void)strlcat(buf, name, sizeof(buf));
47016881ebSschwarze@openbsd.org 	TEST_START(buf);
48016881ebSschwarze@openbsd.org 	wp = wantwidth == -2 ? NULL : &width;
49016881ebSschwarze@openbsd.org 	len = snmprintf(buf, sizeof(buf), wp, "%s", mbs);
50016881ebSschwarze@openbsd.org 	ASSERT_INT_EQ(len, wantlen);
51016881ebSschwarze@openbsd.org 	ASSERT_STRING_EQ(buf, wants);
52016881ebSschwarze@openbsd.org 	ASSERT_INT_EQ(width, wantwidth);
53016881ebSschwarze@openbsd.org 	TEST_DONE();
54016881ebSschwarze@openbsd.org }
55016881ebSschwarze@openbsd.org 
56016881ebSschwarze@openbsd.org void
tests(void)57016881ebSschwarze@openbsd.org tests(void)
58016881ebSschwarze@openbsd.org {
59016881ebSschwarze@openbsd.org 	char	*loc;
60016881ebSschwarze@openbsd.org 
61016881ebSschwarze@openbsd.org 	TEST_START("utf8_setlocale");
62016881ebSschwarze@openbsd.org 	loc = setlocale(LC_CTYPE, "en_US.UTF-8");
63016881ebSschwarze@openbsd.org 	ASSERT_PTR_NE(loc, NULL);
64016881ebSschwarze@openbsd.org 	TEST_DONE();
65016881ebSschwarze@openbsd.org 
6675f0844bSschwarze@openbsd.org 	badarg();
67d5499190Sdjm@openbsd.org 	one(1, "empty", "", 2, 0, 0, "");
68d5499190Sdjm@openbsd.org 	one(1, "ascii", "x", -2, -2, -2, "x");
69d5499190Sdjm@openbsd.org 	one(1, "newline", "a\nb", -2, -2, -2, "a\nb");
70d5499190Sdjm@openbsd.org 	one(1, "cr", "a\rb", -2, -2, -2, "a\rb");
71d5499190Sdjm@openbsd.org 	one(1, "tab", "a\tb", -2, -2, -2, "a\tb");
72d5499190Sdjm@openbsd.org 	one(1, "esc", "\033x", -2, -2, -2, "\\033x");
73d5499190Sdjm@openbsd.org 	one(1, "inv_badbyte", "\377x", -2, -2, -2, "\\377x");
74d5499190Sdjm@openbsd.org 	one(1, "inv_nocont", "\341x", -2, -2, -2, "\\341x");
75d5499190Sdjm@openbsd.org 	one(1, "inv_nolead", "a\200b", -2, -2, -2, "a\\200b");
76d5499190Sdjm@openbsd.org 	one(1, "sz_ascii", "1234567890123456", -2, -2, 16, "123456789012345");
77d5499190Sdjm@openbsd.org 	one(1, "sz_esc", "123456789012\033", -2, -2, 16, "123456789012");
78d5499190Sdjm@openbsd.org 	one(1, "width_ascii", "123", 2, 2, -1, "12");
79d5499190Sdjm@openbsd.org 	one(1, "width_double", "a\343\201\201", 2, 1, -1, "a");
80d5499190Sdjm@openbsd.org 	one(1, "double_fit", "a\343\201\201", 3, 3, 4, "a\343\201\201");
81d5499190Sdjm@openbsd.org 	one(1, "double_spc", "a\343\201\201", 4, 3, 4, "a\343\201\201");
82d5499190Sdjm@openbsd.org 
83d5499190Sdjm@openbsd.org 	TEST_START("C_setlocale");
84d5499190Sdjm@openbsd.org 	loc = setlocale(LC_CTYPE, "C");
85d5499190Sdjm@openbsd.org 	ASSERT_PTR_NE(loc, NULL);
86d5499190Sdjm@openbsd.org 	TEST_DONE();
87d5499190Sdjm@openbsd.org 
88d5499190Sdjm@openbsd.org 	badarg();
89d5499190Sdjm@openbsd.org 	one(0, "empty", "", 2, 0, 0, "");
90d5499190Sdjm@openbsd.org 	one(0, "ascii", "x", -2, -2, -2, "x");
91d5499190Sdjm@openbsd.org 	one(0, "newline", "a\nb", -2, -2, -2, "a\nb");
92d5499190Sdjm@openbsd.org 	one(0, "cr", "a\rb", -2, -2, -2, "a\rb");
93d5499190Sdjm@openbsd.org 	one(0, "tab", "a\tb", -2, -2, -2, "a\tb");
94d5499190Sdjm@openbsd.org 	one(0, "esc", "\033x", -2, -2, -2, "\\033x");
95d5499190Sdjm@openbsd.org 	one(0, "inv_badbyte", "\377x", -2, -2, -2, "\\377x");
96d5499190Sdjm@openbsd.org 	one(0, "inv_nocont", "\341x", -2, -2, -2, "\\341x");
97d5499190Sdjm@openbsd.org 	one(0, "inv_nolead", "a\200b", -2, -2, -2, "a\\200b");
98d5499190Sdjm@openbsd.org 	one(0, "sz_ascii", "1234567890123456", -2, -2, 16, "123456789012345");
99d5499190Sdjm@openbsd.org 	one(0, "sz_esc", "123456789012\033", -2, -2, 16, "123456789012");
100d5499190Sdjm@openbsd.org 	one(0, "width_ascii", "123", 2, 2, -1, "12");
101d5499190Sdjm@openbsd.org 	one(0, "width_double", "a\343\201\201", 2, 1, -1, "a");
102d5499190Sdjm@openbsd.org 	one(0, "double_fit", "a\343\201\201", 7, 5, -1, "a\\343");
103d5499190Sdjm@openbsd.org 	one(0, "double_spc", "a\343\201\201", 13, 13, 13, "a\\343\\201\\201");
104016881ebSschwarze@openbsd.org }
105