Skip to content

Commit 0706ebc

Browse files
committed
added unittest for b2rot
1 parent a14eebc commit 0706ebc

File tree

3 files changed

+229
-4
lines changed

3 files changed

+229
-4
lines changed

src/Box2D.NET/B2Vec2.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,14 +67,14 @@ public B2Vec2(float x, float y)
6767
[MethodImpl(MethodImplOptions.AggressiveInlining)]
6868
public static bool operator ==(B2Vec2 a, B2Vec2 b)
6969
{
70-
return a.X == b.X && a.Y == b.Y;
70+
return a.X.Equals(b.X) && a.Y.Equals(b.Y);
7171
}
7272

7373
/// Binary vector inequality
7474
[MethodImpl(MethodImplOptions.AggressiveInlining)]
7575
public static bool operator !=(B2Vec2 a, B2Vec2 b)
7676
{
77-
return a.X != b.X || a.Y != b.Y;
77+
return !(a == b);
7878
}
7979
}
8080
}

test/Box2D.NET.Test/B2RotTest.cs

Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
using Box2D.NET;
2+
using NUnit.Framework;
3+
using static Box2D.NET.B2MathFunction;
4+
using System;
5+
6+
namespace Box2D.NET.Test;
7+
8+
public class B2RotTest
9+
{
10+
[Test]
11+
public void Test_B2Rot_Constructor()
12+
{
13+
var rot = b2MakeRot(B2_PI / 4);
14+
Assert.That(rot.c, Is.EqualTo(MathF.Cos(B2_PI / 4)).Within(FLT_EPSILON), "Cosine of 45 degrees should match MathF.Cos");
15+
Assert.That(rot.s, Is.EqualTo(MathF.Sin(B2_PI / 4)).Within(FLT_EPSILON), "Sine of 45 degrees should match MathF.Sin");
16+
17+
var zeroRot = b2MakeRot(0.0f);
18+
Assert.That(zeroRot.c, Is.EqualTo(1.0f).Within(FLT_EPSILON), "Cosine of 0 degrees should be 1");
19+
Assert.That(zeroRot.s, Is.EqualTo(0.0f).Within(FLT_EPSILON), "Sine of 0 degrees should be 0");
20+
}
21+
22+
[Test]
23+
public void Test_B2Rot_Normalization()
24+
{
25+
var rot = new B2Rot(2.0f, 0.0f);
26+
var norm = b2NormalizeRot(rot);
27+
Assert.That(norm.c, Is.EqualTo(1.0f).Within(FLT_EPSILON), "Normalized cosine should be 1");
28+
Assert.That(norm.s, Is.EqualTo(0.0f).Within(FLT_EPSILON), "Normalized sine should be 0");
29+
Assert.That(norm.c * norm.c + norm.s * norm.s, Is.EqualTo(1.0f).Within(FLT_EPSILON), "Normalized rotation should have unit length");
30+
}
31+
32+
[Test]
33+
public void Test_B2Rot_VectorRotation()
34+
{
35+
var rot = b2MakeRot(B2_PI / 2);
36+
var vec = new B2Vec2(1.0f, 0.0f);
37+
var result = b2RotateVector(rot, vec);
38+
Assert.That(result.X, Is.EqualTo(0.0f).Within(FLT_EPSILON), "90-degree rotation should map (1,0) to (0,1)");
39+
Assert.That(result.Y, Is.EqualTo(1.0f).Within(FLT_EPSILON), "90-degree rotation should map (1,0) to (0,1)");
40+
}
41+
42+
[Test]
43+
public void Test_B2Rot_Integration()
44+
{
45+
var rot = b2MakeRot(0.0f);
46+
float deltaAngle = B2_PI / 2;
47+
var result = b2IntegrateRotation(rot, deltaAngle);
48+
float expectedMag = MathF.Sqrt(1.0f + (B2_PI/2) * (B2_PI/2));
49+
float expectedC = 1.0f / expectedMag;
50+
float expectedS = (B2_PI/2) / expectedMag;
51+
Assert.That(result.c, Is.EqualTo(expectedC).Within(0.0001f), "Integrated rotation cosine should match expected value");
52+
Assert.That(result.s, Is.EqualTo(expectedS).Within(0.0001f), "Integrated rotation sine should match expected value");
53+
Assert.That(result.c * result.c + result.s * result.s, Is.EqualTo(1.0f).Within(0.0001f), "Integrated rotation should remain normalized");
54+
}
55+
56+
[Test]
57+
public void Test_B2Rot_Multiplication()
58+
{
59+
var rot1 = b2MakeRot(B2_PI / 4);
60+
var rot2 = b2MakeRot(B2_PI / 4);
61+
var result = b2MulRot(rot1, rot2);
62+
Assert.That(result.c, Is.EqualTo(0.0f).Within(FLT_EPSILON), "Multiplying two 45-degree rotations should give 90 degrees");
63+
Assert.That(result.s, Is.EqualTo(1.0f).Within(FLT_EPSILON), "Multiplying two 45-degree rotations should give 90 degrees");
64+
}
65+
66+
[Test]
67+
public void Test_B2Rot_Validation()
68+
{
69+
var validRot = b2MakeRot(B2_PI / 4);
70+
Assert.That(b2IsValidRotation(validRot), Is.True, "Normalized rotation should be valid");
71+
72+
var invalidRot = new B2Rot(2.0f, 0.0f);
73+
Assert.That(b2IsValidRotation(invalidRot), Is.False, "Non-normalized rotation should be invalid");
74+
75+
var nanRot = new B2Rot(float.NaN, 0.0f);
76+
Assert.That(b2IsValidRotation(nanRot), Is.False, "Rotation with NaN should be invalid");
77+
}
78+
79+
[Test]
80+
public void Test_B2Rot_GetAngle()
81+
{
82+
var rot = b2MakeRot(B2_PI / 4);
83+
Assert.That(b2Rot_GetAngle(rot), Is.EqualTo(B2_PI / 4).Within(0.0001f), "GetAngle should return the original angle");
84+
}
85+
86+
[Test]
87+
public void Test_B2Rot_GetAxes()
88+
{
89+
var rot = b2MakeRot(B2_PI / 2);
90+
var xAxis = b2Rot_GetXAxis(rot);
91+
var yAxis = b2Rot_GetYAxis(rot);
92+
Assert.That(xAxis.X, Is.EqualTo(0.0f).Within(FLT_EPSILON), "90-degree rotation X-axis should point up");
93+
Assert.That(xAxis.Y, Is.EqualTo(1.0f).Within(FLT_EPSILON), "90-degree rotation X-axis should point up");
94+
Assert.That(yAxis.X, Is.EqualTo(-1.0f).Within(FLT_EPSILON), "90-degree rotation Y-axis should point left");
95+
Assert.That(yAxis.Y, Is.EqualTo(0.0f).Within(FLT_EPSILON), "90-degree rotation Y-axis should point left");
96+
}
97+
98+
[Test]
99+
public void Test_B2Rot_InvMulRot()
100+
{
101+
var rot1 = b2MakeRot(B2_PI / 4);
102+
var rot2 = b2MakeRot(B2_PI / 4);
103+
var result = b2InvMulRot(rot1, rot2);
104+
Assert.That(result.c, Is.EqualTo(1.0f).Within(FLT_EPSILON), "Inverse multiplication of same rotations should give identity");
105+
Assert.That(result.s, Is.EqualTo(0.0f).Within(FLT_EPSILON), "Inverse multiplication of same rotations should give identity");
106+
}
107+
108+
[Test]
109+
public void Test_B2Rot_RelativeAngle()
110+
{
111+
var rot1 = b2MakeRot(0.0f);
112+
var rot2 = b2MakeRot(B2_PI / 2);
113+
Assert.That(b2RelativeAngle(rot2, rot1), Is.EqualTo(B2_PI / 2).Within(FLT_EPSILON), "Relative angle between 0 and 90 degrees should be 90 degrees");
114+
}
115+
116+
[Test]
117+
public void Test_B2Rot_UnwindAngle()
118+
{
119+
Assert.That(b2UnwindAngle(3 * B2_PI / 2), Is.EqualTo(-B2_PI / 2).Within(FLT_EPSILON), "Angle greater than PI should be normalized to [-PI, PI]");
120+
Assert.That(b2UnwindAngle(-3 * B2_PI / 2), Is.EqualTo(B2_PI / 2).Within(FLT_EPSILON), "Angle less than -PI should be normalized to [-PI, PI]");
121+
Assert.That(b2UnwindAngle(B2_PI), Is.EqualTo(B2_PI).Within(FLT_EPSILON), "PI should remain unchanged");
122+
Assert.That(b2UnwindAngle(-B2_PI), Is.EqualTo(-B2_PI).Within(FLT_EPSILON), "-PI should remain unchanged");
123+
}
124+
125+
[Test]
126+
public void Test_B2Rot_UnwindLargeAngle()
127+
{
128+
Assert.That(b2UnwindLargeAngle(5 * B2_PI), Is.EqualTo(B2_PI).Within(0.0001f), "Large angle should be normalized to [-PI, PI]");
129+
Assert.That(b2UnwindLargeAngle(-5 * B2_PI), Is.EqualTo(-B2_PI).Within(0.0001f), "Large negative angle should be normalized to [-PI, PI]");
130+
}
131+
132+
[Test]
133+
public void Test_B2Rot_InvRotateVector()
134+
{
135+
var rot = b2MakeRot(B2_PI / 2);
136+
var vec = new B2Vec2(0.0f, 1.0f);
137+
var result = b2InvRotateVector(rot, vec);
138+
Assert.That(result.X, Is.EqualTo(1.0f).Within(FLT_EPSILON), "Inverse rotation should map (0,1) back to (1,0)");
139+
Assert.That(result.Y, Is.EqualTo(0.0f).Within(FLT_EPSILON), "Inverse rotation should map (0,1) back to (1,0)");
140+
}
141+
}

test/Box2D.NET.Test/B2Vec2Test.cs

Lines changed: 86 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
using Box2D.NET;
21
using NUnit.Framework;
32

43
namespace Box2D.NET.Test;
@@ -123,4 +122,89 @@ public void Test_B2Vec2_ScalarMultiplication()
123122
Assert.That(result4.X, Is.EqualTo(0.0f));
124123
Assert.That(result4.Y, Is.EqualTo(0.0f));
125124
}
126-
}
125+
126+
[Test]
127+
public void Test_B2Vec2_Equality()
128+
{
129+
// Case 1: Exact equality
130+
var vec1 = new B2Vec2(1.0f, 2.0f);
131+
var vec2 = new B2Vec2(1.0f, 2.0f);
132+
Assert.That(vec1 == vec2, Is.True);
133+
Assert.That(vec1 != vec2, Is.False);
134+
135+
// Case 2: Within epsilon
136+
var vec3 = new B2Vec2(1.0f + B2MathFunction.FLT_EPSILON * 0.5f, 2.0f);
137+
var vec4 = new B2Vec2(1.0f, 2.0f);
138+
Assert.That(vec3 == vec4, Is.True);
139+
Assert.That(vec3 != vec4, Is.False);
140+
141+
// Case 3: Beyond epsilon
142+
var vec5 = new B2Vec2(1.0f + B2MathFunction.FLT_EPSILON * 2.0f, 2.0f);
143+
var vec6 = new B2Vec2(1.0f, 2.0f);
144+
Assert.That(vec5 == vec6, Is.False);
145+
Assert.That(vec5 != vec6, Is.True);
146+
147+
// Case 4: NaN handling
148+
var vec7 = new B2Vec2(float.NaN, 2.0f);
149+
var vec8 = new B2Vec2(1.0f, 2.0f);
150+
Assert.That(vec7 == vec8, Is.False);
151+
Assert.That(vec7 != vec8, Is.True);
152+
153+
// Case 5: Infinity handling
154+
var vec9 = new B2Vec2(float.PositiveInfinity, 2.0f);
155+
var vec10 = new B2Vec2(float.PositiveInfinity, 2.0f);
156+
Assert.That(vec9 == vec10, Is.True); // Infinity == Infinity in C/C++
157+
Assert.That(vec9 != vec10, Is.False);
158+
159+
// Case 5.1: Different infinity signs
160+
var vec9_1 = new B2Vec2(float.PositiveInfinity, 2.0f);
161+
var vec10_1 = new B2Vec2(float.NegativeInfinity, 2.0f);
162+
Assert.That(vec9_1 == vec10_1, Is.False); // +Infinity != -Infinity
163+
Assert.That(vec9_1 != vec10_1, Is.True);
164+
165+
// Case 6: Zero comparison
166+
var vec11 = new B2Vec2(0.0f, 0.0f);
167+
var vec12 = new B2Vec2(0.0f, 0.0f);
168+
Assert.That(vec11 == vec12, Is.True);
169+
Assert.That(vec11 != vec12, Is.False);
170+
171+
// Case 7: Very small numbers
172+
var vec13 = new B2Vec2(float.Epsilon, float.Epsilon);
173+
var vec14 = new B2Vec2(0.0f, 0.0f);
174+
Assert.That(vec13 == vec14, Is.False);
175+
Assert.That(vec13 != vec14, Is.True);
176+
177+
// Case 8: Self comparison
178+
var vec15 = new B2Vec2(1.0f, 2.0f);
179+
Assert.That(vec15 == vec15, Is.True);
180+
Assert.That(vec15 != vec15, Is.False);
181+
}
182+
183+
[Test]
184+
public void Test_B2Vec2_Equality_EdgeCases()
185+
{
186+
// Case 1: Maximum float values
187+
var vec1 = new B2Vec2(float.MaxValue, float.MaxValue);
188+
var vec2 = new B2Vec2(float.MaxValue, float.MaxValue);
189+
Assert.That(vec1 == vec2, Is.True);
190+
Assert.That(vec1 != vec2, Is.False);
191+
192+
// Case 2: Minimum float values
193+
var vec3 = new B2Vec2(float.MinValue, float.MinValue);
194+
var vec4 = new B2Vec2(float.MinValue, float.MinValue);
195+
Assert.That(vec3 == vec4, Is.True);
196+
Assert.That(vec3 != vec4, Is.False);
197+
198+
// Case 3: Mixed signs
199+
var vec5 = new B2Vec2(-1.0f, 1.0f);
200+
var vec6 = new B2Vec2(-1.0f, 1.0f);
201+
Assert.That(vec5 == vec6, Is.True);
202+
Assert.That(vec5 != vec6, Is.False);
203+
204+
// Case 4: Very small numbers (using float.Epsilon)
205+
var vec7 = new B2Vec2(float.Epsilon, float.Epsilon);
206+
var vec8 = new B2Vec2(0.0f, 0.0f);
207+
Assert.That(vec7 == vec8, Is.False); // float.Epsilon은 0이 아님
208+
Assert.That(vec7 != vec8, Is.True);
209+
}
210+
}

0 commit comments

Comments
 (0)