Skip to content

Commit 045f75b

Browse files
authored
Merge pull request #112 from linksplatform/issue-12-c83ed885
Validate sizes, order, balance and threads of all trees like GLib's g_tree_node_check
2 parents 52b4edf + cbf505d commit 045f75b

22 files changed

Lines changed: 1252 additions & 16 deletions

‎.github/workflows/cpp-test.yml‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,9 @@ jobs:
7272
os: ubuntu-24.04
7373
cc: clang
7474
cxx: clang++
75-
# Every attach and detach validates the whole tree (sizes, order and
76-
# the size balanced property) under AddressSanitizer and UBSan.
75+
# Every attach and detach validates the whole tree (sizes, order,
76+
# balance and, for the AVL tree, threads) under AddressSanitizer and
77+
# UBSan.
7778
- name: linux-gcc-sanitizers-validation
7879
os: ubuntu-24.04
7980
cc: gcc

‎.github/workflows/csharp.yml‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,8 +110,9 @@ jobs:
110110
node ../.github/scripts/check-csharp-coverage.mjs "$RUNNER_TEMP"/coverage/**/coverage.cobertura.xml
111111
112112
# ENABLE_TREE_AUTO_DEBUG_AND_VALIDATION makes every attach and detach check
113-
# the sizes of the whole tree, so the tests also prove that no operation
114-
# leaves a broken tree behind, even for a moment the tests do not inspect.
113+
# the whole tree (sizes, order, balance and, for the AVL tree, threads), so
114+
# the tests also prove that no operation leaves a broken tree behind, even
115+
# for a moment the tests do not inspect.
115116
# Release keeps the Debug.WriteLine tree dumps compiled out.
116117
treeValidation:
117118
needs: findChangedCsFiles

‎README.md‎

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -185,9 +185,20 @@ They check after every attach and detach that the trees keep their sizes, order
185185
and balance property, that the height stays within the AVL bound, that the
186186
recursive and the recursionless tree build identical trees with the minimum
187187
number of rotations, and that every line of the libraries is executed (100%
188-
line coverage for C# and C++). They also run with
189-
`ENABLE_TREE_AUTO_DEBUG_AND_VALIDATION`, which makes every tree validate itself
190-
after each change:
188+
line coverage for C# and C++).
189+
190+
`ValidateTree(root)` checks a whole tree like
191+
[`g_tree_node_check`](https://github.com/GNOME/glib/blob/798b99675a68f7b936006b386c4fdad17e455bb1/glib/gtree.c#L1334-L1388)
192+
of GLib and throws (`InvalidOperationException` in C#, `std::runtime_error` in
193+
C++) with the first broken rule: the size of every node, the order of the
194+
nodes (equal keys are allowed only in the size balanced trees), the size
195+
balanced property, and for the AVL tree the balance of every node (the height
196+
of the right subtree minus the height of the left one, within -1..1) and the
197+
threads (the left thread of a node points to the previous node and the right
198+
thread to the next one, 0 at both ends). The tests run it after every attach
199+
and detach, and check that it reports each broken rule. The tests also run
200+
with `ENABLE_TREE_AUTO_DEBUG_AND_VALIDATION`, which makes every tree validate
201+
itself after each change:
191202

192203
```sh
193204
cd csharp && dotnet test -c Release -p:TreeValidation=true
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,21 @@
11
#include <gtest/gtest.h>
2+
#include <algorithm>
23
#include <atomic>
34
#include <cstdint>
45
#include <cstdlib>
6+
#include <memory>
57
#include <new>
8+
#include <numeric>
9+
#include <random>
10+
#include <string>
11+
#include <vector>
612
#include <Platform.Collections.Methods.h>
713
#include "Trees.h"
814
#include "AllocationCounter.cpp"
915
#include "TreesTests.cpp"
1016
#include "TreeBalanceTests.cpp"
1117
#include "SizeBalancedTreeRotationTests.cpp"
1218
#include "TreeEdgeCaseTests.cpp"
19+
#include "TreeValidationTests.cpp"
1320
#include "DoublyLinkedListTests.cpp"
1421
#include "TreeAllocationTests.cpp"

‎cpp/Platform.Collections.Methods.Tests/InspectableTree.cpp‎

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -373,6 +373,104 @@
373373
this->SetSize(node, size);
374374
}
375375

376+
/// <summary>
377+
/// <para>Writes an AVL node directly, so that tests can build trees that break the rules of the AVL tree.</para>
378+
/// <para>Записывает узел АВЛ-дерева напрямую, чтобы тесты могли строить деревья, нарушающие правила АВЛ-дерева.</para>
379+
/// </summary>
380+
/// <param name="node">
381+
/// <para>The node to change.</para>
382+
/// <para>Изменяемый узел.</para>
383+
/// </param>
384+
/// <param name="left">
385+
/// <para>The new left link: the left child, or the previous node when leftIsChild is false.</para>
386+
/// <para>Новая левая ссылка: левый потомок или предыдущий узел, если leftIsChild равен false.</para>
387+
/// </param>
388+
/// <param name="leftIsChild">
389+
/// <para>Whether the left link points to a child rather than a thread.</para>
390+
/// <para>Указывает ли левая ссылка на потомка, а не на нить.</para>
391+
/// </param>
392+
/// <param name="right">
393+
/// <para>The new right link: the right child, or the next node when rightIsChild is false.</para>
394+
/// <para>Новая правая ссылка: правый потомок или следующий узел, если rightIsChild равен false.</para>
395+
/// </param>
396+
/// <param name="rightIsChild">
397+
/// <para>Whether the right link points to a child rather than a thread.</para>
398+
/// <para>Указывает ли правая ссылка на потомка, а не на нить.</para>
399+
/// </param>
400+
/// <param name="size">
401+
/// <para>The new size of the node, which is not checked.</para>
402+
/// <para>Новый размер узла, который не проверяется.</para>
403+
/// </param>
404+
/// <param name="balance">
405+
/// <para>The new balance of the node, which is not checked.</para>
406+
/// <para>Новый баланс узла, который не проверяется.</para>
407+
/// </param>
408+
public: void SetNode(TElement node, TElement left, bool leftIsChild, TElement right, bool rightIsChild, TElement size, std::int8_t balance)
409+
{
410+
this->SetLeft(node, left);
411+
this->SetLeftIsChild(node, leftIsChild);
412+
this->SetRight(node, right);
413+
this->SetRightIsChild(node, rightIsChild);
414+
this->SetSize(node, size);
415+
this->SetBalance(node, balance);
416+
}
417+
418+
/// <summary>
419+
/// <para>Writes the left link of the node directly and keeps its flag, so that tests can break a thread.</para>
420+
/// <para>Записывает левую ссылку узла напрямую, сохраняя её флаг, чтобы тесты могли сломать нить.</para>
421+
/// </summary>
422+
/// <param name="node">
423+
/// <para>The node to change.</para>
424+
/// <para>Изменяемый узел.</para>
425+
/// </param>
426+
/// <param name="left">
427+
/// <para>The new left link, which is not checked.</para>
428+
/// <para>Новая левая ссылка, которая не проверяется.</para>
429+
/// </param>
430+
public: void SetLeftLinkOf(TElement node, TElement left) { this->SetLeft(node, left); }
431+
432+
/// <summary>
433+
/// <para>Writes the right link of the node directly and keeps its flag, so that tests can break a thread.</para>
434+
/// <para>Записывает правую ссылку узла напрямую, сохраняя её флаг, чтобы тесты могли сломать нить.</para>
435+
/// </summary>
436+
/// <param name="node">
437+
/// <para>The node to change.</para>
438+
/// <para>Изменяемый узел.</para>
439+
/// </param>
440+
/// <param name="right">
441+
/// <para>The new right link, which is not checked.</para>
442+
/// <para>Новая правая ссылка, которая не проверяется.</para>
443+
/// </param>
444+
public: void SetRightLinkOf(TElement node, TElement right) { this->SetRight(node, right); }
445+
446+
/// <summary>
447+
/// <para>Writes the balance of the node directly, so that tests can break it.</para>
448+
/// <para>Записывает баланс узла напрямую, чтобы тесты могли его сломать.</para>
449+
/// </summary>
450+
/// <param name="node">
451+
/// <para>The node to change.</para>
452+
/// <para>Изменяемый узел.</para>
453+
/// </param>
454+
/// <param name="balance">
455+
/// <para>The new balance, which is not checked.</para>
456+
/// <para>Новый баланс, который не проверяется.</para>
457+
/// </param>
458+
public: void SetBalanceOf(TElement node, std::int8_t balance) { this->SetBalance(node, balance); }
459+
460+
/// <summary>
461+
/// <para>Writes the size of the node directly, so that tests can break it.</para>
462+
/// <para>Записывает размер узла напрямую, чтобы тесты могли его сломать.</para>
463+
/// </summary>
464+
/// <param name="node">
465+
/// <para>The node to change.</para>
466+
/// <para>Изменяемый узел.</para>
467+
/// </param>
468+
/// <param name="size">
469+
/// <para>The new size, which is not checked.</para>
470+
/// <para>Новый размер, который не проверяется.</para>
471+
/// </param>
472+
public: void SetSizeOf(TElement node, TElement size) { this->SetSize(node, size); }
473+
376474
/// <summary>
377475
/// <para>Determines whether the key of the first node, node >> KeyShift, is less than the key of the second node.</para>
378476
/// <para>Определяет, меньше ли ключ первого узла, node >> KeyShift, чем ключ второго узла.</para>

‎cpp/Platform.Collections.Methods.Tests/TreeBalanceTests.cpp‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,8 @@ namespace Platform::Collections::Methods::Tests
145145
auto height = Height(tree, tree.Root);
146146
auto limit = MaximumHeight(count);
147147
ASSERT_LE(height, limit) << "height " << height << " exceeds " << limit << " for " << count << " nodes";
148+
// Sizes, order and, for the AVL tree, balances and threads (like g_tree_node_check of GLib).
149+
ASSERT_NO_THROW(tree.ValidateTree(tree.Root));
148150
if (sizeBalanced)
149151
{
150152
ASSERT_NO_THROW(CheckSizeBalanced(tree, tree.Root));

0 commit comments

Comments
 (0)