Loading...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 | /* $NetBSD: t_printf.c,v 1.8 2012/04/11 16:21:42 jruoho Exp $ */ /*- * Copyright (c) 2010 The NetBSD Foundation, Inc. * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. */ #include <sys/types.h> #include <sys/resource.h> #include <math.h> #include <stdio.h> #include <stdint.h> #include <string.h> #include <time.h> #include <stdlib.h> #include "darwintest.h" T_DECL(netbsd_snprintf_c99, "Test printf(3) C99 conformance (PR lib/22019)") { char s[4]; (void)memset(s, '\0', sizeof(s)); (void)snprintf(s, sizeof(s), "%#.o", 0); (void)printf("printf = %#.o\n", 0); (void)fprintf(stderr, "snprintf = %s", s); T_ASSERT_EQ(strlen(s), (size_t)1, NULL); T_ASSERT_EQ(s[0], '0', NULL); } T_DECL(netbsd_snprintf_dotzero, "PR lib/32951: %%.0f formats (0.0,0.5] to \"0.\"") { char s[4]; T_WITH_ERRNO; T_EXPECT_EQ(snprintf(s, sizeof(s), "%.0f", 0.1), 1, NULL); T_EXPECT_EQ_STR(s, "0", NULL); } T_DECL(netbsd_snprintf_float, "test that floating conversions don't leak memory") { union { double d; uint64_t bits; } u; uint32_t ul, uh; time_t now; char buf[1000]; #if 0 // rlimit isn't a good way to test for memory growth on our platform. // Even a relatively simple program will use an unpredictable and // large amount of memory just to reach main(). // To test for proper leaks, we rely on libdarwintest's built-in // leaks support. To test for non-leaking growth, well... we don't // have a great way to do that at present. struct rlimit rl; rl.rlim_cur = rl.rlim_max = 1 * 1024 * 1024; T_WITH_ERRNO; T_EXPECT_NE(setrlimit(RLIMIT_AS, &rl), -1, NULL); rl.rlim_cur = rl.rlim_max = 1 * 1024 * 1024; T_EXPECT_POSIX_SUCCESS(setrlimit(RLIMIT_DATA, &rl), NULL); #endif time(&now); srand((unsigned int)now); for (size_t i = 0; i < 10000; i++) { ul = (uint32_t)rand(); uh = (uint32_t)rand(); u.bits = (uint64_t)uh << 32 | ul; T_EXPECT_POSIX_SUCCESS(snprintf(buf, sizeof buf, " %.2f", u.d), NULL); } } T_DECL(netbsd_sprintf_zeropad, "Test output format zero padding (PR lib/44113)") { char str[1024]; T_WITH_ERRNO; T_EXPECT_EQ(sprintf(str, "%010f", 0.0), 10, NULL); T_EXPECT_EQ_STR(str, "000.000000", NULL); /* ieeefp */ #ifndef __vax__ /* printf(3) should ignore zero padding for nan/inf */ T_WITH_ERRNO; T_EXPECT_EQ(sprintf(str, "%010f", NAN), 10, NULL); T_EXPECT_EQ_STR(str, " nan", NULL); T_WITH_ERRNO; T_EXPECT_EQ(sprintf(str, "%010f", INFINITY), 10, NULL); T_EXPECT_EQ_STR(str, " inf", NULL); #endif } |