From e798e1bb80b8e6ca4ef71dc6ccdea3a40a7da1d1 Mon Sep 17 00:00:00 2001 From: Don Cross Date: Tue, 21 May 2019 10:17:59 -0400 Subject: [PATCH] Work in progress: C moon phase unit test. --- generate/ctest.c | 66 ++++++++++++++++++++++++++++++++++++++++++-- generate/unit_test_c | 1 + 2 files changed, 65 insertions(+), 2 deletions(-) diff --git a/generate/ctest.c b/generate/ctest.c index 45bcffb8..1720ad92 100644 --- a/generate/ctest.c +++ b/generate/ctest.c @@ -42,6 +42,7 @@ static int AstroCheck(void); 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); int main(int argc, const char *argv[]) { @@ -56,12 +57,20 @@ int main(int argc, const char *argv[]) if (argc == 3) { - if (!strcmp(argv[1], "seasons")) + const char *verb = argv[1]; + const char *filename = argv[2]; + + if (!strcmp(verb, "seasons")) { - const char *filename = argv[2]; CHECK(SeasonsTest(filename)); goto success; } + + if (!strcmp(verb, "moonphase")) + { + CHECK(MoonPhase(filename)); + goto success; + } } if (argc == 4) @@ -479,3 +488,56 @@ fail: } /*-----------------------------------------------------------------------------------------------------------*/ + +static int MoonPhase(const char *filename) +{ + int error = 1; + FILE *infile = NULL; + int lnum, nscanned; + int quarter, year, month, day, hour, minute; + double second; + char line[200]; + + infile = fopen(filename, "rt"); + if (infile == NULL) + { + fprintf(stderr, "MoonPhase: Cannot open input file '%s'\n", filename); + error = 1; + goto fail; + } + + /* + 0 1800-01-25T03:21:00.000Z + 1 1800-02-01T20:40:00.000Z + 2 1800-02-09T17:26:00.000Z + 3 1800-02-16T15:49:00.000Z + */ + lnum = 0; + while (fgets(line, sizeof(line), infile)) + { + ++lnum; + nscanned = sscanf(line, "%d %d-%d-%dT%d:%d:%lfZ", &quarter, &year, &month, &day, &hour, &minute, &second); + if (nscanned != 7) + { + fprintf(stderr, "MoonPhase(%s line %d): Invalid data format\n", filename, lnum); + error = 1; + goto fail; + } + + if (quarter < 0 || quarter > 3) + { + fprintf(stderr, "MoonPhase(%s line %d): Invalid quarter %d\n", filename, lnum, quarter); + error = 1; + goto fail; + } + } + + printf("MoonPhase: passed %d lines for file %s\n", lnum, filename); + error = 0; + +fail: + if (infile != NULL) fclose(infile); + return error; +} + +/*-----------------------------------------------------------------------------------------------------------*/ diff --git a/generate/unit_test_c b/generate/unit_test_c index 9640bc1b..75688534 100755 --- a/generate/unit_test_c +++ b/generate/unit_test_c @@ -24,6 +24,7 @@ echo "$0: Built 'ctest' program." ./generate check temp/c_check.txt || Fail "Verification failure for C unit test output." ./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." echo "unit_test_c: success" exit 0