Skip to content

Validate sizes, order, balance and threads of all trees like GLib's g_tree_node_check - #112

Merged
konard merged 13 commits into
mainfrom
issue-12-c83ed885
Dec 4, 2026
Merged

konard merged 13 commits into
mainfrom
issue-12-c83ed885

Conversation

@konard

@konard konard commented Sep 14, 2025 •

Copy link
Copy Markdown
Member

Summary

Adds the validation logic of g_tree_node_check and g_tree_node_dump from GLib's gtree.c to all trees, in C# and in C++.

ValidateTree(root) checks a whole tree. It throws the first broken rule (InvalidOperationException in C#, std::runtime_error in C++), with the same messages in both languages:

Rule Trees
The size of every node is the size of its subtree (the existing ValidateSizes). All
The nodes are in order: strictly for the AVL tree, which rejects equal keys, and non-strictly for the size balanced trees, which keep them. All
Chen's size balanced property: each subtree is at least as large as both subtrees of its sibling. Both size balanced trees
The balance of every node equals the height of the right subtree minus the height of the left one, as in g_tree_node_check, and is within -1..1. AVL
The left thread of every node points to the previous node, and the right thread to the next one, with 0 at both ends. This is g_tree_node_check's check that the neighbours point back to the node. AVL

Like g_tree_node_dump, PrintNode of the C# AVL tree now prints the nonzero threads of a node (<left, >right).

With ENABLE_TREE_AUTO_DEBUG_AND_VALIDATION, Attach and Detach now run ValidateTree after the operation instead of only ValidateSizes. Before the operation they still check only sizes, because the size balanced trees rebuild trees that older code built without the size balanced property.

Tests and checks (per the review comment)

  • New TreeValidationTests (C# and C++):
    • Valid AVL, size balanced and recursionless size balanced trees pass the validation after each of 64 random attaches and detaches.
    • Equal keys pass in the size balanced trees and fail the strict order in the AVL tree.
    • Each rule is broken by hand, and its exact message is checked: order, size, size balance on both sides, each thread including both ends, balance, and the AVL property.
  • AssertBalanced in TreeBalanceTests now runs ValidateTree after every attach and detach of the existing workloads.
  • The lists were already compared against a reference list after each of 20,000 random operations (size, both ends, both links of every element), so they needed no new checks.
  • ValidateTree, ValidateSizeBalance, the AVL test setters and the new tests have XML documentation in English and Russian, so the Doxygen documentation check of main passes after the merge.
  • No existing operation produced an invalid tree, so the new checks proved no library bug.

Local results:

  • C#: all tests pass in Release and with -p:TreeValidation=true, with 100% line coverage.
  • C++: all tests pass in Release and with TREE_VALIDATION=1, with 100% line coverage of the headers (908/908, gcovr 8.8).

The README Tests section, the release notes of the unreleased C# 1.0.1 and C++ 0.4.0 packages, and dev/log/issues/12/pulls/112 describe the checks.

How to verify

cd csharp && dotnet test -c Release --filter FullyQualifiedName~TreeValidation
cd csharp && dotnet test -c Release -p:TreeValidation=true
TREE_VALIDATION=1 cpp/build-and-test.sh

Fixes #12

Adding CLAUDE.md with task information for AI processing.
This file will be removed when the task is complete.

Issue: #12
@konard konard self-assigned this Sep 14, 2025
Implements validation logic from GNOME/glib/gtree.c for tree integrity checks including:
- Balance factor validation (comparing calculated vs stored balance)
- AVL property validation (balance factor must be -1, 0, or 1)
- Height calculation for tree nodes
- Recursive validation of subtrees
- Integration with existing size validation

The validation is enabled by ENABLE_TREE_AUTO_DEBUG_AND_VALIDATION and follows the
same patterns as the GLib implementation. Includes comprehensive test coverage.

Fixes issue #12

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
@konard konard changed the title [WIP] Add validation logic from glib/gtree.c Add GLib-style validation logic for AVL tree structures Sep 14, 2025
@konard
konard marked this pull request as ready for review September 14, 2025 08:21
@konard

konard commented Dec 3, 2026

Copy link
Copy Markdown
Member Author

We need to double check we have full tests coverage and all tests and quality checks expected for trees and nothing is missing, so all our collection algorithms are fully covered with tests and checks, if we prove with any tests we have a bug - we fix it here.

@konard
konard marked this pull request as draft December 4, 2026 01:55
@konard

konard commented Dec 4, 2026

Copy link
Copy Markdown
Member Author

🤖 AI Work Session Started

Started at 2026-12-04T01:55:17.284Z. Please wait for it to finish before giving feedback.

Runtime: solve v2.50.0 · tool claude · model opus · task image konard/hive-mind-dind:2.50.0@sha256:39bda1084d8f7e24e6f1f758c32f40113f68d385759aa862c36b72e9b7570bb6

…e_node_check

ValidateTree checks the sizes and the order of every tree, the size balanced
tree properties of both size balanced trees, and the balances, the AVL
property and the threads of the AVL tree. The validation mode runs it after
every attach and detach, and the balance tests run it after every operation.
The AVL tree prints its threads like g_tree_node_dump.
…ee_node_check

ValidateTree checks sizes and order of every tree, the size balanced
property of the size balanced trees, and balances and threads of the AVL
tree. Attach and Detach run it under ENABLE_TREE_AUTO_DEBUG_AND_VALIDATION.
TreeValidationTests mirror the C# tests, and the README describes the
checks.
@konard konard changed the title Add GLib-style validation logic for AVL tree structures Validate sizes, order, balance and threads of all trees like GLib's g_tree_node_check Dec 4, 2026
@konard
konard marked this pull request as ready for review December 4, 2026 02:31
…k of an empty tree

Codacy (SonarCSharp S3241) flagged the returned height that only the
recursive calls use. An empty C# tree no longer reads node 0, as in C++.
# Conflicts:
#	cpp/Platform.Collections.Methods.Tests/InspectableTree.cpp
#	cpp/Platform.Collections.Methods/Platform.Collections.Methods.TemplateLibrary.nuspec
#	cpp/Platform.Collections.Methods/Trees/SizeBalancedTreeMethods.h
#	cpp/Platform.Collections.Methods/Trees/SizedAndThreadedAVLBalancedTreeMethods.h
#	csharp/Platform.Collections.Methods.Tests/InspectableSizedAndThreadedAVLBalancedTree.cs
#	csharp/Platform.Collections.Methods/Platform.Collections.Methods.csproj
#	csharp/Platform.Collections.Methods/Trees/SizeBalancedTreeMethods.cs
#	csharp/Platform.Collections.Methods/Trees/SizedBinaryTreeMethodsBase.cs
Main now checks with Doxygen that every public and protected C# and C++
element is documented, so ValidateTree, ValidateSizeBalance, the AVL test
setters and the validation tests get bilingual summaries, parameters,
returns and exceptions.
@konard

konard commented Dec 4, 2026

Copy link
Copy Markdown
Member Author

Working session summary

PR #112 is now mergeable with main again, and every CI check passes on the latest commit (cbf505d): #112

  • Conflicts with main: after the documentation PR (Document every public C# and C++ element in English and Russian, and generate the C++ API documentation #111) merged into main, eight files conflicted with this branch. I kept both sides in every case, so main's longer docs and the new validation code are both there. A diff against main shows that only intended lines were removed: the two ValidateSizes calls that are now ValidateTree, and two release notes, which now combine both PRs' sentences.
  • New documentation check: main now uses Doxygen to require docs on every public and protected member. It flagged 24 places from this PR, plus one missing type parameter in the C++ tests. I added English and Russian docs to ValidateTree, ValidateSizeBalance, the AVL test setters, and the new validation tests in both languages, and committed this separately as 172b407.
  • Checked locally before pushing:
    • The C++ tests pass in Release.
    • All 96 C# tests pass.
    • All 206 script tests pass.
    • The XML docs, Doxygen coverage, file-size and markdownlint checks pass.
  • CI on GitHub: the C++ tests pass on gcc, clang, macOS, MSVC and the sanitizer run with tree validation on. The C# and C++ coverage jobs, CodeQL, Codacy, the documentation build and the workflow checks also pass. Only the release, deploy and auto-merge jobs were skipped; they don't run on a pull request.
  • PR and log: the PR description still contains "Fixes Add validation logic from glib/gtree.c #12" and now mentions the docs. The work log in dev/log/issues/12/pulls/112 is updated too.
  • Review and comments: more than 15 minutes after the push, no review bot had left a review or comment. The only feedback on the PR and issue is still konard's coverage comment, which earlier work on this PR already covered.

The PR is out of draft and ready to merge.


This summary was automatically extracted from the AI working session output.

@konard

konard commented Dec 4, 2026

Copy link
Copy Markdown
Member Author

🤖 Solution Draft Log

This log file contains the complete execution trace of the AI solution draft process.

💰 Cost estimation:

  • Public pricing estimate: $7.811167
  • Calculated by Anthropic: $7.813429
  • Difference: $0.002262 (+0.03%)

📊 Context and tokens usage:

Claude Opus 5.5: (4 sub-sessions)

  1. 117.3K / 1M (12%) input tokens, 14.8K / 128K (12%) output tokens
  2. 115.8K / 1M (12%) input tokens, 37.5K / 128K (29%) output tokens
  3. 113.8K / 1M (11%) input tokens, 25.3K / 128K (20%) output tokens
  4. 85.8K / 1M (9%) input tokens, 18.5K / 128K (14%) output tokens

Total: (19.3K new + 373.3K cache writes + 12.1M cache reads) input tokens, 116.8K output tokens, $7.811167 cost

🤖 Models used:

  • Tool: Anthropic Claude Code
  • Requested: opus (claude-opus-5-5)
  • Thinking level: high (~23999 tokens)
  • Model: Claude Opus 5.5 (claude-opus-5-5)

📎 Log file uploaded as Gist (5642KB)


This AI session has ended. Monitoring of this pull request continues. Feel free to review and add feedback while checks are running.

@konard
konard merged commit 045f75b into main Dec 4, 2026
39 checks passed
@konard

konard commented Dec 4, 2026

Copy link
Copy Markdown
Member Author

🎉 Auto-merged

This pull request has been automatically merged by hive-mind.

  • All CI checks have passed

Linked issues

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add validation logic from glib/gtree.c

1 participant