Path: blob/main/lib/libc/tests/gen/fmtcheck_test.c
39500 views
/* $NetBSD: tfmtcheck.c,v 1.3 2008/04/28 20:23:04 martin Exp $ */12/*-3* Copyright (c) 2000 The NetBSD Foundation, Inc.4* All rights reserved.5*6* This code was contributed to The NetBSD Foundation by Allen Briggs.7*8* Redistribution and use in source and binary forms, with or without9* modification, are permitted provided that the following conditions10* are met:11* 1. Redistributions of source code must retain the above copyright12* notice, this list of conditions and the following disclaimer.13* 2. Redistributions in binary form must reproduce the above copyright14* notice, this list of conditions and the following disclaimer in the15* documentation and/or other materials provided with the distribution.16*17* THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS18* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED19* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR20* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS21* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR22* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF23* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS24* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN25* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)26* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE27* POSSIBILITY OF SUCH DAMAGE.28*/2930#include <sys/param.h>31#include <err.h>32#include <stdio.h>33#include <stdlib.h>3435#include <atf-c.h>3637struct test_fmt {38char *fmt1;39char *fmt2;40int correct;41} test_fmts[] = {42{ "%d", "%d", 1 },43{ "%2d", "%2.2d", 1 },44{ "%x", "%d", 1 },45{ "%u", "%d", 1 },46{ "%03d", "%d", 1 },47{ "%-2d", "%d", 1 },48{ "%d", "%-12.1d", 1 },49{ "%d", "%-01.3d", 1 },50{ "%X", "%-01.3d", 1 },51{ "%D", "%ld", 1 },52{ "%s", "%s", 1 },53{ "%s", "This is a %s test", 1 },54{ "Hi, there. This is a %s test", "%s", 1 },55{ "%d", "%s", 2 },56{ "%e", "%s", 2 },57{ "%r", "%d", 2 },58{ "%*.2d", "%*d", 1 },59{ "%2.*d", "%*d", 2 },60{ "%*d", "%*d", 1 },61{ "%-3", "%d", 2 },62{ "%d %s", "%d", 2 },63{ "%*.*.*d", "%*.*.*d", 2 },64{ "%d", "%d %s", 1 },65{ "%40s", "%20s", 1 },66{ "%x %x %x", "%o %u %d", 1 },67{ "%o %u %d", "%x %x %X", 1 },68{ "%#o %u %#-d", "%x %#x %X", 1 },69{ "%qd", "%llx", 1 },70{ "%%", "%llx", 1 },71{ "%p %30s %#llx %-10.*e", "This number %lu%% and string %s has %qd numbers and %.*g floats", 1 },72};7374ATF_TC_WITHOUT_HEAD(fmtcheck_test);75ATF_TC_BODY(fmtcheck_test, tc)76{77int i;78const char *f, *cf, *f1, *f2;7980for (i = 0; i < nitems(test_fmts); i++) {81f1 = test_fmts[i].fmt1;82f2 = test_fmts[i].fmt2;83f = fmtcheck(f1, f2);84if (test_fmts[i].correct == 1)85cf = f1;86else87cf = f2;88ATF_CHECK_MSG(f == cf,89"Test %d: (%s) vs. (%s) failed "90"(should have returned %s)", i + 1, f1, f2,91(test_fmts[i].correct == 1) ? "1st" : "2nd");92}93}9495ATF_TP_ADD_TCS(tp)96{9798ATF_TP_ADD_TC(tp, fmtcheck_test);99100return (atf_no_error());101}102103104