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