1*57718be8SEnji Cooper /* $NetBSD: t_swab.c,v 1.2 2011/07/07 08:27:36 jruoho Exp $ */
2*57718be8SEnji Cooper
3*57718be8SEnji Cooper /*-
4*57718be8SEnji Cooper * Copyright (c) 2001 The NetBSD Foundation, Inc.
5*57718be8SEnji Cooper * All rights reserved.
6*57718be8SEnji Cooper *
7*57718be8SEnji Cooper * This code was contributed to The NetBSD Foundation by Christos Zoulas.
8*57718be8SEnji Cooper *
9*57718be8SEnji Cooper * Redistribution and use in source and binary forms, with or without
10*57718be8SEnji Cooper * modification, are permitted provided that the following conditions
11*57718be8SEnji Cooper * are met:
12*57718be8SEnji Cooper * 1. Redistributions of source code must retain the above copyright
13*57718be8SEnji Cooper * notice, this list of conditions and the following disclaimer.
14*57718be8SEnji Cooper * 2. Redistributions in binary form must reproduce the above copyright
15*57718be8SEnji Cooper * notice, this list of conditions and the following disclaimer in the
16*57718be8SEnji Cooper * documentation and/or other materials provided with the distribution.
17*57718be8SEnji Cooper *
18*57718be8SEnji Cooper * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
19*57718be8SEnji Cooper * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
20*57718be8SEnji Cooper * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
21*57718be8SEnji Cooper * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
22*57718be8SEnji Cooper * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23*57718be8SEnji Cooper * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24*57718be8SEnji Cooper * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25*57718be8SEnji Cooper * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26*57718be8SEnji Cooper * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27*57718be8SEnji Cooper * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28*57718be8SEnji Cooper * POSSIBILITY OF SUCH DAMAGE.
29*57718be8SEnji Cooper */
30*57718be8SEnji Cooper #include <atf-c.h>
31*57718be8SEnji Cooper
32*57718be8SEnji Cooper #include <stdio.h>
33*57718be8SEnji Cooper #include <stdlib.h>
34*57718be8SEnji Cooper #include <string.h>
35*57718be8SEnji Cooper #include <unistd.h>
36*57718be8SEnji Cooper
37*57718be8SEnji Cooper #include <err.h>
38*57718be8SEnji Cooper
39*57718be8SEnji Cooper #define MAXCHK 100
40*57718be8SEnji Cooper
41*57718be8SEnji Cooper static void
build(char * a,char * b,size_t n)42*57718be8SEnji Cooper build(char *a, char *b, size_t n) {
43*57718be8SEnji Cooper size_t i;
44*57718be8SEnji Cooper
45*57718be8SEnji Cooper n >>= 1;
46*57718be8SEnji Cooper for (i = 0; i < n; i += 2) {
47*57718be8SEnji Cooper b[i+1] = a[i] = (char)i;
48*57718be8SEnji Cooper b[i] = a[i+1] = (char)(i+1);
49*57718be8SEnji Cooper }
50*57718be8SEnji Cooper for (n <<= 1; n < MAXCHK; n++)
51*57718be8SEnji Cooper a[n] = b[n] = (char)~0;
52*57718be8SEnji Cooper }
53*57718be8SEnji Cooper
54*57718be8SEnji Cooper static void
dump(const char * f,char * b,size_t l)55*57718be8SEnji Cooper dump(const char *f, char *b, size_t l)
56*57718be8SEnji Cooper {
57*57718be8SEnji Cooper
58*57718be8SEnji Cooper printf("%s ", f);
59*57718be8SEnji Cooper while (l--)
60*57718be8SEnji Cooper printf("%.2x ", (unsigned char)*b++);
61*57718be8SEnji Cooper printf("\n");
62*57718be8SEnji Cooper }
63*57718be8SEnji Cooper
64*57718be8SEnji Cooper ATF_TC(swab_basic);
ATF_TC_HEAD(swab_basic,tc)65*57718be8SEnji Cooper ATF_TC_HEAD(swab_basic, tc)
66*57718be8SEnji Cooper {
67*57718be8SEnji Cooper
68*57718be8SEnji Cooper atf_tc_set_md_var(tc, "descr", "Test swab results");
69*57718be8SEnji Cooper }
70*57718be8SEnji Cooper
ATF_TC_BODY(swab_basic,tc)71*57718be8SEnji Cooper ATF_TC_BODY(swab_basic, tc)
72*57718be8SEnji Cooper {
73*57718be8SEnji Cooper char a[MAXCHK], b[MAXCHK], r[MAXCHK];
74*57718be8SEnji Cooper size_t i;
75*57718be8SEnji Cooper
76*57718be8SEnji Cooper for (i = 0; i < MAXCHK; i += 2) {
77*57718be8SEnji Cooper build(a, b, i);
78*57718be8SEnji Cooper (void)memset(r, ~0, MAXCHK);
79*57718be8SEnji Cooper swab(a, r, i);
80*57718be8SEnji Cooper if (memcmp(b, r, MAXCHK) != 0) {
81*57718be8SEnji Cooper fprintf(stderr, "pattern mismatch at %lu bytes",
82*57718be8SEnji Cooper (unsigned long)i);
83*57718be8SEnji Cooper dump("expect:", b, MAXCHK);
84*57718be8SEnji Cooper dump("result:", r, MAXCHK);
85*57718be8SEnji Cooper atf_tc_fail("Check stderr for details");
86*57718be8SEnji Cooper }
87*57718be8SEnji Cooper }
88*57718be8SEnji Cooper }
89*57718be8SEnji Cooper
ATF_TP_ADD_TCS(tp)90*57718be8SEnji Cooper ATF_TP_ADD_TCS(tp)
91*57718be8SEnji Cooper {
92*57718be8SEnji Cooper ATF_TP_ADD_TC(tp, swab_basic);
93*57718be8SEnji Cooper
94*57718be8SEnji Cooper return atf_no_error();
95*57718be8SEnji Cooper }
96