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