github.com/songzhibin97/go-baseutils@v0.0.2-0.20240302024150-487d8ce9c082/structure/sets/skipset/skipset2_test.go (about)

     1  package skipset
     2  
     3  import (
     4  	"strings"
     5  	"testing"
     6  
     7  	"github.com/songzhibin97/go-baseutils/base/bcomparator"
     8  )
     9  
    10  func TestSetNew(t *testing.T) {
    11  	set := New[int](bcomparator.IntComparator())
    12  	set.Add(2, 1)
    13  
    14  	if actualValue := set.Size(); actualValue != 2 {
    15  		t.Errorf("Got %v expected %v", actualValue, 2)
    16  	}
    17  	if actualValue := set.Contains(1); actualValue != true {
    18  		t.Errorf("Got %v expected %v", actualValue, true)
    19  	}
    20  	if actualValue := set.Contains(2); actualValue != true {
    21  		t.Errorf("Got %v expected %v", actualValue, true)
    22  	}
    23  	if actualValue := set.Contains(3); actualValue != false {
    24  		t.Errorf("Got %v expected %v", actualValue, true)
    25  	}
    26  }
    27  
    28  func TestSetAdd(t *testing.T) {
    29  	set := New[int](bcomparator.IntComparator())
    30  	set.Add()
    31  	set.Add(1)
    32  	set.Add(2)
    33  	set.Add(2, 3)
    34  	set.Add()
    35  	if actualValue := set.Empty(); actualValue != false {
    36  		t.Errorf("Got %v expected %v", actualValue, false)
    37  	}
    38  	if actualValue := set.Size(); actualValue != 3 {
    39  		t.Errorf("Got %v expected %v", actualValue, 3)
    40  	}
    41  }
    42  
    43  func TestSetContains(t *testing.T) {
    44  	set := New[int](bcomparator.IntComparator())
    45  	set.Add(3, 1, 2)
    46  	set.Add(2, 3)
    47  	set.Add()
    48  	if actualValue := set.Contains(); actualValue != true {
    49  		t.Errorf("Got %v expected %v", actualValue, true)
    50  	}
    51  	if actualValue := set.Contains(1); actualValue != true {
    52  		t.Errorf("Got %v expected %v", actualValue, true)
    53  	}
    54  	if actualValue := set.Contains(1, 2, 3); actualValue != true {
    55  		t.Errorf("Got %v expected %v", actualValue, true)
    56  	}
    57  	if actualValue := set.Contains(1, 2, 3, 4); actualValue != false {
    58  		t.Errorf("Got %v expected %v", actualValue, false)
    59  	}
    60  }
    61  
    62  func TestSetRemove(t *testing.T) {
    63  	set := New[int](bcomparator.IntComparator())
    64  	set.Add(3, 1, 2)
    65  	set.Remove()
    66  	if actualValue := set.Size(); actualValue != 3 {
    67  		t.Errorf("Got %v expected %v", actualValue, 3)
    68  	}
    69  	set.Remove(1)
    70  	if actualValue := set.Size(); actualValue != 2 {
    71  		t.Errorf("Got %v expected %v", actualValue, 2)
    72  	}
    73  	set.Remove(3)
    74  	set.Remove(3)
    75  	set.Remove()
    76  	set.Remove(2)
    77  	if actualValue := set.Size(); actualValue != 0 {
    78  		t.Errorf("Got %v expected %v", actualValue, 0)
    79  	}
    80  }
    81  
    82  func TestSetString(t *testing.T) {
    83  	c := New[int](bcomparator.IntComparator())
    84  	c.Add(1)
    85  	if !strings.HasPrefix(c.String(), "SkipSet") {
    86  		t.Errorf("String should start with container name")
    87  	}
    88  }
    89  
    90  func benchmarkContains[E int](b *testing.B, set *Set[E], size int) {
    91  	for i := 0; i < b.N; i++ {
    92  		for n := 0; n < size; n++ {
    93  			set.Contains(E(n))
    94  		}
    95  	}
    96  }
    97  
    98  func benchmarkAdd[E int](b *testing.B, set *Set[E], size int) {
    99  	for i := 0; i < b.N; i++ {
   100  		for n := 0; n < size; n++ {
   101  			set.Add(E(n))
   102  		}
   103  	}
   104  }
   105  
   106  func benchmarkRemove[E int](b *testing.B, set *Set[E], size int) {
   107  	for i := 0; i < b.N; i++ {
   108  		for n := 0; n < size; n++ {
   109  			set.Remove(E(n))
   110  		}
   111  	}
   112  }
   113  
   114  func BenchmarkSkipSetContains100(b *testing.B) {
   115  	b.StopTimer()
   116  	size := 100
   117  	set := New[int](bcomparator.IntComparator())
   118  	for n := 0; n < size; n++ {
   119  		set.Add(n)
   120  	}
   121  	b.StartTimer()
   122  	benchmarkContains(b, set, size)
   123  }
   124  
   125  func BenchmarkSkipSetContains1000(b *testing.B) {
   126  	b.StopTimer()
   127  	size := 1000
   128  	set := New[int](bcomparator.IntComparator())
   129  	for n := 0; n < size; n++ {
   130  		set.Add(n)
   131  	}
   132  	b.StartTimer()
   133  	benchmarkContains(b, set, size)
   134  }
   135  
   136  func BenchmarkSkipSetContains10000(b *testing.B) {
   137  	b.StopTimer()
   138  	size := 10000
   139  	set := New[int](bcomparator.IntComparator())
   140  	for n := 0; n < size; n++ {
   141  		set.Add(n)
   142  	}
   143  	b.StartTimer()
   144  	benchmarkContains(b, set, size)
   145  }
   146  
   147  func BenchmarkSkipSetContains100000(b *testing.B) {
   148  	b.StopTimer()
   149  	size := 100000
   150  	set := New[int](bcomparator.IntComparator())
   151  	for n := 0; n < size; n++ {
   152  		set.Add(n)
   153  	}
   154  	b.StartTimer()
   155  	benchmarkContains(b, set, size)
   156  }
   157  
   158  func BenchmarkSkipSetAdd100(b *testing.B) {
   159  	b.StopTimer()
   160  	size := 100
   161  	set := New[int](bcomparator.IntComparator())
   162  	b.StartTimer()
   163  	benchmarkAdd(b, set, size)
   164  }
   165  
   166  func BenchmarkSkipSetAdd1000(b *testing.B) {
   167  	b.StopTimer()
   168  	size := 1000
   169  	set := New[int](bcomparator.IntComparator())
   170  	for n := 0; n < size; n++ {
   171  		set.Add(n)
   172  	}
   173  	b.StartTimer()
   174  	benchmarkAdd(b, set, size)
   175  }
   176  
   177  func BenchmarkSkipSetAdd10000(b *testing.B) {
   178  	b.StopTimer()
   179  	size := 10000
   180  	set := New[int](bcomparator.IntComparator())
   181  	for n := 0; n < size; n++ {
   182  		set.Add(n)
   183  	}
   184  	b.StartTimer()
   185  	benchmarkAdd(b, set, size)
   186  }
   187  
   188  func BenchmarkSkipSetAdd100000(b *testing.B) {
   189  	b.StopTimer()
   190  	size := 100000
   191  	set := New[int](bcomparator.IntComparator())
   192  	for n := 0; n < size; n++ {
   193  		set.Add(n)
   194  	}
   195  	b.StartTimer()
   196  	benchmarkAdd(b, set, size)
   197  }
   198  
   199  func BenchmarkSkipSetRemove100(b *testing.B) {
   200  	b.StopTimer()
   201  	size := 100
   202  	set := New[int](bcomparator.IntComparator())
   203  	for n := 0; n < size; n++ {
   204  		set.Add(n)
   205  	}
   206  	b.StartTimer()
   207  	benchmarkRemove(b, set, size)
   208  }
   209  
   210  func BenchmarkSkipSetRemove1000(b *testing.B) {
   211  	b.StopTimer()
   212  	size := 1000
   213  	set := New[int](bcomparator.IntComparator())
   214  	for n := 0; n < size; n++ {
   215  		set.Add(n)
   216  	}
   217  	b.StartTimer()
   218  	benchmarkRemove(b, set, size)
   219  }
   220  
   221  func BenchmarkSkipSetRemove10000(b *testing.B) {
   222  	b.StopTimer()
   223  	size := 10000
   224  	set := New[int](bcomparator.IntComparator())
   225  	for n := 0; n < size; n++ {
   226  		set.Add(n)
   227  	}
   228  	b.StartTimer()
   229  	benchmarkRemove(b, set, size)
   230  }
   231  
   232  func BenchmarkSkipSetRemove100000(b *testing.B) {
   233  	b.StopTimer()
   234  	size := 100000
   235  	set := New[int](bcomparator.IntComparator())
   236  	for n := 0; n < size; n++ {
   237  		set.Add(n)
   238  	}
   239  	b.StartTimer()
   240  	benchmarkRemove(b, set, size)
   241  }