xref: /netbsd-src/tests/lib/libc/string/t_swab.c (revision ddb0a7e7a71a4bcc4d61a5a8832f8cf885fc5c51)
1 /*	$NetBSD: t_swab.c,v 1.3 2022/12/28 15:34:19 riastradh Exp $ */
2 
3 /*-
4  * Copyright (c) 2001 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code was contributed to The NetBSD Foundation by Christos Zoulas.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
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.
17  *
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.
29  */
30 #include <atf-c.h>
31 
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include <string.h>
35 #include <unistd.h>
36 
37 #include <err.h>
38 
39 #define MAXCHK	100
40 
41 static void
build(char * a,char * b,size_t n)42 build(char *a, char *b, size_t n)
43 {
44 	size_t i;
45 
46 	for (i = 0; i < n; i += 2) {
47 		b[i + 1] = a[i] = (char)i;
48 		b[i] = a[i + 1] = (char)(i + 1);
49 	}
50 	if (n & 1)
51 		a[n - 1] = b[n - 1] = (char)~1;
52 	for (; n < MAXCHK; n++)
53 		a[n] = b[n] = (char)~0;
54 }
55 
56 static void
dump(const char * f,char * b,size_t l)57 dump(const char *f, char *b, size_t l)
58 {
59 
60 	printf("%s ", f);
61 	while (l--)
62 		printf("%.2x ", (unsigned char)*b++);
63 	printf("\n");
64 }
65 
66 ATF_TC(swab_basic);
ATF_TC_HEAD(swab_basic,tc)67 ATF_TC_HEAD(swab_basic, tc)
68 {
69 
70 	atf_tc_set_md_var(tc, "descr", "Test swab results");
71 }
72 
ATF_TC_BODY(swab_basic,tc)73 ATF_TC_BODY(swab_basic, tc)
74 {
75 	char a[MAXCHK], b[MAXCHK], r[MAXCHK];
76 	size_t i;
77 	bool failed = false;
78 
79 	for (i = 0; i < MAXCHK; i++) {
80 		build(a, b, i);
81 		(void)memset(r, ~0, MAXCHK);
82 		swab(a, r, i);
83 		if (i & 1)	/* last byte unspecified if odd length */
84 			r[i - 1] = (char )~1;
85 		if (memcmp(b, r, MAXCHK) != 0) {
86 			printf("pattern mismatch at %zu bytes\n", i);
87 			dump("expect:", b, MAXCHK);
88 			dump("result:", r, MAXCHK);
89 			failed = true;
90 		}
91 	}
92 	if (failed)
93 		atf_tc_fail_nonfatal("Check stdout for details");
94 }
95 
ATF_TP_ADD_TCS(tp)96 ATF_TP_ADD_TCS(tp)
97 {
98 	ATF_TP_ADD_TC(tp, swab_basic);
99 
100 	return atf_no_error();
101 }
102