From 5e1a3ad6e092666d6e1264cb0b555c98595aae41 Mon Sep 17 00:00:00 2001 From: Pavol Rusnak Date: Thu, 27 Jul 2017 19:57:04 +0200 Subject: [PATCH] tests: add more tests for bn_format{,_uint64} --- test_check.c | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/test_check.c b/test_check.c index 37c775c4e1..80aadc5305 100644 --- a/test_check.c +++ b/test_check.c @@ -23,6 +23,7 @@ #include #include +#include #include #include #include @@ -417,6 +418,29 @@ START_TEST(test_bignum_digitcount) } END_TEST +START_TEST(test_bignum_format_uint64) { + char buf[128], str[128]; + int r; + // test for 10000 and 99999 when i=5 + for (int i = 1; i <= 19; i++) { + uint64_t m = 1; + uint64_t n = 9; + for (int j = 2; j <= i; j++) { + m = m * 10; + n = n * 10 + 9; + } + sprintf(str, "%" PRIu64, m); + r = bn_format_uint64(m, NULL, NULL, 0, 0, false, buf, sizeof(buf)); + ck_assert_int_eq(r, strlen(str)); + ck_assert_str_eq(buf, str); + sprintf(str, "%" PRIu64, n); + r = bn_format_uint64(n, NULL, NULL, 0, 0, false, buf, sizeof(buf)); + ck_assert_int_eq(r, strlen(str)); + ck_assert_str_eq(buf, str); + } +} +END_TEST + START_TEST(test_bignum_format) { bignum256 a; char buf[128]; @@ -487,6 +511,11 @@ START_TEST(test_bignum_format) { ck_assert_int_eq(r, 1); ck_assert_str_eq(buf, "5"); + bn_read_be(fromhex("0000000000000000000000000000000000000000000000000000000000000009"), &a); + r = bn_format(&a, NULL, NULL, 0, 0, false, buf, sizeof(buf)); + ck_assert_int_eq(r, 1); + ck_assert_str_eq(buf, "9"); + bn_read_be(fromhex("000000000000000000000000000000000000000000000000000000000000000a"), &a); r = bn_format(&a, NULL, NULL, 0, 0, false, buf, sizeof(buf)); ck_assert_int_eq(r, 2); @@ -502,6 +531,11 @@ START_TEST(test_bignum_format) { ck_assert_int_eq(r, 2); ck_assert_str_eq(buf, "50"); + bn_read_be(fromhex("0000000000000000000000000000000000000000000000000000000000000063"), &a); + r = bn_format(&a, NULL, NULL, 0, 0, false, buf, sizeof(buf)); + ck_assert_int_eq(r, 2); + ck_assert_str_eq(buf, "99"); + bn_read_be(fromhex("0000000000000000000000000000000000000000000000000000000000000064"), &a); r = bn_format(&a, NULL, NULL, 0, 0, false, buf, sizeof(buf)); ck_assert_int_eq(r, 3); @@ -517,6 +551,11 @@ START_TEST(test_bignum_format) { ck_assert_int_eq(r, 3); ck_assert_str_eq(buf, "500"); + bn_read_be(fromhex("00000000000000000000000000000000000000000000000000000000000003e7"), &a); + r = bn_format(&a, NULL, NULL, 0, 0, false, buf, sizeof(buf)); + ck_assert_int_eq(r, 3); + ck_assert_str_eq(buf, "999"); + bn_read_be(fromhex("00000000000000000000000000000000000000000000000000000000000003e8"), &a); r = bn_format(&a, NULL, NULL, 0, 0, false, buf, sizeof(buf)); ck_assert_int_eq(r, 4); @@ -3274,6 +3313,7 @@ Suite *test_suite(void) tcase_add_test(tc, test_bignum_digitcount); tcase_add_test(tc, test_bignum_is_less); tcase_add_test(tc, test_bignum_format); + tcase_add_test(tc, test_bignum_format_uint64); suite_add_tcase(s, tc); tc = tcase_create("base32");