github.com/haraldrudell/parl@v0.4.176/iters/simple-func_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 iters 7 8 import ( 9 "testing" 10 11 "github.com/haraldrudell/parl/perrors" 12 ) 13 14 func TestSimpleFunc(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 = NewSimpleFunctionIterator(newHasSimpleFunc(values).SimpleIteratorFunc) 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 // 86 // Cond should return true and update value 87 reset() 88 condition = iterator.Cond(&value) 89 _ = condition 90 condition = iterator.Cond(&value) 91 _ = condition 92 value = zeroValue 93 condition = iterator.Cond(&value) 94 if condition { 95 t.Error("CondCondCond condition true") 96 } 97 if value != zeroValue { 98 t.Errorf("CondCondCond value %d exp %d", value, zeroValue) 99 } 100 101 // NextNext should return second value 102 reset() 103 value, hasValue = iterator.Next() 104 _ = value 105 _ = hasValue 106 value, hasValue = iterator.Next() 107 if !hasValue { 108 t.Error("NextNext hasValue false") 109 } 110 if value != value2 { 111 t.Errorf("NextNext value %d exp %d", value, value2) 112 } 113 114 // NextNextNext should return no value 115 reset() 116 value, hasValue = iterator.Next() 117 _ = value 118 _ = hasValue 119 value, hasValue = iterator.Next() 120 _ = value 121 _ = hasValue 122 value, hasValue = iterator.Next() 123 if hasValue { 124 t.Error("NextNextNext hasValue true") 125 } 126 if value != zeroValue { 127 t.Errorf("NextNextNext value %d exp %d", value, zeroValue) 128 } 129 130 // CancelCond should return false 131 reset() 132 err = iterator.Cancel() 133 _ = err 134 condition = iterator.Cond(&value) 135 if condition { 136 t.Error("CancelCond condition true") 137 } 138 139 // CancelNext should return no value 140 reset() 141 err = iterator.Cancel() 142 _ = err 143 value, hasValue = iterator.Next() 144 if hasValue { 145 t.Error("CancelNext hasValue true") 146 } 147 if value != zeroValue { 148 t.Errorf("CancelNext value %d exp %d", value, zeroValue) 149 } 150 } 151 152 type hasSimpleFunc[T any] struct { 153 slice []T 154 index int 155 } 156 157 func newHasSimpleFunc[T any](slice []T) (s *hasSimpleFunc[T]) { return &hasSimpleFunc[T]{slice: slice} } 158 func (s *hasSimpleFunc[T]) SimpleIteratorFunc() (value T, hasValue bool) { 159 if hasValue = s.index < len(s.slice); !hasValue { 160 return 161 } 162 value = s.slice[s.index] 163 s.index++ 164 165 return 166 }