From e6852cd9f546b6d5f347571bf387b3b83895c78e Mon Sep 17 00:00:00 2001 From: Sarah GLINER Date: Thu, 17 Aug 2023 11:57:08 +0200 Subject: [PATCH] fpu64_trimmed: add unit tests (cherry picked from commit 6a775cdcd17e85d8bebeb5c3d735b9de38337670) --- unit-tests/lib_standard_app/test_format.c | 27 +++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/unit-tests/lib_standard_app/test_format.c b/unit-tests/lib_standard_app/test_format.c index 54fab79c7..d1092dde0 100644 --- a/unit-tests/lib_standard_app/test_format.c +++ b/unit-tests/lib_standard_app/test_format.c @@ -82,6 +82,32 @@ static void test_format_fpu64(void **state) { assert_false(format_fpu64(temp2, sizeof(temp2) - 20, amount, 18)); } +static void test_format_fpu64_trimmed(void **state) { + (void) state; + + char temp[22] = {0}; + + uint64_t amount = 100000000ull; // satoshi + memset(temp, 0, sizeof(temp)); + assert_true(format_fpu64_trimmed(temp, sizeof(temp), amount, 8)); + assert_string_equal(temp, "1"); // BTC + + amount = 24964823ull; // satoshi + memset(temp, 0, sizeof(temp)); + assert_true(format_fpu64_trimmed(temp, sizeof(temp), amount, 8)); + assert_string_equal(temp, "0.24964823"); // BTC + + amount = 100ull; // satoshi + memset(temp, 0, sizeof(temp)); + assert_true(format_fpu64_trimmed(temp, sizeof(temp), amount, 8)); + assert_string_equal(temp, "0.000001"); // BTC + + amount = 1000000000ull; // satoshi + memset(temp, 0, sizeof(temp)); + assert_true(format_fpu64_trimmed(temp, sizeof(temp), amount, 8)); + assert_string_equal(temp, "10"); // BTC +} + static void test_format_hex(void **state) { (void) state; @@ -99,6 +125,7 @@ int main() { const struct CMUnitTest tests[] = {cmocka_unit_test(test_format_i64), cmocka_unit_test(test_format_u64), cmocka_unit_test(test_format_fpu64), + cmocka_unit_test(test_format_fpu64_trimmed), cmocka_unit_test(test_format_hex)}; return cmocka_run_group_tests(tests, NULL, NULL);