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

     1  package redblacktree
     2  
     3  import (
     4  	"encoding/json"
     5  	"fmt"
     6  	"github.com/songzhibin97/go-baseutils/base/bcomparator"
     7  	"reflect"
     8  
     9  	"strings"
    10  	"testing"
    11  )
    12  
    13  func TestRedBlackTreeGet(t *testing.T) {
    14  	tree := NewWithIntComparator[string]()
    15  
    16  	if actualValue := tree.Size(); actualValue != 0 {
    17  		t.Errorf("Got %v expected %v", actualValue, 0)
    18  	}
    19  
    20  	if actualValue := tree.GetNode(2).Size(); actualValue != 0 {
    21  		t.Errorf("Got %v expected %v", actualValue, 0)
    22  	}
    23  
    24  	tree.Put(1, "x") // 1->x
    25  	tree.Put(2, "b") // 1->x, 2->b (in order)
    26  	tree.Put(1, "a") // 1->a, 2->b (in order, replacement)
    27  	tree.Put(3, "c") // 1->a, 2->b, 3->c (in order)
    28  	tree.Put(4, "d") // 1->a, 2->b, 3->c, 4->d (in order)
    29  	tree.Put(5, "e") // 1->a, 2->b, 3->c, 4->d, 5->e (in order)
    30  	tree.Put(6, "f") // 1->a, 2->b, 3->c, 4->d, 5->e, 6->f (in order)
    31  
    32  	fmt.Println(tree)
    33  	//
    34  	//  RedBlackTree
    35  	//  │           ┌── 6
    36  	//  │       ┌── 5
    37  	//  │   ┌── 4
    38  	//  │   │   └── 3
    39  	//  └── 2
    40  	//       └── 1
    41  
    42  	if actualValue := tree.Size(); actualValue != 6 {
    43  		t.Errorf("Got %v expected %v", actualValue, 6)
    44  	}
    45  
    46  	if actualValue := tree.GetNode(4).Size(); actualValue != 4 {
    47  		t.Errorf("Got %v expected %v", actualValue, 4)
    48  	}
    49  
    50  	if actualValue := tree.GetNode(2).Size(); actualValue != 6 {
    51  		t.Errorf("Got %v expected %v", actualValue, 6)
    52  	}
    53  
    54  	if actualValue := tree.GetNode(8).Size(); actualValue != 0 {
    55  		t.Errorf("Got %v expected %v", actualValue, 0)
    56  	}
    57  }
    58  
    59  func TestRedBlackTreePut(t *testing.T) {
    60  	tree := NewWithIntComparator[string]()
    61  	tree.Put(5, "e")
    62  	tree.Put(6, "f")
    63  	tree.Put(7, "g")
    64  	tree.Put(3, "c")
    65  	tree.Put(4, "d")
    66  	tree.Put(1, "x")
    67  	tree.Put(2, "b")
    68  	tree.Put(1, "a") //overwrite
    69  
    70  	if actualValue := tree.Size(); actualValue != 7 {
    71  		t.Errorf("Got %v expected %v", actualValue, 7)
    72  	}
    73  	if actualValue, expectedValue := tree.Keys(), []int{1, 2, 3, 4, 5, 6, 7}; !reflect.DeepEqual(actualValue, expectedValue) {
    74  		t.Errorf("Got %v expected %v", actualValue, expectedValue)
    75  	}
    76  	if actualValue, expectedValue := tree.Values(), []string{"a", "b", "c", "d", "e", "f", "g"}; !reflect.DeepEqual(actualValue, expectedValue) {
    77  		t.Errorf("Got %v expected %v", actualValue, expectedValue)
    78  	}
    79  
    80  	tests1 := [][]interface{}{
    81  		{1, "a", true},
    82  		{2, "b", true},
    83  		{3, "c", true},
    84  		{4, "d", true},
    85  		{5, "e", true},
    86  		{6, "f", true},
    87  		{7, "g", true},
    88  		{8, "", false},
    89  	}
    90  
    91  	for _, test := range tests1 {
    92  		// retrievals
    93  		actualValue, actualFound := tree.Get(test[0].(int))
    94  		if actualValue != test[1] || actualFound != test[2] {
    95  			t.Errorf("Got %v expected %v", actualValue, test[1])
    96  		}
    97  	}
    98  }
    99  
   100  func TestRedBlackTreeRemove(t *testing.T) {
   101  	tree := NewWithIntComparator[string]()
   102  	tree.Put(5, "e")
   103  	tree.Put(6, "f")
   104  	tree.Put(7, "g")
   105  	tree.Put(3, "c")
   106  	tree.Put(4, "d")
   107  	tree.Put(1, "x")
   108  	tree.Put(2, "b")
   109  	tree.Put(1, "a") //overwrite
   110  
   111  	tree.Remove(5)
   112  	tree.Remove(6)
   113  	tree.Remove(7)
   114  	tree.Remove(8)
   115  	tree.Remove(5)
   116  
   117  	if actualValue, expectedValue := tree.Keys(), []int{1, 2, 3, 4}; !reflect.DeepEqual(actualValue, expectedValue) {
   118  		t.Errorf("Got %v expected %v", actualValue, expectedValue)
   119  	}
   120  	if actualValue, expectedValue := tree.Values(), []string{"a", "b", "c", "d"}; !reflect.DeepEqual(actualValue, expectedValue) {
   121  		t.Errorf("Got %v expected %v", actualValue, expectedValue)
   122  	}
   123  	if actualValue := tree.Size(); actualValue != 4 {
   124  		t.Errorf("Got %v expected %v", actualValue, 7)
   125  	}
   126  
   127  	tests2 := [][]interface{}{
   128  		{1, "a", true},
   129  		{2, "b", true},
   130  		{3, "c", true},
   131  		{4, "d", true},
   132  		{5, "", false},
   133  		{6, "", false},
   134  		{7, "", false},
   135  		{8, "", false},
   136  	}
   137  
   138  	for _, test := range tests2 {
   139  		actualValue, actualFound := tree.Get(test[0].(int))
   140  		if actualValue != test[1] || actualFound != test[2] {
   141  			t.Errorf("Got %v expected %v", actualValue, test[1])
   142  		}
   143  	}
   144  
   145  	tree.Remove(1)
   146  	tree.Remove(4)
   147  	tree.Remove(2)
   148  	tree.Remove(3)
   149  	tree.Remove(2)
   150  	tree.Remove(2)
   151  
   152  	if actualValue, expectedValue := tree.Keys(), []int{}; !reflect.DeepEqual(actualValue, expectedValue) {
   153  		t.Errorf("Got %v expected %v", actualValue, expectedValue)
   154  	}
   155  	if actualValue, expectedValue := tree.Values(), []string{}; !reflect.DeepEqual(actualValue, expectedValue) {
   156  		t.Errorf("Got %v expected %v", actualValue, expectedValue)
   157  	}
   158  	if empty, size := tree.Empty(), tree.Size(); empty != true || size != -0 {
   159  		t.Errorf("Got %v expected %v", empty, true)
   160  	}
   161  
   162  }
   163  
   164  func TestRedBlackTreeLeftAndRight(t *testing.T) {
   165  	tree := NewWithIntComparator[string]()
   166  
   167  	if actualValue := tree.Left(); actualValue != nil {
   168  		t.Errorf("Got %v expected %v", actualValue, nil)
   169  	}
   170  	if actualValue := tree.Right(); actualValue != nil {
   171  		t.Errorf("Got %v expected %v", actualValue, nil)
   172  	}
   173  
   174  	tree.Put(1, "a")
   175  	tree.Put(5, "e")
   176  	tree.Put(6, "f")
   177  	tree.Put(7, "g")
   178  	tree.Put(3, "c")
   179  	tree.Put(4, "d")
   180  	tree.Put(1, "x") // overwrite
   181  	tree.Put(2, "b")
   182  
   183  	if actualValue, expectedValue := fmt.Sprintf("%d", tree.Left().Key), "1"; actualValue != expectedValue {
   184  		t.Errorf("Got %v expected %v", actualValue, expectedValue)
   185  	}
   186  	if actualValue, expectedValue := fmt.Sprintf("%s", tree.Left().Value), "x"; actualValue != expectedValue {
   187  		t.Errorf("Got %v expected %v", actualValue, expectedValue)
   188  	}
   189  
   190  	if actualValue, expectedValue := fmt.Sprintf("%d", tree.Right().Key), "7"; actualValue != expectedValue {
   191  		t.Errorf("Got %v expected %v", actualValue, expectedValue)
   192  	}
   193  	if actualValue, expectedValue := fmt.Sprintf("%s", tree.Right().Value), "g"; actualValue != expectedValue {
   194  		t.Errorf("Got %v expected %v", actualValue, expectedValue)
   195  	}
   196  }
   197  
   198  func TestRedBlackTreeCeilingAndFloor(t *testing.T) {
   199  	tree := NewWith[int, string](bcomparator.IntComparator())
   200  
   201  	if node, found := tree.Floor(0); node != nil || found {
   202  		t.Errorf("Got %v expected %v", node, "<nil>")
   203  	}
   204  	if node, found := tree.Ceiling(0); node != nil || found {
   205  		t.Errorf("Got %v expected %v", node, "<nil>")
   206  	}
   207  
   208  	tree.Put(5, "e")
   209  	tree.Put(6, "f")
   210  	tree.Put(7, "g")
   211  	tree.Put(3, "c")
   212  	tree.Put(4, "d")
   213  	tree.Put(1, "x")
   214  	tree.Put(2, "b")
   215  
   216  	if node, found := tree.Floor(4); node.Key != 4 || !found {
   217  		t.Errorf("Got %v expected %v", node.Key, 4)
   218  	}
   219  	if node, found := tree.Floor(0); node != nil || found {
   220  		t.Errorf("Got %v expected %v", node, "<nil>")
   221  	}
   222  
   223  	if node, found := tree.Ceiling(4); node.Key != 4 || !found {
   224  		t.Errorf("Got %v expected %v", node.Key, 4)
   225  	}
   226  	if node, found := tree.Ceiling(8); node != nil || found {
   227  		t.Errorf("Got %v expected %v", node, "<nil>")
   228  	}
   229  }
   230  
   231  func TestRedBlackTreeIteratorNextOnEmpty(t *testing.T) {
   232  	tree := NewWithIntComparator[int]()
   233  	it := tree.Iterator()
   234  	for it.Next() {
   235  		t.Errorf("Shouldn't iterate on empty tree")
   236  	}
   237  }
   238  
   239  func TestRedBlackTreeIteratorPrevOnEmpty(t *testing.T) {
   240  	tree := NewWithIntComparator[int]()
   241  	it := tree.Iterator()
   242  	for it.Prev() {
   243  		t.Errorf("Shouldn't iterate on empty tree")
   244  	}
   245  }
   246  
   247  func TestRedBlackTreeIterator1Next(t *testing.T) {
   248  	tree := NewWithIntComparator[string]()
   249  	tree.Put(5, "e")
   250  	tree.Put(6, "f")
   251  	tree.Put(7, "g")
   252  	tree.Put(3, "c")
   253  	tree.Put(4, "d")
   254  	tree.Put(1, "x")
   255  	tree.Put(2, "b")
   256  	tree.Put(1, "a") //overwrite
   257  	// │   ┌── 7
   258  	// └── 6
   259  	//     │   ┌── 5
   260  	//     └── 4
   261  	//         │   ┌── 3
   262  	//         └── 2
   263  	//             └── 1
   264  	it := tree.Iterator()
   265  	count := 0
   266  	for it.Next() {
   267  		count++
   268  		key := it.Key()
   269  		if actualValue, expectedValue := key, count; actualValue != expectedValue {
   270  			t.Errorf("Got %v expected %v", actualValue, expectedValue)
   271  		}
   272  	}
   273  	if actualValue, expectedValue := count, tree.Size(); actualValue != expectedValue {
   274  		t.Errorf("Size different. Got %v expected %v", actualValue, expectedValue)
   275  	}
   276  }
   277  
   278  func TestRedBlackTreeIterator1Prev(t *testing.T) {
   279  	tree := NewWithIntComparator[string]()
   280  	tree.Put(5, "e")
   281  	tree.Put(6, "f")
   282  	tree.Put(7, "g")
   283  	tree.Put(3, "c")
   284  	tree.Put(4, "d")
   285  	tree.Put(1, "x")
   286  	tree.Put(2, "b")
   287  	tree.Put(1, "a") //overwrite
   288  	// │   ┌── 7
   289  	// └── 6
   290  	//     │   ┌── 5
   291  	//     └── 4
   292  	//         │   ┌── 3
   293  	//         └── 2
   294  	//             └── 1
   295  	it := tree.Iterator()
   296  	for it.Next() {
   297  	}
   298  	countDown := tree.size
   299  	for it.Prev() {
   300  		key := it.Key()
   301  		if actualValue, expectedValue := key, countDown; actualValue != expectedValue {
   302  			t.Errorf("Got %v expected %v", actualValue, expectedValue)
   303  		}
   304  		countDown--
   305  	}
   306  	if actualValue, expectedValue := countDown, 0; actualValue != expectedValue {
   307  		t.Errorf("Size different. Got %v expected %v", actualValue, expectedValue)
   308  	}
   309  }
   310  
   311  func TestRedBlackTreeIterator2Next(t *testing.T) {
   312  	tree := NewWithIntComparator[string]()
   313  	tree.Put(3, "c")
   314  	tree.Put(1, "a")
   315  	tree.Put(2, "b")
   316  	it := tree.Iterator()
   317  	count := 0
   318  	for it.Next() {
   319  		count++
   320  		key := it.Key()
   321  		if actualValue, expectedValue := key, count; actualValue != expectedValue {
   322  			t.Errorf("Got %v expected %v", actualValue, expectedValue)
   323  		}
   324  	}
   325  	if actualValue, expectedValue := count, tree.Size(); actualValue != expectedValue {
   326  		t.Errorf("Size different. Got %v expected %v", actualValue, expectedValue)
   327  	}
   328  }
   329  
   330  func TestRedBlackTreeIterator2Prev(t *testing.T) {
   331  	tree := NewWithIntComparator[string]()
   332  	tree.Put(3, "c")
   333  	tree.Put(1, "a")
   334  	tree.Put(2, "b")
   335  	it := tree.Iterator()
   336  	for it.Next() {
   337  	}
   338  	countDown := tree.size
   339  	for it.Prev() {
   340  		key := it.Key()
   341  		if actualValue, expectedValue := key, countDown; actualValue != expectedValue {
   342  			t.Errorf("Got %v expected %v", actualValue, expectedValue)
   343  		}
   344  		countDown--
   345  	}
   346  	if actualValue, expectedValue := countDown, 0; actualValue != expectedValue {
   347  		t.Errorf("Size different. Got %v expected %v", actualValue, expectedValue)
   348  	}
   349  }
   350  
   351  func TestRedBlackTreeIterator3Next(t *testing.T) {
   352  	tree := NewWithIntComparator[string]()
   353  	tree.Put(1, "a")
   354  	it := tree.Iterator()
   355  	count := 0
   356  	for it.Next() {
   357  		count++
   358  		key := it.Key()
   359  		if actualValue, expectedValue := key, count; actualValue != expectedValue {
   360  			t.Errorf("Got %v expected %v", actualValue, expectedValue)
   361  		}
   362  	}
   363  	if actualValue, expectedValue := count, tree.Size(); actualValue != expectedValue {
   364  		t.Errorf("Size different. Got %v expected %v", actualValue, expectedValue)
   365  	}
   366  }
   367  
   368  func TestRedBlackTreeIterator3Prev(t *testing.T) {
   369  	tree := NewWithIntComparator[string]()
   370  	tree.Put(1, "a")
   371  	it := tree.Iterator()
   372  	for it.Next() {
   373  	}
   374  	countDown := tree.size
   375  	for it.Prev() {
   376  		key := it.Key()
   377  		if actualValue, expectedValue := key, countDown; actualValue != expectedValue {
   378  			t.Errorf("Got %v expected %v", actualValue, expectedValue)
   379  		}
   380  		countDown--
   381  	}
   382  	if actualValue, expectedValue := countDown, 0; actualValue != expectedValue {
   383  		t.Errorf("Size different. Got %v expected %v", actualValue, expectedValue)
   384  	}
   385  }
   386  
   387  func TestRedBlackTreeIterator4Next(t *testing.T) {
   388  	tree := NewWithIntComparator[int]()
   389  	tree.Put(13, 5)
   390  	tree.Put(8, 3)
   391  	tree.Put(17, 7)
   392  	tree.Put(1, 1)
   393  	tree.Put(11, 4)
   394  	tree.Put(15, 6)
   395  	tree.Put(25, 9)
   396  	tree.Put(6, 2)
   397  	tree.Put(22, 8)
   398  	tree.Put(27, 10)
   399  	// │           ┌── 27
   400  	// │       ┌── 25
   401  	// │       │   └── 22
   402  	// │   ┌── 17
   403  	// │   │   └── 15
   404  	// └── 13
   405  	//     │   ┌── 11
   406  	//     └── 8
   407  	//         │   ┌── 6
   408  	//         └── 1
   409  	it := tree.Iterator()
   410  	count := 0
   411  	for it.Next() {
   412  		count++
   413  		value := it.Value()
   414  		if actualValue, expectedValue := value, count; actualValue != expectedValue {
   415  			t.Errorf("Got %v expected %v", actualValue, expectedValue)
   416  		}
   417  	}
   418  	if actualValue, expectedValue := count, tree.Size(); actualValue != expectedValue {
   419  		t.Errorf("Size different. Got %v expected %v", actualValue, expectedValue)
   420  	}
   421  }
   422  
   423  func TestRedBlackTreeIterator4Prev(t *testing.T) {
   424  	tree := NewWithIntComparator[int]()
   425  	tree.Put(13, 5)
   426  	tree.Put(8, 3)
   427  	tree.Put(17, 7)
   428  	tree.Put(1, 1)
   429  	tree.Put(11, 4)
   430  	tree.Put(15, 6)
   431  	tree.Put(25, 9)
   432  	tree.Put(6, 2)
   433  	tree.Put(22, 8)
   434  	tree.Put(27, 10)
   435  	// │           ┌── 27
   436  	// │       ┌── 25
   437  	// │       │   └── 22
   438  	// │   ┌── 17
   439  	// │   │   └── 15
   440  	// └── 13
   441  	//     │   ┌── 11
   442  	//     └── 8
   443  	//         │   ┌── 6
   444  	//         └── 1
   445  	it := tree.Iterator()
   446  	count := tree.Size()
   447  	for it.Next() {
   448  	}
   449  	for it.Prev() {
   450  		value := it.Value()
   451  		if actualValue, expectedValue := value, count; actualValue != expectedValue {
   452  			t.Errorf("Got %v expected %v", actualValue, expectedValue)
   453  		}
   454  		count--
   455  	}
   456  	if actualValue, expectedValue := count, 0; actualValue != expectedValue {
   457  		t.Errorf("Size different. Got %v expected %v", actualValue, expectedValue)
   458  	}
   459  }
   460  
   461  func TestRedBlackTreeIteratorBegin(t *testing.T) {
   462  	tree := NewWithIntComparator[string]()
   463  	tree.Put(3, "c")
   464  	tree.Put(1, "a")
   465  	tree.Put(2, "b")
   466  	it := tree.Iterator()
   467  
   468  	if it.node != nil {
   469  		t.Errorf("Got %v expected %v", it.node, nil)
   470  	}
   471  
   472  	it.Begin()
   473  
   474  	if it.node != nil {
   475  		t.Errorf("Got %v expected %v", it.node, nil)
   476  	}
   477  
   478  	for it.Next() {
   479  	}
   480  
   481  	it.Begin()
   482  
   483  	if it.node != nil {
   484  		t.Errorf("Got %v expected %v", it.node, nil)
   485  	}
   486  
   487  	it.Next()
   488  	if key, value := it.Key(), it.Value(); key != 1 || value != "a" {
   489  		t.Errorf("Got %v,%v expected %v,%v", key, value, 1, "a")
   490  	}
   491  }
   492  
   493  func TestRedBlackTreeIteratorEnd(t *testing.T) {
   494  	tree := NewWithIntComparator[string]()
   495  	it := tree.Iterator()
   496  
   497  	if it.node != nil {
   498  		t.Errorf("Got %v expected %v", it.node, nil)
   499  	}
   500  
   501  	it.End()
   502  	if it.node != nil {
   503  		t.Errorf("Got %v expected %v", it.node, nil)
   504  	}
   505  
   506  	tree.Put(3, "c")
   507  	tree.Put(1, "a")
   508  	tree.Put(2, "b")
   509  	it.End()
   510  	if it.node != nil {
   511  		t.Errorf("Got %v expected %v", it.node, nil)
   512  	}
   513  
   514  	it.Prev()
   515  	if key, value := it.Key(), it.Value(); key != 3 || value != "c" {
   516  		t.Errorf("Got %v,%v expected %v,%v", key, value, 3, "c")
   517  	}
   518  }
   519  
   520  func TestRedBlackTreeIteratorFirst(t *testing.T) {
   521  	tree := NewWithIntComparator[string]()
   522  	tree.Put(3, "c")
   523  	tree.Put(1, "a")
   524  	tree.Put(2, "b")
   525  	it := tree.Iterator()
   526  	if actualValue, expectedValue := it.First(), true; actualValue != expectedValue {
   527  		t.Errorf("Got %v expected %v", actualValue, expectedValue)
   528  	}
   529  	if key, value := it.Key(), it.Value(); key != 1 || value != "a" {
   530  		t.Errorf("Got %v,%v expected %v,%v", key, value, 1, "a")
   531  	}
   532  }
   533  
   534  func TestRedBlackTreeIteratorLast(t *testing.T) {
   535  	tree := NewWithIntComparator[string]()
   536  	tree.Put(3, "c")
   537  	tree.Put(1, "a")
   538  	tree.Put(2, "b")
   539  	it := tree.Iterator()
   540  	if actualValue, expectedValue := it.Last(), true; actualValue != expectedValue {
   541  		t.Errorf("Got %v expected %v", actualValue, expectedValue)
   542  	}
   543  	if key, value := it.Key(), it.Value(); key != 3 || value != "c" {
   544  		t.Errorf("Got %v,%v expected %v,%v", key, value, 3, "c")
   545  	}
   546  }
   547  
   548  func TestRedBlackTreeIteratorNextTo(t *testing.T) {
   549  	// Sample seek function, i.e. string starting with "b"
   550  	seek := func(index int, value string) bool {
   551  		return strings.HasSuffix(value, "b")
   552  	}
   553  
   554  	// NextTo (empty)
   555  	{
   556  		tree := NewWithIntComparator[string]()
   557  		it := tree.Iterator()
   558  		for it.NextTo(seek) {
   559  			t.Errorf("Shouldn't iterate on empty tree")
   560  		}
   561  	}
   562  
   563  	// NextTo (not found)
   564  	{
   565  		tree := NewWithIntComparator[string]()
   566  		tree.Put(0, "xx")
   567  		tree.Put(1, "yy")
   568  		it := tree.Iterator()
   569  		for it.NextTo(seek) {
   570  			t.Errorf("Shouldn't iterate on empty tree")
   571  		}
   572  	}
   573  
   574  	// NextTo (found)
   575  	{
   576  		tree := NewWithIntComparator[string]()
   577  		tree.Put(2, "cc")
   578  		tree.Put(0, "aa")
   579  		tree.Put(1, "bb")
   580  		it := tree.Iterator()
   581  		it.Begin()
   582  		if !it.NextTo(seek) {
   583  			t.Errorf("Shouldn't iterate on empty tree")
   584  		}
   585  		if index, value := it.Key(), it.Value(); index != 1 || value != "bb" {
   586  			t.Errorf("Got %v,%v expected %v,%v", index, value, 1, "bb")
   587  		}
   588  		if !it.Next() {
   589  			t.Errorf("Should go to first element")
   590  		}
   591  		if index, value := it.Key(), it.Value(); index != 2 || value != "cc" {
   592  			t.Errorf("Got %v,%v expected %v,%v", index, value, 2, "cc")
   593  		}
   594  		if it.Next() {
   595  			t.Errorf("Should not go past last element")
   596  		}
   597  	}
   598  }
   599  
   600  func TestRedBlackTreeIteratorPrevTo(t *testing.T) {
   601  	// Sample seek function, i.e. string starting with "b"
   602  	seek := func(index int, value string) bool {
   603  		return strings.HasSuffix(value, "b")
   604  	}
   605  
   606  	// PrevTo (empty)
   607  	{
   608  		tree := NewWithIntComparator[string]()
   609  		it := tree.Iterator()
   610  		it.End()
   611  		for it.PrevTo(seek) {
   612  			t.Errorf("Shouldn't iterate on empty tree")
   613  		}
   614  	}
   615  
   616  	// PrevTo (not found)
   617  	{
   618  		tree := NewWithIntComparator[string]()
   619  		tree.Put(0, "xx")
   620  		tree.Put(1, "yy")
   621  		it := tree.Iterator()
   622  		it.End()
   623  		for it.PrevTo(seek) {
   624  			t.Errorf("Shouldn't iterate on empty tree")
   625  		}
   626  	}
   627  
   628  	// PrevTo (found)
   629  	{
   630  		tree := NewWithIntComparator[string]()
   631  		tree.Put(2, "cc")
   632  		tree.Put(0, "aa")
   633  		tree.Put(1, "bb")
   634  		it := tree.Iterator()
   635  		it.End()
   636  		if !it.PrevTo(seek) {
   637  			t.Errorf("Shouldn't iterate on empty tree")
   638  		}
   639  		if index, value := it.Key(), it.Value(); index != 1 || value != "bb" {
   640  			t.Errorf("Got %v,%v expected %v,%v", index, value, 1, "bb")
   641  		}
   642  		if !it.Prev() {
   643  			t.Errorf("Should go to first element")
   644  		}
   645  		if index, value := it.Key(), it.Value(); index != 0 || value != "aa" {
   646  			t.Errorf("Got %v,%v expected %v,%v", index, value, 0, "aa")
   647  		}
   648  		if it.Prev() {
   649  			t.Errorf("Should not go before first element")
   650  		}
   651  	}
   652  }
   653  
   654  func TestRedBlackTreeSerialization(t *testing.T) {
   655  	tree := NewWithStringComparator[string]()
   656  	tree.Put("c", "3")
   657  	tree.Put("b", "2")
   658  	tree.Put("a", "1")
   659  
   660  	var err error
   661  	assert := func() {
   662  		if actualValue, expectedValue := tree.Size(), 3; actualValue != expectedValue {
   663  			t.Errorf("Got %v expected %v", actualValue, expectedValue)
   664  		}
   665  		if actualValue := tree.Keys(); actualValue[0] != "a" || actualValue[1] != "b" || actualValue[2] != "c" {
   666  			t.Errorf("Got %v expected %v", actualValue, "[a,b,c]")
   667  		}
   668  		if actualValue := tree.Values(); actualValue[0] != "1" || actualValue[1] != "2" || actualValue[2] != "3" {
   669  			t.Errorf("Got %v expected %v", actualValue, "[1,2,3]")
   670  		}
   671  		if err != nil {
   672  			t.Errorf("Got error %v", err)
   673  		}
   674  	}
   675  
   676  	assert()
   677  
   678  	bytes, err := tree.MarshalJSON()
   679  	assert()
   680  
   681  	err = tree.UnmarshalJSON(bytes)
   682  	assert()
   683  
   684  	bytes, err = json.Marshal([]interface{}{"a", "b", "c", tree})
   685  	if err != nil {
   686  		t.Errorf("Got error %v", err)
   687  	}
   688  
   689  	err = json.Unmarshal([]byte(`{"a":"1","b":"2"}`), &tree)
   690  	if err != nil {
   691  		t.Errorf("Got error %v", err)
   692  	}
   693  }
   694  
   695  func TestRedBlackTreeString(t *testing.T) {
   696  	c := NewWithStringComparator[int]()
   697  	c.Put("a", 1)
   698  	if !strings.HasPrefix(c.String(), "RedBlackTree") {
   699  		t.Errorf("String should start with container name")
   700  	}
   701  }
   702  
   703  func benchmarkGet[K int, V struct{}](b *testing.B, tree *Tree[K, V], size int) {
   704  	for i := 0; i < b.N; i++ {
   705  		for n := 0; n < size; n++ {
   706  			tree.Get(K(n))
   707  		}
   708  	}
   709  }
   710  
   711  func benchmarkPut[K int, V struct{}](b *testing.B, tree *Tree[K, V], size int) {
   712  	for i := 0; i < b.N; i++ {
   713  		for n := 0; n < size; n++ {
   714  			tree.Put(K(n), struct{}{})
   715  		}
   716  	}
   717  }
   718  
   719  func benchmarkRemove[K int, V struct{}](b *testing.B, tree *Tree[K, V], size int) {
   720  	for i := 0; i < b.N; i++ {
   721  		for n := 0; n < size; n++ {
   722  			tree.Remove(K(n))
   723  		}
   724  	}
   725  }
   726  
   727  func BenchmarkRedBlackTreeGet100(b *testing.B) {
   728  	b.StopTimer()
   729  	size := 100
   730  	tree := NewWithIntComparator[struct{}]()
   731  	for n := 0; n < size; n++ {
   732  		tree.Put(n, struct{}{})
   733  	}
   734  	b.StartTimer()
   735  	benchmarkGet(b, tree, size)
   736  }
   737  
   738  func BenchmarkRedBlackTreeGet1000(b *testing.B) {
   739  	b.StopTimer()
   740  	size := 1000
   741  	tree := NewWithIntComparator[struct{}]()
   742  	for n := 0; n < size; n++ {
   743  		tree.Put(n, struct{}{})
   744  	}
   745  	b.StartTimer()
   746  	benchmarkGet(b, tree, size)
   747  }
   748  
   749  func BenchmarkRedBlackTreeGet10000(b *testing.B) {
   750  	b.StopTimer()
   751  	size := 10000
   752  	tree := NewWithIntComparator[struct{}]()
   753  	for n := 0; n < size; n++ {
   754  		tree.Put(n, struct{}{})
   755  	}
   756  	b.StartTimer()
   757  	benchmarkGet(b, tree, size)
   758  }
   759  
   760  func BenchmarkRedBlackTreeGet100000(b *testing.B) {
   761  	b.StopTimer()
   762  	size := 100000
   763  	tree := NewWithIntComparator[struct{}]()
   764  	for n := 0; n < size; n++ {
   765  		tree.Put(n, struct{}{})
   766  	}
   767  	b.StartTimer()
   768  	benchmarkGet(b, tree, size)
   769  }
   770  
   771  func BenchmarkRedBlackTreePut100(b *testing.B) {
   772  	b.StopTimer()
   773  	size := 100
   774  	tree := NewWithIntComparator[struct{}]()
   775  	b.StartTimer()
   776  	benchmarkPut(b, tree, size)
   777  }
   778  
   779  func BenchmarkRedBlackTreePut1000(b *testing.B) {
   780  	b.StopTimer()
   781  	size := 1000
   782  	tree := NewWithIntComparator[struct{}]()
   783  	for n := 0; n < size; n++ {
   784  		tree.Put(n, struct{}{})
   785  	}
   786  	b.StartTimer()
   787  	benchmarkPut(b, tree, size)
   788  }
   789  
   790  func BenchmarkRedBlackTreePut10000(b *testing.B) {
   791  	b.StopTimer()
   792  	size := 10000
   793  	tree := NewWithIntComparator[struct{}]()
   794  	for n := 0; n < size; n++ {
   795  		tree.Put(n, struct{}{})
   796  	}
   797  	b.StartTimer()
   798  	benchmarkPut(b, tree, size)
   799  }
   800  
   801  func BenchmarkRedBlackTreePut100000(b *testing.B) {
   802  	b.StopTimer()
   803  	size := 100000
   804  	tree := NewWithIntComparator[struct{}]()
   805  	for n := 0; n < size; n++ {
   806  		tree.Put(n, struct{}{})
   807  	}
   808  	b.StartTimer()
   809  	benchmarkPut(b, tree, size)
   810  }
   811  
   812  func BenchmarkRedBlackTreeRemove100(b *testing.B) {
   813  	b.StopTimer()
   814  	size := 100
   815  	tree := NewWithIntComparator[struct{}]()
   816  	for n := 0; n < size; n++ {
   817  		tree.Put(n, struct{}{})
   818  	}
   819  	b.StartTimer()
   820  	benchmarkRemove(b, tree, size)
   821  }
   822  
   823  func BenchmarkRedBlackTreeRemove1000(b *testing.B) {
   824  	b.StopTimer()
   825  	size := 1000
   826  	tree := NewWithIntComparator[struct{}]()
   827  	for n := 0; n < size; n++ {
   828  		tree.Put(n, struct{}{})
   829  	}
   830  	b.StartTimer()
   831  	benchmarkRemove(b, tree, size)
   832  }
   833  
   834  func BenchmarkRedBlackTreeRemove10000(b *testing.B) {
   835  	b.StopTimer()
   836  	size := 10000
   837  	tree := NewWithIntComparator[struct{}]()
   838  	for n := 0; n < size; n++ {
   839  		tree.Put(n, struct{}{})
   840  	}
   841  	b.StartTimer()
   842  	benchmarkRemove(b, tree, size)
   843  }
   844  
   845  func BenchmarkRedBlackTreeRemove100000(b *testing.B) {
   846  	b.StopTimer()
   847  	size := 100000
   848  	tree := NewWithIntComparator[struct{}]()
   849  	for n := 0; n < size; n++ {
   850  		tree.Put(n, struct{}{})
   851  	}
   852  	b.StartTimer()
   853  	benchmarkRemove(b, tree, size)
   854  }