-
Notifications
You must be signed in to change notification settings - Fork 63
/
Copy pathvecn_test.go
147 lines (106 loc) · 3.23 KB
/
vecn_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
// This file is generated from mgl32/vecn_test.go; DO NOT EDIT
// Copyright 2014 The go-gl Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package mgl64
import (
"math"
"testing"
)
func TestVecNCross(t *testing.T) {
v1 := Vec3{1, 3, 5}
v2 := Vec3{2, 4, 6}
correct := v1.Cross(v2)
correctN := NewVecNFromData(correct[:])
v1n := NewVecNFromData(v1[:])
v2n := NewVecNFromData(v2[:])
result := v1n.Cross(nil, v2n)
if !correctN.ApproxEqualThreshold(result, 1e-4) {
t.Errorf("VecN cross product is incorrect. Got: %v; Expected: %v", result, correctN)
}
}
func TestVecNDot(t *testing.T) {
v1 := Vec3{1, 3, 5}
v2 := Vec3{2, 4, 6}
correct := v1.Dot(v2)
v1n := NewVecNFromData(v1[:])
v2n := NewVecNFromData(v2[:])
result := v1n.Dot(v2n)
if !FloatEqualThreshold(correct, result, 1e-4) {
t.Errorf("Dot product doesn't work for VecN. Got: %v, Expected: %v", result, correct)
}
}
func TestVecNMul(t *testing.T) {
v1 := Vec3{1, 3, 5}
correct := v1.Mul(3)
correctN := NewVecNFromData(correct[:])
v1n := NewVecNFromData(v1[:])
result := v1n.Mul(nil, 3)
if !correctN.ApproxEqualThreshold(result, 1e-4) {
t.Errorf("VecN scalar multiplication is incorrect. Got: %v; Expected: %v", result, correctN)
}
}
func TestVecNNormalize(t *testing.T) {
v1 := Vec3{1, 3, 5}
correct := v1.Normalize()
correctN := NewVecNFromData(correct[:])
v1n := NewVecNFromData(v1[:])
result := v1n.Normalize(nil)
if !correctN.ApproxEqualThreshold(result, 1e-4) {
t.Errorf("VecN normalization is incorrect. Got: %v; Expected: %v", result, correctN)
}
}
func TestVecNAdd(t *testing.T) {
v1 := Vec3{1, 3, 5}
v2 := Vec3{2, 4, 6}
correct := v1.Add(v2)
correctN := NewVecNFromData(correct[:])
v1n := NewVecNFromData(v1[:])
v2n := NewVecNFromData(v2[:])
result := v1n.Add(nil, v2n)
if !correctN.ApproxEqualThreshold(result, 1e-4) {
t.Errorf("VecN addition is incorrect. Got: %v; Expected: %v", result, correctN)
}
}
func TestVecNSub(t *testing.T) {
v1 := Vec3{1, 3, 5}
v2 := Vec3{2, 4, 6}
correct := v1.Sub(v2)
correctN := NewVecNFromData(correct[:])
v1n := NewVecNFromData(v1[:])
v2n := NewVecNFromData(v2[:])
result := v1n.Sub(nil, v2n)
if !correctN.ApproxEqualThreshold(result, 1e-4) {
t.Errorf("VecN subtraction is incorrect. Got: %v; Expected: %v", result, correctN)
}
}
func TestVecNOuterProd(t *testing.T) {
v1 := Vec3{1, 2, 3}
v2 := Vec2{10, 11}
v1n := NewVecNFromData(v1[:])
v2n := NewVecNFromData(v2[:])
correct := v1.OuterProd2(v2)
correctN := NewMatrixFromData(correct[:], 3, 2)
result := v1n.OuterProd(nil, v2n)
if !correctN.ApproxEqualThreshold(result, 1e-4) {
t.Errorf("VecN outer product is incorrect. Got: %v; Expected: %v", result, correctN)
}
}
func TestVecNLenSqr(t *testing.T) {
v1 := Vec3{3, -5, 9}
v2 := Vec4{10, 11, 3, -7}
v1n := NewVecNFromData(v1[:])
v2n := NewVecNFromData(v2[:])
tests := map[*VecN]float64{
nil: math.NaN(),
NewVecN(0): 0,
v1n: 115,
v2n: 279,
}
for input, want := range tests {
result := input.LenSqr()
if float64(result) != want && (math.IsNaN(float64(result)) != math.IsNaN(want)) {
t.Errorf("VecN square length is incorred. Got: %v; Expected: %v", result, want)
}
}
}