|
| 1 | +# test_api_consistency_matrix.rb - This file is part of the RubyTree package. |
| 2 | +# |
| 3 | +# Copyright (c) 2026 Anupam Sengupta |
| 4 | +# |
| 5 | +# All rights reserved. |
| 6 | +# |
| 7 | +# Redistribution and use in source and binary forms, with or without modification, |
| 8 | +# are permitted provided that the following conditions are met: |
| 9 | +# |
| 10 | +# - Redistributions of source code must retain the above copyright notice, this |
| 11 | +# list of conditions and the following disclaimer. |
| 12 | +# |
| 13 | +# - Redistributions in binary form must reproduce the above copyright notice, this |
| 14 | +# list of conditions and the following disclaimer in the documentation and/or |
| 15 | +# other materials provided with the distribution. |
| 16 | +# |
| 17 | +# - Neither the name of the organization nor the names of its contributors may |
| 18 | +# be used to endorse or promote products derived from this software without |
| 19 | +# specific prior written permission. |
| 20 | +# |
| 21 | +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 22 | +# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 23 | +# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 24 | +# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR |
| 25 | +# ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
| 26 | +# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
| 27 | +# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON |
| 28 | +# ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 29 | +# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 30 | +# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 31 | +# |
| 32 | +# frozen_string_literal: true |
| 33 | + |
| 34 | +require 'test/unit' |
| 35 | +require_relative '../lib/tree/aatree' |
| 36 | +require_relative '../lib/tree/avltree' |
| 37 | +require_relative '../lib/tree/binaryheap' |
| 38 | +require_relative '../lib/tree/binarymaxheap' |
| 39 | +require_relative '../lib/tree/binarysearchtree' |
| 40 | +require_relative '../lib/tree/btree' |
| 41 | +require_relative '../lib/tree/fenwicktree' |
| 42 | +require_relative '../lib/tree/intervaltree' |
| 43 | +require_relative '../lib/tree/orderstatistictree' |
| 44 | +require_relative '../lib/tree/redblacktree' |
| 45 | +require_relative '../lib/tree/segmenttree' |
| 46 | +require_relative '../lib/tree/splaytree' |
| 47 | +require_relative '../lib/tree/treap' |
| 48 | +require_relative '../lib/tree/trie' |
| 49 | + |
| 50 | +module TestTree |
| 51 | + # API consistency guardrails by semantic family. |
| 52 | + class TestApiConsistencyMatrix < Test::Unit::TestCase |
| 53 | + def test_ordered_node_tree_method_surface |
| 54 | + required = %i[insert add search delete key] |
| 55 | + |
| 56 | + [ |
| 57 | + Tree::BinarySearchTreeNode.new('root', 10), |
| 58 | + Tree::AvlTreeNode.new('root', 10), |
| 59 | + Tree::RedBlackTreeNode.new('root', 10), |
| 60 | + Tree::SplayTreeNode.new('root', 10), |
| 61 | + Tree::TreapNode.new('root', 10), |
| 62 | + Tree::OrderStatisticTreeNode.new('root', 10), |
| 63 | + Tree::IntervalTreeNode.new('root', 10..20) |
| 64 | + ].each do |tree| |
| 65 | + assert_methods_present(tree, required) |
| 66 | + end |
| 67 | + end |
| 68 | + |
| 69 | + def test_heap_tree_method_surface |
| 70 | + required = %i[insert add peek extract search delete key] |
| 71 | + |
| 72 | + [ |
| 73 | + Tree::BinaryHeapNode.new('root', 10), |
| 74 | + Tree::BinaryMaxHeapNode.new('root', 10) |
| 75 | + ].each do |tree| |
| 76 | + assert_methods_present(tree, required) |
| 77 | + end |
| 78 | + end |
| 79 | + |
| 80 | + def test_key_value_tree_method_surface |
| 81 | + required = %i[ |
| 82 | + insert << search delete [] []= size length each keys values to_a to_h |
| 83 | + as_json to_json <=> |
| 84 | + ] |
| 85 | + class_methods = %i[from_hash json_create] |
| 86 | + |
| 87 | + [Tree::AATree.new, Tree::BTree.new].each do |tree| |
| 88 | + assert_methods_present(tree, required) |
| 89 | + assert_methods_present(tree.class, class_methods) |
| 90 | + end |
| 91 | + end |
| 92 | + |
| 93 | + def test_aggregate_tree_method_surface |
| 94 | + required = %i[ |
| 95 | + length each update range_sum [] []= keys values to_a to_h as_json |
| 96 | + to_json <=> |
| 97 | + ] |
| 98 | + class_methods = %i[from_hash json_create] |
| 99 | + |
| 100 | + [Tree::FenwickTree.new(3), Tree::SegmentTree.new(3)].each do |tree| |
| 101 | + assert_methods_present(tree, required) |
| 102 | + assert_methods_present(tree.class, class_methods) |
| 103 | + end |
| 104 | + |
| 105 | + assert_equal(true, Tree::FenwickTree.new(3).respond_to?(:sum)) |
| 106 | + assert_equal(true, Tree::SegmentTree.new(3).respond_to?(:sum)) |
| 107 | + end |
| 108 | + |
| 109 | + def test_trie_method_surface |
| 110 | + trie = Tree::TrieNode.new('') |
| 111 | + required = %i[ |
| 112 | + insert << include? prefix? delete delete? words_with_prefix terminal? |
| 113 | + ] |
| 114 | + |
| 115 | + assert_methods_present(trie, required) |
| 116 | + end |
| 117 | + |
| 118 | + def test_intentionally_absent_shovel_for_array_backed_trees |
| 119 | + assert_equal(false, Tree::FenwickTree.new(3).respond_to?(:<<)) |
| 120 | + assert_equal(false, Tree::SegmentTree.new(3).respond_to?(:<<)) |
| 121 | + end |
| 122 | + |
| 123 | + private |
| 124 | + |
| 125 | + def assert_methods_present(target, methods) |
| 126 | + methods.each do |method_name| |
| 127 | + assert_equal( |
| 128 | + true, |
| 129 | + target.respond_to?(method_name), |
| 130 | + "Expected #{target.class} to respond to ##{method_name}" |
| 131 | + ) |
| 132 | + end |
| 133 | + end |
| 134 | + end |
| 135 | +end |
0 commit comments