diff options
Diffstat (limited to 'utilities')
| -rw-r--r-- | utilities/utilities.go | 18 | ||||
| -rw-r--r-- | utilities/utilities_test.go | 10 |
2 files changed, 22 insertions, 6 deletions
diff --git a/utilities/utilities.go b/utilities/utilities.go index e75d373..ff04023 100644 --- a/utilities/utilities.go +++ b/utilities/utilities.go @@ -30,13 +30,10 @@ import ( "golang.org/x/exp/constraints" ) -var ( - // GitVersion is the Git revision hash - GitVersion = "dev" -) +// GitVersion is the Git revision hash +var GitVersion = "dev" func Iota(low int, high int) (made []int) { - made = make([]int, high-low) for counter := low; counter < high; counter++ { made[counter-low] = counter @@ -67,7 +64,7 @@ func AbsPercentDifference( ) } -func Conditional(condition bool, t string, f string) string { +func Conditional[T any](condition bool, t T, f T) T { if condition { return t } @@ -229,3 +226,12 @@ func ContextSignaler(ctxt context.Context, st time.Duration, condition *func() b return } } + +type Pair[T any] struct { + First T + Second T +} + +func PerSecondToInterval(rate int64) time.Duration { + return time.Duration(time.Second.Nanoseconds() / rate) +} diff --git a/utilities/utilities_test.go b/utilities/utilities_test.go index aa66f6b..9cd4ef0 100644 --- a/utilities/utilities_test.go +++ b/utilities/utilities_test.go @@ -116,3 +116,13 @@ func TestWaitWithContext(t *testing.T) { wg.Wait() } + +func TestPerSecondToInterval(t *testing.T) { + if time.Second != PerSecondToInterval(1) { + t.Fatalf("A number of nanoseconds is not equal to a second!") + } + + if time.Second/2 != PerSecondToInterval(2) { + t.Fatalf("Something that happens twice per second should happen every 5000ns.") + } +} |
