]> git.sesse.net Git - vlc/blob - src/test/dictionary.c
update module LIST file.
[vlc] / src / test / dictionary.c
1 /*****************************************************************************
2  * dictionary.c: Tests for vlc_dictionary_t
3  *****************************************************************************
4  * Copyright (C) 2007 Pierre d'Herbemont
5  * $Id$
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
20  *****************************************************************************/
21
22 #ifdef HAVE_CONFIG_H
23 # include "config.h"
24 #endif
25
26 #include <vlc/vlc.h>
27 #include "vlc_arrays.h"
28
29 #include <stdio.h>
30 #include <stdlib.h>
31
32 static void test_dictionary_validity (vlc_dictionary_t * p_dict, const char ** our_keys, int size )
33 {
34     /* Test values and keys now */
35     char ** keys = vlc_dictionary_all_keys( p_dict );
36     int i, j;
37
38     assert( keys );
39
40     for( j = 0; keys[j]; j++ )
41     {
42         vlc_bool_t found = VLC_FALSE;
43         for( i = 0; i < size; i++ )
44         {
45             if(!strcmp( keys[j], our_keys[i] ))
46             {
47                 found = VLC_TRUE;
48                 break;
49             }
50         }
51         free( keys[j] );
52         assert( found );
53     }
54     free( keys );
55
56     for( i = 0; i < size; i++ )
57         assert( vlc_dictionary_value_for_key( p_dict, our_keys[i] ) == (void*)i );
58 }
59
60 int main (void)
61 {
62     static const char * our_keys[] = {
63         "Hello", "Hella", "flowmeter", "Frostnipped", "frostnipped", "remiform", "quadrifoliolate", "singularity", "unafflicted"
64     };
65     const int size = sizeof(our_keys)/sizeof(our_keys[0]);
66     char ** keys;
67     int i = 0;
68
69     vlc_dictionary_t dict;
70     vlc_dictionary_init( &dict, 0 );
71
72     assert( vlc_dictionary_keys_count( &dict ) == 0 );
73
74     keys = vlc_dictionary_all_keys( &dict );
75     assert( keys && !keys[0] );
76     free(keys);
77
78
79     /* Insert some values */
80     for( i = 0; i < size; i++ )
81         vlc_dictionary_insert( &dict, our_keys[i], (void*)i );
82
83     test_dictionary_validity( &dict, our_keys, size );
84
85     vlc_dictionary_remove_value_for_key( &dict, our_keys[size-1] );
86
87     test_dictionary_validity( &dict, our_keys, size-1 );
88     
89     vlc_dictionary_clear( &dict );
90
91     assert( vlc_dictionary_keys_count( &dict ) == 0 );
92     return 0;
93 }