Patrick Welche <prlw1@cam.ac.uk>
[netbsd-mini2440.git] / regress / lib / libc / string / swab / swabcheck.c
blob59724a462028d46cadd326d56d25a4646e09caa8
1 /* $NetBSD: swabcheck.c,v 1.1 2005/03/13 15:33:45 perry Exp $ */
3 /*-
4 * Copyright (c) 2001 The NetBSD Foundation, Inc.
5 * All rights reserved.
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
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.
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.
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <string.h>
34 #include <unistd.h>
36 #include <err.h>
38 #define MAXCHK 100
40 static void
41 build(char *a, char *b, size_t n) {
42 size_t i;
44 n >>= 1;
45 for (i = 0; i < n; i += 2) {
46 b[i+1] = a[i] = (char)i;
47 b[i] = a[i+1] = (char)(i+1);
49 for (n <<= 1; n < MAXCHK; n++)
50 a[n] = b[n] = (char)~0;
54 static void
55 dump(f, b, l)
56 const char *f;
57 char *b;
58 size_t l;
60 printf("%s ", f);
61 while (l--)
62 printf("%.2x ", (unsigned char)*b++);
63 printf("\n");
66 int
67 main(int argc, char *argv[])
69 char a[MAXCHK], b[MAXCHK], r[MAXCHK];
70 size_t i;
72 for (i = 0; i < MAXCHK; i += 2) {
73 build(a, b, i);
74 (void)memset(r, ~0, MAXCHK);
75 swab(a, r, i);
76 if (memcmp(b, r, MAXCHK) != 0) {
77 warnx("pattern mismatch at %lu bytes",
78 (unsigned long)i);
79 dump("expect:", b, MAXCHK);
80 dump("result:", r, MAXCHK);
81 return 1;
84 return 0;