Skip to content

Commit

Permalink
c/leap: 1st iteration
Browse files Browse the repository at this point in the history
  • Loading branch information
vpayno committed Jul 7, 2023
1 parent 1ea4658 commit 1f1b868
Show file tree
Hide file tree
Showing 5 changed files with 242 additions and 56 deletions.
1 change: 1 addition & 0 deletions c/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,4 @@
- [high-scores](./high-scores/README.md)
- [bob](./bob/README.md)
- [triangle](./triangle/README.md)
- [leap](./leap/README.md)
7 changes: 6 additions & 1 deletion c/leap/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
JavaRanch Cattle Drive, exercise 3 - http://www.javaranch.com/leap.jsp

### My Solution

- [my solution](./leap.c)
- [run-tests](./run-tests-c.txt)
16 changes: 16 additions & 0 deletions c/leap/leap.c
Original file line number Diff line number Diff line change
@@ -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;
}
175 changes: 175 additions & 0 deletions c/leap/run-tests-c.txt
Original file line number Diff line number Diff line change
@@ -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

===============================================================================

99 changes: 44 additions & 55 deletions c/leap/test_leap.c
Original file line number Diff line number Diff line change
@@ -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();
}

0 comments on commit 1f1b868

Please sign in to comment.