mardi 10 novembre 2020

Why does the compiler want a semicolon before this function that returns a struct?

I am attempting to write a property based test using theft (https://github.com/silentbicycle/theft/blob/master/doc/usage.md)

Following the above guide and also the advice from this blog post (https://yakking.branchable.com/posts/property-testing-in-c/)

I wrote the following basic test code:

#include "theft.h"
#include "limits.h"

// The following tested function was adapted from the original Doom source code
//This was originally written for a platform with a 32bit word.
//For the sake of discussion I am not fixing Carmack's code.

//Instead of maketic being a Global var mutated by a different function, we are making a param.

int ExpandTics (int low, int maketic)
{
    int delta;

    delta = low - (maketic&0xff);

    if (delta >= -64 && delta <= 64)
        return (maketic&~0xff) + low;
    if (delta > 64)
        return (maketic&~0xff) - 256 + low;
    if (delta < -64)
        return (maketic&~0xff) + 256 + low;

    printf("ExpandTics: strange value %i at maketic %i",low,maketic);
    return 0;
}


struct IntArray {
    int len;
    int arr[];
};

//Adapted from https://yakking.branchable.com/posts/property-testing-in-c/

enum theft_alloc_res allocate_int_array(struct theft *t, void *data, void **result)
{
    //We need only two ints for this
    int SIZE_LIMIT = 2;

    int size = theft_random_choice(t, SIZE_LIMIT);

    struct IntArray *numbers = malloc(sizeof (struct IntArray) + size * sizeof(int));

    if (numbers == NULL) {
        return THEFT_ALLOC_ERROR;
    }

    for (int i = 0; i < size; i++) {
        numbers->arr[i] = theft_random_choice(t, INT_MAX);
    }

    numbers->len = size;

    *result = numbers;

    return THEFT_ALLOC_OK;
}

static struct theft_type_info random_array_info = {
        .alloc = allocate_int_array,
        .free = theft_generic_free_cb,
        .autoshrink_config = {
                .enable = true,
        }

}

enum theft_trial_res prop_no_zero_no_neg_1(struct theft *t, void *arg1){
        struct IntArray *test_array = test_input;
        //I think that's legit C
        int result = ExpandTics(test_array.arr[0],test_array.arr[1])
        if(result == 0 || result == -1){
                return THEFT_TRIAL_FAIL;
        }

        return THEFT_TRIAL_PASS;

}

int main(int argc, char  *argv[]){

        theft_seed seed = theft_seed_of_time();
        struct theft_run_config config = {
                .name = __func__,
                .prop1 = prop_no_zero_no_neg_1,
                .type_info = { &random_array_info },
                .seed = seed
        };

        return (theft_run(&config) == THEFT_RUN_PASS) ? EXIT_SUCCESS : EXIT_FAILURE;
}

The compiler emits the following error:

vagrant@vagrant-ubuntu-trusty-64:/vagrant$ gcc -std=c99 -o theft_test.o theft_doom.c -ltheft
theft_doom.c:67:1: error: expected ‘,’ or ‘;’ before ‘enum’
 enum theft_trial_res prop_no_zero_no_neg_1(struct theft *t, void *arg1){
 ^
theft_doom.c: In function ‘main’:
theft_doom.c:85:19: error: ‘prop_no_zero_no_neg_1’ undeclared (first use in this function)
          .prop1 = prop_no_zero_no_neg_1,

So my interpretation of the code I adapted from the blogpost is that the function "prop_no_zero_no_neg_1" returns a "theft_trial_res" enum. If that's the case, why does compiler think it needs a semicolon before "enum"?

the definition of the enum should be in the theft.h header file. Am I missing a prototype somewhere or something? My tired brain is failing to resolve this apparently simple syntax error.

Aucun commentaire:

Enregistrer un commentaire