The solution function of the “Zebra Puzzle” exercise takes no parameters. So, it is supposed to return the same answer regardless of how many times you run. But the test code calls the solution function twice. For this particular exercise, this can be pretty expensive, depending on the efficiency of the solution.
Fortunately, it is super easy to have the test code call the solution function only once. Here is the revised code.
#include "zebra_puzzle.h"
#include "test-framework/unity.h"
void setUp(void)
{
}
void tearDown(void)
{
}
static solution_t solution;
static void test_who_drinks_water(void)
{
TEST_ASSERT_EQUAL_STRING("Norwegian", solution.drinks_water);
}
static void test_who_owns_the_zebra(void)
{
// TEST_IGNORE(); // delete this line to run test
TEST_ASSERT_EQUAL_STRING("Japanese", solution.owns_zebra);
}
int main(void)
{
UNITY_BEGIN();
solution = solve_puzzle();
RUN_TEST(test_who_drinks_water);
RUN_TEST(test_who_owns_the_zebra);
return UNITY_END();
}
Do you see any problems in this change?