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