github.com/haraldrudell/parl@v0.4.176/sets/set_test.go (about)

     1  /*
     2  © 2023–present Harald Rudell <harald.rudell@gmail.com> (https://haraldrudell.github.io/haraldrudell/)
     3  ISC License
     4  */
     5  
     6  package sets
     7  
     8  import (
     9  	"testing"
    10  
    11  	"github.com/haraldrudell/parl/iters"
    12  )
    13  
    14  func TestSet(t *testing.T) {
    15  	var value1, value2 = 1, 2
    16  	var name1, name2 = "n1", "n2"
    17  
    18  	var value1full string
    19  	var value1stringT = name1
    20  	var values = []SetElement[int]{
    21  		{ValueV: value1, Name: name1},
    22  		{ValueV: value2, Name: name2},
    23  	}
    24  
    25  	var isValid, hasValue bool
    26  	var value, zeroValue int
    27  	var iterator iters.Iterator[int]
    28  	var full string
    29  
    30  	// IsValid() Iterator() Description() StringT() String()
    31  	var set Set[int]
    32  	var reset = func() {
    33  		set = NewSet[int](values)
    34  	}
    35  
    36  	// IsValid of element should return true
    37  	reset()
    38  	isValid = set.IsValid(value1)
    39  	if !isValid {
    40  		t.Error("IsValid false")
    41  	}
    42  
    43  	// IsValid of non-element should return false
    44  	reset()
    45  	isValid = set.IsValid(zeroValue)
    46  	if isValid {
    47  		t.Error("IsValid true")
    48  	}
    49  
    50  	// Iterator should iterate
    51  	reset()
    52  	iterator = set.Iterator()
    53  	value, hasValue = iterator.Next()
    54  	_ = hasValue
    55  	if value != value1 {
    56  		t.Errorf("Iterator.Next %d exp %d", value, value1)
    57  	}
    58  
    59  	// Description
    60  	reset()
    61  	full = set.Description(value1)
    62  	if full != value1full {
    63  		t.Errorf("Description %q exp %q", full, value1full)
    64  	}
    65  
    66  	// StringT
    67  	reset()
    68  	full = set.StringT(value1)
    69  	if full != value1stringT {
    70  		t.Errorf("StringT %q exp %q", full, value1stringT)
    71  	}
    72  }