1272343Sngie/* $NetBSD: t_fmtcheck.c,v 1.3 2014/06/14 08:19:02 apb Exp $ */ 2272343Sngie 3272343Sngie/*- 4272343Sngie * Copyright (c) 2000 The NetBSD Foundation, Inc. 5272343Sngie * All rights reserved. 6272343Sngie * 7272343Sngie * This code was contributed to The NetBSD Foundation by Allen Briggs. 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 31272343Sngie#include <atf-c.h> 32272343Sngie 33272343Sngie#include <stdio.h> 34272343Sngie#include <stdlib.h> 35272343Sngie 36272343Sngieconst char *fmtcheck(const char *f1, const char *f2) 37272343Sngie __attribute__((__format_arg__(2))); 38272343Sngie 39272343Sngie#include <err.h> 40272343Sngie 41272343Sngiestruct test_fmt { 42272343Sngie const char *fmt1; 43272343Sngie const char *fmt2; 44272343Sngie int correct; 45272343Sngie} test_fmts[] = { 46272343Sngie { "%d", "%d", 1 }, 47272343Sngie { "%2d", "%2.2d", 1 }, 48272343Sngie { "%x", "%d", 1 }, 49272343Sngie { "%u", "%d", 1 }, 50272343Sngie { "%03d", "%d", 1 }, 51272343Sngie { "%-2d", "%d", 1 }, 52272343Sngie { "%d", "%-12.1d", 1 }, 53272343Sngie { "%d", "%-01.3d", 1 }, 54272343Sngie { "%X", "%-01.3d", 1 }, 55272343Sngie { "%D", "%ld", 1 }, 56272343Sngie { "%s", "%s", 1 }, 57272343Sngie { "%s", "This is a %s test", 1 }, 58272343Sngie { "Hi, there. This is a %s test", "%s", 1 }, 59272343Sngie { "%d", "%s", 2 }, 60272343Sngie { "%e", "%s", 2 }, 61272343Sngie { "%r", "%d", 2 }, 62272343Sngie { "%*.2d", "%*d", 1 }, 63272343Sngie { "%2.*d", "%*d", 2 }, 64272343Sngie { "%*d", "%*d", 1 }, 65272343Sngie { "%-3", "%d", 2 }, 66272343Sngie { "%d %s", "%d", 2 }, 67272343Sngie { "%*.*.*d", "%*.*.*d", 2 }, 68272343Sngie { "%d", "%d %s", 1 }, 69272343Sngie { "%40s", "%20s", 1 }, 70272343Sngie { "%x %x %x", "%o %u %d", 1 }, 71272343Sngie { "%o %u %d", "%x %x %X", 1 }, 72272343Sngie { "%#o %u %#-d", "%x %#x %X", 1 }, 73272343Sngie { "%qd", "%llx", 1 }, 74272343Sngie { "%%", "%llx", 1 }, 75272343Sngie { "%ld %30s %#llx %-10.*e", "This number %lu%% and string %s has %qd numbers and %.*g floats", 1 }, 76272343Sngie { "%o", "%lx", 2 }, 77272343Sngie { "%p", "%lu", 2 }, 78272343Sngie}; 79272343Sngie 80272343SngieATF_TC(fmtcheck_basic); 81272343SngieATF_TC_HEAD(fmtcheck_basic, tc) 82272343Sngie{ 83272343Sngie 84272343Sngie atf_tc_set_md_var(tc, "descr", "Test fmtcheck(3)"); 85272343Sngie} 86272343Sngie 87272343SngieATF_TC_BODY(fmtcheck_basic, tc) 88272343Sngie{ 89272343Sngie unsigned int i, r; 90272343Sngie const char *f, *cf, *f1, *f2; 91272343Sngie 92272343Sngie r = 0; 93272343Sngie for (i = 0 ; i < __arraycount(test_fmts); i++) { 94272343Sngie f1 = test_fmts[i].fmt1; 95272343Sngie f2 = test_fmts[i].fmt2; 96272343Sngie f = fmtcheck(f1, f2); 97272343Sngie if (test_fmts[i].correct == 1) { 98272343Sngie cf = f1; 99272343Sngie } else { 100272343Sngie cf = f2; 101272343Sngie } 102272343Sngie if (f != cf) { 103272343Sngie r++; 104272343Sngie atf_tc_fail_nonfatal("Test %d: (%s) vs. (%s) failed " 105272343Sngie "(should have returned %s)", i, f1, f2, 106272343Sngie (test_fmts[i].correct == 1) ? "1st" : "2nd"); 107272343Sngie } 108272343Sngie } 109272343Sngie} 110272343Sngie 111272343SngieATF_TP_ADD_TCS(tp) 112272343Sngie{ 113272343Sngie 114272343Sngie ATF_TP_ADD_TC(tp, fmtcheck_basic); 115272343Sngie 116272343Sngie return atf_no_error(); 117272343Sngie} 118