github.com/songzhibin97/go-baseutils@v0.0.2-0.20240302024150-487d8ce9c082/structure/maps/skipmap/skipmap2_test.go (about)

     1  package skipmap
     2  
     3  import (
     4  	"reflect"
     5  	"strings"
     6  	"testing"
     7  
     8  	"github.com/songzhibin97/go-baseutils/base/bcomparator"
     9  )
    10  
    11  func TestMapPut(t *testing.T) {
    12  	m := New[int, string](bcomparator.IntComparator())
    13  	m.Put(5, "e")
    14  	m.Put(6, "f")
    15  	m.Put(7, "g")
    16  	m.Put(3, "c")
    17  	m.Put(4, "d")
    18  	m.Put(1, "x")
    19  	m.Put(2, "b")
    20  	m.Put(1, "a") //overwrite
    21  
    22  	if actualValue := m.Size(); actualValue != 7 {
    23  		t.Errorf("Got %v expected %v", actualValue, 7)
    24  	}
    25  	if actualValue, expectedValue := m.Keys(), []int{1, 2, 3, 4, 5, 6, 7}; !sameElements(actualValue, expectedValue) {
    26  		t.Errorf("Got %v expected %v", actualValue, expectedValue)
    27  	}
    28  	if actualValue, expectedValue := m.Values(), []string{"a", "b", "c", "d", "e", "f", "g"}; !sameElements(actualValue, expectedValue) {
    29  		t.Errorf("Got %v expected %v", actualValue, expectedValue)
    30  	}
    31  
    32  	// key,expectedValue,expectedFound
    33  	tests1 := [][]interface{}{
    34  		{1, "a", true},
    35  		{2, "b", true},
    36  		{3, "c", true},
    37  		{4, "d", true},
    38  		{5, "e", true},
    39  		{6, "f", true},
    40  		{7, "g", true},
    41  		{8, "", false},
    42  	}
    43  
    44  	for _, test := range tests1 {
    45  		// retrievals
    46  		actualValue, actualFound := m.Get(test[0].(int))
    47  		if actualValue != test[1] || actualFound != test[2] {
    48  			t.Errorf("Got %v expected %v", actualValue, test[1])
    49  		}
    50  	}
    51  }
    52  
    53  func TestMapRemove(t *testing.T) {
    54  	m := New[int, string](bcomparator.IntComparator())
    55  	m.Put(5, "e")
    56  	m.Put(6, "f")
    57  	m.Put(7, "g")
    58  	m.Put(3, "c")
    59  	m.Put(4, "d")
    60  	m.Put(1, "x")
    61  	m.Put(2, "b")
    62  	m.Put(1, "a") //overwrite
    63  
    64  	m.Remove(5)
    65  	m.Remove(6)
    66  	m.Remove(7)
    67  	m.Remove(8)
    68  	m.Remove(5)
    69  
    70  	if actualValue, expectedValue := m.Keys(), []int{1, 2, 3, 4}; !sameElements(actualValue, expectedValue) {
    71  		t.Errorf("Got %v expected %v", actualValue, expectedValue)
    72  	}
    73  
    74  	if actualValue, expectedValue := m.Values(), []string{"a", "b", "c", "d"}; !sameElements(actualValue, expectedValue) {
    75  		t.Errorf("Got %v expected %v", actualValue, expectedValue)
    76  	}
    77  	if actualValue := m.Size(); actualValue != 4 {
    78  		t.Errorf("Got %v expected %v", actualValue, 4)
    79  	}
    80  
    81  	tests2 := [][]interface{}{
    82  		{1, "a", true},
    83  		{2, "b", true},
    84  		{3, "c", true},
    85  		{4, "d", true},
    86  		{5, "", false},
    87  		{6, "", false},
    88  		{7, "", false},
    89  		{8, "", false},
    90  	}
    91  
    92  	for _, test := range tests2 {
    93  		actualValue, actualFound := m.Get(test[0].(int))
    94  		if actualValue != test[1] || actualFound != test[2] {
    95  			t.Errorf("Got %v expected %v", actualValue, test[1])
    96  		}
    97  	}
    98  
    99  	m.Remove(1)
   100  	m.Remove(4)
   101  	m.Remove(2)
   102  	m.Remove(3)
   103  	m.Remove(2)
   104  	m.Remove(2)
   105  
   106  	if actualValue, expectedValue := m.Keys(), []int{}; !reflect.DeepEqual(actualValue, expectedValue) {
   107  		t.Errorf("Got %v expected %v", actualValue, expectedValue)
   108  	}
   109  	if actualValue, expectedValue := m.Values(), []string{}; !reflect.DeepEqual(actualValue, expectedValue) {
   110  		t.Errorf("Got %v expected %v", actualValue, expectedValue)
   111  	}
   112  	if actualValue := m.Size(); actualValue != 0 {
   113  		t.Errorf("Got %v expected %v", actualValue, 0)
   114  	}
   115  	if actualValue := m.Empty(); actualValue != true {
   116  		t.Errorf("Got %v expected %v", actualValue, true)
   117  	}
   118  }
   119  
   120  func TestMapString(t *testing.T) {
   121  	c := New[string, int](bcomparator.StringComparator())
   122  	c.Put("a", 1)
   123  	if !strings.HasPrefix(c.String(), "SkipMap") {
   124  		t.Errorf("String should start with container name")
   125  	}
   126  }
   127  
   128  func sameElements[E any](a []E, b []E) bool {
   129  	if len(a) != len(b) {
   130  		return false
   131  	}
   132  	for _, av := range a {
   133  		found := false
   134  		for _, bv := range b {
   135  			if reflect.DeepEqual(av, bv) {
   136  				found = true
   137  				break
   138  			}
   139  		}
   140  		if !found {
   141  			return false
   142  		}
   143  	}
   144  	return true
   145  }
   146  
   147  func benchmarkGet[K int, V struct{}](b *testing.B, m *Map[K, V], size int) {
   148  	for i := 0; i < b.N; i++ {
   149  		for n := 0; n < size; n++ {
   150  			m.Get(K(n))
   151  		}
   152  	}
   153  }
   154  
   155  func benchmarkPut[K int, V struct{}](b *testing.B, m *Map[K, V], size int) {
   156  	for i := 0; i < b.N; i++ {
   157  		for n := 0; n < size; n++ {
   158  			m.Put(K(n), struct{}{})
   159  		}
   160  	}
   161  }
   162  
   163  func benchmarkRemove[K int, V struct{}](b *testing.B, m *Map[K, V], size int) {
   164  	for i := 0; i < b.N; i++ {
   165  		for n := 0; n < size; n++ {
   166  			m.Remove(K(n))
   167  		}
   168  	}
   169  }
   170  
   171  func BenchmarkSkipMapGet100(b *testing.B) {
   172  	b.StopTimer()
   173  	size := 100
   174  	m := New[int, struct{}](bcomparator.IntComparator())
   175  	for n := 0; n < size; n++ {
   176  		m.Put(n, struct{}{})
   177  	}
   178  	b.StartTimer()
   179  	benchmarkGet(b, m, size)
   180  }
   181  
   182  func BenchmarkSkipMapGet1000(b *testing.B) {
   183  	b.StopTimer()
   184  	size := 1000
   185  	m := New[int, struct{}](bcomparator.IntComparator())
   186  	for n := 0; n < size; n++ {
   187  		m.Put(n, struct{}{})
   188  	}
   189  	b.StartTimer()
   190  	benchmarkGet(b, m, size)
   191  }
   192  
   193  func BenchmarkSkipMapGet10000(b *testing.B) {
   194  	b.StopTimer()
   195  	size := 10000
   196  	m := New[int, struct{}](bcomparator.IntComparator())
   197  	for n := 0; n < size; n++ {
   198  		m.Put(n, struct{}{})
   199  	}
   200  	b.StartTimer()
   201  	benchmarkGet(b, m, size)
   202  }
   203  
   204  func BenchmarkSkipMapGet100000(b *testing.B) {
   205  	b.StopTimer()
   206  	size := 100000
   207  	m := New[int, struct{}](bcomparator.IntComparator())
   208  	for n := 0; n < size; n++ {
   209  		m.Put(n, struct{}{})
   210  	}
   211  	b.StartTimer()
   212  	benchmarkGet(b, m, size)
   213  }
   214  
   215  func BenchmarkSkipMapPut100(b *testing.B) {
   216  	b.StopTimer()
   217  	size := 100
   218  	m := New[int, struct{}](bcomparator.IntComparator())
   219  	b.StartTimer()
   220  	benchmarkPut(b, m, size)
   221  }
   222  
   223  func BenchmarkSkipMapPut1000(b *testing.B) {
   224  	b.StopTimer()
   225  	size := 1000
   226  	m := New[int, struct{}](bcomparator.IntComparator())
   227  	for n := 0; n < size; n++ {
   228  		m.Put(n, struct{}{})
   229  	}
   230  	b.StartTimer()
   231  	benchmarkPut(b, m, size)
   232  }
   233  
   234  func BenchmarkSkipMapPut10000(b *testing.B) {
   235  	b.StopTimer()
   236  	size := 10000
   237  	m := New[int, struct{}](bcomparator.IntComparator())
   238  	for n := 0; n < size; n++ {
   239  		m.Put(n, struct{}{})
   240  	}
   241  	b.StartTimer()
   242  	benchmarkPut(b, m, size)
   243  }
   244  
   245  func BenchmarkSkipMapPut100000(b *testing.B) {
   246  	b.StopTimer()
   247  	size := 100000
   248  	m := New[int, struct{}](bcomparator.IntComparator())
   249  	for n := 0; n < size; n++ {
   250  		m.Put(n, struct{}{})
   251  	}
   252  	b.StartTimer()
   253  	benchmarkPut(b, m, size)
   254  }
   255  
   256  func BenchmarkSkipMapRemove100(b *testing.B) {
   257  	b.StopTimer()
   258  	size := 100
   259  	m := New[int, struct{}](bcomparator.IntComparator())
   260  	for n := 0; n < size; n++ {
   261  		m.Put(n, struct{}{})
   262  	}
   263  	b.StartTimer()
   264  	benchmarkRemove(b, m, size)
   265  }
   266  
   267  func BenchmarkSkipMapRemove1000(b *testing.B) {
   268  	b.StopTimer()
   269  	size := 1000
   270  	m := New[int, struct{}](bcomparator.IntComparator())
   271  	for n := 0; n < size; n++ {
   272  		m.Put(n, struct{}{})
   273  	}
   274  	b.StartTimer()
   275  	benchmarkRemove(b, m, size)
   276  }
   277  
   278  func BenchmarkSkipMapRemove10000(b *testing.B) {
   279  	b.StopTimer()
   280  	size := 10000
   281  	m := New[int, struct{}](bcomparator.IntComparator())
   282  	for n := 0; n < size; n++ {
   283  		m.Put(n, struct{}{})
   284  	}
   285  	b.StartTimer()
   286  	benchmarkRemove(b, m, size)
   287  }
   288  
   289  func BenchmarkSkipMapRemove100000(b *testing.B) {
   290  	b.StopTimer()
   291  	size := 100000
   292  	m := New[int, struct{}](bcomparator.IntComparator())
   293  	for n := 0; n < size; n++ {
   294  		m.Put(n, struct{}{})
   295  	}
   296  	b.StartTimer()
   297  	benchmarkRemove(b, m, size)
   298  }