#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];
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);
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);
#ifndef __vax__
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
}