Add ASSERT macro
This commit is contained in:
parent
2cf8782e91
commit
244bfe8740
31
src/c_kzg.h
31
src/c_kzg.h
|
@ -42,7 +42,7 @@ typedef enum {
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#define CHECK(cond) \
|
#define CHECK(cond) \
|
||||||
if (!(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(); \
|
abort(); \
|
||||||
}
|
}
|
||||||
#define TRY(result) \
|
#define TRY(result) \
|
||||||
|
@ -53,6 +53,11 @@ typedef enum {
|
||||||
abort(); \
|
abort(); \
|
||||||
} \
|
} \
|
||||||
}
|
}
|
||||||
|
#define ASSERT(cond) \
|
||||||
|
if (!(cond)) { \
|
||||||
|
printf("\n%s:%d: Failed ASSERT: %s\n", __FILE__, __LINE__, #cond); \
|
||||||
|
abort(); \
|
||||||
|
}
|
||||||
#else
|
#else
|
||||||
#define CHECK(cond) \
|
#define CHECK(cond) \
|
||||||
if (!(cond)) return C_KZG_BADARGS
|
if (!(cond)) return C_KZG_BADARGS
|
||||||
|
@ -62,11 +67,15 @@ typedef enum {
|
||||||
if (ret == C_KZG_MALLOC) return ret; \
|
if (ret == C_KZG_MALLOC) return ret; \
|
||||||
if (ret != C_KZG_OK) return C_KZG_ERROR; \
|
if (ret != C_KZG_OK) return C_KZG_ERROR; \
|
||||||
}
|
}
|
||||||
|
#define ASSERT(cond) \
|
||||||
|
if (!(cond)) return C_KZG_ERROR
|
||||||
#endif // DEBUG
|
#endif // DEBUG
|
||||||
|
|
||||||
/** @def CHECK
|
/** @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).
|
* 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`,
|
* - `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
|
/** @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).
|
* 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
|
* - `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
|
* @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
|
#endif // C_KZG_H
|
||||||
|
|
|
@ -124,7 +124,7 @@ C_KZG_RET recover_poly_from_samples(fr_t *reconstructed_data, fr_t *samples, uin
|
||||||
|
|
||||||
// Check all is well
|
// Check all is well
|
||||||
for (uint64_t i = 0; i < len_samples; i++) {
|
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
|
// 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
|
// Check all is well
|
||||||
for (uint64_t i = 0; i < len_samples; i++) {
|
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);
|
free(scratch);
|
||||||
|
|
Loading…
Reference in New Issue