aboutsummaryrefslogtreecommitdiffstats
path: root/tests/utils
diff options
context:
space:
mode:
authorHarald Welte <laforge@gnumonks.org>2018-06-06 16:58:17 +0200
committerHarald Welte <laforge@gnumonks.org>2018-06-06 16:58:53 +0200
commit15a5f8de00c9c11a985ab16279ffae02b1e4667f (patch)
treeefacad0ecc80250d0258ed0cae5dda314680779c /tests/utils
parentdfb0b97f55452edc4ca4145f25c45b1d74014782 (diff)
Add osmo_isqrt32() to compute 32bit integer square root
Diffstat (limited to 'tests/utils')
-rw-r--r--tests/utils/utils_test.c22
-rw-r--r--tests/utils/utils_test.ok2
2 files changed, 24 insertions, 0 deletions
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 <stdio.h>
#include <ctype.h>
+#include <time.h>
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"'
'<buf-too-small>'
- NULL string becomes a "NULL" literal:
'NULL'
+
+Testing integer square-root