]> git.sesse.net Git - vlc/blob - test/native/algo.c
A bit of cleanup and test
[vlc] / test / native / algo.c
1 /*****************************************************************************
2  * algo.c : Algorithms test
3  *****************************************************************************
4  * Copyright (C) 2006 VideoLAN
5  * $Id: i18n.c 16157 2006-07-29 13:32:12Z zorglub $
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 #include "../pyunit.h"
23 #include <vlc/vlc.h>
24
25 /**********************************************************************
26  * Binary search
27  *********************************************************************/
28
29 PyObject *bsearch_direct_test( PyObject *self, PyObject *args )
30 {
31 #define DIRCHECK( size, initial, checked, expected ) { \
32     int array[size] = initial; \
33     int answer = -1;  \
34     BSEARCH( array, size, , int, checked, answer ); \
35     ASSERT( answer == expected , "" ); }
36
37 #define ORDERED10 {0,1,2,3,4,5,6,7,8,9}
38     DIRCHECK( 10, ORDERED10, 0, 0 );
39     DIRCHECK( 10, ORDERED10, 1, 1 );
40     DIRCHECK( 10, ORDERED10, 2, 2 );
41     DIRCHECK( 10, ORDERED10, 3, 3 );
42     DIRCHECK( 10, ORDERED10, 4, 4 );
43     DIRCHECK( 10, ORDERED10, 5, 5 );
44     DIRCHECK( 10, ORDERED10, 6, 6 );
45     DIRCHECK( 10, ORDERED10, 7, 7 );
46     DIRCHECK( 10, ORDERED10, 8, 8 );
47     DIRCHECK( 10, ORDERED10, 9,9 );
48
49     DIRCHECK( 10, ORDERED10, 10, -1 );
50     DIRCHECK( 10, ORDERED10, -1, -1 );
51
52     /* TODO: tests on unordered arrays, odd number of elements, 1 element, 2 */
53
54     Py_INCREF( Py_None);
55     return Py_None;
56 }
57
58 struct bsearch_tester
59 {
60     int key; int value;
61 };
62
63 /* Lighter test, we just check correct member access, all the real testing
64  * has been made already */
65 PyObject *bsearch_member_test( PyObject *self, PyObject *args )
66 {
67     struct bsearch_tester array[] =
68     {
69         { 0, 12 }, { 1, 22 } , { 2, 33 } , { 3, 68 } , { 4, 56 } 
70     };
71 #define MEMBCHECK( checked, expected ) { \
72     int answer = -1;  \
73     BSEARCH( array, 5, .key , int, checked, answer ); \
74     ASSERT( answer == expected , "" ); }
75
76     MEMBCHECK( 0, 0 ) ;
77     MEMBCHECK( 1, 1 );
78     MEMBCHECK( 2, 2 );
79     MEMBCHECK( 3, 3 );
80     MEMBCHECK( 4, 4 );
81     MEMBCHECK( 5, -1 );
82
83     Py_INCREF( Py_None);
84     return Py_None;
85 }
86
87 /**********************************************************************
88  * Dictionnary
89  *********************************************************************/
90 static void DumpDict( dict_t *p_dict )
91 {
92     int i = 0;
93     fprintf( stderr, "**** Begin Dump ****\n" );
94     for( i = 0 ; i < p_dict->i_entries; i++ )
95     {
96         fprintf( stderr, "Entry %i - hash %lli int %i string %s data %p\n", 
97                         i, p_dict->p_entries[i].i_hash,
98                         p_dict->p_entries[i].i_int,
99                         p_dict->p_entries[i].psz_string,
100                         p_dict->p_entries[i].p_data );
101     }
102     fprintf( stderr, "**** End Dump ****\n" );
103 }
104
105 PyObject *dict_test( PyObject *self, PyObject *args )
106 {
107     int i42 = 42,i40 = 40,i12 = 12, i0 = 0, i00 = 0;
108
109     dict_t *p_dict = vlc_DictNew();
110     ASSERT( p_dict->i_entries == 0, "" );
111     ASSERT( p_dict->p_entries == NULL, "" );
112
113     vlc_DictInsert( p_dict, 0, NULL, (void*)(&i42) );
114     ASSERT( p_dict->i_entries == 1, "" );
115     ASSERT( p_dict->p_entries[0].p_data == (void*)(&i42), "" );
116
117     vlc_DictInsert( p_dict, 1, "42", (void*)(&i42) );
118     ASSERT( p_dict->i_entries == 2, "" );
119     ASSERT( vlc_DictGet( p_dict, 1, "42" ) == (void*)(&i42), "" );
120     ASSERT( vlc_DictGet( p_dict, 0, "42" ) == NULL , "");
121     ASSERT( vlc_DictGet( p_dict, 1, " 42" ) == NULL , "");
122
123     vlc_DictInsert( p_dict, 1, "12", (void*)(&i12) );
124     ASSERT( vlc_DictGet( p_dict, 1, "12") == (void*)(&i12), "" );
125
126     vlc_DictInsert( p_dict, 3, "40", (void*)(&i40) );
127     ASSERT( vlc_DictGet( p_dict, 3, "40") == (void*)(&i40), "" );
128     ASSERT( vlc_DictGet( p_dict, 1, "12") == (void*)(&i12), "" );
129     ASSERT( vlc_DictGet( p_dict, 1, "42") == (void*)(&i42), "" );
130
131     vlc_DictInsert( p_dict, 12, "zero-1", (void*)(&i0) );
132     vlc_DictInsert( p_dict, 5, "zero-0", (void*)(&i00) );
133     ASSERT( vlc_DictGet( p_dict, 12, "zero-1") == (void*)(&i0), "" );
134     ASSERT( vlc_DictGet( p_dict, 5, "zero-0") == (void*)(&i00), "" );
135     ASSERT( vlc_DictGet( p_dict, 12, "zero-0") == NULL, "" );
136
137     vlc_DictInsert( p_dict, 0, "12", (void*)(&i12) );
138     vlc_DictInsert( p_dict, 0, "thisisaverylongstringwith12", (void*)(&i12) );
139     ASSERT( vlc_DictGet( p_dict, 0, "thisisaverylongstringwith12" ) ==
140                     (void*)(&i12),"" );
141     ASSERT( vlc_DictGet( p_dict, 0, "thisisaverylongstringwith13" ) == NULL,"");
142
143     vlc_DictClear( p_dict );
144
145     Py_INCREF( Py_None);
146     return Py_None;
147 }