[app][tests] tests for floating point printf

This commit is contained in:
Travis Geiselbrecht
2014-08-19 15:37:05 -07:00
parent caf2715fd3
commit 30912e2f26
6 changed files with 189 additions and 79 deletions

View File

@@ -0,0 +1,17 @@
#include <stdio.h>
#define countof(a) (sizeof(a) / sizeof((a)[0]))
#include "float_test_vec.c"
int main(void)
{
printf("floating point printf tests\n");
for (size_t i = 0; i < float_test_vec_size; i++) {
PRINT_FLOAT;
}
return 0;
}

View File

@@ -0,0 +1,79 @@
/*
* Copyright (c) 2014 Travis Geiselbrecht
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files
* (the "Software"), to deal in the Software without restriction,
* including without limitation the rights to use, copy, modify, merge,
* publish, distribute, sublicense, and/or sell copies of the Software,
* and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#include <stdint.h>
union double_int {
double d;
uint64_t i;
};
static const union double_int float_test_vec[] = {
{ .d = -2.0 },
{ .d = -1.0 },
{ .d = -0.5 },
{ .d = -0.0 },
{ .d = 0.0 },
{ .d = 0.01 },
{ .d = 0.1 },
{ .d = 0.2 },
{ .d = 0.25 },
{ .d = 0.5 },
{ .d = 0.75 },
{ .d = 1.0 },
{ .d = 2.0 },
{ .d = 3.0 },
{ .d = 10.0 },
{ .d = 100.0 },
{ .d = 123456.0 },
{ .d = -123456.0 },
{ .d = 0.12345 },
{ .d = 0.0000012345 },
{ .d = 0.0000019999 },
{ .d = 0.0000015 },
{ .i = 0x4005bf0a8b145649ULL }, // e
{ .i = 0x400921fb54442d18ULL }, // pi
{ .i = 0x43f0000000000000ULL }, // 2^64
{ .i = 0x7fefffffffffffffULL }, // largest normalized
{ .i = 0x0010000000000000ULL }, // least positive normalized
{ .i = 0x0000000000000001ULL }, // smallest possible denorm
{ .i = 0x000fffffffffffffULL }, // largest possible denorm
{ .i = 0x7ff0000000000001ULL }, // smallest SNAn
{ .i = 0x7ff7ffffffffffffULL }, // largest SNAn
{ .i = 0x7ff8000000000000ULL }, // smallest QNAn
{ .i = 0x7fffffffffffffffULL }, // largest QNAn
{ .i = 0xfff0000000000000ULL }, // -infinity
{ .i = 0x7ff0000000000000ULL }, // +infinity
};
#define countof(a) (sizeof(a) / sizeof((a)[0]))
static const unsigned int float_test_vec_size = countof(float_test_vec);
#define PRINT_FLOAT \
printf("0x%016llx %f %F %a %A\n", \
float_test_vec[i], \
*(const double *)&float_test_vec[i], \
*(const double *)&float_test_vec[i], \
*(const double *)&float_test_vec[i], \
*(const double *)&float_test_vec[i])

View File

@@ -1,5 +1,5 @@
/*
* Copyright (c) 2008 Travis Geiselbrecht
* Copyright (c) 2008-2014 Travis Geiselbrecht
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files
@@ -27,6 +27,7 @@
int thread_tests(void);
void printf_tests(void);
void printf_tests_float(void);
void clock_tests(void);
void float_tests(void);
void benchmarks(void);

View File

@@ -1,5 +1,5 @@
/*
* Copyright (c) 2008 Travis Geiselbrecht
* Copyright (c) 2008-2014 Travis Geiselbrecht
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files
@@ -26,95 +26,106 @@
void printf_tests(void)
{
printf("printf tests\n");
printf("printf tests\n");
printf("numbers:\n");
printf("int8: %hhd %hhd %hhd\n", -12, 0, 254);
printf("uint8: %hhu %hhu %hhu\n", -12, 0, 254);
printf("int16: %hd %hd %hd\n", -1234, 0, 1234);
printf("uint16:%hu %hu %hu\n", -1234, 0, 1234);
printf("int: %d %d %d\n", -12345678, 0, 12345678);
printf("uint: %u %u %u\n", -12345678, 0, 12345678);
printf("long: %ld %ld %ld\n", -12345678L, 0L, 12345678L);
printf("ulong: %lu %lu %lu\n", -12345678UL, 0UL, 12345678UL);
printf("numbers:\n");
printf("int8: %hhd %hhd %hhd\n", -12, 0, 254);
printf("uint8: %hhu %hhu %hhu\n", -12, 0, 254);
printf("int16: %hd %hd %hd\n", -1234, 0, 1234);
printf("uint16:%hu %hu %hu\n", -1234, 0, 1234);
printf("int: %d %d %d\n", -12345678, 0, 12345678);
printf("uint: %u %u %u\n", -12345678, 0, 12345678);
printf("long: %ld %ld %ld\n", -12345678L, 0L, 12345678L);
printf("ulong: %lu %lu %lu\n", -12345678UL, 0UL, 12345678UL);
printf("longlong: %lli %lli %lli\n", -12345678LL, 0LL, 12345678LL);
printf("ulonglong: %llu %llu %llu\n", -12345678LL, 0LL, 12345678LL);
printf("ssize_t: %zd %zd %zd\n", (ssize_t)-12345678, (ssize_t)0, (ssize_t)12345678);
printf("usize_t: %zu %zu %zu\n", (size_t)-12345678, (size_t)0, (size_t)12345678);
printf("intmax_t: %jd %jd %jd\n", (intmax_t)-12345678, (intmax_t)0, (intmax_t)12345678);
printf("uintmax_t: %ju %ju %ju\n", (uintmax_t)-12345678, (uintmax_t)0, (uintmax_t)12345678);
printf("ptrdiff_t: %td %td %td\n", (ptrdiff_t)-12345678, (ptrdiff_t)0, (ptrdiff_t)12345678);
printf("ptrdiff_t (u): %tu %tu %tu\n", (ptrdiff_t)-12345678, (ptrdiff_t)0, (ptrdiff_t)12345678);
printf("longlong: %lli %lli %lli\n", -12345678LL, 0LL, 12345678LL);
printf("ulonglong: %llu %llu %llu\n", -12345678LL, 0LL, 12345678LL);
printf("ssize_t: %zd %zd %zd\n", (ssize_t)-12345678, (ssize_t)0, (ssize_t)12345678);
printf("usize_t: %zu %zu %zu\n", (size_t)-12345678, (size_t)0, (size_t)12345678);
printf("intmax_t: %jd %jd %jd\n", (intmax_t)-12345678, (intmax_t)0, (intmax_t)12345678);
printf("uintmax_t: %ju %ju %ju\n", (uintmax_t)-12345678, (uintmax_t)0, (uintmax_t)12345678);
printf("ptrdiff_t: %td %td %td\n", (ptrdiff_t)-12345678, (ptrdiff_t)0, (ptrdiff_t)12345678);
printf("ptrdiff_t (u): %tu %tu %tu\n", (ptrdiff_t)-12345678, (ptrdiff_t)0, (ptrdiff_t)12345678);
printf("hex:\n");
printf("uint8: %hhx %hhx %hhx\n", -12, 0, 254);
printf("uint16:%hx %hx %hx\n", -1234, 0, 1234);
printf("uint: %x %x %x\n", -12345678, 0, 12345678);
printf("ulong: %lx %lx %lx\n", -12345678UL, 0UL, 12345678UL);
printf("ulong: %X %X %X\n", -12345678, 0, 12345678);
printf("ulonglong: %llx %llx %llx\n", -12345678LL, 0LL, 12345678LL);
printf("usize_t: %zx %zx %zx\n", (size_t)-12345678, (size_t)0, (size_t)12345678);
printf("hex:\n");
printf("uint8: %hhx %hhx %hhx\n", -12, 0, 254);
printf("uint16:%hx %hx %hx\n", -1234, 0, 1234);
printf("uint: %x %x %x\n", -12345678, 0, 12345678);
printf("ulong: %lx %lx %lx\n", -12345678UL, 0UL, 12345678UL);
printf("ulong: %X %X %X\n", -12345678, 0, 12345678);
printf("ulonglong: %llx %llx %llx\n", -12345678LL, 0LL, 12345678LL);
printf("usize_t: %zx %zx %zx\n", (size_t)-12345678, (size_t)0, (size_t)12345678);
printf("alt/sign:\n");
printf("uint: %#x %#X\n", 0xabcdef, 0xabcdef);
printf("int: %+d %+d\n", 12345678, -12345678);
printf("int: % d %+d\n", 12345678, 12345678);
printf("alt/sign:\n");
printf("uint: %#x %#X\n", 0xabcdef, 0xabcdef);
printf("int: %+d %+d\n", 12345678, -12345678);
printf("int: % d %+d\n", 12345678, 12345678);
printf("formatting\n");
printf("int: a%8da\n", 12345678);
printf("int: a%9da\n", 12345678);
printf("int: a%-9da\n", 12345678);
printf("int: a%10da\n", 12345678);
printf("int: a%-10da\n", 12345678);
printf("int: a%09da\n", 12345678);
printf("int: a%010da\n", 12345678);
printf("int: a%6da\n", 12345678);
printf("formatting\n");
printf("int: a%8da\n", 12345678);
printf("int: a%9da\n", 12345678);
printf("int: a%-9da\n", 12345678);
printf("int: a%10da\n", 12345678);
printf("int: a%-10da\n", 12345678);
printf("int: a%09da\n", 12345678);
printf("int: a%010da\n", 12345678);
printf("int: a%6da\n", 12345678);
printf("a%1sa\n", "b");
printf("a%9sa\n", "b");
printf("a%-9sa\n", "b");
printf("a%5sa\n", "thisisatest");
printf("a%1sa\n", "b");
printf("a%9sa\n", "b");
printf("a%-9sa\n", "b");
printf("a%5sa\n", "thisisatest");
printf("%03d\n", -2); /* '-02' */
printf("%0+3d\n", -2); /* '-02' */
printf("%0+3d\n", 2); /* '+02' */
printf("%+3d\n", 2); /* ' +2' */
printf("% 3d\n", -2000); /* '-2000' */
printf("% 3d\n", 2000); /* ' 2000' */
printf("%+3d\n", 2000); /* '+2000' */
printf("%10s\n", "test"); /* ' test' */
printf("%010s\n", "test"); /* ' test' */
printf("%-10s\n", "test"); /* 'test ' */
printf("%-010s\n", "test"); /* 'test ' */
printf("%03d\n", -2); /* '-02' */
printf("%0+3d\n", -2); /* '-02' */
printf("%0+3d\n", 2); /* '+02' */
printf("%+3d\n", 2); /* ' +2' */
printf("% 3d\n", -2000); /* '-2000' */
printf("% 3d\n", 2000); /* ' 2000' */
printf("%+3d\n", 2000); /* '+2000' */
printf("%10s\n", "test"); /* ' test' */
printf("%010s\n", "test"); /* ' test' */
printf("%-10s\n", "test"); /* 'test ' */
printf("%-010s\n", "test"); /* 'test ' */
int err;
int err;
err = printf("a");
printf(" returned %d\n", err);
err = printf("ab");
printf(" returned %d\n", err);
err = printf("abc");
printf(" returned %d\n", err);
err = printf("abcd");
printf(" returned %d\n", err);
err = printf("abcde");
printf(" returned %d\n", err);
err = printf("abcdef");
printf(" returned %d\n", err);
err = printf("a");
printf(" returned %d\n", err);
err = printf("ab");
printf(" returned %d\n", err);
err = printf("abc");
printf(" returned %d\n", err);
err = printf("abcd");
printf(" returned %d\n", err);
err = printf("abcde");
printf(" returned %d\n", err);
err = printf("abcdef");
printf(" returned %d\n", err);
/* make sure snprintf terminates at the right spot */
char buf[32];
/* make sure snprintf terminates at the right spot */
char buf[32];
memset(buf, 0, sizeof(buf));
err = sprintf(buf, "0123456789abcdef012345678");
printf("sprintf returns %d\n", err);
hexdump8(buf, sizeof(buf));
memset(buf, 0, sizeof(buf));
err = sprintf(buf, "0123456789abcdef012345678");
printf("sprintf returns %d\n", err);
hexdump8(buf, sizeof(buf));
memset(buf, 0, sizeof(buf));
err = snprintf(buf, 15, "0123456789abcdef012345678");
printf("snprintf returns %d\n", err);
hexdump8(buf, sizeof(buf));
memset(buf, 0, sizeof(buf));
err = snprintf(buf, 15, "0123456789abcdef012345678");
printf("snprintf returns %d\n", err);
hexdump8(buf, sizeof(buf));
}
#include "float_test_vec.c"
void printf_tests_float(void)
{
printf("floating point printf tests\n");
for (size_t i = 0; i < float_test_vec_size; i++) {
PRINT_FLOAT;
}
}

View File

@@ -13,6 +13,7 @@ MODULE_SRCS += \
$(LOCAL_DIR)/benchmarks.c \
$(LOCAL_DIR)/float.c \
$(LOCAL_DIR)/float_instructions.S \
$(LOCAL_DIR)/float_test_vec.c \
$(LOCAL_DIR)/fibo.c
MODULE_COMPILEFLAGS += -Wno-format

View File

@@ -30,6 +30,7 @@
STATIC_COMMAND_START
STATIC_COMMAND("printf_tests", "test printf", (console_cmd)&printf_tests)
STATIC_COMMAND("printf_tests_float", "test printf with floating point", (console_cmd)&printf_tests_float)
STATIC_COMMAND("thread_tests", "test the scheduler", (console_cmd)&thread_tests)
STATIC_COMMAND("clock_tests", "test clocks", (console_cmd)&clock_tests)
#if ARM_WITH_VFP