Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue #254, fixing infinite loop in AVL tree #265

Merged
merged 1 commit into from
Aug 5, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion libuavcan/include/uavcan/util/avl_tree.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ class UAVCAN_EXPORT AvlTree : Noncopyable
static void appendToEndOf(Node* head, Node* newNode) {
Node* target = head;
while(target->equal_keys != UAVCAN_NULLPTR) {
target = head->equal_keys;
target = target->equal_keys;
}

target->equal_keys = newNode;
Expand Down
30 changes: 30 additions & 0 deletions libuavcan/test/util/avl_tree.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,36 @@ TEST(AvlTree, Sanity) {
EXPECT_EQ(6, pool.getNumUsedBlocks());
}

/**
* Reproduce then verify the fix for Issue #254.
*/
TEST(AvlTree, Issue254)
{
constexpr std::size_t block_size = 64;
constexpr std::size_t block_count = 24;
uavcan::PoolAllocator<block_size * block_count, block_size> pool;

AvlTree<Entry> tree(pool, 99999);

Entry* e1_0 = makeEntry(&pool, 1, 1);
Entry* e1_1 = makeEntry(&pool, 1, 1);
Entry* e1_2 = makeEntry(&pool, 1, 1);
Entry* e1_3 = makeEntry(&pool, 1, 1);

tree.insert(e1_0);
tree.insert(e1_1);
tree.insert(e1_2);
tree.insert(e1_3);

EXPECT_TRUE(tree.contains(e1_0));
EXPECT_TRUE(tree.contains(e1_1));
EXPECT_TRUE(tree.contains(e1_2));
EXPECT_TRUE(tree.contains(e1_3));
EXPECT_EQ(e1_0, tree.max());
EXPECT_EQ(4, tree.getSize());
EXPECT_EQ(8, pool.getNumUsedBlocks());
}

/* Test multiple entries with same 'key' */
TEST(AvlTree, MultipleEntriesPerKey) {
uavcan::PoolAllocator<64 * 24, 64> pool; // 4 (x2) entries capacity
Expand Down