xref: /netbsd-src/tests/lib/libc/string/t_swab.c (revision ddb0a7e7a71a4bcc4d61a5a8832f8cf885fc5c51)
1*ddb0a7e7Sriastradh /*	$NetBSD: t_swab.c,v 1.3 2022/12/28 15:34:19 riastradh Exp $ */
23ca5a7dbSpgoyette 
33ca5a7dbSpgoyette /*-
43ca5a7dbSpgoyette  * Copyright (c) 2001 The NetBSD Foundation, Inc.
53ca5a7dbSpgoyette  * All rights reserved.
63ca5a7dbSpgoyette  *
73ca5a7dbSpgoyette  * This code was contributed to The NetBSD Foundation by Christos Zoulas.
83ca5a7dbSpgoyette  *
93ca5a7dbSpgoyette  * Redistribution and use in source and binary forms, with or without
103ca5a7dbSpgoyette  * modification, are permitted provided that the following conditions
113ca5a7dbSpgoyette  * are met:
123ca5a7dbSpgoyette  * 1. Redistributions of source code must retain the above copyright
133ca5a7dbSpgoyette  *    notice, this list of conditions and the following disclaimer.
143ca5a7dbSpgoyette  * 2. Redistributions in binary form must reproduce the above copyright
153ca5a7dbSpgoyette  *    notice, this list of conditions and the following disclaimer in the
163ca5a7dbSpgoyette  *    documentation and/or other materials provided with the distribution.
173ca5a7dbSpgoyette  *
183ca5a7dbSpgoyette  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
193ca5a7dbSpgoyette  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
203ca5a7dbSpgoyette  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
213ca5a7dbSpgoyette  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
223ca5a7dbSpgoyette  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
233ca5a7dbSpgoyette  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
243ca5a7dbSpgoyette  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
253ca5a7dbSpgoyette  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
263ca5a7dbSpgoyette  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
273ca5a7dbSpgoyette  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
283ca5a7dbSpgoyette  * POSSIBILITY OF SUCH DAMAGE.
293ca5a7dbSpgoyette  */
303ca5a7dbSpgoyette #include <atf-c.h>
313ca5a7dbSpgoyette 
323ca5a7dbSpgoyette #include <stdio.h>
333ca5a7dbSpgoyette #include <stdlib.h>
343ca5a7dbSpgoyette #include <string.h>
353ca5a7dbSpgoyette #include <unistd.h>
363ca5a7dbSpgoyette 
373ca5a7dbSpgoyette #include <err.h>
383ca5a7dbSpgoyette 
393ca5a7dbSpgoyette #define MAXCHK	100
403ca5a7dbSpgoyette 
413ca5a7dbSpgoyette static void
build(char * a,char * b,size_t n)42*ddb0a7e7Sriastradh build(char *a, char *b, size_t n)
43*ddb0a7e7Sriastradh {
443ca5a7dbSpgoyette 	size_t i;
453ca5a7dbSpgoyette 
463ca5a7dbSpgoyette 	for (i = 0; i < n; i += 2) {
473ca5a7dbSpgoyette 		b[i + 1] = a[i] = (char)i;
483ca5a7dbSpgoyette 		b[i] = a[i + 1] = (char)(i + 1);
493ca5a7dbSpgoyette 	}
50*ddb0a7e7Sriastradh 	if (n & 1)
51*ddb0a7e7Sriastradh 		a[n - 1] = b[n - 1] = (char)~1;
52*ddb0a7e7Sriastradh 	for (; n < MAXCHK; n++)
533ca5a7dbSpgoyette 		a[n] = b[n] = (char)~0;
543ca5a7dbSpgoyette }
553ca5a7dbSpgoyette 
563ca5a7dbSpgoyette static void
dump(const char * f,char * b,size_t l)573ca5a7dbSpgoyette dump(const char *f, char *b, size_t l)
583ca5a7dbSpgoyette {
593ca5a7dbSpgoyette 
603ca5a7dbSpgoyette 	printf("%s ", f);
613ca5a7dbSpgoyette 	while (l--)
623ca5a7dbSpgoyette 		printf("%.2x ", (unsigned char)*b++);
633ca5a7dbSpgoyette 	printf("\n");
643ca5a7dbSpgoyette }
653ca5a7dbSpgoyette 
661c3ef2c7Sjruoho ATF_TC(swab_basic);
ATF_TC_HEAD(swab_basic,tc)671c3ef2c7Sjruoho ATF_TC_HEAD(swab_basic, tc)
683ca5a7dbSpgoyette {
693ca5a7dbSpgoyette 
703ca5a7dbSpgoyette 	atf_tc_set_md_var(tc, "descr", "Test swab results");
713ca5a7dbSpgoyette }
723ca5a7dbSpgoyette 
ATF_TC_BODY(swab_basic,tc)731c3ef2c7Sjruoho ATF_TC_BODY(swab_basic, tc)
743ca5a7dbSpgoyette {
753ca5a7dbSpgoyette 	char a[MAXCHK], b[MAXCHK], r[MAXCHK];
763ca5a7dbSpgoyette 	size_t i;
77*ddb0a7e7Sriastradh 	bool failed = false;
783ca5a7dbSpgoyette 
79*ddb0a7e7Sriastradh 	for (i = 0; i < MAXCHK; i++) {
803ca5a7dbSpgoyette 		build(a, b, i);
813ca5a7dbSpgoyette 		(void)memset(r, ~0, MAXCHK);
823ca5a7dbSpgoyette 		swab(a, r, i);
83*ddb0a7e7Sriastradh 		if (i & 1)	/* last byte unspecified if odd length */
84*ddb0a7e7Sriastradh 			r[i - 1] = (char )~1;
853ca5a7dbSpgoyette 		if (memcmp(b, r, MAXCHK) != 0) {
86*ddb0a7e7Sriastradh 			printf("pattern mismatch at %zu bytes\n", i);
873ca5a7dbSpgoyette 			dump("expect:", b, MAXCHK);
883ca5a7dbSpgoyette 			dump("result:", r, MAXCHK);
89*ddb0a7e7Sriastradh 			failed = true;
903ca5a7dbSpgoyette 		}
913ca5a7dbSpgoyette 	}
92*ddb0a7e7Sriastradh 	if (failed)
93*ddb0a7e7Sriastradh 		atf_tc_fail_nonfatal("Check stdout for details");
943ca5a7dbSpgoyette }
953ca5a7dbSpgoyette 
ATF_TP_ADD_TCS(tp)963ca5a7dbSpgoyette ATF_TP_ADD_TCS(tp)
973ca5a7dbSpgoyette {
981c3ef2c7Sjruoho 	ATF_TP_ADD_TC(tp, swab_basic);
993ca5a7dbSpgoyette 
1003ca5a7dbSpgoyette 	return atf_no_error();
1013ca5a7dbSpgoyette }
102