11ee02192SEnji Cooper /* $OpenBSD: sprintf_test.c,v 1.3 2004/09/16 20:22:26 otto Exp $ */
21ee02192SEnji Cooper
31ee02192SEnji Cooper /*
41ee02192SEnji Cooper * Copyright (c) 2003 Theo de Raadt
51ee02192SEnji Cooper * All rights reserved.
61ee02192SEnji Cooper *
71ee02192SEnji Cooper * Redistribution and use in source and binary forms, with or without
81ee02192SEnji Cooper * modification, are permitted provided that the following conditions
91ee02192SEnji Cooper * are met:
101ee02192SEnji Cooper *
111ee02192SEnji Cooper * - Redistributions of source code must retain the above copyright
121ee02192SEnji Cooper * notice, this list of conditions and the following disclaimer.
131ee02192SEnji Cooper * - Redistributions in binary form must reproduce the above
141ee02192SEnji Cooper * copyright notice, this list of conditions and the following
151ee02192SEnji Cooper * disclaimer in the documentation and/or other materials provided
161ee02192SEnji Cooper * with the distribution.
171ee02192SEnji Cooper *
181ee02192SEnji Cooper * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
191ee02192SEnji Cooper * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
201ee02192SEnji Cooper * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
211ee02192SEnji Cooper * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
221ee02192SEnji Cooper * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
231ee02192SEnji Cooper * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
241ee02192SEnji Cooper * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
251ee02192SEnji Cooper * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
261ee02192SEnji Cooper * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
271ee02192SEnji Cooper * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
281ee02192SEnji Cooper * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
291ee02192SEnji Cooper * POSSIBILITY OF SUCH DAMAGE.
301ee02192SEnji Cooper */
311ee02192SEnji Cooper
32*83095e1eSConrad Meyer #include <sys/param.h>
331ee02192SEnji Cooper #include <stdio.h>
341ee02192SEnji Cooper #include <stdlib.h>
351ee02192SEnji Cooper #include <string.h>
361ee02192SEnji Cooper #include <wchar.h>
371ee02192SEnji Cooper
384c05e4eaSEnji Cooper #include <atf-c.h>
394c05e4eaSEnji Cooper
401ee02192SEnji Cooper const char correct[] =
411ee02192SEnji Cooper "|xx 01 02 03 04\n"
421ee02192SEnji Cooper "|xx 05 06 07 08\n"
431ee02192SEnji Cooper "|xx 09 10 11 12\n"
441ee02192SEnji Cooper "|xx 13 14 15 16\n"
451ee02192SEnji Cooper "|xx 17 18 19 20\n"
461ee02192SEnji Cooper "|xx 21 22 23 24\n"
471ee02192SEnji Cooper "|xx 25 26 27 28\n"
481ee02192SEnji Cooper "|xx 29 30 31 32\n"
491ee02192SEnji Cooper "|xx 33 34 35 36\n"
501ee02192SEnji Cooper "|xx 37 38 39 40\n"
511ee02192SEnji Cooper "|xx 41 42 43 44\n"
521ee02192SEnji Cooper "|xx 45 -1 1 -1 1\n";
531ee02192SEnji Cooper
541ee02192SEnji Cooper const char correct2[] =
551ee02192SEnji Cooper "b bs BSD";
564c05e4eaSEnji Cooper static char buf[1024];
574c05e4eaSEnji Cooper static wchar_t wbuf1[1024], wbuf2[1024];
584c05e4eaSEnji Cooper static const char *temp;
591ee02192SEnji Cooper
604c05e4eaSEnji Cooper ATF_TC_WITHOUT_HEAD(positional_normal);
ATF_TC_BODY(positional_normal,tc)614c05e4eaSEnji Cooper ATF_TC_BODY(positional_normal, tc)
621ee02192SEnji Cooper {
631ee02192SEnji Cooper
641ee02192SEnji Cooper /* Test positional arguments */
651ee02192SEnji Cooper snprintf(buf, sizeof buf,
661ee02192SEnji Cooper "|xx %1$s %2$s %3$s %4$s\n"
671ee02192SEnji Cooper "|xx %5$s %6$s %7$s %8$s\n"
681ee02192SEnji Cooper "|xx %9$s %10$s %11$s %12$s\n"
691ee02192SEnji Cooper "|xx %13$s %14$s %15$s %16$s\n"
701ee02192SEnji Cooper "|xx %17$s %18$s %19$s %20$s\n"
711ee02192SEnji Cooper "|xx %21$s %22$s %23$s %24$s\n"
721ee02192SEnji Cooper "|xx %25$s %26$s %27$s %28$s\n"
731ee02192SEnji Cooper "|xx %29$s %30$s %31$s %32$s\n"
741ee02192SEnji Cooper "|xx %33$s %34$s %35$s %36$s\n"
751ee02192SEnji Cooper "|xx %37$s %38$s %39$s %40$s\n"
761ee02192SEnji Cooper "|xx %41$s %42$s %43$s %44$s\n"
771ee02192SEnji Cooper "|xx %45$d %46$ld %47$lld %48$d %49$lld\n",
781ee02192SEnji Cooper "01", "02", "03", "04", "05", "06",
791ee02192SEnji Cooper "07", "08", "09", "10", "11", "12",
801ee02192SEnji Cooper "13", "14", "15", "16", "17", "18",
811ee02192SEnji Cooper "19", "20", "21", "22", "23", "24",
821ee02192SEnji Cooper "25", "26", "27", "28", "29", "30",
831ee02192SEnji Cooper "31", "32", "33", "34", "35", "36",
841ee02192SEnji Cooper "37", "38", "39", "40", "41", "42",
851ee02192SEnji Cooper "43", "44", 45, -1L, 1LL, -1, 1LL
861ee02192SEnji Cooper );
874c05e4eaSEnji Cooper ATF_REQUIRE_MSG(wcscmp(wbuf1, wbuf2) == 0,
884c05e4eaSEnji Cooper "buffers didn't match");
894c05e4eaSEnji Cooper }
904c05e4eaSEnji Cooper
914c05e4eaSEnji Cooper ATF_TC_WITHOUT_HEAD(positional_wide);
ATF_TC_BODY(positional_wide,tc)924c05e4eaSEnji Cooper ATF_TC_BODY(positional_wide, tc)
934c05e4eaSEnji Cooper {
941ee02192SEnji Cooper
95*83095e1eSConrad Meyer swprintf(wbuf1, nitems(wbuf1),
961ee02192SEnji Cooper L"|xx %1$s %2$s %3$s %4$s\n"
971ee02192SEnji Cooper "|xx %5$s %6$s %7$s %8$s\n"
981ee02192SEnji Cooper "|xx %9$s %10$s %11$s %12$s\n"
991ee02192SEnji Cooper "|xx %13$s %14$s %15$s %16$s\n"
1001ee02192SEnji Cooper "|xx %17$s %18$s %19$s %20$s\n"
1011ee02192SEnji Cooper "|xx %21$s %22$s %23$s %24$s\n"
1021ee02192SEnji Cooper "|xx %25$s %26$s %27$s %28$s\n"
1031ee02192SEnji Cooper "|xx %29$s %30$s %31$s %32$s\n"
1041ee02192SEnji Cooper "|xx %33$s %34$s %35$s %36$s\n"
1051ee02192SEnji Cooper "|xx %37$s %38$s %39$s %40$s\n"
1061ee02192SEnji Cooper "|xx %41$s %42$s %43$s %44$s\n"
1071ee02192SEnji Cooper "|xx %45$d %46$ld %47$lld %48$d %49$lld\n",
1081ee02192SEnji Cooper "01", "02", "03", "04", "05", "06",
1091ee02192SEnji Cooper "07", "08", "09", "10", "11", "12",
1101ee02192SEnji Cooper "13", "14", "15", "16", "17", "18",
1111ee02192SEnji Cooper "19", "20", "21", "22", "23", "24",
1121ee02192SEnji Cooper "25", "26", "27", "28", "29", "30",
1131ee02192SEnji Cooper "31", "32", "33", "34", "35", "36",
1141ee02192SEnji Cooper "37", "38", "39", "40", "41", "42",
1151ee02192SEnji Cooper "43", "44", 45, -1L, 1LL, -1, 1LL
1161ee02192SEnji Cooper );
1171ee02192SEnji Cooper temp = correct;
118*83095e1eSConrad Meyer mbsrtowcs(wbuf2, &temp, nitems(wbuf2), NULL);
1194c05e4eaSEnji Cooper ATF_REQUIRE_MSG(wcscmp(wbuf1, wbuf2) == 0,
1204c05e4eaSEnji Cooper "buffers didn't match");
1214c05e4eaSEnji Cooper }
1224c05e4eaSEnji Cooper
1234c05e4eaSEnji Cooper ATF_TC_WITHOUT_HEAD(positional_precision);
ATF_TC_BODY(positional_precision,tc)1244c05e4eaSEnji Cooper ATF_TC_BODY(positional_precision, tc)
1254c05e4eaSEnji Cooper {
1261ee02192SEnji Cooper
1271ee02192SEnji Cooper snprintf(buf, sizeof buf, "%2$.*4$s %2$.*3$s %1$s",
1281ee02192SEnji Cooper "BSD", "bsd", 2, 1);
1294c05e4eaSEnji Cooper ATF_REQUIRE_MSG(strcmp(buf, correct2) == 0,
1304c05e4eaSEnji Cooper "buffers didn't match");
1314c05e4eaSEnji Cooper }
1324c05e4eaSEnji Cooper
1334c05e4eaSEnji Cooper ATF_TC_WITHOUT_HEAD(positional_precision_wide);
ATF_TC_BODY(positional_precision_wide,tc)1344c05e4eaSEnji Cooper ATF_TC_BODY(positional_precision_wide, tc)
1354c05e4eaSEnji Cooper {
1361ee02192SEnji Cooper
1371ee02192SEnji Cooper swprintf(wbuf1, sizeof buf, L"%2$.*4$s %2$.*3$s %1$s",
1381ee02192SEnji Cooper "BSD", "bsd", 2, 1);
1391ee02192SEnji Cooper temp = correct2;
140*83095e1eSConrad Meyer mbsrtowcs(wbuf2, &temp, nitems(wbuf2), NULL);
1414c05e4eaSEnji Cooper ATF_REQUIRE_MSG(wcscmp(wbuf1, wbuf2) == 0,
1424c05e4eaSEnji Cooper "buffers didn't match");
1434c05e4eaSEnji Cooper }
1441ee02192SEnji Cooper
ATF_TP_ADD_TCS(tp)1454c05e4eaSEnji Cooper ATF_TP_ADD_TCS(tp)
1464c05e4eaSEnji Cooper {
1474c05e4eaSEnji Cooper
1484c05e4eaSEnji Cooper ATF_TP_ADD_TC(tp, positional_normal);
1494c05e4eaSEnji Cooper ATF_TP_ADD_TC(tp, positional_wide);
1504c05e4eaSEnji Cooper ATF_TP_ADD_TC(tp, positional_precision);
1514c05e4eaSEnji Cooper ATF_TP_ADD_TC(tp, positional_precision_wide);
1524c05e4eaSEnji Cooper
1534c05e4eaSEnji Cooper return (atf_no_error());
1541ee02192SEnji Cooper }
155