hortinstein / NOSETEST

MIT License
1 stars 0 forks source link

Statement Expression Addition #3

Open hortinstein opened 4 years ago

hortinstein commented 4 years ago

Add this toy example I wrote somewhere for error handling


#include <stdio.h>

#define FAILURE 1
#define SUCCESS 0

#define DEBUG_ERROR(fmt, args...) fprintf(stderr, "ERROR: %s:%d:%s(): " fmt "\n", \
    __FILE__, __LINE__, __func__, ##args)

#define CHECK_ARGS(a,b) \
       ({ \
        if(a){ \
           DEBUG_ERROR(b); \
           goto fail;\
        }})

#define CHECK_SUCCESS(a,b) \
       ({ \
        if(FAILURE == a){ \
           DEBUG_ERROR(b); \
           goto fail;\
        }})

int check(int *a,int *b)
{
    CHECK_ARGS((!a || !b),"argument error");

    return SUCCESS;
fail:
    return FAILURE;
}

int main(void){

    int a=4;
    int b=5; 
    CHECK_SUCCESS(check(NULL,NULL),"check1 failed");
    CHECK_SUCCESS(check(&a,&b),"check2 failed");

    return SUCCESS;
fail:
    return FAILURE;
}