Skip to content

Commit

Permalink
Getting rid of check library
Browse files Browse the repository at this point in the history
* Add continuinty test
* Organise test structure
  • Loading branch information
soroush committed Jan 3, 2025
1 parent db854da commit 022a393
Show file tree
Hide file tree
Showing 14 changed files with 261 additions and 571 deletions.
23 changes: 3 additions & 20 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -23,25 +23,8 @@ project(calendars
HOMEPAGE_URL "https://github.com/soroush/libcalendars"
LANGUAGES C
)
if(WIN32)
find_package(check CONFIG REQUIRED)
add_library(check INTERFACE)
target_link_libraries(check INTERFACE Check::checkShared)
elseif(UNIX)
find_package(PkgConfig REQUIRED)
pkg_check_modules(CHECK REQUIRED check)
set(check_FOUND ${CHECK_FOUND})
add_library(check INTERFACE)
target_include_directories(check INTERFACE ${CHECK_INCLUDE_DIRS})
target_link_libraries(check INTERFACE ${CHECK_LIBRARIES})
link_directories(${CHECK_LIBRARY_DIRS})
endif()

add_subdirectory(lib)
enable_testing()

if(${check_FOUND})
enable_testing()
add_subdirectory(tests)
else()
message(STATUS "check library not found. Disabling the tests.")
endif()
add_subdirectory(lib)
add_subdirectory(tests)
2 changes: 1 addition & 1 deletion lib/src/cl-gregorian.c
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ uint8_t gr_days_in_month(uint8_t month, int16_t year) {
case 11:
return 30;
break;
case 02:
case 2:
return gr_is_leap(year) ? 29 : 28;
break;
default:
Expand Down
2 changes: 2 additions & 0 deletions lib/src/cl-solar-hijri.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ static const int32_t cycle_days = 1029983;
static const uint16_t cycle_years = 2820;
/* 365 + leapRatio */
static const double year_length = 365.24219858156028368;
// static const double year_length = 365.242374;
// static const double year_length = 365.2421875;
/* 475/01/01 AP, start of 2820 cycle */
static const uint32_t hijri_shamsi_epoch = 2121446;
/* 683.0 / 2820.0 */
Expand Down
2 changes: 1 addition & 1 deletion tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ include_directories(PUBLIC $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/../incl

# Commont test mathematics
add_library(calendar_test_common OBJECT calendar-arithmetic.c)
target_link_libraries(calendar_test_common PUBLIC calendars check)
target_link_libraries(calendar_test_common PUBLIC calendars)

link_libraries(calendar_test_common calendars)

Expand Down
79 changes: 14 additions & 65 deletions tests/babylonian.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,77 +17,26 @@
*
*/

#include "calendar-arithmetic.h"

#include <libcalendars/cl-babylonian.h>
#include <libcalendars/cl-gregorian.h>
#include <check.h>

#include "calendar-arithmetic.h"

#include <stdlib.h>
#include <assert.h>

START_TEST(babylonian_jdn)
{
uint32_t jd = 0;
uint32_t out_jd = 0;
for (jd = 0; jd < 2488069; jd++)
{
test_julian_day(&ba_to_jdn, &jdn_to_ba, jd, &out_jd);
assert(jd == out_jd);
}
}
END_TEST

START_TEST(babylonian_gregorian)
{
uint32_t jd = 0;
uint32_t out_jd = 0;
uint32_t in_jd;
int16_t gyi;
uint8_t gmi;
uint16_t gdi;
int16_t gyo;
uint8_t gmo;
uint16_t gdo;
for (jd = 0; jd < 2488069; jd++)
{
test_gregorian_calendar(&ba_to_gr, &gr_to_ba, jd,
&gyi, &gmi, &gdi,
&gyo, &gmo, &gdo);
assert(gyi == gyo);
assert(gmi == gmo);
assert(gdi == gdo);
}
}
END_TEST

Suite* create_tests(void)
{
Suite* suit;
TCase* babylonian;

suit = suite_create("Calendar Arithmetic");

babylonian = tcase_create("Babylonian");
tcase_add_test(babylonian, babylonian_jdn);
tcase_add_test(babylonian, babylonian_gregorian);

suite_add_tcase(suit, babylonian);
return suit;
}

int main(void)
{
int number_failed;
Suite* all_tests;
SRunner* runner;

all_tests = create_tests();
runner = srunner_create(all_tests);
srunner_set_fork_status(runner, CK_NOFORK);
srunner_run_all(runner, CK_NORMAL);
number_failed = srunner_ntests_failed(runner);
srunner_free(runner);

return (number_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
const test_context ctx = {
.to_jdn = &ba_to_jdn,
.from_jdn = &jdn_to_ba,
.to_gr = &ba_to_gr,
.from_gr = &gr_to_ba,
.jdn_to_gr = &jdn_to_gr,
.min_jd = 0,
.max_jd = 2488069,
};

test_julian_day(&ctx);
test_gregorian_calendar(&ctx);
}
126 changes: 112 additions & 14 deletions tests/calendar-arithmetic.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,33 +19,131 @@

#include <stdint.h>
#include <stdio.h>
#include <assert.h>
#include <libcalendars/cl-gregorian.h>
#include "calendar-arithmetic.h"

void test_julian_day(
test_context* ctx,
uint32_t in_jd, uint32_t* out_jd)
void test_julian_day(const test_context* const ctx)
{
assert(ctx);
int16_t year;
uint8_t month;
uint16_t day;
ctx->jdn_to_cal(in_jd, &year, &month, &day);
ctx->cal_to_jdn(out_jd, year, month, day);
uint32_t out_jd;
for (uint32_t in_jd = ctx->min_jd; in_jd < ctx->max_jd; in_jd++)
{
ctx->from_jdn(in_jd, &year, &month, &day);
ctx->to_jdn(&out_jd, year, month, day);
assert(in_jd == out_jd);
}
}

void test_gregorian_calendar(
test_context* ctx,
uint32_t in_jd, int16_t* gyi, uint8_t* gmi, uint16_t* gdi, int16_t* gyo, uint8_t* gmo, uint16_t* gdo)
void test_gregorian_calendar(const test_context* const ctx)
{
assert(ctx);

int16_t year;
uint8_t month;
uint16_t day;
jdn_to_gr(in_jd, gyi, gmi, gdi);
ctx->gr_to_cal(*gyi, *gmi, *gdi, &year, &month, &day);
ctx->cal_to_gr(year, month, day, gyo, gmo, gdo);
int16_t gyi;
uint8_t gmi;
uint16_t gdi;
int16_t gyo;
uint8_t gmo;
uint16_t gdo;

for (uint32_t in_jd = ctx->min_jd; in_jd < ctx->max_jd; in_jd++)
{

ctx->jdn_to_gr(in_jd, &gyi, &gmi, &gdi);
ctx->from_gr(gyi, gmi, gdi, &year, &month, &day);
ctx->to_gr(year, month, day, &gyo, &gmo, &gdo);
assert(gyi == gyo);
assert(gmi == gmo);
assert(gdi == gdo);
}
}

// void test_continuity(test_context ctx, uint8_t* valid)
// {
void test_continuity(const test_context* const ctx)
{
assert(ctx);
uint16_t days_in_year = 0;
uint16_t days_in_month = 0;
int16_t year;
uint8_t month;
uint16_t day;

ctx->from_jdn(ctx->min_jd, &year, &month, &day);

// }
for (uint32_t in_jd = ctx->min_jd + 1; in_jd < ctx->max_jd; in_jd++)
{
int16_t new_year;
uint8_t new_month;
uint16_t new_day;
ctx->from_jdn(in_jd, &new_year, &new_month, &new_day);
printf("Date: %04d-%02d-%02d - Previous: %04d-%02d-%02d - DiM: %04d - DiY: %04d\n",
new_year, new_month, new_day,
year, month, day,
days_in_month, days_in_year);
if (new_year == year && new_month == month && new_day == day + 1)
{
// Most common case. Continue counting
if (days_in_year)
days_in_year++;
if (days_in_month)
days_in_month++;
}
else if (new_year == year && new_month == month + 1 && new_day == 1)
{
// Same year, start of the new month
if (days_in_month)
{
if (ctx->days_in_month(month, year) != days_in_month)
{
printf("Number of days in month is wrong! Date: %04d-%02d-%02d\n"
"Counted days since start of the month: %04d\n",
year, month, day, days_in_month);
}
// assert(ctx->days_in_month(month, year) == days_in_month);
}
days_in_month = 1;
if (days_in_year)
days_in_year++;
}
else if (new_year == year + 1 && new_month == 1 && new_day == 1)
{
// In case, we were counting before, check for the days in year and mount
if (days_in_year)
{
if (ctx->days_in_year(year) != days_in_year)
{
printf("Number of days in year is wrong! Date: %04d-%02d-%02d\n"
"Counted days since start of the year: %04d\n",
year, month, day, days_in_year);
}
assert(ctx->days_in_year(year) == days_in_year);
}
if (days_in_month)
{
if (ctx->days_in_month(month, year) != days_in_month)
{
printf("Number of days in month is wrong! Date: %04d-%02d-%02d\n"
"Counted days since start of the month: %04d\n",
year, month, day, days_in_month);
}
assert(ctx->days_in_month(month, year) == days_in_month);
}
// In case we were not counting, Start counting days in the year and month
days_in_year = 1;
days_in_month = 1;
}
else
{
// Should not happen
// abort();
}
year = new_year;
month = new_month;
day = new_day;
}
}
31 changes: 13 additions & 18 deletions tests/calendar-arithmetic.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,26 +22,21 @@
#include <stdint.h>
#include <stdio.h>

typedef struct _test_context
typedef struct test_context_t
{
void (*cal_to_jdn)(uint32_t*, int16_t, uint8_t, uint16_t);
void (*jdn_to_cal)(uint32_t, int16_t*, uint8_t*, uint16_t*);
void (*cal_to_gr)(int16_t, uint8_t, uint16_t, int16_t*, uint8_t*, uint16_t*);
void (*gr_to_cal)(int16_t, uint8_t, uint16_t, int16_t*, uint8_t*, uint16_t*);
void (*to_jdn)(uint32_t*, int16_t, uint8_t, uint16_t);
void (*from_jdn)(uint32_t, int16_t*, uint8_t*, uint16_t*);
void (*to_gr)(int16_t, uint8_t, uint16_t, int16_t*, uint8_t*, uint16_t*);
void (*from_gr)(int16_t, uint8_t, uint16_t, int16_t*, uint8_t*, uint16_t*);
void (*jdn_to_gr)(uint32_t, int16_t*, uint8_t*, uint16_t*);
uint8_t (*days_in_month)(uint8_t month, int16_t year);
uint16_t (*days_in_year)(int16_t year);
uint8_t (*month_in_year)(int16_t year);
uint8_t (*is_valid)(int16_t year, uint8_t month, uint16_t day);
uint32_t min_jd;
uint32_t max_jd;
} test_context;

void test_julian_day(
// void (*cal_to_jdn)(uint32_t*, int16_t, uint8_t, uint16_t),
// void (*jdn_to_cal)(uint32_t, int16_t*, uint8_t*, uint16_t*),
test_context* ctx,
uint32_t in_jd, uint32_t* out_jd);

void test_gregorian_calendar(
// void (*cal_to_gr)(int16_t, uint8_t, uint16_t, int16_t*, uint8_t*, uint16_t*),
// void (*gr_to_cal)(int16_t, uint8_t, uint16_t, int16_t*, uint8_t*, uint16_t*),
test_context* ctx,
uint32_t in_jd, int16_t*, uint8_t*, uint16_t*, int16_t*, uint8_t*, uint16_t*);

void test_continuity(test_context ctx, uint32_t jdn, uint8_t* valid);
void test_julian_day(const test_context* const ctx);
void test_gregorian_calendar(const test_context* const ctx);
void test_continuity(const test_context* const ctx);
Loading

0 comments on commit 022a393

Please sign in to comment.