]>
Commit | Line | Data |
---|---|---|
b061a43b A |
1 | /* $NetBSD: t_printf.c,v 1.8 2012/04/11 16:21:42 jruoho Exp $ */ |
2 | ||
3 | /*- | |
4 | * Copyright (c) 2010 The NetBSD Foundation, Inc. | |
5 | * All rights reserved. | |
6 | * | |
7 | * Redistribution and use in source and binary forms, with or without | |
8 | * modification, are permitted provided that the following conditions | |
9 | * are met: | |
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. | |
15 | * | |
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. | |
27 | */ | |
28 | ||
29 | #include <sys/types.h> | |
30 | #include <sys/resource.h> | |
31 | #include <math.h> | |
32 | #include <stdio.h> | |
33 | #include <stdint.h> | |
34 | #include <string.h> | |
35 | #include <time.h> | |
36 | #include <stdlib.h> | |
37 | ||
38 | #include "darwintest.h" | |
39 | ||
40 | T_DECL(netbsd_snprintf_c99, "Test printf(3) C99 conformance (PR lib/22019)") | |
41 | { | |
42 | char s[4]; | |
43 | ||
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); | |
48 | ||
49 | T_ASSERT_EQ(strlen(s), (size_t)1, NULL); | |
50 | T_ASSERT_EQ(s[0], '0', NULL); | |
51 | } | |
52 | ||
53 | T_DECL(netbsd_snprintf_dotzero, "PR lib/32951: %%.0f formats (0.0,0.5] to \"0.\"") | |
54 | { | |
55 | char s[4]; | |
56 | ||
57 | T_WITH_ERRNO; T_EXPECT_EQ(snprintf(s, sizeof(s), "%.0f", 0.1), 1, NULL); | |
58 | T_EXPECT_EQ_STR(s, "0", NULL); | |
59 | } | |
60 | ||
61 | T_DECL(netbsd_snprintf_float, "test that floating conversions don't leak memory") | |
62 | { | |
63 | union { | |
64 | double d; | |
65 | uint64_t bits; | |
66 | } u; | |
67 | uint32_t ul, uh; | |
68 | time_t now; | |
69 | char buf[1000]; | |
70 | struct rlimit rl; | |
71 | ||
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); | |
76 | ||
77 | time(&now); | |
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); | |
84 | } | |
85 | } | |
86 | ||
87 | T_DECL(netbsd_sprintf_zeropad, "Test output format zero padding (PR lib/44113)") | |
88 | { | |
89 | char str[1024]; | |
90 | ||
91 | T_WITH_ERRNO; T_EXPECT_EQ(sprintf(str, "%010f", 0.0), 10, NULL); | |
92 | T_EXPECT_EQ_STR(str, "000.000000", NULL); | |
93 | ||
94 | /* ieeefp */ | |
95 | #ifndef __vax__ | |
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); | |
101 | #endif | |
102 | } |