RicardasSim

testing indexes

Oct 6th, 2019
379
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.80 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <stdint.h>
  5.  
  6. #define elemCount 4
  7.  
  8. enum flags{
  9.     FLAG1   = 0x00000001,
  10.     FLAG2   = 0x00000002
  11. };
  12.  
  13. int32_t g_testArray_1[elemCount];
  14. int32_t g_testArray_2[elemCount];
  15.  
  16. int32_t g_flag_1;
  17. int32_t g_flag_2;
  18.  
  19. uint32_t i;
  20.  
  21. void resetVal()
  22. {
  23.     memset (g_testArray_1, 0, sizeof *g_testArray_1 * elemCount);
  24.     memset (g_testArray_2, 0, sizeof *g_testArray_2 * elemCount);
  25.  
  26.     g_flag_1 = -1;
  27.     g_flag_2 = -1;
  28. }
  29.  
  30. /*
  31. find the first matching index for g_testArray_1,FLAG1 and g_testArray_2,FLAG2
  32. if there is none, find first for g_testArray_1,FLAG1  and first for g_testArray_2,FLAG2
  33. */
  34.  
  35. void checkIndexes()
  36. {
  37.  
  38.     for( i=0; i < elemCount; ++i )
  39.     {
  40.         if( g_testArray_1[i] & FLAG1 && g_testArray_2[i] & FLAG2 )
  41.         {
  42.             g_flag_1 = i;
  43.             g_flag_2 = i;
  44.             return;
  45.         }
  46.  
  47.         if( g_flag_1 == -1 )
  48.         {
  49.             if( g_testArray_1[i] & FLAG1) g_flag_1 = i;
  50.         }
  51.  
  52.         if( g_flag_2 == -1 )
  53.         {
  54.             if( g_testArray_2[i] & FLAG2) g_flag_2 = i;
  55.         }
  56.  
  57.     }
  58.  
  59. }
  60.  
  61. void printRezult()
  62. {
  63.     //pass only if g_flag_1 and g_flag_2 not -1
  64.     if( g_flag_1 == -1 || g_flag_2 == -1 )
  65.     {
  66.         printf("did not pass\n");
  67.         if( g_flag_1 == -1) printf("g_flag_1, index not found.\n");
  68.         if( g_flag_2 == -1) printf("g_flag_2, index not found.\n");
  69.     }
  70.     else
  71.     {
  72.         printf("has passed\n");
  73.         printf("g_flag_1 index: %d\n", g_flag_1);
  74.         printf("g_flag_2 index: %d\n", g_flag_2);
  75.     }
  76. }
  77.  
  78. int main()
  79. {
  80.  
  81.     //Test 1
  82.     printf("Test 1: ");
  83.  
  84.     resetVal();
  85.  
  86.     g_testArray_1[0] = 1;
  87.     g_testArray_2[0] = 2;
  88.  
  89.     checkIndexes();
  90.     printRezult();
  91.  
  92.     //Test 2
  93.     printf("Test 2: ");
  94.  
  95.     resetVal();
  96.  
  97.     g_testArray_1[0] = 1;
  98.     g_testArray_2[1] = 2;
  99.     g_testArray_1[3] = 1;
  100.     g_testArray_2[3] = 2;
  101.  
  102.     checkIndexes();
  103.     printRezult();
  104.  
  105.     //Test 3
  106.     printf("Test 3: ");
  107.  
  108.     resetVal();
  109.  
  110.     g_testArray_1[0] = 1;
  111.     g_testArray_2[0] = 2;
  112.     g_testArray_1[3] = 1;
  113.     g_testArray_2[3] = 2;
  114.  
  115.     checkIndexes();
  116.     printRezult();
  117.  
  118.     //Test 4
  119.     printf("Test 4: ");
  120.  
  121.     resetVal();
  122.  
  123.     g_testArray_1[1] = 1;
  124.     g_testArray_2[2] = 2;
  125.  
  126.     checkIndexes();
  127.     printRezult();
  128.  
  129.     //Test 5
  130.     printf("Test 5: ");
  131.  
  132.     resetVal();
  133.  
  134.     g_testArray_1[0] = 1;
  135.     g_testArray_2[1] = 1;
  136.  
  137.     checkIndexes();
  138.     printRezult();
  139.  
  140.     return 0;
  141. }
  142.  
  143. /*
  144. output:
  145.  
  146. Test 1: has passed
  147. g_flag_1 index: 0
  148. g_flag_2 index: 0
  149. Test 2: has passed
  150. g_flag_1 index: 3
  151. g_flag_2 index: 3
  152. Test 3: has passed
  153. g_flag_1 index: 0
  154. g_flag_2 index: 0
  155. Test 4: has passed
  156. g_flag_1 index: 1
  157. g_flag_2 index: 2
  158. Test 5: did not pass
  159. g_flag_2, index not found.
  160.  
  161. */
Add Comment
Please, Sign In to add comment