File: mpd_data_model.c

package info (click to toggle)
gmpc 11.8.16-23
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 20,600 kB
  • sloc: ansic: 51,080; sh: 10,438; makefile: 1,748; xml: 9
file content (314 lines) | stat: -rw-r--r-- 9,847 bytes parent folder | download | duplicates (7)
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
/* Gnome Music Player Client (GMPC)
 * Copyright (C) 2004-2011 Qball Cow <qball@gmpclient.org>
 * Project homepage: http://gmpc.wikia.com/
 
 * 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 of the License, 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.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include <stdio.h>
#include <stdlib.h>
#include <glib.h>
#include <gtk/gtk.h>
#include "gmpc-mpddata-model.h"


/** 
 * Stuff to make linker happy 
 */
GObject *config = NULL;
GObject *gmw = NULL;
GObject *gmpc_signals = NULL;

void show_error_message(void)
{
}
const gchar *meta_data_get_uri(gpointer input)
{
    return NULL;
}

int cfg_get_single_value_as_int_with_default(GObject *config, const gchar *a, const gchar *b, int def)
{
    return def;
}

gchar * cfg_get_single_value_as_string_with_default(GObject *config, const gchar *a, const gchar *b, gchar* def)
{
    return g_strdup(def);
}
char * 	gmpc_signals_get_browser_markup	(GObject * self)
{
    return NULL;
}
void gmpc_meta_watcher_get_meta_path_callback()
{
}
void playlist3_show_error_message()
{
}
/**
 * Create test list 
 */
MpdData * get_test_list_songs(int length)
{
    MpdData *retv = NULL;
    int i = 0;
    for(;i<length;i++)
    {
        retv = mpd_new_data_struct_append(retv);
        retv->type = MPD_DATA_TYPE_SONG;
        retv->song = mpd_newSong();
        retv->song->file = g_strdup_printf("%i", i);
        retv->song->title = g_strdup_printf("%i", i);
        retv->song->pos = i;
        retv->song->time = i;
    }
    return mpd_data_get_first(retv);
}
MpdData * get_test_list_tags(int length, gboolean reverse)
{
    MpdData *retv = NULL;
    int i = 0;
    for(;i<length;i++)
    {
        retv = mpd_new_data_struct_append(retv);
        retv->type = MPD_DATA_TYPE_TAG;
        retv->tag= g_strdup_printf("%08i", (reverse)?length-i-1:i);
        retv->tag_type = 0;
    }
    return mpd_data_get_first(retv);
}
void test_songs_data_slow()
{
    GtkTreePath *path;
    GtkTreeIter iter;
    int length = 10000;
    int j = 0;
    MpdData *list = get_test_list_tags(length, FALSE);
    MpdData *list2 = get_test_list_tags(length-10, TRUE);
    GmpcMpdDataModel *model = gmpc_mpddata_model_new();
    g_assert(list != NULL);
    g_assert(list2 != NULL);
    g_assert(model != NULL);
    
    gmpc_mpddata_model_set_mpd_data_slow(GMPC_MPDDATA_MODEL(model), list);
    /* this should merge the 2 list, ignoring duplicates */
    gmpc_mpddata_model_set_mpd_data_slow(GMPC_MPDDATA_MODEL(model), list2);

    for(j=0; j< length-10;j++){
        path = gtk_tree_path_new_from_indices(j, -1);
        gchar *pos;

        g_assert(gtk_tree_model_get_iter(GTK_TREE_MODEL(model), &iter, path));
        gtk_tree_model_get(GTK_TREE_MODEL(model), &iter, MPDDATA_MODEL_COL_SONG_TITLE, &pos,-1);
        /* pos always has a 1 offset. this is because if you want to show the index number, you want to start at 1 */
        g_assert(pos != NULL);
        g_assert_cmpint(atoi(pos), ==, j);
        g_free(pos);
        g_assert_cmpint(gmpc_mpddata_model_get_pos(model, &iter), ==, j);
        gtk_tree_path_free(path);
    }
    /* Not allowed to be next iter */
    path = gtk_tree_path_new_from_indices(j, -1);
    g_assert(!gtk_tree_model_get_iter(GTK_TREE_MODEL(model), &iter, path));
    gtk_tree_path_free(path);

    g_object_unref(model);
}

/**
 * This tests the function that turns a GtkTreePAth into an iter.
 * With the has_up
 */
void test_songs_path_to_iter_with_up()
{
    int length = 10000;
    int j = 0;
    MpdData *list = get_test_list_songs(length);
    GmpcMpdDataModel *model = gmpc_mpddata_model_new();
    g_assert(list != NULL);
    g_assert(model != NULL);
    gmpc_mpddata_model_set_has_up(model, TRUE);
    gmpc_mpddata_model_set_mpd_data(GMPC_MPDDATA_MODEL(model), list);
    /* Skip the first, as that is up */
    for(j=1; j< length;j++){
        GtkTreePath *path = gtk_tree_path_new_from_indices(j, -1);
        GtkTreeIter iter;
        int pos;
        mpd_Song *song;
        g_assert(gtk_tree_model_get_iter(GTK_TREE_MODEL(model), &iter, path));
        gtk_tree_model_get(GTK_TREE_MODEL(model), &iter, MPDDATA_MODEL_COL_SONG_POS, &pos,MPDDATA_MODEL_COL_MPDSONG, &song,-1);
        /* pos always has a 1 offset minus the _up_ offset. this is because if you want to show the index number, you want to start at 1 */
        g_assert_cmpint(pos, ==, j);
        /* song pos should match */
        g_assert_cmpint(song->pos+1, == , j);

        g_assert_cmpint(gmpc_mpddata_model_get_pos(model, &iter), ==, j);
        gtk_tree_path_free(path);
    }


    g_object_unref(model);
}


/**
 * This tests the function that turns a GtkTreePAth into an iter.
 */
void test_songs_path_to_iter()
{
    int length = 10000;
    int j = 0;
    MpdData *list = get_test_list_songs(length);
    GmpcMpdDataModel *model = gmpc_mpddata_model_new();

    g_assert(list != NULL);
    g_assert(model != NULL);
    gmpc_mpddata_model_set_mpd_data(GMPC_MPDDATA_MODEL(model), list);

    for(j=0; j< length;j++){
        GtkTreePath *path = gtk_tree_path_new_from_indices(j, -1);
        GtkTreeIter iter;
        int pos;
        mpd_Song *song;
        g_assert(gtk_tree_model_get_iter(GTK_TREE_MODEL(model), &iter, path));
        gtk_tree_model_get(GTK_TREE_MODEL(model), &iter, MPDDATA_MODEL_COL_SONG_POS, &pos,MPDDATA_MODEL_COL_MPDSONG, &song,-1);
        /* pos always has a 1 offset. this is because if you want to show the index number, you want to start at 1 */
        g_assert_cmpint(pos-1, ==, j);
        /* song pos should match */
        g_assert_cmpint(song->pos, == , j);

        g_assert_cmpint(gmpc_mpddata_model_get_pos(model, &iter), ==, j);
        gtk_tree_path_free(path);
    }


    g_object_unref(model);
}

void test_songs_total_playtime()
{
    long unsigned playtime = 0;
    int length = 10000;
    int j = 0;
    MpdData *list = get_test_list_songs(length);
    GmpcMpdDataModel *model = gmpc_mpddata_model_new();

    g_assert(list != NULL);
    g_assert(model != NULL);
    gmpc_mpddata_model_set_mpd_data(GMPC_MPDDATA_MODEL(model), list);
    
    playtime = gmpc_mpddata_model_get_playtime(GMPC_MPDDATA_MODEL(model));
    g_assert(playtime == 49995000); 

    g_object_unref(model);
}

void test_songs_num_rows()
{

    long unsigned playtime = 0;
    int length = 10000;
    int j = 0;
    MpdData *list = get_test_list_songs(length);
    GmpcMpdDataModel *model = gmpc_mpddata_model_new();

    g_assert(list != NULL);
    g_assert(model != NULL);
    gmpc_mpddata_model_set_mpd_data(GMPC_MPDDATA_MODEL(model), list);

    g_assert(model->num_rows == length);
    g_assert(gtk_tree_model_iter_n_children(GTK_TREE_MODEL(model), NULL) == length);

    g_object_unref(model);
}

void test_songs_num_rows_with_up()
{

    long unsigned playtime = 0;
    int length = 10000;
    int j = 0;
    MpdData *list = get_test_list_songs(length);
    GmpcMpdDataModel *model = gmpc_mpddata_model_new();

    g_assert(list != NULL);
    g_assert(model != NULL);

    gmpc_mpddata_model_set_has_up(model, TRUE);
    gmpc_mpddata_model_set_mpd_data(GMPC_MPDDATA_MODEL(model), list);

    g_assert(model->num_rows == length+1);
    g_assert(gtk_tree_model_iter_n_children(GTK_TREE_MODEL(model), NULL) == length+1);

    g_object_unref(model);
}

void test_iter_children()
{
    long unsigned playtime = 0;
    int length = 10000;
    int j = 0;
    GtkTreeIter iter, parent;
    MpdData *list = get_test_list_songs(length);
    GmpcMpdDataModel *model = gmpc_mpddata_model_new();

    g_assert(model != NULL);
    g_assert(list != NULL);

    g_assert(!gtk_tree_model_iter_children (GTK_TREE_MODEL(model), &iter, NULL));


    gmpc_mpddata_model_set_mpd_data(GMPC_MPDDATA_MODEL(model), list);

    g_assert(gtk_tree_model_iter_children (GTK_TREE_MODEL(model), &parent, NULL));
    /* but no child */
    g_assert(!gtk_tree_model_iter_children (GTK_TREE_MODEL(model), &iter, &parent));

    g_object_unref(model);
}
void test_iter_children_has_up()
{
    long unsigned playtime = 0;
    GtkTreeIter iter, parent;
    GmpcMpdDataModel *model = gmpc_mpddata_model_new();

    g_assert(model != NULL);

    g_assert(!gtk_tree_model_iter_children (GTK_TREE_MODEL(model), &iter, NULL));
    gmpc_mpddata_model_set_has_up(model, TRUE);
    g_assert(!gtk_tree_model_iter_children (GTK_TREE_MODEL(model), &parent, NULL));

    /* but no child */
    g_assert(!gtk_tree_model_iter_children (GTK_TREE_MODEL(model), &iter, &parent));

    g_object_unref(model);
}
int main(int argc, char **argv)
{
    GtkTreeIter iter;
    g_type_init();
    g_test_init(&argc, &argv, NULL);
    
    g_test_add_func("/GmpcMpdDataModel/num_rows", test_songs_num_rows);
    g_test_add_func("/GmpcMpdDataModel/num_rows_with_up", test_songs_num_rows_with_up);
    g_test_add_func("/GmpcMpdDataModel/path_to_iter", test_songs_path_to_iter);
    g_test_add_func("/GmpcMpdDataModel/path_to_iter_with_up", test_songs_path_to_iter_with_up);
    g_test_add_func("/GmpcMpdDAtaModel/test_songs_data_slow", test_songs_data_slow);
    g_test_add_func("/GmpcMpdDataModel/total_playtime", test_songs_total_playtime);
    g_test_add_func("/GmpcMpdDataModel/iter_children",  test_iter_children);
    g_test_add_func("/GmpcMpdDataModel/iter_children_has_up",  test_iter_children_has_up);

    return g_test_run();
}