Subversion Repositories HelenOS

Rev

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