add nodiscard annotations to fix warnings in msvc

This commit is contained in:
Daan Leijen 2022-04-19 10:10:10 -07:00
parent e1b27a0fc2
commit 5b172280b0
4 changed files with 74 additions and 39 deletions

View file

@ -57,7 +57,9 @@ int main(void) {
// ---------------------------------------------------
CHECK_BODY("malloc-zero",{
void* p = mi_malloc(0); mi_free(p);
void* p = mi_malloc(0);
result = (p != NULL);
mi_free(p);
});
CHECK_BODY("malloc-nomem1",{
result = (mi_malloc((size_t)PTRDIFF_MAX + (size_t)1) == NULL);
@ -174,6 +176,21 @@ int main(void) {
result = ok;
});
// ---------------------------------------------------
// Reallocation
// ---------------------------------------------------
CHECK_BODYX("realloc-null1") {
void* p = mi_realloc(NULL,4);
result = (p != NULL);
mi_free(p);
};
CHECK_BODYX("realloc-null2") {
void* p = mi_realloc(NULL,0); // <https://en.cppreference.com/w/c/memory/realloc> "If ptr is NULL, the behavior is the same as calling malloc(new_size)."
result = (p != NULL);
mi_free(p);
};
// ---------------------------------------------------
// Heaps
// ---------------------------------------------------

View file

@ -15,11 +15,29 @@ terms of the MIT license. A copy of the license can be found in the file
static int ok = 0;
static int failed = 0;
static bool check_result(bool result, const char* testname, const char* fname, long lineno) {
if (!(result)) {
failed++;
fprintf(stderr,"\n FAILED: %s: %s:%d:\n", testname, fname, lineno);
/* exit(1); */
}
else {
ok++;
fprintf(stderr, "ok.\n");
}
return true;
}
#define CHECK_BODYX(name) \
fprintf(stderr,"test: %s... ", name ); \
for(bool done = false, result = true; !done; done = check_result(result,name,__FILE__,__LINE__))
#define CHECK_BODY(name,body) \
do { \
fprintf(stderr,"test: %s... ", name ); \
bool result = true; \
do { body } while(false); \
do { body } while(false); \
if (!(result)) { \
failed++; \
fprintf(stderr, \
@ -35,7 +53,7 @@ static int failed = 0;
} \
} while (false)
#define CHECK(name,expr) CHECK_BODY(name,{ result = (expr); })
#define CHECK(name,expr) CHECK_BODYX(name){ result = (expr); }
// Print summary of test. Return value can be directly use as a return value for main().
static inline int print_test_summary(void)