TestProductionCode.c 2.3 KB

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