-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Addresses issue #20 Co-authored-by: Galih Fajar <[email protected]>
- Loading branch information
1 parent
9e7a7a8
commit 1923952
Showing
2 changed files
with
82 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package govec | ||
|
||
import ( | ||
"math/rand" | ||
"time" | ||
|
||
"golang.org/x/exp/constraints" | ||
) | ||
|
||
// RandV2F returns a new random normalized V2F vector | ||
func RandV2F[T constraints.Float]() V2F[T] { | ||
r := rand.New(rand.NewSource(time.Now().UnixNano())) | ||
return V2F[T]{T(r.Float32()), T(r.Float32())}.Norm() | ||
} | ||
|
||
// RandV3F returns a new random normalized V3F vector | ||
func RandV3F[T constraints.Float]() V3F[T] { | ||
r := rand.New(rand.NewSource(time.Now().UnixNano())) | ||
return V3F[T]{T(r.Float32()), T(r.Float32()), T(r.Float32())}.Norm() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package govec | ||
|
||
import ( | ||
"math" | ||
"testing" | ||
) | ||
|
||
func Test_RandV2F(t *testing.T) { | ||
t.Run("generate 2 different random f32 vectors, shouldn't be equal", func(t *testing.T) { | ||
v1 := RandV2F[float32]() | ||
v2 := RandV2F[float32]() | ||
|
||
if v1.X == v2.X || v1.Y == v2.Y || v1.X == v1.Y || v2.X == v2.Y { | ||
t.Errorf("Expected [%f, %f] not equal to each other or with [%f, %f]", v1.X, v1.Y, v2.X, v2.Y) | ||
} | ||
}) | ||
|
||
t.Run("generate 2 different random f64 vectors, shouldn't be equal", func(t *testing.T) { | ||
v1 := RandV2F[float64]() | ||
v2 := RandV2F[float64]() | ||
|
||
if v1.X == v2.X || v1.Y == v2.Y || v1.X == v1.Y || v2.X == v2.Y { | ||
t.Errorf("Expected [%f, %f] not equal to each other or with [%f, %f]", v1.X, v1.Y, v2.X, v2.Y) | ||
} | ||
}) | ||
|
||
t.Run("randomized vectors should be normalized", func(t *testing.T) { | ||
v := RandV2F[float32]() | ||
|
||
if !almostEqual[float64](math.Sqrt(float64((v.X*v.X)+(v.Y*v.Y))), 1.0, 1e-5) { | ||
t.Errorf("Expected [%f, %f] to be normalized!", v.X, v.Y) | ||
} | ||
}) | ||
} | ||
|
||
func Test_RandV3F(t *testing.T) { | ||
t.Run("generate 2 different random f32 vectors, shouldn't be equal", func(t *testing.T) { | ||
v1 := RandV3F[float32]() | ||
v2 := RandV3F[float32]() | ||
|
||
if v1.X == v2.X || v1.Y == v2.Y || v1.Z == v2.Z || v1.X == v1.Y || v1.X == v1.Z || v1.Y == v1.Z || v2.X == v2.Y || v2.X == v2.Z || v2.Y == v2.Z { | ||
t.Errorf("Expected [%f, %f, %f] not equal to each other or with [%f, %f, %f]", v1.X, v1.Y, v1.Z, v2.X, v2.Y, v2.Z) | ||
} | ||
}) | ||
|
||
t.Run("generate 2 different random f64 vectors, shouldn't be equal", func(t *testing.T) { | ||
v1 := RandV3F[float64]() | ||
v2 := RandV3F[float64]() | ||
|
||
if v1.X == v2.X || v1.Y == v2.Y || v1.Z == v2.Z || v1.X == v1.Y || v1.X == v1.Z || v1.Y == v1.Z || v2.X == v2.Y || v2.X == v2.Z || v2.Y == v2.Z { | ||
t.Errorf("Expected [%f, %f, %f] not equal to each other or with [%f, %f, %f]", v1.X, v1.Y, v1.Z, v2.X, v2.Y, v2.Z) | ||
} | ||
}) | ||
|
||
t.Run("randomized vectors should be normalized", func(t *testing.T) { | ||
v := RandV3F[float32]() | ||
|
||
if !almostEqual[float64](math.Sqrt(float64((v.X*v.X)+(v.Y*v.Y)+(v.Z*v.Z))), 1.0, 1e-5) { | ||
t.Errorf("Expected [%f, %f, %f] to be normalized!", v.X, v.Y, v.Z) | ||
} | ||
}) | ||
} |