aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRobin Krahl <me@robin-krahl.de>2016-03-10 18:03:22 +0100
committerRobin Krahl <me@robin-krahl.de>2016-03-10 18:03:22 +0100
commit99319968f3e3ebbdce6c37f04243f734e98b28b3 (patch)
tree9b750ab244fbd2c359999a0432572a57a1c84dd3
parent2d3fffa502e808452848b7396b4e121917ddc787 (diff)
downloaddbfp-99319968f3e3ebbdce6c37f04243f734e98b28b3.tar.gz
dbfp-99319968f3e3ebbdce6c37f04243f734e98b28b3.tar.bz2
dbfp: introduce dbfp_location_close
dbfp_location_close releases the resources allocated by a dbfp_location structure. It does not free the structure itself.
-rw-r--r--dbfp.c9
-rw-r--r--dbfp.h1
-rw-r--r--dbfp_check.c71
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);
+}