go.charczuk.com@v0.0.0-20240327042549-bc490516bd1a/sdk/assert/its_epsilon.go (about) 1 /* 2 3 Copyright (c) 2024 - Present. Will Charczuk. All rights reserved. 4 Use of this source code is governed by a MIT license that can be found in the LICENSE file at the root of the repository. 5 6 */ 7 8 package assert 9 10 import ( 11 "testing" 12 ) 13 14 type IEpsilon interface { 15 ~int | ~float64 16 } 17 18 // ItsEpsilon asserts that two numbers are within an epsilon of each other. 19 func ItsEpsilon[T IEpsilon](t *testing.T, actual, expected, epsilon T, message ...any) { 20 t.Helper() 21 22 if itsEpsilon(actual, expected, epsilon) { 23 Fatalf(t, "expected ∆(%v, %v) to be less than %v", []any{actual, expected, epsilon}, message) 24 } 25 } 26 27 func itsEpsilon[T IEpsilon](actual, expected, epsilon T) bool { 28 var delta T 29 if actual >= expected { 30 delta = actual - expected 31 } else { 32 delta = expected - actual 33 } 34 if delta > epsilon { 35 return true 36 } 37 return false 38 }