xref: /openbsd-src/regress/lib/libc/sprintf/sprintf_test.c (revision e7277155d40e32499ad52cd47be1ceb60122b00c)
1*e7277155Sderaadt /*	$OpenBSD: sprintf_test.c,v 1.5 2021/09/02 09:46:21 deraadt Exp $ */
2cd37c4bbSderaadt 
3cd37c4bbSderaadt /*
4cd37c4bbSderaadt  * Copyright (c) 2003 Theo de Raadt
5cd37c4bbSderaadt  * All rights reserved.
6cd37c4bbSderaadt  *
7cd37c4bbSderaadt  * Redistribution and use in source and binary forms, with or without
8cd37c4bbSderaadt  * modification, are permitted provided that the following conditions
9cd37c4bbSderaadt  * are met:
10cd37c4bbSderaadt  *
11cd37c4bbSderaadt  *    - Redistributions of source code must retain the above copyright
12cd37c4bbSderaadt  *      notice, this list of conditions and the following disclaimer.
13cd37c4bbSderaadt  *    - Redistributions in binary form must reproduce the above
14cd37c4bbSderaadt  *      copyright notice, this list of conditions and the following
15cd37c4bbSderaadt  *      disclaimer in the documentation and/or other materials provided
16cd37c4bbSderaadt  *      with the distribution.
17cd37c4bbSderaadt  *
18cd37c4bbSderaadt  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19cd37c4bbSderaadt  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20cd37c4bbSderaadt  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
21cd37c4bbSderaadt  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
22cd37c4bbSderaadt  * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
23cd37c4bbSderaadt  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
24cd37c4bbSderaadt  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25cd37c4bbSderaadt  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
26cd37c4bbSderaadt  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27cd37c4bbSderaadt  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
28cd37c4bbSderaadt  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29cd37c4bbSderaadt  * POSSIBILITY OF SUCH DAMAGE.
30cd37c4bbSderaadt  */
31cd37c4bbSderaadt 
32c672277dSotto #include <stddef.h>
33cd37c4bbSderaadt #include <stdio.h>
34cd37c4bbSderaadt #include <stdlib.h>
351821443cSdavid #include <string.h>
36cd37c4bbSderaadt #include <signal.h>
37cd37c4bbSderaadt 
38cd37c4bbSderaadt char correct[] =
39cd37c4bbSderaadt 	"|xx 01 02 03 04\n"
40cd37c4bbSderaadt 	"|xx 05 06 07 08\n"
41cd37c4bbSderaadt 	"|xx 09 10 11 12\n"
42cd37c4bbSderaadt 	"|xx 13 14 15 16\n"
43cd37c4bbSderaadt 	"|xx 17 18 19 20\n"
44cd37c4bbSderaadt 	"|xx 21 22 23 24\n"
45cd37c4bbSderaadt 	"|xx 25 26 27 28\n"
46cd37c4bbSderaadt 	"|xx 29 30 31 32\n"
47cd37c4bbSderaadt 	"|xx 33 34 35 36\n"
48cd37c4bbSderaadt 	"|xx 37 38 39 40\n"
4938449f02Sotto 	"|xx 41 42 43 44\n"
5038449f02Sotto 	"|xx 45 -1 1 -1 1\n";
51cd37c4bbSderaadt 
52c672277dSotto char correct2[] =
53c672277dSotto 	"1 0 -1 1 1 2 1 3 -1 4 1  \n"
54c672277dSotto 	"1 -1 1 1 -1 1  \n";
55c672277dSotto 
56cd37c4bbSderaadt int
main(int argc,char * argv[])57cd37c4bbSderaadt main(int argc, char *argv[])
58cd37c4bbSderaadt {
59cd37c4bbSderaadt 	char buf[1024];
60c672277dSotto 	size_t sz1, sz2, sz3, sz4;
61c672277dSotto 	ptrdiff_t p1, p2, p3, p4;
62cd37c4bbSderaadt 
63cd37c4bbSderaadt 	/* Test positional arguments */
64cd37c4bbSderaadt 	snprintf(buf, sizeof buf,
65cd37c4bbSderaadt 	    "|xx %1$s %2$s %3$s %4$s\n"
66cd37c4bbSderaadt 	    "|xx %5$s %6$s %7$s %8$s\n"
67cd37c4bbSderaadt 	    "|xx %9$s %10$s %11$s %12$s\n"
68cd37c4bbSderaadt 	    "|xx %13$s %14$s %15$s %16$s\n"
69cd37c4bbSderaadt 	    "|xx %17$s %18$s %19$s %20$s\n"
70cd37c4bbSderaadt 	    "|xx %21$s %22$s %23$s %24$s\n"
71cd37c4bbSderaadt 	    "|xx %25$s %26$s %27$s %28$s\n"
72cd37c4bbSderaadt 	    "|xx %29$s %30$s %31$s %32$s\n"
73cd37c4bbSderaadt 	    "|xx %33$s %34$s %35$s %36$s\n"
74cd37c4bbSderaadt 	    "|xx %37$s %38$s %39$s %40$s\n"
7538449f02Sotto 	    "|xx %41$s %42$s %43$s %44$s\n"
7638449f02Sotto 	    "|xx %45$d %46$ld %47$lld %48$d %49$lld\n",
77cd37c4bbSderaadt 	    "01", "02", "03", "04", "05", "06",
78cd37c4bbSderaadt 	    "07", "08", "09", "10", "11", "12",
79cd37c4bbSderaadt 	    "13", "14", "15", "16", "17", "18",
80cd37c4bbSderaadt 	    "19", "20", "21", "22", "23", "24",
81cd37c4bbSderaadt 	    "25", "26", "27", "28", "29", "30",
82cd37c4bbSderaadt 	    "31", "32", "33", "34", "35", "36",
83cd37c4bbSderaadt 	    "37", "38", "39", "40", "41", "42",
8438449f02Sotto 	    "43", "44", 45, -1L, 1LL, -1, 1LL
85cd37c4bbSderaadt 	    );
86cd37c4bbSderaadt 
87c672277dSotto 	if (strcmp(buf, correct) != 0)
88cd37c4bbSderaadt 		exit(1);
89c672277dSotto 	exit(0);
90cd37c4bbSderaadt }
91