summaryrefslogtreecommitdiff
path: root/utilities
diff options
context:
space:
mode:
Diffstat (limited to 'utilities')
-rw-r--r--utilities/utilities.go23
-rw-r--r--utilities/utilities_test.go13
2 files changed, 30 insertions, 6 deletions
diff --git a/utilities/utilities.go b/utilities/utilities.go
index 5143da5..01e2cdd 100644
--- a/utilities/utilities.go
+++ b/utilities/utilities.go
@@ -24,23 +24,34 @@ import (
"strings"
"sync/atomic"
"time"
+
+ "golang.org/x/exp/constraints"
)
+func Iota(low int, high int) (made []int) {
+
+ made = make([]int, high-low)
+ for counter := low; counter < high; counter++ {
+ made[counter-low] = counter
+ }
+ return
+}
+
func IsInterfaceNil(ifc interface{}) bool {
return ifc == nil ||
(reflect.ValueOf(ifc).Kind() == reflect.Ptr && reflect.ValueOf(ifc).IsNil())
}
-func SignedPercentDifference(
- current float64,
- previous float64,
+func SignedPercentDifference[T constraints.Float | constraints.Integer](
+ current T,
+ previous T,
) (difference float64) {
//return ((current - previous) / (float64(current+previous) / 2.0)) * float64(
//100,
// )
- return ((current - previous) / previous) * float64(
- 100,
- )
+ fCurrent := float64(current)
+ fPrevious := float64(previous)
+ return ((fCurrent - fPrevious) / fPrevious) * 100.0
}
func AbsPercentDifference(
diff --git a/utilities/utilities_test.go b/utilities/utilities_test.go
index 107c2d2..3a84d76 100644
--- a/utilities/utilities_test.go
+++ b/utilities/utilities_test.go
@@ -14,11 +14,24 @@
package utilities
import (
+ "log"
"sync"
"testing"
"time"
)
+func TestIota(t *testing.T) {
+ r := Iota(6, 15)
+
+ l := 6
+ for _, vr := range r {
+ if vr != l {
+ log.Fatalf("Iota failed: expected %d, got %d\n", l, vr)
+ }
+ l++
+ }
+}
+
func TestReadAfterCloseOnBufferedChannel(t *testing.T) {
communication := make(chan int, 100)