1181155Sdas/*	$NetBSD: tfmtcheck.c,v 1.3 2008/04/28 20:23:04 martin Exp $	*/
2181155Sdas
3181155Sdas/*-
4181155Sdas * Copyright (c) 2000 The NetBSD Foundation, Inc.
5181155Sdas * All rights reserved.
6181155Sdas *
7181155Sdas * This code was contributed to The NetBSD Foundation by Allen Briggs.
8181155Sdas *
9181155Sdas * Redistribution and use in source and binary forms, with or without
10181155Sdas * modification, are permitted provided that the following conditions
11181155Sdas * are met:
12181155Sdas * 1. Redistributions of source code must retain the above copyright
13181155Sdas *    notice, this list of conditions and the following disclaimer.
14181155Sdas * 2. Redistributions in binary form must reproduce the above copyright
15181155Sdas *    notice, this list of conditions and the following disclaimer in the
16181155Sdas *    documentation and/or other materials provided with the distribution.
17181155Sdas *
18181155Sdas * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
19181155Sdas * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
20181155Sdas * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
21181155Sdas * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
22181155Sdas * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23181155Sdas * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24181155Sdas * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25181155Sdas * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26181155Sdas * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27181155Sdas * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28181155Sdas * POSSIBILITY OF SUCH DAMAGE.
29181155Sdas */
30181155Sdas
31181155Sdas#include <sys/cdefs.h>
32181155Sdas__FBSDID("$FreeBSD$");
33181155Sdas
34181155Sdas#include <err.h>
35181155Sdas#include <stdio.h>
36181155Sdas#include <stdlib.h>
37181155Sdas
38181155Sdasstruct test_fmt {
39181155Sdas	char	*fmt1;
40181155Sdas	char	*fmt2;
41181155Sdas	int	correct;
42181155Sdas} test_fmts[] = {
43181155Sdas	{ "%d", "%d", 1 },
44181155Sdas	{ "%2d", "%2.2d", 1 },
45181155Sdas	{ "%x", "%d", 1 },
46181155Sdas	{ "%u", "%d", 1 },
47181155Sdas	{ "%03d", "%d", 1 },
48181155Sdas	{ "%-2d", "%d", 1 },
49181155Sdas	{ "%d", "%-12.1d", 1 },
50181155Sdas	{ "%d", "%-01.3d", 1 },
51181155Sdas	{ "%X", "%-01.3d", 1 },
52181155Sdas	{ "%D", "%ld", 1 },
53181155Sdas	{ "%s", "%s", 1 },
54181155Sdas	{ "%s", "This is a %s test", 1 },
55181155Sdas	{ "Hi, there.  This is a %s test", "%s", 1 },
56181155Sdas	{ "%d", "%s", 2 },
57181155Sdas	{ "%e", "%s", 2 },
58181155Sdas	{ "%r", "%d", 2 },
59181155Sdas	{ "%*.2d", "%*d", 1 },
60181155Sdas	{ "%2.*d", "%*d", 2 },
61181155Sdas	{ "%*d", "%*d", 1 },
62181155Sdas	{ "%-3", "%d", 2 },
63181155Sdas	{ "%d %s", "%d", 2 },
64181155Sdas	{ "%*.*.*d", "%*.*.*d", 2 },
65181155Sdas	{ "%d", "%d %s", 1 },
66181155Sdas	{ "%40s", "%20s", 1 },
67181155Sdas	{ "%x %x %x", "%o %u %d", 1 },
68181155Sdas	{ "%o %u %d", "%x %x %X", 1 },
69181155Sdas	{ "%#o %u %#-d", "%x %#x %X", 1 },
70181155Sdas	{ "%qd", "%llx", 1 },
71181155Sdas	{ "%%", "%llx", 1 },
72181155Sdas	{ "%p %30s %#llx %-10.*e", "This number %lu%% and string %s has %qd numbers and %.*g floats", 1 },
73181155Sdas};
74181155Sdas
75181155Sdasint
76181155Sdasmain(int argc, char *argv[])
77181155Sdas{
78181155Sdas	int		i, n, r;
79181155Sdas	const char	*f, *cf, *f1, *f2;
80181155Sdas
81181155Sdas	printf("1..1\n");
82181155Sdas	r = 0;
83181155Sdas	n = sizeof(test_fmts) / sizeof(test_fmts[0]);
84181155Sdas	for (i=0 ; i<n ; i++) {
85181155Sdas		f1 = test_fmts[i].fmt1;
86181155Sdas		f2 = test_fmts[i].fmt2;
87181155Sdas		f = fmtcheck(f1, f2);
88181155Sdas		if (test_fmts[i].correct == 1) {
89181155Sdas			cf = f1;
90181155Sdas		} else {
91181155Sdas			cf = f2;
92181155Sdas		}
93181155Sdas		if (f != cf) {
94181155Sdas			r++;
95181155Sdas			errx(1, "Test %d: (%s) vs. (%s) failed "
96181155Sdas			    "(should have returned %s)", i, f1, f2,
97181155Sdas			    (test_fmts[i].correct == 1) ? "1st" : "2nd");
98181155Sdas		}
99181155Sdas	}
100181155Sdas	printf("ok 1\n");
101181155Sdas	exit(0);
102181155Sdas}
103