2017 July, 10 Monday 12:13


Testing is a sensitive sector of software development; this is true when coding in every programming language. While I was coding a new algorithm as per instruction of my Phd supervisor, I came to need a little testing framework in order to ensure no bugs were present inside the algorithm. 

With that in mind I started looking for a simple and intuitive testing utility. Sadly, everything I found hasn't all the feature I needed: Since I had an experience with cutest, I ended up using that as my testing framework. However, the syntax is just too cucumbersome: Aside from lots of boilrplate code, this testing framework suffered from a quite annoying memory leak (at least in the latest version I have used). 

During my C++ development, I discovered Catch; this incredible testing framework provides little to none boilerplate code; furthermore with WHEN, THEN clauses you can compress testing code to improve testing maintenability.

Since Catch does not support C language (at least to my knowledge) I decided to design and develop a simple C unit framework. The testing framework is inspired by:

  1. catch;
  2. JUnit;
  3. simplicity and little to no boilerplate code;

 

After some coding, we can showcase the core of CrashC features: given this code:

int main(int argc, const char* argv[]) {

	char buffer[1000];

	TESTCASE("testcase", "") {
		isprintf(buffer, "a");
		WHEN("when1", "") {
			isprintf(buffer, "b");
		}
		isprintf(buffer, "c");
		WHEN("when2", "") {
			isprintf(buffer, "d");
		}
		isprintf(buffer, "e");
		WHEN("when3", "") {
			isprintf(buffer, "f");
		}
		isprintf(buffer, "g");
	}
	assert( strcmp(buffer, "abcegacdegacefg") == 0);
	resetBuffer();
}

CrashC autoamtically expands it to 3 similar but different test cases:

int main(int argc, const char* argv[]) {

	char buffer[1000];

	TEST1() {
		isprintf(buffer, "a");
		isprintf(buffer, "b");
		isprintf(buffer, "c");
		isprintf(buffer, "e");
		isprintf(buffer, "g");
	}

	TEST2() {
		isprintf(buffer, "a");
		isprintf(buffer, "c");
		isprintf(buffer, "d");
		isprintf(buffer, "e");
		isprintf(buffer, "g");
	}

	TETS3() {
		isprintf(buffer, "a");
		isprintf(buffer, "c");
		isprintf(buffer, "e");
		isprintf(buffer, "f");
		isprintf(buffer, "g");
	}

	assert( strcmp(buffer, "abcegacdegacefg") == 0);
	resetBuffer();
}

Although this exmaple is simply, you can already see how CrashC (inspired by Catch) can dramatically removes redundant testing code. 

As I'm writing this, Lorenzo Nodari is developing the other incredible features of CrashC. CrashC is still in the heavy developments phase, but I think the code of the library will be finished by the end of this summer! If you're interested, watch CrashC github repo