github.com/insionng/yougam@v0.0.0-20170714101924-2bc18d833463/libraries/petar/GoLLRB/llrb/llrb_test.go (about)

     1  // Copyright 2010 Petar Maymounkov. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package llrb
     6  
     7  import (
     8  	"math"
     9  	"math/rand"
    10  	"testing"
    11  )
    12  
    13  func TestCases(t *testing.T) {
    14  	tree := New()
    15  	tree.ReplaceOrInsert(Int(1))
    16  	tree.ReplaceOrInsert(Int(1))
    17  	if tree.Len() != 1 {
    18  		t.Errorf("expecting len 1")
    19  	}
    20  	if !tree.Has(Int(1)) {
    21  		t.Errorf("expecting to find key=1")
    22  	}
    23  
    24  	tree.Delete(Int(1))
    25  	if tree.Len() != 0 {
    26  		t.Errorf("expecting len 0")
    27  	}
    28  	if tree.Has(Int(1)) {
    29  		t.Errorf("not expecting to find key=1")
    30  	}
    31  
    32  	tree.Delete(Int(1))
    33  	if tree.Len() != 0 {
    34  		t.Errorf("expecting len 0")
    35  	}
    36  	if tree.Has(Int(1)) {
    37  		t.Errorf("not expecting to find key=1")
    38  	}
    39  }
    40  
    41  func TestReverseInsertOrder(t *testing.T) {
    42  	tree := New()
    43  	n := 100
    44  	for i := 0; i < n; i++ {
    45  		tree.ReplaceOrInsert(Int(n - i))
    46  	}
    47  	i := 0
    48  	tree.AscendGreaterOrEqual(Int(0), func(item Item) bool {
    49  		i++
    50  		if item.(Int) != Int(i) {
    51  			t.Errorf("bad order: got %d, expect %d", item.(Int), i)
    52  		}
    53  		return true
    54  	})
    55  }
    56  
    57  func TestRange(t *testing.T) {
    58  	tree := New()
    59  	order := []String{
    60  		"ab", "aba", "abc", "a", "aa", "aaa", "b", "a-", "a!",
    61  	}
    62  	for _, i := range order {
    63  		tree.ReplaceOrInsert(i)
    64  	}
    65  	k := 0
    66  	tree.AscendRange(String("ab"), String("ac"), func(item Item) bool {
    67  		if k > 3 {
    68  			t.Fatalf("returned more items than expected")
    69  		}
    70  		i1 := order[k]
    71  		i2 := item.(String)
    72  		if i1 != i2 {
    73  			t.Errorf("expecting %s, got %s", i1, i2)
    74  		}
    75  		k++
    76  		return true
    77  	})
    78  }
    79  
    80  func TestRandomInsertOrder(t *testing.T) {
    81  	tree := New()
    82  	n := 1000
    83  	perm := rand.Perm(n)
    84  	for i := 0; i < n; i++ {
    85  		tree.ReplaceOrInsert(Int(perm[i]))
    86  	}
    87  	j := 0
    88  	tree.AscendGreaterOrEqual(Int(0), func(item Item) bool {
    89  		if item.(Int) != Int(j) {
    90  			t.Fatalf("bad order")
    91  		}
    92  		j++
    93  		return true
    94  	})
    95  }
    96  
    97  func TestRandomReplace(t *testing.T) {
    98  	tree := New()
    99  	n := 100
   100  	perm := rand.Perm(n)
   101  	for i := 0; i < n; i++ {
   102  		tree.ReplaceOrInsert(Int(perm[i]))
   103  	}
   104  	perm = rand.Perm(n)
   105  	for i := 0; i < n; i++ {
   106  		if replaced := tree.ReplaceOrInsert(Int(perm[i])); replaced == nil || replaced.(Int) != Int(perm[i]) {
   107  			t.Errorf("error replacing")
   108  		}
   109  	}
   110  }
   111  
   112  func TestRandomInsertSequentialDelete(t *testing.T) {
   113  	tree := New()
   114  	n := 1000
   115  	perm := rand.Perm(n)
   116  	for i := 0; i < n; i++ {
   117  		tree.ReplaceOrInsert(Int(perm[i]))
   118  	}
   119  	for i := 0; i < n; i++ {
   120  		tree.Delete(Int(i))
   121  	}
   122  }
   123  
   124  func TestRandomInsertDeleteNonExistent(t *testing.T) {
   125  	tree := New()
   126  	n := 100
   127  	perm := rand.Perm(n)
   128  	for i := 0; i < n; i++ {
   129  		tree.ReplaceOrInsert(Int(perm[i]))
   130  	}
   131  	if tree.Delete(Int(200)) != nil {
   132  		t.Errorf("deleted non-existent item")
   133  	}
   134  	if tree.Delete(Int(-2)) != nil {
   135  		t.Errorf("deleted non-existent item")
   136  	}
   137  	for i := 0; i < n; i++ {
   138  		if u := tree.Delete(Int(i)); u == nil || u.(Int) != Int(i) {
   139  			t.Errorf("delete failed")
   140  		}
   141  	}
   142  	if tree.Delete(Int(200)) != nil {
   143  		t.Errorf("deleted non-existent item")
   144  	}
   145  	if tree.Delete(Int(-2)) != nil {
   146  		t.Errorf("deleted non-existent item")
   147  	}
   148  }
   149  
   150  func TestRandomInsertPartialDeleteOrder(t *testing.T) {
   151  	tree := New()
   152  	n := 100
   153  	perm := rand.Perm(n)
   154  	for i := 0; i < n; i++ {
   155  		tree.ReplaceOrInsert(Int(perm[i]))
   156  	}
   157  	for i := 1; i < n-1; i++ {
   158  		tree.Delete(Int(i))
   159  	}
   160  	j := 0
   161  	tree.AscendGreaterOrEqual(Int(0), func(item Item) bool {
   162  		switch j {
   163  		case 0:
   164  			if item.(Int) != Int(0) {
   165  				t.Errorf("expecting 0")
   166  			}
   167  		case 1:
   168  			if item.(Int) != Int(n-1) {
   169  				t.Errorf("expecting %d", n-1)
   170  			}
   171  		}
   172  		j++
   173  		return true
   174  	})
   175  }
   176  
   177  func TestRandomInsertStats(t *testing.T) {
   178  	tree := New()
   179  	n := 100000
   180  	perm := rand.Perm(n)
   181  	for i := 0; i < n; i++ {
   182  		tree.ReplaceOrInsert(Int(perm[i]))
   183  	}
   184  	avg, _ := tree.HeightStats()
   185  	expAvg := math.Log2(float64(n)) - 1.5
   186  	if math.Abs(avg-expAvg) >= 2.0 {
   187  		t.Errorf("too much deviation from expected average height")
   188  	}
   189  }
   190  
   191  func BenchmarkInsert(b *testing.B) {
   192  	tree := New()
   193  	for i := 0; i < b.N; i++ {
   194  		tree.ReplaceOrInsert(Int(b.N - i))
   195  	}
   196  }
   197  
   198  func BenchmarkDelete(b *testing.B) {
   199  	b.StopTimer()
   200  	tree := New()
   201  	for i := 0; i < b.N; i++ {
   202  		tree.ReplaceOrInsert(Int(b.N - i))
   203  	}
   204  	b.StartTimer()
   205  	for i := 0; i < b.N; i++ {
   206  		tree.Delete(Int(i))
   207  	}
   208  }
   209  
   210  func BenchmarkDeleteMin(b *testing.B) {
   211  	b.StopTimer()
   212  	tree := New()
   213  	for i := 0; i < b.N; i++ {
   214  		tree.ReplaceOrInsert(Int(b.N - i))
   215  	}
   216  	b.StartTimer()
   217  	for i := 0; i < b.N; i++ {
   218  		tree.DeleteMin()
   219  	}
   220  }
   221  
   222  func TestInsertNoReplace(t *testing.T) {
   223  	tree := New()
   224  	n := 1000
   225  	for q := 0; q < 2; q++ {
   226  		perm := rand.Perm(n)
   227  		for i := 0; i < n; i++ {
   228  			tree.InsertNoReplace(Int(perm[i]))
   229  		}
   230  	}
   231  	j := 0
   232  	tree.AscendGreaterOrEqual(Int(0), func(item Item) bool {
   233  		if item.(Int) != Int(j/2) {
   234  			t.Fatalf("bad order")
   235  		}
   236  		j++
   237  		return true
   238  	})
   239  }