diff options
author | Mattias Andrée <m@maandree.se> | 2025-03-25 21:47:16 +0100 |
---|---|---|
committer | Mattias Andrée <m@maandree.se> | 2025-03-25 21:47:16 +0100 |
commit | 024cf2f59a7ee23f46140932b7b617f1f3fc1508 (patch) | |
tree | 0a2203ffa897b652f75eaefa14ea066774b63b55 | |
parent | readme: list optional dependencies + update todo (diff) | |
download | libgeome-024cf2f59a7ee23f46140932b7b617f1f3fc1508.tar.gz libgeome-024cf2f59a7ee23f46140932b7b617f1f3fc1508.tar.bz2 libgeome-024cf2f59a7ee23f46140932b7b617f1f3fc1508.tar.xz |
Add tests and some fixes1.0
Signed-off-by: Mattias Andrée <m@maandree.se>
-rw-r--r-- | .gitignore | 2 | ||||
-rw-r--r-- | Makefile | 29 | ||||
-rw-r--r-- | README | 4 | ||||
-rw-r--r-- | libgeome.h | 17 | ||||
-rw-r--r-- | libgeome_get_from_command.c | 8 | ||||
-rw-r--r-- | libgeome_netservices.c | 2 | ||||
-rw-r--r-- | test-command.c | 24 | ||||
-rw-r--r-- | test-common.c | 33 | ||||
-rw-r--r-- | test-common.h | 7 | ||||
-rw-r--r-- | test-file.c | 21 | ||||
-rw-r--r-- | test-netservice.c | 33 | ||||
-rw-r--r-- | test-time.c | 21 | ||||
-rw-r--r-- | test-timezone.c | 21 |
13 files changed, 211 insertions, 11 deletions
@@ -12,3 +12,5 @@ *.gcov *.gcno *.gcda +*.t +*.to @@ -29,12 +29,29 @@ HDR =\ libgeome.h\ common.h +TEST =\ + test-time.t\ + test-timezone.t\ + test-file.t\ + test-command.t\ + test-netservice.t + +TEST_OBJ =\ + test-common.to\ + $(TEST:.t=.to) + +TEST_HDR =\ + libgeome.h\ + test-common.h + LOBJ = $(OBJ:.o=.lo) -all: libgeome.a libgeome.$(LIBEXT) +all: libgeome.a libgeome.$(LIBEXT) $(TEST) $(OBJ): $(HDR) $(LOBJ): $(HDR) +$(TEST): test-common.to libgeome.a +$(TEST_OBJ): $(TEST_HDR) .c.o: $(CC) -c -o $@ $< $(CFLAGS) $(CPPFLAGS) @@ -42,6 +59,12 @@ $(LOBJ): $(HDR) .c.lo: $(CC) -fPIC -c -o $@ $< $(CFLAGS) $(CPPFLAGS) +.c.to: + $(CC) -c -o $@ $< $(CFLAGS) $(CPPFLAGS) + +.to.t: + $(CC) -o $@ $< test-common.to libgeome.a $(CFLAGS) $(CPPFLAGS) -lm + libgeome.a: $(OBJ) @rm -f -- $@ $(AR) rc $@ $(OBJ) @@ -68,10 +91,10 @@ uninstall: -rm -f -- "$(DESTDIR)$(PREFIX)/include/libgeome.h" clean: - -rm -f -- *.o *.a *.lo *.su *.so *.so.* *.dll *.dylib + -rm -f -- *.o *.a *.lo *.su *.so *.so.* *.dll *.dylib *.t *.to -rm -f -- *.gch *.gcov *.gcno *.gcda *.$(LIBEXT) .SUFFIXES: -.SUFFIXES: .lo .o .c +.SUFFIXES: .lo .o .c .t .to .PHONY: all install uninstall clean @@ -8,6 +8,8 @@ SYNOPSIS #define LIBGEOME_DATUM_LONGITUDE /* omitted */ #define LIBGEOME_DATUM_ALTITUDE /* omitted */ + #define LIBGEOME_LAST_DATUM /* whatever the datum represented with the highest bit is */ + struct libgeome_data { uint64_t requested_data; double latitude; @@ -37,7 +39,7 @@ SYNOPSIS const char *const *args; }; - extern struct libgeome_netservice libgeome_netservices[]; + extern const struct libgeome_netservice libgeome_netservices[]; extern const size_t libgeome_netservices_count; void libgeome_basic_context(struct libgeome_context *, const char *); @@ -10,6 +10,19 @@ #define LIBGEOME_DATUM_LONGITUDE UINT64_C(0x0000000000000002) #define LIBGEOME_DATUM_ALTITUDE UINT64_C(0x0000000000000004) +/** + * The highest bit in `.requested_data` that is used + * in the applications version of `struct libgeome_data` + * + * You can an #if'ed CPP warning to detected when this + * value has been changed, so you can add support for + * new data in your application. When you do this, you + * can also always set the `.requested_data` to + * `LIBGEOME_LAST_DATUM - 1U | LIBGEOME_LAST_DATUM` + * to get all data your application support. + */ +#define LIBGEOME_LAST_DATUM LIBGEOME_DATUM_ALTITUDE + /** * Geolocation data @@ -96,7 +109,7 @@ struct libgeome_netservice { /** * Geolocation data that may be provided by the service * - * 0 if unknown (always known (non-zero) when retreived + * 0 if unknown (always known (non-zero) when retrieved * from libgeome_netservices) * * See `struct libgeome_data.requested_data` @@ -156,7 +169,7 @@ struct libgeome_netservice { * Be aware these services may have terms of use that * restrict how you are allowed to use them. */ -extern struct libgeome_netservice libgeome_netservices[]; +extern const struct libgeome_netservice libgeome_netservices[]; /** * The number of elements in `libgeome_netservices` diff --git a/libgeome_get_from_command.c b/libgeome_get_from_command.c index 4d1c5af..8e224b6 100644 --- a/libgeome_get_from_command.c +++ b/libgeome_get_from_command.c @@ -75,8 +75,8 @@ libgeome_get_from_command(struct libgeome_context *ctx, struct libgeome_data *ou free(text); if (out->requested_data & LIBGEOME_DATUM_LATITUDE) { - if (!isfinite(location.latitude) || location.latitude < -90 || location.latitude > -90) { - ctx->print_debug(ctx, "invalide latitude retrieved: %g\n", location.latitude); + if (!isfinite(location.latitude) || location.latitude < -90 || location.latitude > 90) { + ctx->print_debug(ctx, "invalid latitude retrieved: %g\n", location.latitude); out->requested_data ^= LIBGEOME_DATUM_LATITUDE; } else { out->latitude = location.latitude; @@ -84,8 +84,8 @@ libgeome_get_from_command(struct libgeome_context *ctx, struct libgeome_data *ou } if (out->requested_data & LIBGEOME_DATUM_LONGITUDE) { - if (!isfinite(location.longitude) || location.longitude < -90 || location.longitude > -90) { - ctx->print_debug(ctx, "invalide longitude retrieved: %g\n", location.longitude); + if (!isfinite(location.longitude) || location.longitude < -180 || location.longitude > 180) { + ctx->print_debug(ctx, "invalid longitude retrieved: %g\n", location.longitude); out->requested_data ^= LIBGEOME_DATUM_LONGITUDE; } else { out->longitude = location.longitude; diff --git a/libgeome_netservices.c b/libgeome_netservices.c index 7701e2f..a275b2b 100644 --- a/libgeome_netservices.c +++ b/libgeome_netservices.c @@ -20,7 +20,7 @@ static const char *args3[] = GET("https://geolocation-db.com/json/"); .path = "curl", .args = (ARGS)\ } -struct libgeome_netservice libgeome_netservices[] = { +const struct libgeome_netservice libgeome_netservices[] = { COMMON(0, args0a), COMMON(0, args0b), COMMON(0, args0c), diff --git a/test-command.c b/test-command.c new file mode 100644 index 0000000..f2de29b --- /dev/null +++ b/test-command.c @@ -0,0 +1,24 @@ +/* See LICENSE file for copyright and license details. */ +#include "test-common.h" + +#define LIMIT_BYTES 4098 +#define LIMIT_SECONDS 10 + + +int +main(int argc, char *argv[]) +{ + struct libgeome_context ctx; + struct libgeome_data data; + + if (argc < 2) { + fprintf(stderr, "usage: %s command [argument] ...", argv[0]); + return 1; + } + + test_common_init(&ctx, &data, argv[0]); + if (libgeome_get_from_command(&ctx, &data, LIMIT_BYTES, LIMIT_SECONDS, argv[1], (const void *)&argv[1])) + return 1; + test_common_print(&data); + return 0; +} diff --git a/test-common.c b/test-common.c new file mode 100644 index 0000000..ea56bdb --- /dev/null +++ b/test-common.c @@ -0,0 +1,33 @@ +/* See LICENSE file for copyright and license details. */ +#include "test-common.h" + + +#if LIBGEOME_LAST_DATUM != LIBGEOME_DATUM_ALTITUDE +# warning struct libgeome_data has been updated, test_common_print() is out of date +#endif + + +void +test_common_init(struct libgeome_context *ctx_out, struct libgeome_data *data_out, const char *procname) +{ + libgeome_basic_context(ctx_out, procname); + ctx_out->print_debug = ctx_out->print_error; + data_out->requested_data = (LIBGEOME_LAST_DATUM - 1U) | LIBGEOME_LAST_DATUM; +} + + +void +test_common_print(struct libgeome_data *data) +{ + if (!data->requested_data) + printf("No data retrieved"); + + if (data->requested_data & LIBGEOME_DATUM_LATITUDE) + printf("Latitude: %g\n", data->latitude); + if (data->requested_data & LIBGEOME_DATUM_LONGITUDE) + printf("Longitude: %g\n", data->longitude); + if (data->requested_data & LIBGEOME_DATUM_ALTITUDE) + printf("Altitude: %g\n", data->altitude); + + data->requested_data = (LIBGEOME_LAST_DATUM - 1U) | LIBGEOME_LAST_DATUM; +} diff --git a/test-common.h b/test-common.h new file mode 100644 index 0000000..150a9e0 --- /dev/null +++ b/test-common.h @@ -0,0 +1,7 @@ +/* See LICENSE file for copyright and license details. */ +#include "libgeome.h" + +#include <stdio.h> + +void test_common_init(struct libgeome_context *ctx_out, struct libgeome_data *data_out, const char *procname); +void test_common_print(struct libgeome_data *data); diff --git a/test-file.c b/test-file.c new file mode 100644 index 0000000..1e9743d --- /dev/null +++ b/test-file.c @@ -0,0 +1,21 @@ +/* See LICENSE file for copyright and license details. */ +#include "test-common.h" + + +int +main(int argc, char *argv[]) +{ + struct libgeome_context ctx; + struct libgeome_data data; + + if (argc > 2) { + fprintf(stderr, "usage: %s [file]", argv[0]); + return 1; + } + + test_common_init(&ctx, &data, argv[0]); + if (libgeome_get_from_file(&ctx, &data, argv[1])) + return 1; + test_common_print(&data); + return 0; +} diff --git a/test-netservice.c b/test-netservice.c new file mode 100644 index 0000000..dd31ce8 --- /dev/null +++ b/test-netservice.c @@ -0,0 +1,33 @@ +/* See LICENSE file for copyright and license details. */ +#include "test-common.h" + + +int +main(int argc, char *argv[]) +{ + struct libgeome_context ctx; + struct libgeome_data data; + size_t i, j; + int ret = 1; + + if (argc != 1) { + fprintf(stderr, "usage: %s", argv[0]); + return 1; + } + + test_common_init(&ctx, &data, argv[0]); + + for (i = 0; i < libgeome_netservices_count; i++) { + if (i) + printf("\n"); + printf("Running:"); + for (j = 0; libgeome_netservices[i].args[j]; j++) + printf(" %s", libgeome_netservices[i].args[j]); + printf("\n"); + if (!libgeome_get_from_netservice(&ctx, &data, &libgeome_netservices[i])) { + ret = 0; + test_common_print(&data); + } + } + return ret; +} diff --git a/test-time.c b/test-time.c new file mode 100644 index 0000000..a717b39 --- /dev/null +++ b/test-time.c @@ -0,0 +1,21 @@ +/* See LICENSE file for copyright and license details. */ +#include "test-common.h" + + +int +main(int argc, char *argv[]) +{ + struct libgeome_context ctx; + struct libgeome_data data; + + if (argc != 1) { + fprintf(stderr, "usage: %s", argv[0]); + return 1; + } + + test_common_init(&ctx, &data, argv[0]); + if (libgeome_get_from_time(&ctx, &data)) + return 1; + test_common_print(&data); + return 0; +} diff --git a/test-timezone.c b/test-timezone.c new file mode 100644 index 0000000..46e218a --- /dev/null +++ b/test-timezone.c @@ -0,0 +1,21 @@ +/* See LICENSE file for copyright and license details. */ +#include "test-common.h" + + +int +main(int argc, char *argv[]) +{ + struct libgeome_context ctx; + struct libgeome_data data; + + if (argc != 1) { + fprintf(stderr, "usage: %s", argv[0]); + return 1; + } + + test_common_init(&ctx, &data, argv[0]); + if (libgeome_get_from_timezone(&ctx, &data)) + return 1; + test_common_print(&data); + return 0; +} |