// assert() example by gbmhunter for www.mbedded.ninja
// Designed as an example of assert() for embedded systems.
// See http://w...content-available-to-author-only...d.ninja/programming/languages/c/assertions-assert

#include <stdio.h>
#include <stdbool.h>

// Comment out to disable asserts
#define DEBUG

// We will use captial letters for ASSERT to differentiate it from the system-provided
// assert() if it exists.
#ifdef DEBUG
	#define ASSERT(exp)  ((void)(exp), (exp ? : AssertFailed(__FILE__, __LINE__, #exp)))
#else
	#define ASSERT(exp) (void)(0);
#endif

//! @brief		Assert failed handling function.
//! @details	This will be called by the ASSERT() macro.
void AssertFailed(const char * filename, int lineNumber, const char * expression) {
	printf("%s, line %i: Assertion \"%s\" failed.\r\n", filename, lineNumber, expression);	
}

int main(void) {
	
	// Some really basic (and useless) assert() tests
	ASSERT(true);
	ASSERT(false);
	
	// Some more practical assert tests
	int x = 3;
	
    ((void)(x = 7), x==7 ? x = 4: x = 2);
	ASSERT(x == 3);
	ASSERT(x == 4);
	
	// An assert() with an embedded assignment should
	// give a compiler error.
	//ASSERT(x = 1);
	
	return 0;
}