RicardasSim icon

testing indexes

RicardasSim | PRO | 10/06/19 08:01:34 PM UTC | 0 ⭐ | 623 👁️ | Never ⏰ | []
C |

2.8 KB

|

None

|

0 👍

/

0 👎

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
 
#define elemCount 4
 
enum flags{
    FLAG1   = 0x00000001,
    FLAG2   = 0x00000002
};
 
int32_t g_testArray_1[elemCount];
int32_t g_testArray_2[elemCount];
 
int32_t g_flag_1;
int32_t g_flag_2;
 
uint32_t i;
 
void resetVal()
{
    memset (g_testArray_1, 0, sizeof *g_testArray_1 * elemCount);
    memset (g_testArray_2, 0, sizeof *g_testArray_2 * elemCount);
 
    g_flag_1 = -1;
    g_flag_2 = -1;
}
 
/*
find the first matching index for g_testArray_1,FLAG1 and g_testArray_2,FLAG2
if there is none, find first for g_testArray_1,FLAG1  and first for g_testArray_2,FLAG2
*/
 
void checkIndexes()
{
 
    for( i=0; i < elemCount; ++i )
    {
        if( g_testArray_1[i] & FLAG1 && g_testArray_2[i] & FLAG2 )
        {
            g_flag_1 = i;
            g_flag_2 = i;
            return;
        }
 
        if( g_flag_1 == -1 )
        {
            if( g_testArray_1[i] & FLAG1) g_flag_1 = i;
        }
 
        if( g_flag_2 == -1 )
        {
            if( g_testArray_2[i] & FLAG2) g_flag_2 = i;
        }
 
    }
 
}
 
void printRezult()
{
    //pass only if g_flag_1 and g_flag_2 not -1
    if( g_flag_1 == -1 || g_flag_2 == -1 )
    {
        printf("did not pass\n");
        if( g_flag_1 == -1) printf("g_flag_1, index not found.\n");
        if( g_flag_2 == -1) printf("g_flag_2, index not found.\n");
    }
    else
    {
        printf("has passed\n");
        printf("g_flag_1 index: %d\n", g_flag_1);
        printf("g_flag_2 index: %d\n", g_flag_2);
    }
}
 
int main()
{
 
    //Test 1
    printf("Test 1: ");
 
    resetVal();
 
    g_testArray_1[0] = 1;
    g_testArray_2[0] = 2;
 
    checkIndexes();
    printRezult();
 
    //Test 2
    printf("Test 2: ");
 
    resetVal();
 
    g_testArray_1[0] = 1;
    g_testArray_2[1] = 2;
    g_testArray_1[3] = 1;
    g_testArray_2[3] = 2;
 
    checkIndexes();
    printRezult();
 
    //Test 3
    printf("Test 3: ");
 
    resetVal();
 
    g_testArray_1[0] = 1;
    g_testArray_2[0] = 2;
    g_testArray_1[3] = 1;
    g_testArray_2[3] = 2;
 
    checkIndexes();
    printRezult();
 
    //Test 4
    printf("Test 4: ");
 
    resetVal();
 
    g_testArray_1[1] = 1;
    g_testArray_2[2] = 2;
 
    checkIndexes();
    printRezult();
 
    //Test 5
    printf("Test 5: ");
 
    resetVal();
 
    g_testArray_1[0] = 1;
    g_testArray_2[1] = 1;
 
    checkIndexes();
    printRezult();
 
    return 0;
}
 
/*
output:
 
Test 1: has passed
g_flag_1 index: 0
g_flag_2 index: 0
Test 2: has passed
g_flag_1 index: 3
g_flag_2 index: 3
Test 3: has passed
g_flag_1 index: 0
g_flag_2 index: 0
Test 4: has passed
g_flag_1 index: 1
g_flag_2 index: 2
Test 5: did not pass
g_flag_2, index not found.
 
*/

Comments