/* * Copyright (C) 2016 Robin Krahl * * dbfp_check.c */ #include #include #include #include "dbfp.h" 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; int err; err = dbfp_init(&dbfp, "test-key"); ck_assert_int_eq(err, 0); ck_assert_str_eq(dbfp.key, "test-key"); dbfp_close(&dbfp); } END_TEST START_TEST(test_dbfp_access) { int err; struct dbfp dbfp; struct dbfp_location *locations = NULL; size_t n; err = dbfp_init(&dbfp, api_key); ck_assert_int_eq(err, 0); err = dbfp_query_location(&dbfp, "", &n, &locations); ck_assert_int_eq(err, 0); free(locations); dbfp_close(&dbfp); } END_TEST START_TEST(test_dbfp_location_simple) { int err; struct dbfp dbfp; struct dbfp_location *locations = NULL; size_t n; size_t i; err = dbfp_init(&dbfp, api_key); ck_assert_int_eq(err, 0); err = dbfp_query_location(&dbfp, "freiburg", &n, &locations); ck_assert_int_eq(err, 0); 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); err = dbfp_query_location(&dbfp, "freib", &n, &locations); ck_assert_int_eq(err, 0); 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); err = dbfp_query_location(&dbfp, "freiburg im", &n, &locations); ck_assert_int_eq(err, 0); 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); dbfp_close(&dbfp); } END_TEST START_TEST(test_dbfp_departure_simple) { int err; struct dbfp dbfp; struct dbfp_departure *departures = NULL; time_t now = time(0); size_t n; size_t i; err = dbfp_init(&dbfp, api_key); ck_assert_int_eq(err, 0); err = dbfp_query_departure(&dbfp, "8000105", 0, &n, &departures); ck_assert_int_eq(err, 0); ck_assert_int_gt(n, 0); for (i = 0; i < n; i++) dbfp_departure_close(&departures[i]); free(departures); err = dbfp_query_departure(&dbfp, "8000105", now, &n, &departures); ck_assert_int_eq(err, 0); for (i = 0; i < n; i++) dbfp_departure_close(&departures[i]); free(departures); dbfp_close(&dbfp); } END_TEST int main(int argc, char **argv) { int num_failed = 0; Suite *s; SRunner *sr; TCase *tc_basic; TCase *tc_location; TCase *tc_departure; api_key = getenv("DBFP_API_KEY"); if (!api_key) { fprintf(stderr, "DBFP_API_KEY not set\n"); return EXIT_FAILURE; } s = suite_create("dbfp"); sr = srunner_create(s); tc_basic = tcase_create("basic"); tcase_add_test(tc_basic, test_dbfp_create); tcase_add_test(tc_basic, test_dbfp_access); suite_add_tcase(s, tc_basic); tc_location = tcase_create("location"); tcase_add_test(tc_location, test_dbfp_location_simple); suite_add_tcase(s, tc_location); tc_departure = tcase_create("departure"); tcase_add_test(tc_departure, test_dbfp_departure_simple); suite_add_tcase(s, tc_departure); srunner_run_all(sr, CK_ENV); num_failed = srunner_ntests_failed(sr); srunner_free(sr); 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); }