diff --git a/test/unit/radix_tree_t.c b/test/unit/radix_tree_t.c index ea033c7d2..6b4494455 100644 --- a/test/unit/radix_tree_t.c +++ b/test/unit/radix_tree_t.c @@ -1,5 +1,5 @@ // Copyright (C) 2018 Red Hat, Inc. All rights reserved. -// +// // This file is part of LVM2. // // This copyrighted material is made available to anyone wishing to use, @@ -9,7 +9,7 @@ // You should have received a copy of the GNU Lesser General Public License // along with this program; if not, write to the Free Software Foundation, // Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA - + #include "units.h" #include "base/data-struct/radix-tree.h" #include "base/memory/container_of.h" @@ -348,7 +348,7 @@ static void test_remove_prefix(void *fixture) T_ASSERT(radix_tree_is_well_formed(rt)); - // remove keys in a sub range + // remove keys in a sub range k[0] = 21; T_ASSERT_EQUAL(radix_tree_remove_prefix(rt, k, 1), count); T_ASSERT(radix_tree_is_well_formed(rt)); @@ -542,7 +542,7 @@ static void test_remove_calls_dtr(void *fixture) } T_ASSERT(radix_tree_is_well_formed(rt)); - + // double check for (i = 0; i < DTR_COUNT; i++) { uint8_t *k = keys + (i * 3); @@ -597,7 +597,7 @@ static void test_destroy_calls_dtr(void *fixture) T_ASSERT(radix_tree_is_well_formed(rt)); } - + radix_tree_destroy(rt); T_ASSERT(c.c == DTR_COUNT); for (i = 0; i < DTR_COUNT; i++) @@ -810,6 +810,37 @@ static void test_bcache_scenario3(void *fixture) #include "test/unit/rt_case1.c" } +static bool _uniq_visit(struct radix_tree_iterator *it, + const void *key, size_t keylen, union radix_value v) +{ + struct visitor *vt = container_of(it, struct visitor, it); + T_ASSERT_EQUAL(v.n, 2); // always expecting value == 2 + vt->count++; + return true; +} + +static void test_uniq_insert(void *fixture) +{ + struct radix_tree *rt = fixture; + struct visitor vt = { .it.visit = _uniq_visit }; + uint8_t k[6] = "test1"; + uint8_t l[6] = "test2"; + union radix_value v1 = { .n = 1 }; + union radix_value v2 = { .n = 2 }; + + T_ASSERT(radix_tree_insert(rt, k, sizeof(k), v1)); + T_ASSERT(radix_tree_insert(rt, k, sizeof(k), v1)); + T_ASSERT_EQUAL(radix_tree_uniq_insert(rt, k, sizeof(k), v2), -1); + + T_ASSERT(radix_tree_uniq_insert(rt, l, sizeof(l), v1)); + T_ASSERT_EQUAL(radix_tree_uniq_insert(rt, l, sizeof(l), v2), -1); + + T_ASSERT(radix_tree_is_well_formed(rt)); + + radix_tree_iterate(rt, NULL, 0, &vt.it); + T_ASSERT_EQUAL(radix_tree_size(rt), vt.count); +} + //---------------------------------------------------------------- #define T(path, desc, fn) register_test(ts, "/base/data-struct/radix-tree/" path, desc, fn) @@ -846,6 +877,7 @@ void radix_tree_tests(struct dm_list *all_tests) T("bcache-scenario", "A specific series of keys from a bcache scenario", test_bcache_scenario); T("bcache-scenario-2", "A second series of keys from a bcache scenario", test_bcache_scenario2); T("bcache-scenario-3", "A third series of keys from a bcache scenario", test_bcache_scenario3); + T("uniq-insert", "Test insert with test for uniq key", test_uniq_insert); dm_list_add(all_tests, &ts->list); }