github.com/songzhibin97/go-baseutils@v0.0.2-0.20240302024150-487d8ce9c082/structure/trees/btree/btree_test.go (about)

     1  package btree
     2  
     3  import (
     4  	"encoding/json"
     5  	"reflect"
     6  	"strings"
     7  	"testing"
     8  )
     9  
    10  func TestBTreeGet1(t *testing.T) {
    11  	tree := NewWithIntComparator[string](3)
    12  	tree.Put(1, "a")
    13  	tree.Put(2, "b")
    14  	tree.Put(3, "c")
    15  	tree.Put(4, "d")
    16  	tree.Put(5, "e")
    17  	tree.Put(6, "f")
    18  	tree.Put(7, "g")
    19  
    20  	tests := [][]interface{}{
    21  		{0, "", false},
    22  		{1, "a", true},
    23  		{2, "b", true},
    24  		{3, "c", true},
    25  		{4, "d", true},
    26  		{5, "e", true},
    27  		{6, "f", true},
    28  		{7, "g", true},
    29  		{8, "", false},
    30  	}
    31  
    32  	for _, test := range tests {
    33  		if value, found := tree.Get(test[0].(int)); value != test[1] || found != test[2] {
    34  			t.Errorf("Got %v,%v expected %v,%v", value, found, test[1], test[2])
    35  		}
    36  	}
    37  }
    38  
    39  func TestBTreeGet2(t *testing.T) {
    40  	tree := NewWithIntComparator[string](3)
    41  	tree.Put(7, "g")
    42  	tree.Put(9, "i")
    43  	tree.Put(10, "j")
    44  	tree.Put(6, "f")
    45  	tree.Put(3, "c")
    46  	tree.Put(4, "d")
    47  	tree.Put(5, "e")
    48  	tree.Put(8, "h")
    49  	tree.Put(2, "b")
    50  	tree.Put(1, "a")
    51  
    52  	tests := [][]interface{}{
    53  		{0, "", false},
    54  		{1, "a", true},
    55  		{2, "b", true},
    56  		{3, "c", true},
    57  		{4, "d", true},
    58  		{5, "e", true},
    59  		{6, "f", true},
    60  		{7, "g", true},
    61  		{8, "h", true},
    62  		{9, "i", true},
    63  		{10, "j", true},
    64  		{11, "", false},
    65  	}
    66  
    67  	for _, test := range tests {
    68  		if value, found := tree.Get(test[0].(int)); value != test[1] || found != test[2] {
    69  			t.Errorf("Got %v,%v expected %v,%v", value, found, test[1], test[2])
    70  		}
    71  	}
    72  }
    73  
    74  func TestBTreeGet3(t *testing.T) {
    75  	tree := NewWithIntComparator[string](3)
    76  
    77  	if actualValue := tree.Size(); actualValue != 0 {
    78  		t.Errorf("Got %v expected %v", actualValue, 0)
    79  	}
    80  
    81  	if actualValue := tree.GetNode(2).Size(); actualValue != 0 {
    82  		t.Errorf("Got %v expected %v", actualValue, 0)
    83  	}
    84  
    85  	tree.Put(1, "x") // 1->x
    86  	tree.Put(2, "b") // 1->x, 2->b (in order)
    87  	tree.Put(1, "a") // 1->a, 2->b (in order, replacement)
    88  	tree.Put(3, "c") // 1->a, 2->b, 3->c (in order)
    89  	tree.Put(4, "d") // 1->a, 2->b, 3->c, 4->d (in order)
    90  	tree.Put(5, "e") // 1->a, 2->b, 3->c, 4->d, 5->e (in order)
    91  	tree.Put(6, "f") // 1->a, 2->b, 3->c, 4->d, 5->e, 6->f (in order)
    92  	tree.Put(7, "g") // 1->a, 2->b, 3->c, 4->d, 5->e, 6->f, 7->g (in order)
    93  
    94  	// BTree
    95  	//         1
    96  	//     2
    97  	//         3
    98  	// 4
    99  	//         5
   100  	//     6
   101  	//         7
   102  
   103  	if actualValue := tree.Size(); actualValue != 7 {
   104  		t.Errorf("Got %v expected %v", actualValue, 7)
   105  	}
   106  
   107  	if actualValue := tree.GetNode(2).Size(); actualValue != 3 {
   108  		t.Errorf("Got %v expected %v", actualValue, 3)
   109  	}
   110  
   111  	if actualValue := tree.GetNode(4).Size(); actualValue != 7 {
   112  		t.Errorf("Got %v expected %v", actualValue, 7)
   113  	}
   114  
   115  	if actualValue := tree.GetNode(8).Size(); actualValue != 0 {
   116  		t.Errorf("Got %v expected %v", actualValue, 0)
   117  	}
   118  }
   119  
   120  func TestBTreePut1(t *testing.T) {
   121  	// https://upload.wikimedia.org/wikipedia/commons/3/33/B_tree_insertion_example.png
   122  	tree := NewWithIntComparator[int](3)
   123  	assertValidTree(t, tree, 0)
   124  
   125  	tree.Put(1, 0)
   126  	assertValidTree(t, tree, 1)
   127  	assertValidTreeNode(t, tree.Root, 1, 0, []int{1}, false)
   128  
   129  	tree.Put(2, 1)
   130  	assertValidTree(t, tree, 2)
   131  	assertValidTreeNode(t, tree.Root, 2, 0, []int{1, 2}, false)
   132  
   133  	tree.Put(3, 2)
   134  	assertValidTree(t, tree, 3)
   135  	assertValidTreeNode(t, tree.Root, 1, 2, []int{2}, false)
   136  	assertValidTreeNode(t, tree.Root.Children[0], 1, 0, []int{1}, true)
   137  	assertValidTreeNode(t, tree.Root.Children[1], 1, 0, []int{3}, true)
   138  
   139  	tree.Put(4, 2)
   140  	assertValidTree(t, tree, 4)
   141  	assertValidTreeNode(t, tree.Root, 1, 2, []int{2}, false)
   142  	assertValidTreeNode(t, tree.Root.Children[0], 1, 0, []int{1}, true)
   143  	assertValidTreeNode(t, tree.Root.Children[1], 2, 0, []int{3, 4}, true)
   144  
   145  	tree.Put(5, 2)
   146  	assertValidTree(t, tree, 5)
   147  	assertValidTreeNode(t, tree.Root, 2, 3, []int{2, 4}, false)
   148  	assertValidTreeNode(t, tree.Root.Children[0], 1, 0, []int{1}, true)
   149  	assertValidTreeNode(t, tree.Root.Children[1], 1, 0, []int{3}, true)
   150  	assertValidTreeNode(t, tree.Root.Children[2], 1, 0, []int{5}, true)
   151  
   152  	tree.Put(6, 2)
   153  	assertValidTree(t, tree, 6)
   154  	assertValidTreeNode(t, tree.Root, 2, 3, []int{2, 4}, false)
   155  	assertValidTreeNode(t, tree.Root.Children[0], 1, 0, []int{1}, true)
   156  	assertValidTreeNode(t, tree.Root.Children[1], 1, 0, []int{3}, true)
   157  	assertValidTreeNode(t, tree.Root.Children[2], 2, 0, []int{5, 6}, true)
   158  
   159  	tree.Put(7, 2)
   160  	assertValidTree(t, tree, 7)
   161  	assertValidTreeNode(t, tree.Root, 1, 2, []int{4}, false)
   162  	assertValidTreeNode(t, tree.Root.Children[0], 1, 2, []int{2}, true)
   163  	assertValidTreeNode(t, tree.Root.Children[1], 1, 2, []int{6}, true)
   164  	assertValidTreeNode(t, tree.Root.Children[0].Children[0], 1, 0, []int{1}, true)
   165  	assertValidTreeNode(t, tree.Root.Children[0].Children[1], 1, 0, []int{3}, true)
   166  	assertValidTreeNode(t, tree.Root.Children[1].Children[0], 1, 0, []int{5}, true)
   167  	assertValidTreeNode(t, tree.Root.Children[1].Children[1], 1, 0, []int{7}, true)
   168  }
   169  
   170  func TestBTreePut2(t *testing.T) {
   171  	tree := NewWithIntComparator[int](4)
   172  	assertValidTree(t, tree, 0)
   173  
   174  	tree.Put(0, 0)
   175  	assertValidTree(t, tree, 1)
   176  	assertValidTreeNode(t, tree.Root, 1, 0, []int{0}, false)
   177  
   178  	tree.Put(2, 2)
   179  	assertValidTree(t, tree, 2)
   180  	assertValidTreeNode(t, tree.Root, 2, 0, []int{0, 2}, false)
   181  
   182  	tree.Put(1, 1)
   183  	assertValidTree(t, tree, 3)
   184  	assertValidTreeNode(t, tree.Root, 3, 0, []int{0, 1, 2}, false)
   185  
   186  	tree.Put(1, 1)
   187  	assertValidTree(t, tree, 3)
   188  	assertValidTreeNode(t, tree.Root, 3, 0, []int{0, 1, 2}, false)
   189  
   190  	tree.Put(3, 3)
   191  	assertValidTree(t, tree, 4)
   192  	assertValidTreeNode(t, tree.Root, 1, 2, []int{1}, false)
   193  	assertValidTreeNode(t, tree.Root.Children[0], 1, 0, []int{0}, true)
   194  	assertValidTreeNode(t, tree.Root.Children[1], 2, 0, []int{2, 3}, true)
   195  
   196  	tree.Put(4, 4)
   197  	assertValidTree(t, tree, 5)
   198  	assertValidTreeNode(t, tree.Root, 1, 2, []int{1}, false)
   199  	assertValidTreeNode(t, tree.Root.Children[0], 1, 0, []int{0}, true)
   200  	assertValidTreeNode(t, tree.Root.Children[1], 3, 0, []int{2, 3, 4}, true)
   201  
   202  	tree.Put(5, 5)
   203  	assertValidTree(t, tree, 6)
   204  	assertValidTreeNode(t, tree.Root, 2, 3, []int{1, 3}, false)
   205  	assertValidTreeNode(t, tree.Root.Children[0], 1, 0, []int{0}, true)
   206  	assertValidTreeNode(t, tree.Root.Children[1], 1, 0, []int{2}, true)
   207  	assertValidTreeNode(t, tree.Root.Children[2], 2, 0, []int{4, 5}, true)
   208  }
   209  
   210  func TestBTreePut3(t *testing.T) {
   211  	// http://www.geeksforgeeks.org/b-tree-set-1-insert-2/
   212  	tree := NewWithIntComparator[int](6)
   213  	assertValidTree(t, tree, 0)
   214  
   215  	tree.Put(10, 0)
   216  	assertValidTree(t, tree, 1)
   217  	assertValidTreeNode(t, tree.Root, 1, 0, []int{10}, false)
   218  
   219  	tree.Put(20, 1)
   220  	assertValidTree(t, tree, 2)
   221  	assertValidTreeNode(t, tree.Root, 2, 0, []int{10, 20}, false)
   222  
   223  	tree.Put(30, 2)
   224  	assertValidTree(t, tree, 3)
   225  	assertValidTreeNode(t, tree.Root, 3, 0, []int{10, 20, 30}, false)
   226  
   227  	tree.Put(40, 3)
   228  	assertValidTree(t, tree, 4)
   229  	assertValidTreeNode(t, tree.Root, 4, 0, []int{10, 20, 30, 40}, false)
   230  
   231  	tree.Put(50, 4)
   232  	assertValidTree(t, tree, 5)
   233  	assertValidTreeNode(t, tree.Root, 5, 0, []int{10, 20, 30, 40, 50}, false)
   234  
   235  	tree.Put(60, 5)
   236  	assertValidTree(t, tree, 6)
   237  	assertValidTreeNode(t, tree.Root, 1, 2, []int{30}, false)
   238  	assertValidTreeNode(t, tree.Root.Children[0], 2, 0, []int{10, 20}, true)
   239  	assertValidTreeNode(t, tree.Root.Children[1], 3, 0, []int{40, 50, 60}, true)
   240  
   241  	tree.Put(70, 6)
   242  	assertValidTree(t, tree, 7)
   243  	assertValidTreeNode(t, tree.Root, 1, 2, []int{30}, false)
   244  	assertValidTreeNode(t, tree.Root.Children[0], 2, 0, []int{10, 20}, true)
   245  	assertValidTreeNode(t, tree.Root.Children[1], 4, 0, []int{40, 50, 60, 70}, true)
   246  
   247  	tree.Put(80, 7)
   248  	assertValidTree(t, tree, 8)
   249  	assertValidTreeNode(t, tree.Root, 1, 2, []int{30}, false)
   250  	assertValidTreeNode(t, tree.Root.Children[0], 2, 0, []int{10, 20}, true)
   251  	assertValidTreeNode(t, tree.Root.Children[1], 5, 0, []int{40, 50, 60, 70, 80}, true)
   252  
   253  	tree.Put(90, 8)
   254  	assertValidTree(t, tree, 9)
   255  	assertValidTreeNode(t, tree.Root, 2, 3, []int{30, 60}, false)
   256  	assertValidTreeNode(t, tree.Root.Children[0], 2, 0, []int{10, 20}, true)
   257  	assertValidTreeNode(t, tree.Root.Children[1], 2, 0, []int{40, 50}, true)
   258  	assertValidTreeNode(t, tree.Root.Children[2], 3, 0, []int{70, 80, 90}, true)
   259  }
   260  
   261  func TestBTreePut4(t *testing.T) {
   262  	tree := NewWithIntComparator[any](3)
   263  	assertValidTree(t, tree, 0)
   264  
   265  	tree.Put(6, nil)
   266  	assertValidTree(t, tree, 1)
   267  	assertValidTreeNode(t, tree.Root, 1, 0, []int{6}, false)
   268  
   269  	tree.Put(5, nil)
   270  	assertValidTree(t, tree, 2)
   271  	assertValidTreeNode(t, tree.Root, 2, 0, []int{5, 6}, false)
   272  
   273  	tree.Put(4, nil)
   274  	assertValidTree(t, tree, 3)
   275  	assertValidTreeNode(t, tree.Root, 1, 2, []int{5}, false)
   276  	assertValidTreeNode(t, tree.Root.Children[0], 1, 0, []int{4}, true)
   277  	assertValidTreeNode(t, tree.Root.Children[1], 1, 0, []int{6}, true)
   278  
   279  	tree.Put(3, nil)
   280  	assertValidTree(t, tree, 4)
   281  	assertValidTreeNode(t, tree.Root, 1, 2, []int{5}, false)
   282  	assertValidTreeNode(t, tree.Root.Children[0], 2, 0, []int{3, 4}, true)
   283  	assertValidTreeNode(t, tree.Root.Children[1], 1, 0, []int{6}, true)
   284  
   285  	tree.Put(2, nil)
   286  	assertValidTree(t, tree, 5)
   287  	assertValidTreeNode(t, tree.Root, 2, 3, []int{3, 5}, false)
   288  	assertValidTreeNode(t, tree.Root.Children[0], 1, 0, []int{2}, true)
   289  	assertValidTreeNode(t, tree.Root.Children[1], 1, 0, []int{4}, true)
   290  	assertValidTreeNode(t, tree.Root.Children[2], 1, 0, []int{6}, true)
   291  
   292  	tree.Put(1, nil)
   293  	assertValidTree(t, tree, 6)
   294  	assertValidTreeNode(t, tree.Root, 2, 3, []int{3, 5}, false)
   295  	assertValidTreeNode(t, tree.Root.Children[0], 2, 0, []int{1, 2}, true)
   296  	assertValidTreeNode(t, tree.Root.Children[1], 1, 0, []int{4}, true)
   297  	assertValidTreeNode(t, tree.Root.Children[2], 1, 0, []int{6}, true)
   298  
   299  	tree.Put(0, nil)
   300  	assertValidTree(t, tree, 7)
   301  	assertValidTreeNode(t, tree.Root, 1, 2, []int{3}, false)
   302  	assertValidTreeNode(t, tree.Root.Children[0], 1, 2, []int{1}, true)
   303  	assertValidTreeNode(t, tree.Root.Children[1], 1, 2, []int{5}, true)
   304  	assertValidTreeNode(t, tree.Root.Children[0].Children[0], 1, 0, []int{0}, true)
   305  	assertValidTreeNode(t, tree.Root.Children[0].Children[1], 1, 0, []int{2}, true)
   306  	assertValidTreeNode(t, tree.Root.Children[1].Children[0], 1, 0, []int{4}, true)
   307  	assertValidTreeNode(t, tree.Root.Children[1].Children[1], 1, 0, []int{6}, true)
   308  
   309  	tree.Put(-1, nil)
   310  	assertValidTree(t, tree, 8)
   311  	assertValidTreeNode(t, tree.Root, 1, 2, []int{3}, false)
   312  	assertValidTreeNode(t, tree.Root.Children[0], 1, 2, []int{1}, true)
   313  	assertValidTreeNode(t, tree.Root.Children[1], 1, 2, []int{5}, true)
   314  	assertValidTreeNode(t, tree.Root.Children[0].Children[0], 2, 0, []int{-1, 0}, true)
   315  	assertValidTreeNode(t, tree.Root.Children[0].Children[1], 1, 0, []int{2}, true)
   316  	assertValidTreeNode(t, tree.Root.Children[1].Children[0], 1, 0, []int{4}, true)
   317  	assertValidTreeNode(t, tree.Root.Children[1].Children[1], 1, 0, []int{6}, true)
   318  
   319  	tree.Put(-2, nil)
   320  	assertValidTree(t, tree, 9)
   321  	assertValidTreeNode(t, tree.Root, 1, 2, []int{3}, false)
   322  	assertValidTreeNode(t, tree.Root.Children[0], 2, 3, []int{-1, 1}, true)
   323  	assertValidTreeNode(t, tree.Root.Children[1], 1, 2, []int{5}, true)
   324  	assertValidTreeNode(t, tree.Root.Children[0].Children[0], 1, 0, []int{-2}, true)
   325  	assertValidTreeNode(t, tree.Root.Children[0].Children[1], 1, 0, []int{0}, true)
   326  	assertValidTreeNode(t, tree.Root.Children[0].Children[2], 1, 0, []int{2}, true)
   327  	assertValidTreeNode(t, tree.Root.Children[1].Children[0], 1, 0, []int{4}, true)
   328  	assertValidTreeNode(t, tree.Root.Children[1].Children[1], 1, 0, []int{6}, true)
   329  
   330  	tree.Put(-3, nil)
   331  	assertValidTree(t, tree, 10)
   332  	assertValidTreeNode(t, tree.Root, 1, 2, []int{3}, false)
   333  	assertValidTreeNode(t, tree.Root.Children[0], 2, 3, []int{-1, 1}, true)
   334  	assertValidTreeNode(t, tree.Root.Children[1], 1, 2, []int{5}, true)
   335  	assertValidTreeNode(t, tree.Root.Children[0].Children[0], 2, 0, []int{-3, -2}, true)
   336  	assertValidTreeNode(t, tree.Root.Children[0].Children[1], 1, 0, []int{0}, true)
   337  	assertValidTreeNode(t, tree.Root.Children[0].Children[2], 1, 0, []int{2}, true)
   338  	assertValidTreeNode(t, tree.Root.Children[1].Children[0], 1, 0, []int{4}, true)
   339  	assertValidTreeNode(t, tree.Root.Children[1].Children[1], 1, 0, []int{6}, true)
   340  
   341  	tree.Put(-4, nil)
   342  	assertValidTree(t, tree, 11)
   343  	assertValidTreeNode(t, tree.Root, 2, 3, []int{-1, 3}, false)
   344  	assertValidTreeNode(t, tree.Root.Children[0], 1, 2, []int{-3}, true)
   345  	assertValidTreeNode(t, tree.Root.Children[1], 1, 2, []int{1}, true)
   346  	assertValidTreeNode(t, tree.Root.Children[2], 1, 2, []int{5}, true)
   347  	assertValidTreeNode(t, tree.Root.Children[0].Children[0], 1, 0, []int{-4}, true)
   348  	assertValidTreeNode(t, tree.Root.Children[0].Children[1], 1, 0, []int{-2}, true)
   349  	assertValidTreeNode(t, tree.Root.Children[1].Children[0], 1, 0, []int{0}, true)
   350  	assertValidTreeNode(t, tree.Root.Children[1].Children[1], 1, 0, []int{2}, true)
   351  	assertValidTreeNode(t, tree.Root.Children[2].Children[0], 1, 0, []int{4}, true)
   352  	assertValidTreeNode(t, tree.Root.Children[2].Children[1], 1, 0, []int{6}, true)
   353  }
   354  
   355  func TestBTreeRemove1(t *testing.T) {
   356  	// empty
   357  	tree := NewWithIntComparator[int](3)
   358  	tree.Remove(1)
   359  	assertValidTree(t, tree, 0)
   360  }
   361  
   362  func TestBTreeRemove2(t *testing.T) {
   363  	// leaf node (no underflow)
   364  	tree := NewWithIntComparator[int](3)
   365  	tree.Put(1, 0)
   366  	tree.Put(2, 0)
   367  
   368  	tree.Remove(1)
   369  	assertValidTree(t, tree, 1)
   370  	assertValidTreeNode(t, tree.Root, 1, 0, []int{2}, false)
   371  
   372  	tree.Remove(2)
   373  	assertValidTree(t, tree, 0)
   374  }
   375  
   376  func TestBTreeRemove3(t *testing.T) {
   377  	// merge with right (underflow)
   378  	{
   379  		tree := NewWithIntComparator[any](3)
   380  		tree.Put(1, nil)
   381  		tree.Put(2, nil)
   382  		tree.Put(3, nil)
   383  
   384  		tree.Remove(1)
   385  		assertValidTree(t, tree, 2)
   386  		assertValidTreeNode(t, tree.Root, 2, 0, []int{2, 3}, false)
   387  	}
   388  	// merge with left (underflow)
   389  	{
   390  		tree := NewWithIntComparator[any](3)
   391  		tree.Put(1, nil)
   392  		tree.Put(2, nil)
   393  		tree.Put(3, nil)
   394  
   395  		tree.Remove(3)
   396  		assertValidTree(t, tree, 2)
   397  		assertValidTreeNode(t, tree.Root, 2, 0, []int{1, 2}, false)
   398  	}
   399  }
   400  
   401  func TestBTreeRemove4(t *testing.T) {
   402  	// rotate left (underflow)
   403  	tree := NewWithIntComparator[any](3)
   404  	tree.Put(1, nil)
   405  	tree.Put(2, nil)
   406  	tree.Put(3, nil)
   407  	tree.Put(4, nil)
   408  
   409  	assertValidTree(t, tree, 4)
   410  	assertValidTreeNode(t, tree.Root, 1, 2, []int{2}, false)
   411  	assertValidTreeNode(t, tree.Root.Children[0], 1, 0, []int{1}, true)
   412  	assertValidTreeNode(t, tree.Root.Children[1], 2, 0, []int{3, 4}, true)
   413  
   414  	tree.Remove(1)
   415  	assertValidTree(t, tree, 3)
   416  	assertValidTreeNode(t, tree.Root, 1, 2, []int{3}, false)
   417  	assertValidTreeNode(t, tree.Root.Children[0], 1, 0, []int{2}, true)
   418  	assertValidTreeNode(t, tree.Root.Children[1], 1, 0, []int{4}, true)
   419  }
   420  
   421  func TestBTreeRemove5(t *testing.T) {
   422  	// rotate right (underflow)
   423  	tree := NewWithIntComparator[any](3)
   424  	tree.Put(1, nil)
   425  	tree.Put(2, nil)
   426  	tree.Put(3, nil)
   427  	tree.Put(0, nil)
   428  
   429  	assertValidTree(t, tree, 4)
   430  	assertValidTreeNode(t, tree.Root, 1, 2, []int{2}, false)
   431  	assertValidTreeNode(t, tree.Root.Children[0], 2, 0, []int{0, 1}, true)
   432  	assertValidTreeNode(t, tree.Root.Children[1], 1, 0, []int{3}, true)
   433  
   434  	tree.Remove(3)
   435  	assertValidTree(t, tree, 3)
   436  	assertValidTreeNode(t, tree.Root, 1, 2, []int{1}, false)
   437  	assertValidTreeNode(t, tree.Root.Children[0], 1, 0, []int{0}, true)
   438  	assertValidTreeNode(t, tree.Root.Children[1], 1, 0, []int{2}, true)
   439  }
   440  
   441  func TestBTreeRemove6(t *testing.T) {
   442  	// root height reduction after a series of underflows on right side
   443  	// use simulator: https://www.cs.usfca.edu/~galles/visualization/BTree.html
   444  	tree := NewWithIntComparator[any](3)
   445  	tree.Put(1, nil)
   446  	tree.Put(2, nil)
   447  	tree.Put(3, nil)
   448  	tree.Put(4, nil)
   449  	tree.Put(5, nil)
   450  	tree.Put(6, nil)
   451  	tree.Put(7, nil)
   452  
   453  	assertValidTree(t, tree, 7)
   454  	assertValidTreeNode(t, tree.Root, 1, 2, []int{4}, false)
   455  	assertValidTreeNode(t, tree.Root.Children[0], 1, 2, []int{2}, true)
   456  	assertValidTreeNode(t, tree.Root.Children[1], 1, 2, []int{6}, true)
   457  	assertValidTreeNode(t, tree.Root.Children[0].Children[0], 1, 0, []int{1}, true)
   458  	assertValidTreeNode(t, tree.Root.Children[0].Children[1], 1, 0, []int{3}, true)
   459  	assertValidTreeNode(t, tree.Root.Children[1].Children[0], 1, 0, []int{5}, true)
   460  	assertValidTreeNode(t, tree.Root.Children[1].Children[1], 1, 0, []int{7}, true)
   461  
   462  	tree.Remove(7)
   463  	assertValidTree(t, tree, 6)
   464  	assertValidTreeNode(t, tree.Root, 2, 3, []int{2, 4}, false)
   465  	assertValidTreeNode(t, tree.Root.Children[0], 1, 0, []int{1}, true)
   466  	assertValidTreeNode(t, tree.Root.Children[1], 1, 0, []int{3}, true)
   467  	assertValidTreeNode(t, tree.Root.Children[2], 2, 0, []int{5, 6}, true)
   468  }
   469  
   470  func TestBTreeRemove7(t *testing.T) {
   471  	// root height reduction after a series of underflows on left side
   472  	// use simulator: https://www.cs.usfca.edu/~galles/visualization/BTree.html
   473  	tree := NewWithIntComparator[any](3)
   474  	tree.Put(1, nil)
   475  	tree.Put(2, nil)
   476  	tree.Put(3, nil)
   477  	tree.Put(4, nil)
   478  	tree.Put(5, nil)
   479  	tree.Put(6, nil)
   480  	tree.Put(7, nil)
   481  
   482  	assertValidTree(t, tree, 7)
   483  	assertValidTreeNode(t, tree.Root, 1, 2, []int{4}, false)
   484  	assertValidTreeNode(t, tree.Root.Children[0], 1, 2, []int{2}, true)
   485  	assertValidTreeNode(t, tree.Root.Children[1], 1, 2, []int{6}, true)
   486  	assertValidTreeNode(t, tree.Root.Children[0].Children[0], 1, 0, []int{1}, true)
   487  	assertValidTreeNode(t, tree.Root.Children[0].Children[1], 1, 0, []int{3}, true)
   488  	assertValidTreeNode(t, tree.Root.Children[1].Children[0], 1, 0, []int{5}, true)
   489  	assertValidTreeNode(t, tree.Root.Children[1].Children[1], 1, 0, []int{7}, true)
   490  
   491  	tree.Remove(1) // series of underflows
   492  	assertValidTree(t, tree, 6)
   493  	assertValidTreeNode(t, tree.Root, 2, 3, []int{4, 6}, false)
   494  	assertValidTreeNode(t, tree.Root.Children[0], 2, 0, []int{2, 3}, true)
   495  	assertValidTreeNode(t, tree.Root.Children[1], 1, 0, []int{5}, true)
   496  	assertValidTreeNode(t, tree.Root.Children[2], 1, 0, []int{7}, true)
   497  
   498  	// clear all remaining
   499  	tree.Remove(2)
   500  	assertValidTree(t, tree, 5)
   501  	assertValidTreeNode(t, tree.Root, 2, 3, []int{4, 6}, false)
   502  	assertValidTreeNode(t, tree.Root.Children[0], 1, 0, []int{3}, true)
   503  	assertValidTreeNode(t, tree.Root.Children[1], 1, 0, []int{5}, true)
   504  	assertValidTreeNode(t, tree.Root.Children[2], 1, 0, []int{7}, true)
   505  
   506  	tree.Remove(3)
   507  	assertValidTree(t, tree, 4)
   508  	assertValidTreeNode(t, tree.Root, 1, 2, []int{6}, false)
   509  	assertValidTreeNode(t, tree.Root.Children[0], 2, 0, []int{4, 5}, true)
   510  	assertValidTreeNode(t, tree.Root.Children[1], 1, 0, []int{7}, true)
   511  
   512  	tree.Remove(4)
   513  	assertValidTree(t, tree, 3)
   514  	assertValidTreeNode(t, tree.Root, 1, 2, []int{6}, false)
   515  	assertValidTreeNode(t, tree.Root.Children[0], 1, 0, []int{5}, true)
   516  	assertValidTreeNode(t, tree.Root.Children[1], 1, 0, []int{7}, true)
   517  
   518  	tree.Remove(5)
   519  	assertValidTree(t, tree, 2)
   520  	assertValidTreeNode(t, tree.Root, 2, 0, []int{6, 7}, false)
   521  
   522  	tree.Remove(6)
   523  	assertValidTree(t, tree, 1)
   524  	assertValidTreeNode(t, tree.Root, 1, 0, []int{7}, false)
   525  
   526  	tree.Remove(7)
   527  	assertValidTree(t, tree, 0)
   528  }
   529  
   530  func TestBTreeRemove8(t *testing.T) {
   531  	// use simulator: https://www.cs.usfca.edu/~galles/visualization/BTree.html
   532  	tree := NewWithIntComparator[any](3)
   533  	tree.Put(1, nil)
   534  	tree.Put(2, nil)
   535  	tree.Put(3, nil)
   536  	tree.Put(4, nil)
   537  	tree.Put(5, nil)
   538  	tree.Put(6, nil)
   539  	tree.Put(7, nil)
   540  	tree.Put(8, nil)
   541  	tree.Put(9, nil)
   542  
   543  	assertValidTree(t, tree, 9)
   544  	assertValidTreeNode(t, tree.Root, 1, 2, []int{4}, false)
   545  	assertValidTreeNode(t, tree.Root.Children[0], 1, 2, []int{2}, true)
   546  	assertValidTreeNode(t, tree.Root.Children[1], 2, 3, []int{6, 8}, true)
   547  	assertValidTreeNode(t, tree.Root.Children[0].Children[0], 1, 0, []int{1}, true)
   548  	assertValidTreeNode(t, tree.Root.Children[0].Children[1], 1, 0, []int{3}, true)
   549  	assertValidTreeNode(t, tree.Root.Children[1].Children[0], 1, 0, []int{5}, true)
   550  	assertValidTreeNode(t, tree.Root.Children[1].Children[1], 1, 0, []int{7}, true)
   551  	assertValidTreeNode(t, tree.Root.Children[1].Children[2], 1, 0, []int{9}, true)
   552  
   553  	tree.Remove(1)
   554  	assertValidTree(t, tree, 8)
   555  	assertValidTreeNode(t, tree.Root, 1, 2, []int{6}, false)
   556  	assertValidTreeNode(t, tree.Root.Children[0], 1, 2, []int{4}, true)
   557  	assertValidTreeNode(t, tree.Root.Children[1], 1, 2, []int{8}, true)
   558  	assertValidTreeNode(t, tree.Root.Children[0].Children[0], 2, 0, []int{2, 3}, true)
   559  	assertValidTreeNode(t, tree.Root.Children[0].Children[1], 1, 0, []int{5}, true)
   560  	assertValidTreeNode(t, tree.Root.Children[1].Children[0], 1, 0, []int{7}, true)
   561  	assertValidTreeNode(t, tree.Root.Children[1].Children[1], 1, 0, []int{9}, true)
   562  }
   563  
   564  func TestBTreeRemove9(t *testing.T) {
   565  	const max = 1000
   566  	orders := []int{3, 4, 5, 6, 7, 8, 9, 10, 20, 100, 500, 1000, 5000, 10000}
   567  	for _, order := range orders {
   568  
   569  		tree := NewWithIntComparator[int](order)
   570  
   571  		{
   572  			for i := 1; i <= max; i++ {
   573  				tree.Put(i, i)
   574  			}
   575  			assertValidTree(t, tree, max)
   576  
   577  			for i := 1; i <= max; i++ {
   578  				if _, found := tree.Get(i); !found {
   579  					t.Errorf("Not found %v", i)
   580  				}
   581  			}
   582  
   583  			for i := 1; i <= max; i++ {
   584  				tree.Remove(i)
   585  			}
   586  			assertValidTree(t, tree, 0)
   587  		}
   588  
   589  		{
   590  			for i := max; i > 0; i-- {
   591  				tree.Put(i, i)
   592  			}
   593  			assertValidTree(t, tree, max)
   594  
   595  			for i := max; i > 0; i-- {
   596  				if _, found := tree.Get(i); !found {
   597  					t.Errorf("Not found %v", i)
   598  				}
   599  			}
   600  
   601  			for i := max; i > 0; i-- {
   602  				tree.Remove(i)
   603  			}
   604  			assertValidTree(t, tree, 0)
   605  		}
   606  	}
   607  }
   608  
   609  func TestBTreeHeight(t *testing.T) {
   610  	tree := NewWithIntComparator[int](3)
   611  	if actualValue, expectedValue := tree.Height(), 0; actualValue != expectedValue {
   612  		t.Errorf("Got %v expected %v", actualValue, expectedValue)
   613  	}
   614  
   615  	tree.Put(1, 0)
   616  	if actualValue, expectedValue := tree.Height(), 1; actualValue != expectedValue {
   617  		t.Errorf("Got %v expected %v", actualValue, expectedValue)
   618  	}
   619  
   620  	tree.Put(2, 1)
   621  	if actualValue, expectedValue := tree.Height(), 1; actualValue != expectedValue {
   622  		t.Errorf("Got %v expected %v", actualValue, expectedValue)
   623  	}
   624  
   625  	tree.Put(3, 2)
   626  	if actualValue, expectedValue := tree.Height(), 2; actualValue != expectedValue {
   627  		t.Errorf("Got %v expected %v", actualValue, expectedValue)
   628  	}
   629  
   630  	tree.Put(4, 2)
   631  	if actualValue, expectedValue := tree.Height(), 2; actualValue != expectedValue {
   632  		t.Errorf("Got %v expected %v", actualValue, expectedValue)
   633  	}
   634  
   635  	tree.Put(5, 2)
   636  	if actualValue, expectedValue := tree.Height(), 2; actualValue != expectedValue {
   637  		t.Errorf("Got %v expected %v", actualValue, expectedValue)
   638  	}
   639  
   640  	tree.Put(6, 2)
   641  	if actualValue, expectedValue := tree.Height(), 2; actualValue != expectedValue {
   642  		t.Errorf("Got %v expected %v", actualValue, expectedValue)
   643  	}
   644  
   645  	tree.Put(7, 2)
   646  	if actualValue, expectedValue := tree.Height(), 3; actualValue != expectedValue {
   647  		t.Errorf("Got %v expected %v", actualValue, expectedValue)
   648  	}
   649  
   650  	tree.Remove(1)
   651  	tree.Remove(2)
   652  	tree.Remove(3)
   653  	tree.Remove(4)
   654  	tree.Remove(5)
   655  	tree.Remove(6)
   656  	tree.Remove(7)
   657  	if actualValue, expectedValue := tree.Height(), 0; actualValue != expectedValue {
   658  		t.Errorf("Got %v expected %v", actualValue, expectedValue)
   659  	}
   660  }
   661  
   662  func TestBTreeLeftAndRight(t *testing.T) {
   663  	tree := NewWithIntComparator[string](3)
   664  
   665  	if actualValue := tree.Left(); actualValue != nil {
   666  		t.Errorf("Got %v expected %v", actualValue, nil)
   667  	}
   668  	if actualValue := tree.Right(); actualValue != nil {
   669  		t.Errorf("Got %v expected %v", actualValue, nil)
   670  	}
   671  
   672  	tree.Put(1, "a")
   673  	tree.Put(5, "e")
   674  	tree.Put(6, "f")
   675  	tree.Put(7, "g")
   676  	tree.Put(3, "c")
   677  	tree.Put(4, "d")
   678  	tree.Put(1, "x") // overwrite
   679  	tree.Put(2, "b")
   680  
   681  	if actualValue, expectedValue := tree.LeftKey(), 1; actualValue != expectedValue {
   682  		t.Errorf("Got %v expected %v", actualValue, expectedValue)
   683  	}
   684  	if actualValue, expectedValue := tree.LeftValue(), "x"; actualValue != expectedValue {
   685  		t.Errorf("Got %v expected %v", actualValue, expectedValue)
   686  	}
   687  
   688  	if actualValue, expectedValue := tree.RightKey(), 7; actualValue != expectedValue {
   689  		t.Errorf("Got %v expected %v", actualValue, expectedValue)
   690  	}
   691  	if actualValue, expectedValue := tree.RightValue(), "g"; actualValue != expectedValue {
   692  		t.Errorf("Got %v expected %v", actualValue, expectedValue)
   693  	}
   694  }
   695  
   696  func TestBTreeIteratorValuesAndKeys(t *testing.T) {
   697  	tree := NewWithIntComparator[string](4)
   698  	tree.Put(4, "d")
   699  	tree.Put(5, "e")
   700  	tree.Put(6, "f")
   701  	tree.Put(3, "c")
   702  	tree.Put(1, "a")
   703  	tree.Put(7, "g")
   704  	tree.Put(2, "b")
   705  	tree.Put(1, "x") // override
   706  	if actualValue, expectedValue := tree.Keys(), []int{1, 2, 3, 4, 5, 6, 7}; !reflect.DeepEqual(actualValue, expectedValue) {
   707  		t.Errorf("Got %v expected %v", actualValue, expectedValue)
   708  	}
   709  	if actualValue, expectedValue := tree.Values(), []string{"x", "b", "c", "d", "e", "f", "g"}; !reflect.DeepEqual(actualValue, expectedValue) {
   710  		t.Errorf("Got %v expected %v", actualValue, expectedValue)
   711  	}
   712  	if actualValue := tree.Size(); actualValue != 7 {
   713  		t.Errorf("Got %v expected %v", actualValue, 7)
   714  	}
   715  }
   716  
   717  func TestBTreeIteratorNextOnEmpty(t *testing.T) {
   718  	tree := NewWithIntComparator[int](3)
   719  	it := tree.Iterator()
   720  	for it.Next() {
   721  		t.Errorf("Shouldn't iterate on empty tree")
   722  	}
   723  }
   724  
   725  func TestBTreeIteratorPrevOnEmpty(t *testing.T) {
   726  	tree := NewWithIntComparator[int](3)
   727  	it := tree.Iterator()
   728  	for it.Prev() {
   729  		t.Errorf("Shouldn't iterate on empty tree")
   730  	}
   731  }
   732  
   733  func TestBTreeIterator1Next(t *testing.T) {
   734  	tree := NewWithIntComparator[string](3)
   735  	tree.Put(5, "e")
   736  	tree.Put(6, "f")
   737  	tree.Put(7, "g")
   738  	tree.Put(3, "c")
   739  	tree.Put(4, "d")
   740  	tree.Put(1, "x")
   741  	tree.Put(2, "b")
   742  	tree.Put(1, "a") //overwrite
   743  	it := tree.Iterator()
   744  	count := 0
   745  	for it.Next() {
   746  		count++
   747  		key := it.Key()
   748  		if actualValue, expectedValue := key, count; actualValue != expectedValue {
   749  			t.Errorf("Got %v expected %v", actualValue, expectedValue)
   750  		}
   751  	}
   752  	if actualValue, expectedValue := count, tree.Size(); actualValue != expectedValue {
   753  		t.Errorf("Size different. Got %v expected %v", actualValue, expectedValue)
   754  	}
   755  }
   756  
   757  func TestBTreeIterator1Prev(t *testing.T) {
   758  	tree := NewWithIntComparator[string](3)
   759  	tree.Put(5, "e")
   760  	tree.Put(6, "f")
   761  	tree.Put(7, "g")
   762  	tree.Put(3, "c")
   763  	tree.Put(4, "d")
   764  	tree.Put(1, "x")
   765  	tree.Put(2, "b")
   766  	tree.Put(1, "a") //overwrite
   767  	it := tree.Iterator()
   768  	for it.Next() {
   769  	}
   770  	countDown := tree.size
   771  	for it.Prev() {
   772  		key := it.Key()
   773  		if actualValue, expectedValue := key, countDown; actualValue != expectedValue {
   774  			t.Errorf("Got %v expected %v", actualValue, expectedValue)
   775  		}
   776  		countDown--
   777  	}
   778  	if actualValue, expectedValue := countDown, 0; actualValue != expectedValue {
   779  		t.Errorf("Size different. Got %v expected %v", actualValue, expectedValue)
   780  	}
   781  }
   782  
   783  func TestBTreeIterator2Next(t *testing.T) {
   784  	tree := NewWithIntComparator[string](3)
   785  	tree.Put(3, "c")
   786  	tree.Put(1, "a")
   787  	tree.Put(2, "b")
   788  	it := tree.Iterator()
   789  	count := 0
   790  	for it.Next() {
   791  		count++
   792  		key := it.Key()
   793  		if actualValue, expectedValue := key, count; actualValue != expectedValue {
   794  			t.Errorf("Got %v expected %v", actualValue, expectedValue)
   795  		}
   796  	}
   797  	if actualValue, expectedValue := count, tree.Size(); actualValue != expectedValue {
   798  		t.Errorf("Size different. Got %v expected %v", actualValue, expectedValue)
   799  	}
   800  }
   801  
   802  func TestBTreeIterator2Prev(t *testing.T) {
   803  	tree := NewWithIntComparator[string](3)
   804  	tree.Put(3, "c")
   805  	tree.Put(1, "a")
   806  	tree.Put(2, "b")
   807  	it := tree.Iterator()
   808  	for it.Next() {
   809  	}
   810  	countDown := tree.size
   811  	for it.Prev() {
   812  		key := it.Key()
   813  		if actualValue, expectedValue := key, countDown; actualValue != expectedValue {
   814  			t.Errorf("Got %v expected %v", actualValue, expectedValue)
   815  		}
   816  		countDown--
   817  	}
   818  	if actualValue, expectedValue := countDown, 0; actualValue != expectedValue {
   819  		t.Errorf("Size different. Got %v expected %v", actualValue, expectedValue)
   820  	}
   821  }
   822  
   823  func TestBTreeIterator3Next(t *testing.T) {
   824  	tree := NewWithIntComparator[string](3)
   825  	tree.Put(1, "a")
   826  	it := tree.Iterator()
   827  	count := 0
   828  	for it.Next() {
   829  		count++
   830  		key := it.Key()
   831  		if actualValue, expectedValue := key, count; actualValue != expectedValue {
   832  			t.Errorf("Got %v expected %v", actualValue, expectedValue)
   833  		}
   834  	}
   835  	if actualValue, expectedValue := count, tree.Size(); actualValue != expectedValue {
   836  		t.Errorf("Size different. Got %v expected %v", actualValue, expectedValue)
   837  	}
   838  }
   839  
   840  func TestBTreeIterator3Prev(t *testing.T) {
   841  	tree := NewWithIntComparator[string](3)
   842  	tree.Put(1, "a")
   843  	it := tree.Iterator()
   844  	for it.Next() {
   845  	}
   846  	countDown := tree.size
   847  	for it.Prev() {
   848  		key := it.Key()
   849  		if actualValue, expectedValue := key, countDown; actualValue != expectedValue {
   850  			t.Errorf("Got %v expected %v", actualValue, expectedValue)
   851  		}
   852  		countDown--
   853  	}
   854  	if actualValue, expectedValue := countDown, 0; actualValue != expectedValue {
   855  		t.Errorf("Size different. Got %v expected %v", actualValue, expectedValue)
   856  	}
   857  }
   858  
   859  func TestBTreeIterator4Next(t *testing.T) {
   860  	tree := NewWithIntComparator[int](3)
   861  	tree.Put(13, 5)
   862  	tree.Put(8, 3)
   863  	tree.Put(17, 7)
   864  	tree.Put(1, 1)
   865  	tree.Put(11, 4)
   866  	tree.Put(15, 6)
   867  	tree.Put(25, 9)
   868  	tree.Put(6, 2)
   869  	tree.Put(22, 8)
   870  	tree.Put(27, 10)
   871  	it := tree.Iterator()
   872  	count := 0
   873  	for it.Next() {
   874  		count++
   875  		value := it.Value()
   876  		if actualValue, expectedValue := value, count; actualValue != expectedValue {
   877  			t.Errorf("Got %v expected %v", actualValue, expectedValue)
   878  		}
   879  	}
   880  	if actualValue, expectedValue := count, tree.Size(); actualValue != expectedValue {
   881  		t.Errorf("Size different. Got %v expected %v", actualValue, expectedValue)
   882  	}
   883  }
   884  
   885  func TestBTreeIterator4Prev(t *testing.T) {
   886  	tree := NewWithIntComparator[int](3)
   887  	tree.Put(13, 5)
   888  	tree.Put(8, 3)
   889  	tree.Put(17, 7)
   890  	tree.Put(1, 1)
   891  	tree.Put(11, 4)
   892  	tree.Put(15, 6)
   893  	tree.Put(25, 9)
   894  	tree.Put(6, 2)
   895  	tree.Put(22, 8)
   896  	tree.Put(27, 10)
   897  	it := tree.Iterator()
   898  	count := tree.Size()
   899  	for it.Next() {
   900  	}
   901  	for it.Prev() {
   902  		value := it.Value()
   903  		if actualValue, expectedValue := value, count; actualValue != expectedValue {
   904  			t.Errorf("Got %v expected %v", actualValue, expectedValue)
   905  		}
   906  		count--
   907  	}
   908  	if actualValue, expectedValue := count, 0; actualValue != expectedValue {
   909  		t.Errorf("Size different. Got %v expected %v", actualValue, expectedValue)
   910  	}
   911  }
   912  
   913  func TestBTreeIteratorBegin(t *testing.T) {
   914  	tree := NewWithIntComparator[string](3)
   915  	tree.Put(3, "c")
   916  	tree.Put(1, "a")
   917  	tree.Put(2, "b")
   918  	it := tree.Iterator()
   919  
   920  	if it.node != nil {
   921  		t.Errorf("Got %v expected %v", it.node, nil)
   922  	}
   923  
   924  	it.Begin()
   925  
   926  	if it.node != nil {
   927  		t.Errorf("Got %v expected %v", it.node, nil)
   928  	}
   929  
   930  	for it.Next() {
   931  	}
   932  
   933  	it.Begin()
   934  
   935  	if it.node != nil {
   936  		t.Errorf("Got %v expected %v", it.node, nil)
   937  	}
   938  
   939  	it.Next()
   940  	if key, value := it.Key(), it.Value(); key != 1 || value != "a" {
   941  		t.Errorf("Got %v,%v expected %v,%v", key, value, 1, "a")
   942  	}
   943  }
   944  
   945  func TestBTreeIteratorEnd(t *testing.T) {
   946  	tree := NewWithIntComparator[string](3)
   947  	it := tree.Iterator()
   948  
   949  	if it.node != nil {
   950  		t.Errorf("Got %v expected %v", it.node, nil)
   951  	}
   952  
   953  	it.End()
   954  	if it.node != nil {
   955  		t.Errorf("Got %v expected %v", it.node, nil)
   956  	}
   957  
   958  	tree.Put(3, "c")
   959  	tree.Put(1, "a")
   960  	tree.Put(2, "b")
   961  	it.End()
   962  	if it.node != nil {
   963  		t.Errorf("Got %v expected %v", it.node, nil)
   964  	}
   965  
   966  	it.Prev()
   967  	if key, value := it.Key(), it.Value(); key != 3 || value != "c" {
   968  		t.Errorf("Got %v,%v expected %v,%v", key, value, 3, "c")
   969  	}
   970  }
   971  
   972  func TestBTreeIteratorFirst(t *testing.T) {
   973  	tree := NewWithIntComparator[string](3)
   974  	tree.Put(3, "c")
   975  	tree.Put(1, "a")
   976  	tree.Put(2, "b")
   977  	it := tree.Iterator()
   978  	if actualValue, expectedValue := it.First(), true; actualValue != expectedValue {
   979  		t.Errorf("Got %v expected %v", actualValue, expectedValue)
   980  	}
   981  	if key, value := it.Key(), it.Value(); key != 1 || value != "a" {
   982  		t.Errorf("Got %v,%v expected %v,%v", key, value, 1, "a")
   983  	}
   984  }
   985  
   986  func TestBTreeIteratorLast(t *testing.T) {
   987  	tree := NewWithIntComparator[string](3)
   988  	tree.Put(3, "c")
   989  	tree.Put(1, "a")
   990  	tree.Put(2, "b")
   991  	it := tree.Iterator()
   992  	if actualValue, expectedValue := it.Last(), true; actualValue != expectedValue {
   993  		t.Errorf("Got %v expected %v", actualValue, expectedValue)
   994  	}
   995  	if key, value := it.Key(), it.Value(); key != 3 || value != "c" {
   996  		t.Errorf("Got %v,%v expected %v,%v", key, value, 3, "c")
   997  	}
   998  }
   999  
  1000  func TestBTreeSearch(t *testing.T) {
  1001  	{
  1002  		tree := NewWithIntComparator[int](3)
  1003  		tree.Root = &Node[int, int]{Entries: []*Entry[int, int]{}, Children: make([]*Node[int, int], 0)}
  1004  		tests := [][]interface{}{
  1005  			{0, 0, false},
  1006  		}
  1007  		for _, test := range tests {
  1008  			index, found := tree.search(tree.Root, test[0].(int))
  1009  			if actualValue, expectedValue := index, test[1]; actualValue != expectedValue {
  1010  				t.Errorf("Got %v expected %v", actualValue, expectedValue)
  1011  			}
  1012  			if actualValue, expectedValue := found, test[2]; actualValue != expectedValue {
  1013  				t.Errorf("Got %v expected %v", actualValue, expectedValue)
  1014  			}
  1015  		}
  1016  	}
  1017  	{
  1018  		tree := NewWithIntComparator[int](3)
  1019  		tree.Root = &Node[int, int]{Entries: []*Entry[int, int]{{2, 0}, {4, 1}, {6, 2}}, Children: []*Node[int, int]{}}
  1020  		tests := [][]interface{}{
  1021  			{0, 0, false},
  1022  			{1, 0, false},
  1023  			{2, 0, true},
  1024  			{3, 1, false},
  1025  			{4, 1, true},
  1026  			{5, 2, false},
  1027  			{6, 2, true},
  1028  			{7, 3, false},
  1029  		}
  1030  		for _, test := range tests {
  1031  			index, found := tree.search(tree.Root, test[0].(int))
  1032  			if actualValue, expectedValue := index, test[1]; actualValue != expectedValue {
  1033  				t.Errorf("Got %v expected %v", actualValue, expectedValue)
  1034  			}
  1035  			if actualValue, expectedValue := found, test[2]; actualValue != expectedValue {
  1036  				t.Errorf("Got %v expected %v", actualValue, expectedValue)
  1037  			}
  1038  		}
  1039  	}
  1040  }
  1041  
  1042  func assertValidTree[K, V any](t *testing.T, tree *Tree[K, V], expectedSize int) {
  1043  	if actualValue, expectedValue := tree.size, expectedSize; actualValue != expectedValue {
  1044  		t.Errorf("Got %v expected %v for tree size", actualValue, expectedValue)
  1045  	}
  1046  }
  1047  
  1048  func assertValidTreeNode[K, V any](t *testing.T, node *Node[K, V], expectedEntries int, expectedChildren int, keys []int, hasParent bool) {
  1049  	if actualValue, expectedValue := node.Parent != nil, hasParent; actualValue != expectedValue {
  1050  		t.Errorf("Got %v expected %v for hasParent", actualValue, expectedValue)
  1051  	}
  1052  	if actualValue, expectedValue := len(node.Entries), expectedEntries; actualValue != expectedValue {
  1053  		t.Errorf("Got %v expected %v for entries size", actualValue, expectedValue)
  1054  	}
  1055  	if actualValue, expectedValue := len(node.Children), expectedChildren; actualValue != expectedValue {
  1056  		t.Errorf("Got %v expected %v for children size", actualValue, expectedValue)
  1057  	}
  1058  	for i, key := range keys {
  1059  		if actualValue, expectedValue := node.Entries[i].Key, key; !reflect.DeepEqual(actualValue, expectedValue) {
  1060  			t.Errorf("Got %v expected %v for key", actualValue, expectedValue)
  1061  		}
  1062  	}
  1063  }
  1064  
  1065  func TestBTreeIteratorNextTo(t *testing.T) {
  1066  	// Sample seek function, i.e. string starting with "b"
  1067  	seek := func(index int, value string) bool {
  1068  		return strings.HasSuffix(value, "b")
  1069  	}
  1070  
  1071  	// NextTo (empty)
  1072  	{
  1073  		tree := NewWithIntComparator[string](3)
  1074  		it := tree.Iterator()
  1075  		for it.NextTo(seek) {
  1076  			t.Errorf("Shouldn't iterate on empty tree")
  1077  		}
  1078  	}
  1079  
  1080  	// NextTo (not found)
  1081  	{
  1082  		tree := NewWithIntComparator[string](3)
  1083  		tree.Put(0, "xx")
  1084  		tree.Put(1, "yy")
  1085  		it := tree.Iterator()
  1086  		for it.NextTo(seek) {
  1087  			t.Errorf("Shouldn't iterate on empty tree")
  1088  		}
  1089  	}
  1090  
  1091  	// NextTo (found)
  1092  	{
  1093  		tree := NewWithIntComparator[string](3)
  1094  		tree.Put(2, "cc")
  1095  		tree.Put(0, "aa")
  1096  		tree.Put(1, "bb")
  1097  		it := tree.Iterator()
  1098  		it.Begin()
  1099  		if !it.NextTo(seek) {
  1100  			t.Errorf("Shouldn't iterate on empty tree")
  1101  		}
  1102  		if index, value := it.Key(), it.Value(); index != 1 || value != "bb" {
  1103  			t.Errorf("Got %v,%v expected %v,%v", index, value, 1, "bb")
  1104  		}
  1105  		if !it.Next() {
  1106  			t.Errorf("Should go to first element")
  1107  		}
  1108  		if index, value := it.Key(), it.Value(); index != 2 || value != "cc" {
  1109  			t.Errorf("Got %v,%v expected %v,%v", index, value, 2, "cc")
  1110  		}
  1111  		if it.Next() {
  1112  			t.Errorf("Should not go past last element")
  1113  		}
  1114  	}
  1115  }
  1116  
  1117  func TestBTreeIteratorPrevTo(t *testing.T) {
  1118  	// Sample seek function, i.e. string starting with "b"
  1119  	seek := func(index int, value string) bool {
  1120  		return strings.HasSuffix(value, "b")
  1121  	}
  1122  
  1123  	// PrevTo (empty)
  1124  	{
  1125  		tree := NewWithIntComparator[string](3)
  1126  		it := tree.Iterator()
  1127  		it.End()
  1128  		for it.PrevTo(seek) {
  1129  			t.Errorf("Shouldn't iterate on empty tree")
  1130  		}
  1131  	}
  1132  
  1133  	// PrevTo (not found)
  1134  	{
  1135  		tree := NewWithIntComparator[string](3)
  1136  		tree.Put(0, "xx")
  1137  		tree.Put(1, "yy")
  1138  		it := tree.Iterator()
  1139  		it.End()
  1140  		for it.PrevTo(seek) {
  1141  			t.Errorf("Shouldn't iterate on empty tree")
  1142  		}
  1143  	}
  1144  
  1145  	// PrevTo (found)
  1146  	{
  1147  		tree := NewWithIntComparator[string](3)
  1148  		tree.Put(2, "cc")
  1149  		tree.Put(0, "aa")
  1150  		tree.Put(1, "bb")
  1151  		it := tree.Iterator()
  1152  		it.End()
  1153  		if !it.PrevTo(seek) {
  1154  			t.Errorf("Shouldn't iterate on empty tree")
  1155  		}
  1156  		if index, value := it.Key(), it.Value(); index != 1 || value != "bb" {
  1157  			t.Errorf("Got %v,%v expected %v,%v", index, value, 1, "bb")
  1158  		}
  1159  		if !it.Prev() {
  1160  			t.Errorf("Should go to first element")
  1161  		}
  1162  		if index, value := it.Key(), it.Value(); index != 0 || value != "aa" {
  1163  			t.Errorf("Got %v,%v expected %v,%v", index, value, 0, "aa")
  1164  		}
  1165  		if it.Prev() {
  1166  			t.Errorf("Should not go before first element")
  1167  		}
  1168  	}
  1169  }
  1170  
  1171  func TestBTreeSerialization(t *testing.T) {
  1172  	tree := NewWithStringComparator[string](3)
  1173  	tree.Put("c", "3")
  1174  	tree.Put("b", "2")
  1175  	tree.Put("a", "1")
  1176  
  1177  	var err error
  1178  	assert := func() {
  1179  		if actualValue, expectedValue := tree.Size(), 3; actualValue != expectedValue {
  1180  			t.Errorf("Got %v expected %v", actualValue, expectedValue)
  1181  		}
  1182  		if actualValue := tree.Keys(); actualValue[0] != "a" || actualValue[1] != "b" || actualValue[2] != "c" {
  1183  			t.Errorf("Got %v expected %v", actualValue, "[a,b,c]")
  1184  		}
  1185  		if actualValue := tree.Values(); actualValue[0] != "1" || actualValue[1] != "2" || actualValue[2] != "3" {
  1186  			t.Errorf("Got %v expected %v", actualValue, "[1,2,3]")
  1187  		}
  1188  		if err != nil {
  1189  			t.Errorf("Got error %v", err)
  1190  		}
  1191  	}
  1192  
  1193  	assert()
  1194  
  1195  	bytes, err := tree.MarshalJSON()
  1196  	assert()
  1197  
  1198  	err = tree.UnmarshalJSON(bytes)
  1199  	assert()
  1200  
  1201  	bytes, err = json.Marshal([]interface{}{"a", "b", "c", tree})
  1202  	if err != nil {
  1203  		t.Errorf("Got error %v", err)
  1204  	}
  1205  
  1206  	err = json.Unmarshal([]byte(`{"a":"1","b":"2"}`), &tree)
  1207  	if err != nil {
  1208  		t.Errorf("Got error %v", err)
  1209  	}
  1210  }
  1211  
  1212  func TestBTreeString(t *testing.T) {
  1213  	c := NewWithStringComparator[int](3)
  1214  	c.Put("a", 1)
  1215  	if !strings.HasPrefix(c.String(), "BTree") {
  1216  		t.Errorf("String should start with container name")
  1217  	}
  1218  }
  1219  
  1220  func benchmarkGet[K int, V struct{}](b *testing.B, tree *Tree[K, V], size int) {
  1221  	for i := 0; i < b.N; i++ {
  1222  		for n := 0; n < size; n++ {
  1223  			tree.Get(K(n))
  1224  		}
  1225  	}
  1226  }
  1227  
  1228  func benchmarkPut[K int, V struct{}](b *testing.B, tree *Tree[K, V], size int) {
  1229  	for i := 0; i < b.N; i++ {
  1230  		for n := 0; n < size; n++ {
  1231  			tree.Put(K(n), struct{}{})
  1232  		}
  1233  	}
  1234  }
  1235  
  1236  func benchmarkRemove[K int, V struct{}](b *testing.B, tree *Tree[K, V], size int) {
  1237  	for i := 0; i < b.N; i++ {
  1238  		for n := 0; n < size; n++ {
  1239  			tree.Remove(K(n))
  1240  		}
  1241  	}
  1242  }
  1243  
  1244  func BenchmarkBTreeGet100(b *testing.B) {
  1245  	b.StopTimer()
  1246  	size := 100
  1247  	tree := NewWithIntComparator[struct{}](128)
  1248  	for n := 0; n < size; n++ {
  1249  		tree.Put(n, struct{}{})
  1250  	}
  1251  	b.StartTimer()
  1252  	benchmarkGet(b, tree, size)
  1253  }
  1254  
  1255  func BenchmarkBTreeGet1000(b *testing.B) {
  1256  	b.StopTimer()
  1257  	size := 1000
  1258  	tree := NewWithIntComparator[struct{}](128)
  1259  	for n := 0; n < size; n++ {
  1260  		tree.Put(n, struct{}{})
  1261  	}
  1262  	b.StartTimer()
  1263  	benchmarkGet(b, tree, size)
  1264  }
  1265  
  1266  func BenchmarkBTreeGet10000(b *testing.B) {
  1267  	b.StopTimer()
  1268  	size := 10000
  1269  	tree := NewWithIntComparator[struct{}](128)
  1270  	for n := 0; n < size; n++ {
  1271  		tree.Put(n, struct{}{})
  1272  	}
  1273  	b.StartTimer()
  1274  	benchmarkGet(b, tree, size)
  1275  }
  1276  
  1277  func BenchmarkBTreeGet100000(b *testing.B) {
  1278  	b.StopTimer()
  1279  	size := 100000
  1280  	tree := NewWithIntComparator[struct{}](128)
  1281  	for n := 0; n < size; n++ {
  1282  		tree.Put(n, struct{}{})
  1283  	}
  1284  	b.StartTimer()
  1285  	benchmarkGet(b, tree, size)
  1286  }
  1287  
  1288  func BenchmarkBTreePut100(b *testing.B) {
  1289  	b.StopTimer()
  1290  	size := 100
  1291  	tree := NewWithIntComparator[struct{}](128)
  1292  	b.StartTimer()
  1293  	benchmarkPut(b, tree, size)
  1294  }
  1295  
  1296  func BenchmarkBTreePut1000(b *testing.B) {
  1297  	b.StopTimer()
  1298  	size := 1000
  1299  	tree := NewWithIntComparator[struct{}](128)
  1300  	for n := 0; n < size; n++ {
  1301  		tree.Put(n, struct{}{})
  1302  	}
  1303  	b.StartTimer()
  1304  	benchmarkPut(b, tree, size)
  1305  }
  1306  
  1307  func BenchmarkBTreePut10000(b *testing.B) {
  1308  	b.StopTimer()
  1309  	size := 10000
  1310  	tree := NewWithIntComparator[struct{}](128)
  1311  	for n := 0; n < size; n++ {
  1312  		tree.Put(n, struct{}{})
  1313  	}
  1314  	b.StartTimer()
  1315  	benchmarkPut(b, tree, size)
  1316  }
  1317  
  1318  func BenchmarkBTreePut100000(b *testing.B) {
  1319  	b.StopTimer()
  1320  	size := 100000
  1321  	tree := NewWithIntComparator[struct{}](128)
  1322  	for n := 0; n < size; n++ {
  1323  		tree.Put(n, struct{}{})
  1324  	}
  1325  	b.StartTimer()
  1326  	benchmarkPut(b, tree, size)
  1327  }
  1328  
  1329  func BenchmarkBTreeRemove100(b *testing.B) {
  1330  	b.StopTimer()
  1331  	size := 100
  1332  	tree := NewWithIntComparator[struct{}](128)
  1333  	for n := 0; n < size; n++ {
  1334  		tree.Put(n, struct{}{})
  1335  	}
  1336  	b.StartTimer()
  1337  	benchmarkRemove(b, tree, size)
  1338  }
  1339  
  1340  func BenchmarkBTreeRemove1000(b *testing.B) {
  1341  	b.StopTimer()
  1342  	size := 1000
  1343  	tree := NewWithIntComparator[struct{}](128)
  1344  	for n := 0; n < size; n++ {
  1345  		tree.Put(n, struct{}{})
  1346  	}
  1347  	b.StartTimer()
  1348  	benchmarkRemove(b, tree, size)
  1349  }
  1350  
  1351  func BenchmarkBTreeRemove10000(b *testing.B) {
  1352  	b.StopTimer()
  1353  	size := 10000
  1354  	tree := NewWithIntComparator[struct{}](128)
  1355  	for n := 0; n < size; n++ {
  1356  		tree.Put(n, struct{}{})
  1357  	}
  1358  	b.StartTimer()
  1359  	benchmarkRemove(b, tree, size)
  1360  }
  1361  
  1362  func BenchmarkBTreeRemove100000(b *testing.B) {
  1363  	b.StopTimer()
  1364  	size := 100000
  1365  	tree := NewWithIntComparator[struct{}](128)
  1366  	for n := 0; n < size; n++ {
  1367  		tree.Put(n, struct{}{})
  1368  	}
  1369  	b.StartTimer()
  1370  	benchmarkRemove(b, tree, size)
  1371  }