github.com/haraldrudell/parl@v0.4.176/sets/pointer-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  	"strconv"
    10  	"testing"
    11  
    12  	"github.com/haraldrudell/parl/iters"
    13  )
    14  
    15  func TestPointerSet(t *testing.T) {
    16  	var value1, value2 = basics(1), basics(2)
    17  	var value1d = strconv.Itoa(int(value1))
    18  	var values = []*basics{&value1, &value2}
    19  
    20  	var isValid, hasValue bool
    21  	var value, zeroValue *basics
    22  	var iterator iters.Iterator[*basics]
    23  	var full string
    24  
    25  	// IsValid() Iterator() Description() StringT() String()
    26  	var set Set[*basics]
    27  	var reset = func() {
    28  		// E is basics
    29  		// T is int
    30  		set = NewPointerSet(values)
    31  	}
    32  
    33  	// IsValid of element should return true
    34  	reset()
    35  	isValid = set.IsValid(&value1)
    36  	if !isValid {
    37  		t.Error("IsValid false")
    38  	}
    39  
    40  	// IsValid of non-element should return false
    41  	reset()
    42  	isValid = set.IsValid(zeroValue)
    43  	if isValid {
    44  		t.Error("IsValid true")
    45  	}
    46  
    47  	// Iterator should iterate
    48  	reset()
    49  	iterator = set.Iterator()
    50  	value, hasValue = iterator.Next()
    51  	_ = hasValue
    52  	if value != &value1 {
    53  		t.Errorf("Iterator.Next %d exp %d", value, value1)
    54  	}
    55  
    56  	// Description
    57  	reset()
    58  	full = set.Description(&value1)
    59  	if full != value1d {
    60  		t.Errorf("Description %q exp %q", full, value1d)
    61  	}
    62  
    63  	// StringT
    64  	reset()
    65  	var _ = (&FuncSet[int, string]{}).StringT
    66  	full = set.StringT(&value1)
    67  	if full != value1d {
    68  		t.Errorf("StringT %q exp %q", full, value1d)
    69  	}
    70  }