aboutsummaryrefslogtreecommitdiff
path: root/src/providers/tests/hafas-bin6.c
blob: 8c256e0d4ae837b1fff622dbc9f580370df17c7a (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
/*
 * db.c: Deutsche Bahn provider for libplanfahr
 *
 * Copyright (C) 2014 Guido Günther
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2, or (at your option)
 * any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.	See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 *
 * Author: Guido Günther <agx@sigxcpu.org>
 */

#include "../hafas-bin6.c"

/* Make sure we can parse the station xml list as returned by the current Deutsche Bahn Hafas */
static void
test_parse_locs (void)
{
    GSList *locs;
    LpfLoc *loc;
    char *name;
    double lon, lat;

    char *xml = g_strjoin(NULL,
"<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>",
"<ResC xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:noNamespaceSchemaLocation=\"No path to XML scheme defined\" ver=\"1.1\" prod=\"String\" lang=\"EN\">",
"  <MLcRes flag=\"FINAL\">",
"    <MLc t=\"ST\" n=\"Erpel(Rhein)\" i=\"A=1@O=Erpel(Rhein)@X=7241593@Y=50582067@U=80@L=008001858@B=1@p=1386184594@\" x=\"7241593\" y=\"50582067\" />",
"    <MLc t=\"ST\" n=\"Erpel B42\" i=\"A=1@O=Erpel B42@X=7231174@Y=50583649@U=81@L=000441204@B=1@p=1387219918@\" x=\"7231174\" y=\"50583649\" />",
"    <MLc t=\"ST\" n=\"Bahnhofstr., Erpel\" i=\"A=1@O=Bahnhofstr., Erpel@X=7243175@Y=50580943@U=81@L=000448602@B=1@p=1387219918@\" x=\"7243175\" y=\"50580943\" />",
"    <MLc t=\"ST\" n=\"Rheinfähre, Erpel\" i=\"A=1@O=Rheinfähre, Erpel@X=7238401@Y=50581663@U=81@L=000441205@B=1@p=1387219918@\" x=\"7238401\" y=\"50581663\" />",
"    <MLc t=\"ST\" n=\"Orsberg Ort, Erpel\" i=\"A=1@O=Orsberg Ort, Erpel@X=7243399@Y=50593061@U=81@L=000454657@B=1@p=1387219918@\" x=\"7243399\" y=\"50593061\" />",
"    <MLc t=\"ST\" n=\"Neutor, Erpel\" i=\"A=1@O=Neutor, Erpel@X=7235282@Y=50584108@U=81@L=000454652@B=1@p=1387219918@\" x=\"7235282\" y=\"50584108\" />",
"    <MLc t=\"ST\" n=\"Erpeldange Am Schlass, Luxemburg\" i=\"A=1@O=Erpeldange Am Schlass, Luxemburg@X=6114741@Y=49852458@U=81@L=000864180@B=1@p=1387219918@\" x=\"6114741\" y=\"49852458\" />",
"  </MLcRes>",
"</ResC>", NULL);

    locs = parse_locs_xml(xml);
    g_assert_nonnull (locs);
    g_assert_cmpint (g_slist_length (locs), ==, 7);

    loc = g_slist_nth (locs, 4)->data;
    g_object_get (loc, "name", &name, "long", &lon, "lat", &lat, NULL);

    g_assert_cmpstr (name, ==, "Orsberg Ort, Erpel");
    g_assert_cmpint (lon, ==, 7);
    g_assert_cmpint (lat, ==, 50);

    g_slist_free_full (locs, g_object_unref);
    g_free (xml);
    g_free (name);
}

/* Make sure we can parse the binary data trip information */
static void
test_parse_trips (void)
{
    GSList *trips;
    gchar *binary;
    gsize  length;
    int i;
    GSList *parts;
    LpfTrip *trip;
    LpfTripPart *part;
    LpfLoc *stop;
    gchar *name;
    GDateTime *dep, *arr;

    g_assert_true(g_file_get_contents(LPF_TEST_SRCDIR "/hafas-bin-6-station-query-1.bin", &binary, &length, NULL));

    trips = hafas_binary_parse_trips (binary, length);

    g_assert (g_slist_length (trips) == 3);

    for (i = 0; i < g_slist_length (trips); i++) {
        trip = LPF_TRIP(g_slist_nth_data (trips, i));
        g_object_get (G_OBJECT(trip), "parts", &parts, NULL);

        part = LPF_TRIP_PART(g_slist_nth_data (parts, 0));
        g_object_get (G_OBJECT(part), "start", &stop, NULL);
        g_object_get (G_OBJECT(stop),
                      "name", &name,
                      "arrival", &arr,
                      "departure", &dep,
                      NULL);
        /* All trips start in Erpel */
        g_assert (!g_strcmp0 (name, "Erpel(Rhein)"));
        g_free (name);

        g_assert (dep != NULL);
        g_assert (arr == NULL);
        g_date_time_unref (dep);

        /* All trips end in Unkel */
        part = LPF_TRIP_PART(g_slist_nth_data (parts,
                                               g_slist_length(parts)-1));
        g_object_get (G_OBJECT(part), "end", &stop, NULL);
        g_object_get (G_OBJECT(stop),
                      "name", &name,
                      "arrival", &arr,
                      "departure", &dep,
                      NULL);

        g_assert (dep == NULL);
        g_assert (arr != NULL);
        g_date_time_unref (arr);

        g_assert (g_strrstr (name, "Unkel") != NULL);
        g_free (name);
    }

    g_slist_free (trips);

}


int main(int argc, char **argv)
{
    gboolean ret;
    g_test_init (&argc, &argv, NULL);

    g_test_add_func ("/providers/de-db/parse_stations", test_parse_locs);
    g_test_add_func ("/providers/de-db/parse_trips", test_parse_trips);

    ret = g_test_run ();
    return ret;
}