Subversion Repositories HelenOS

Rev

Rev 4075 | Rev 4350 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

  1. /*
  2.  * Copyright (c) 2009 Lukas 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
  30.  *  @{
  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.  
  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"
  49.  
  50. #include "self_test.h"
  51.  
  52. #define TEST( name, function_call, result );    {   \
  53.     printf( "\n\t%s", ( name ));                    \
  54.     if(( function_call ) != ( result )){            \
  55.         printf( "\tERROR" );                        \
  56.         error = 1;                                  \
  57.     }else{                                          \
  58.         printf( "\tOK" );                           \
  59.     }                                               \
  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 ){
  87.     int error = 0;
  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" );
  94.     string = measured_string_create_bulk( "I am a measured string!", 0 );
  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" );
  103.     TEST( "update ucho 3 einval", char_map_update( & cm, "ucho", 0, 3 ), EINVAL );
  104.     TEST( "initialize", char_map_initialize( & cm ), EOK );
  105.     TEST( "get_value", char_map_get_value( & cm ), CHAR_MAP_NULL );
  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 );
  133.     TEST( "add micnicnic 5 9 eok", char_map_add( & cm, "micnicnic", 5, 9 ), EOK );
  134.     TEST( "find micni 9", char_map_find( & cm, "micni", 0 ), 9 );
  135.     TEST( "find micnicn 5 9", char_map_find( & cm, "micnicn", 5 ), 9 );
  136.     TEST( "add 10.0.2.2 4 15 eok", char_map_add( & cm, "\x10\x0\x2\x2", 4, 15 ), EOK );
  137.     TEST( "find 10.0.2.2 4 15", char_map_find( & cm, "\x10\x0\x2\x2", 4 ), 15 );
  138.     printf( "\n\tdestroy" );
  139.     char_map_destroy( & cm );
  140.     TEST( "update ucho 3 einval", char_map_update( & cm, "ucho", 0, 3 ), EINVAL );
  141.     printf( "\nOK" );
  142.  
  143.     if( error ) return EINVAL;
  144.  
  145. #endif
  146.  
  147. #if NET_SELF_TEST_INT_MAP
  148.     int_map_t   im;
  149.  
  150.     x = ( int * ) malloc( sizeof( int ));
  151.     y = ( int * ) malloc( sizeof( int ));
  152.     z = ( int * ) malloc( sizeof( int ));
  153.     u = ( int * ) malloc( sizeof( int ));
  154.     v = ( int * ) malloc( sizeof( int ));
  155.     w = ( int * ) malloc( sizeof( int ));
  156.  
  157.     im.magic = 0;
  158.     printf( "\nInt map test" );
  159.     TEST( "add 1 x einval", int_map_add( & im, 1, x ), EINVAL );
  160.     TEST( "count -1", int_map_count( & im ), -1 );
  161.     TEST( "initialize", int_map_initialize( & im ), EOK );
  162.     TEST( "count 0", int_map_count( & im ), 0 );
  163.     TEST( "find 1 null", int_map_find( & im, 1 ), NULL );
  164.     TEST( "add 1 x 0", int_map_add( & im, 1, x ), 0 );
  165.     TEST( "find 1 x", int_map_find( & im, 1 ), x );
  166.     int_map_exclude( & im, 1 );
  167.     TEST( "find 1 null", int_map_find( & im, 1 ), NULL );
  168.     TEST( "add 1 y 1", int_map_add( & im, 1, y ), 1 );
  169.     TEST( "find 1 y", int_map_find( & im, 1 ), y );
  170.     TEST( "add 4 z 2", int_map_add( & im, 4, z ), 2 );
  171.     TEST( "get 2 z", int_map_get_index( & im, 2 ), z );
  172.     TEST( "find 4 z", int_map_find( & im, 4 ), z );
  173.     TEST( "find 1 y", int_map_find( & im, 1 ), y );
  174.     TEST( "count 3", int_map_count( & im ), 3 );
  175.     TEST( "add 2 u 3", int_map_add( & im, 2, u ), 3 );
  176.     TEST( "find 2 u", int_map_find( & im, 2 ), u );
  177.     TEST( "add 3 v 4", int_map_add( & im, 3, v ), 4 );
  178.     TEST( "find 3 v", int_map_find( & im, 3 ), v );
  179.     TEST( "get 4 v", int_map_get_index( & im, 4 ), v );
  180.     TEST( "add 6 w 5", int_map_add( & im, 6, w ), 5 );
  181.     TEST( "find 6 w", int_map_find( & im, 6 ), w );
  182.     TEST( "count 6", int_map_count( & im ), 6 );
  183.     int_map_exclude( & im, 1 );
  184.     TEST( "find 1 null", int_map_find( & im, 1 ), NULL );
  185.     TEST( "find 2 u", int_map_find( & im, 2 ), u );
  186.     int_map_exclude( & im, 7 );
  187.     TEST( "find 2 u", int_map_find( & im, 2 ), u );
  188.     TEST( "find 6 w", int_map_find( & im, 6 ), w );
  189.     int_map_exclude_index( & im, 4 );
  190.     TEST( "get 4 null", int_map_get_index( & im, 4 ), NULL );
  191.     TEST( "find 3 null", int_map_find( & im, 3 ), NULL );
  192.     printf( "\n\tdestroy" );
  193.     int_map_destroy( & im );
  194.     TEST( "count -1", int_map_count( & im ), -1 );
  195.     printf( "\nOK" );
  196.  
  197.     if( error ) return EINVAL;
  198.  
  199. #endif
  200.  
  201. #if NET_SELF_TEST_GENERIC_FIELD
  202.     int_field_t gf;
  203.  
  204.     x = ( int * ) malloc( sizeof( int ));
  205.     y = ( int * ) malloc( sizeof( int ));
  206.     z = ( int * ) malloc( sizeof( int ));
  207.     u = ( int * ) malloc( sizeof( int ));
  208.     v = ( int * ) malloc( sizeof( int ));
  209.     w = ( int * ) malloc( sizeof( int ));
  210.  
  211.     gf.magic = 0;
  212.     printf( "\nGeneric field test" );
  213.     TEST( "add x einval", int_field_add( & gf, x ), EINVAL );
  214.     TEST( "count -1", int_field_count( & gf ), -1 );
  215.     TEST( "initialize", int_field_initialize( & gf ), EOK );
  216.     TEST( "count 0", int_field_count( & gf ), 0 );
  217.     TEST( "get 1 null", int_field_get_index( & gf, 1 ), NULL );
  218.     TEST( "add x 0", int_field_add( & gf, x ), 0 );
  219.     TEST( "get 0 x", int_field_get_index( & gf, 0 ), x );
  220.     int_field_exclude_index( & gf, 0 );
  221.     TEST( "get 0 null", int_field_get_index( & gf, 0 ), NULL );
  222.     TEST( "add y 1", int_field_add( & gf, y ), 1 );
  223.     TEST( "get 1 y", int_field_get_index( & gf, 1 ), y );
  224.     TEST( "add z 2", int_field_add( & gf, z ), 2 );
  225.     TEST( "get 2 z", int_field_get_index( & gf, 2 ), z );
  226.     TEST( "get 1 y", int_field_get_index( & gf, 1 ), y );
  227.     TEST( "count 3", int_field_count( & gf ), 3 );
  228.     TEST( "add u 3", int_field_add( & gf, u ), 3 );
  229.     TEST( "get 3 u", int_field_get_index( & gf, 3 ), u );
  230.     TEST( "add v 4", int_field_add( & gf, v ), 4 );
  231.     TEST( "get 4 v", int_field_get_index( & gf, 4 ), v );
  232.     TEST( "add w 5", int_field_add( & gf, w ), 5 );
  233.     TEST( "get 5 w", int_field_get_index( & gf, 5 ), w );
  234.     TEST( "count 6", int_field_count( & gf ), 6 );
  235.     int_field_exclude_index( & gf, 1 );
  236.     TEST( "get 1 null", int_field_get_index( & gf, 1 ), NULL );
  237.     TEST( "get 3 u", int_field_get_index( & gf, 3 ), u );
  238.     int_field_exclude_index( & gf, 7 );
  239.     TEST( "get 3 u", int_field_get_index( & gf, 3 ), u );
  240.     TEST( "get 5 w", int_field_get_index( & gf, 5 ), w );
  241.     int_field_exclude_index( & gf, 4 );
  242.     TEST( "get 4 null", int_field_get_index( & gf, 4 ), NULL );
  243.     printf( "\n\tdestroy" );
  244.     int_field_destroy( & gf );
  245.     TEST( "count -1", int_field_count( & gf ), -1 );
  246.     printf( "\nOK" );
  247.  
  248.     if( error ) return EINVAL;
  249.  
  250. #endif
  251.  
  252. #if NET_SELF_TEST_GENERIC_CHAR_MAP
  253.     int_char_map_t  icm;
  254.  
  255.     x = ( int * ) malloc( sizeof( int ));
  256.     y = ( int * ) malloc( sizeof( int ));
  257.     z = ( int * ) malloc( sizeof( int ));
  258.     u = ( int * ) malloc( sizeof( int ));
  259.     v = ( int * ) malloc( sizeof( int ));
  260.     w = ( int * ) malloc( sizeof( int ));
  261.  
  262.     icm.magic = 0;
  263.     printf( "\nGeneric char map test" );
  264.     TEST( "add ucho z einval", int_char_map_add( & icm, "ucho", 0, z ), EINVAL );
  265.     TEST( "initialize", int_char_map_initialize( & icm ), EOK );
  266.     printf( "\n\texclude bla null" );
  267.     int_char_map_exclude( & icm, "bla", 0 );
  268.     TEST( "find bla null", int_char_map_find( & icm, "bla", 0 ), NULL );
  269.     TEST( "add bla x eok", int_char_map_add( & icm, "bla", 0, x ), EOK );
  270.     TEST( "find bla x", int_char_map_find( & icm, "bla", 0 ), x );
  271.     TEST( "add bla y eexists", int_char_map_add( & icm, "bla", 0, y ), EEXISTS );
  272.     printf( "\n\texclude bla y" );
  273.     int_char_map_exclude( & icm, "bla", 0 );
  274.     printf( "\n\texclude bla null" );
  275.     int_char_map_exclude( & icm, "bla", 0 );
  276.     TEST( "add blabla v eok", int_char_map_add( & icm, "blabla", 0, v ), EOK );
  277.     TEST( "find blabla v", int_char_map_find( & icm, "blabla", 0 ), v );
  278.     TEST( "add bla w eok", int_char_map_add( & icm, "bla", 0, w ), EOK );
  279.     TEST( "find bla w", int_char_map_find( & icm, "bla", 0 ), w );
  280.     printf( "\n\texclude bla" );
  281.     int_char_map_exclude( & icm, "bla", 0 );
  282.     TEST( "find bla null", int_char_map_find( & icm, "bla", 0 ), NULL );
  283.     TEST( "find blabla v", int_char_map_find( & icm, "blabla", 0 ), v );
  284.     TEST( "add auto u eok", int_char_map_add( & icm, "auto", 0, u ), EOK );
  285.     TEST( "find auto u", int_char_map_find( & icm, "auto", 0 ), u );
  286.     printf( "\n\tdestroy" );
  287.     int_char_map_destroy( & icm );
  288.     TEST( "add ucho z einval", int_char_map_add( & icm, "ucho", 0, z ), EINVAL );
  289.     printf( "\nOK" );
  290.  
  291.     if( error ) return EINVAL;
  292.  
  293. #endif
  294.  
  295. #if NET_SELF_TEST_CRC
  296.     uint32_t    value;
  297.  
  298.     printf( "\nCRC computation test" );
  299.     value = ~ compute_crc32( ~ 0, "123456789", 8 * 9 );
  300.     TEST( "123456789", value, 0xCBF43926 );
  301.     printf( "\t=> %X", value );
  302.     value = ~ compute_crc32( ~ 0, "1", 8 );
  303.     TEST( "1", value, 0x83DCEFB7 );
  304.     printf( "\t=> %X", value );
  305.     value = ~ compute_crc32( ~ 0, "12", 8 * 2 );
  306.     TEST( "12", value, 0x4F5344CD );
  307.     printf( "\t=> %X", value );
  308.     value = ~ compute_crc32( ~ 0, "123", 8 * 3 );
  309.     TEST( "123", value, 0x884863D2 );
  310.     printf( "\t=> %X", value );
  311.     value = ~ compute_crc32( ~ 0, "1234", 8 * 4 );
  312.     TEST( "1234", value, 0x9BE3E0A3 );
  313.     printf( "\t=> %X", value );
  314.     value = ~ compute_crc32( ~ 0, "12345678", 8 * 8 );
  315.     TEST( "12345678", value, 0x9AE0DAAF );
  316.     printf( "\t=> %X", value );
  317.     value = ~ compute_crc32( ~ 0, "ahoj pane", 8 * 9 );
  318.     TEST( "ahoj pane", value, 0x5FC3D706 );
  319.     printf( "\t=> %X", value );
  320.  
  321.     if( error ) return EINVAL;
  322.  
  323. #endif
  324.  
  325.     return EOK;
  326. }
  327.  
  328. #endif
  329.  
  330. /** @}
  331.  */
  332.