1272343Sngie/*	$NetBSD: t_swab.c,v 1.2 2011/07/07 08:27:36 jruoho Exp $ */
2272343Sngie
3272343Sngie/*-
4272343Sngie * Copyright (c) 2001 The NetBSD Foundation, Inc.
5272343Sngie * All rights reserved.
6272343Sngie *
7272343Sngie * This code was contributed to The NetBSD Foundation by Christos Zoulas.
8272343Sngie *
9272343Sngie * Redistribution and use in source and binary forms, with or without
10272343Sngie * modification, are permitted provided that the following conditions
11272343Sngie * are met:
12272343Sngie * 1. Redistributions of source code must retain the above copyright
13272343Sngie *    notice, this list of conditions and the following disclaimer.
14272343Sngie * 2. Redistributions in binary form must reproduce the above copyright
15272343Sngie *    notice, this list of conditions and the following disclaimer in the
16272343Sngie *    documentation and/or other materials provided with the distribution.
17272343Sngie *
18272343Sngie * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
19272343Sngie * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
20272343Sngie * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
21272343Sngie * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
22272343Sngie * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23272343Sngie * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24272343Sngie * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25272343Sngie * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26272343Sngie * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27272343Sngie * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28272343Sngie * POSSIBILITY OF SUCH DAMAGE.
29272343Sngie */
30272343Sngie#include <atf-c.h>
31272343Sngie
32272343Sngie#include <stdio.h>
33272343Sngie#include <stdlib.h>
34272343Sngie#include <string.h>
35272343Sngie#include <unistd.h>
36272343Sngie
37272343Sngie#include <err.h>
38272343Sngie
39272343Sngie#define MAXCHK	100
40272343Sngie
41272343Sngiestatic void
42272343Sngiebuild(char *a, char *b, size_t n) {
43272343Sngie	size_t i;
44272343Sngie
45272343Sngie	n >>= 1;
46272343Sngie	for (i = 0; i < n; i += 2) {
47272343Sngie		b[i+1] = a[i] = (char)i;
48272343Sngie		b[i] = a[i+1] = (char)(i+1);
49272343Sngie	}
50272343Sngie	for (n <<= 1; n < MAXCHK; n++)
51272343Sngie		a[n] = b[n] = (char)~0;
52272343Sngie}
53272343Sngie
54272343Sngiestatic void
55272343Sngiedump(const char *f, char *b, size_t l)
56272343Sngie{
57272343Sngie
58272343Sngie	printf("%s ", f);
59272343Sngie	while (l--)
60272343Sngie		printf("%.2x ", (unsigned char)*b++);
61272343Sngie	printf("\n");
62272343Sngie}
63272343Sngie
64272343SngieATF_TC(swab_basic);
65272343SngieATF_TC_HEAD(swab_basic, tc)
66272343Sngie{
67272343Sngie
68272343Sngie	atf_tc_set_md_var(tc, "descr", "Test swab results");
69272343Sngie}
70272343Sngie
71272343SngieATF_TC_BODY(swab_basic, tc)
72272343Sngie{
73272343Sngie	char a[MAXCHK], b[MAXCHK], r[MAXCHK];
74272343Sngie	size_t i;
75272343Sngie
76272343Sngie	for (i = 0; i < MAXCHK; i += 2) {
77272343Sngie		build(a, b, i);
78272343Sngie		(void)memset(r, ~0, MAXCHK);
79272343Sngie		swab(a, r, i);
80272343Sngie		if (memcmp(b, r, MAXCHK) != 0) {
81272343Sngie			fprintf(stderr, "pattern mismatch at %lu bytes",
82272343Sngie			    (unsigned long)i);
83272343Sngie			dump("expect:", b, MAXCHK);
84272343Sngie			dump("result:", r, MAXCHK);
85272343Sngie			atf_tc_fail("Check stderr for details");
86272343Sngie		}
87272343Sngie	}
88272343Sngie}
89272343Sngie
90272343SngieATF_TP_ADD_TCS(tp)
91272343Sngie{
92272343Sngie	ATF_TP_ADD_TC(tp, swab_basic);
93272343Sngie
94272343Sngie	return atf_no_error();
95272343Sngie}
96