Binary installer for CUnit-2.1 on MinGW

From ASCEND

Jump to: navigation, search

Here is an installer for CUnit version 2.1-0 on MinGW using NSIS:

This binary has been tested on Windows XP with MinGW GCC 3.4.5 and SCons 0.97.D001 with Python 2.5.1.

Here is an example of a trivial test program that it can build:

#include <CUnit/CUnit.h>
#include <CUnit/Basic.h>

#include <stdlib.h>
#include <stdio.h>

char *str = NULL;
#define MAXLEN 40

int suite1_setup(void){
	//fprintf(stderr,"ALLOCATING MEMORY\n");
	str = malloc(sizeof(char)*MAXLEN);
	return (str==0);
}

void test_suite1_allocated(void){
	fprintf(stderr,"STARTING TEST...\n");
	CU_ASSERT(str!=NULL);
	sprintf(str,"%s","hello");
	CU_ASSERT(strcmp(str,"hello")==0);
}

int suite1_teardown(void){
	//fprintf(stderr,"\n\nCLEANING UP\n");
	free(str);
	return 0;
}

int main(void){

	if(CU_initialize_registry() != CUE_SUCCESS){
		fprintf(stderr,"ERROR: Unable to initialize registry\n");
		exit(1);
	}

	CU_pSuite s;
	s = CU_add_suite("suite1", &suite1_setup, &suite1_teardown);
	if(!s){
		fprintf(stderr,"ERROR: Unable to add test suite (%s)\n", CU_get_error_msg());
		exit(1);
	}

	if(!CU_ADD_TEST(s,test_suite1_allocated)){
		fprintf(stderr,"ERROR: Unable to register test (%s)\n", CU_get_error_msg());
		exit(2);
	}

	CU_BasicRunMode mode = CU_BRM_VERBOSE;
	CU_ErrorAction error_action = CUEA_ABORT;

	CU_basic_set_mode(mode);
	CU_set_error_action(error_action);

	CU_basic_run_tests();

	CU_cleanup_registry();
}

Here is a SCons script that builds the above program. This has been tested and works on both Linux and Windows:

env = Environment(tools=['mingw'])

import SCons.Util
import os.path

default_cunit_include = None
default_cunit_lib = None

# this function looks up the installed location of CUnit in the
# windows registry, or else uses the default values specified above
# (if you're on Linux, for example).
def find_cunit(env):
	"""
	Try and figure out if NSIS is installed on this machine, and if so,
	where.
	"""
	if SCons.Util.can_read_reg:
		# If we can read the registry, get the NSIS command from it
		try:
			k = SCons.Util.RegOpenKeyEx(SCons.Util.hkey_mod.HKEY_LOCAL_MACHINE,
									  'SOFTWARE\\cunit')
			inc, tok = SCons.Util.RegQueryValueEx(k,"INSTALL_INCLUDE")
			lib, tok = SCons.Util.RegQueryValueEx(k,"INSTALL_LIB")
			if os.path.exists(inc) and os.path.exists(lib):
				env['CUNIT_INCLUDE'] = inc
				env['CUNIT_LIB'] = lib
				return True
		except:
		  pass
	env['CUNIT_INCLUDE'] = default_cunit_include
	env['CUNIT_LIB'] = default_cunit_lib
	return False


find_cunit(env)

print env['CUNIT_INCLUDE']

env.AppendUnique(
	CFLAGS = ['-g']
	,LINKFLAGS = ['-g']
	,CPPPATH = env['CUNIT_INCLUDE']
	,LIBPATH = env['CUNIT_LIB']
	,LIBS = ['cunit']
)

env.Program('mytest',['mytest.c'])

And here is the output

CUnit - A Unit testing framework for C - Version 2.1-0
     http://cunit.sourceforge.net/


Suite: suite1
  Test: test_suite1_allocated ... STARTING TEST...
passed

--Run Summary: Type      Total     Ran  Passed  Failed
               suites        1       1     n/a       0
               tests         1       1       1       0
               asserts       2       2       2       0