Annotation of embedaddon/strongswan/src/libstrongswan/tests/suites/test_printf.c, revision 1.1
1.1 ! misho 1: /*
! 2: * Copyright (C) 2013 Martin Willi
! 3: * Copyright (C) 2013 revosec AG
! 4: *
! 5: * This program is free software; you can redistribute it and/or modify it
! 6: * under the terms of the GNU General Public License as published by the
! 7: * Free Software Foundation; either version 2 of the License, or (at your
! 8: * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
! 9: *
! 10: * This program is distributed in the hope that it will be useful, but
! 11: * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
! 12: * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
! 13: * for more details.
! 14: */
! 15:
! 16: #include "test_suite.h"
! 17:
! 18: #include <errno.h>
! 19: #include <math.h>
! 20: #include <inttypes.h>
! 21:
! 22: static void verify(char *expected, char *format, ...)
! 23: {
! 24: char buf[128];
! 25: va_list args;
! 26:
! 27: va_start(args, format);
! 28: vsnprintf(buf, sizeof(buf), format, args);
! 29: ck_assert_str_eq(expected, buf);
! 30: va_end(args);
! 31:
! 32: #ifdef HAVE_FMEMOPEN
! 33: {
! 34: FILE *mem;
! 35:
! 36: mem = fmemopen(buf, sizeof(buf), "w");
! 37: va_start(args, format);
! 38: vfprintf(mem, format, args);
! 39: va_end(args);
! 40: fclose(mem);
! 41: ck_assert_str_eq(expected, buf);
! 42: }
! 43: #endif /* HAVE_FMEMOPEN */
! 44: }
! 45:
! 46: START_TEST(test_printf_null)
! 47: {
! 48: char buf[16];
! 49:
! 50: /* on FreeBSD "(null)" gets printed even when a precision of 0 is used.
! 51: * because printing of "(null)" for NULL is not standardized we don't verify
! 52: * the output and just make sure there is no crash */
! 53: snprintf(buf, sizeof(buf), "%s", NULL);
! 54: }
! 55: END_TEST
! 56:
! 57: START_TEST(test_printf_strings)
! 58: {
! 59: verify("a bc def", "%s %s %s", "a", "bc", "def");
! 60: verify("", "%.0s", "asdfg");
! 61: verify("asd", "%.3s", "asdfg");
! 62: verify("asdf", "%.*s", (int)4, "asdfg");
! 63: verify(" asdf", "%6s", "asdf");
! 64: verify(" asdf", "%+6s", "asdf");
! 65: verify("asdf ", "%-6s", "asdf");
! 66: }
! 67: END_TEST
! 68:
! 69: START_TEST(test_printf_err)
! 70: {
! 71: errno = EINVAL;
! 72: verify((char*)strerror(errno), "%m");
! 73: }
! 74: END_TEST
! 75:
! 76: START_TEST(test_printf_unsigned)
! 77: {
! 78: verify("1 23 456", "%u %lu %llu", 1, (u_long)23, (uint64_t)456);
! 79: verify("65535 255", "%hu %hhu", 0x1ffff, 0x1ff);
! 80: verify("123456789", "%zu", (size_t)123456789);
! 81: verify(" 12", "%5u", 12);
! 82: verify("12 ", "%-5u", 12);
! 83: verify("0012", "%04u", 12);
! 84: verify("0012", "%.4u", 12);
! 85: }
! 86: END_TEST
! 87:
! 88: START_TEST(test_printf_signed)
! 89: {
! 90: verify("-1 -23 -456", "%d %ld %lld", -1, (long)-23, (int64_t)-456);
! 91: verify("-1 -1", "%hd %hhd", 0x1ffff, 0x1ff);
! 92: verify("123456789", "%zd", (ssize_t)123456789);
! 93: verify(" -12", "%5d", -12);
! 94: verify("-12 ", "%-5d", -12);
! 95: verify("-012", "%04d", -12);
! 96: verify("-0012", "%.4d", -12);
! 97: }
! 98: END_TEST
! 99:
! 100: START_TEST(test_printf_hex)
! 101: {
! 102: verify("1 23 456", "%x %lx %llx", 1, (u_long)0x23, (uint64_t)0x456);
! 103: verify("12abcdef 12ABCDEF", "%x %X", 0x12ABCDEF, 0x12ABCDEF);
! 104: verify("ffff ff", "%hx %hhx", 0x1ffff, 0x1ff);
! 105: verify("23456789", "%zx", (size_t)0x23456789);
! 106: verify(" ab", "%5x", 0xab);
! 107: verify("ab ", "%-5x", 0xab);
! 108: verify("00ab", "%04x", 0xab);
! 109: verify("00ab", "%.4x", 0xab);
! 110: }
! 111: END_TEST
! 112:
! 113: START_TEST(test_printf_float)
! 114: {
! 115: verify("0.000000", "%f", 0.0);
! 116: verify("1.000000", "%f", 1.0);
! 117: verify("12345.1", "%.1f", 12345.123);
! 118: verify("1", "%.0f", 1.0);
! 119: verify("1.3", "%.1f", 1.346789);
! 120: verify("1.23", "%.2f", 1.23456789);
! 121: verify("1.123", "%.3f", 1.123456789);
! 122: verify("1.0123", "%.4f", 1.0123456789);
! 123:
! 124: verify("-1.000000", "%f", -1.0);
! 125: verify("-12345.1", "%.1f", -12345.123);
! 126: verify("-1", "%.0f", -1.0);
! 127: verify("-1.3", "%.1f", -1.3456789);
! 128: verify("-1.23", "%.2f", -1.23456789);
! 129: verify("-1.123", "%.3f", -1.123456789);
! 130: verify("-1.0123", "%.4f", -1.0123456789);
! 131:
! 132: verify(" 1.2", "%5.1f", 1.234);
! 133: verify("001.2", "%05.1f", 1.234);
! 134: verify("1.2 ", "%-5.1f", 1.234);
! 135:
! 136: verify("12346", "%.0f", 12345.6789);
! 137: verify("2", "%.0f", 1.5);
! 138: verify("1", "%.0f", 1.49);
! 139: verify("1.2", "%.1f", 1.151);
! 140: verify("1.1", "%.1f", 1.149);
! 141: verify("1.13", "%.2f", 1.1251);
! 142: verify("1.12", "%.2f", 1.1249);
! 143: verify("1.124", "%.3f", 1.12351);
! 144: verify("1.123", "%.3f", 1.12349);
! 145:
! 146: verify("-12346", "%.0f", -12345.6789);
! 147: verify("-2", "%.0f", -1.51);
! 148: verify("-1", "%.0f", -1.49);
! 149: verify("-1.2", "%.1f", -1.151);
! 150: verify("-1.1", "%.1f", -1.149);
! 151: verify("-1.13", "%.2f", -1.1251);
! 152: verify("-1.12", "%.2f", -1.1249);
! 153: verify("-1.124", "%.3f", -1.12351);
! 154: verify("-1.123", "%.3f", -1.12349);
! 155:
! 156: #ifdef NAN
! 157: verify("nan", "%.3f", NAN);
! 158: verify(" nan", "%5.3f", NAN);
! 159: verify("NAN", "%.3F", NAN);
! 160: verify("NAN ", "%-5.3F", NAN);
! 161: #endif
! 162: #ifdef INFINITY
! 163: verify("inf", "%.3f", INFINITY);
! 164: verify("-inf", "%.4f", -INFINITY);
! 165: verify("INF", "%.3F", INFINITY);
! 166: verify("-INF", "%.4F", -INFINITY);
! 167: #endif
! 168: }
! 169: END_TEST
! 170:
! 171: START_TEST(test_printf_pri)
! 172: {
! 173: verify("255", "%" PRIu8, (uint8_t)0xFF);
! 174: verify("65535", "%" PRIu16, (uint16_t)0xFFFF);
! 175: verify("4294967295", "%" PRIu32, (uint32_t)0x1FFFFFFFFll);
! 176: verify("18446744073709551615", "%" PRIu64, (uint64_t)0xFFFFFFFFFFFFFFFFll);
! 177:
! 178: verify("-1", "%" PRId8, (int8_t)-1);
! 179: verify("-1", "%" PRId16, (int16_t)-1);
! 180: verify("-1", "%" PRId32, (int32_t)-1);
! 181: verify("-1", "%" PRId64, (int64_t)-1);
! 182:
! 183: verify("1", "%" PRIuMAX, (uintmax_t)1);
! 184: verify("1", "%" PRIuPTR, (uintptr_t)1);
! 185:
! 186: verify("-1", "%" PRIdMAX, (intmax_t)-1);
! 187: verify("-1", "%" PRIdPTR, (intptr_t)-1);
! 188: }
! 189: END_TEST
! 190:
! 191: Suite *printf_suite_create()
! 192: {
! 193: Suite *s;
! 194: TCase *tc;
! 195:
! 196: s = suite_create("printf");
! 197:
! 198: tc = tcase_create("strings");
! 199: tcase_add_test(tc, test_printf_null);
! 200: tcase_add_test(tc, test_printf_strings);
! 201: suite_add_tcase(s, tc);
! 202:
! 203: tc = tcase_create("err");
! 204: tcase_add_test(tc, test_printf_err);
! 205: suite_add_tcase(s, tc);
! 206:
! 207: tc = tcase_create("unsigned");
! 208: tcase_add_test(tc, test_printf_unsigned);
! 209: suite_add_tcase(s, tc);
! 210:
! 211: tc = tcase_create("siged");
! 212: tcase_add_test(tc, test_printf_signed);
! 213: suite_add_tcase(s, tc);
! 214:
! 215: tc = tcase_create("hex");
! 216: tcase_add_test(tc, test_printf_hex);
! 217: suite_add_tcase(s, tc);
! 218:
! 219: tc = tcase_create("float");
! 220: tcase_add_test(tc, test_printf_float);
! 221: suite_add_tcase(s, tc);
! 222:
! 223: tc = tcase_create("PRI*");
! 224: tcase_add_test(tc, test_printf_pri);
! 225: suite_add_tcase(s, tc);
! 226:
! 227: return s;
! 228: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>