Subversion Repositories HelenOS

Rev

Rev 4350 | Rev 4743 | 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
4350 mejdrech 34
 *  Self tests implementation.
3666 mejdrech 35
 */
36
 
37
#include "configuration.h"
38
 
39
#if NET_SELF_TEST
40
 
41
#include <errno.h>
42
#include <malloc.h>
43
#include <stdio.h>
44
 
4075 mejdrech 45
#include "include/crc.h"
46
#include "structures/int_map.h"
47
#include "structures/char_map.h"
48
#include "structures/generic_char_map.h"
49
#include "structures/measured_strings.h"
4588 mejdrech 50
#include "structures/dynamic_fifo.h"
3666 mejdrech 51
 
52
#include "self_test.h"
53
 
4350 mejdrech 54
/** Tests the function, compares the result and remembers if the result differs.
55
 *  @param name The test name. Input parameter.
56
 *  @param function_call The function to be called and checked. Input parameter.
57
 *  @param result The expected result. Input parameter.
58
 */
3846 mejdrech 59
#define TEST( name, function_call, result );    {   \
3666 mejdrech 60
    printf( "\n\t%s", ( name ));                    \
3846 mejdrech 61
    if(( function_call ) != ( result )){            \
4588 mejdrech 62
        printf( "\tERROR\n" );                      \
4075 mejdrech 63
        error = 1;                                  \
3846 mejdrech 64
    }else{                                          \
4588 mejdrech 65
        printf( "\tOK\n" );                         \
3846 mejdrech 66
    }                                               \
3666 mejdrech 67
}
68
 
69
#if NET_SELF_TEST_INT_MAP
70
 
71
    INT_MAP_DECLARE( int_map, int );
72
 
73
    INT_MAP_IMPLEMENT( int_map, int );
74
 
75
#endif
76
 
77
#if NET_SELF_TEST_GENERIC_FIELD
78
 
79
    GENERIC_FIELD_DECLARE( int_field, int )
80
 
81
    GENERIC_FIELD_IMPLEMENT( int_field, int )
82
 
83
#endif
84
 
85
#if NET_SELF_TEST_GENERIC_CHAR_MAP
86
 
87
    GENERIC_CHAR_MAP_DECLARE( int_char_map, int )
88
 
89
    GENERIC_CHAR_MAP_IMPLEMENT( int_char_map, int )
90
 
91
#endif
92
 
93
int self_test( void ){
4075 mejdrech 94
    int error = 0;
3666 mejdrech 95
    int * x, * y, * z, * u, * v, * w;
96
 
97
#if NET_SELF_TEST_MEASURED_STRINGS
98
    measured_string_ref string;
99
 
100
    printf( "\nMeasured strings test" );
4075 mejdrech 101
    string = measured_string_create_bulk( "I am a measured string!", 0 );
3666 mejdrech 102
    printf( "\n%x, %s at %x of %d", string, string->value, string->value, string->length );
103
    printf( "\nOK" );
104
#endif
105
 
106
#if NET_SELF_TEST_CHAR_MAP
107
    char_map_t  cm;
108
 
109
    printf( "\nChar map test" );
3846 mejdrech 110
    TEST( "update ucho 3 einval", char_map_update( & cm, "ucho", 0, 3 ), EINVAL );
3666 mejdrech 111
    TEST( "initialize", char_map_initialize( & cm ), EOK );
112
    TEST( "get_value", char_map_get_value( & cm ), CHAR_MAP_NULL );
3846 mejdrech 113
    TEST( "exclude bla null", char_map_exclude( & cm, "bla", 0 ), CHAR_MAP_NULL );
114
    TEST( "find bla null", char_map_find( & cm, "bla", 0 ), CHAR_MAP_NULL );
115
    TEST( "add bla 1 eok", char_map_add( & cm, "bla", 0, 1 ), EOK );
116
    TEST( "find bla 1", char_map_find( & cm, "bla", 0 ), 1 );
117
    TEST( "add bla 10 eexists", char_map_add( & cm, "bla", 0, 10 ), EEXISTS );
118
    TEST( "update bla 2 eok", char_map_update( & cm, "bla", 0, 2 ), EOK );
119
    TEST( "find bla 2", char_map_find( & cm, "bla", 0 ), 2 );
120
    TEST( "update ucho 2 eok", char_map_update( & cm, "ucho", 0, 2 ), EOK );
121
    TEST( "exclude bla 2", char_map_exclude( & cm, "bla", 0 ), 2 );
122
    TEST( "exclude bla null", char_map_exclude( & cm, "bla", 0 ), CHAR_MAP_NULL );
123
    TEST( "find ucho 2", char_map_find( & cm, "ucho", 0 ), 2 );
124
    TEST( "update ucho 3 eok", char_map_update( & cm, "ucho", 0, 3 ), EOK );
125
    TEST( "find ucho 3", char_map_find( & cm, "ucho", 0 ), 3 );
126
    TEST( "add blabla 5 eok", char_map_add( & cm, "blabla", 0, 5 ), EOK );
127
    TEST( "find blabla 5", char_map_find( & cm, "blabla", 0 ), 5 );
128
    TEST( "add bla 6 eok", char_map_add( & cm, "bla", 0, 6 ), EOK );
129
    TEST( "find bla 6", char_map_find( & cm, "bla", 0 ), 6 );
130
    TEST( "exclude bla 6", char_map_exclude( & cm, "bla", 0 ), 6 );
131
    TEST( "find bla null", char_map_find( & cm, "bla", 0 ), CHAR_MAP_NULL );
132
    TEST( "find blabla 5", char_map_find( & cm, "blabla", 0 ), 5 );
133
    TEST( "add auto 7 eok", char_map_add( & cm, "auto", 0, 7 ), EOK );
134
    TEST( "find auto 7", char_map_find( & cm, "auto", 0 ), 7 );
135
    TEST( "add kara 8 eok", char_map_add( & cm, "kara", 0, 8 ), EOK );
136
    TEST( "find kara 8", char_map_find( & cm, "kara", 0 ), 8 );
137
    TEST( "add nic 9 eok", char_map_add( & cm, "nic", 0, 9 ), EOK );
138
    TEST( "find nic 9", char_map_find( & cm, "nic", 0 ), 9 );
139
    TEST( "find blabla 5", char_map_find( & cm, "blabla", 0 ), 5 );
4332 mejdrech 140
    TEST( "add micnicnic 5 9 eok", char_map_add( & cm, "micnicnic", 5, 9 ), EOK );
141
    TEST( "find micni 9", char_map_find( & cm, "micni", 0 ), 9 );
142
    TEST( "find micnicn 5 9", char_map_find( & cm, "micnicn", 5 ), 9 );
143
    TEST( "add 10.0.2.2 4 15 eok", char_map_add( & cm, "\x10\x0\x2\x2", 4, 15 ), EOK );
144
    TEST( "find 10.0.2.2 4 15", char_map_find( & cm, "\x10\x0\x2\x2", 4 ), 15 );
3666 mejdrech 145
    printf( "\n\tdestroy" );
146
    char_map_destroy( & cm );
3846 mejdrech 147
    TEST( "update ucho 3 einval", char_map_update( & cm, "ucho", 0, 3 ), EINVAL );
3666 mejdrech 148
    printf( "\nOK" );
149
 
4075 mejdrech 150
    if( error ) return EINVAL;
151
 
3666 mejdrech 152
#endif
153
 
154
#if NET_SELF_TEST_INT_MAP
155
    int_map_t   im;
156
 
157
    x = ( int * ) malloc( sizeof( int ));
158
    y = ( int * ) malloc( sizeof( int ));
159
    z = ( int * ) malloc( sizeof( int ));
160
    u = ( int * ) malloc( sizeof( int ));
161
    v = ( int * ) malloc( sizeof( int ));
162
    w = ( int * ) malloc( sizeof( int ));
163
 
164
    im.magic = 0;
165
    printf( "\nInt map test" );
166
    TEST( "add 1 x einval", int_map_add( & im, 1, x ), EINVAL );
167
    TEST( "count -1", int_map_count( & im ), -1 );
168
    TEST( "initialize", int_map_initialize( & im ), EOK );
169
    TEST( "count 0", int_map_count( & im ), 0 );
170
    TEST( "find 1 null", int_map_find( & im, 1 ), NULL );
171
    TEST( "add 1 x 0", int_map_add( & im, 1, x ), 0 );
172
    TEST( "find 1 x", int_map_find( & im, 1 ), x );
173
    int_map_exclude( & im, 1 );
174
    TEST( "find 1 null", int_map_find( & im, 1 ), NULL );
175
    TEST( "add 1 y 1", int_map_add( & im, 1, y ), 1 );
176
    TEST( "find 1 y", int_map_find( & im, 1 ), y );
177
    TEST( "add 4 z 2", int_map_add( & im, 4, z ), 2 );
178
    TEST( "get 2 z", int_map_get_index( & im, 2 ), z );
179
    TEST( "find 4 z", int_map_find( & im, 4 ), z );
180
    TEST( "find 1 y", int_map_find( & im, 1 ), y );
181
    TEST( "count 3", int_map_count( & im ), 3 );
182
    TEST( "add 2 u 3", int_map_add( & im, 2, u ), 3 );
183
    TEST( "find 2 u", int_map_find( & im, 2 ), u );
184
    TEST( "add 3 v 4", int_map_add( & im, 3, v ), 4 );
185
    TEST( "find 3 v", int_map_find( & im, 3 ), v );
186
    TEST( "get 4 v", int_map_get_index( & im, 4 ), v );
187
    TEST( "add 6 w 5", int_map_add( & im, 6, w ), 5 );
188
    TEST( "find 6 w", int_map_find( & im, 6 ), w );
189
    TEST( "count 6", int_map_count( & im ), 6 );
190
    int_map_exclude( & im, 1 );
191
    TEST( "find 1 null", int_map_find( & im, 1 ), NULL );
192
    TEST( "find 2 u", int_map_find( & im, 2 ), u );
193
    int_map_exclude( & im, 7 );
194
    TEST( "find 2 u", int_map_find( & im, 2 ), u );
195
    TEST( "find 6 w", int_map_find( & im, 6 ), w );
196
    int_map_exclude_index( & im, 4 );
197
    TEST( "get 4 null", int_map_get_index( & im, 4 ), NULL );
198
    TEST( "find 3 null", int_map_find( & im, 3 ), NULL );
199
    printf( "\n\tdestroy" );
200
    int_map_destroy( & im );
201
    TEST( "count -1", int_map_count( & im ), -1 );
202
    printf( "\nOK" );
203
 
4075 mejdrech 204
    if( error ) return EINVAL;
205
 
3666 mejdrech 206
#endif
207
 
208
#if NET_SELF_TEST_GENERIC_FIELD
209
    int_field_t gf;
210
 
211
    x = ( int * ) malloc( sizeof( int ));
212
    y = ( int * ) malloc( sizeof( int ));
213
    z = ( int * ) malloc( sizeof( int ));
214
    u = ( int * ) malloc( sizeof( int ));
215
    v = ( int * ) malloc( sizeof( int ));
216
    w = ( int * ) malloc( sizeof( int ));
217
 
218
    gf.magic = 0;
219
    printf( "\nGeneric field test" );
220
    TEST( "add x einval", int_field_add( & gf, x ), EINVAL );
221
    TEST( "count -1", int_field_count( & gf ), -1 );
222
    TEST( "initialize", int_field_initialize( & gf ), EOK );
223
    TEST( "count 0", int_field_count( & gf ), 0 );
224
    TEST( "get 1 null", int_field_get_index( & gf, 1 ), NULL );
225
    TEST( "add x 0", int_field_add( & gf, x ), 0 );
226
    TEST( "get 0 x", int_field_get_index( & gf, 0 ), x );
227
    int_field_exclude_index( & gf, 0 );
228
    TEST( "get 0 null", int_field_get_index( & gf, 0 ), NULL );
229
    TEST( "add y 1", int_field_add( & gf, y ), 1 );
230
    TEST( "get 1 y", int_field_get_index( & gf, 1 ), y );
231
    TEST( "add z 2", int_field_add( & gf, z ), 2 );
232
    TEST( "get 2 z", int_field_get_index( & gf, 2 ), z );
233
    TEST( "get 1 y", int_field_get_index( & gf, 1 ), y );
234
    TEST( "count 3", int_field_count( & gf ), 3 );
235
    TEST( "add u 3", int_field_add( & gf, u ), 3 );
236
    TEST( "get 3 u", int_field_get_index( & gf, 3 ), u );
237
    TEST( "add v 4", int_field_add( & gf, v ), 4 );
238
    TEST( "get 4 v", int_field_get_index( & gf, 4 ), v );
239
    TEST( "add w 5", int_field_add( & gf, w ), 5 );
240
    TEST( "get 5 w", int_field_get_index( & gf, 5 ), w );
241
    TEST( "count 6", int_field_count( & gf ), 6 );
242
    int_field_exclude_index( & gf, 1 );
243
    TEST( "get 1 null", int_field_get_index( & gf, 1 ), NULL );
244
    TEST( "get 3 u", int_field_get_index( & gf, 3 ), u );
245
    int_field_exclude_index( & gf, 7 );
246
    TEST( "get 3 u", int_field_get_index( & gf, 3 ), u );
247
    TEST( "get 5 w", int_field_get_index( & gf, 5 ), w );
248
    int_field_exclude_index( & gf, 4 );
249
    TEST( "get 4 null", int_field_get_index( & gf, 4 ), NULL );
250
    printf( "\n\tdestroy" );
251
    int_field_destroy( & gf );
252
    TEST( "count -1", int_field_count( & gf ), -1 );
253
    printf( "\nOK" );
254
 
4075 mejdrech 255
    if( error ) return EINVAL;
256
 
3666 mejdrech 257
#endif
258
 
259
#if NET_SELF_TEST_GENERIC_CHAR_MAP
260
    int_char_map_t  icm;
261
 
262
    x = ( int * ) malloc( sizeof( int ));
263
    y = ( int * ) malloc( sizeof( int ));
264
    z = ( int * ) malloc( sizeof( int ));
265
    u = ( int * ) malloc( sizeof( int ));
266
    v = ( int * ) malloc( sizeof( int ));
267
    w = ( int * ) malloc( sizeof( int ));
268
 
269
    icm.magic = 0;
270
    printf( "\nGeneric char map test" );
3846 mejdrech 271
    TEST( "add ucho z einval", int_char_map_add( & icm, "ucho", 0, z ), EINVAL );
3666 mejdrech 272
    TEST( "initialize", int_char_map_initialize( & icm ), EOK );
273
    printf( "\n\texclude bla null" );
3846 mejdrech 274
    int_char_map_exclude( & icm, "bla", 0 );
275
    TEST( "find bla null", int_char_map_find( & icm, "bla", 0 ), NULL );
276
    TEST( "add bla x eok", int_char_map_add( & icm, "bla", 0, x ), EOK );
277
    TEST( "find bla x", int_char_map_find( & icm, "bla", 0 ), x );
278
    TEST( "add bla y eexists", int_char_map_add( & icm, "bla", 0, y ), EEXISTS );
3666 mejdrech 279
    printf( "\n\texclude bla y" );
3846 mejdrech 280
    int_char_map_exclude( & icm, "bla", 0 );
3666 mejdrech 281
    printf( "\n\texclude bla null" );
3846 mejdrech 282
    int_char_map_exclude( & icm, "bla", 0 );
283
    TEST( "add blabla v eok", int_char_map_add( & icm, "blabla", 0, v ), EOK );
284
    TEST( "find blabla v", int_char_map_find( & icm, "blabla", 0 ), v );
285
    TEST( "add bla w eok", int_char_map_add( & icm, "bla", 0, w ), EOK );
286
    TEST( "find bla w", int_char_map_find( & icm, "bla", 0 ), w );
3666 mejdrech 287
    printf( "\n\texclude bla" );
3846 mejdrech 288
    int_char_map_exclude( & icm, "bla", 0 );
289
    TEST( "find bla null", int_char_map_find( & icm, "bla", 0 ), NULL );
290
    TEST( "find blabla v", int_char_map_find( & icm, "blabla", 0 ), v );
291
    TEST( "add auto u eok", int_char_map_add( & icm, "auto", 0, u ), EOK );
292
    TEST( "find auto u", int_char_map_find( & icm, "auto", 0 ), u );
3666 mejdrech 293
    printf( "\n\tdestroy" );
294
    int_char_map_destroy( & icm );
3846 mejdrech 295
    TEST( "add ucho z einval", int_char_map_add( & icm, "ucho", 0, z ), EINVAL );
3666 mejdrech 296
    printf( "\nOK" );
297
 
4075 mejdrech 298
    if( error ) return EINVAL;
299
 
3666 mejdrech 300
#endif
301
 
4075 mejdrech 302
#if NET_SELF_TEST_CRC
303
    uint32_t    value;
304
 
305
    printf( "\nCRC computation test" );
306
    value = ~ compute_crc32( ~ 0, "123456789", 8 * 9 );
307
    TEST( "123456789", value, 0xCBF43926 );
308
    printf( "\t=> %X", value );
309
    value = ~ compute_crc32( ~ 0, "1", 8 );
310
    TEST( "1", value, 0x83DCEFB7 );
311
    printf( "\t=> %X", value );
312
    value = ~ compute_crc32( ~ 0, "12", 8 * 2 );
313
    TEST( "12", value, 0x4F5344CD );
314
    printf( "\t=> %X", value );
315
    value = ~ compute_crc32( ~ 0, "123", 8 * 3 );
316
    TEST( "123", value, 0x884863D2 );
317
    printf( "\t=> %X", value );
318
    value = ~ compute_crc32( ~ 0, "1234", 8 * 4 );
319
    TEST( "1234", value, 0x9BE3E0A3 );
320
    printf( "\t=> %X", value );
321
    value = ~ compute_crc32( ~ 0, "12345678", 8 * 8 );
322
    TEST( "12345678", value, 0x9AE0DAAF );
323
    printf( "\t=> %X", value );
324
    value = ~ compute_crc32( ~ 0, "ahoj pane", 8 * 9 );
325
    TEST( "ahoj pane", value, 0x5FC3D706 );
326
    printf( "\t=> %X", value );
327
 
328
    if( error ) return EINVAL;
329
 
330
#endif
331
 
4588 mejdrech 332
#if NET_SELF_TEST_DYNAMIC_FIFO
333
    dyn_fifo_t  fifo;
334
 
335
    printf( "\nDynamic fifo test" );
336
    TEST( "add 1 einval", dyn_fifo_push( & fifo, 1, 0 ), EINVAL );
337
    TEST( "initialize", dyn_fifo_initialize( & fifo, 1 ), EOK );
338
    TEST( "add 1 eok", dyn_fifo_push( & fifo, 1, 0 ), EOK );
339
    TEST( "pop 1", dyn_fifo_pop( & fifo ), 1 );
340
    TEST( "pop enoent", dyn_fifo_pop( & fifo ), ENOENT );
341
    TEST( "add 2 eok", dyn_fifo_push( & fifo, 2, 1 ), EOK );
342
    TEST( "add 3 enomem", dyn_fifo_push( & fifo, 3, 1 ), ENOMEM );
343
    TEST( "add 3 eok", dyn_fifo_push( & fifo, 3, 0 ), EOK );
344
    TEST( "pop 2", dyn_fifo_pop( & fifo ), 2 );
345
    TEST( "pop 3", dyn_fifo_pop( & fifo ), 3 );
346
    TEST( "add 4 eok", dyn_fifo_push( & fifo, 4, 2 ), EOK );
347
    TEST( "add 5 eok", dyn_fifo_push( & fifo, 5, 2 ), EOK );
348
    TEST( "add 6 enomem", dyn_fifo_push( & fifo, 6, 2 ), ENOMEM );
349
    TEST( "add 6 eok", dyn_fifo_push( & fifo, 6, 5 ), EOK );
350
    TEST( "add 7 eok", dyn_fifo_push( & fifo, 7, 5 ), EOK );
351
    TEST( "pop 4", dyn_fifo_pop( & fifo ), 4 );
352
    TEST( "pop 5", dyn_fifo_pop( & fifo ), 5 );
353
    TEST( "add 8 eok", dyn_fifo_push( & fifo, 8, 5 ), EOK );
354
    TEST( "add 9 eok", dyn_fifo_push( & fifo, 9, 5 ), EOK );
355
    TEST( "add 10 eok", dyn_fifo_push( & fifo, 10, 6 ), EOK );
356
    TEST( "add 11 eok", dyn_fifo_push( & fifo, 11, 6 ), EOK );
357
    TEST( "pop 6", dyn_fifo_pop( & fifo ), 6 );
358
    TEST( "pop 7", dyn_fifo_pop( & fifo ), 7 );
359
    TEST( "add 12 eok", dyn_fifo_push( & fifo, 12, 6 ), EOK );
360
    TEST( "add 13 eok", dyn_fifo_push( & fifo, 13, 6 ), EOK );
361
    TEST( "add 14 enomem", dyn_fifo_push( & fifo, 14, 6 ), ENOMEM );
362
    TEST( "add 14 eok", dyn_fifo_push( & fifo, 14, 8 ), EOK );
363
    TEST( "pop 8", dyn_fifo_pop( & fifo ), 8 );
364
    TEST( "pop 9", dyn_fifo_pop( & fifo ), 9 );
365
    TEST( "pop 10", dyn_fifo_pop( & fifo ), 10 );
366
    TEST( "pop 11", dyn_fifo_pop( & fifo ), 11 );
367
    TEST( "pop 12", dyn_fifo_pop( & fifo ), 12 );
368
    TEST( "pop 13", dyn_fifo_pop( & fifo ), 13 );
369
    TEST( "pop 14", dyn_fifo_pop( & fifo ), 14 );
370
    TEST( "destroy", dyn_fifo_destroy( & fifo ), EOK );
371
    TEST( "add 15 einval", dyn_fifo_push( & fifo, 1, 0 ), EINVAL );
372
    if( error ) return EINVAL;
373
 
374
#endif
375
 
3666 mejdrech 376
    return EOK;
377
}
378
 
379
#endif
380
 
381
/** @}
382
 */