From: Gerhard Sittig Date: Sat, 12 Feb 2022 14:00:44 +0000 (+0100) Subject: tests: address printf format issues, spotted by clang on macos X-Git-Url: https://sigrok.org/gitaction?a=commitdiff_plain;h=11cd0ff15948a6c3999f66a6b45ace11049b09ee;p=libsigrok.git tests: address printf format issues, spotted by clang on macos The sr_rational data type has int64_t and uint64_t members, but analog.c and strutil.c source code used "%ld" and "%lu" formats. Use PRIu64 et al instead. .../libsigrok/tests/strutil.c:125:14: warning: format specifies type 'long' but the argument has type 'int64_t' (aka 'long long') [-Wformat] input, rational.p, rational.q); ^~~~~~~~~~ /usr/local/Cellar/check/0.15.2/include/check.h:472:77: note: expanded from macro 'fail_unless' _ck_assert_failed(__FILE__, __LINE__, "Assertion '"#expr"' failed" , ## __VA_ARGS__, NULL) ^~~~~~~~~~~ Also make test stimulus static. It's a constant vector of literals and is exclusively used in the test routine, need not occupy stack space. --- diff --git a/tests/analog.c b/tests/analog.c index 8490bf9f..904f0cea 100644 --- a/tests/analog.c +++ b/tests/analog.c @@ -598,7 +598,7 @@ END_TEST START_TEST(test_mult_rational) { - const struct sr_rational r[][3] = { + static const struct sr_rational r[][3] = { /* a * b = c */ { { 1, 1 }, { 1, 1 }, { 1, 1 }}, { { 2, 1 }, { 3, 1 }, { 6, 1 }}, @@ -625,13 +625,15 @@ START_TEST(test_mult_rational) { { 10000*3, 4 }, { -80000*3, 1 }, { -200000000*9, 1 }}, }; - for (unsigned i = 0; i < ARRAY_SIZE(r); i++) { - struct sr_rational res; + size_t i; + struct sr_rational res; + int rc; - int rc = sr_rational_mult(&res, &r[i][0], &r[i][1]); + for (i = 0; i < ARRAY_SIZE(r); i++) { + rc = sr_rational_mult(&res, &r[i][0], &r[i][1]); fail_unless(rc == SR_OK); fail_unless(sr_rational_eq(&res, &r[i][2]) == 1, - "sr_rational_mult() failed: [%d] %ld/%lu != %ld/%lu.", + "sr_rational_mult() failed: [%zu] %" PRIi64 "/%" PRIu64 " != %" PRIi64 "/%" PRIu64 ".", i, res.p, res.q, r[i][2].p, r[i][2].q); } } @@ -639,7 +641,7 @@ END_TEST START_TEST(test_div_rational) { - const struct sr_rational r[][3] = { + static const struct sr_rational r[][3] = { /* a * b = c */ { { 1, 1 }, { 1, 1 }, { 1, 1 }}, { { 2, 1 }, { 1, 3 }, { 6, 1 }}, @@ -662,19 +664,20 @@ START_TEST(test_div_rational) { { 10000*3, 4 }, { -1, 80000*3 }, { -200000000*9, 1 }}, }; - for (unsigned i = 0; i < ARRAY_SIZE(r); i++) { - struct sr_rational res; + size_t i; + struct sr_rational res; + int rc; - int rc = sr_rational_div(&res, &r[i][0], &r[i][1]); + for (i = 0; i < ARRAY_SIZE(r); i++) { + rc = sr_rational_div(&res, &r[i][0], &r[i][1]); fail_unless(rc == SR_OK); fail_unless(sr_rational_eq(&res, &r[i][2]) == 1, - "sr_rational_mult() failed: [%d] %ld/%lu != %ld/%lu.", + "sr_rational_mult() failed: [%zu] %" PRIi64 "/%" PRIu64 " != %" PRIi64 "/%" PRIu64 ".", i, res.p, res.q, r[i][2].p, r[i][2].q); } { - struct sr_rational res; - int rc = sr_rational_div(&res, &r[0][0], &((struct sr_rational){ 0, 5 })); + rc = sr_rational_div(&res, &r[0][0], &((struct sr_rational){ 0, 5 })); fail_unless(rc == SR_ERR_ARG); } diff --git a/tests/strutil.c b/tests/strutil.c index ec223332..f4bbe9ec 100644 --- a/tests/strutil.c +++ b/tests/strutil.c @@ -121,7 +121,7 @@ static void test_rational(const char *input, struct sr_rational expected) fail_unless(ret == SR_OK, "Unexpected rc for '%s': %d, errno %d.", input, ret, errno); fail_unless((expected.p == rational.p) && (expected.q == rational.q), - "Invalid result for '%s': %ld/%ld'.", + "Invalid result for '%s': %" PRIi64 "/%" PRIu64 "'.", input, rational.p, rational.q); }