jeudi 29 octobre 2020

How to assert test bool functions and switch statements

I want to be able to assert test all of my functions to ensure that they are working correctly, however I'm having difficulty in figuring out how to do this for (basically any of my functions but mainly) bool functions and switch statements. One method that was suggested was to create a histogram for the probability functions but implementing this has proven difficult, so any guidance would be appreciated.

#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#include <assert.h>

#define ROWLENGTH 30 + BUFF
#define COLLENGTH 80 + BUFF
#define BUFF 1
#define GENERATIONS 1000
#define G 250
#define L (10 * G)

enum { space = 0, tree = 1, fire = 2 };
typedef enum { false, true } bool;

void test(void);
void newGen(int a[][COLLENGTH]);
void copyArray(int a[][COLLENGTH], int b[][COLLENGTH]);
int stateSwitch(int a, bool b);
bool neighCheck(int i, int j, int a[][COLLENGTH]);
bool probabTree(void);
bool probabFire(void);
void replace(int a[][COLLENGTH]);

int main(void)
{
    int forest[ROWLENGTH][COLLENGTH] = { {0}, {0} };
    int i;

    test();

    srand(time(NULL));

    for (i = 1; i <= GENERATIONS; i++) {
        printf("\nGen %d:\n", i);
        newGen(forest);
    }
    test();
    return 0;
}

void test(void)
{
    int testArr[ROWLENGTH][COLLENGTH] = { {0}, {0} };
    int testArr2[ROWLENGTH][COLLENGTH];
    int testArr3[ROWLENGTH][COLLENGTH];

    int i, j;

    for (i = 1; i < ROWLENGTH; i++) {
        for (j = 1; j < COLLENGTH; j++) {
            assert(neighCheck(i, j, testArr) == true ||
                neighCheck(i, j, testArr) == false);
        }
    }

    copyArray(testArr, testArr2);
    copyArray(testArr2, testArr3);
    assert(testArr2[5][5] == testArr3[5][5]);
}

void newGen(int arr[][COLLENGTH])
{
    int newForest[ROWLENGTH][COLLENGTH] = { {0}, {0} };
    int i, j, currCell;

    for (i = 1; i < ROWLENGTH; i++) {
        for (j = 1; j < COLLENGTH; j++) {
            currCell = arr[i][j];

            /* Adds the state returned from the sS function to the array*/
            newForest[i][j] = stateSwitch(currCell, neighCheck(i, j, arr));
        }
    }
    replace(newForest);

    copyArray(newForest, arr);
}

/* Checks neighbours to see if they are or should be on fire */
bool neighCheck(int i, int j, int a[][COLLENGTH])
{
    bool neighbourOnFire;
    int x, y, neighX, neighY, curreNeigh;

    /* Bool set up to change after neighbours looped*/
    neighbourOnFire = false;
    /* The neighbours -looping from -1 -> 1 to get index of each neighbour*/
    for (x = -1; x < 2; x++) {
        for (y = -1; y < 2; y++) {
            /* Disregards current (middle) cell*/
            if (!((x == 0) && (y == 0))) {
                /* Get indexes of the neighbour we're looking at */
                neighX = i + x;
                neighY = j + y;
                /* Checks for edges*/
                if (neighX >= 0 && neighY >= 0 && neighX < ROWLENGTH &&
                    neighY < COLLENGTH) {
                    /* Get the neighbour using the indexes above */
                    curreNeigh = a[neighX][neighY];

                    if (curreNeigh == fire) {
                        neighbourOnFire = true;
                    }
                }
            }
        }
    }
    return neighbourOnFire;
}

/* Function that determines the state of the array's cells */
int stateSwitch(int a, bool b)
{
    int newState;

    newState = space;

    switch (a) {
    case space:
        newState = probabTree();
        break;
    case tree:
        if (b || probabFire()) {
            newState = fire;
        }
        else {
            newState = tree;
        }
        break;
    case fire:
        newState = space;
        break;
    }
    return newState;
}

bool probabTree(void)
{
    if ((rand() % G) == 0) {
        return true;
    }
    else {
        return false;
    }
}

bool probabFire(void)
{
    if ((rand() % L) == 0) {
        return true;
    }
    else {
        return false;
    }
}
/* Replaces the states, 0, 1, 2, with characters,  , @, * */
void replace(int a[][COLLENGTH])
{
    int i, j;

    for (i = 1; i < ROWLENGTH; i++) {
        for (j = 1; j < COLLENGTH; j++) {
            if (a[i][j] == space) {
                printf("%c", ' ');
            }
            if (a[i][j] == tree) {
                printf("%c", '@');
            }
            if (a[i][j] == fire) {
                printf("%c", '*');
            }
            if (j % COLLENGTH - BUFF == 0) {
                printf("\n");
            }
        }
    }
}

/* To copy one 2D array into another */
void copyArray(int a[][COLLENGTH], int b[][COLLENGTH])
{
    int i, j;
    for (i = 0; i < ROWLENGTH; i++) {
        for (j = 0; j < COLLENGTH; j++) {
            b[i][j] = a[i][j];
        }
    }
}

Aucun commentaire:

Enregistrer un commentaire