1*57718be8SEnji Cooper /* $NetBSD: t_fmtcheck.c,v 1.3 2014/06/14 08:19:02 apb Exp $ */ 2*57718be8SEnji Cooper 3*57718be8SEnji Cooper /*- 4*57718be8SEnji Cooper * Copyright (c) 2000 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 Allen Briggs. 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 31*57718be8SEnji Cooper #include <atf-c.h> 32*57718be8SEnji Cooper 33*57718be8SEnji Cooper #include <stdio.h> 34*57718be8SEnji Cooper #include <stdlib.h> 35*57718be8SEnji Cooper 36*57718be8SEnji Cooper const char *fmtcheck(const char *f1, const char *f2) 37*57718be8SEnji Cooper __attribute__((__format_arg__(2))); 38*57718be8SEnji Cooper 39*57718be8SEnji Cooper #include <err.h> 40*57718be8SEnji Cooper 41*57718be8SEnji Cooper struct test_fmt { 42*57718be8SEnji Cooper const char *fmt1; 43*57718be8SEnji Cooper const char *fmt2; 44*57718be8SEnji Cooper int correct; 45*57718be8SEnji Cooper } test_fmts[] = { 46*57718be8SEnji Cooper { "%d", "%d", 1 }, 47*57718be8SEnji Cooper { "%2d", "%2.2d", 1 }, 48*57718be8SEnji Cooper { "%x", "%d", 1 }, 49*57718be8SEnji Cooper { "%u", "%d", 1 }, 50*57718be8SEnji Cooper { "%03d", "%d", 1 }, 51*57718be8SEnji Cooper { "%-2d", "%d", 1 }, 52*57718be8SEnji Cooper { "%d", "%-12.1d", 1 }, 53*57718be8SEnji Cooper { "%d", "%-01.3d", 1 }, 54*57718be8SEnji Cooper { "%X", "%-01.3d", 1 }, 55*57718be8SEnji Cooper { "%D", "%ld", 1 }, 56*57718be8SEnji Cooper { "%s", "%s", 1 }, 57*57718be8SEnji Cooper { "%s", "This is a %s test", 1 }, 58*57718be8SEnji Cooper { "Hi, there. This is a %s test", "%s", 1 }, 59*57718be8SEnji Cooper { "%d", "%s", 2 }, 60*57718be8SEnji Cooper { "%e", "%s", 2 }, 61*57718be8SEnji Cooper { "%r", "%d", 2 }, 62*57718be8SEnji Cooper { "%*.2d", "%*d", 1 }, 63*57718be8SEnji Cooper { "%2.*d", "%*d", 2 }, 64*57718be8SEnji Cooper { "%*d", "%*d", 1 }, 65*57718be8SEnji Cooper { "%-3", "%d", 2 }, 66*57718be8SEnji Cooper { "%d %s", "%d", 2 }, 67*57718be8SEnji Cooper { "%*.*.*d", "%*.*.*d", 2 }, 68*57718be8SEnji Cooper { "%d", "%d %s", 1 }, 69*57718be8SEnji Cooper { "%40s", "%20s", 1 }, 70*57718be8SEnji Cooper { "%x %x %x", "%o %u %d", 1 }, 71*57718be8SEnji Cooper { "%o %u %d", "%x %x %X", 1 }, 72*57718be8SEnji Cooper { "%#o %u %#-d", "%x %#x %X", 1 }, 73*57718be8SEnji Cooper { "%qd", "%llx", 1 }, 74*57718be8SEnji Cooper { "%%", "%llx", 1 }, 75*57718be8SEnji Cooper { "%ld %30s %#llx %-10.*e", "This number %lu%% and string %s has %qd numbers and %.*g floats", 1 }, 76*57718be8SEnji Cooper { "%o", "%lx", 2 }, 77*57718be8SEnji Cooper { "%p", "%lu", 2 }, 78*57718be8SEnji Cooper }; 79*57718be8SEnji Cooper 80*57718be8SEnji Cooper ATF_TC(fmtcheck_basic); 81*57718be8SEnji Cooper ATF_TC_HEAD(fmtcheck_basic, tc) 82*57718be8SEnji Cooper { 83*57718be8SEnji Cooper 84*57718be8SEnji Cooper atf_tc_set_md_var(tc, "descr", "Test fmtcheck(3)"); 85*57718be8SEnji Cooper } 86*57718be8SEnji Cooper 87*57718be8SEnji Cooper ATF_TC_BODY(fmtcheck_basic, tc) 88*57718be8SEnji Cooper { 89*57718be8SEnji Cooper unsigned int i, r; 90*57718be8SEnji Cooper const char *f, *cf, *f1, *f2; 91*57718be8SEnji Cooper 92*57718be8SEnji Cooper r = 0; 93*57718be8SEnji Cooper for (i = 0 ; i < __arraycount(test_fmts); i++) { 94*57718be8SEnji Cooper f1 = test_fmts[i].fmt1; 95*57718be8SEnji Cooper f2 = test_fmts[i].fmt2; 96*57718be8SEnji Cooper f = fmtcheck(f1, f2); 97*57718be8SEnji Cooper if (test_fmts[i].correct == 1) { 98*57718be8SEnji Cooper cf = f1; 99*57718be8SEnji Cooper } else { 100*57718be8SEnji Cooper cf = f2; 101*57718be8SEnji Cooper } 102*57718be8SEnji Cooper if (f != cf) { 103*57718be8SEnji Cooper r++; 104*57718be8SEnji Cooper atf_tc_fail_nonfatal("Test %d: (%s) vs. (%s) failed " 105*57718be8SEnji Cooper "(should have returned %s)", i, f1, f2, 106*57718be8SEnji Cooper (test_fmts[i].correct == 1) ? "1st" : "2nd"); 107*57718be8SEnji Cooper } 108*57718be8SEnji Cooper } 109*57718be8SEnji Cooper } 110*57718be8SEnji Cooper 111*57718be8SEnji Cooper ATF_TP_ADD_TCS(tp) 112*57718be8SEnji Cooper { 113*57718be8SEnji Cooper 114*57718be8SEnji Cooper ATF_TP_ADD_TC(tp, fmtcheck_basic); 115*57718be8SEnji Cooper 116*57718be8SEnji Cooper return atf_no_error(); 117*57718be8SEnji Cooper } 118