]> git.sesse.net Git - vlc/blob - src/test/dictionary.c
7c823749b1add2c414614d1cbba2b216d15b3653
[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 #undef NDEBUG
27 #include <assert.h>
28
29 #include <vlc_common.h>
30 #include "vlc_arrays.h"
31
32 #include <stdio.h>
33 #include <stdlib.h>
34
35 static void test_dictionary_validity (vlc_dictionary_t * p_dict, const char ** our_keys, int size )
36 {
37     /* Test values and keys now */
38     char ** keys = vlc_dictionary_all_keys( p_dict );
39     int i, j;
40
41     assert( keys );
42
43     for( j = 0; keys[j]; j++ )
44     {
45         bool found = false;
46         for( i = 0; i < size; i++ )
47         {
48             if(!strcmp( keys[j], our_keys[i] ))
49             {
50                 found = true;
51                 break;
52             }
53         }
54         free( keys[j] );
55         assert( found );
56     }
57     free( keys );
58
59     for( i = 0; i < size; i++ )
60         assert( vlc_dictionary_value_for_key( p_dict, our_keys[i] ) == (void*)i );
61 }
62
63 int main (void)
64 {
65     static const char * our_keys[] = {
66         "Hello", "Hella", "flowmeter", "Frostnipped", "frostnipped", "remiform", "quadrifoliolate", "singularity", "unafflicted"
67     };
68     const int size = sizeof(our_keys)/sizeof(our_keys[0]);
69     char ** keys;
70     int i = 0;
71
72     vlc_dictionary_t dict;
73     vlc_dictionary_init( &dict, 0 );
74
75     assert( vlc_dictionary_keys_count( &dict ) == 0 );
76
77     keys = vlc_dictionary_all_keys( &dict );
78     assert( keys && !keys[0] );
79     free(keys);
80
81
82     /* Insert some values */
83     for( i = 0; i < size; i++ )
84         vlc_dictionary_insert( &dict, our_keys[i], (void*)i );
85
86     test_dictionary_validity( &dict, our_keys, size );
87
88     vlc_dictionary_remove_value_for_key( &dict, our_keys[size-1], NULL, NULL );
89
90     test_dictionary_validity( &dict, our_keys, size-1 );
91
92     vlc_dictionary_clear( &dict, NULL, NULL );
93
94     assert( vlc_dictionary_keys_count( &dict ) == 0 );
95     return 0;
96 }