Work in progress: C moon phase unit test.

This commit is contained in:
Don Cross
2019-05-21 10:17:59 -04:00
parent 4513036af0
commit e798e1bb80
2 changed files with 65 additions and 2 deletions

View File

@@ -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;
}
/*-----------------------------------------------------------------------------------------------------------*/

View File

@@ -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