| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
|
| 3 |
#include "format-util.h" |
| 4 |
#include "tests.h" |
| 5 |
#include "uchar.h" |
| 6 |
|
| 7 |
/* Do some basic checks on STRLEN() and DECIMAL_STR_MAX() */ |
| 8 |
assert_cc(STRLEN("") == 0); |
| 9 |
assert_cc(STRLEN("a") == 1); |
| 10 |
assert_cc(STRLEN("123") == 3); |
| 11 |
assert_cc(STRLEN(u8"") == 0); |
| 12 |
assert_cc(STRLEN(u8"a") == 1); |
| 13 |
assert_cc(STRLEN(u8"123") == 3); |
| 14 |
assert_cc(STRLEN(u"") == 0); |
| 15 |
assert_cc(STRLEN(u"a") == sizeof(char16_t)); |
| 16 |
assert_cc(STRLEN(u"123") == 3 * sizeof(char16_t)); |
| 17 |
assert_cc(STRLEN(U"") == 0); |
| 18 |
assert_cc(STRLEN(U"a") == sizeof(char32_t)); |
| 19 |
assert_cc(STRLEN(U"123") == 3 * sizeof(char32_t)); |
| 20 |
assert_cc(STRLEN(L"") == 0); |
| 21 |
assert_cc(STRLEN(L"a") == sizeof(wchar_t)); |
| 22 |
assert_cc(STRLEN(L"123") == 3 * sizeof(wchar_t)); |
| 23 |
assert_cc(DECIMAL_STR_MAX(uint8_t) == STRLEN("255")+1); |
| 24 |
assert_cc(DECIMAL_STR_MAX(int8_t) == STRLEN("-127")+1); |
| 25 |
assert_cc(DECIMAL_STR_MAX(uint64_t) == STRLEN("18446744073709551615")+1); |
| 26 |
assert_cc(DECIMAL_STR_MAX(int64_t) == CONST_MAX(STRLEN("-9223372036854775808"), STRLEN("9223372036854775807"))+1); |
| 27 |
assert_cc(DECIMAL_STR_MAX(signed char) == STRLEN("-127")+1); |
| 28 |
assert_cc(DECIMAL_STR_MAX(unsigned char) == STRLEN("255")+1); |
| 29 |
assert_cc(CONST_MAX(DECIMAL_STR_MAX(int8_t), STRLEN("xxx")) == 5); |
| 30 |
|
| 31 |
static void test_format_bytes_one( |
| 32 |
uint64_t val, |
| 33 |
bool trailing_B, |
| 34 |
const char *iec_with_p, |
| 35 |
const char *iec_without_p, |
| 36 |
const char *iec_always_p, |
| 37 |
const char *si_with_p, |
| 38 |
const char *si_without_p, |
| 39 |
const char *si_always_p) { |
| 40 |
char buf[FORMAT_BYTES_MAX]; |
| 41 |
|
| 42 |
ASSERT_STREQ(format_bytes_full(buf, sizeof buf, val, FORMAT_BYTES_USE_IEC | FORMAT_BYTES_BELOW_POINT | (trailing_B ? FORMAT_BYTES_TRAILING_B : 0)), iec_with_p); |
| 43 |
ASSERT_STREQ(format_bytes_full(buf, sizeof buf, val, FORMAT_BYTES_USE_IEC | (trailing_B ? FORMAT_BYTES_TRAILING_B : 0)), iec_without_p); |
| 44 |
ASSERT_STREQ(format_bytes_full(buf, sizeof buf, val, FORMAT_BYTES_USE_IEC | FORMAT_BYTES_ALWAYS_POINT | (trailing_B ? FORMAT_BYTES_TRAILING_B : 0)), iec_always_p); |
| 45 |
ASSERT_STREQ(format_bytes_full(buf, sizeof buf, val, FORMAT_BYTES_BELOW_POINT | (trailing_B ? FORMAT_BYTES_TRAILING_B : 0)), si_with_p); |
| 46 |
ASSERT_STREQ(format_bytes_full(buf, sizeof buf, val, trailing_B ? FORMAT_BYTES_TRAILING_B : 0), si_without_p); |
| 47 |
ASSERT_STREQ(format_bytes_full(buf, sizeof buf, val, FORMAT_BYTES_ALWAYS_POINT | (trailing_B ? FORMAT_BYTES_TRAILING_B : 0)), si_always_p); |
| 48 |
} |
| 49 |
|
| 50 |
TEST(format_bytes) { |
| 51 |
test_format_bytes_one(900, true, "900B", "900B", "900B", "900B", "900B", "900B"); |
| 52 |
test_format_bytes_one(900, false, "900", "900", "900", "900", "900", "900"); |
| 53 |
test_format_bytes_one(1023, true, "1023B", "1023B", "1023B", "1K", "1K", "1.0K"); |
| 54 |
test_format_bytes_one(1023, false, "1023", "1023", "1023", "1K", "1K", "1.0K"); |
| 55 |
test_format_bytes_one(1024, true, "1K", "1K", "1.0K", "1K", "1K", "1.0K"); |
| 56 |
test_format_bytes_one(1024, false, "1K", "1K", "1.0K", "1K", "1K", "1.0K"); |
| 57 |
test_format_bytes_one(1100, true, "1K", "1K", "1.0K", "1.1K", "1K", "1.1K"); |
| 58 |
test_format_bytes_one(1500, true, "1.4K", "1K", "1.4K", "1.5K", "1K", "1.5K"); |
| 59 |
test_format_bytes_one(UINT64_C(3)*1024*1024, true, "3M", "3M", "3.0M", "3.1M", "3M", "3.1M"); |
| 60 |
test_format_bytes_one(UINT64_C(3)*1024*1024*1024, true, "3G", "3G", "3.0G", "3.2G", "3G", "3.2G"); |
| 61 |
test_format_bytes_one(UINT64_C(3)*1024*1024*1024*1024, true, "3T", "3T", "3.0T", "3.2T", "3T", "3.2T"); |
| 62 |
test_format_bytes_one(UINT64_C(3)*1024*1024*1024*1024*1024, true, "3P", "3P", "3.0P", "3.3P", "3P", "3.3P"); |
| 63 |
test_format_bytes_one(UINT64_C(3)*1024*1024*1024*1024*1024*1024, true, "3E", "3E", "3.0E", "3.4E", "3E", "3.4E"); |
| 64 |
test_format_bytes_one(UINT64_MAX, true, NULL, NULL, NULL, NULL, NULL, NULL); |
| 65 |
test_format_bytes_one(UINT64_MAX, false, NULL, NULL, NULL, NULL, NULL, NULL); |
| 66 |
} |
| 67 |
|
| 68 |
DEFINE_TEST_MAIN(LOG_INFO); |