1
2#include "ProductionCode.h"
3#include "unity.h"
4
5/* sometimes you may want to get at local data in a module.
6 * for example: If you plan to pass by reference, this could be useful
7 * however, it should often be avoided */
8extern int Counter;
9
10void setUp(void)
11{
12  /* This is run before EACH TEST */
13  Counter = 0x5a5a;
14}
15
16void tearDown(void)
17{
18}
19
20
21void test_FindFunction_WhichIsBroken_ShouldReturnZeroIfItemIsNotInList_WhichWorksEvenInOurBrokenCode(void)
22{
23  /* All of these should pass */
24  TEST_ASSERT_EQUAL(0, FindFunction_WhichIsBroken(78));
25  TEST_ASSERT_EQUAL(0, FindFunction_WhichIsBroken(2));
26  TEST_ASSERT_EQUAL(0, FindFunction_WhichIsBroken(33));
27  TEST_ASSERT_EQUAL(0, FindFunction_WhichIsBroken(999));
28  TEST_ASSERT_EQUAL(0, FindFunction_WhichIsBroken(-1));
29}
30
31void test_FindFunction_WhichIsBroken_ShouldReturnTheIndexForItemsInList_WhichWillFailBecauseOurFunctionUnderTestIsBroken(void)
32{
33  /* You should see this line fail in your test summary */
34  TEST_ASSERT_EQUAL(1, FindFunction_WhichIsBroken(34));
35
36  /* Notice the rest of these didn't get a chance to run because the line above failed.
37   * Unit tests abort each test function on the first sign of trouble.
38   * Then NEXT test function runs as normal. */
39  TEST_ASSERT_EQUAL(8, FindFunction_WhichIsBroken(8888));
40}
41
42void test_FunctionWhichReturnsLocalVariable_ShouldReturnTheCurrentCounterValue(void)
43{
44    /* This should be true because setUp set this up for us before this test */
45    TEST_ASSERT_EQUAL_HEX(0x5a5a, FunctionWhichReturnsLocalVariable());
46
47    /* This should be true because we can still change our answer */
48    Counter = 0x1234;
49    TEST_ASSERT_EQUAL_HEX(0x1234, FunctionWhichReturnsLocalVariable());
50}
51
52void test_FunctionWhichReturnsLocalVariable_ShouldReturnTheCurrentCounterValueAgain(void)
53{
54    /* This should be true again because setup was rerun before this test (and after we changed it to 0x1234) */
55    TEST_ASSERT_EQUAL_HEX(0x5a5a, FunctionWhichReturnsLocalVariable());
56}
57
58void test_FunctionWhichReturnsLocalVariable_ShouldReturnCurrentCounter_ButFailsBecauseThisTestIsActuallyFlawed(void)
59{
60    /* Sometimes you get the test wrong.  When that happens, you get a failure too... and a quick look should tell
61     * you what actually happened...which in this case was a failure to setup the initial condition. */
62    TEST_ASSERT_EQUAL_HEX(0x1234, FunctionWhichReturnsLocalVariable());
63}
64