]>
git.saurik.com Git - apple/libc.git/blob - tests/netbsd_vis.c
067096490920be9c96e88d3b79e42e8d229b552c
1 /* $NetBSD: t_vis.c,v 1.7 2014/09/08 19:01:03 christos Exp $ */
4 * Copyright (c) 2002 The NetBSD Foundation, Inc.
7 * This code was contributed to The NetBSD Foundation by Christos Zoulas.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
19 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
20 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
21 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
22 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 * POSSIBILITY OF SUCH DAMAGE.
36 #include <darwintest.h>
38 static int styles
[] = {
46 #if 0 /* Not reversible */
51 #if 0 /* Not supported by vis(3) */
58 T_DECL(strvis_basic
, "strvis(3)")
60 char *srcbuf
, *dstbuf
, *visbuf
;
63 T_ASSERT_NOTNULL((srcbuf
= malloc(SIZE
)), NULL
);
64 T_ASSERT_NOTNULL((dstbuf
= malloc(SIZE
+ 1)), NULL
);
65 T_ASSERT_NOTNULL((visbuf
= malloc(SIZE
* 4 + 1)), NULL
);
67 for (i
= 0; i
< SIZE
; i
++)
70 for (i
= 0; i
< sizeof(styles
)/sizeof(styles
[0]); i
++) {
71 T_ASSERT_GT(strsvisx(visbuf
, srcbuf
, SIZE
, styles
[i
], ""), 0, NULL
);
72 memset(dstbuf
, 0, SIZE
);
73 T_ASSERT_GT(strunvisx(dstbuf
, visbuf
,
74 styles
[i
] & (VIS_HTTP1808
|VIS_MIMESTYLE
)), 0, NULL
);
75 for (j
= 0; j
< SIZE
; j
++)
76 if (dstbuf
[j
] != (char)j
)
77 T_FAIL("Failed for style %x, char %d [%d]", styles
[i
], j
, dstbuf
[j
]);
78 if (dstbuf
[SIZE
] != '\0')
79 T_FAIL("Failed for style %x, the result must be null-terminated [%d]", dstbuf
[SIZE
]);
86 T_DECL(strvis_null
, "strvis(3) NULL")
89 strvis(dst
, NULL
, VIS_SAFE
);
90 T_ASSERT_EQ(dst
[0], '\0', NULL
);
91 T_ASSERT_EQ(dst
[1], 'a', NULL
);
94 T_DECL(strvis_empty
, "strvis(3) empty")
97 strvis(dst
, "", VIS_SAFE
);
98 T_ASSERT_EQ(dst
[0], '\0', NULL
);
99 T_ASSERT_EQ(dst
[1], 'a', NULL
);
102 T_DECL(strunvis_hex
, "strunvis(3) \\Xxx")
104 static const struct {
109 { "\\xff", "\xff", 1 },
110 { "\\x1", "\x1", 1 },
111 { "\\x1\\x02", "\x1\x2", 2 },
112 { "\\x1x", "\x1x", 2 },
117 for (size_t i
= 0; i
< sizeof(ed
)/sizeof(ed
[0]); i
++) {
118 T_ASSERT_EQ(strunvis(uv
, ed
[i
].e
), ed
[i
].error
, NULL
);
120 T_ASSERT_EQ(memcmp(ed
[i
].d
, uv
, (unsigned long)ed
[i
].error
), 0, NULL
);