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: releng/10.3/lib/libc/tests/gen/fmtcheck_test.c 291190 2015-11-23 10:53:01Z ngie $");
33181155Sdas
34290572Sngie#include <sys/param.h>
35181155Sdas#include <err.h>
36181155Sdas#include <stdio.h>
37181155Sdas#include <stdlib.h>
38181155Sdas
39290572Sngie#include <atf-c.h>
40290572Sngie
41181155Sdasstruct test_fmt {
42181155Sdas	char	*fmt1;
43181155Sdas	char	*fmt2;
44181155Sdas	int	correct;
45181155Sdas} test_fmts[] = {
46181155Sdas	{ "%d", "%d", 1 },
47181155Sdas	{ "%2d", "%2.2d", 1 },
48181155Sdas	{ "%x", "%d", 1 },
49181155Sdas	{ "%u", "%d", 1 },
50181155Sdas	{ "%03d", "%d", 1 },
51181155Sdas	{ "%-2d", "%d", 1 },
52181155Sdas	{ "%d", "%-12.1d", 1 },
53181155Sdas	{ "%d", "%-01.3d", 1 },
54181155Sdas	{ "%X", "%-01.3d", 1 },
55181155Sdas	{ "%D", "%ld", 1 },
56181155Sdas	{ "%s", "%s", 1 },
57181155Sdas	{ "%s", "This is a %s test", 1 },
58181155Sdas	{ "Hi, there.  This is a %s test", "%s", 1 },
59181155Sdas	{ "%d", "%s", 2 },
60181155Sdas	{ "%e", "%s", 2 },
61181155Sdas	{ "%r", "%d", 2 },
62181155Sdas	{ "%*.2d", "%*d", 1 },
63181155Sdas	{ "%2.*d", "%*d", 2 },
64181155Sdas	{ "%*d", "%*d", 1 },
65181155Sdas	{ "%-3", "%d", 2 },
66181155Sdas	{ "%d %s", "%d", 2 },
67181155Sdas	{ "%*.*.*d", "%*.*.*d", 2 },
68181155Sdas	{ "%d", "%d %s", 1 },
69181155Sdas	{ "%40s", "%20s", 1 },
70181155Sdas	{ "%x %x %x", "%o %u %d", 1 },
71181155Sdas	{ "%o %u %d", "%x %x %X", 1 },
72181155Sdas	{ "%#o %u %#-d", "%x %#x %X", 1 },
73181155Sdas	{ "%qd", "%llx", 1 },
74181155Sdas	{ "%%", "%llx", 1 },
75181155Sdas	{ "%p %30s %#llx %-10.*e", "This number %lu%% and string %s has %qd numbers and %.*g floats", 1 },
76181155Sdas};
77181155Sdas
78290572SngieATF_TC_WITHOUT_HEAD(fmtcheck_test);
79290572SngieATF_TC_BODY(fmtcheck_test, tc)
80181155Sdas{
81290572Sngie	int i;
82290572Sngie	const char *f, *cf, *f1, *f2;
83181155Sdas
84290572Sngie	for (i = 0; i < nitems(test_fmts); i++) {
85181155Sdas		f1 = test_fmts[i].fmt1;
86181155Sdas		f2 = test_fmts[i].fmt2;
87181155Sdas		f = fmtcheck(f1, f2);
88290572Sngie		if (test_fmts[i].correct == 1)
89181155Sdas			cf = f1;
90290572Sngie		else
91181155Sdas			cf = f2;
92290572Sngie		ATF_CHECK_MSG(f == cf,
93290572Sngie		    "Test %d: (%s) vs. (%s) failed "
94290572Sngie		    "(should have returned %s)", i + 1, f1, f2,
95290572Sngie		    (test_fmts[i].correct == 1) ? "1st" : "2nd");
96181155Sdas	}
97181155Sdas}
98290572Sngie
99290572SngieATF_TP_ADD_TCS(tp)
100290572Sngie{
101290572Sngie
102290572Sngie	ATF_TP_ADD_TC(tp, fmtcheck_test);
103290572Sngie
104290572Sngie	return (atf_no_error());
105290572Sngie}
106