[systemd-devel] [PATCH] test-hashmap.c: added unit-test for hashmap

Kay Sievers kay at vrfy.org
Fri Apr 26 14:58:15 PDT 2013


On Fri, Apr 26, 2013 at 8:49 PM, Daniel Buch <boogiewasthere at gmail.com> wrote:
> Version 2, with leak fix. This should be good enough to be pushed.

> +static void test_uint64_compare_func(void) {
> +        assert_se((uint64_t)trivial_compare_func("a", "a") == (uint64_t)0);
> +        assert_se((uint64_t)trivial_compare_func("a", "b") == (uint64_t)-1);
> +        assert_se((uint64_t)trivial_compare_func("b", "a") == (uint64_t)1);
> +}
> +
> +static void test_trivial_compare_func(void) {
> +        assert_se(trivial_compare_func("a", "a") == 0);
> +        assert_se(trivial_compare_func("a", "b") == -1);
> +        assert_se(trivial_compare_func("b", "a") == 1);
> +}

We cannot use "trivial" here to compare string values and expect
predictable results. "Trivial" will just compare the raw pointers,
which the compiler can arrange as it likes to; "a" is not necessarily
the same as another "a". No doubt, the hashmap.c code should get some
comments here explaining things and how they can/should be used, it's
really not obvious what's going on.

We can only compare numeric values with INT_TO_PTR(333) or something like that.

What is the  (uint64_t) cast supposed to test here? And we cast a -1
to an unsigned? That's intentional?

Thanks,
Kay


More information about the systemd-devel mailing list