github.com/haraldrudell/parl@v0.4.176/sets/slice-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 TestSliceSet(t *testing.T) {
    15  	type e struct{ s string }
    16  	var value1, value2 = 0, 2
    17  	var name1, name2 = "n1", "n2"
    18  
    19  	var value1full string
    20  	var value1stringT = "{" + name1 + "}"
    21  	var values = []e{{name1}, {name2}}
    22  
    23  	var isValid, hasValue bool
    24  	var value int
    25  	var full string
    26  	var iterator iters.Iterator[int]
    27  	var elementType *e
    28  	var eIterator iters.Iterator[*e]
    29  
    30  	// IsValid() Iterator() Description() StringT() String()
    31  	var set SetID[int, e]
    32  	var reset = func() {
    33  		set = NewSliceSet(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(value2)
    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  
    73  	// Element should return element
    74  	reset()
    75  	elementType = set.Element(value1)
    76  	if elementType != &values[0] {
    77  		t.Error("set.Element bad")
    78  	}
    79  
    80  	// EIterator should iterate
    81  	reset()
    82  	eIterator = set.EIterator()
    83  	elementType, hasValue = eIterator.Next()
    84  	_ = hasValue
    85  	if elementType != &values[0] {
    86  		t.Error("EIterator.Next bad")
    87  	}
    88  }