Skip to content

Commit

Permalink
Play with tests
Browse files Browse the repository at this point in the history
  • Loading branch information
soroush committed Dec 31, 2024
1 parent a5ddbe2 commit 717d792
Show file tree
Hide file tree
Showing 10 changed files with 349 additions and 20 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -438,4 +438,5 @@ __pycache__/

# End of https://www.gitignore.io/api/c,c++,linux,windows,autotools,visualstudio

build/*
build/*
out/*
11 changes: 9 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,14 @@ project(calendars
LANGUAGES C
)

enable_testing()
find_package(GTest CONFIG)
find_package(check CONFIG REQUIRED)

add_subdirectory(lib)
add_subdirectory(tests)

if(${GTest_FOUND})
enable_testing()
add_subdirectory(tests)
else()
message(STATUS "GTest library not found. Disabling the tests.")
endif()
21 changes: 19 additions & 2 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,33 @@ cmake_minimum_required(VERSION 3.16.0)

# Calendar tests

link_libraries(calendars)
enable_language(CXX)
enable_testing()

link_libraries(calendars GTest::gtest GTest::gtest_main $<IF:$<TARGET_EXISTS:Check::check>,Check::check,Check::checkShared>)

include_directories(PUBLIC $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/../include/>)

file(GLOB_RECURSE ALL_SOURCES "*.c")

set_property(SOURCE ${ALL_SOURCES} PROPERTY LANGUAGE CXX)

add_executable(tst_gregorian tst-common.c tst-common.h tst-gregorian.c)
add_executable(tst_milankovic tst-common.c tst-common.h tst-milankovic.c)
add_executable(tst_julian tst-common.c tst-common.h tst-julian.c)
add_executable(tst_solar_hijri tst-common.c tst-common.h tst-solar-hijri.c)
add_executable(tst_jewish tst-common.c tst-common.h tst-jewish.c)
add_executable(tst_islamic_civil tst-common.c tst-common.h tst-islamic-civil.c)

# add_executable(tst_solar_hijri
# calendar-arithmetic.cpp
# tst-solar-hijri.cpp
# )

add_executable(tst_solar_hijri
tst-solar-hijri.c
calendar-arithmetic.c
)

set_target_properties(tst_gregorian tst_julian tst_milankovic tst_solar_hijri tst_jewish tst_islamic_civil
PROPERTIES
C_STANDARD 90
Expand All @@ -41,6 +57,7 @@ add_test(NAME "Gregorian" COMMAND tst_gregorian WORKING_DIRECTORY ${CMAKE_BINARY
add_test(NAME "Julian" COMMAND tst_julian WORKING_DIRECTORY ${CMAKE_BINARY_DIR})
add_test(NAME "Milankovic" COMMAND tst_milankovic WORKING_DIRECTORY ${CMAKE_BINARY_DIR})
add_test(NAME "SolarHijri" COMMAND tst_solar_hijri WORKING_DIRECTORY ${CMAKE_BINARY_DIR})
# add_test(NAME "SolarHijri_C" COMMAND tst_solar_hijri_c WORKING_DIRECTORY ${CMAKE_BINARY_DIR})
add_test(NAME "Jewish" COMMAND tst_jewish WORKING_DIRECTORY ${CMAKE_BINARY_DIR})
add_test(NAME "IslamicCivil" COMMAND tst_islamic_civil WORKING_DIRECTORY ${CMAKE_BINARY_DIR})

Expand Down
48 changes: 48 additions & 0 deletions tests/calendar-arithmetic.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
* Copyright (C) 2021-2025 - Soroush Rabiei, <[email protected]>
* This file is part of libcalendar.
*
* libcalendar is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* libcalendar is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with libcalendar. If not, see <http://www.gnu.org/licenses/>.
*
*/

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

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*),
uint32_t in_jd, uint32_t* out_jd)
{
int16_t year;
uint8_t month;
uint16_t day;
(*jdn_to_cal)(in_jd, &year, &month, &day);
(*cal_to_jdn)(out_jd, year, month, day);
}

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*),
uint32_t in_jd, int16_t* gyi, uint8_t* gmi, uint16_t* gdi, int16_t* gyo, uint8_t* gmo, uint16_t* gdo)
{
int16_t year;
uint8_t month;
uint16_t day;
jdn_to_gr(in_jd, gyi, gmi, gdi);
(*gr_to_cal)(*gyi, *gmi, *gdi, &year, &month, &day);
(*cal_to_gr)(year, month, day, gyo, gmo, gdo);
}
75 changes: 75 additions & 0 deletions tests/calendar-arithmetic.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
/*
* Copyright (C) 2021-2025 - Soroush Rabiei, <[email protected]>
* This file is part of libcalendar.
*
* libcalendar is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* libcalendar is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with libcalendar. If not, see <http://www.gnu.org/licenses/>.
*
*/

#include "calendar-arithmetic.hpp"

CalendarArithmetic::CalendarArithmetic()
: ::testing::Test()
, min_jdn(0)
, max_jdn(0)
{
}

void CalendarArithmetic::SetRange(uint32_t min_jdn_, uint32_t max_jdn_)
{
this->min_jdn = min_jdn_;
this->max_jdn = max_jdn_;
}

void CalendarArithmetic::JulianDay(CalendarArithmetic::jdn_to_calendar to_calendar, CalendarArithmetic::calendar_to_jdn to_jdn)
{
ASSERT_NE(to_calendar, nullptr) << "`to_calendar` function not set.";
ASSERT_NE(to_jdn, nullptr) << "`to_jdn` function not set.";
uint32_t jdn = 0;
uint32_t jdn2 = 0;
for (jdn = min_jdn; jdn < max_jdn; ++jdn)
{
int16_t year;
uint8_t month;
uint16_t day;
to_calendar(jdn, &year, &month, &day);
to_jdn(&jdn2, year, month, day);
ASSERT_EQ(jdn2, jdn);
}
}

void CalendarArithmetic::Gregorian(CalendarArithmetic::jdn_to_calendar to_calendar,
CalendarArithmetic::calendar_to_calendar cal_to_gr,
CalendarArithmetic::calendar_to_calendar gr_to_cal)
{
uint32_t jdn = 0;
uint32_t jdn2 = 0;
for (jdn = min_jdn; jdn < max_jdn; ++jdn)
{
int16_t gy[2] = {0, 0};
uint8_t gm[2] = {0, 0};
uint16_t gd[2] = {0, 0};
int16_t year;
uint8_t month;
uint16_t day;

to_calendar(jdn, &gy[0], &gm[0], &gd[0]);
gr_to_cal(gy[0], gm[0], gd[0], &year, &month, &day);
cal_to_gr(year, month, day, &gy[1], &gm[1], &gd[1]);

ASSERT_EQ(gy[0], gy[1]);
ASSERT_EQ(gm[0], gm[1]);
ASSERT_EQ(gd[0], gd[1]);
}
}
31 changes: 31 additions & 0 deletions tests/calendar-arithmetic.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* Copyright (C) 2021-2025 - Soroush Rabiei, <[email protected]>
* This file is part of libcalendar.
*
* libcalendar is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* libcalendar is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with libcalendar. If not, see <http://www.gnu.org/licenses/>.
*
*/

#include <stdint.h>
#include <stdio.h>

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*),
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*),
uint32_t in_jd, int16_t*, uint8_t*, uint16_t*, int16_t*, uint8_t*, uint16_t*);
40 changes: 40 additions & 0 deletions tests/calendar-arithmetic.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* Copyright (C) 2021-2025 - Soroush Rabiei, <[email protected]>
* This file is part of libcalendar.
*
* libcalendar is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* libcalendar is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with libcalendar. If not, see <http://www.gnu.org/licenses/>.
*
*/

#pragma once

#include <gtest/gtest.h>
#include <functional>

class CalendarArithmetic : public ::testing::Test {
protected:
using jdn_to_calendar = std::function<void(uint32_t, int16_t*, uint8_t*, uint16_t*)>;
using calendar_to_jdn = std::function<void(uint32_t*, int16_t, uint8_t, uint16_t)>;
using calendar_to_calendar = std::function<void(int16_t, uint8_t, uint16_t, int16_t*, uint8_t*, uint16_t*)>;

CalendarArithmetic();

void SetRange(uint32_t min_jdn, uint32_t max_jdn);
void JulianDay(jdn_to_calendar, calendar_to_jdn);
void Gregorian(jdn_to_calendar, calendar_to_calendar, calendar_to_calendar);

private:
uint32_t min_jdn;
uint32_t max_jdn;
};
25 changes: 18 additions & 7 deletions tests/tst-gregorian.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,12 @@
*/

#include <libcalendars/cl-gregorian.h>
#include "tst-common.h"
#include <gtest/gtest.h>

#include <stdio.h>

// #include "tst-common.h"

/* Because it makes no sense to convert dates from Gregorian to Gregorian,
* we provide dummy funvtions for tests.
*/
Expand All @@ -38,10 +41,18 @@ void gr_to_gr2(int16_t gyear, uint8_t gmonth, uint16_t gday,
*cday = gday;
}

int main(int argc, char *argv[]) {
int result;
printf("TEST: Gregorian Calendar...\n");
result = tst_calendar(&gr_to_jdn, &jdn_to_gr, &gr_to_gr1, &gr_to_gr2, 0, 2488069);
printf("%s: Gregorian Calendar.\n", result == 0 ? "PASS" : "FAIL");
return result;
// int main(int argc, char *argv[]) {
// int result;
// printf("TEST: Gregorian Calendar...\n");
// result = tst_calendar(&gr_to_jdn, &jdn_to_gr, &gr_to_gr1, &gr_to_gr2, 0, 2488069);
// printf("%s: Gregorian Calendar.\n", result == 0 ? "PASS" : "FAIL");
// return result;
// }

TEST(Gregorian, ToJDN)
{
}

TEST(Gregorian, FromJDN)
{
}
81 changes: 73 additions & 8 deletions tests/tst-solar-hijri.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,77 @@
*/

#include <libcalendars/cl-solar-hijri.h>
#include "tst-common.h"

int main(int argc, char *argv[]) {
int result;
printf("TEST: Solar Hijri Calendar...\n");
result = tst_calendar(&sh_to_jdn, &jdn_to_sh, &sh_to_gr, &gr_to_sh, 0, 2488069);
printf("%s: Solar Hijri Calendar.\n", result == 0 ? "PASS" : "FAIL");
return result;
#include <libcalendars/cl-gregorian.h>
#include <check.h>

#include "calendar-arithmetic.h"

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

START_TEST(solar_hijri_jdn)
{
uint32_t jd = 0;
uint32_t out_jd = 0;
for (jd = 0; jd < 2488069; jd++)
{
test_julian_day(&sh_to_jdn, &jdn_to_sh, jd, &out_jd);
assert(jd == out_jd);
}
}
END_TEST

START_TEST(solar_hijri_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(&sh_to_gr, &gr_to_sh, 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* solar_hijri;
TCase* islamic;

suit = suite_create("Calendar Arithmetic");

solar_hijri = tcase_create("Solar Hijri");
tcase_add_test(solar_hijri, solar_hijri_jdn);
tcase_add_test(solar_hijri, solar_hijri_gregorian);

suite_add_tcase(suit, solar_hijri);
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;
}
Loading

0 comments on commit 717d792

Please sign in to comment.