From 99319968f3e3ebbdce6c37f04243f734e98b28b3 Mon Sep 17 00:00:00 2001 From: Robin Krahl Date: Thu, 10 Mar 2016 18:03:22 +0100 Subject: dbfp: introduce dbfp_location_close dbfp_location_close releases the resources allocated by a dbfp_location structure. It does not free the structure itself. --- dbfp.c | 9 ++++++++ dbfp.h | 1 + dbfp_check.c | 71 +++++++++++++++++++++++++----------------------------------- 3 files changed, 39 insertions(+), 42 deletions(-) diff --git a/dbfp.c b/dbfp.c index b36a17c..ec9543b 100644 --- a/dbfp.c +++ b/dbfp.c @@ -91,6 +91,15 @@ void dbfp_close(struct dbfp *dbfp) dbfp->key = NULL; } +void dbfp_location_close(struct dbfp_location *location) +{ + if (!location) + return; + + free(location->name); + free(location->id); +} + int dbfp_query_location_name(struct dbfp *dbfp, char *input, size_t *n, struct dbfp_location **locations) { diff --git a/dbfp.h b/dbfp.h index 7871372..589ac06 100644 --- a/dbfp.h +++ b/dbfp.h @@ -33,6 +33,7 @@ struct dbfp_location { int dbfp_init(struct dbfp *dbfp, char *key); void dbfp_close(struct dbfp *dbfp); +void dbfp_location_close(struct dbfp_location *location); int dbfp_query_location_name(struct dbfp *dbfp, char *input, size_t *n, struct dbfp_location **locations); diff --git a/dbfp_check.c b/dbfp_check.c index 29db4a3..f816cfc 100644 --- a/dbfp_check.c +++ b/dbfp_check.c @@ -12,6 +12,10 @@ static char *api_key; +static void assert_location_eq(size_t n, struct dbfp_location *locations, + size_t i, const char *name, const char *id, float lon, + float lat); + START_TEST(test_dbfp_create) { struct dbfp dbfp; @@ -55,57 +59,27 @@ START_TEST(test_dbfp_location_simple) err = dbfp_query_location_name(&dbfp, "freiburg", &n, &locations); ck_assert_int_eq(err, 0); - - ck_assert_int_gt(n, 0); - ck_assert_ptr_ne(locations, NULL); - - ck_assert_str_eq(locations[0].name, "Freiburg(Breisgau) Hbf"); - ck_assert_str_eq(locations[0].id, "008000107"); - ck_assert(locations[0].lon == 7.841173f); - ck_assert(locations[0].lat == 47.997696f); - - for (i = 0; i < n; i++) { - free(locations[i].name); - free(locations[i].id); - } + assert_location_eq(n, locations, 0, "Freiburg(Breisgau) Hbf", + "008000107", 7.841173f, 47.997696f); + for (i = 0; i < n; i++) + dbfp_location_close(&locations[i]); free(locations); - locations = NULL; err = dbfp_query_location_name(&dbfp, "freib", &n, &locations); ck_assert_int_eq(err, 0); - - ck_assert_int_gt(n, 0); - ck_assert_ptr_ne(locations, NULL); - - ck_assert_str_eq(locations[0].name, "Freiburg(Breisgau) Hbf"); - ck_assert_str_eq(locations[0].id, "008000107"); - ck_assert(locations[0].lon == 7.841173f); - ck_assert(locations[0].lat == 47.997696f); - - for (i = 0; i < n; i++) { - free(locations[i].name); - free(locations[i].id); - } + assert_location_eq(n, locations, 0, "Freiburg(Breisgau) Hbf", + "008000107", 7.841173f, 47.997696f); + for (i = 0; i < n; i++) + dbfp_location_close(&locations[i]); free(locations); - locations = NULL; err = dbfp_query_location_name(&dbfp, "freiburg im", &n, &locations); ck_assert_int_eq(err, 0); - - ck_assert_int_gt(n, 0); - ck_assert_ptr_ne(locations, NULL); - - ck_assert_str_eq(locations[0].name, "Freiburg(Breisgau) Hbf"); - ck_assert_str_eq(locations[0].id, "008000107"); - ck_assert(locations[0].lon == 7.841173f); - ck_assert(locations[0].lat == 47.997696f); - - for (i = 0; i < n; i++) { - free(locations[i].name); - free(locations[i].id); - } + assert_location_eq(n, locations, 0, "Freiburg(Breisgau) Hbf", + "008000107", 7.841173f, 47.997696f); + for (i = 0; i < n; i++) + dbfp_location_close(&locations[i]); free(locations); - locations = NULL; dbfp_close(&dbfp); } @@ -143,3 +117,16 @@ int main(int argc, char **argv) return (num_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE; } + +static void assert_location_eq(size_t n, struct dbfp_location *locations, + size_t i, const char *name, const char *id, float lon, + float lat) +{ + ck_assert_ptr_ne(locations, NULL); + ck_assert_uint_lt(i, n); + + ck_assert_str_eq(locations[i].name, name); + ck_assert_str_eq(locations[i].id, id); + ck_assert(locations[i].lon == lon); + ck_assert(locations[i].lat == lat); +} -- cgit v1.2.1