3 using System.Collections.Generic;
5 namespace OpenTK.Tests.Math
8 /// Generates Quaternion test data.
10 public class QuaternionTestDataGenerator
13 /// Returns the single axis test cases.
14 /// 1. param: rotation in euler angles
15 /// 2. param: expected result of xyz-component of quaternion
16 /// 3. param: test name (Don't found a working way how to pass this to xUnit runner). I let it here for test documentation
18 /// <value>The single axis test cases.</value>
19 public static IEnumerable<object> SingleAxisTestCases()
21 yield return new object[] { new Vector3(1, 0, 0), Vector3.UnitX, "Rotate around x axis" };
22 yield return new object[] { new Vector3(0, 1, 0), Vector3.UnitY, "Rotate around y axis" };
23 yield return new object[] { new Vector3(0, 0, 1), Vector3.UnitZ, "Rotate around z axis" };
27 /// Returns the single ToAxisAngle test cases.
28 /// 1. param: Quaternion which a definied value of xyz-component.
29 /// 2. param: expected result of xyz-component of quaternion
30 /// 3. param: test name (Don't found a working way how to pass this to xUnit runner). I let it here for test documentation
32 /// <value>The single axis test cases.</value>
33 public static IEnumerable<object[]> ToAxisAngleTestCases()
35 yield return new object[] { new Quaternion(Vector3.UnitX, 0), Vector3.UnitX, "Rotate around x axis" };
36 yield return new object[] { new Quaternion(Vector3.UnitY, 0), Vector3.UnitY, "Rotate around y axis" };
37 yield return new object[] { new Quaternion(Vector3.UnitZ, 0), Vector3.UnitZ, "Rotate around z axis" };
42 /// Provides some methods which helps to verify test results
44 internal static class QuaternionTestHelper
47 /// Verifies the direction of an given <see cref="Vector3"/>.
49 /// <returns>false: When <paramref name="toTest"/> does contain xyz values, when it should be 0,
50 /// or does not contain 0 when it should be</returns>
51 /// <param name="toTest">To test</param>
52 /// <param name="expected">Expected directions. Values getting only 0 checked</param>
53 public static bool VerifyEuqalSignleDirection(Vector3 toTest, Vector3 expected)
55 //To verify the direction of an vector, just respect the 0 values and check against these.
56 //The length of the vectors are ignored.
94 public class Quaternion_Tests
97 /// Checks if a single given value (either pitch, yaw or roll) get converted into correct x,y,z value of quaternion.
99 /// <param name="eulerValues">euler angle values</param>
100 /// <param name="expectedResult">expected xyz component of quaternion</param>
101 /// <param name="testName">Taken from nUnit test data. Don't know how to name data driven tests for xUnit which actually works.</param>
103 [MemberData(nameof(QuaternionTestDataGenerator.SingleAxisTestCases), MemberType = typeof(QuaternionTestDataGenerator))]
104 public void CtorEulerAnglesFloat_SingleEulerAngleSet_RotateCorrectAxis(Vector3 eulerValues, Vector3 expectedResult, String testName)
106 //Arrange + Act: Create Quaternion with "pitch/yaw/roll"
107 var cut = new Quaternion(eulerValues.X, eulerValues.Y, eulerValues.Z);
109 //Assert: Use helper, to check if part of the two correct axis is zero. I just want check the direction
110 Vector3 resultXYZ = cut.Xyz;
112 Assert.True(QuaternionTestHelper.VerifyEuqalSignleDirection(resultXYZ, expectedResult));
116 /// Checks if a single given value (either pitch, yaw or roll) get converted into correct x,y,z value of quaternion.
118 /// <param name="eulerValues">euler angle values</param>
119 /// <param name="expectedResult">expected xyz component of quaternion</param>
120 /// <param name="testName">Taken from nUnit test data. Don't know how to name data driven tests for xUnit which actually works.</param>
122 [MemberData(nameof(QuaternionTestDataGenerator.SingleAxisTestCases), MemberType = typeof(QuaternionTestDataGenerator))]
123 public void CtorEulerAnglesVector3_SingleEulerAngleSet_RotateCorrectAxis(Vector3 eulerValues, Vector3 expectedResult, String testName)
125 //Arrange + Act: Create Quaternion with "pitch/yaw/roll"
126 var cut = new Quaternion(eulerValues.X, eulerValues.Y, eulerValues.Z);
128 //Assert: Use helper, to check if part of the two correct axis is zero. I just want check the direction
129 Vector3 resultXYZ = cut.Xyz;
131 Assert.True(QuaternionTestHelper.VerifyEuqalSignleDirection(resultXYZ, expectedResult));
135 /// Checks if a single given value (either pitch, yaw or roll) get converted into correct x,y,z value of quaternion.
137 /// <param name="eulerValues">euler angle values</param>
138 /// <param name="expectedResult">expected xyz component of quaternion</param>
139 /// <param name="testName">Taken from nUnit test data. Don't know how to name data driven tests for xUnit which actually works.</param>
141 [MemberData(nameof(QuaternionTestDataGenerator.SingleAxisTestCases), MemberType = typeof(QuaternionTestDataGenerator))]
142 public void FromEulerAnglesFloat_SingleEulerAngleSet_RotateCorrectAxis(Vector3 eulerValues, Vector3 expectedResult, String testName)
144 //Arrange + Act: Create Quaternion with "pitch/yaw/roll"
145 var cut = Quaternion.FromEulerAngles(eulerValues.X, eulerValues.Y, eulerValues.Z);
147 //Assert: Use helper, to check if part of the two correct axis is zero. I just want check the direction
148 Vector3 resultXYZ = cut.Xyz;
150 Assert.True(QuaternionTestHelper.VerifyEuqalSignleDirection(resultXYZ, expectedResult));
154 /// Checks if a single given value (either pitch, yaw or roll) get converted into correct x,y,z value of quaternion.
156 /// <param name="eulerValues">euler angle values</param>
157 /// <param name="expectedResult">expected xyz component of quaternion</param>
158 /// <param name="testName">Taken from nUnit test data. Don't know how to name data driven tests for xUnit which actually works.</param>
160 [MemberData(nameof(QuaternionTestDataGenerator.SingleAxisTestCases), MemberType = typeof(QuaternionTestDataGenerator))]
161 public void FromEulerAnglesVector3_SingleEulerAngleSet_RotateCorrectAxis(Vector3 eulerValues, Vector3 expectedResult, String testName)
163 //Arrange + Act: Create Quaternion with "pitch/yaw/roll"
164 var cut = Quaternion.FromEulerAngles(eulerValues);
166 //Assert: Use helper, to check if part of the two correct axis is zero. I just want check the direction
167 Vector3 resultXYZ = cut.Xyz;
169 Assert.True(QuaternionTestHelper.VerifyEuqalSignleDirection(resultXYZ, expectedResult));
173 /// Checks if a single given value (either pitch, yaw or roll) get converted into correct x,y,z value of quaternion.
175 /// <param name="eulerValues">euler angle values</param>
176 /// <param name="expectedResult">expected xyz component of quaternion</param>
177 /// <param name="testName">Taken from nUnit test data. Don't know how to name data driven tests for xUnit which actually works.</param>
179 [MemberData(nameof(QuaternionTestDataGenerator.SingleAxisTestCases), MemberType = typeof(QuaternionTestDataGenerator))]
180 public void FromEulerAnglesOut_SingleEulerAngleSet_RotateCorrectAxis(Vector3 eulerValues, Vector3 expectedResult, String testName)
182 //Arrange + Act: Create Quaternion with "pitch/yaw/roll"
183 var cut = Quaternion.Identity;
184 Quaternion.FromEulerAngles(ref eulerValues, out cut);
186 //Assert: Use helper, to check if part of the two correct axis is zero. I just want check the direction
187 Vector3 resultXYZ = cut.Xyz;
189 Assert.True(QuaternionTestHelper.VerifyEuqalSignleDirection(resultXYZ, expectedResult));
193 /// Check if a quaternion returns a a rotation about the correct coordinate axis
195 /// <param name="cut">Prepared Quaternion</param>
196 /// <param name="expectedResult">Expected result.</param>
197 /// <param name="testName">Taken from nUnit test data. Don't know how to name data driven tests for xUnit which actually works.</param>
199 [MemberData(nameof(QuaternionTestDataGenerator.ToAxisAngleTestCases), MemberType = typeof(QuaternionTestDataGenerator))]
200 public void ToAxisAngle_SingleAxisSetAndAngleIgnored_RotateCorrectAxis(Quaternion cut, Vector3 expectedResult, String testName)
202 //Arrange + Act: Create Quaternion with rotation about X/Y/Z axis
205 cut.ToAxisAngle(out resultXYZ, out dontCare);
207 //Assert: Use helper, to check if part of the two correct axis is zero. I just want check the direction
208 Assert.True(QuaternionTestHelper.VerifyEuqalSignleDirection(resultXYZ, expectedResult));
211 //TODO: Make also checks with rotation angle