Back to home page

LXR

 
 

    


File indexing completed on 2025-05-11 08:24:46

0001 /* SPDX-License-Identifier: BSD-2-Clause */
0002 
0003 /*
0004  *  COPYRIGHT (c) 1989-2012.
0005  *  On-Line Applications Research Corporation (OAR).
0006  *
0007  * Redistribution and use in source and binary forms, with or without
0008  * modification, are permitted provided that the following conditions
0009  * are met:
0010  * 1. Redistributions of source code must retain the above copyright
0011  *    notice, this list of conditions and the following disclaimer.
0012  * 2. Redistributions in binary form must reproduce the above copyright
0013  *    notice, this list of conditions and the following disclaimer in the
0014  *    documentation and/or other materials provided with the distribution.
0015  *
0016  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
0017  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
0018  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
0019  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
0020  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
0021  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
0022  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
0023  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
0024  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
0025  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
0026  * POSSIBILITY OF SUCH DAMAGE.
0027  */
0028 
0029 #ifdef HAVE_CONFIG_H
0030 #include "config.h"
0031 #endif
0032 
0033 #include <tmacros.h>
0034 #include "test_support.h"
0035 
0036 #include <stdio.h>
0037 #include <rtems/assoc.h>
0038 
0039 const char rtems_test_name[] = "SPASSOC 1";
0040 
0041 /* forward declarations to avoid warnings */
0042 rtems_task Init(rtems_task_argument argument);
0043 
0044 const rtems_assoc_t assoc_table_null[] = 
0045   {
0046     { NULL       , 0 , 0  },
0047     { "zero"     , 1 , 8  },
0048     { "one"      , 2 , 4  },
0049     { "two"      , 4 , 2  },
0050     { "three"    , 8 , 1  },
0051     { NULL       , -1, -1 }
0052   };
0053 
0054 const rtems_assoc_t assoc_table_default[] = 
0055   {
0056     { "(default)", 0 , 0  },
0057     { "zero"     , 1 , 8  },
0058     { "one"      , 2 , 4  },
0059     { "two"      , 4 , 2  },
0060     { "three"    , 8 , 1  },
0061     { NULL       , -1, -1 }
0062   };
0063 
0064 const rtems_assoc_t assoc_table[] = 
0065   {
0066     { "zero" , 1 , 8  },
0067     { "one"  , 2 , 4  },
0068     { "two"  , 4 , 2  },
0069     { "three", 8 , 1  },
0070     { NULL   , -1, -1 }
0071   };
0072 
0073 uint32_t local;
0074 uint32_t remote;
0075 const rtems_assoc_t *assoc_item;
0076 char *name;
0077 
0078 static void reset_name( void )
0079 {
0080   memset( name, 0, 40 );
0081 }
0082 
0083 static void test_assoc_32_to_string( void )
0084 {
0085   static const rtems_assoc_32_pair pairs[] = {
0086     { 1, "A" },
0087     { 2, "LOOOOONG" },
0088     { 4, "C" }
0089   };
0090   char buf[4];
0091   size_t len;
0092 
0093   len = rtems_assoc_32_to_string(
0094     0,
0095     buf,
0096     sizeof( buf ),
0097     pairs,
0098     RTEMS_ARRAY_SIZE( pairs ),
0099     ":",
0100     "D"
0101   );
0102   rtems_test_assert( len == 1 );
0103   rtems_test_assert( strcmp( buf, "D" ) == 0 );
0104 
0105   len = rtems_assoc_32_to_string(
0106     1,
0107     buf,
0108     sizeof( buf ),
0109     pairs,
0110     RTEMS_ARRAY_SIZE( pairs ),
0111     ":",
0112     "D"
0113   );
0114   rtems_test_assert( len == 1 );
0115   rtems_test_assert( strcmp( buf, "A" ) == 0 );
0116 
0117   len = rtems_assoc_32_to_string(
0118     5,
0119     buf,
0120     sizeof( buf ),
0121     pairs,
0122     RTEMS_ARRAY_SIZE( pairs ),
0123     ":",
0124     "D"
0125   );
0126   rtems_test_assert( len == 3 );
0127   rtems_test_assert( strcmp( buf, "A:C" ) == 0 );
0128 
0129   len = rtems_assoc_32_to_string(
0130     7,
0131     buf,
0132     sizeof( buf ),
0133     pairs,
0134     RTEMS_ARRAY_SIZE( pairs ),
0135     ":",
0136     "D"
0137   );
0138   rtems_test_assert( len == 12 );
0139   rtems_test_assert( strcmp( buf, "A:L" ) == 0 );
0140 }
0141 
0142 rtems_task Init(
0143   rtems_task_argument argument
0144 )
0145 {
0146   name = malloc(40);
0147   TEST_BEGIN();
0148 
0149   puts( "Init - get local by name -- OK" );
0150   local = rtems_assoc_local_by_name( assoc_table, "zero" );
0151   rtems_test_assert( local == 1 );
0152 
0153   puts( "Init - get local by name -- expect 0" );
0154   local = rtems_assoc_local_by_name( assoc_table, "four" );
0155   rtems_test_assert( local == 0 );
0156 
0157   puts( "Init - get local by remote bitfield -- OK" );
0158   local = rtems_assoc_local_by_remote_bitfield( assoc_table, 1 );
0159   rtems_test_assert( local == 8 );
0160 
0161   puts( "Init - get local by remote bitfield -- expect 0" );
0162   local = rtems_assoc_local_by_remote_bitfield( assoc_table, 0 );
0163   rtems_test_assert( local == 0 );
0164 
0165   puts( "Init - get local by remote -- OK" );
0166   local = rtems_assoc_local_by_remote( assoc_table, 1 );
0167   rtems_test_assert( local == 8 );
0168 
0169   puts( "Init - get local by remote -- expect 0" );
0170   local = rtems_assoc_local_by_remote( assoc_table, 0 );
0171   rtems_test_assert( local == 0 );
0172 
0173   reset_name();
0174   puts( "Init - get name by local bitfield -- OK" );
0175   name = rtems_assoc_name_by_local_bitfield( assoc_table, 1, name );
0176   rtems_test_assert ( !strcmp( name, "zero" ) );
0177 
0178   reset_name();
0179   puts( "Init - get name by local bitfield -- OK" );
0180   name = rtems_assoc_name_by_local_bitfield( assoc_table, 3, name );
0181   rtems_test_assert ( !strcmp( name, "zero one" ) );
0182 
0183   reset_name();
0184   puts( "Init - get name by local bitfield -- expect\"\"" );
0185   name = rtems_assoc_name_by_local_bitfield( assoc_table, 0, name );
0186   rtems_test_assert ( !strcmp( name, "" ) );
0187   
0188   reset_name();
0189   puts( "Init - get name by local -- OK" );
0190   rtems_test_assert( !strcmp( rtems_assoc_name_by_local( assoc_table, 1 ), 
0191                   "zero" ) );
0192   
0193   reset_name();
0194   puts( "Init - get name by local -- using bad value" );
0195   puts( rtems_assoc_name_by_local( assoc_table, 0 ) );
0196 
0197   reset_name();
0198   puts( "Init - get name by remote bitfield -- OK" );
0199   name = 
0200     rtems_assoc_name_by_remote_bitfield( assoc_table, 1, name );
0201   rtems_test_assert ( !strcmp( name, "three" ) );
0202 
0203   reset_name();
0204   puts( "Init - get name by remote bitfield -- OK" );
0205   name = 
0206     rtems_assoc_name_by_remote_bitfield( assoc_table, 3, name );
0207   rtems_test_assert ( !strcmp( name, "three two" ) );
0208 
0209   reset_name();
0210   puts( "Init - get name by remote bitfield -- expect\"\"" );
0211   name = 
0212     rtems_assoc_name_by_remote_bitfield( assoc_table, 0, name );
0213   rtems_test_assert ( !strcmp( name, "" ) );
0214   
0215   reset_name();
0216   puts( "Init - get name by remote -- OK" );
0217   rtems_test_assert( !strcmp( rtems_assoc_name_by_remote( assoc_table, 1 ),
0218                   "three" ) );
0219   
0220   reset_name();
0221   puts( "Init - get name by remote -- using bad value" );
0222   puts( rtems_assoc_name_by_remote( assoc_table, 0 ) );
0223 
0224   puts( "Init - get ptr by local -- OK" );
0225   assoc_item = rtems_assoc_ptr_by_local( assoc_table, 1 );
0226   rtems_test_assert( assoc_item == assoc_table );
0227 
0228   puts( "Init - get ptr by local -- expect NULL" );
0229   assoc_item = rtems_assoc_ptr_by_local( assoc_table, 0 );
0230   rtems_test_assert( assoc_item == 0 );
0231 
0232   puts( "Init - get ptr by remote -- OK" );
0233   assoc_item = rtems_assoc_ptr_by_remote( assoc_table, 8 );
0234   rtems_test_assert( assoc_item == assoc_table );
0235 
0236   puts( "Init - get ptr by remote -- expect NULL" );
0237   assoc_item = rtems_assoc_ptr_by_remote( assoc_table, 0 );
0238   rtems_test_assert( assoc_item == 0 );
0239 
0240   puts( "Init - get ptr by name -- OK" );
0241   assoc_item = rtems_assoc_ptr_by_name( assoc_table, "zero" );
0242   rtems_test_assert( assoc_item == assoc_table );
0243 
0244   puts( "Init - get ptr by name -- expect NULL" );
0245   assoc_item = rtems_assoc_ptr_by_name( assoc_table, "six" );
0246   rtems_test_assert( assoc_item == 0 );
0247 
0248   puts( "Init - get remote by local bitfield -- OK" );
0249   remote = rtems_assoc_remote_by_local_bitfield( assoc_table, 1 );
0250   rtems_test_assert( remote == 8 );
0251 
0252   puts( "Init - get remote by local bitfield -- expect 0" );
0253   remote = rtems_assoc_remote_by_local_bitfield( assoc_table, 0 );
0254   rtems_test_assert( remote == 0 );
0255 
0256   puts( "Init - get remote by local -- OK" );
0257   remote = rtems_assoc_remote_by_local( assoc_table, 1 );
0258   rtems_test_assert( remote == 8 );
0259 
0260   puts( "Init - get remote by local -- expect 0" );
0261   remote = rtems_assoc_remote_by_local( assoc_table, 0 );
0262   rtems_test_assert( remote == 0 );
0263 
0264   puts( "Init - get remote by name -- OK" );
0265   remote = rtems_assoc_remote_by_name( assoc_table, "zero" );
0266   rtems_test_assert( remote == 8 );
0267 
0268   puts( "Init - get remote by name -- expect 0" );
0269   remote = rtems_assoc_remote_by_name( assoc_table, "six" );
0270   rtems_test_assert( remote == 0 );
0271 
0272   puts( "Init - get ptr by name -- expect (default)" );
0273   assoc_item = rtems_assoc_ptr_by_name( assoc_table_default, "six" );
0274   rtems_test_assert( assoc_item == assoc_table_default );
0275 
0276   puts( "Init - get ptr by local -- expect (default)" );
0277   assoc_item = rtems_assoc_ptr_by_local( assoc_table_default, 0 );
0278   rtems_test_assert( assoc_item == assoc_table_default );
0279 
0280   puts( "Init - get ptr by remote -- expect (default)" );
0281   assoc_item = rtems_assoc_ptr_by_remote( assoc_table_default, 0 );
0282   rtems_test_assert( assoc_item == assoc_table_default );
0283 
0284   puts( "Init - get ptr by name -- expect NULL" );
0285   assoc_item = rtems_assoc_ptr_by_name( assoc_table_null, "six" );
0286   rtems_test_assert( assoc_item == 0 );
0287 
0288   puts( "Init - get ptr by local -- expect NULL" );
0289   assoc_item = rtems_assoc_ptr_by_local( assoc_table_null, 0 );
0290   rtems_test_assert( assoc_item == 0 );
0291 
0292   puts( "Init - get ptr by remote -- expect NULL" );
0293   assoc_item = rtems_assoc_ptr_by_remote( assoc_table_null, 0 );
0294   rtems_test_assert( assoc_item == 0 );
0295 
0296   free( name );
0297 
0298   test_assoc_32_to_string();
0299 
0300   TEST_END();
0301 
0302   rtems_test_exit(0);
0303 }
0304 
0305 /* configuration information */
0306 
0307 #define CONFIGURE_APPLICATION_NEEDS_SIMPLE_CONSOLE_DRIVER
0308 #define CONFIGURE_APPLICATION_NEEDS_CLOCK_DRIVER
0309 
0310 #define CONFIGURE_MAXIMUM_TASKS             1
0311 #define CONFIGURE_INITIAL_EXTENSIONS RTEMS_TEST_INITIAL_EXTENSION
0312 
0313 #define CONFIGURE_INIT_TASK_ATTRIBUTES RTEMS_FLOATING_POINT
0314 #define CONFIGURE_RTEMS_INIT_TASKS_TABLE
0315 
0316 #define CONFIGURE_INIT
0317 
0318 #include <rtems/confdefs.h>
0319 /* end of file */