Skip to content

Commit

Permalink
naif.c: Tests and coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
attipaci committed Nov 8, 2024
1 parent f4d36ef commit 9bfe3d2
Show file tree
Hide file tree
Showing 7 changed files with 153 additions and 32 deletions.
8 changes: 0 additions & 8 deletions include/novas.h
Original file line number Diff line number Diff line change
Expand Up @@ -1299,14 +1299,6 @@ double novas_z_add(double z1, double z2);

double novas_z_inv(double z);

// in naif.c

enum novas_planet naif_to_novas_planet(long id);

long novas_to_naif_planet(enum novas_planet id);




// <================= END of SuperNOVAS API =====================>

Expand Down
11 changes: 11 additions & 0 deletions include/solarsystem.h
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,14 @@ novas_planet_provider get_planet_provider();

novas_planet_provider_hp get_planet_provider_hp();

// in naif.c
enum novas_planet naif_to_novas_planet(long id);

long novas_to_naif_planet(enum novas_planet id);

long novas_to_dexxx_planet(enum novas_planet id);

// in solsys-calceph.c
#if USE_CALCEPH
# include "calceph.h"

Expand All @@ -314,6 +322,9 @@ int novas_use_calceph_planets(t_calcephbin *eph);

#endif /* USE_CALCEPH */




/// \cond PRIVATE


Expand Down
53 changes: 32 additions & 21 deletions src/naif.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

#include <errno.h>

#define __NOVAS_INTERNAL_API__ ///< Use definitions meant for internal use by SuperNOVAS only
#include "novas.h"
#include "naif.h"

Expand All @@ -36,20 +37,23 @@
*/
enum novas_planet naif_to_novas_planet(long id) {
switch (id) {
case NAIF_SUN: return NOVAS_SUN;
case NAIF_MOON: return NOVAS_MOON;
case NAIF_SSB: return NOVAS_SSB;
case NAIF_SUN:
return NOVAS_SUN;
case NAIF_MOON:
return NOVAS_MOON;
case NAIF_SSB:
return NOVAS_SSB;
case NAIF_EMB:
errno = EINVAL;
return -1;
return novas_error(-1, EINVAL, "naif_to_novas_planet", "No NOVAS ID for EMB (NAIF=%ld)", NAIF_EMB);
}

// Major planets
if(id >= NOVAS_MERCURY && id <= NOVAS_PLUTO) return id;
if(id > 100 && id < 1000) if(id % 100 == 99) return (id - 99)/100;
if(id >= NOVAS_MERCURY && id <= NOVAS_PLUTO)
return id;
if(id > 100 && id < 1000) if(id % 100 == 99)
return (id - 99)/100;

errno = ENOENT;
return -1;
return novas_error(-1, EINVAL, "naif_to_novas_planet", "Invalid NOVAS major planet no: %ld", id);
}

/**
Expand All @@ -69,16 +73,19 @@ enum novas_planet naif_to_novas_planet(long id) {
* @since 1.2
*/
long novas_to_naif_planet(enum novas_planet id) {
if(id >= NOVAS_MERCURY && id <= NOVAS_PLUTO) return 100 * id + 99; // 1-9: Major planets
if(id >= NOVAS_MERCURY && id <= NOVAS_PLUTO)
return 100 * id + 99; // 1-9: Major planets

switch (id) {
case NOVAS_SUN: return NAIF_SUN;
case NOVAS_MOON: return NAIF_MOON;
case NOVAS_SSB: return NAIF_SSB;
case NOVAS_SUN:
return NAIF_SUN;
case NOVAS_MOON:
return NAIF_MOON;
case NOVAS_SSB:
return NAIF_SSB;
default:
return novas_error(-1, EINVAL, "novas_to_naif_planet", "Invalid NOVAS major planet no: %d", id);
}

return id;
}

/**
Expand All @@ -100,13 +107,17 @@ long novas_to_naif_planet(enum novas_planet id) {
long novas_to_dexxx_planet(enum novas_planet id) {

switch (id) {
case NOVAS_SUN: return NAIF_SUN;
case NOVAS_EARTH: return NAIF_EARTH;
case NOVAS_MOON: return NAIF_MOON;
case NOVAS_SSB: return NAIF_SSB;
case NOVAS_SUN:
return NAIF_SUN;
case NOVAS_EARTH:
return NAIF_EARTH;
case NOVAS_MOON:
return NAIF_MOON;
case NOVAS_SSB:
return NAIF_SSB;
default:
return (id >= NOVAS_MERCURY && id <= NOVAS_PLUTO) ?
id : novas_error(-1, EINVAL, "novas_to_dexxx_planet", "Invalid NOVAS major planet no: %d", id);
}

return id;
}

6 changes: 4 additions & 2 deletions src/solsys-calceph.c
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,8 @@ static int novas_calceph(const char *name, long id, double jd_tdb_high, double j
int novas_use_calceph(t_calcephbin *eph) {
static const char *fn = "novas_use_calceph";

if(!eph) return novas_error(-1, EINVAL, fn, "input ephemeris data is NULL");
if(!eph)
return novas_error(-1, EINVAL, fn, "input ephemeris data is NULL");

if(!calceph_prefetch(eph))
return novas_error(-1, EAGAIN, fn, "calceph_prefetch() failed");
Expand Down Expand Up @@ -346,7 +347,8 @@ int novas_use_calceph(t_calcephbin *eph) {
int novas_use_calceph_planets(t_calcephbin *eph) {
static const char *fn = "novas_use_calceph_planets";

if(!eph) return novas_error(-1, EINVAL, fn, "input ephemeris data is NULL");
if(!eph)
return novas_error(-1, EINVAL, fn, "input ephemeris data is NULL");

if(!calceph_prefetch(eph))
return novas_error(-1, EAGAIN, fn, "calceph_prefetch() failed");
Expand Down
2 changes: 1 addition & 1 deletion test/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ include ../config.mk
#OBJECTS := $(subst obj/,,$(OBJECTS))
OBJECTS := novas.o nutation.o solsys3.o super.o frames.o timescale.o refract.o naif.o
TESTS := test-compat test-cio_file test-super test-errors
COVFILES := novas.c.gcov nutation.c.gcov solsys3.c.gcov super.c.gcov timescale.c.gcov refract.c.gcov frames.c.gcov
COVFILES := novas.c.gcov nutation.c.gcov solsys3.c.gcov super.c.gcov timescale.c.gcov refract.c.gcov frames.c.gcov naif.c.gcov

ifeq ($(CALCEPH_SUPPORT), 1)
CPPFLAGS += -DUSE_CALCEPH=1
Expand Down
40 changes: 40 additions & 0 deletions test/src/test-errors.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include <string.h>

#include "novas.h"
#include "naif.h"

static int check(const char *func, int exp, int error) {
if(error != exp) {
Expand Down Expand Up @@ -1442,6 +1443,41 @@ static int test_z_inv() {
return n;
}

static int test_novas_to_naif_planet() {
int n = 0;

if(check("novas_to_naif_planet:-2", -1, novas_to_naif_planet(-2))) n++;
if(check("novas_to_naif_planet:-1", -1, novas_to_naif_planet(-1))) n++;
if(check("novas_to_naif_planet:hi", -1, novas_to_naif_planet(NOVAS_PLANETS))) n++;

return n;
}

static int test_novas_to_dexxx_planet() {
int n = 0;

if(check("novas_to_dexxx_planet:-2", -1, novas_to_dexxx_planet(-2))) n++;
if(check("novas_to_dexxx_planet:-1", -1, novas_to_dexxx_planet(-1))) n++;
if(check("novas_to_dexxx_planet:hi", -1, novas_to_dexxx_planet(NOVAS_PLANETS))) n++;

return n;
}

static int test_naif_to_novas_planet() {
int n = 0;

if(check("naif_to_novas_planet:-2", -1, naif_to_novas_planet(-1))) n++;
if(check("naif_to_novas_planet:-1", -1, naif_to_novas_planet(-1))) n++;
if(check("naif_to_novas_planet:", -1, naif_to_novas_planet(-1))) n++;
if(check("naif_to_novas_planet:emb", -1, naif_to_novas_planet(NAIF_EMB))) n++;
if(check("naif_to_novas_planet:500", -1, naif_to_novas_planet(500))) n++;
if(check("naif_to_novas_planet:501", -1, naif_to_novas_planet(501))) n++;
if(check("naif_to_novas_planet:598", -1, naif_to_novas_planet(598))) n++;
if(check("naif_to_novas_planet:1000", -1, naif_to_novas_planet(1000))) n++;

return n;
}

int main() {
int n = 0;

Expand Down Expand Up @@ -1566,6 +1602,10 @@ int main() {
if(test_z_add()) n++;
if(test_z_inv()) n++;

if(test_novas_to_naif_planet()) n++;
if(test_novas_to_dexxx_planet()) n++;
if(test_naif_to_novas_planet()) n++;

if(n) fprintf(stderr, " -- FAILED %d tests\n", n);
else fprintf(stderr, " -- OK\n");

Expand Down
65 changes: 65 additions & 0 deletions test/src/test-super.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@

#define __NOVAS_INTERNAL_API__ ///< Use definitions meant for internal use by SuperNOVAS only
#include "novas.h"
#include "naif.h"

#define J2000 2451545.0
#define DAY 86400.0
Expand Down Expand Up @@ -2130,7 +2131,68 @@ static int test_z_inv() {
return n;
}

static int test_novas_to_naif_planet() {

if(!is_ok("novas_to_naif_planet:ssb", novas_to_naif_planet(NOVAS_SSB) != NAIF_SSB)) return 1;
if(!is_ok("novas_to_naif_planet:sun", novas_to_naif_planet(NOVAS_SUN) != NAIF_SUN)) return 1;
if(!is_ok("novas_to_naif_planet:moon", novas_to_naif_planet(NOVAS_MOON) != NAIF_MOON)) return 1;
if(!is_ok("novas_to_naif_planet:earth", novas_to_naif_planet(NOVAS_EARTH) != NAIF_EARTH)) return 1;
if(!is_ok("novas_to_naif_planet:mercury", novas_to_naif_planet(NOVAS_MERCURY) != 199)) return 1;
if(!is_ok("novas_to_naif_planet:venus", novas_to_naif_planet(NOVAS_VENUS) != 299)) return 1;
if(!is_ok("novas_to_naif_planet:mars", novas_to_naif_planet(NOVAS_MARS) != 499)) return 1;
if(!is_ok("novas_to_naif_planet:jupiter", novas_to_naif_planet(NOVAS_JUPITER) != 599)) return 1;
if(!is_ok("novas_to_naif_planet:saturn", novas_to_naif_planet(NOVAS_SATURN) != 699)) return 1;
if(!is_ok("novas_to_naif_planet:uranus", novas_to_naif_planet(NOVAS_URANUS) != 799)) return 1;
if(!is_ok("novas_to_naif_planet:neptune", novas_to_naif_planet(NOVAS_NEPTUNE) != 899)) return 1;
if(!is_ok("novas_to_naif_planet:pluto", novas_to_naif_planet(NOVAS_PLUTO) != 999)) return 1;

return 0;
}

static int test_novas_to_dexxx_planet() {

if(!is_ok("novas_to_dexxx_planet:ssb", novas_to_dexxx_planet(NOVAS_SSB) != NAIF_SSB)) return 1;
if(!is_ok("novas_to_dexxx_planet:sun", novas_to_dexxx_planet(NOVAS_SUN) != NAIF_SUN)) return 1;
if(!is_ok("novas_to_dexxx_planet:moon", novas_to_dexxx_planet(NOVAS_MOON) != NAIF_MOON)) return 1;
if(!is_ok("novas_to_dexxx_planet:earth", novas_to_dexxx_planet(NOVAS_EARTH) != NAIF_EARTH)) return 1;
if(!is_ok("novas_to_dexxx_planet:mercury", novas_to_dexxx_planet(NOVAS_MERCURY) != 1)) return 1;
if(!is_ok("novas_to_dexxx_planet:venus", novas_to_dexxx_planet(NOVAS_VENUS) != 2)) return 1;
if(!is_ok("novas_to_dexxx_planet:mars", novas_to_dexxx_planet(NOVAS_MARS) != 4)) return 1;
if(!is_ok("novas_to_dexxx_planet:jupiter", novas_to_dexxx_planet(NOVAS_JUPITER) != 5)) return 1;
if(!is_ok("novas_to_dexxx_planet:saturn", novas_to_dexxx_planet(NOVAS_SATURN) != 6)) return 1;
if(!is_ok("novas_to_dexxx_planet:uranus", novas_to_dexxx_planet(NOVAS_URANUS) != 7)) return 1;
if(!is_ok("novas_to_dexxx_planet:neptune", novas_to_dexxx_planet(NOVAS_NEPTUNE) != 8)) return 1;
if(!is_ok("novas_to_dexxx_planet:pluto", novas_to_dexxx_planet(NOVAS_PLUTO) != 9)) return 1;

return 0;
}

static int test_naif_to_novas_planet() {

if(!is_ok("naif_to_novas_planet:ssb", naif_to_novas_planet(NAIF_SSB) != NOVAS_SSB)) return 1;
if(!is_ok("naif_to_novas_planet:sun", naif_to_novas_planet(NAIF_SUN) != NOVAS_SUN)) return 1;
if(!is_ok("naif_to_novas_planet:moon", naif_to_novas_planet(NAIF_MOON) != NOVAS_MOON)) return 1;
if(!is_ok("naif_to_novas_planet:earth", naif_to_novas_planet(NAIF_EARTH) != NOVAS_EARTH)) return 1;
if(!is_ok("naif_to_novas_planet:mercury", naif_to_novas_planet(199) != NOVAS_MERCURY)) return 1;
if(!is_ok("naif_to_novas_planet:venus", naif_to_novas_planet(299) != NOVAS_VENUS)) return 1;
if(!is_ok("naif_to_novas_planet:mars", naif_to_novas_planet(499) != NOVAS_MARS)) return 1;
if(!is_ok("naif_to_novas_planet:jupiter", naif_to_novas_planet(599) != NOVAS_JUPITER)) return 1;
if(!is_ok("naif_to_novas_planet:saturn", naif_to_novas_planet(699) != NOVAS_SATURN)) return 1;
if(!is_ok("naif_to_novas_planet:uranus", naif_to_novas_planet(799) != NOVAS_URANUS)) return 1;
if(!is_ok("naif_to_novas_planet:neptune", naif_to_novas_planet(899) != NOVAS_NEPTUNE)) return 1;
if(!is_ok("naif_to_novas_planet:pluto", naif_to_novas_planet(999) != NOVAS_PLUTO)) return 1;
if(!is_ok("naif_to_novas_planet:mercury", naif_to_novas_planet(1) != NOVAS_MERCURY)) return 1;
if(!is_ok("naif_to_novas_planet:venus", naif_to_novas_planet(2) != NOVAS_VENUS)) return 1;
if(!is_ok("naif_to_novas_planet:emb", naif_to_novas_planet(3) != -1)) return 1;
if(!is_ok("naif_to_novas_planet:mars", naif_to_novas_planet(4) != NOVAS_MARS)) return 1;
if(!is_ok("naif_to_novas_planet:jupiter", naif_to_novas_planet(5) != NOVAS_JUPITER)) return 1;
if(!is_ok("naif_to_novas_planet:saturn", naif_to_novas_planet(6) != NOVAS_SATURN)) return 1;
if(!is_ok("naif_to_novas_planet:uranus", naif_to_novas_planet(7) != NOVAS_URANUS)) return 1;
if(!is_ok("naif_to_novas_planet:neptune", naif_to_novas_planet(8) != NOVAS_NEPTUNE)) return 1;
if(!is_ok("naif_to_novas_planet:pluto", naif_to_novas_planet(9) != NOVAS_PLUTO)) return 1;

return 0;
}

int main(int argc, char *argv[]) {
int n = 0;
Expand Down Expand Up @@ -2191,6 +2253,9 @@ int main(int argc, char *argv[]) {
if(test_redshift_vrad()) n++;
if(test_grav_redshift()) n++;

if(test_novas_to_naif_planet()) n++;
if(test_novas_to_dexxx_planet()) n++;
if(test_naif_to_novas_planet()) n++;

n += test_dates();

Expand Down

0 comments on commit 9bfe3d2

Please sign in to comment.