Path: blob/main/lib/libc/tests/stdio/print_positional_test.c
39530 views
/* $OpenBSD: sprintf_test.c,v 1.3 2004/09/16 20:22:26 otto Exp $ */12/*3* Copyright (c) 2003 Theo de Raadt4* All rights reserved.5*6* Redistribution and use in source and binary forms, with or without7* modification, are permitted provided that the following conditions8* are met:9*10* - Redistributions of source code must retain the above copyright11* notice, this list of conditions and the following disclaimer.12* - Redistributions in binary form must reproduce the above13* copyright notice, this list of conditions and the following14* disclaimer in the documentation and/or other materials provided15* with the distribution.16*17* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS18* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT19* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS20* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE21* COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,22* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,23* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;24* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER25* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT26* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN27* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE28* POSSIBILITY OF SUCH DAMAGE.29*/3031#include <sys/param.h>32#include <stdio.h>33#include <stdlib.h>34#include <string.h>35#include <wchar.h>3637#include <atf-c.h>3839const char correct[] =40"|xx 01 02 03 04\n"41"|xx 05 06 07 08\n"42"|xx 09 10 11 12\n"43"|xx 13 14 15 16\n"44"|xx 17 18 19 20\n"45"|xx 21 22 23 24\n"46"|xx 25 26 27 28\n"47"|xx 29 30 31 32\n"48"|xx 33 34 35 36\n"49"|xx 37 38 39 40\n"50"|xx 41 42 43 44\n"51"|xx 45 -1 1 -1 1\n";5253const char correct2[] =54"b bs BSD";55static char buf[1024];56static wchar_t wbuf1[1024], wbuf2[1024];57static const char *temp;5859ATF_TC_WITHOUT_HEAD(positional_normal);60ATF_TC_BODY(positional_normal, tc)61{6263/* Test positional arguments */64snprintf(buf, sizeof buf,65"|xx %1$s %2$s %3$s %4$s\n"66"|xx %5$s %6$s %7$s %8$s\n"67"|xx %9$s %10$s %11$s %12$s\n"68"|xx %13$s %14$s %15$s %16$s\n"69"|xx %17$s %18$s %19$s %20$s\n"70"|xx %21$s %22$s %23$s %24$s\n"71"|xx %25$s %26$s %27$s %28$s\n"72"|xx %29$s %30$s %31$s %32$s\n"73"|xx %33$s %34$s %35$s %36$s\n"74"|xx %37$s %38$s %39$s %40$s\n"75"|xx %41$s %42$s %43$s %44$s\n"76"|xx %45$d %46$ld %47$lld %48$d %49$lld\n",77"01", "02", "03", "04", "05", "06",78"07", "08", "09", "10", "11", "12",79"13", "14", "15", "16", "17", "18",80"19", "20", "21", "22", "23", "24",81"25", "26", "27", "28", "29", "30",82"31", "32", "33", "34", "35", "36",83"37", "38", "39", "40", "41", "42",84"43", "44", 45, -1L, 1LL, -1, 1LL85);86ATF_REQUIRE_MSG(wcscmp(wbuf1, wbuf2) == 0,87"buffers didn't match");88}8990ATF_TC_WITHOUT_HEAD(positional_wide);91ATF_TC_BODY(positional_wide, tc)92{9394swprintf(wbuf1, nitems(wbuf1),95L"|xx %1$s %2$s %3$s %4$s\n"96"|xx %5$s %6$s %7$s %8$s\n"97"|xx %9$s %10$s %11$s %12$s\n"98"|xx %13$s %14$s %15$s %16$s\n"99"|xx %17$s %18$s %19$s %20$s\n"100"|xx %21$s %22$s %23$s %24$s\n"101"|xx %25$s %26$s %27$s %28$s\n"102"|xx %29$s %30$s %31$s %32$s\n"103"|xx %33$s %34$s %35$s %36$s\n"104"|xx %37$s %38$s %39$s %40$s\n"105"|xx %41$s %42$s %43$s %44$s\n"106"|xx %45$d %46$ld %47$lld %48$d %49$lld\n",107"01", "02", "03", "04", "05", "06",108"07", "08", "09", "10", "11", "12",109"13", "14", "15", "16", "17", "18",110"19", "20", "21", "22", "23", "24",111"25", "26", "27", "28", "29", "30",112"31", "32", "33", "34", "35", "36",113"37", "38", "39", "40", "41", "42",114"43", "44", 45, -1L, 1LL, -1, 1LL115);116temp = correct;117mbsrtowcs(wbuf2, &temp, nitems(wbuf2), NULL);118ATF_REQUIRE_MSG(wcscmp(wbuf1, wbuf2) == 0,119"buffers didn't match");120}121122ATF_TC_WITHOUT_HEAD(positional_precision);123ATF_TC_BODY(positional_precision, tc)124{125126snprintf(buf, sizeof buf, "%2$.*4$s %2$.*3$s %1$s",127"BSD", "bsd", 2, 1);128ATF_REQUIRE_MSG(strcmp(buf, correct2) == 0,129"buffers didn't match");130}131132ATF_TC_WITHOUT_HEAD(positional_precision_wide);133ATF_TC_BODY(positional_precision_wide, tc)134{135136swprintf(wbuf1, sizeof buf, L"%2$.*4$s %2$.*3$s %1$s",137"BSD", "bsd", 2, 1);138temp = correct2;139mbsrtowcs(wbuf2, &temp, nitems(wbuf2), NULL);140ATF_REQUIRE_MSG(wcscmp(wbuf1, wbuf2) == 0,141"buffers didn't match");142}143144ATF_TP_ADD_TCS(tp)145{146147ATF_TP_ADD_TC(tp, positional_normal);148ATF_TP_ADD_TC(tp, positional_wide);149ATF_TP_ADD_TC(tp, positional_precision);150ATF_TP_ADD_TC(tp, positional_precision_wide);151152return (atf_no_error());153}154155156