]> git.ipfire.org Git - thirdparty/ntp.git/commitdiff
unity framework
authorHarlan Stenn <stenn@ntp.org>
Sun, 3 May 2015 07:11:22 +0000 (07:11 +0000)
committerHarlan Stenn <stenn@ntp.org>
Sun, 3 May 2015 07:11:22 +0000 (07:11 +0000)
bk: 5545ca1a9m_2V4rbeRSRbtUT0m8W9w

Makefile.am
sntp/Makefile.am
tests/unity-framework/ProductionCode.c [new file with mode: 0644]
tests/unity-framework/ProductionCode.h [new file with mode: 0644]
tests/unity-framework/TestProductionCode.c [new file with mode: 0644]
tests/unity-framework/TestProductionCode_Runner.c [new file with mode: 0644]

index ae96c0094137cc9622203f19d339f7f4ab59564f..60a68d5222b9340588df28b60355b742da408001 100644 (file)
@@ -53,6 +53,7 @@ EXTRA_DIST =                  \
        lib/isc                 \
        libjsmn                 \
        ports                   \
+       tests/unity-framework   \
        \
        deps-ver                \
        \
index 8694ed3e75ca754ea49f2071c20623ee5b865eb3..b2a234247edb9a161fd19b94081d220240576bea 100644 (file)
@@ -53,8 +53,8 @@ sbin_PROGRAMS =               @SNTP_DS@
 ## recursive make dist in sntp including libevent.
 ##
 
-SUBDIRS = include scripts
-DIST_SUBDIRS = include scripts
+SUBDIRS = include scripts unity
+DIST_SUBDIRS = include scripts unity
 
 if BUILD_LIBEVENT
 SUBDIRS += libevent
diff --git a/tests/unity-framework/ProductionCode.c b/tests/unity-framework/ProductionCode.c
new file mode 100644 (file)
index 0000000..500b44b
--- /dev/null
@@ -0,0 +1,24 @@
+
+#include "ProductionCode.h"
+
+int Counter = 0;
+int NumbersToFind[9] = { 0, 34, 55, 66, 32, 11, 1, 77, 888 }; //some obnoxious array to search that is 1-based indexing instead of 0.
+
+// This function is supposed to search through NumbersToFind and find a particular number.  
+// If it finds it, the index is returned.  Otherwise 0 is returned which sorta makes sense since 
+// NumbersToFind is indexed from 1.  Unfortunately it's broken 
+// (and should therefore be caught by our tests)
+int FindFunction_WhichIsBroken(int NumberToFind)
+{
+    int i = 0;
+    while (i <= 8) //Notice I should have been in braces
+        i++;
+        if (NumbersToFind[i] == NumberToFind) //Yikes!  I'm getting run after the loop finishes instead of during it!
+            return i;
+    return 0;
+}
+
+int FunctionWhichReturnsLocalVariable(void)
+{
+    return Counter;
+}
diff --git a/tests/unity-framework/ProductionCode.h b/tests/unity-framework/ProductionCode.h
new file mode 100644 (file)
index 0000000..250ca0d
--- /dev/null
@@ -0,0 +1,3 @@
+
+int FindFunction_WhichIsBroken(int NumberToFind);
+int FunctionWhichReturnsLocalVariable(void);
diff --git a/tests/unity-framework/TestProductionCode.c b/tests/unity-framework/TestProductionCode.c
new file mode 100644 (file)
index 0000000..28a5581
--- /dev/null
@@ -0,0 +1,62 @@
+
+#include "ProductionCode.h"
+#include "unity.h"
+
+//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
+extern int Counter; 
+
+void setUp(void)
+{
+  //This is run before EACH TEST
+  Counter = 0x5a5a;
+}
+
+void tearDown(void)
+{
+}
+
+void test_FindFunction_WhichIsBroken_ShouldReturnZeroIfItemIsNotInList_WhichWorksEvenInOurBrokenCode(void)
+{
+  //All of these should pass
+  TEST_ASSERT_EQUAL(0, FindFunction_WhichIsBroken(78));
+  TEST_ASSERT_EQUAL(0, FindFunction_WhichIsBroken(1));
+  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)
+{
+  // You should see this line fail in your test summary
+  TEST_ASSERT_EQUAL(1, FindFunction_WhichIsBroken(34));
+  
+  // 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. 
+  // Then NEXT test function runs as normal.
+  TEST_ASSERT_EQUAL(8, FindFunction_WhichIsBroken(8888));
+}
+
+void test_FunctionWhichReturnsLocalVariable_ShouldReturnTheCurrentCounterValue(void)
+{
+    //This should be true because setUp set this up for us before this test
+    TEST_ASSERT_EQUAL_HEX(0x5a5a, FunctionWhichReturnsLocalVariable());
+    
+    //This should be true because we can still change our answer
+    Counter = 0x1234;
+    TEST_ASSERT_EQUAL_HEX(0x1234, FunctionWhichReturnsLocalVariable());
+}
+
+void test_FunctionWhichReturnsLocalVariable_ShouldReturnTheCurrentCounterValueAgain(void)
+{
+    //This should be true again because setup was rerun before this test (and after we changed it to 0x1234)
+    TEST_ASSERT_EQUAL_HEX(0x5a5a, FunctionWhichReturnsLocalVariable());
+}
+
+void test_FunctionWhichReturnsLocalVariable_ShouldReturnCurrentCounter_ButFailsBecauseThisTestIsActuallyFlawed(void)
+{
+    //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.
+    TEST_ASSERT_EQUAL_HEX(0x1234, FunctionWhichReturnsLocalVariable());
+}
diff --git a/tests/unity-framework/TestProductionCode_Runner.c b/tests/unity-framework/TestProductionCode_Runner.c
new file mode 100644 (file)
index 0000000..0a795ec
--- /dev/null
@@ -0,0 +1,37 @@
+/* AUTOGENERATED FILE. DO NOT EDIT. */
+#include "unity.h"
+#include <setjmp.h>
+#include <stdio.h>
+
+char MessageBuffer[50];
+
+extern void setUp(void);
+extern void tearDown(void);
+
+extern void test_FindFunction_WhichIsBroken_ShouldReturnZeroIfItemIsNotInList_WhichWorksEvenInOurBrokenCode(void);
+extern void test_FindFunction_WhichIsBroken_ShouldReturnTheIndexForItemsInList_WhichWillFailBecauseOurFunctionUnderTestIsBroken(void);
+extern void test_FunctionWhichReturnsLocalVariable_ShouldReturnTheCurrentCounterValue(void);
+extern void test_FunctionWhichReturnsLocalVariable_ShouldReturnTheCurrentCounterValueAgain(void);
+extern void test_FunctionWhichReturnsLocalVariable_ShouldReturnCurrentCounter_ButFailsBecauseThisTestIsActuallyFlawed(void);
+
+void resetTest(void);
+void resetTest(void)
+{
+  tearDown();
+  setUp();
+}
+
+
+int main(void)
+{
+  UnityBegin("test/TestProductionCode.c");
+
+  RUN_TEST(test_FindFunction_WhichIsBroken_ShouldReturnZeroIfItemIsNotInList_WhichWorksEvenInOurBrokenCode, 20);
+  RUN_TEST(test_FindFunction_WhichIsBroken_ShouldReturnTheIndexForItemsInList_WhichWillFailBecauseOurFunctionUnderTestIsBroken, 30);
+  RUN_TEST(test_FunctionWhichReturnsLocalVariable_ShouldReturnTheCurrentCounterValue, 41);
+  RUN_TEST(test_FunctionWhichReturnsLocalVariable_ShouldReturnTheCurrentCounterValueAgain, 51);
+  RUN_TEST(test_FunctionWhichReturnsLocalVariable_ShouldReturnCurrentCounter_ButFailsBecauseThisTestIsActuallyFlawed, 57);
+
+  UnityEnd();
+  return 0;
+}