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