github.com/haraldrudell/parl@v0.4.176/iters/integer_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 TestInteger(t *testing.T) {
    15  	var value1, value2 = 1, 2
    16  
    17  	var err error
    18  	var value, zeroValue, iterationVariable int
    19  	var hasValue, condition bool
    20  	var actIterator Iterator[int]
    21  
    22  	var iterator Iterator[int]
    23  	var reset = func() {
    24  		iterator = NewIntegerIterator(value1, value2)
    25  	}
    26  	// Init() Cond() Next() Same() Cancel()
    27  	var _ Slice[int]
    28  
    29  	// Init should return zero value and iterator
    30  	reset()
    31  	iterationVariable, actIterator = iterator.Init()
    32  	if iterationVariable != zeroValue {
    33  		t.Errorf("Init iterationVariable %d exp %d", iterationVariable, zeroValue)
    34  	}
    35  	if actIterator != iterator {
    36  		t.Error("Init iterator bad")
    37  	}
    38  
    39  	// Cond should return true and update value
    40  	reset()
    41  	value = zeroValue
    42  	condition = iterator.Cond(&value)
    43  	if !condition {
    44  		t.Error("Cond condition false")
    45  	}
    46  	if value != value1 {
    47  		t.Errorf("Cond value %d exp %d", value, value1)
    48  	}
    49  
    50  	// Next should return first value
    51  	reset()
    52  	value, hasValue = iterator.Next()
    53  	if !hasValue {
    54  		t.Error("Next hasValue false")
    55  	}
    56  	if value != value1 {
    57  		t.Errorf("Next value %d exp %d", value, value1)
    58  	}
    59  
    60  	// Cancel should return no error
    61  	reset()
    62  	err = iterator.Cancel()
    63  	if err != nil {
    64  		t.Errorf("Cancel err: %s", perrors.Short(err))
    65  	}
    66  
    67  	// CondCond should return second value
    68  	reset()
    69  	condition = iterator.Cond(&value)
    70  	_ = condition
    71  	value = zeroValue
    72  	condition = iterator.Cond(&value)
    73  	if !condition {
    74  		t.Error("CondCond condition false")
    75  	}
    76  	if value != value2 {
    77  		t.Errorf("CondCond value %d exp %d", value, value2)
    78  	}
    79  
    80  	// CondCondCond should return no value
    81  	// request IsSame value twice should:
    82  	//	- retrieve the first value and return it
    83  	//	- then return the same value again
    84  	// Cond should return true and update value
    85  	reset()
    86  	condition = iterator.Cond(&value)
    87  	_ = condition
    88  	condition = iterator.Cond(&value)
    89  	_ = condition
    90  	value = zeroValue
    91  	condition = iterator.Cond(&value)
    92  	if condition {
    93  		t.Error("CondCondCond condition true")
    94  	}
    95  	if value != zeroValue {
    96  		t.Errorf("CondCondCond value %d exp %d", value, zeroValue)
    97  	}
    98  
    99  	// NextNext should return second value
   100  	reset()
   101  	value, hasValue = iterator.Next()
   102  	_ = value
   103  	_ = hasValue
   104  	value, hasValue = iterator.Next()
   105  	if !hasValue {
   106  		t.Error("NextNext hasValue false")
   107  	}
   108  	if value != value2 {
   109  		t.Errorf("NextNext value %d exp %d", value, value2)
   110  	}
   111  
   112  	// NextNextNext should return no value
   113  	reset()
   114  	value, hasValue = iterator.Next()
   115  	_ = value
   116  	_ = hasValue
   117  	value, hasValue = iterator.Next()
   118  	_ = value
   119  	_ = hasValue
   120  	value, hasValue = iterator.Next()
   121  	if hasValue {
   122  		t.Error("NextNextNext hasValue true")
   123  	}
   124  	if value != zeroValue {
   125  		t.Errorf("NextNextNext value %d exp %d", value, zeroValue)
   126  	}
   127  
   128  	// CancelCond should return false
   129  	reset()
   130  	err = iterator.Cancel()
   131  	_ = err
   132  	condition = iterator.Cond(&value)
   133  	if condition {
   134  		t.Error("CancelCond condition true")
   135  	}
   136  
   137  	// CancelNext should return no value
   138  	reset()
   139  	err = iterator.Cancel()
   140  	_ = err
   141  	value, hasValue = iterator.Next()
   142  	if hasValue {
   143  		t.Error("CancelNext hasValue true")
   144  	}
   145  	if value != zeroValue {
   146  		t.Errorf("CancelNext value %d exp %d", value, zeroValue)
   147  	}
   148  }
   149  
   150  func TestIntegerDec(t *testing.T) {
   151  	var value1, value2 = 2, 1
   152  
   153  	var err error
   154  	var value, zeroValue, iterationVariable int
   155  	var hasValue, condition bool
   156  	var actIterator Iterator[int]
   157  
   158  	var iterator Iterator[int]
   159  	var reset = func() {
   160  		iterator = NewIntegerIterator(value1, value2)
   161  	}
   162  	// Init() Cond() Next() Same() Cancel()
   163  	var _ Slice[int]
   164  
   165  	// Init should return zero value and iterator
   166  	reset()
   167  	iterationVariable, actIterator = iterator.Init()
   168  	if iterationVariable != zeroValue {
   169  		t.Errorf("Init iterationVariable %d exp %d", iterationVariable, zeroValue)
   170  	}
   171  	if actIterator != iterator {
   172  		t.Error("Init iterator bad")
   173  	}
   174  
   175  	// Cond should return true and update value
   176  	reset()
   177  	value = zeroValue
   178  	condition = iterator.Cond(&value)
   179  	if !condition {
   180  		t.Error("Cond condition false")
   181  	}
   182  	if value != value1 {
   183  		t.Errorf("Cond value %d exp %d", value, value1)
   184  	}
   185  
   186  	// Next should return first value
   187  	reset()
   188  	value, hasValue = iterator.Next()
   189  	if !hasValue {
   190  		t.Error("Next hasValue false")
   191  	}
   192  	if value != value1 {
   193  		t.Errorf("Next value %d exp %d", value, value1)
   194  	}
   195  
   196  	// Cancel should return no error
   197  	reset()
   198  	err = iterator.Cancel()
   199  	if err != nil {
   200  		t.Errorf("Cancel err: %s", perrors.Short(err))
   201  	}
   202  
   203  	// CondCond should return second value
   204  	reset()
   205  	condition = iterator.Cond(&value)
   206  	_ = condition
   207  	value = zeroValue
   208  	condition = iterator.Cond(&value)
   209  	if !condition {
   210  		t.Error("CondCond condition false")
   211  	}
   212  	if value != value2 {
   213  		t.Errorf("CondCond value %d exp %d", value, value2)
   214  	}
   215  
   216  	// CondCondCond should return no value
   217  	// request IsSame value twice should:
   218  	//	- retrieve the first value and return it
   219  	//	- then return the same value again
   220  	// Cond should return true and update value
   221  	reset()
   222  	condition = iterator.Cond(&value)
   223  	_ = condition
   224  	condition = iterator.Cond(&value)
   225  	_ = condition
   226  	value = zeroValue
   227  	condition = iterator.Cond(&value)
   228  	if condition {
   229  		t.Error("CondCondCond condition true")
   230  	}
   231  	if value != zeroValue {
   232  		t.Errorf("CondCondCond value %d exp %d", value, zeroValue)
   233  	}
   234  
   235  	// NextNext should return second value
   236  	reset()
   237  	value, hasValue = iterator.Next()
   238  	_ = value
   239  	_ = hasValue
   240  	value, hasValue = iterator.Next()
   241  	if !hasValue {
   242  		t.Error("NextNext hasValue false")
   243  	}
   244  	if value != value2 {
   245  		t.Errorf("NextNext value %d exp %d", value, value2)
   246  	}
   247  
   248  	// NextNextNext should return no value
   249  	reset()
   250  	value, hasValue = iterator.Next()
   251  	_ = value
   252  	_ = hasValue
   253  	value, hasValue = iterator.Next()
   254  	_ = value
   255  	_ = hasValue
   256  	value, hasValue = iterator.Next()
   257  	if hasValue {
   258  		t.Error("NextNextNext hasValue true")
   259  	}
   260  	if value != zeroValue {
   261  		t.Errorf("NextNextNext value %d exp %d", value, zeroValue)
   262  	}
   263  
   264  	// CancelCond should return false
   265  	reset()
   266  	err = iterator.Cancel()
   267  	_ = err
   268  	condition = iterator.Cond(&value)
   269  	if condition {
   270  		t.Error("CancelCond condition true")
   271  	}
   272  
   273  	// CancelNext should return no value
   274  	reset()
   275  	err = iterator.Cancel()
   276  	_ = err
   277  	value, hasValue = iterator.Next()
   278  	if hasValue {
   279  		t.Error("CancelNext hasValue true")
   280  	}
   281  	if value != zeroValue {
   282  		t.Errorf("CancelNext value %d exp %d", value, zeroValue)
   283  	}
   284  }