TestProductionCode.c 2.3 KB
Newer Older
W
Warwick Stone 已提交
1 2 3 4

#include "ProductionCode.h"
#include "unity.h"

5 6 7
/* sometimes you may want to get at local data in a module.
 * for example: If you plan to pass by reference, this could be useful
 * however, it should often be avoided */
T
teaguecl 已提交
8
extern int Counter;
W
Warwick Stone 已提交
9 10 11

void setUp(void)
{
12
  /* This is run before EACH TEST */
W
Warwick Stone 已提交
13 14 15 16 17 18 19 20 21
  Counter = 0x5a5a;
}

void tearDown(void)
{
}

void test_FindFunction_WhichIsBroken_ShouldReturnZeroIfItemIsNotInList_WhichWorksEvenInOurBrokenCode(void)
{
22
  /* All of these should pass */
W
Warwick Stone 已提交
23
  TEST_ASSERT_EQUAL(0, FindFunction_WhichIsBroken(78));
T
teaguecl 已提交
24
  TEST_ASSERT_EQUAL(0, FindFunction_WhichIsBroken(2));
W
Warwick Stone 已提交
25 26 27 28 29 30 31
  TEST_ASSERT_EQUAL(0, FindFunction_WhichIsBroken(33));
  TEST_ASSERT_EQUAL(0, FindFunction_WhichIsBroken(999));
  TEST_ASSERT_EQUAL(0, FindFunction_WhichIsBroken(-1));
}

void test_FindFunction_WhichIsBroken_ShouldReturnTheIndexForItemsInList_WhichWillFailBecauseOurFunctionUnderTestIsBroken(void)
{
32
  /* You should see this line fail in your test summary */
W
Warwick Stone 已提交
33
  TEST_ASSERT_EQUAL(1, FindFunction_WhichIsBroken(34));
T
teaguecl 已提交
34 35 36

  /* Notice the rest of these didn't get a chance to run because the line above failed.
   * Unit tests abort each test function on the first sign of trouble.
37
   * Then NEXT test function runs as normal. */
W
Warwick Stone 已提交
38 39 40 41 42
  TEST_ASSERT_EQUAL(8, FindFunction_WhichIsBroken(8888));
}

void test_FunctionWhichReturnsLocalVariable_ShouldReturnTheCurrentCounterValue(void)
{
43
    /* This should be true because setUp set this up for us before this test */
W
Warwick Stone 已提交
44
    TEST_ASSERT_EQUAL_HEX(0x5a5a, FunctionWhichReturnsLocalVariable());
T
teaguecl 已提交
45

46
    /* This should be true because we can still change our answer */
W
Warwick Stone 已提交
47 48 49 50 51 52
    Counter = 0x1234;
    TEST_ASSERT_EQUAL_HEX(0x1234, FunctionWhichReturnsLocalVariable());
}

void test_FunctionWhichReturnsLocalVariable_ShouldReturnTheCurrentCounterValueAgain(void)
{
53
    /* This should be true again because setup was rerun before this test (and after we changed it to 0x1234) */
W
Warwick Stone 已提交
54 55 56 57 58
    TEST_ASSERT_EQUAL_HEX(0x5a5a, FunctionWhichReturnsLocalVariable());
}

void 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
     * you what actually happened...which in this case was a failure to setup the initial condition. */
W
Warwick Stone 已提交
61 62
    TEST_ASSERT_EQUAL_HEX(0x1234, FunctionWhichReturnsLocalVariable());
}