From: Gerhard Sittig Date: Tue, 17 Oct 2023 19:22:32 +0000 (+0200) Subject: tests: implement strutil test cases for power-of-two calculation X-Git-Url: https://sigrok.org/gitweb/?a=commitdiff_plain;h=96b8556295055ae7e8736625fffd295201624769;hp=a2f3645bcf73a421192591fc7d2d030f9997fda7;p=libsigrok.git tests: implement strutil test cases for power-of-two calculation Add the "calc" section to the strutil tests, and implement test cases which cover the power-of-two calculation routine. Cover small values which are the immediate use case and the motivation for introducing the helper routine. --- diff --git a/tests/strutil.c b/tests/strutil.c index 55d12d98..3cf132f0 100644 --- a/tests/strutil.c +++ b/tests/strutil.c @@ -573,6 +573,43 @@ START_TEST(test_text_word) } END_TEST +static const struct power_case_t { + size_t value; + size_t want_bits; + size_t want_power; +} power_cases[] = { + { 1, 1, 2, }, + { 2, 2, 4, }, + { 3, 2, 4, }, + { 4, 3, 8, }, + { 5, 3, 8, }, + { 6, 3, 8, }, + { 7, 3, 8, }, + { 8, 4, 16, }, + { 15, 4, 16, }, + { 16, 5, 32, }, + { 31, 5, 32, }, +}; + +START_TEST(test_calc_power_of_two) +{ + size_t case_idx, bits, power; + const struct power_case_t *tcase; + int ret; + + ret = sr_next_power_of_two(0, NULL, NULL); + fail_unless(ret != SR_OK, "invalid value, did not fail"); + + for (case_idx = 0; case_idx < ARRAY_SIZE(power_cases); case_idx++) { + tcase = &power_cases[case_idx]; + ret = sr_next_power_of_two(tcase->value, &bits, &power); + fail_unless(ret == SR_OK, "bits count not found"); + fail_unless(bits == tcase->want_bits, "bits count differs"); + fail_unless(power == tcase->want_power, "power differs"); + } +} +END_TEST + Suite *suite_strutil(void) { Suite *s; @@ -600,5 +637,9 @@ Suite *suite_strutil(void) tcase_add_test(tc, test_text_word); suite_add_tcase(s, tc); + tc = tcase_create("calc"); + tcase_add_test(tc, test_calc_power_of_two); + suite_add_tcase(s, tc); + return s; }