157718be8SEnji Cooper /* $NetBSD: t_printf.c,v 1.8 2012/04/11 16:21:42 jruoho Exp $ */
257718be8SEnji Cooper
357718be8SEnji Cooper /*-
457718be8SEnji Cooper * Copyright (c) 2010 The NetBSD Foundation, Inc.
557718be8SEnji Cooper * All rights reserved.
657718be8SEnji Cooper *
757718be8SEnji Cooper * Redistribution and use in source and binary forms, with or without
857718be8SEnji Cooper * modification, are permitted provided that the following conditions
957718be8SEnji Cooper * are met:
1057718be8SEnji Cooper * 1. Redistributions of source code must retain the above copyright
1157718be8SEnji Cooper * notice, this list of conditions and the following disclaimer.
1257718be8SEnji Cooper * 2. Redistributions in binary form must reproduce the above copyright
1357718be8SEnji Cooper * notice, this list of conditions and the following disclaimer in the
1457718be8SEnji Cooper * documentation and/or other materials provided with the distribution.
1557718be8SEnji Cooper *
1657718be8SEnji Cooper * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
1757718be8SEnji Cooper * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
1857718be8SEnji Cooper * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
1957718be8SEnji Cooper * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
2057718be8SEnji Cooper * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
2157718be8SEnji Cooper * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
2257718be8SEnji Cooper * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
2357718be8SEnji Cooper * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
2457718be8SEnji Cooper * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
2557718be8SEnji Cooper * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
2657718be8SEnji Cooper * POSSIBILITY OF SUCH DAMAGE.
2757718be8SEnji Cooper */
2857718be8SEnji Cooper
2957718be8SEnji Cooper #include <sys/types.h>
3057718be8SEnji Cooper #include <sys/resource.h>
3157718be8SEnji Cooper #include <atf-c.h>
3257718be8SEnji Cooper #include <math.h>
3357718be8SEnji Cooper #include <stdio.h>
3457718be8SEnji Cooper #include <stdint.h>
3557718be8SEnji Cooper #include <string.h>
3657718be8SEnji Cooper #include <time.h>
3757718be8SEnji Cooper #include <stdlib.h>
3857718be8SEnji Cooper
3957718be8SEnji Cooper ATF_TC(snprintf_c99);
ATF_TC_HEAD(snprintf_c99,tc)4057718be8SEnji Cooper ATF_TC_HEAD(snprintf_c99, tc)
4157718be8SEnji Cooper {
4257718be8SEnji Cooper
4357718be8SEnji Cooper atf_tc_set_md_var(tc, "descr",
4457718be8SEnji Cooper "Test printf(3) C99 conformance (PR lib/22019)");
4557718be8SEnji Cooper }
4657718be8SEnji Cooper
ATF_TC_BODY(snprintf_c99,tc)4757718be8SEnji Cooper ATF_TC_BODY(snprintf_c99, tc)
4857718be8SEnji Cooper {
4957718be8SEnji Cooper char s[4];
5057718be8SEnji Cooper
5157718be8SEnji Cooper (void)memset(s, '\0', sizeof(s));
5257718be8SEnji Cooper (void)snprintf(s, sizeof(s), "%#.o", 0);
5357718be8SEnji Cooper (void)printf("printf = %#.o\n", 0);
5457718be8SEnji Cooper (void)fprintf(stderr, "snprintf = %s", s);
5557718be8SEnji Cooper
5657718be8SEnji Cooper ATF_REQUIRE(strlen(s) == 1);
5757718be8SEnji Cooper ATF_REQUIRE(s[0] == '0');
5857718be8SEnji Cooper }
5957718be8SEnji Cooper
6057718be8SEnji Cooper ATF_TC(snprintf_dotzero);
ATF_TC_HEAD(snprintf_dotzero,tc)6157718be8SEnji Cooper ATF_TC_HEAD(snprintf_dotzero, tc)
6257718be8SEnji Cooper {
6357718be8SEnji Cooper
6457718be8SEnji Cooper atf_tc_set_md_var(tc, "descr",
6557718be8SEnji Cooper "PR lib/32951: %%.0f formats (0.0,0.5] to \"0.\"");
6657718be8SEnji Cooper }
6757718be8SEnji Cooper
ATF_TC_BODY(snprintf_dotzero,tc)6857718be8SEnji Cooper ATF_TC_BODY(snprintf_dotzero, tc)
6957718be8SEnji Cooper {
7057718be8SEnji Cooper char s[4];
7157718be8SEnji Cooper
7257718be8SEnji Cooper ATF_CHECK(snprintf(s, sizeof(s), "%.0f", 0.1) == 1);
7357718be8SEnji Cooper ATF_REQUIRE_STREQ(s, "0");
7457718be8SEnji Cooper }
7557718be8SEnji Cooper
7657718be8SEnji Cooper ATF_TC(snprintf_posarg);
ATF_TC_HEAD(snprintf_posarg,tc)7757718be8SEnji Cooper ATF_TC_HEAD(snprintf_posarg, tc)
7857718be8SEnji Cooper {
7957718be8SEnji Cooper
8057718be8SEnji Cooper atf_tc_set_md_var(tc, "descr", "test for positional arguments");
8157718be8SEnji Cooper }
8257718be8SEnji Cooper
ATF_TC_BODY(snprintf_posarg,tc)8357718be8SEnji Cooper ATF_TC_BODY(snprintf_posarg, tc)
8457718be8SEnji Cooper {
8557718be8SEnji Cooper char s[16];
8657718be8SEnji Cooper
8757718be8SEnji Cooper ATF_CHECK(snprintf(s, sizeof(s), "%1$d", -23) == 3);
8857718be8SEnji Cooper ATF_REQUIRE_STREQ(s, "-23");
8957718be8SEnji Cooper }
9057718be8SEnji Cooper
9157718be8SEnji Cooper ATF_TC(snprintf_posarg_width);
ATF_TC_HEAD(snprintf_posarg_width,tc)9257718be8SEnji Cooper ATF_TC_HEAD(snprintf_posarg_width, tc)
9357718be8SEnji Cooper {
9457718be8SEnji Cooper
9557718be8SEnji Cooper atf_tc_set_md_var(tc, "descr", "test for positional arguments with "
9657718be8SEnji Cooper "field width");
9757718be8SEnji Cooper }
9857718be8SEnji Cooper
ATF_TC_BODY(snprintf_posarg_width,tc)9957718be8SEnji Cooper ATF_TC_BODY(snprintf_posarg_width, tc)
10057718be8SEnji Cooper {
10157718be8SEnji Cooper char s[16];
10257718be8SEnji Cooper
10357718be8SEnji Cooper ATF_CHECK(snprintf(s, sizeof(s), "%1$*2$d", -23, 4) == 4);
10457718be8SEnji Cooper ATF_REQUIRE_STREQ(s, " -23");
10557718be8SEnji Cooper }
10657718be8SEnji Cooper
10757718be8SEnji Cooper ATF_TC(snprintf_posarg_error);
ATF_TC_HEAD(snprintf_posarg_error,tc)10857718be8SEnji Cooper ATF_TC_HEAD(snprintf_posarg_error, tc)
10957718be8SEnji Cooper {
11057718be8SEnji Cooper
11157718be8SEnji Cooper atf_tc_set_md_var(tc, "descr", "test for positional arguments out "
11257718be8SEnji Cooper "of bounds");
11357718be8SEnji Cooper }
11457718be8SEnji Cooper
ATF_TC_BODY(snprintf_posarg_error,tc)11557718be8SEnji Cooper ATF_TC_BODY(snprintf_posarg_error, tc)
11657718be8SEnji Cooper {
11757718be8SEnji Cooper char s[16], fmt[32];
11857718be8SEnji Cooper
11957718be8SEnji Cooper snprintf(fmt, sizeof(fmt), "%%%zu$d", SIZE_MAX / sizeof(size_t));
12057718be8SEnji Cooper
12157718be8SEnji Cooper ATF_CHECK(snprintf(s, sizeof(s), fmt, -23) == -1);
12257718be8SEnji Cooper }
12357718be8SEnji Cooper
12457718be8SEnji Cooper ATF_TC(snprintf_float);
ATF_TC_HEAD(snprintf_float,tc)12557718be8SEnji Cooper ATF_TC_HEAD(snprintf_float, tc)
12657718be8SEnji Cooper {
12757718be8SEnji Cooper
12857718be8SEnji Cooper atf_tc_set_md_var(tc, "descr", "test that floating conversions don't"
12957718be8SEnji Cooper " leak memory");
130*1c227d00SEnji Cooper #ifdef __FreeBSD__
131*1c227d00SEnji Cooper atf_tc_set_md_var(tc, "require.memory", "64m");
132*1c227d00SEnji Cooper atf_tc_set_md_var(tc, "require.user", "root");
133*1c227d00SEnji Cooper #endif
13457718be8SEnji Cooper }
13557718be8SEnji Cooper
ATF_TC_BODY(snprintf_float,tc)13657718be8SEnji Cooper ATF_TC_BODY(snprintf_float, tc)
13757718be8SEnji Cooper {
13857718be8SEnji Cooper union {
13957718be8SEnji Cooper double d;
14057718be8SEnji Cooper uint64_t bits;
14157718be8SEnji Cooper } u;
14257718be8SEnji Cooper uint32_t ul, uh;
14357718be8SEnji Cooper time_t now;
14457718be8SEnji Cooper char buf[1000];
14557718be8SEnji Cooper struct rlimit rl;
14657718be8SEnji Cooper
1471f73e6d8SEnji Cooper #ifdef __FreeBSD__
148*1c227d00SEnji Cooper rl.rlim_cur = rl.rlim_max = 32 * 1024 * 1024;
149*1c227d00SEnji Cooper ATF_CHECK(setrlimit(RLIMIT_AS, &rl) != -1);
150*1c227d00SEnji Cooper rl.rlim_cur = rl.rlim_max = 32 * 1024 * 1024;
151*1c227d00SEnji Cooper ATF_CHECK(setrlimit(RLIMIT_DATA, &rl) != -1);
152*1c227d00SEnji Cooper #else
15357718be8SEnji Cooper rl.rlim_cur = rl.rlim_max = 1 * 1024 * 1024;
15457718be8SEnji Cooper ATF_CHECK(setrlimit(RLIMIT_AS, &rl) != -1);
15557718be8SEnji Cooper rl.rlim_cur = rl.rlim_max = 1 * 1024 * 1024;
15657718be8SEnji Cooper ATF_CHECK(setrlimit(RLIMIT_DATA, &rl) != -1);
157*1c227d00SEnji Cooper #endif
15857718be8SEnji Cooper
15957718be8SEnji Cooper time(&now);
16057718be8SEnji Cooper srand(now);
16157718be8SEnji Cooper for (size_t i = 0; i < 10000; i++) {
16257718be8SEnji Cooper ul = rand();
16357718be8SEnji Cooper uh = rand();
16457718be8SEnji Cooper u.bits = (uint64_t)uh << 32 | ul;
16557718be8SEnji Cooper ATF_CHECK(snprintf(buf, sizeof buf, " %.2f", u.d) != -1);
16657718be8SEnji Cooper }
16757718be8SEnji Cooper }
16857718be8SEnji Cooper
16957718be8SEnji Cooper ATF_TC(sprintf_zeropad);
ATF_TC_HEAD(sprintf_zeropad,tc)17057718be8SEnji Cooper ATF_TC_HEAD(sprintf_zeropad, tc)
17157718be8SEnji Cooper {
17257718be8SEnji Cooper atf_tc_set_md_var(tc, "descr",
17357718be8SEnji Cooper "Test output format zero padding (PR lib/44113)");
17457718be8SEnji Cooper }
17557718be8SEnji Cooper
ATF_TC_BODY(sprintf_zeropad,tc)17657718be8SEnji Cooper ATF_TC_BODY(sprintf_zeropad, tc)
17757718be8SEnji Cooper {
17857718be8SEnji Cooper char str[1024];
17957718be8SEnji Cooper
18057718be8SEnji Cooper ATF_CHECK(sprintf(str, "%010f", 0.0) == 10);
18157718be8SEnji Cooper ATF_REQUIRE_STREQ(str, "000.000000");
18257718be8SEnji Cooper
18357718be8SEnji Cooper /* ieeefp */
18457718be8SEnji Cooper #ifndef __vax__
18557718be8SEnji Cooper /* printf(3) should ignore zero padding for nan/inf */
18657718be8SEnji Cooper ATF_CHECK(sprintf(str, "%010f", NAN) == 10);
18757718be8SEnji Cooper ATF_REQUIRE_STREQ(str, " nan");
18857718be8SEnji Cooper ATF_CHECK(sprintf(str, "%010f", INFINITY) == 10);
18957718be8SEnji Cooper ATF_REQUIRE_STREQ(str, " inf");
19057718be8SEnji Cooper #endif
19157718be8SEnji Cooper }
19257718be8SEnji Cooper
ATF_TP_ADD_TCS(tp)19357718be8SEnji Cooper ATF_TP_ADD_TCS(tp)
19457718be8SEnji Cooper {
19557718be8SEnji Cooper
19657718be8SEnji Cooper ATF_TP_ADD_TC(tp, snprintf_c99);
19757718be8SEnji Cooper ATF_TP_ADD_TC(tp, snprintf_dotzero);
19857718be8SEnji Cooper ATF_TP_ADD_TC(tp, snprintf_posarg);
19957718be8SEnji Cooper ATF_TP_ADD_TC(tp, snprintf_posarg_width);
20057718be8SEnji Cooper ATF_TP_ADD_TC(tp, snprintf_posarg_error);
20157718be8SEnji Cooper ATF_TP_ADD_TC(tp, snprintf_float);
20257718be8SEnji Cooper ATF_TP_ADD_TC(tp, sprintf_zeropad);
20357718be8SEnji Cooper
20457718be8SEnji Cooper return atf_no_error();
20557718be8SEnji Cooper }
206