1 /*****************************************************************************
2 * algo.c : Algorithms test
3 *****************************************************************************
4 * Copyright (C) 2006 VideoLAN
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.
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.
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 *****************************************************************************/
22 #include "../pyunit.h"
29 /**********************************************************************
31 *********************************************************************/
33 TYPEDEF_ARRAY(long,long_array_t
);
35 PyObject
*arrays_test( PyObject
*self
, PyObject
*args
)
39 int number2
= 50000; /* For slow with memmove */
46 for( i
= 0 ; i
<number
;i
++) {
47 INSERT_ELEM(p_items
,i_items
, i_items
, i
+50);
50 printf( " Std array %i items appended in "I64Fi
" µs\n", number
,
52 for( i
= number
-1 ; i
>=0; i
--) {
53 REMOVE_ELEM( p_items
, i_items
, i
);
56 printf( " Std array %i items removed in "I64Fi
" µs\n", number
,
59 for( i
= 0 ; i
<number2
;i
++) {
60 int pos
= i_items
== 0 ? 0 : rand() % i_items
;
61 INSERT_ELEM(p_items
, i_items
, pos
, pos
+ 50);
64 printf( " Std array %i items inserted in "I64Fi
" µs\n", number2
,
68 DECL_ARRAY(int) int_array
;
70 ARRAY_INIT(int_array
);
71 ASSERT(int_array
.i_size
== 0, "" );
72 ASSERT(int_array
.i_alloc
== 0, "" );
73 ASSERT(int_array
.p_elems
== 0, "" );
75 ARRAY_APPEND(int_array
, 42 );
76 ASSERT(int_array
.i_size
== 1, "" );
77 ASSERT(int_array
.i_alloc
> 1, "" );
78 ASSERT(int_array
.p_elems
[0] == 42, "" );
79 ARRAY_REMOVE(int_array
,0);
80 ASSERT(int_array
.i_size
== 0, "" );
83 for( i
= 0 ; i
<number
;i
++) {
84 ARRAY_APPEND(int_array
, i
+50);
87 printf( " New array %i items appended in "I64Fi
" µs\n", number
,
89 ASSERT(int_array
.p_elems
[1242] == 1292 , "");
90 for( i
= number
-1 ; i
>=0; i
--) {
91 ARRAY_REMOVE(int_array
,i
);
94 printf( " New array %i items removed in "I64Fi
" µs\n", number
,
97 /* Now random inserts */
98 for( i
= 0 ; i
<number2
;i
++) {
99 int pos
= int_array
.i_size
== 0 ? 0 : rand() % int_array
.i_size
;
100 ARRAY_INSERT(int_array
, pos
+50, pos
);
103 printf( " New array %i items inserted in "I64Fi
" µs\n", number2
,
114 /**********************************************************************
116 *********************************************************************/
118 PyObject
*bsearch_direct_test( PyObject
*self
, PyObject
*args
)
120 #define DIRCHECK( size, initial, checked, expected ) { \
121 int array[size] = initial; \
123 BSEARCH( array, size, , int, checked, answer ); \
124 ASSERT( answer == expected , "" ); }
126 #define ORDERED10 {0,1,2,3,4,5,6,7,8,9}
127 DIRCHECK( 10, ORDERED10
, 0, 0 );
128 DIRCHECK( 10, ORDERED10
, 1, 1 );
129 DIRCHECK( 10, ORDERED10
, 2, 2 );
130 DIRCHECK( 10, ORDERED10
, 3, 3 );
131 DIRCHECK( 10, ORDERED10
, 4, 4 );
132 DIRCHECK( 10, ORDERED10
, 5, 5 );
133 DIRCHECK( 10, ORDERED10
, 6, 6 );
134 DIRCHECK( 10, ORDERED10
, 7, 7 );
135 DIRCHECK( 10, ORDERED10
, 8, 8 );
136 DIRCHECK( 10, ORDERED10
, 9,9 );
138 DIRCHECK( 10, ORDERED10
, 10, -1 );
139 DIRCHECK( 10, ORDERED10
, -1, -1 );
141 /* TODO: tests on unordered arrays, odd number of elements, 1 element, 2 */
147 struct bsearch_tester
152 /* Lighter test, we just check correct member access, all the real testing
153 * has been made already */
154 PyObject
*bsearch_member_test( PyObject
*self
, PyObject
*args
)
156 struct bsearch_tester array
[] =
158 { 0, 12 }, { 1, 22 } , { 2, 33 } , { 3, 68 } , { 4, 56 }
160 #define MEMBCHECK( checked, expected ) { \
162 BSEARCH( array, 5, .key , int, checked, answer ); \
163 ASSERT( answer == expected , "" ); }
176 /**********************************************************************
178 *********************************************************************/
179 DICT_TYPE( test
, int );
181 static void DumpDict( dict_test_t
*p_dict
)
184 fprintf( stderr
, "**** Begin Dump ****\n" );
185 for( i
= 0 ; i
< p_dict
->i_entries
; i
++ )
187 fprintf( stderr
, "Entry %i - hash %lli int %i string %s data %i\n",
188 i
, p_dict
->p_entries
[i
].i_hash
,
189 p_dict
->p_entries
[i
].i_int
,
190 p_dict
->p_entries
[i
].psz_string
,
191 p_dict
->p_entries
[i
].data
);
193 fprintf( stderr
, "**** End Dump ****\n" );
196 PyObject
*dict_test( PyObject
*self
, PyObject
*args
)
198 int i42
= 42,i40
= 40,i12
= 12, i0
= 0, i00
= 0;
205 ASSERT( p_dict
->i_entries
== 0, "" );
206 ASSERT( p_dict
->p_entries
== NULL
, "" );
208 DICT_INSERT( p_dict
, 0, NULL
, i42
);
209 ASSERT( p_dict
->i_entries
== 1, "" );
210 ASSERT( p_dict
->p_entries
[0].data
== i42
, "" );
212 DICT_INSERT( p_dict
, 1, "42", i42
);
213 ASSERT( p_dict
->i_entries
== 2, "" );
215 DICT_LOOKUP( p_dict
, 1, "42", answer
);
216 DICT_GET( p_dict
, 1, "42", answer
);
217 ASSERT( answer
== i42
, "" );
218 DICT_LOOKUP( p_dict
, 0, "42", answer
); ASSERT( answer
== -1, "" );
219 DICT_LOOKUP( p_dict
, 1, " 42", answer
); ASSERT( answer
== -1, "" );
221 DICT_INSERT( p_dict
, 1, "12", i12
);
222 DICT_GET( p_dict
, 1, "12", answer
) ; ASSERT( answer
== i12
, "" );
224 DICT_INSERT( p_dict
, 3, "40", i40
);
225 DICT_GET( p_dict
, 1, "42", answer
); ASSERT( answer
== i42
, "" );
226 DICT_GET( p_dict
, 3, "40", answer
); ASSERT( answer
== i40
, "" );
227 DICT_GET( p_dict
, 1, "12", answer
); ASSERT( answer
== i12
, "" );
229 DICT_INSERT( p_dict
, 12, "zero-1", i0
);
230 DICT_INSERT( p_dict
, 5, "zero-0", i00
);
231 DICT_GET( p_dict
, 12, "zero-1", answer
); ASSERT( answer
== i0
, "" );
232 DICT_GET( p_dict
, 5, "zero-0", answer
); ASSERT( answer
== i00
, "" );
234 DICT_GET( p_dict
, 12, "zero-0", answer
); ASSERT( answer
== -1, "" );
235 DICT_GET( p_dict
, 1, "12", answer
); ASSERT( answer
== i12
, "" );
237 DICT_INSERT( p_dict
, 0, "zero", 17 );
238 DICT_GET( p_dict
, 1, "12", answer
); ASSERT( answer
== i12
, "" );
239 DICT_GET( p_dict
, 12, "zero-1", answer
); ASSERT( answer
== i0
, "" );
240 DICT_GET( p_dict
, 0, "zero", answer
); ASSERT( answer
== 17, "" );
242 DICT_INSERT( p_dict
, 0, "12", i12
);
243 DICT_INSERT( p_dict
, 0, "thisisaverylongstringwith12", i12
);
245 DICT_GET( p_dict
, 0, "thisisaverylongstringwith12", answer
);
246 ASSERT( answer
== i12
, "" );
248 DICT_GET( p_dict
, 0, "thisisaverylongstringwith13", answer
);
249 ASSERT( answer
== -1, "" );
251 DICT_CLEAR( p_dict
);