diff options
author | Thomas Haller <thaller@redhat.com> | 2017-03-23 16:01:07 +0100 |
---|---|---|
committer | Thomas Haller <thaller@redhat.com> | 2017-03-23 19:06:02 +0100 |
commit | 25654cfa31b62d5ea02b165f720811c52afe4d1f (patch) | |
tree | 0b05868c31f32c91ed0efc78c0ab3060110599c4 | |
parent | d628ada05a43b6823a0606cf3ed4645855c52140 (diff) | |
download | NetworkManager-25654cfa31b62d5ea02b165f720811c52afe4d1f.tar.gz |
core: add nm_utils_10pow() utils
-rw-r--r-- | src/nm-core-utils.c | 31 | ||||
-rw-r--r-- | src/nm-core-utils.h | 2 | ||||
-rw-r--r-- | src/tests/test-general.c | 52 |
3 files changed, 85 insertions, 0 deletions
diff --git a/src/nm-core-utils.c b/src/nm-core-utils.c index 25ca18853e..11b2085b2c 100644 --- a/src/nm-core-utils.c +++ b/src/nm-core-utils.c @@ -155,6 +155,37 @@ _nm_singleton_instance_register_destruction (GObject *instance) /*****************************************************************************/ +static double +_10pow (guint16 ex) +{ + double v; + + if (ex == 0) + return 1.0; + + v = _10pow (ex / 2); + v = v * v; + if (ex % 2) + v *= 10; + return v; +} + +/*/ + * nm_utils_10pow: + * @ex: the exponent + * + * Returns: 10^ex or pow(10, ex) + */ +double +nm_utils_10pow (gint16 ex) +{ + if (ex >= 0) + return _10pow (ex); + return 1.0 / _10pow (- ((gint32) ex)); +} + +/*****************************************************************************/ + /* * nm_ethernet_address_is_valid: * @addr: pointer to a binary or ASCII Ethernet address diff --git a/src/nm-core-utils.h b/src/nm-core-utils.h index f9fa88a29b..fbeecf7a21 100644 --- a/src/nm-core-utils.h +++ b/src/nm-core-utils.h @@ -97,6 +97,8 @@ in_addr_t nm_utils_ip4_address_clear_host_address (in_addr_t addr, guint8 plen); const struct in6_addr *nm_utils_ip6_address_clear_host_address (struct in6_addr *dst, const struct in6_addr *src, guint8 plen); gboolean nm_utils_ip6_address_same_prefix (const struct in6_addr *addr_a, const struct in6_addr *addr_b, guint8 plen); +double nm_utils_10pow (gint16 e); + /** * nm_utils_ip6_route_metric_normalize: * @metric: the route metric diff --git a/src/tests/test-general.c b/src/tests/test-general.c index 498615822e..abc1534003 100644 --- a/src/tests/test-general.c +++ b/src/tests/test-general.c @@ -23,6 +23,9 @@ #include <string.h> #include <errno.h> +/* need math.h for isinf() and INFINITY. No need to link with -lm */ +#include <math.h> + #include "NetworkManagerUtils.h" #include "nm-core-internal.h" @@ -1653,6 +1656,53 @@ test_stable_id_generated_complete (void) /*****************************************************************************/ +static void +test_nm_utils_10pow (void) +{ +#define FLOAT_CMP(a, b) \ + G_STMT_START { \ + double _a = (a); \ + double _b = (b); \ + \ + if (isinf (_b)) \ + g_assert (isinf (_a)); \ + else if (_b >= 0.0 && _b <= 0.0) \ + g_assert (_a >= 0.0 && _a <= 0.0); \ + else { \ + double _x = (_a) - (_b); \ + g_assert (_b > 0.0); \ + if (_x < 0.0) \ + _x = -_x; \ + g_assert (_x / _b < 1E-10); \ + } \ + } G_STMT_END + + FLOAT_CMP (nm_utils_10pow (G_MININT16), 0.0); + FLOAT_CMP (nm_utils_10pow (-310), 0.0); + FLOAT_CMP (nm_utils_10pow (-309), 0.0); + FLOAT_CMP (nm_utils_10pow (-308), 1e-308); + FLOAT_CMP (nm_utils_10pow (-307), 1e-307); + FLOAT_CMP (nm_utils_10pow (-1), 1e-1); + FLOAT_CMP (nm_utils_10pow (-2), 1e-2); + FLOAT_CMP (nm_utils_10pow (0), 1e0); + FLOAT_CMP (nm_utils_10pow (1), 1e1); + FLOAT_CMP (nm_utils_10pow (2), 1e2); + FLOAT_CMP (nm_utils_10pow (3), 1e3); + FLOAT_CMP (nm_utils_10pow (4), 1e4); + FLOAT_CMP (nm_utils_10pow (5), 1e5); + FLOAT_CMP (nm_utils_10pow (6), 1e6); + FLOAT_CMP (nm_utils_10pow (7), 1e7); + FLOAT_CMP (nm_utils_10pow (122), 1e122); + FLOAT_CMP (nm_utils_10pow (200), 1e200); + FLOAT_CMP (nm_utils_10pow (307), 1e307); + FLOAT_CMP (nm_utils_10pow (308), 1e308); + FLOAT_CMP (nm_utils_10pow (309), INFINITY); + FLOAT_CMP (nm_utils_10pow (310), INFINITY); + FLOAT_CMP (nm_utils_10pow (G_MAXINT16), INFINITY); +} + +/*****************************************************************************/ + NMTST_DEFINE (); int @@ -1666,6 +1716,8 @@ main (int argc, char **argv) g_test_add_func ("/general/nm_utils_ip6_address_same_prefix", test_nm_utils_ip6_address_same_prefix); g_test_add_func ("/general/nm_utils_log_connection_diff", test_nm_utils_log_connection_diff); + g_test_add_func ("/general/10pow", test_nm_utils_10pow); + g_test_add_func ("/general/connection-match/basic", test_connection_match_basic); g_test_add_func ("/general/connection-match/ip6-method", test_connection_match_ip6_method); g_test_add_func ("/general/connection-match/ip6-method-ignore", test_connection_match_ip6_method_ignore); |