github.com/haraldrudell/parl@v0.4.176/pmaps/pmaps2/map_test.go (about)

     1  /*
     2  © 2022–present Harald Rudell <harald.rudell@gmail.com> (https://haraldrudell.github.io/haraldrudell/)
     3  ISC License
     4  */
     5  
     6  package pmaps2
     7  
     8  import (
     9  	"testing"
    10  )
    11  
    12  type testMapValue struct{ value int }
    13  
    14  func TestMap(t *testing.T) {
    15  	var v1 = testMapValue{1}
    16  	var v2 = testMapValue{2}
    17  	var v3 = testMapValue{3}
    18  	var expLength = 2
    19  	var zeroValue *testMapValue
    20  
    21  	var m, m2 Map[int, *testMapValue]
    22  	var value *testMapValue
    23  	var ok bool
    24  
    25  	m = *NewMap[int, *testMapValue]()
    26  	m.Put(v1.value, &v1)
    27  	m.Put(v2.value, &v2)
    28  	m.Put(v1.value, &v1)
    29  
    30  	// Length should return number of elements
    31  	if m.Length() != expLength {
    32  		t.Errorf("Length %d exp %d", m.Length(), expLength)
    33  	}
    34  
    35  	// Get should return the corresponding mapping
    36  	value, ok = m.Get(v2.value)
    37  	if !ok {
    38  		t.Error("ok false")
    39  	}
    40  	if value != &v2 {
    41  		t.Errorf("Get %v exp %v", value, &v2)
    42  	}
    43  
    44  	// Get for non-existing mapping is zero-value, false
    45  	value, ok = m.Get(v3.value)
    46  	if ok {
    47  		t.Error("ok true")
    48  	}
    49  	if value != zeroValue {
    50  		t.Errorf("Get2 %v exp %v", value, zeroValue)
    51  	}
    52  
    53  	// Clone should return duplicate
    54  	m.Clone(&m2)
    55  	if m2.Length() != expLength {
    56  		t.Errorf("Length m2 %d exp %d", m2.Length(), expLength)
    57  	}
    58  }