From daf2dddcceee2222e88c2816e8b060db4e3d0357 Mon Sep 17 00:00:00 2001 From: Don Cross Date: Tue, 21 May 2019 15:20:18 -0400 Subject: [PATCH] Validated C function SearchRelativeLongitude. --- generate/ctest.c | 111 ++++++++++++++++++++++++++++++++++ generate/template/astronomy.c | 1 - generate/unit_test_c | 1 + source/c/astronomy.h | 1 + 4 files changed, 113 insertions(+), 1 deletion(-) diff --git a/generate/ctest.c b/generate/ctest.c index 3a8c7ef6..416397d4 100644 --- a/generate/ctest.c +++ b/generate/ctest.c @@ -43,6 +43,8 @@ static int Diff(const char *c_filename, const char *js_filename); static int DiffLine(int lnum, const char *cline, const char *jline, double *maxdiff, int *worst_lnum); static int SeasonsTest(const char *filename); static int MoonPhase(const char *filename); +static int ElongationTest(void); +static astro_body_t ParseBodyName(const char *name); int main(int argc, const char *argv[]) { @@ -55,6 +57,16 @@ int main(int argc, const char *argv[]) goto success; } + if (argc == 2) + { + const char *verb = argv[1]; + if (!strcmp(verb, "elongation")) + { + CHECK(ElongationTest()); + goto success; + } + } + if (argc == 3) { const char *verb = argv[1]; @@ -615,3 +627,102 @@ fail: } /*-----------------------------------------------------------------------------------------------------------*/ + +static astro_body_t ParseBodyName(const char *name) +{ + if (!strcmp(name, "Mercury")) return BODY_MERCURY; + if (!strcmp(name, "Venus")) return BODY_VENUS; + if (!strcmp(name, "Earth")) return BODY_EARTH; + if (!strcmp(name, "Mars")) return BODY_MARS; + if (!strcmp(name, "Jupiter")) return BODY_JUPITER; + if (!strcmp(name, "Saturn")) return BODY_SATURN; + if (!strcmp(name, "Uranus")) return BODY_URANUS; + if (!strcmp(name, "Neptune")) return BODY_NEPTUNE; + if (!strcmp(name, "Pluto")) return BODY_PLUTO; + if (!strcmp(name, "Sun")) return BODY_SUN; + if (!strcmp(name, "Moon")) return BODY_MOON; + return BODY_INVALID; +} + +static int TestElongFile(const char *filename, double targetRelLon) +{ + int error = 1; + FILE *infile = NULL; + int lnum; + char line[100]; + char name[20]; + int year, month, day, hour, minute; + int nscanned; + astro_time_t search_date, expected_time; + astro_body_t body; + astro_search_result_t search_result; + double diff_minutes; + + infile = fopen(filename, "rt"); + if (infile == NULL) + { + fprintf(stderr, "TestElongFile: Cannot open input file: %s\n", filename); + error = 1; + goto fail; + } + + lnum = 0; + while (fgets(line, sizeof(line), infile)) + { + /* 2018-05-09T00:28Z Jupiter */ + nscanned = sscanf(line, "%d-%d-%dT%d:%dZ %9[A-Za-z]", &year, &month, &day, &hour, &minute, name); + if (nscanned != 6) + { + fprintf(stderr, "TestElongFile(%s line %d): Invalid data format.\n", filename, lnum); + error = 1; + goto fail; + } + + body = ParseBodyName(name); + if (body == BODY_INVALID) + { + fprintf(stderr, "TestElongFile(%s line %d): Invalid body name '%s'\n", filename, lnum, name); + error = 1; + goto fail; + } + + search_date = Astronomy_MakeTime(year, 1, 1, 0, 0, 0.0); + expected_time = Astronomy_MakeTime(year, month, day, hour, minute, 0.0); + search_result = Astronomy_SearchRelativeLongitude(body, targetRelLon, search_date); + if (search_result.status != ASTRO_SUCCESS) + { + fprintf(stderr, "TestElongFile(%s line %d): SearchRelativeLongitude returned %d\n", filename, lnum, search_result.status); + error = 1; + goto fail; + } + + diff_minutes = (24.0 * 60.0) * (search_result.time.tt - expected_time.tt); + printf("TestElongFile: %-7s error = %6.3lf minutes, iterations = %3d\n", name, diff_minutes, search_result.iter); + if (fabs(diff_minutes) > 15.0) + { + fprintf(stderr, "TestElongFile(%s line %d): EXCESSIVE ERROR\n", filename, lnum); + error = 1; + goto fail; + } + } + + printf("TestElongFile: passed %d rows of data\n", lnum); + error = 0; + +fail: + if (infile != NULL) fclose(infile); + return error; +} + +static int ElongationTest(void) +{ + int error; + + CHECK(TestElongFile("longitude/opposition_2018.txt", 0.0)); + +fail: + return error; +} + +/*-----------------------------------------------------------------------------------------------------------*/ + diff --git a/generate/template/astronomy.c b/generate/template/astronomy.c index 1a75402b..3852e05f 100644 --- a/generate/template/astronomy.c +++ b/generate/template/astronomy.c @@ -2447,6 +2447,5 @@ astro_search_result_t Astronomy_SearchRelativeLongitude(astro_body_t body, doubl X SearchLunarApsis - SearchMaxElongation X SearchPeakMagnitude - - SearchRelativeLongitude X SearchRiseSet */ diff --git a/generate/unit_test_c b/generate/unit_test_c index 75688534..591b7308 100755 --- a/generate/unit_test_c +++ b/generate/unit_test_c @@ -25,6 +25,7 @@ echo "$0: Built 'ctest' program." ./ctest diff temp/c_check.txt temp/js_check.txt || Fail "Diff(C,JS) failure." ./ctest seasons seasons/seasons.txt || Fail "Failed C seasons test." ./ctest moonphase moonphase/moonphases.txt || Fail "Failed C moon phase test." +./ctest elongation || Fail "Failed C elongation tests." echo "unit_test_c: success" exit 0 diff --git a/source/c/astronomy.h b/source/c/astronomy.h index 6474e6b1..9117147f 100644 --- a/source/c/astronomy.h +++ b/source/c/astronomy.h @@ -75,6 +75,7 @@ astro_angle_result_t; typedef enum { + BODY_INVALID = -1, BODY_MERCURY, BODY_VENUS, BODY_EARTH,