135375f98Sopenharmony_ci
235375f98Sopenharmony_ci#include "ProductionCode.h"
335375f98Sopenharmony_ci#include "unity.h"
435375f98Sopenharmony_ci
535375f98Sopenharmony_ci/* sometimes you may want to get at local data in a module.
635375f98Sopenharmony_ci * for example: If you plan to pass by reference, this could be useful
735375f98Sopenharmony_ci * however, it should often be avoided */
835375f98Sopenharmony_ciextern int Counter;
935375f98Sopenharmony_ci
1035375f98Sopenharmony_civoid setUp(void)
1135375f98Sopenharmony_ci{
1235375f98Sopenharmony_ci  /* This is run before EACH TEST */
1335375f98Sopenharmony_ci  Counter = 0x5a5a;
1435375f98Sopenharmony_ci}
1535375f98Sopenharmony_ci
1635375f98Sopenharmony_civoid tearDown(void)
1735375f98Sopenharmony_ci{
1835375f98Sopenharmony_ci}
1935375f98Sopenharmony_ci
2035375f98Sopenharmony_ci
2135375f98Sopenharmony_civoid test_FindFunction_WhichIsBroken_ShouldReturnZeroIfItemIsNotInList_WhichWorksEvenInOurBrokenCode(void)
2235375f98Sopenharmony_ci{
2335375f98Sopenharmony_ci  /* All of these should pass */
2435375f98Sopenharmony_ci  TEST_ASSERT_EQUAL(0, FindFunction_WhichIsBroken(78));
2535375f98Sopenharmony_ci  TEST_ASSERT_EQUAL(0, FindFunction_WhichIsBroken(2));
2635375f98Sopenharmony_ci  TEST_ASSERT_EQUAL(0, FindFunction_WhichIsBroken(33));
2735375f98Sopenharmony_ci  TEST_ASSERT_EQUAL(0, FindFunction_WhichIsBroken(999));
2835375f98Sopenharmony_ci  TEST_ASSERT_EQUAL(0, FindFunction_WhichIsBroken(-1));
2935375f98Sopenharmony_ci}
3035375f98Sopenharmony_ci
3135375f98Sopenharmony_civoid test_FindFunction_WhichIsBroken_ShouldReturnTheIndexForItemsInList_WhichWillFailBecauseOurFunctionUnderTestIsBroken(void)
3235375f98Sopenharmony_ci{
3335375f98Sopenharmony_ci  /* You should see this line fail in your test summary */
3435375f98Sopenharmony_ci  TEST_ASSERT_EQUAL(1, FindFunction_WhichIsBroken(34));
3535375f98Sopenharmony_ci
3635375f98Sopenharmony_ci  /* Notice the rest of these didn't get a chance to run because the line above failed.
3735375f98Sopenharmony_ci   * Unit tests abort each test function on the first sign of trouble.
3835375f98Sopenharmony_ci   * Then NEXT test function runs as normal. */
3935375f98Sopenharmony_ci  TEST_ASSERT_EQUAL(8, FindFunction_WhichIsBroken(8888));
4035375f98Sopenharmony_ci}
4135375f98Sopenharmony_ci
4235375f98Sopenharmony_civoid test_FunctionWhichReturnsLocalVariable_ShouldReturnTheCurrentCounterValue(void)
4335375f98Sopenharmony_ci{
4435375f98Sopenharmony_ci    /* This should be true because setUp set this up for us before this test */
4535375f98Sopenharmony_ci    TEST_ASSERT_EQUAL_HEX(0x5a5a, FunctionWhichReturnsLocalVariable());
4635375f98Sopenharmony_ci
4735375f98Sopenharmony_ci    /* This should be true because we can still change our answer */
4835375f98Sopenharmony_ci    Counter = 0x1234;
4935375f98Sopenharmony_ci    TEST_ASSERT_EQUAL_HEX(0x1234, FunctionWhichReturnsLocalVariable());
5035375f98Sopenharmony_ci}
5135375f98Sopenharmony_ci
5235375f98Sopenharmony_civoid test_FunctionWhichReturnsLocalVariable_ShouldReturnTheCurrentCounterValueAgain(void)
5335375f98Sopenharmony_ci{
5435375f98Sopenharmony_ci    /* This should be true again because setup was rerun before this test (and after we changed it to 0x1234) */
5535375f98Sopenharmony_ci    TEST_ASSERT_EQUAL_HEX(0x5a5a, FunctionWhichReturnsLocalVariable());
5635375f98Sopenharmony_ci}
5735375f98Sopenharmony_ci
5835375f98Sopenharmony_civoid test_FunctionWhichReturnsLocalVariable_ShouldReturnCurrentCounter_ButFailsBecauseThisTestIsActuallyFlawed(void)
5935375f98Sopenharmony_ci{
6035375f98Sopenharmony_ci    /* Sometimes you get the test wrong.  When that happens, you get a failure too... and a quick look should tell
6135375f98Sopenharmony_ci     * you what actually happened...which in this case was a failure to setup the initial condition. */
6235375f98Sopenharmony_ci    TEST_ASSERT_EQUAL_HEX(0x1234, FunctionWhichReturnsLocalVariable());
6335375f98Sopenharmony_ci}
64