diff --git a/c/README.md b/c/README.md index 8d05d0cd..abab81b8 100644 --- a/c/README.md +++ b/c/README.md @@ -23,3 +23,4 @@ - [high-scores](./high-scores/README.md) - [bob](./bob/README.md) - [triangle](./triangle/README.md) +- [leap](./leap/README.md) diff --git a/c/leap/README.md b/c/leap/README.md index 46a8a1d6..fd5c68fc 100644 --- a/c/leap/README.md +++ b/c/leap/README.md @@ -49,4 +49,9 @@ For a delightful, four minute explanation of the whole leap year phenomenon, go ### Based on -JavaRanch Cattle Drive, exercise 3 - http://www.javaranch.com/leap.jsp \ No newline at end of file +JavaRanch Cattle Drive, exercise 3 - http://www.javaranch.com/leap.jsp + +### My Solution + +- [my solution](./leap.c) +- [run-tests](./run-tests-c.txt) diff --git a/c/leap/leap.c b/c/leap/leap.c index f601d9f9..1dd8850c 100644 --- a/c/leap/leap.c +++ b/c/leap/leap.c @@ -1 +1,17 @@ #include "leap.h" + +bool leap_year(int year) { + if (year % 400 == 0) { + return true; + } + + if (year % 100 == 0) { + return false; + } + + if (year % 4 == 0) { + return true; + } + + return false; +} diff --git a/c/leap/run-tests-c.txt b/c/leap/run-tests-c.txt new file mode 100644 index 00000000..7a8b122f --- /dev/null +++ b/c/leap/run-tests-c.txt @@ -0,0 +1,175 @@ +Running automated test file(s): + + +=============================================================================== + +Running: make clean +rm -rf *.o *.out *.out.dSYM + +real 0m0.004s +user 0m0.002s +sys 0m0.001s + +=============================================================================== + +Running: make test | ansifilter +Compiling tests.out +test_leap.c:59:test_year_not_divisible_by_4_in_common_year:PASS +test_leap.c:60:test_year_divisible_by_2_not_divisible_by_4_in_common_year:PASS +test_leap.c:61:test_year_divisible_by_4_not_divisible_by_100_in_leap_year:PASS +test_leap.c:62:test_year_divisible_by_4_and_5_is_still_a_leap_year:PASS +test_leap.c:63:test_year_divisible_by_100_not_divisible_by_400_in_common_year:PASS +test_leap.c:64:test_year_divisible_by_100_but_not_by_3_is_still_not_a_leap_year:PASS +test_leap.c:65:test_year_divisible_by_400_in_leap_year:PASS +test_leap.c:66:test_year_divisible_by_400_but_not_by_125_is_still_a_leap_year:PASS +test_leap.c:67:test_year_divisible_by_200_not_divisible_by_400_in_common_year:PASS + +----------------------- +9 Tests 0 Failures 0 Ignored +OK + +real 0m0.102s +user 0m0.071s +sys 0m0.031s + +=============================================================================== + +Running: make memcheck | ansifilter +Compiling memcheck +test_leap.c:59:test_year_not_divisible_by_4_in_common_year:PASS +test_leap.c:60:test_year_divisible_by_2_not_divisible_by_4_in_common_year:PASS +test_leap.c:61:test_year_divisible_by_4_not_divisible_by_100_in_leap_year:PASS +test_leap.c:62:test_year_divisible_by_4_and_5_is_still_a_leap_year:PASS +test_leap.c:63:test_year_divisible_by_100_not_divisible_by_400_in_common_year:PASS +test_leap.c:64:test_year_divisible_by_100_but_not_by_3_is_still_not_a_leap_year:PASS +test_leap.c:65:test_year_divisible_by_400_in_leap_year:PASS +test_leap.c:66:test_year_divisible_by_400_but_not_by_125_is_still_a_leap_year:PASS +test_leap.c:67:test_year_divisible_by_200_not_divisible_by_400_in_common_year:PASS + +----------------------- +9 Tests 0 Failures 0 Ignored +OK +Memory check passed + +real 0m0.124s +user 0m0.093s +sys 0m0.032s + +=============================================================================== + +Running: clang-check-16 ./leap.c ./test_leap.c ./leap.h -- + +real 0m0.035s +user 0m0.022s +sys 0m0.013s + +=============================================================================== + +Running: clang-tidy-16 ./leap.c ./test_leap.c ./leap.h -checks=*,-llvm-header-guard,-llvmlibc-restrict-system-libc-headers -- | head -n 100 +12 warnings generated. +2169 warnings generated. +2177 warnings generated. +/home/vpayno/git_vpayno/exercism-workspace/c/leap/leap.c:4:16: warning: 400 is a magic number; consider replacing it with a named constant [cppcoreguidelines-avoid-magic-numbers,readability-magic-numbers] + if (year % 400== 0) { + ^ +/home/vpayno/git_vpayno/exercism-workspace/c/leap/leap.c:8:16: warning: 100 is a magic number; consider replacing it with a named constant [cppcoreguidelines-avoid-magic-numbers,readability-magic-numbers] + if (year % 100== 0) { + ^ +/home/vpayno/git_vpayno/exercism-workspace/c/leap/test_leap.c:9:5: warning: kernel performance could be improved by unrolling this loop with a '#pragma unroll' directive [altera-unroll-loops] + TEST_ASSERT_FALSE(leap_year(2015)); + ^ +/home/vpayno/git_vpayno/exercism-workspace/c/leap/test-framework/unity.h:128:100: note: expanded from macro 'TEST_ASSERT_FALSE' +#define TEST_ASSERT_FALSE(condition) UNITY_TEST_ASSERT( !(condition), __LINE__, " Expected FALSE Was TRUE") + ^ +/home/vpayno/git_vpayno/exercism-workspace/c/leap/test-framework/unity_internals.h:775:98: note: expanded from macro 'UNITY_TEST_ASSERT' +#define UNITY_TEST_ASSERT(condition, line, message) do {if (condition) {} else {UNITY_TEST_FAIL((UNITY_LINE_TYPE)(line), (message));}} while(0) + ^ +/home/vpayno/git_vpayno/exercism-workspace/c/leap/test_leap.c:14:5: warning: kernel performance could be improved by unrolling this loop with a '#pragma unroll' directive [altera-unroll-loops] + TEST_ASSERT_FALSE(leap_year(1970)); + ^ +/home/vpayno/git_vpayno/exercism-workspace/c/leap/test-framework/unity.h:128:100: note: expanded from macro 'TEST_ASSERT_FALSE' +#define TEST_ASSERT_FALSE(condition) UNITY_TEST_ASSERT( !(condition), __LINE__, " Expected FALSE Was TRUE") + ^ +/home/vpayno/git_vpayno/exercism-workspace/c/leap/test-framework/unity_internals.h:775:98: note: expanded from macro 'UNITY_TEST_ASSERT' +#define UNITY_TEST_ASSERT(condition, line, message) do {if (condition) {} else {UNITY_TEST_FAIL((UNITY_LINE_TYPE)(line), (message));}} while(0) + ^ +/home/vpayno/git_vpayno/exercism-workspace/c/leap/test_leap.c:19:5: warning: kernel performance could be improved by unrolling this loop with a '#pragma unroll' directive [altera-unroll-loops] + TEST_ASSERT_TRUE(leap_year(1996)); + ^ +/home/vpayno/git_vpayno/exercism-workspace/c/leap/test-framework/unity.h:126:100: note: expanded from macro 'TEST_ASSERT_TRUE' +#define TEST_ASSERT_TRUE(condition) UNITY_TEST_ASSERT( (condition), __LINE__, " Expected TRUE Was FALSE") + ^ +/home/vpayno/git_vpayno/exercism-workspace/c/leap/test-framework/unity_internals.h:775:98: note: expanded from macro 'UNITY_TEST_ASSERT' +#define UNITY_TEST_ASSERT(condition, line, message) do {if (condition) {} else {UNITY_TEST_FAIL((UNITY_LINE_TYPE)(line), (message));}} while(0) + ^ +/home/vpayno/git_vpayno/exercism-workspace/c/leap/test_leap.c:24:5: warning: kernel performance could be improved by unrolling this loop with a '#pragma unroll' directive [altera-unroll-loops] + TEST_ASSERT_TRUE(leap_year(1960)); + ^ +/home/vpayno/git_vpayno/exercism-workspace/c/leap/test-framework/unity.h:126:100: note: expanded from macro 'TEST_ASSERT_TRUE' +#define TEST_ASSERT_TRUE(condition) UNITY_TEST_ASSERT( (condition), __LINE__, " Expected TRUE Was FALSE") + ^ +/home/vpayno/git_vpayno/exercism-workspace/c/leap/test-framework/unity_internals.h:775:98: note: expanded from macro 'UNITY_TEST_ASSERT' +#define UNITY_TEST_ASSERT(condition, line, message) do {if (condition) {} else {UNITY_TEST_FAIL((UNITY_LINE_TYPE)(line), (message));}} while(0) + ^ +/home/vpayno/git_vpayno/exercism-workspace/c/leap/test_leap.c:30:5: warning: kernel performance could be improved by unrolling this loop with a '#pragma unroll' directive [altera-unroll-loops] + TEST_ASSERT_FALSE(leap_year(2100)); + ^ +/home/vpayno/git_vpayno/exercism-workspace/c/leap/test-framework/unity.h:128:100: note: expanded from macro 'TEST_ASSERT_FALSE' +#define TEST_ASSERT_FALSE(condition) UNITY_TEST_ASSERT( !(condition), __LINE__, " Expected FALSE Was TRUE") + ^ +/home/vpayno/git_vpayno/exercism-workspace/c/leap/test-framework/unity_internals.h:775:98: note: expanded from macro 'UNITY_TEST_ASSERT' +#define UNITY_TEST_ASSERT(condition, line, message) do {if (condition) {} else {UNITY_TEST_FAIL((UNITY_LINE_TYPE)(line), (message));}} while(0) + ^ +/home/vpayno/git_vpayno/exercism-workspace/c/leap/test_leap.c:36:5: warning: kernel performance could be improved by unrolling this loop with a '#pragma unroll' directive [altera-unroll-loops] + TEST_ASSERT_FALSE(leap_year(1900)); + ^ +/home/vpayno/git_vpayno/exercism-workspace/c/leap/test-framework/unity.h:128:100: note: expanded from macro 'TEST_ASSERT_FALSE' +#define TEST_ASSERT_FALSE(condition) UNITY_TEST_ASSERT( !(condition), __LINE__, " Expected FALSE Was TRUE") + ^ +/home/vpayno/git_vpayno/exercism-workspace/c/leap/test-framework/unity_internals.h:775:98: note: expanded from macro 'UNITY_TEST_ASSERT' +#define UNITY_TEST_ASSERT(condition, line, message) do {if (condition) {} else {UNITY_TEST_FAIL((UNITY_LINE_TYPE)(line), (message));}} while(0) + ^ +/home/vpayno/git_vpayno/exercism-workspace/c/leap/test_leap.c:41:5: warning: kernel performance could be improved by unrolling this loop with a '#pragma unroll' directive [altera-unroll-loops] + TEST_ASSERT_TRUE(leap_year(2000)); + ^ +/home/vpayno/git_vpayno/exercism-workspace/c/leap/test-framework/unity.h:126:100: note: expanded from macro 'TEST_ASSERT_TRUE' +#define TEST_ASSERT_TRUE(condition) UNITY_TEST_ASSERT( (condition), __LINE__, " Expected TRUE Was FALSE") + ^ +/home/vpayno/git_vpayno/exercism-workspace/c/leap/test-framework/unity_internals.h:775:98: note: expanded from macro 'UNITY_TEST_ASSERT' +#define UNITY_TEST_ASSERT(condition, line, message) do {if (condition) {} else {UNITY_TEST_FAIL((UNITY_LINE_TYPE)(line), (message));}} while(0) + ^ +/home/vpayno/git_vpayno/exercism-workspace/c/leap/test_leap.c:47:5: warning: kernel performance could be improved by unrolling this loop with a '#pragma unroll' directive [altera-unroll-loops] + TEST_ASSERT_TRUE(leap_year(240Suppressed 2164 warnings (2164 in non-user code). +Use -header-filter=.* to display errors from all non-system headers. Use -system-headers to display errors from system headers as well. +0)); + ^ +/home/vpayno/git_vpayno/exercism-workspace/c/leap/test-framework/unity.h:126:100: note: expanded from macro 'TEST_ASSERT_TRUE' +#define TEST_ASSERT_TRUE(condition) UNITY_TEST_ASSERT( (condition), __LINE__, " Expected TRUE Was FALSE") + ^ +/home/vpayno/git_vpayno/exercism-workspace/c/leap/test-framework/unity_internals.h:775:98: note: expanded from macro 'UNITY_TEST_ASSERT' +#define UNITY_TEST_ASSERT(condition, line, message) do {if (condition) {} else {UNITY_TEST_FAIL((UNITY_LINE_TYPE)(line), (message));}} while(0) + ^ +/home/vpayno/git_vpayno/exercism-workspace/c/leap/test_leap.c:53:5: warning: kernel performance could be improved by unrolling this loop with a '#pragma unroll' directive [altera-unroll-loops] + TEST_ASSERT_FALSE(leap_year(1800)); + ^ +/home/vpayno/git_vpayno/exercism-workspace/c/leap/test-framework/unity.h:128:100: note: expanded from macro 'TEST_ASSERT_FALSE' +#define TEST_ASSERT_FALSE(condition) UNITY_TEST_ASSERT( !(condition), __LINE__, " Expected FALSE Was TRUE") + ^ +/home/vpayno/git_vpayno/exercism-workspace/c/leap/test-framework/unity_internals.h:775:98: note: expanded from macro 'UNITY_TEST_ASSERT' +#define UNITY_TEST_ASSERT(condition, line, message) do {if (condition) {} else {UNITY_TEST_FAIL((UNITY_LINE_TYPE)(line), (message));}} while(0) + ^ + +real 0m0.109s +user 0m0.092s +sys 0m0.018s + +=============================================================================== + +Running: clang-format-16 -style=file -i ./leap.c ./test_leap.c ./leap.h + +real 0m0.020s +user 0m0.014s +sys 0m0.006s + +=============================================================================== + diff --git a/c/leap/test_leap.c b/c/leap/test_leap.c index 2d149d56..0adf3f45 100644 --- a/c/leap/test_leap.c +++ b/c/leap/test_leap.c @@ -1,81 +1,70 @@ -#include "test-framework/unity.h" #include "leap.h" +#include "test-framework/unity.h" -void setUp(void) -{ -} +void setUp(void) {} -void tearDown(void) -{ -} +void tearDown(void) {} -static void test_year_not_divisible_by_4_in_common_year(void) -{ - TEST_ASSERT_FALSE(leap_year(2015)); +static void test_year_not_divisible_by_4_in_common_year(void) { + TEST_ASSERT_FALSE(leap_year(2015)); } -static void test_year_divisible_by_2_not_divisible_by_4_in_common_year(void) -{ - TEST_IGNORE(); // delete this line to run test - TEST_ASSERT_FALSE(leap_year(1970)); +static void test_year_divisible_by_2_not_divisible_by_4_in_common_year(void) { + // TEST_IGNORE(); // delete this line to run test + TEST_ASSERT_FALSE(leap_year(1970)); } -static void test_year_divisible_by_4_not_divisible_by_100_in_leap_year(void) -{ - TEST_IGNORE(); - TEST_ASSERT_TRUE(leap_year(1996)); +static void test_year_divisible_by_4_not_divisible_by_100_in_leap_year(void) { + // TEST_IGNORE(); + TEST_ASSERT_TRUE(leap_year(1996)); } -static void test_year_divisible_by_4_and_5_is_still_a_leap_year(void) -{ - TEST_IGNORE(); - TEST_ASSERT_TRUE(leap_year(1960)); +static void test_year_divisible_by_4_and_5_is_still_a_leap_year(void) { + // TEST_IGNORE(); + TEST_ASSERT_TRUE(leap_year(1960)); } -static void test_year_divisible_by_100_not_divisible_by_400_in_common_year(void) -{ - TEST_IGNORE(); - TEST_ASSERT_FALSE(leap_year(2100)); +static void +test_year_divisible_by_100_not_divisible_by_400_in_common_year(void) { + // TEST_IGNORE(); + TEST_ASSERT_FALSE(leap_year(2100)); } static void -test_year_divisible_by_100_but_not_by_3_is_still_not_a_leap_year(void) -{ - TEST_IGNORE(); - TEST_ASSERT_FALSE(leap_year(1900)); +test_year_divisible_by_100_but_not_by_3_is_still_not_a_leap_year(void) { + // TEST_IGNORE(); + TEST_ASSERT_FALSE(leap_year(1900)); } -static void test_year_divisible_by_400_in_leap_year(void) -{ - TEST_IGNORE(); - TEST_ASSERT_TRUE(leap_year(2000)); +static void test_year_divisible_by_400_in_leap_year(void) { + // TEST_IGNORE(); + TEST_ASSERT_TRUE(leap_year(2000)); } -static void test_year_divisible_by_400_but_not_by_125_is_still_a_leap_year(void) -{ - TEST_IGNORE(); - TEST_ASSERT_TRUE(leap_year(2400)); +static void +test_year_divisible_by_400_but_not_by_125_is_still_a_leap_year(void) { + // TEST_IGNORE(); + TEST_ASSERT_TRUE(leap_year(2400)); } -static void test_year_divisible_by_200_not_divisible_by_400_in_common_year(void) -{ - TEST_IGNORE(); - TEST_ASSERT_FALSE(leap_year(1800)); +static void +test_year_divisible_by_200_not_divisible_by_400_in_common_year(void) { + // TEST_IGNORE(); + TEST_ASSERT_FALSE(leap_year(1800)); } -int main(void) -{ - UnityBegin("test_leap.c"); +int main(void) { + UnityBegin("test_leap.c"); - RUN_TEST(test_year_not_divisible_by_4_in_common_year); - RUN_TEST(test_year_divisible_by_2_not_divisible_by_4_in_common_year); - RUN_TEST(test_year_divisible_by_4_not_divisible_by_100_in_leap_year); - RUN_TEST(test_year_divisible_by_4_and_5_is_still_a_leap_year); - RUN_TEST(test_year_divisible_by_100_not_divisible_by_400_in_common_year); - RUN_TEST(test_year_divisible_by_100_but_not_by_3_is_still_not_a_leap_year); - RUN_TEST(test_year_divisible_by_400_in_leap_year); - RUN_TEST(test_year_divisible_by_400_but_not_by_125_is_still_a_leap_year); - RUN_TEST(test_year_divisible_by_200_not_divisible_by_400_in_common_year); + RUN_TEST(test_year_not_divisible_by_4_in_common_year); + RUN_TEST(test_year_divisible_by_2_not_divisible_by_4_in_common_year); + RUN_TEST(test_year_divisible_by_4_not_divisible_by_100_in_leap_year); + RUN_TEST(test_year_divisible_by_4_and_5_is_still_a_leap_year); + RUN_TEST(test_year_divisible_by_100_not_divisible_by_400_in_common_year); + RUN_TEST(test_year_divisible_by_100_but_not_by_3_is_still_not_a_leap_year); + RUN_TEST(test_year_divisible_by_400_in_leap_year); + RUN_TEST(test_year_divisible_by_400_but_not_by_125_is_still_a_leap_year); + RUN_TEST(test_year_divisible_by_200_not_divisible_by_400_in_common_year); - return UnityEnd(); + return UnityEnd(); }