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