/* libmisc/tests/test_fmt.c - Tests for * * Copyright (C) 2025 Luke T. Shumaker * SPDX-License-Identifier: AGPL-3.0-or-later */ #include /* for strcmp(), memcmp(), memset() */ #include #include "test.h" int main() { char str[128] = {}; #define do_print(...) fmt_snprint(str, sizeof(str), __VA_ARGS__) do_print("hello ", 9, " world!\n"); test_assert(strcmp(str, "hello 9 world!\n") == 0); memset(str, 0, sizeof(str)); do_print("hello ", (base8, 9), " world!\n"); test_assert(strcmp(str, "hello 11 world!\n") == 0); memset(str, 0, sizeof(str)); do_print("hello ", (base2, 9), (qstr, " world!\n")); test_assert(strcmp(str, "hello 1001\" world!\\n\"") == 0); memset(str, 0, sizeof(str)); do_print("hello ", (base16, 17), " world!\n"); test_assert(strcmp(str, "hello 11 world!\n") == 0); memset(str, 0, sizeof(str)); do_print((strn, "hello ", 4)); test_assert(strcmp(str, "hell") == 0); memset(str, 0, sizeof(str)); do_print((strn, "h\0ello ", 4)); test_assert(memcmp(str, "h\0\0", 3) == 0); memset(str, 0, sizeof(str)); do_print((mem, "hello ", 4)); test_assert(strcmp(str, "hell") == 0); memset(str, 0, sizeof(str)); do_print((mem, "hello\0world", strlen("hello world")+1)); test_assert(memcmp(str, "hello\0world", strlen("hello world")+1) == 0); memset(str, 0, sizeof(str)); do_print((qmem, "hello\0world", strlen("hello world")+1)); test_assert(strcmp(str, "\"hello\\0world\\0\"") == 0); memset(str, 0, sizeof(str)); do_print((qstr, "hello\0world")); test_assert(strcmp(str, "\"hello\"") == 0); memset(str, 0, sizeof(str)); do_print((qstrn, "hello\0world", strlen("hello world")+1)); test_assert(strcmp(str, "\"hello\"") == 0); memset(str, 0, sizeof(str)); do_print((qstrn, "hello\0world", 4)); test_assert(strcmp(str, "\"hell\"") == 0); memset(str, 0, sizeof(str)); do_print((byte, 'h'), (byte, 'w')); test_assert(strcmp(str, "hw") == 0); memset(str, 0, sizeof(str)); do_print((qbyte, 'h'), (qbyte, 'w')); test_assert(strcmp(str, "'h''w'") == 0); memset(str, 0, sizeof(str)); do_print("zero ", 0); test_assert(strcmp(str, "zero 0") == 0); memset(str, 0, sizeof(str)); const char *const_str = "hello"; do_print(const_str); test_assert(strcmp(str, "hello") == 0); memset(str, 0, sizeof(str)); bool t = true; do_print(t); test_assert(strcmp(str, "true") == 0); memset(str, 0, sizeof(str)); bool f = false; do_print(f); test_assert(strcmp(str, "false") == 0); memset(str, 0, sizeof(str)); /* Check that it accepts all primitive types of integer, not * just all sizes of integer (e.g., on x86-64, * sizeof(long)==sizeof(int), but they're different primitive * types). */ { signed char x = 42; do_print("schar ", x); test_assert(strcmp(str, "schar 42") == 0); memset(str, 0, sizeof(str)); } { unsigned char x = 43; do_print("uchar ", x); test_assert(strcmp(str, "uchar 43") == 0); memset(str, 0, sizeof(str)); } { short x = 44; do_print("short ", x); test_assert(strcmp(str, "short 44") == 0); memset(str, 0, sizeof(str)); } { unsigned short x = 45; do_print("ushort ", x); test_assert(strcmp(str, "ushort 45") == 0); memset(str, 0, sizeof(str)); } { int x = 46; do_print("int ", x); test_assert(strcmp(str, "int 46") == 0); memset(str, 0, sizeof(str)); } { unsigned int x = 47; do_print("uint ", x); test_assert(strcmp(str, "uint 47") == 0); memset(str, 0, sizeof(str)); } { long x = 48; do_print("long ", x); test_assert(strcmp(str, "long 48") == 0); memset(str, 0, sizeof(str)); } { unsigned long x = 49; do_print("ulong ", x); test_assert(strcmp(str, "ulong 49") == 0); memset(str, 0, sizeof(str)); } { long long x = 50; do_print("long long ", x); test_assert(strcmp(str, "long long 50") == 0); memset(str, 0, sizeof(str)); } { unsigned long long x = 51; do_print("ulong long ", x); test_assert(strcmp(str, "ulong long 51") == 0); memset(str, 0, sizeof(str)); } do_print((ljust, 10, ' ', (base10, 1), "x")); test_assert(strcmp(str, "1x ") == 0); memset(str, 0, sizeof(str)); do_print((rjust, 10, ' ', (base10, 1), "x")); test_assert(strcmp(str, " 1x") == 0); memset(str, 0, sizeof(str)); return 0; }