github.com/haraldrudell/parl@v0.4.176/atomic-max_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 parl
     7  
     8  import (
     9  	"testing"
    10  )
    11  
    12  func TestAtomicMax(t *testing.T) {
    13  	var threshold1, value1, value2 = 1, 1, 2
    14  
    15  	var value, zeroValue int
    16  	var hasValue, isNewMax, isPanic bool
    17  	var err error
    18  
    19  	var a AtomicMax[int]
    20  	_ = 1
    21  
    22  	// a new should have no value
    23  	a = AtomicMax[int]{}
    24  	value, hasValue = a.Max()
    25  	if value != zeroValue {
    26  		t.Errorf("new value %d exp %d", value, zeroValue)
    27  	}
    28  	if hasValue {
    29  		t.Error("new hasValue true")
    30  	}
    31  
    32  	// Value below threshold should not be a max
    33  	a = *NewAtomicMax(threshold1)
    34  	isNewMax = a.Value(zeroValue)
    35  	if isNewMax {
    36  		t.Error("below isNewMax")
    37  	}
    38  	value, hasValue = a.Max()
    39  	if value != zeroValue {
    40  		t.Errorf("below value %d exp %d", value, zeroValue)
    41  	}
    42  	if hasValue {
    43  		t.Error("below hasValue true")
    44  	}
    45  
    46  	// zero should be max
    47  	a = AtomicMax[int]{}
    48  	isNewMax = a.Value(zeroValue)
    49  	if !isNewMax {
    50  		t.Error("zero isNewMax false")
    51  	}
    52  	value, hasValue = a.Max()
    53  	if value != zeroValue {
    54  		t.Errorf("zero value %d exp %d", value, zeroValue)
    55  	}
    56  	if !hasValue {
    57  		t.Error("zero hasValue false")
    58  	}
    59  
    60  	// zero-zero should not be max
    61  	a = AtomicMax[int]{}
    62  	isNewMax = a.Value(zeroValue)
    63  	_ = isNewMax
    64  	isNewMax = a.Value(zeroValue)
    65  	if isNewMax {
    66  		t.Error("zero isNewMax false")
    67  	}
    68  
    69  	// equal to threshold should be max
    70  	a = *NewAtomicMax(threshold1)
    71  	isNewMax = a.Value(value1)
    72  	if !isNewMax {
    73  		t.Error("equal isNewMax false")
    74  	}
    75  	value, hasValue = a.Max()
    76  	if value != value1 {
    77  		t.Errorf("equal value %d exp %d", value, value1)
    78  	}
    79  	if !hasValue {
    80  		t.Error("equal hasValue false")
    81  	}
    82  
    83  	// smaller value should not be max
    84  	a = AtomicMax[int]{}
    85  	isNewMax = a.Value(value2)
    86  	_ = isNewMax
    87  	isNewMax = a.Value(value1)
    88  	if isNewMax {
    89  		t.Error("smaller isNewMax")
    90  	}
    91  	value, hasValue = a.Max()
    92  	if value != value2 {
    93  		t.Errorf("smaller value %d exp %d", value, value2)
    94  	}
    95  	if !hasValue {
    96  		t.Error("smaller hasValue false")
    97  	}
    98  
    99  	// max1 should work
   100  	a = AtomicMax[int]{}
   101  	isNewMax = a.Value(value1)
   102  	_ = isNewMax
   103  	value = a.Max1()
   104  	if value != value1 {
   105  		t.Errorf("Max1 value %d exp %d", value, value1)
   106  	}
   107  
   108  	// negative threshold should panic
   109  	isPanic, err = invokeNewAtomicMax()
   110  	if !isPanic {
   111  		t.Error("negative threshold no panic")
   112  	}
   113  	if err == nil {
   114  		t.Error("negative threshold no error")
   115  	}
   116  
   117  	// negative value should panic
   118  	isPanic, err = invokeAtomicMaxValue()
   119  	if !isPanic {
   120  		t.Error("negative value no panic")
   121  	}
   122  	if err == nil {
   123  		t.Error("negative value no error")
   124  	}
   125  }
   126  
   127  func invokeNewAtomicMax() (isPanic bool, err error) {
   128  	defer PanicToErr(&err, &isPanic)
   129  
   130  	NewAtomicMax(-1)
   131  
   132  	return
   133  }
   134  
   135  func invokeAtomicMaxValue() (isPanic bool, err error) {
   136  	defer PanicToErr(&err, &isPanic)
   137  
   138  	NewAtomicMax(0).Value(-1)
   139  
   140  	return
   141  }