From 15a5f8de00c9c11a985ab16279ffae02b1e4667f Mon Sep 17 00:00:00 2001 From: Harald Welte Date: Wed, 6 Jun 2018 16:58:17 +0200 Subject: Add osmo_isqrt32() to compute 32bit integer square root Change-Id: I2b96db6e037e72e92317fec874877e473a1cf909 --- tests/utils/utils_test.c | 22 ++++++++++++++++++++++ tests/utils/utils_test.ok | 2 ++ 2 files changed, 24 insertions(+) (limited to 'tests/utils') diff --git a/tests/utils/utils_test.c b/tests/utils/utils_test.c index a1243527..cb4e476c 100644 --- a/tests/utils/utils_test.c +++ b/tests/utils/utils_test.c @@ -27,6 +27,7 @@ #include #include +#include static void hexdump_test(void) { @@ -425,6 +426,26 @@ static void str_quote_test(void) printf("'%s'\n", osmo_quote_str_buf(NULL, -1, out_buf, 10)); } +static void isqrt_test(void) +{ + int i; + + printf("\nTesting integer square-root\n"); + srand(time(NULL)); + for (i = 0; i < 1024; i++) { + uint16_t x; + uint32_t r = rand(); + if (RAND_MAX < UINT16_MAX) + x = r * (UINT16_MAX/RAND_MAX); + else + x = r; + uint32_t sq = x*x; + uint32_t y = osmo_isqrt32(sq); + if (y != x) + printf("ERROR: x=%u, sq=%u, osmo_isqrt(%u) = %u\n", x, sq, sq, y); + } +} + int main(int argc, char **argv) { static const struct log_info log_info = {}; @@ -437,5 +458,6 @@ int main(int argc, char **argv) bcd_test(); str_escape_test(); str_quote_test(); + isqrt_test(); return 0; } diff --git a/tests/utils/utils_test.ok b/tests/utils/utils_test.ok index 5bc3896b..ea9216f0 100644 --- a/tests/utils/utils_test.ok +++ b/tests/utils/utils_test.ok @@ -137,3 +137,5 @@ NOT passed through. '"printable"' '' - NULL string becomes a "NULL" literal: 'NULL' + +Testing integer square-root -- cgit v1.2.3