Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Mock a function in c at run time

branch: master

Fetching latest commit…

Octocat-spinner-32-eaf2f5

Cannot retrieve the latest commit at this time

Octocat-spinner-32 .gitignore
Octocat-spinner-32 ExtremeCMock.h
Octocat-spinner-32 ExtremeCMock.pc
Octocat-spinner-32 Makefile
Octocat-spinner-32 Makefile-common
Octocat-spinner-32 Makefile32
Octocat-spinner-32 Makefile64
Octocat-spinner-32 README
Octocat-spinner-32 linkedlist.h
Octocat-spinner-32 mock.c
Octocat-spinner-32 test.c
Octocat-spinner-32 testpp.cpp
README
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 https://github.com/acgreek/ExtremeCUnit, 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


EXAMPLE 
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));
	mock_func(time,time_mock);
	printf("time is now %lu\n", time(NULL));
	printf("time is now %lu\n", time(NULL));
	unmock_all();
	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.