Difficulty |
3 |
Reading material |
1. Implementation
Implement the class
|
2. Writing Tests
The only way to check the correctness of your implementation is to write tests.
When writing down test cases, it is crucial to only mention that information that is necessary to fully specify the test. For example,
TEST_CASE("(0,0,0,0) + (0,0,0,0) == (0,0,0,0)")
{
Quaternion quat1(0,0,0,0);
Quaternion quat2(0,0,0,0);
Quaternion expected(0,0,0,0);
Quaternion actual = quat1 + quat2;
CHECK(actual == expected);
}
TEST_CASE("(1,0,0,0) + (0,0,0,0) == (1,0,0,0)")
{
Quaternion quat1(1,0,0,0);
Quaternion quat2(0,0,0,0);
Quaternion expected(1,0,0,0);
Quaternion actual = quat1 + quat2;
CHECK(actual == expected);
}
are badly written tests, as there is way too much duplication. Were the way quaternions are modelled to change, we’d have to rewrite each test.
It is therefore important to rely on helper functions or macros in order to distill the essence of each test. What is actually tested is that
and
Only this information should be mentioned in the tests:
TEST_ADDITION(Q(0,0,0,0), Q(0,0,0,0), Q(0,0,0,0))
TEST_ADDITION(Q(1,0,0,0), Q(0,0,0,0), Q(1,0,0,0))
This is a much more compact notation and expresses exactly what we want to test without mentioning any implementation details.
Of course, we still need to define TEST_ADDITION
:
#define Q(a, b, c, d) (a, b, c, d)
#define TEST_ADDITION(q1, q2, qe) \
TEST_CASE("Addition of "#q1 " and " #q2 " should give " #qe) \
{ \
Quaternion quat1 q1; \
Quaternion quat2 q2; \
Quaternion expected qe; \
Quaternion actual = quat1 + quat2; \
CHECK(actual == expected); \
}
If we were to modify the way we designed quaternions, we’d only have to update this macro.
3. Evaluation
Important
|
For people who already made this extension: you also need to define operators |
Write tests that check quaternion addition (both
|
Write tests that check quaternion subtraction (both
|
Write tests that check multiplication between quaternions and reals (both
|
Write tests that check multiplication between quaternions (only
As a reminder, here’s
\[ \begin{array}{r|ccc}
\times & i & j & k \\
\hline
i & -1 & k & -j \\
j & -k & -1 & i \\
k & j & -i & -1 \\
\end{array}\]
A few example combinations are
|
Write tests that check rotation. Test all the following combinations:
This should total 27 tests. You can rely on the following macros, together with some test cases:
|