Subversion Repositories HelenOS

Rev

Rev 3912 | Rev 4332 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
3666 mejdrech 1
/*
3912 mejdrech 2
 * Copyright (c) 2009 Lukas Mejdrech
3666 mejdrech 3
 * All rights reserved.
4
 *
5
 * Redistribution and use in source and binary forms, with or without
6
 * modification, are permitted provided that the following conditions
7
 * are met:
8
 *
9
 * - Redistributions of source code must retain the above copyright
10
 *   notice, this list of conditions and the following disclaimer.
11
 * - Redistributions in binary form must reproduce the above copyright
12
 *   notice, this list of conditions and the following disclaimer in the
13
 *   documentation and/or other materials provided with the distribution.
14
 * - The name of the author may not be used to endorse or promote products
15
 *   derived from this software without specific prior written permission.
16
 *
17
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
 */
28
 
29
/** @addtogroup net
3912 mejdrech 30
 *  @{
3666 mejdrech 31
 */
32
 
33
/** @file
34
 */
35
 
36
#include "configuration.h"
37
 
38
#if NET_SELF_TEST
39
 
40
#include <errno.h>
41
#include <malloc.h>
42
#include <stdio.h>
43
 
4075 mejdrech 44
#include "include/crc.h"
45
#include "structures/int_map.h"
46
#include "structures/char_map.h"
47
#include "structures/generic_char_map.h"
48
#include "structures/measured_strings.h"
3666 mejdrech 49
 
50
#include "self_test.h"
51
 
3846 mejdrech 52
#define TEST( name, function_call, result );    {   \
3666 mejdrech 53
    printf( "\n\t%s", ( name ));                    \
3846 mejdrech 54
    if(( function_call ) != ( result )){            \
55
        printf( "\tERROR" );                        \
4075 mejdrech 56
        error = 1;                                  \
3846 mejdrech 57
    }else{                                          \
58
        printf( "\tOK" );                           \
59
    }                                               \
3666 mejdrech 60
}
61
 
62
#if NET_SELF_TEST_INT_MAP
63
 
64
    INT_MAP_DECLARE( int_map, int );
65
 
66
    INT_MAP_IMPLEMENT( int_map, int );
67
 
68
#endif
69
 
70
#if NET_SELF_TEST_GENERIC_FIELD
71
 
72
    GENERIC_FIELD_DECLARE( int_field, int )
73
 
74
    GENERIC_FIELD_IMPLEMENT( int_field, int )
75
 
76
#endif
77
 
78
#if NET_SELF_TEST_GENERIC_CHAR_MAP
79
 
80
    GENERIC_CHAR_MAP_DECLARE( int_char_map, int )
81
 
82
    GENERIC_CHAR_MAP_IMPLEMENT( int_char_map, int )
83
 
84
#endif
85
 
86
int self_test( void ){
4075 mejdrech 87
    int error = 0;
3666 mejdrech 88
    int * x, * y, * z, * u, * v, * w;
89
 
90
#if NET_SELF_TEST_MEASURED_STRINGS
91
    measured_string_ref string;
92
 
93
    printf( "\nMeasured strings test" );
4075 mejdrech 94
    string = measured_string_create_bulk( "I am a measured string!", 0 );
3666 mejdrech 95
    printf( "\n%x, %s at %x of %d", string, string->value, string->value, string->length );
96
    printf( "\nOK" );
97
#endif
98
 
99
#if NET_SELF_TEST_CHAR_MAP
100
    char_map_t  cm;
101
 
102
    printf( "\nChar map test" );
3846 mejdrech 103
    TEST( "update ucho 3 einval", char_map_update( & cm, "ucho", 0, 3 ), EINVAL );
3666 mejdrech 104
    TEST( "initialize", char_map_initialize( & cm ), EOK );
105
    TEST( "get_value", char_map_get_value( & cm ), CHAR_MAP_NULL );
3846 mejdrech 106
    TEST( "exclude bla null", char_map_exclude( & cm, "bla", 0 ), CHAR_MAP_NULL );
107
    TEST( "find bla null", char_map_find( & cm, "bla", 0 ), CHAR_MAP_NULL );
108
    TEST( "add bla 1 eok", char_map_add( & cm, "bla", 0, 1 ), EOK );
109
    TEST( "find bla 1", char_map_find( & cm, "bla", 0 ), 1 );
110
    TEST( "add bla 10 eexists", char_map_add( & cm, "bla", 0, 10 ), EEXISTS );
111
    TEST( "update bla 2 eok", char_map_update( & cm, "bla", 0, 2 ), EOK );
112
    TEST( "find bla 2", char_map_find( & cm, "bla", 0 ), 2 );
113
    TEST( "update ucho 2 eok", char_map_update( & cm, "ucho", 0, 2 ), EOK );
114
    TEST( "exclude bla 2", char_map_exclude( & cm, "bla", 0 ), 2 );
115
    TEST( "exclude bla null", char_map_exclude( & cm, "bla", 0 ), CHAR_MAP_NULL );
116
    TEST( "find ucho 2", char_map_find( & cm, "ucho", 0 ), 2 );
117
    TEST( "update ucho 3 eok", char_map_update( & cm, "ucho", 0, 3 ), EOK );
118
    TEST( "find ucho 3", char_map_find( & cm, "ucho", 0 ), 3 );
119
    TEST( "add blabla 5 eok", char_map_add( & cm, "blabla", 0, 5 ), EOK );
120
    TEST( "find blabla 5", char_map_find( & cm, "blabla", 0 ), 5 );
121
    TEST( "add bla 6 eok", char_map_add( & cm, "bla", 0, 6 ), EOK );
122
    TEST( "find bla 6", char_map_find( & cm, "bla", 0 ), 6 );
123
    TEST( "exclude bla 6", char_map_exclude( & cm, "bla", 0 ), 6 );
124
    TEST( "find bla null", char_map_find( & cm, "bla", 0 ), CHAR_MAP_NULL );
125
    TEST( "find blabla 5", char_map_find( & cm, "blabla", 0 ), 5 );
126
    TEST( "add auto 7 eok", char_map_add( & cm, "auto", 0, 7 ), EOK );
127
    TEST( "find auto 7", char_map_find( & cm, "auto", 0 ), 7 );
128
    TEST( "add kara 8 eok", char_map_add( & cm, "kara", 0, 8 ), EOK );
129
    TEST( "find kara 8", char_map_find( & cm, "kara", 0 ), 8 );
130
    TEST( "add nic 9 eok", char_map_add( & cm, "nic", 0, 9 ), EOK );
131
    TEST( "find nic 9", char_map_find( & cm, "nic", 0 ), 9 );
132
    TEST( "find blabla 5", char_map_find( & cm, "blabla", 0 ), 5 );
3666 mejdrech 133
    printf( "\n\tdestroy" );
134
    char_map_destroy( & cm );
3846 mejdrech 135
    TEST( "update ucho 3 einval", char_map_update( & cm, "ucho", 0, 3 ), EINVAL );
3666 mejdrech 136
    printf( "\nOK" );
137
 
4075 mejdrech 138
    if( error ) return EINVAL;
139
 
3666 mejdrech 140
#endif
141
 
142
#if NET_SELF_TEST_INT_MAP
143
    int_map_t   im;
144
 
145
    x = ( int * ) malloc( sizeof( int ));
146
    y = ( int * ) malloc( sizeof( int ));
147
    z = ( int * ) malloc( sizeof( int ));
148
    u = ( int * ) malloc( sizeof( int ));
149
    v = ( int * ) malloc( sizeof( int ));
150
    w = ( int * ) malloc( sizeof( int ));
151
 
152
    im.magic = 0;
153
    printf( "\nInt map test" );
154
    TEST( "add 1 x einval", int_map_add( & im, 1, x ), EINVAL );
155
    TEST( "count -1", int_map_count( & im ), -1 );
156
    TEST( "initialize", int_map_initialize( & im ), EOK );
157
    TEST( "count 0", int_map_count( & im ), 0 );
158
    TEST( "find 1 null", int_map_find( & im, 1 ), NULL );
159
    TEST( "add 1 x 0", int_map_add( & im, 1, x ), 0 );
160
    TEST( "find 1 x", int_map_find( & im, 1 ), x );
161
    int_map_exclude( & im, 1 );
162
    TEST( "find 1 null", int_map_find( & im, 1 ), NULL );
163
    TEST( "add 1 y 1", int_map_add( & im, 1, y ), 1 );
164
    TEST( "find 1 y", int_map_find( & im, 1 ), y );
165
    TEST( "add 4 z 2", int_map_add( & im, 4, z ), 2 );
166
    TEST( "get 2 z", int_map_get_index( & im, 2 ), z );
167
    TEST( "find 4 z", int_map_find( & im, 4 ), z );
168
    TEST( "find 1 y", int_map_find( & im, 1 ), y );
169
    TEST( "count 3", int_map_count( & im ), 3 );
170
    TEST( "add 2 u 3", int_map_add( & im, 2, u ), 3 );
171
    TEST( "find 2 u", int_map_find( & im, 2 ), u );
172
    TEST( "add 3 v 4", int_map_add( & im, 3, v ), 4 );
173
    TEST( "find 3 v", int_map_find( & im, 3 ), v );
174
    TEST( "get 4 v", int_map_get_index( & im, 4 ), v );
175
    TEST( "add 6 w 5", int_map_add( & im, 6, w ), 5 );
176
    TEST( "find 6 w", int_map_find( & im, 6 ), w );
177
    TEST( "count 6", int_map_count( & im ), 6 );
178
    int_map_exclude( & im, 1 );
179
    TEST( "find 1 null", int_map_find( & im, 1 ), NULL );
180
    TEST( "find 2 u", int_map_find( & im, 2 ), u );
181
    int_map_exclude( & im, 7 );
182
    TEST( "find 2 u", int_map_find( & im, 2 ), u );
183
    TEST( "find 6 w", int_map_find( & im, 6 ), w );
184
    int_map_exclude_index( & im, 4 );
185
    TEST( "get 4 null", int_map_get_index( & im, 4 ), NULL );
186
    TEST( "find 3 null", int_map_find( & im, 3 ), NULL );
187
    printf( "\n\tdestroy" );
188
    int_map_destroy( & im );
189
    TEST( "count -1", int_map_count( & im ), -1 );
190
    printf( "\nOK" );
191
 
4075 mejdrech 192
    if( error ) return EINVAL;
193
 
3666 mejdrech 194
#endif
195
 
196
#if NET_SELF_TEST_GENERIC_FIELD
197
    int_field_t gf;
198
 
199
    x = ( int * ) malloc( sizeof( int ));
200
    y = ( int * ) malloc( sizeof( int ));
201
    z = ( int * ) malloc( sizeof( int ));
202
    u = ( int * ) malloc( sizeof( int ));
203
    v = ( int * ) malloc( sizeof( int ));
204
    w = ( int * ) malloc( sizeof( int ));
205
 
206
    gf.magic = 0;
207
    printf( "\nGeneric field test" );
208
    TEST( "add x einval", int_field_add( & gf, x ), EINVAL );
209
    TEST( "count -1", int_field_count( & gf ), -1 );
210
    TEST( "initialize", int_field_initialize( & gf ), EOK );
211
    TEST( "count 0", int_field_count( & gf ), 0 );
212
    TEST( "get 1 null", int_field_get_index( & gf, 1 ), NULL );
213
    TEST( "add x 0", int_field_add( & gf, x ), 0 );
214
    TEST( "get 0 x", int_field_get_index( & gf, 0 ), x );
215
    int_field_exclude_index( & gf, 0 );
216
    TEST( "get 0 null", int_field_get_index( & gf, 0 ), NULL );
217
    TEST( "add y 1", int_field_add( & gf, y ), 1 );
218
    TEST( "get 1 y", int_field_get_index( & gf, 1 ), y );
219
    TEST( "add z 2", int_field_add( & gf, z ), 2 );
220
    TEST( "get 2 z", int_field_get_index( & gf, 2 ), z );
221
    TEST( "get 1 y", int_field_get_index( & gf, 1 ), y );
222
    TEST( "count 3", int_field_count( & gf ), 3 );
223
    TEST( "add u 3", int_field_add( & gf, u ), 3 );
224
    TEST( "get 3 u", int_field_get_index( & gf, 3 ), u );
225
    TEST( "add v 4", int_field_add( & gf, v ), 4 );
226
    TEST( "get 4 v", int_field_get_index( & gf, 4 ), v );
227
    TEST( "add w 5", int_field_add( & gf, w ), 5 );
228
    TEST( "get 5 w", int_field_get_index( & gf, 5 ), w );
229
    TEST( "count 6", int_field_count( & gf ), 6 );
230
    int_field_exclude_index( & gf, 1 );
231
    TEST( "get 1 null", int_field_get_index( & gf, 1 ), NULL );
232
    TEST( "get 3 u", int_field_get_index( & gf, 3 ), u );
233
    int_field_exclude_index( & gf, 7 );
234
    TEST( "get 3 u", int_field_get_index( & gf, 3 ), u );
235
    TEST( "get 5 w", int_field_get_index( & gf, 5 ), w );
236
    int_field_exclude_index( & gf, 4 );
237
    TEST( "get 4 null", int_field_get_index( & gf, 4 ), NULL );
238
    printf( "\n\tdestroy" );
239
    int_field_destroy( & gf );
240
    TEST( "count -1", int_field_count( & gf ), -1 );
241
    printf( "\nOK" );
242
 
4075 mejdrech 243
    if( error ) return EINVAL;
244
 
3666 mejdrech 245
#endif
246
 
247
#if NET_SELF_TEST_GENERIC_CHAR_MAP
248
    int_char_map_t  icm;
249
 
250
    x = ( int * ) malloc( sizeof( int ));
251
    y = ( int * ) malloc( sizeof( int ));
252
    z = ( int * ) malloc( sizeof( int ));
253
    u = ( int * ) malloc( sizeof( int ));
254
    v = ( int * ) malloc( sizeof( int ));
255
    w = ( int * ) malloc( sizeof( int ));
256
 
257
    icm.magic = 0;
258
    printf( "\nGeneric char map test" );
3846 mejdrech 259
    TEST( "add ucho z einval", int_char_map_add( & icm, "ucho", 0, z ), EINVAL );
3666 mejdrech 260
    TEST( "initialize", int_char_map_initialize( & icm ), EOK );
261
    printf( "\n\texclude bla null" );
3846 mejdrech 262
    int_char_map_exclude( & icm, "bla", 0 );
263
    TEST( "find bla null", int_char_map_find( & icm, "bla", 0 ), NULL );
264
    TEST( "add bla x eok", int_char_map_add( & icm, "bla", 0, x ), EOK );
265
    TEST( "find bla x", int_char_map_find( & icm, "bla", 0 ), x );
266
    TEST( "add bla y eexists", int_char_map_add( & icm, "bla", 0, y ), EEXISTS );
3666 mejdrech 267
    printf( "\n\texclude bla y" );
3846 mejdrech 268
    int_char_map_exclude( & icm, "bla", 0 );
3666 mejdrech 269
    printf( "\n\texclude bla null" );
3846 mejdrech 270
    int_char_map_exclude( & icm, "bla", 0 );
271
    TEST( "add blabla v eok", int_char_map_add( & icm, "blabla", 0, v ), EOK );
272
    TEST( "find blabla v", int_char_map_find( & icm, "blabla", 0 ), v );
273
    TEST( "add bla w eok", int_char_map_add( & icm, "bla", 0, w ), EOK );
274
    TEST( "find bla w", int_char_map_find( & icm, "bla", 0 ), w );
3666 mejdrech 275
    printf( "\n\texclude bla" );
3846 mejdrech 276
    int_char_map_exclude( & icm, "bla", 0 );
277
    TEST( "find bla null", int_char_map_find( & icm, "bla", 0 ), NULL );
278
    TEST( "find blabla v", int_char_map_find( & icm, "blabla", 0 ), v );
279
    TEST( "add auto u eok", int_char_map_add( & icm, "auto", 0, u ), EOK );
280
    TEST( "find auto u", int_char_map_find( & icm, "auto", 0 ), u );
3666 mejdrech 281
    printf( "\n\tdestroy" );
282
    int_char_map_destroy( & icm );
3846 mejdrech 283
    TEST( "add ucho z einval", int_char_map_add( & icm, "ucho", 0, z ), EINVAL );
3666 mejdrech 284
    printf( "\nOK" );
285
 
4075 mejdrech 286
    if( error ) return EINVAL;
287
 
3666 mejdrech 288
#endif
289
 
4075 mejdrech 290
#if NET_SELF_TEST_CRC
291
    uint32_t    value;
292
 
293
    printf( "\nCRC computation test" );
294
    value = ~ compute_crc32( ~ 0, "123456789", 8 * 9 );
295
    TEST( "123456789", value, 0xCBF43926 );
296
    printf( "\t=> %X", value );
297
    value = ~ compute_crc32( ~ 0, "1", 8 );
298
    TEST( "1", value, 0x83DCEFB7 );
299
    printf( "\t=> %X", value );
300
    value = ~ compute_crc32( ~ 0, "12", 8 * 2 );
301
    TEST( "12", value, 0x4F5344CD );
302
    printf( "\t=> %X", value );
303
    value = ~ compute_crc32( ~ 0, "123", 8 * 3 );
304
    TEST( "123", value, 0x884863D2 );
305
    printf( "\t=> %X", value );
306
    value = ~ compute_crc32( ~ 0, "1234", 8 * 4 );
307
    TEST( "1234", value, 0x9BE3E0A3 );
308
    printf( "\t=> %X", value );
309
    value = ~ compute_crc32( ~ 0, "12345678", 8 * 8 );
310
    TEST( "12345678", value, 0x9AE0DAAF );
311
    printf( "\t=> %X", value );
312
    value = ~ compute_crc32( ~ 0, "ahoj pane", 8 * 9 );
313
    TEST( "ahoj pane", value, 0x5FC3D706 );
314
    printf( "\t=> %X", value );
315
 
316
    if( error ) return EINVAL;
317
 
318
#endif
319
 
3666 mejdrech 320
    return EOK;
321
}
322
 
323
#endif
324
 
325
/** @}
326
 */