Skip to content


Subversion checkout URL

You can clone with
Download ZIP
Mock a function in c at run time
C C++
Branch: master

cleaned up compiler warnings

latest commit a101d58fd2 authored


to install execute `make; sudo make install`

to build execute `sudo make -f Makefile32 install ` for ia32 build or `sudo make -f Makefile64 install` for ia64 build

to build the system default, you can just execute 'sudo make install'

on ubuntu you may need to install for ia32 build on 64 bit platform:

 sudo apt-get install ia32-libs
 sudo apt-get install libc6-dev-i386

for examples of how to use to mock a function see test.c 
to use this add to your compile commandline `pkg-config ExtremeCUnit --cflags` and to the link command line `pkg-config ExtremeCUnit --libs`

to build and run the test app, you need to install my ExtremeCUnit testing framework, then in this directory, execute 'make run', if you see no errors all tests passed. To version status of each test execute dist/test -v

Say you have some library that function you want to test and it call the libc function 'time_t time(time_t * t)'. That is going to return current time which is not necessarily going to be consistent in your test. So here is is an example of how to mock the time function. It works even when some library you are linking with calls the time function

source for mock_time.c :
#include <time.h>
#include <stdio.h>
#include <ExtremeCMock.h>
long time_mock()
	static int now = 10;
	return now++;

int main (int argc, char *argv) {
	printf("time is now %lu\n", time(NULL));
	printf("time is now %lu\n", time(NULL));
	printf("time is now %lu\n", time(NULL));
	printf("time is now %lu\n", time(NULL));
	return 0;

compile and run : 
username@hostname:~/my-src/ExtremeCMock$ gcc -o mock_time mock_time.c `pkg-config --cflags --libs ExtremeCMock` 
username@hostname:~/my-src/ExtremeCMock$ ./mock_time 
time is now 1343102848
time is now 10
time is now 11
time is now 1343102848

Something went wrong with that request. Please try again.