From d16c27b08571f4bb46da72571d17da8197c544c8 Mon Sep 17 00:00:00 2001 From: mvandervoord Date: Fri, 25 Oct 2019 10:17:12 -0400 Subject: [PATCH] - added target for checking ANSI compliance - fixed ANSI (C89) issues, including #418 --- docs/UnityConfigurationGuide.md | 9 +++ extras/fixture/test/unity_fixture_Test.c | 4 +- src/unity.c | 2 +- src/unity_internals.h | 5 +- test/rakefile_helper.rb | 9 +++ test/targets/ansi.yml | 48 ++++++++++++++ test/testdata/CException.h | 2 +- test/testdata/Defs.h | 2 +- test/testdata/cmock.h | 2 +- test/testdata/mockMock.h | 2 +- test/testdata/testRunnerGenerator.c | 4 +- test/testdata/testRunnerGeneratorWithMocks.c | 2 + test/tests/test_generate_test_runner.rb | 20 +++++- test/tests/testparameterized.c | 3 - test/tests/testunity.c | 66 +++++++++++--------- 15 files changed, 134 insertions(+), 46 deletions(-) create mode 100644 test/targets/ansi.yml diff --git a/docs/UnityConfigurationGuide.md b/docs/UnityConfigurationGuide.md index 68fb106..bdfb853 100644 --- a/docs/UnityConfigurationGuide.md +++ b/docs/UnityConfigurationGuide.md @@ -427,6 +427,15 @@ will allow you to specify how Unity will treat these assertions. - AS_NONE - Unity will disallow the use of these shorthand macros altogether, insisting that developers choose a more descriptive option. +#### `UNITY_SUPPORT_VARIADIC_MACROS` + +This will force Unity to support variadic macros when using it's own built-in +RUN_TEST macro. This will rarely be necessary. Most often, Unity will automatically +detect if the compiler supports variadic macros by checking to see if it's C99+ +compatible. In the event that the compiler supports variadic macros, but is primarily +C89 (ANSI), defining this option will allow you to use them. This option is also not +necessary when using Ceedling or the test runner generator script. + ## Getting Into The Guts There will be cases where the options above aren't quite going to get everything diff --git a/extras/fixture/test/unity_fixture_Test.c b/extras/fixture/test/unity_fixture_Test.c index a842b08..da44d5d 100644 --- a/extras/fixture/test/unity_fixture_Test.c +++ b/extras/fixture/test/unity_fixture_Test.c @@ -345,7 +345,7 @@ TEST_TEAR_DOWN(LeakDetection) /* This tricky set of defines lets us see if we are using the Spy, returns 1 if true */ #ifdef __STDC_VERSION__ -#if __STDC_VERSION__ >= 199901L +#if UNITY_SUPPORT_VARIADIC_MACROS #define USING_SPY_AS(a) EXPAND_AND_USE_2ND(ASSIGN_VALUE(a), 0) #define ASSIGN_VALUE(a) VAL_##a #define VAL_UnityOutputCharSpy_OutputChar 0, 1 @@ -354,7 +354,7 @@ TEST_TEAR_DOWN(LeakDetection) #if USING_SPY_AS(UNITY_OUTPUT_CHAR) #define USING_OUTPUT_SPY /* UNITY_OUTPUT_CHAR = UnityOutputCharSpy_OutputChar */ #endif -#endif /* >= 199901 */ +#endif /* UNITY_SUPPORT_VARIADIC_MACROS */ #else /* __STDC_VERSION__ else */ #define UnityOutputCharSpy_OutputChar 42 diff --git a/src/unity.c b/src/unity.c index 9883fd7..bcc0283 100644 --- a/src/unity.c +++ b/src/unity.c @@ -1814,9 +1814,9 @@ int UnityVerbosity = 1; /*-----------------------------------------------*/ int UnityParseOptions(int argc, char** argv) { + int i; UnityOptionIncludeNamed = NULL; UnityOptionExcludeNamed = NULL; - int i; for (i = 1; i < argc; i++) { diff --git a/src/unity_internals.h b/src/unity_internals.h index 0995281..0b8578e 100644 --- a/src/unity_internals.h +++ b/src/unity_internals.h @@ -683,6 +683,10 @@ extern const char UnityStrErrShorthand[]; #ifndef RUN_TEST #ifdef __STDC_VERSION__ #if __STDC_VERSION__ >= 199901L +#define UNITY_SUPPORT_VARIADIC +#endif +#endif +#ifdef UNITY_SUPPORT_VARIADIC_MACROS #define RUN_TEST(...) UnityDefaultTestRun(RUN_TEST_FIRST(__VA_ARGS__), RUN_TEST_SECOND(__VA_ARGS__)) #define RUN_TEST_FIRST(...) RUN_TEST_FIRST_HELPER(__VA_ARGS__, throwaway) #define RUN_TEST_FIRST_HELPER(first, ...) (first), #first @@ -690,7 +694,6 @@ extern const char UnityStrErrShorthand[]; #define RUN_TEST_SECOND_HELPER(first, second, ...) (second) #endif #endif -#endif /* If we can't do the tricky version, we'll just have to require them to always include the line number */ #ifndef RUN_TEST diff --git a/test/rakefile_helper.rb b/test/rakefile_helper.rb index 7106868..56f6dcc 100644 --- a/test/rakefile_helper.rb +++ b/test/rakefile_helper.rb @@ -200,6 +200,15 @@ def run_tests(test_files) # Build and execute each unit test test_files.each do |test| + + # Drop Out if we're skipping this type of test + if $cfg[:skip_tests] + if $cfg[:skip_tests].include?(:parameterized) && test.match(/parameterized/) + report("Skipping Parameterized Tests for this Target:IGNORE") + next + end + end + obj_list = [] unless $cfg['compiler']['aux_sources'].nil? diff --git a/test/targets/ansi.yml b/test/targets/ansi.yml new file mode 100644 index 0000000..9cc393d --- /dev/null +++ b/test/targets/ansi.yml @@ -0,0 +1,48 @@ +compiler: + path: gcc + source_path: '../src/' + unit_tests_path: &unit_tests_path 'tests/' + build_path: &build_path 'build/' + options: + - '-c' + - '-m64' + - '-Wall' + - '-Wno-address' + - '-ansi' + #- '-pedantic' + includes: + prefix: '-I' + items: + - 'src/' + - '../src/' + - 'testdata/' + - *unit_tests_path + defines: + prefix: '-D' + items: + - UNITY_INCLUDE_DOUBLE + - UNITY_SUPPORT_TEST_CASES + - UNITY_EXCLUDE_TESTING_NEW_COMMENTS + object_files: + prefix: '-o' + extension: '.o' + destination: *build_path +linker: + path: gcc + options: + - -lm + - '-m64' + includes: + prefix: '-I' + object_files: + path: *build_path + extension: '.o' + bin_files: + prefix: '-o' + extension: '.exe' + destination: *build_path +colour: true +:unity: + :plugins: [] +:skip_tests: + - :parameterized \ No newline at end of file diff --git a/test/testdata/CException.h b/test/testdata/CException.h index 91ad3e1..3872fa7 100644 --- a/test/testdata/CException.h +++ b/test/testdata/CException.h @@ -8,4 +8,4 @@ #define Try if (e) #define Catch(a) if (!a) -#endif //CEXCEPTION_H +#endif diff --git a/test/testdata/Defs.h b/test/testdata/Defs.h index d3a90c0..b1dc83e 100644 --- a/test/testdata/Defs.h +++ b/test/testdata/Defs.h @@ -5,4 +5,4 @@ extern int CounterSuiteSetup; -#endif //DEF_H +#endif diff --git a/test/testdata/cmock.h b/test/testdata/cmock.h index c6149be..33ddbfc 100644 --- a/test/testdata/cmock.h +++ b/test/testdata/cmock.h @@ -11,4 +11,4 @@ void mockMock_Init(void) { mockMock_Init_Counter++; } void mockMock_Verify(void) { mockMock_Verify_Counter++; } void mockMock_Destroy(void) { mockMock_Destroy_Counter++; } -#endif //CMOCK_H +#endif diff --git a/test/testdata/mockMock.h b/test/testdata/mockMock.h index 0a2c616..5c6829d 100644 --- a/test/testdata/mockMock.h +++ b/test/testdata/mockMock.h @@ -10,4 +10,4 @@ void mockMock_Init(void); void mockMock_Verify(void); void mockMock_Destroy(void); -#endif //MOCK_MOCK_H +#endif diff --git a/test/testdata/testRunnerGenerator.c b/test/testdata/testRunnerGenerator.c index 1139939..b5dd97f 100644 --- a/test/testdata/testRunnerGenerator.c +++ b/test/testdata/testRunnerGenerator.c @@ -108,10 +108,12 @@ void custtest_ThisTestPassesWhenCustomTeardownRan(void) TEST_ASSERT_EQUAL_MESSAGE(2, CounterTeardown, "Custom Teardown Wasn't Run"); } +#ifndef UNITY_EXCLUDE_TESTING_NEW_COMMENTS //void test_NewStyleCommentsShouldBeIgnored(void) //{ // TEST_ASSERT_FAIL("New Style Comments Should Be Ignored"); //} +#endif void test_NotBeConfusedByLongComplicatedStrings(void) { @@ -185,5 +187,3 @@ void suitetest_ThisTestPassesWhenCustomSuiteSetupAndTeardownRan(void) { TEST_ASSERT_EQUAL_MESSAGE(1, CounterSuiteSetup, "Suite Setup Should Have Run"); } - - diff --git a/test/testdata/testRunnerGeneratorWithMocks.c b/test/testdata/testRunnerGeneratorWithMocks.c index 4aacbf9..aaceda4 100644 --- a/test/testdata/testRunnerGeneratorWithMocks.c +++ b/test/testdata/testRunnerGeneratorWithMocks.c @@ -109,10 +109,12 @@ void custtest_ThisTestPassesWhenCustomTeardownRan(void) TEST_ASSERT_EQUAL_MESSAGE(2, CounterTeardown, "Custom Teardown Wasn't Run"); } +#ifndef UNITY_EXCLUDE_TESTING_NEW_COMMENTS //void test_NewStyleCommentsShouldBeIgnored(void) //{ // TEST_ASSERT_FAIL("New Style Comments Should Be Ignored"); //} +#endif void test_NotBeConfusedByLongComplicatedStrings(void) { diff --git a/test/tests/test_generate_test_runner.rb b/test/tests/test_generate_test_runner.rb index 0b51bcd..ebf011d 100644 --- a/test/tests/test_generate_test_runner.rb +++ b/test/tests/test_generate_test_runner.rb @@ -144,6 +144,7 @@ :test_prefix => "paratest", :use_param_tests => true, }, + :features => [ :parameterized ], :expected => { :to_pass => [ 'paratest_ShouldHandleParameterizedTests\(25\)', 'paratest_ShouldHandleParameterizedTests\(125\)', @@ -160,6 +161,7 @@ :testfile => 'testdata/testRunnerGenerator.c', :testdefines => ['TEST'], :cmdline => " --test_prefix=\"paratest\" --use_param_tests=1", + :features => [ :parameterized ], :expected => { :to_pass => [ 'paratest_ShouldHandleParameterizedTests\(25\)', 'paratest_ShouldHandleParameterizedTests\(125\)', @@ -179,6 +181,7 @@ :yaml => { :test_prefix => "paratest" }, + :features => [ :parameterized ], :expected => { :to_pass => [ 'paratest_ShouldHandleParameterizedTests\(25\)', 'paratest_ShouldHandleParameterizedTests\(125\)', @@ -468,6 +471,7 @@ :test_prefix => "paratest", :use_param_tests => true, }, + :features => [ :parameterized ], :expected => { :to_pass => [ 'paratest_ShouldHandleParameterizedTests\(25\)', 'paratest_ShouldHandleParameterizedTests\(125\)', @@ -484,6 +488,7 @@ :testfile => 'testdata/testRunnerGeneratorWithMocks.c', :testdefines => ['TEST'], :cmdline => " --test_prefix=\"paratest\" --use_param_tests=1", + :features => [ :parameterized ], :expected => { :to_pass => [ 'paratest_ShouldHandleParameterizedTests\(25\)', 'paratest_ShouldHandleParameterizedTests\(125\)', @@ -503,6 +508,7 @@ :yaml => { :test_prefix => "paratest" }, + :features => [ :parameterized ], :expected => { :to_pass => [ 'paratest_ShouldHandleParameterizedTests\(25\)', 'paratest_ShouldHandleParameterizedTests\(125\)', @@ -1044,6 +1050,7 @@ :test_prefix => "paratest" }, :cmdline_args => "-n ShouldHandleParameterizedTests", + :features => [ :parameterized ], :expected => { :to_pass => [ 'paratest_ShouldHandleParameterizedTests\(25\)', 'paratest_ShouldHandleParameterizedTests\(125\)', @@ -1090,6 +1097,7 @@ :cmdline_args => true, }, :cmdline_args => "-l", + :features => [ :parameterized ], :expected => { :to_pass => [ ], :to_fail => [ ], @@ -1151,10 +1159,18 @@ }, ] -def runner_test(test, runner, expected, test_defines, cmdline_args) +def runner_test(test, runner, expected, test_defines, cmdline_args, features) # Tack on TEST define for compiling unit tests load_configuration($cfg_file) + # Drop Out if we're skipping this type of test + if $cfg[:skip_tests] && features + if $cfg[:skip_tests].include?(:parameterized) && features.include?(:parameterized) + report("Skipping Parameterized Tests for this Target:IGNORE") + return true + end + end + #compile objects obj_list = [ compile(runner, test_defines), @@ -1239,7 +1255,7 @@ def verify_number(expected, expression, output) end #test the script against the specified test file and check results - if (runner_test(testset[:testfile], runner_name, testset[:expected], testset[:testdefines], testset[:cmdline_args])) + if (runner_test(testset[:testfile], runner_name, testset[:expected], testset[:testdefines], testset[:cmdline_args], testset[:features])) report "#{testset_name}:PASS" else report "#{testset_name}:FAIL" diff --git a/test/tests/testparameterized.c b/test/tests/testparameterized.c index 6100834..0583cb7 100644 --- a/test/tests/testparameterized.c +++ b/test/tests/testparameterized.c @@ -169,6 +169,3 @@ void test_CharsArePreserved(unsigned index, char c) NextExpectedCharIndex++; } - - - diff --git a/test/tests/testunity.c b/test/tests/testunity.c index 297849f..2771a99 100644 --- a/test/tests/testunity.c +++ b/test/tests/testunity.c @@ -8,8 +8,9 @@ #include #include -// Dividing by these constants produces +/- infinity. -// The rationale is given in UnityAssertFloatIsInf's body. +/* Dividing by these constants produces +/- infinity. + * The rationale is given in UnityAssertFloatIsInf's body. + */ #ifndef UNITY_EXCLUDE_FLOAT static const UNITY_FLOAT f_zero = 0.0f; #endif @@ -297,7 +298,9 @@ void testNotEqualInt16s(void) void testNotEqualInt32s(void) { EXPECT_ABORT_BEGIN - TEST_ASSERT_EQUAL_INT32(-2147483647, -2147483648); //use largest 32 bit negative to test printability + /*use largest 32 bit negative to test printability*/ + /*note: (-2147483647 - 1) is used instead of -2147483648 because of C90 casting rules */ + TEST_ASSERT_EQUAL_INT32(-2147483647, (-2147483647 - 1)); VERIFY_FAILS_END } @@ -336,8 +339,8 @@ void testNotEqualUInt16s(void) { UNITY_UINT16 v0, v1; - v0 = 65535; - v1 = 65534; + v0 = 65535u; + v1 = 65534u; EXPECT_ABORT_BEGIN TEST_ASSERT_EQUAL_UINT16(v0, v1); @@ -348,8 +351,8 @@ void testNotEqualUInt32s(void) { UNITY_UINT32 v0, v1; - v0 = 4294967295; - v1 = 4294967294; + v0 = 4294967295u; + v1 = 4294967294u; EXPECT_ABORT_BEGIN TEST_ASSERT_EQUAL_UINT32(v0, v1); @@ -1116,7 +1119,7 @@ void testHEX8sNotWithinDeltaAndCustomMessage(void) VERIFY_FAILS_END } -//----------------- +/*-----------------*/ void testUINT32sWithinDelta(void) { @@ -1340,7 +1343,7 @@ void testINT8sNotWithinDeltaAndCustomMessage(void) VERIFY_FAILS_END } -//------------------------ +/*------------------------*/ void testInt64ArrayWithinDelta(void) { @@ -2807,7 +2810,7 @@ void testHEX8ArrayWithinDeltaSamePointerAndMessage(void) TEST_ASSERT_HEX8_ARRAY_WITHIN_MESSAGE(60, expected, expected, 3, "Custom Message."); } -//----------------- +/*-----------------*/ void testGreaterThan(void) { @@ -2998,8 +3001,8 @@ void testGreaterThanUINT32(void) UNITY_UINT32 v0, v1; UNITY_UINT32 *p0, *p1; - v0 = 0; - v1 = 4294967295; + v0 = 0u; + v1 = 4294967295u; p0 = &v0; p1 = &v1; @@ -3012,7 +3015,7 @@ void testGreaterThanUINT32(void) void testNotGreaterThanUINT32(void) { EXPECT_ABORT_BEGIN - TEST_ASSERT_GREATER_THAN_UINT32(4294967295, 0); + TEST_ASSERT_GREATER_THAN_UINT32(4294967295u, 0); VERIFY_FAILS_END } @@ -3323,7 +3326,7 @@ void testGreaterOrEqualUINT32(void) UNITY_UINT32 *p0, *p1, *p2; v0 = 0; - v1 = 4294967295; + v1 = 4294967295u; v2 = 0; p0 = &v0; p1 = &v1; @@ -3342,7 +3345,7 @@ void testGreaterOrEqualUINT32(void) void testNotGreaterOrEqualUINT32(void) { EXPECT_ABORT_BEGIN - TEST_ASSERT_GREATER_OR_EQUAL_UINT32(4294967295, 0); + TEST_ASSERT_GREATER_OR_EQUAL_UINT32(4294967295u, 0); VERIFY_FAILS_END } @@ -3433,8 +3436,7 @@ void testNotGreaterOrEqualHEX32(void) VERIFY_FAILS_END } -//----------------- - +/*-----------------*/ void testLessThan(void) { @@ -3625,7 +3627,7 @@ void testLessThanUINT32(void) UNITY_UINT32 v0, v1; UNITY_UINT32 *p0, *p1; - v0 = 4294967295; + v0 = 4294967295u; v1 = 0; p0 = &v0; p1 = &v1; @@ -3639,7 +3641,7 @@ void testLessThanUINT32(void) void testNotLessThanUINT32(void) { EXPECT_ABORT_BEGIN - TEST_ASSERT_LESS_THAN_UINT32(0, 4294967295); + TEST_ASSERT_LESS_THAN_UINT32(0, 4294967295u); VERIFY_FAILS_END } @@ -3949,9 +3951,9 @@ void testLessOrEqualUINT32(void) UNITY_UINT32 v0, v1, v2; UNITY_UINT32 *p0, *p1, *p2; - v0 = 4294967295; + v0 = 4294967295u; v1 = 0; - v2 = 4294967295; + v2 = 4294967295u; p0 = &v0; p1 = &v1; p2 = &v2; @@ -3969,7 +3971,7 @@ void testLessOrEqualUINT32(void) void testNotLessOrEqualUINT32(void) { EXPECT_ABORT_BEGIN - TEST_ASSERT_LESS_OR_EQUAL_UINT32(0, 4294967295); + TEST_ASSERT_LESS_OR_EQUAL_UINT32(0, 4294967295u); VERIFY_FAILS_END } @@ -4060,7 +4062,8 @@ void testNotLessOrEqualHEX32(void) VERIFY_FAILS_END } -//----------------- +/*-----------------*/ + void testEqualStrings(void) { const char *testString = "foo"; @@ -5651,14 +5654,14 @@ void testIgnoredAndThenFailInTearDown(void) TEST_IGNORE(); } -// Tricky series of macros to set USING_OUTPUT_SPY +/* Tricky series of macros to set USING_OUTPUT_SPY */ #define USING_SPY_AS(a) EXPAND_AND_USE_2ND(ASSIGN_VALUE(a), 0) #define ASSIGN_VALUE(a) VAL_##a #define VAL_putcharSpy 0, 1 #define EXPAND_AND_USE_2ND(a, b) SECOND_PARAM(a, b, throwaway) #define SECOND_PARAM(a, b, ...) b #if USING_SPY_AS(UNITY_OUTPUT_CHAR) - #define USING_OUTPUT_SPY // true only if UNITY_OUTPUT_CHAR = putcharSpy + #define USING_OUTPUT_SPY /* true only if UNITY_OUTPUT_CHAR = putcharSpy */ #endif #ifdef USING_OUTPUT_SPY @@ -5712,7 +5715,7 @@ void testFailureCountIncrementsAndIsReturnedAtEnd(void) { UNITY_UINT savedFailures = Unity.TestFailures; Unity.CurrentTestFailed = 1; - startPutcharSpy(); // Suppress output + startPutcharSpy(); /* Suppress output */ startFlushSpy(); TEST_ASSERT_EQUAL(0, getFlushSpyCalls()); UnityConcludeTest(); @@ -5725,7 +5728,7 @@ void testFailureCountIncrementsAndIsReturnedAtEnd(void) #endif endFlushSpy(); - startPutcharSpy(); // Suppress output + startPutcharSpy(); /* Suppress output */ int failures = UnityEnd(); Unity.TestFailures--; endPutcharSpy(); @@ -5798,7 +5801,7 @@ void testPrintNumbersUnsigned32(void) } -// ===================== THESE TEST WILL RUN IF YOUR CONFIG INCLUDES 64 BIT SUPPORT ================== +/* ===================== THESE TEST WILL RUN IF YOUR CONFIG INCLUDES 64 BIT SUPPORT ================== */ void testPrintNumbersInt64(void) { @@ -6175,7 +6178,8 @@ void testNotEqualInt64Arrays(void) VERIFY_FAILS_END #endif } -// ===================== THESE TEST WILL RUN IF YOUR CONFIG INCLUDES FLOAT SUPPORT ================== + +/* ===================== THESE TEST WILL RUN IF YOUR CONFIG INCLUDES FLOAT SUPPORT ================== */ void testFloatsWithinDelta(void) { @@ -7033,7 +7037,7 @@ void testFloatPrintingRandomSamples(void) #endif } -// ===================== THESE TEST WILL RUN IF YOUR CONFIG INCLUDES DOUBLE SUPPORT ================== +/* ===================== THESE TEST WILL RUN IF YOUR CONFIG INCLUDES DOUBLE SUPPORT ================== */ void testDoublesWithinDelta(void) { @@ -7774,7 +7778,7 @@ void testDoublePrintingInfinityAndNaN(void) #endif } -// ===================== THESE TEST WILL RUN IF YOUR CONFIG INCLUDES DETAIL SUPPORT ================== +/* ===================== THESE TEST WILL RUN IF YOUR CONFIG INCLUDES DETAIL SUPPORT ================== */ void testThatDetailsCanBeHandleOneDetail(void) {