Add ASSERT macro

This commit is contained in:
Ben Edgington 2021-06-24 16:22:37 +01:00
parent 2cf8782e91
commit 244bfe8740
2 changed files with 30 additions and 5 deletions

View File

@ -42,7 +42,7 @@ typedef enum {
#include <stdio.h>
#define CHECK(cond) \
if (!(cond)) { \
printf("\n%s:%d: Failed ASSERT: %s\n", __FILE__, __LINE__, #cond); \
printf("\n%s:%d: Failed CHECK: %s\n", __FILE__, __LINE__, #cond); \
abort(); \
}
#define TRY(result) \
@ -53,6 +53,11 @@ typedef enum {
abort(); \
} \
}
#define ASSERT(cond) \
if (!(cond)) { \
printf("\n%s:%d: Failed ASSERT: %s\n", __FILE__, __LINE__, #cond); \
abort(); \
}
#else
#define CHECK(cond) \
if (!(cond)) return C_KZG_BADARGS
@ -62,11 +67,15 @@ typedef enum {
if (ret == C_KZG_MALLOC) return ret; \
if (ret != C_KZG_OK) return C_KZG_ERROR; \
}
#define ASSERT(cond) \
if (!(cond)) return C_KZG_ERROR
#endif // DEBUG
/** @def CHECK
*
* Handle errors.
* Test input parameters.
*
* Differs from `ASSERT` in returning `C_KZG_BADARGS`.
*
* This macro comes in two versions according to whether `DEBUG` is defined or not (`-DDEBUG` compiler flag).
* - `DEBUG` is undefined: when @p cond is false, return from the current function with the value `C_KZG_BADARGS`,
@ -79,7 +88,7 @@ typedef enum {
/** @def TRY
*
* Handle errors.
* Handle errors in called functions.
*
* This macro comes in two versions according to whether `DEBUG` is defined or not (`-DDEBUG` compiler flag).
* - `DEBUG` is undefined: if the @p result is not `C_KZG_OK`, return immediately with either `C_KZG_MALLOC` or
@ -89,4 +98,20 @@ typedef enum {
*
* @param result The function call result to be tested
*/
/** @def ASSERT
*
* Test the correctness of statements.
*
* Differs from `CHECK` in returning `C_KZG_ERROR`.
*
* This macro comes in two versions according to whether `DEBUG` is defined or not (`-DDEBUG` compiler flag).
* - `DEBUG` is undefined: when @p cond is false, return from the current function with the value `C_KZG_ERROR`,
* otherwise continue.
* - `DEBUG` is defined: when @p cond is false, print file and line number information and abort the run. This is very
* useful for dubugging. The @p ret parameter is ignored in this case.
*
* @param cond The condition to be tested
*/
#endif // C_KZG_H

View File

@ -124,7 +124,7 @@ C_KZG_RET recover_poly_from_samples(fr_t *reconstructed_data, fr_t *samples, uin
// Check all is well
for (uint64_t i = 0; i < len_samples; i++) {
TRY(fr_is_null(&samples[i]) == fr_is_zero(&zero_eval[i]) ? C_KZG_OK : C_KZG_ERROR);
ASSERT(fr_is_null(&samples[i]) == fr_is_zero(&zero_eval[i]));
}
// Construct E * Z_r,I: the loop makes the evaluation polynomial
@ -170,7 +170,7 @@ C_KZG_RET recover_poly_from_samples(fr_t *reconstructed_data, fr_t *samples, uin
// Check all is well
for (uint64_t i = 0; i < len_samples; i++) {
TRY(fr_is_null(&samples[i]) || fr_equal(&reconstructed_data[i], &samples[i]) ? C_KZG_OK : C_KZG_ERROR);
ASSERT(fr_is_null(&samples[i]) || fr_equal(&reconstructed_data[i], &samples[i]));
}
free(scratch);