1 /*****************************************************************************
2 * algo.c : Algorithms test
3 *****************************************************************************
4 * Copyright (C) 2006 VideoLAN
5 * $Id: i18n.c 16157 2006-07-29 13:32:12Z zorglub $
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"
25 /**********************************************************************
27 *********************************************************************/
29 PyObject *bsearch_direct_test( PyObject *self, PyObject *args )
31 #define DIRCHECK( size, initial, checked, expected ) { \
32 int array[size] = initial; \
34 BSEARCH( array, size, , int, checked, answer ); \
35 ASSERT( answer == expected , "" ); }
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 );
49 DIRCHECK( 10, ORDERED10, 10, -1 );
50 DIRCHECK( 10, ORDERED10, -1, -1 );
52 /* TODO: tests on unordered arrays, odd number of elements, 1 element, 2 */
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 )
67 struct bsearch_tester array[] =
69 { 0, 12 }, { 1, 22 } , { 2, 33 } , { 3, 68 } , { 4, 56 }
71 #define MEMBCHECK( checked, expected ) { \
73 BSEARCH( array, 5, .key , int, checked, answer ); \
74 ASSERT( answer == expected , "" ); }
87 /**********************************************************************
89 *********************************************************************/
90 static void DumpDict( dict_t *p_dict )
93 fprintf( stderr, "**** Begin Dump ****\n" );
94 for( i = 0 ; i < p_dict->i_entries; i++ )
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 );
102 fprintf( stderr, "**** End Dump ****\n" );
105 PyObject *dict_test( PyObject *self, PyObject *args )
107 int i42 = 42,i40 = 40,i12 = 12, i0 = 0, i00 = 0;
109 dict_t *p_dict = vlc_DictNew();
110 ASSERT( p_dict->i_entries == 0, "" );
111 ASSERT( p_dict->p_entries == NULL, "" );
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), "" );
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 , "");
123 vlc_DictInsert( p_dict, 1, "12", (void*)(&i12) );
124 ASSERT( vlc_DictGet( p_dict, 1, "12") == (void*)(&i12), "" );
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), "" );
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, "" );
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" ) ==
141 ASSERT( vlc_DictGet( p_dict, 0, "thisisaverylongstringwith13" ) == NULL,"");
143 vlc_DictClear( p_dict );