github.com/haraldrudell/parl@v0.4.176/iters/slice_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 iters
     7  
     8  import (
     9  	"testing"
    10  
    11  	"github.com/haraldrudell/parl/perrors"
    12  )
    13  
    14  func TestSliceIterator(t *testing.T) {
    15  	var value1, value2 = 1, 2
    16  	var values = []int{value1, value2}
    17  
    18  	var err error
    19  	var value, zeroValue, iterationVariable int
    20  	var hasValue, condition bool
    21  	var actIterator Iterator[int]
    22  
    23  	var iterator Iterator[int]
    24  	var reset = func() {
    25  		iterator = NewSliceIterator(values)
    26  	}
    27  	// Init() Cond() Next() Same() Cancel()
    28  	var _ Slice[int]
    29  
    30  	// Init should return zero value and iterator
    31  	reset()
    32  	iterationVariable, actIterator = iterator.Init()
    33  	if iterationVariable != zeroValue {
    34  		t.Errorf("Init iterationVariable %d exp %d", iterationVariable, zeroValue)
    35  	}
    36  	if actIterator != iterator {
    37  		t.Error("Init iterator bad")
    38  	}
    39  
    40  	// Cond should return true and update value
    41  	reset()
    42  	value = zeroValue
    43  	condition = iterator.Cond(&value)
    44  	if !condition {
    45  		t.Error("Cond condition false")
    46  	}
    47  	if value != value1 {
    48  		t.Errorf("Cond value %d exp %d", value, value1)
    49  	}
    50  
    51  	// Next should return first value
    52  	reset()
    53  	value, hasValue = iterator.Next()
    54  	if !hasValue {
    55  		t.Error("Next hasValue false")
    56  	}
    57  	if value != value1 {
    58  		t.Errorf("Next value %d exp %d", value, value1)
    59  	}
    60  
    61  	// Cancel should return no error
    62  	reset()
    63  	err = iterator.Cancel()
    64  	if err != nil {
    65  		t.Errorf("Cancel err: %s", perrors.Short(err))
    66  	}
    67  
    68  	// CondCond should return second value
    69  	reset()
    70  	condition = iterator.Cond(&value)
    71  	_ = condition
    72  	value = zeroValue
    73  	condition = iterator.Cond(&value)
    74  	if !condition {
    75  		t.Error("CondCond condition false")
    76  	}
    77  	if value != value2 {
    78  		t.Errorf("Cond value %d exp %d", value, value2)
    79  	}
    80  
    81  	// CondCondCond should return no value
    82  	// request IsSame value twice should:
    83  	//	- retrieve the first value and return it
    84  	//	- then return the same value again
    85  	// Cond should return true and update value
    86  	reset()
    87  	condition = iterator.Cond(&value)
    88  	_ = condition
    89  	condition = iterator.Cond(&value)
    90  	_ = condition
    91  	value = zeroValue
    92  	condition = iterator.Cond(&value)
    93  	if condition {
    94  		t.Error("CondCondCond condition true")
    95  	}
    96  	if value != zeroValue {
    97  		t.Errorf("CondCondCond value %d exp %d", value, zeroValue)
    98  	}
    99  
   100  	// NextNext should return second value
   101  	reset()
   102  	value, hasValue = iterator.Next()
   103  	_ = value
   104  	_ = hasValue
   105  	value, hasValue = iterator.Next()
   106  	if !hasValue {
   107  		t.Error("NextNext hasValue false")
   108  	}
   109  	if value != value2 {
   110  		t.Errorf("NextNext value %d exp %d", value, value2)
   111  	}
   112  
   113  	// NextNextNext should return no value
   114  	reset()
   115  	value, hasValue = iterator.Next()
   116  	_ = value
   117  	_ = hasValue
   118  	value, hasValue = iterator.Next()
   119  	_ = value
   120  	_ = hasValue
   121  	value, hasValue = iterator.Next()
   122  	if hasValue {
   123  		t.Error("NextNextNext hasValue true")
   124  	}
   125  	if value != zeroValue {
   126  		t.Errorf("NextNextNext value %d exp %d", value, zeroValue)
   127  	}
   128  
   129  	// CancelCond should return false
   130  	reset()
   131  	err = iterator.Cancel()
   132  	_ = err
   133  	condition = iterator.Cond(&value)
   134  	if condition {
   135  		t.Error("CancelCond condition true")
   136  	}
   137  
   138  	// CancelNext should return no value
   139  	reset()
   140  	err = iterator.Cancel()
   141  	_ = err
   142  	value, hasValue = iterator.Next()
   143  	if hasValue {
   144  		t.Error("CancelNext hasValue true")
   145  	}
   146  	if value != zeroValue {
   147  		t.Errorf("CancelNext value %d exp %d", value, zeroValue)
   148  	}
   149  }