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