github.com/dubbogo/gost@v1.14.0/container/set/hashset_test.go (about)

     1  /*
     2   * Licensed to the Apache Software Foundation (ASF) under one or more
     3   * contributor license agreements.  See the NOTICE file distributed with
     4   * this work for additional information regarding copyright ownership.
     5   * The ASF licenses this file to You under the Apache License, Version 2.0
     6   * (the "License"); you may not use this file except in compliance with
     7   * the License.  You may obtain a copy of the License at
     8   *
     9   *     http://www.apache.org/licenses/LICENSE-2.0
    10   *
    11   * Unless required by applicable law or agreed to in writing, software
    12   * distributed under the License is distributed on an "AS IS" BASIS,
    13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    14   * See the License for the specific language governing permissions and
    15   * limitations under the License.
    16   */
    17  package gxset
    18  
    19  import (
    20  	"testing"
    21  )
    22  
    23  func TestSetNew(t *testing.T) {
    24  	set := NewSet(2, 1)
    25  
    26  	if actualValue := set.Size(); actualValue != 2 {
    27  		t.Errorf("Got %v expected %v", actualValue, 2)
    28  	}
    29  	if actualValue := set.Contains(1); actualValue != true {
    30  		t.Errorf("Got %v expected %v", actualValue, true)
    31  	}
    32  	if actualValue := set.Contains(2); actualValue != true {
    33  		t.Errorf("Got %v expected %v", actualValue, true)
    34  	}
    35  	if actualValue := set.Contains(3); actualValue != false {
    36  		t.Errorf("Got %v expected %v", actualValue, true)
    37  	}
    38  }
    39  
    40  func TestSetAdd(t *testing.T) {
    41  	set := NewSet()
    42  	set.Add()
    43  	set.Add(1)
    44  	set.Add(2)
    45  	set.Add(2, 3)
    46  	set.Add()
    47  	if actualValue := set.Empty(); actualValue != false {
    48  		t.Errorf("Got %v expected %v", actualValue, false)
    49  	}
    50  	if actualValue := set.Size(); actualValue != 3 {
    51  		t.Errorf("Got %v expected %v", actualValue, 3)
    52  	}
    53  }
    54  
    55  func TestSetContains(t *testing.T) {
    56  	set := NewSet()
    57  	set.Add(3, 1, 2)
    58  	set.Add(2, 3)
    59  	set.Add()
    60  	if actualValue := set.Contains(); actualValue != true {
    61  		t.Errorf("Got %v expected %v", actualValue, true)
    62  	}
    63  	if actualValue := set.Contains(1); actualValue != true {
    64  		t.Errorf("Got %v expected %v", actualValue, true)
    65  	}
    66  	if actualValue := set.Contains(1, 2, 3); actualValue != true {
    67  		t.Errorf("Got %v expected %v", actualValue, true)
    68  	}
    69  	if actualValue := set.Contains(1, 2, 3, 4); actualValue != false {
    70  		t.Errorf("Got %v expected %v", actualValue, false)
    71  	}
    72  }
    73  
    74  func TestSetRemove(t *testing.T) {
    75  	set := NewSet()
    76  	set.Add(3, 1, 2)
    77  	set.Remove()
    78  	if actualValue := set.Size(); actualValue != 3 {
    79  		t.Errorf("Got %v expected %v", actualValue, 3)
    80  	}
    81  	set.Remove(1)
    82  	if actualValue := set.Size(); actualValue != 2 {
    83  		t.Errorf("Got %v expected %v", actualValue, 2)
    84  	}
    85  	set.Remove(3)
    86  	set.Remove(3)
    87  	set.Remove()
    88  	set.Remove(2)
    89  	if actualValue := set.Size(); actualValue != 0 {
    90  		t.Errorf("Got %v expected %v", actualValue, 0)
    91  	}
    92  }
    93  
    94  func benchmarkContains(b *testing.B, set *HashSet, size int) {
    95  	for i := 0; i < b.N; i++ {
    96  		for n := 0; n < size; n++ {
    97  			set.Contains(n)
    98  		}
    99  	}
   100  }
   101  
   102  func benchmarkAdd(b *testing.B, set *HashSet, size int) {
   103  	for i := 0; i < b.N; i++ {
   104  		for n := 0; n < size; n++ {
   105  			set.Add(n)
   106  		}
   107  	}
   108  }
   109  
   110  func benchmarkRemove(b *testing.B, set *HashSet, size int) {
   111  	for i := 0; i < b.N; i++ {
   112  		for n := 0; n < size; n++ {
   113  			set.Remove(n)
   114  		}
   115  	}
   116  }
   117  
   118  func BenchmarkHashSetContains100(b *testing.B) {
   119  	b.StopTimer()
   120  	size := 100
   121  	set := NewSet()
   122  	for n := 0; n < size; n++ {
   123  		set.Add(n)
   124  	}
   125  	b.StartTimer()
   126  	benchmarkContains(b, set, size)
   127  }
   128  
   129  func BenchmarkHashSetContains1000(b *testing.B) {
   130  	b.StopTimer()
   131  	size := 1000
   132  	set := NewSet()
   133  	for n := 0; n < size; n++ {
   134  		set.Add(n)
   135  	}
   136  	b.StartTimer()
   137  	benchmarkContains(b, set, size)
   138  }
   139  
   140  func BenchmarkHashSetContains10000(b *testing.B) {
   141  	b.StopTimer()
   142  	size := 10000
   143  	set := NewSet()
   144  	for n := 0; n < size; n++ {
   145  		set.Add(n)
   146  	}
   147  	b.StartTimer()
   148  	benchmarkContains(b, set, size)
   149  }
   150  
   151  func BenchmarkHashSetContains100000(b *testing.B) {
   152  	b.StopTimer()
   153  	size := 100000
   154  	set := NewSet()
   155  	for n := 0; n < size; n++ {
   156  		set.Add(n)
   157  	}
   158  	b.StartTimer()
   159  	benchmarkContains(b, set, size)
   160  }
   161  
   162  func BenchmarkHashSetAdd100(b *testing.B) {
   163  	b.StopTimer()
   164  	size := 100
   165  	set := NewSet()
   166  	b.StartTimer()
   167  	benchmarkAdd(b, set, size)
   168  }
   169  
   170  func BenchmarkHashSetAdd1000(b *testing.B) {
   171  	b.StopTimer()
   172  	size := 1000
   173  	set := NewSet()
   174  	for n := 0; n < size; n++ {
   175  		set.Add(n)
   176  	}
   177  	b.StartTimer()
   178  	benchmarkAdd(b, set, size)
   179  }
   180  
   181  func BenchmarkHashSetAdd10000(b *testing.B) {
   182  	b.StopTimer()
   183  	size := 10000
   184  	set := NewSet()
   185  	for n := 0; n < size; n++ {
   186  		set.Add(n)
   187  	}
   188  	b.StartTimer()
   189  	benchmarkAdd(b, set, size)
   190  }
   191  
   192  func BenchmarkHashSetAdd100000(b *testing.B) {
   193  	b.StopTimer()
   194  	size := 100000
   195  	set := NewSet()
   196  	for n := 0; n < size; n++ {
   197  		set.Add(n)
   198  	}
   199  	b.StartTimer()
   200  	benchmarkAdd(b, set, size)
   201  }
   202  
   203  func BenchmarkHashSetRemove100(b *testing.B) {
   204  	b.StopTimer()
   205  	size := 100
   206  	set := NewSet()
   207  	for n := 0; n < size; n++ {
   208  		set.Add(n)
   209  	}
   210  	b.StartTimer()
   211  	benchmarkRemove(b, set, size)
   212  }
   213  
   214  func BenchmarkHashSetRemove1000(b *testing.B) {
   215  	b.StopTimer()
   216  	size := 1000
   217  	set := NewSet()
   218  	for n := 0; n < size; n++ {
   219  		set.Add(n)
   220  	}
   221  	b.StartTimer()
   222  	benchmarkRemove(b, set, size)
   223  }
   224  
   225  func BenchmarkHashSetRemove10000(b *testing.B) {
   226  	b.StopTimer()
   227  	size := 10000
   228  	set := NewSet()
   229  	for n := 0; n < size; n++ {
   230  		set.Add(n)
   231  	}
   232  	b.StartTimer()
   233  	benchmarkRemove(b, set, size)
   234  }
   235  
   236  func BenchmarkHashSetRemove100000(b *testing.B) {
   237  	b.StopTimer()
   238  	size := 100000
   239  	set := NewSet()
   240  	for n := 0; n < size; n++ {
   241  		set.Add(n)
   242  	}
   243  	b.StartTimer()
   244  	benchmarkRemove(b, set, size)
   245  }