github.com/mymmsc/gox@v1.3.33/util/hashset/hashset_test.go (about)

     1  // Copyright (c) 2015, Emir Pasic. 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 hashset
     6  
     7  import (
     8  	"testing"
     9  )
    10  
    11  func TestSetNew(t *testing.T) {
    12  	set := New(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()
    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()
    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()
    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 TestSetSerialization(t *testing.T) {
    83  	set := New()
    84  	set.Add("a", "b", "c")
    85  
    86  	var err error
    87  	assert := func() {
    88  		if actualValue, expectedValue := set.Size(), 3; actualValue != expectedValue {
    89  			t.Errorf("Got %v expected %v", actualValue, expectedValue)
    90  		}
    91  		if actualValue := set.Contains("a", "b", "c"); actualValue != true {
    92  			t.Errorf("Got %v expected %v", actualValue, true)
    93  		}
    94  		if err != nil {
    95  			t.Errorf("Got error %v", err)
    96  		}
    97  	}
    98  
    99  	assert()
   100  
   101  	json, err := set.ToJSON()
   102  	assert()
   103  
   104  	err = set.FromJSON(json)
   105  	assert()
   106  }
   107  
   108  func benchmarkContains(b *testing.B, set *Set, size int) {
   109  	for i := 0; i < b.N; i++ {
   110  		for n := 0; n < size; n++ {
   111  			set.Contains(n)
   112  		}
   113  	}
   114  }
   115  
   116  func benchmarkAdd(b *testing.B, set *Set, size int) {
   117  	for i := 0; i < b.N; i++ {
   118  		for n := 0; n < size; n++ {
   119  			set.Add(n)
   120  		}
   121  	}
   122  }
   123  
   124  func benchmarkRemove(b *testing.B, set *Set, size int) {
   125  	for i := 0; i < b.N; i++ {
   126  		for n := 0; n < size; n++ {
   127  			set.Remove(n)
   128  		}
   129  	}
   130  }
   131  
   132  func BenchmarkHashSetContains100(b *testing.B) {
   133  	b.StopTimer()
   134  	size := 100
   135  	set := New()
   136  	for n := 0; n < size; n++ {
   137  		set.Add(n)
   138  	}
   139  	b.StartTimer()
   140  	benchmarkContains(b, set, size)
   141  }
   142  
   143  func BenchmarkHashSetContains1000(b *testing.B) {
   144  	b.StopTimer()
   145  	size := 1000
   146  	set := New()
   147  	for n := 0; n < size; n++ {
   148  		set.Add(n)
   149  	}
   150  	b.StartTimer()
   151  	benchmarkContains(b, set, size)
   152  }
   153  
   154  func BenchmarkHashSetContains10000(b *testing.B) {
   155  	b.StopTimer()
   156  	size := 10000
   157  	set := New()
   158  	for n := 0; n < size; n++ {
   159  		set.Add(n)
   160  	}
   161  	b.StartTimer()
   162  	benchmarkContains(b, set, size)
   163  }
   164  
   165  func BenchmarkHashSetContains100000(b *testing.B) {
   166  	b.StopTimer()
   167  	size := 100000
   168  	set := New()
   169  	for n := 0; n < size; n++ {
   170  		set.Add(n)
   171  	}
   172  	b.StartTimer()
   173  	benchmarkContains(b, set, size)
   174  }
   175  
   176  func BenchmarkHashSetAdd100(b *testing.B) {
   177  	b.StopTimer()
   178  	size := 100
   179  	set := New()
   180  	b.StartTimer()
   181  	benchmarkAdd(b, set, size)
   182  }
   183  
   184  func BenchmarkHashSetAdd1000(b *testing.B) {
   185  	b.StopTimer()
   186  	size := 1000
   187  	set := New()
   188  	for n := 0; n < size; n++ {
   189  		set.Add(n)
   190  	}
   191  	b.StartTimer()
   192  	benchmarkAdd(b, set, size)
   193  }
   194  
   195  func BenchmarkHashSetAdd10000(b *testing.B) {
   196  	b.StopTimer()
   197  	size := 10000
   198  	set := New()
   199  	for n := 0; n < size; n++ {
   200  		set.Add(n)
   201  	}
   202  	b.StartTimer()
   203  	benchmarkAdd(b, set, size)
   204  }
   205  
   206  func BenchmarkHashSetAdd100000(b *testing.B) {
   207  	b.StopTimer()
   208  	size := 100000
   209  	set := New()
   210  	for n := 0; n < size; n++ {
   211  		set.Add(n)
   212  	}
   213  	b.StartTimer()
   214  	benchmarkAdd(b, set, size)
   215  }
   216  
   217  func BenchmarkHashSetRemove100(b *testing.B) {
   218  	b.StopTimer()
   219  	size := 100
   220  	set := New()
   221  	for n := 0; n < size; n++ {
   222  		set.Add(n)
   223  	}
   224  	b.StartTimer()
   225  	benchmarkRemove(b, set, size)
   226  }
   227  
   228  func BenchmarkHashSetRemove1000(b *testing.B) {
   229  	b.StopTimer()
   230  	size := 1000
   231  	set := New()
   232  	for n := 0; n < size; n++ {
   233  		set.Add(n)
   234  	}
   235  	b.StartTimer()
   236  	benchmarkRemove(b, set, size)
   237  }
   238  
   239  func BenchmarkHashSetRemove10000(b *testing.B) {
   240  	b.StopTimer()
   241  	size := 10000
   242  	set := New()
   243  	for n := 0; n < size; n++ {
   244  		set.Add(n)
   245  	}
   246  	b.StartTimer()
   247  	benchmarkRemove(b, set, size)
   248  }
   249  
   250  func BenchmarkHashSetRemove100000(b *testing.B) {
   251  	b.StopTimer()
   252  	size := 100000
   253  	set := New()
   254  	for n := 0; n < size; n++ {
   255  		set.Add(n)
   256  	}
   257  	b.StartTimer()
   258  	benchmarkRemove(b, set, size)
   259  }