github.com/chain5j/chain5j-pkg@v1.0.7/collection/maps/treemap/v2/treemap_test.go (about)

     1  package treemap
     2  
     3  import (
     4  	"testing"
     5  )
     6  
     7  func less(x, y int) bool { return x < y }
     8  
     9  func TestNew(t *testing.T) {
    10  	testNew(t, New[int, string]())
    11  	testNew(t, NewWithKeyCompare[int, string](less))
    12  }
    13  
    14  func TestSet(t *testing.T) {
    15  	testSet(t, New[int, string]())
    16  	testSet(t, NewWithKeyCompare[int, string](less))
    17  }
    18  
    19  func TestDel(t *testing.T) {
    20  	testDel(t, New[int, string]())
    21  	testDel(t, NewWithKeyCompare[int, string](less))
    22  }
    23  
    24  func TestGet(t *testing.T) {
    25  	testGet(t, New[int, string]())
    26  	testGet(t, NewWithKeyCompare[int, string](less))
    27  }
    28  
    29  func TestContains(t *testing.T) {
    30  	testContains(t, New[int, string]())
    31  	testContains(t, NewWithKeyCompare[int, string](less))
    32  }
    33  
    34  func TestLen(t *testing.T) {
    35  	testLen(t, New[int, string]())
    36  	testLen(t, NewWithKeyCompare[int, string](less))
    37  }
    38  
    39  func TestClear(t *testing.T) {
    40  	testClear(t, New[int, string]())
    41  	testClear(t, NewWithKeyCompare[int, string](less))
    42  }
    43  
    44  func TestRange(t *testing.T) {
    45  	testRange(t, New[int, string]())
    46  	testRange(t, NewWithKeyCompare[int, string](less))
    47  }
    48  
    49  func TestLowerBound(t *testing.T) {
    50  	testLowerBound(t, New[int, string]())
    51  	testLowerBound(t, NewWithKeyCompare[int, string](less))
    52  }
    53  
    54  func TestUpperBound(t *testing.T) {
    55  	testUpperBound(t, New[int, string]())
    56  	testUpperBound(t, NewWithKeyCompare[int, string](less))
    57  }
    58  
    59  func TestEmptyRange(t *testing.T) {
    60  	testEmptyRange(t, New[int, string]())
    61  	testEmptyRange(t, NewWithKeyCompare[int, string](less))
    62  }
    63  
    64  func TestDelNil(t *testing.T) {
    65  	testDelNil(t, New[int, string]())
    66  	testDelNil(t, NewWithKeyCompare[int, string](less))
    67  }
    68  
    69  func TestIteration(t *testing.T) {
    70  	testIteration(t, New[int, string]())
    71  	testIteration(t, NewWithKeyCompare[int, string](less))
    72  }
    73  
    74  func TestOutOfBoundsForwardIterationNext(t *testing.T) {
    75  	testOutOfBoundsForwardIterationNext(t, New[int, string]())
    76  	testOutOfBoundsForwardIterationNext(t, NewWithKeyCompare[int, string](less))
    77  }
    78  
    79  func TestOutOfBoundsForwardIterationPrev(t *testing.T) {
    80  	testOutOfBoundsForwardIterationPrev(t, New[int, string]())
    81  	testOutOfBoundsForwardIterationPrev(t, NewWithKeyCompare[int, string](less))
    82  }
    83  
    84  func TestOutOfBoundsReverseIterationNext(t *testing.T) {
    85  	testOutOfBoundsReverseIterationNext(t, New[int, string]())
    86  	testOutOfBoundsReverseIterationNext(t, NewWithKeyCompare[int, string](less))
    87  }
    88  
    89  func TestOutOfBoundsReverseIterationPrev(t *testing.T) {
    90  	testOutOfBoundsReverseIterationPrev(t, New[int, string]())
    91  	testOutOfBoundsReverseIterationPrev(t, NewWithKeyCompare[int, string](less))
    92  }
    93  
    94  func TestRangeSingle(t *testing.T) {
    95  	testRangeSingle(t, New[int, string]())
    96  	testRangeSingle(t, NewWithKeyCompare[int, string](less))
    97  }
    98  
    99  func testNew(t *testing.T, tr *TreeMap[int, string]) {
   100  	if tr.Len() != 0 {
   101  		t.Error("count should be zero")
   102  	}
   103  	if tr.endNode.left != nil {
   104  		t.Error("root should be zero")
   105  	}
   106  }
   107  
   108  func testSet(t *testing.T, tr *TreeMap[int, string]) {
   109  	x := "x"
   110  	tr.Set(0, x)
   111  	if tr.endNode.left.key != 0 {
   112  		t.Errorf("wrong key, expected 0, got %d", tr.endNode.left.key)
   113  	}
   114  	if v := tr.endNode.left.value; v != x {
   115  		t.Errorf("wrong returned value, expected '%s', got '%s'", x, v)
   116  	}
   117  	if tr.Len() != 1 {
   118  		t.Errorf("wrong count, expected 1, got %d", tr.Len())
   119  	}
   120  }
   121  
   122  func testDel(t *testing.T, tr *TreeMap[int, string]) {
   123  	tr.Set(0, "x")
   124  	tr.Del(0)
   125  	if tr.Len() != 0 {
   126  		t.Errorf("wrong count after deletion, expected 0, got %d", tr.Len())
   127  	}
   128  	if tr.endNode.left != nil {
   129  		t.Error("wrong tree state after deletion")
   130  	}
   131  }
   132  
   133  func testGet(t *testing.T, tr *TreeMap[int, string]) {
   134  	x := "x"
   135  	tr.Set(0, x)
   136  	v, ok := tr.Get(0)
   137  	if v != x || !ok {
   138  		t.Errorf("wrong returned value, expected 'x', got '%s'", v)
   139  	}
   140  	if tr.Len() != 1 {
   141  		t.Errorf("wrong count, expected 1, got %d", tr.Len())
   142  	}
   143  	if v, ok := tr.Get(2); v != "" || ok {
   144  		t.Errorf("wrong returned value, expected nil, got '%v'", v)
   145  	}
   146  	if tr.Len() != 1 {
   147  		t.Errorf("wrong count, expected 1, got %d", tr.Len())
   148  	}
   149  }
   150  
   151  func testContains(t *testing.T, tr *TreeMap[int, string]) {
   152  	tr.Set(0, "x")
   153  	val := tr.Contains(0)
   154  	if !val {
   155  		t.Error("existing is not exist")
   156  	}
   157  	val = tr.Contains(1)
   158  	if val {
   159  		t.Error("not existing is exist")
   160  	}
   161  }
   162  
   163  func testLen(t *testing.T, tr *TreeMap[int, string]) {
   164  	if tr.Len() != 0 {
   165  		t.Errorf("wrong count, expected 0, got %d", tr.Len())
   166  	}
   167  	tr.Set(0, "x")
   168  	if tr.Len() != 1 {
   169  		t.Errorf("wrong count, expected 1, got %d", tr.Len())
   170  	}
   171  	tr.Set(1, "x")
   172  	if tr.Len() != 2 {
   173  		t.Errorf("wrong count, expected 2, got %d", tr.Len())
   174  	}
   175  	tr.Del(1)
   176  	if tr.Len() != 1 {
   177  		t.Errorf("wrong count, expected 1, got %d", tr.Len())
   178  	}
   179  	tr.Del(0)
   180  	if tr.Len() != 0 {
   181  		t.Errorf("wrong count, expected 0, got %d", tr.Len())
   182  	}
   183  }
   184  
   185  func testClear(t *testing.T, tr *TreeMap[int, string]) {
   186  	tr.Set(0, "x")
   187  	tr.Set(1, "y")
   188  	tr.Set(2, "z")
   189  	tr.Clear()
   190  	if tr.Len() != 0 {
   191  		t.Error("count is not zero")
   192  	}
   193  	if tr.endNode.left != nil {
   194  		t.Error("root is not nil")
   195  	}
   196  }
   197  
   198  func testRange(t *testing.T, tr *TreeMap[int, string]) {
   199  	tr.Set(0, "x")
   200  	tr.Set(1, "y")
   201  	tr.Set(2, "z")
   202  	tr.Set(3, "m")
   203  	tr.Set(4, "n")
   204  	it, end := tr.Range(1, 3)
   205  	testRangeEqual(t, it, end, []string{"y", "z", "m"})
   206  	it, end = tr.Range(1, 9)
   207  	testRangeEqual(t, it, end, []string{"y", "z", "m", "n"})
   208  }
   209  
   210  func testRangeEqual(t *testing.T, it, end ForwardIterator[int, string], exp []string) {
   211  	var actual []string
   212  	for ; it != end; it.Next() {
   213  		actual = append(actual, it.Value())
   214  	}
   215  	if len(actual) != len(exp) {
   216  		t.Errorf("wrong range length, expected %d, got %d", len(exp), len(actual))
   217  	}
   218  	for i, v := range exp {
   219  		if actual[i] != v {
   220  			t.Errorf("wrong value, expected '%s', got '%s'", exp[i], actual[i])
   221  		}
   222  	}
   223  }
   224  
   225  func testLowerBound(t *testing.T, tr *TreeMap[int, string]) {
   226  	it := tr.LowerBound(0)
   227  	if it.Valid() {
   228  		t.Error("lower bound should not exists")
   229  		return
   230  	}
   231  	tr.Set(2, "a")
   232  	tr.Set(4, "b")
   233  	tr.Set(6, "c")
   234  	tr.Set(8, "d")
   235  	tr.Set(10, "e")
   236  	tr.Set(12, "e")
   237  	tr.Set(14, "e")
   238  	tr.Set(16, "e")
   239  	tr.Set(18, "e")
   240  	tr.Set(20, "e")
   241  
   242  	tbl := [][2]int{
   243  		{0, 2},
   244  		{2, 2},
   245  		{3, 4},
   246  		{4, 4},
   247  		{9, 10},
   248  		{10, 10},
   249  		{11, 12},
   250  		{19, 20},
   251  		{20, 20},
   252  	}
   253  
   254  	for _, tb := range tbl {
   255  		it = tr.LowerBound(tb[0])
   256  		if !it.Valid() {
   257  			t.Error("lower bound should exists")
   258  			return
   259  		}
   260  		if k := it.Key(); k != tb[1] {
   261  			t.Errorf("lower bound should be %v", tb[1])
   262  			return
   263  		}
   264  	}
   265  
   266  	it = tr.LowerBound(21)
   267  	if it.Valid() {
   268  		t.Error("lower bound should not exists")
   269  		return
   270  	}
   271  }
   272  
   273  func testUpperBound(t *testing.T, tr *TreeMap[int, string]) {
   274  	it := tr.UpperBound(0)
   275  	if it.Valid() {
   276  		t.Error("upper bound should not exists")
   277  		return
   278  	}
   279  	tr.Set(2, "a")
   280  	tr.Set(4, "b")
   281  	tr.Set(6, "c")
   282  	tr.Set(8, "d")
   283  	tr.Set(10, "e")
   284  	tr.Set(12, "e")
   285  	tr.Set(14, "e")
   286  	tr.Set(16, "e")
   287  	tr.Set(18, "e")
   288  	tr.Set(20, "e")
   289  
   290  	tbl := [][2]int{
   291  		{0, 2},
   292  		{2, 4},
   293  		{3, 4},
   294  		{4, 6},
   295  		{9, 10},
   296  		{10, 12},
   297  		{11, 12},
   298  		{19, 20},
   299  	}
   300  
   301  	for _, tb := range tbl {
   302  		it = tr.UpperBound(tb[0])
   303  		if !it.Valid() {
   304  			t.Error("lower bound should exists")
   305  			return
   306  		}
   307  		if k := it.Key(); k != tb[1] {
   308  			t.Errorf("upper bound should be %v", tb[1])
   309  			return
   310  		}
   311  	}
   312  
   313  	it = tr.UpperBound(20)
   314  	if it.Valid() {
   315  		t.Error("upper bound should not exists")
   316  		return
   317  	}
   318  	it = tr.UpperBound(21)
   319  	if it.Valid() {
   320  		t.Error("upper bound should not exists")
   321  		return
   322  	}
   323  }
   324  
   325  func testEmptyRange(t *testing.T, tr *TreeMap[int, string]) {
   326  	tr.Set(0, "x")
   327  	tr.Set(1, "y")
   328  	tr.Set(2, "z")
   329  	tr.Set(3, "m")
   330  	tr.Set(4, "n")
   331  	if rng, end := tr.Range(5, 10); rng != end {
   332  		t.Error("range should be empty")
   333  	}
   334  }
   335  
   336  func testDelNil(t *testing.T, tr *TreeMap[int, string]) {
   337  	x := "x"
   338  	tr.Set(0, x)
   339  	tr.Del(1)
   340  	if tr.Len() != 1 {
   341  		t.Errorf("wrong count after del, expected 1, got %d", tr.Len())
   342  	}
   343  }
   344  
   345  func testIteration(t *testing.T, tr *TreeMap[int, string]) {
   346  	kvs := []struct {
   347  		key   int
   348  		value string
   349  	}{
   350  		{0, "a"},
   351  		{1, "b"},
   352  		{2, "c"},
   353  		{3, "d"},
   354  		{4, "e"},
   355  	}
   356  	for _, kv := range kvs {
   357  		tr.Set(kv.key, kv.value)
   358  	}
   359  	assert := func(expKey int, expValue string, gotKey int, gotValue string) {
   360  		if expKey != gotKey || expValue != gotValue {
   361  			t.Errorf("expected %v, %s, got %v, %s", expKey, expValue, gotKey, gotValue)
   362  		}
   363  	}
   364  	count := 0
   365  	fwd := tr.Iterator()
   366  	for ; fwd.Valid(); fwd.Next() {
   367  		assert(kvs[count].key, kvs[count].value, fwd.Key(), fwd.Value())
   368  		count++
   369  	}
   370  	for fwd != tr.Iterator() {
   371  		fwd.Prev()
   372  		count--
   373  		assert(kvs[count].key, kvs[count].value, fwd.Key(), fwd.Value())
   374  	}
   375  	count = len(kvs)
   376  	rev := tr.Reverse()
   377  	for ; rev.Valid(); rev.Next() {
   378  		count--
   379  		assert(kvs[count].key, kvs[count].value, rev.Key(), rev.Value())
   380  	}
   381  	rbegin := tr.Reverse()
   382  	for rev != rbegin {
   383  		rev.Prev()
   384  		assert(kvs[count].key, kvs[count].value, rev.Key(), rev.Value())
   385  		count++
   386  	}
   387  }
   388  
   389  func testOutOfBoundsForwardIterationNext(t *testing.T, tr *TreeMap[int, string]) {
   390  	tr.Set(0, "a")
   391  	tr.Set(1, "b")
   392  	tr.Set(2, "c")
   393  	tr.Set(3, "d")
   394  	tr.Set(4, "e")
   395  	it := tr.Iterator()
   396  	for ; it.Valid(); it.Next() {
   397  	}
   398  	defer func() {
   399  		if r := recover(); r == nil {
   400  			t.Error("should have panicked!")
   401  		}
   402  	}()
   403  	it.Next()
   404  }
   405  
   406  func testOutOfBoundsForwardIterationPrev(t *testing.T, tr *TreeMap[int, string]) {
   407  	tr.Set(0, "a")
   408  	tr.Set(1, "b")
   409  	tr.Set(2, "c")
   410  	tr.Set(3, "d")
   411  	tr.Set(4, "e")
   412  	it := tr.Iterator()
   413  	defer func() {
   414  		if r := recover(); r == nil {
   415  			t.Error("should have panicked!")
   416  		}
   417  	}()
   418  	it.Prev()
   419  }
   420  
   421  func testOutOfBoundsReverseIterationNext(t *testing.T, tr *TreeMap[int, string]) {
   422  	tr.Set(0, "a")
   423  	tr.Set(1, "b")
   424  	tr.Set(2, "c")
   425  	tr.Set(3, "d")
   426  	tr.Set(4, "e")
   427  	it := tr.Reverse()
   428  	for ; it.Valid(); it.Next() {
   429  	}
   430  	defer func() {
   431  		if r := recover(); r == nil {
   432  			t.Error("should have panicked!")
   433  		}
   434  	}()
   435  	it.Next()
   436  }
   437  
   438  func testOutOfBoundsReverseIterationPrev(t *testing.T, tr *TreeMap[int, string]) {
   439  	tr.Set(0, "a")
   440  	tr.Set(1, "b")
   441  	tr.Set(2, "c")
   442  	tr.Set(3, "d")
   443  	tr.Set(4, "e")
   444  	it := tr.Reverse()
   445  	defer func() {
   446  		if r := recover(); r == nil {
   447  			t.Error("should have panicked!")
   448  		}
   449  	}()
   450  	it.Prev()
   451  }
   452  
   453  func testRangeSingle(t *testing.T, tr *TreeMap[int, string]) {
   454  	tr.Set(0, "a")
   455  	tr.Set(1, "b")
   456  	tr.Set(2, "c")
   457  	visited := false
   458  	for it, end := tr.Range(1, 1); it != end; it.Next() {
   459  		if visited || it.Value() != "b" {
   460  			t.Error("only single element 'b' should be found")
   461  		}
   462  		visited = true
   463  	}
   464  	if !visited {
   465  		t.Error("single element 'b' should be found")
   466  	}
   467  }