]>
git.saurik.com Git - apple/libc.git/blob - tests/netbsd_printf.c
1 /* $NetBSD: t_printf.c,v 1.8 2012/04/11 16:21:42 jruoho Exp $ */
4 * Copyright (c) 2010 The NetBSD Foundation, Inc.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
29 #include <sys/types.h>
30 #include <sys/resource.h>
38 #include "darwintest.h"
40 T_DECL(netbsd_snprintf_c99
, "Test printf(3) C99 conformance (PR lib/22019)")
44 (void)memset(s
, '\0', sizeof(s
));
45 (void)snprintf(s
, sizeof(s
), "%#.o", 0);
46 (void)printf("printf = %#.o\n", 0);
47 (void)fprintf(stderr
, "snprintf = %s", s
);
49 T_ASSERT_EQ(strlen(s
), (size_t)1, NULL
);
50 T_ASSERT_EQ(s
[0], '0', NULL
);
53 T_DECL(netbsd_snprintf_dotzero
, "PR lib/32951: %%.0f formats (0.0,0.5] to \"0.\"")
57 T_WITH_ERRNO
; T_EXPECT_EQ(snprintf(s
, sizeof(s
), "%.0f", 0.1), 1, NULL
);
58 T_EXPECT_EQ_STR(s
, "0", NULL
);
61 T_DECL(netbsd_snprintf_float
, "test that floating conversions don't leak memory")
72 rl
.rlim_cur
= rl
.rlim_max
= 1 * 1024 * 1024;
73 T_WITH_ERRNO
; T_EXPECT_NE(setrlimit(RLIMIT_AS
, &rl
), -1, NULL
);
74 rl
.rlim_cur
= rl
.rlim_max
= 1 * 1024 * 1024;
75 T_EXPECT_POSIX_SUCCESS(setrlimit(RLIMIT_DATA
, &rl
), NULL
);
78 srand((unsigned int)now
);
79 for (size_t i
= 0; i
< 10000; i
++) {
80 ul
= (uint32_t)rand();
81 uh
= (uint32_t)rand();
82 u
.bits
= (uint64_t)uh
<< 32 | ul
;
83 T_EXPECT_POSIX_SUCCESS(snprintf(buf
, sizeof buf
, " %.2f", u
.d
), NULL
);
87 T_DECL(netbsd_sprintf_zeropad
, "Test output format zero padding (PR lib/44113)")
91 T_WITH_ERRNO
; T_EXPECT_EQ(sprintf(str
, "%010f", 0.0), 10, NULL
);
92 T_EXPECT_EQ_STR(str
, "000.000000", NULL
);
96 /* printf(3) should ignore zero padding for nan/inf */
97 T_WITH_ERRNO
; T_EXPECT_EQ(sprintf(str
, "%010f", NAN
), 10, NULL
);
98 T_EXPECT_EQ_STR(str
, " nan", NULL
);
99 T_WITH_ERRNO
; T_EXPECT_EQ(sprintf(str
, "%010f", INFINITY
), 10, NULL
);
100 T_EXPECT_EQ_STR(str
, " inf", NULL
);