aboutsummaryrefslogtreecommitdiff
path: root/dbfp_check.c
blob: bda63e52fe95753ad4932be87d167ad77ed3be92 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
/*
 * Copyright (C) 2016 Robin Krahl <robin.krahl@ireas.org>
 *
 * dbfp_check.c
 */

#include <check.h>
#include <stdio.h>
#include <stdlib.h>

#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);

static void assert_departure_eq(size_t n, struct dbfp_departure *departures,
		size_t i, const char *name, const char *type,
		const char *stopid, const char *stop, time_t time,
		const char *direction, const char *track);

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_name(&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_name(&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_name(&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_name(&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;
	struct tm tm = { .tm_year = 116, .tm_mon = 5, .tm_mday = 1,
		.tm_hour = 17, .tm_min = 42 };
	time_t time = mktime(&tm);
	size_t n;
	size_t i;

	err = dbfp_init(&dbfp, api_key);
	ck_assert_int_eq(err, 0);

	err = dbfp_query_departure(&dbfp, NULL, "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, NULL, "8000105", time, &n,
			&departures);
	ck_assert_int_eq(err, 0);
	tm.tm_hour++;
	time = mktime(&tm);
	assert_departure_eq(n, departures, 0, "ICE 1556", "ICE", "8000105",
			"Frankfurt(Main)Hbf", time, "Wiesbaden Hbf", "6");
	tm.tm_min = 49;
	time = mktime(&tm);
	assert_departure_eq(n, departures, 1, "IC 2274", "IC", "8000105",
			"Frankfurt(Main)Hbf", time, "Kassel-Wilhelmshöhe",
			"11");
	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);
}

static void assert_departure_eq(size_t n, struct dbfp_departure *departures,
		size_t i, const char *name, const char *type,
		const char *stopid, const char *stop, time_t time,
		const char *direction, const char *track)
{
	ck_assert_ptr_ne(departures, NULL);
	ck_assert_uint_lt(i, n);

	ck_assert_str_eq(departures[i].name, name);
	ck_assert_str_eq(departures[i].type, type);
	ck_assert_str_eq(departures[i].stopid, stopid);
	ck_assert_str_eq(departures[i].stop, stop);
	ck_assert_uint_eq(departures[i].time, time);
	ck_assert_str_eq(departures[i].direction, direction);
	ck_assert_str_eq(departures[i].track, track);
}