Getting Started with CUT

Sergei Gnezdov

November 19, 2004

1 Introduction

Cut is a unit testing framework for C, C++ and Objective-C. This document covers version 2.3 of CUT. Our goal is to help you to get started with CUT. To download CUT, go to http://www.falvotech.com/projects/cut.php .

2 Getting Ready

Cut comes in source code form. You will have to compile it to start using. You should have no problem to compile it on BSD (FreeBSD) or Linux machines. To compile run

  make

command without arguments. This should create cutgen application.

3 Test Driven Development in C with CUT

3.1 Overview

To show the usage of CUT we will create a very simple C project. You will have to create a directory for your project. It is referenced as Project directory further on. The directory will contain the following files:

• Makefile

• Main.c

• Compute.h • TestCompute.c

• Compute.c

• cut.h

3.2 Creating Makefile

Create Makefile with the following content:

CC = gcc
LD = gcc
MODULES = Compute
OBJS = $ (MODULES:%=%.o )
TESTS = $ (MODULES:%=Test%.c )
TESTS OBJS = $ (MODULES:%=Test%.o )
LIBS =
CCOPTS = −c
DEBUG =
# d e f a u l t t a r g e t
help :
echo ” Type ’ make a p p l i c a t i o n ’ to build the a p p l i c a t i o n . ”
echo ” Type ’ make check ’ to biuld the a p p l i c a t i o n . ”
# top l e v e l t a r g e t to c r e a t e production a p p l i c a t i o n
a p p l i c a t i o n : $ (OBJS) Main . o
$ (LD) $ (LDOPTS) $ (MODULES:%=%.o ) Main . o $ ( LIBS) −o app
# top l e v e l t a r g e t to run t e s t c a s e s
t e s t : cutcheck
. / cutcheck
# top l e v e l t a r g e t to clean up the p r o j e c t d i r e c t o r y
clean :
−rm ∗. o app cutcheck
cutcheck . c : $ (TESTS)
cutgen −o cutcheck . c $ (TESTS)
cutcheck : $ (OBJS) $ (TESTS OBJS ) cutcheck . o
$ (LD) $ (LDOPTS) $ (OBJS) $ (TESTS OBJS ) $ ( LIBS)\

cutcheck . o −o cutcheck
. c . o :
$ (CC) $ (CCOPTS) $ (DEBUG) −o $@ $<

3.3 Defining Sum Function

Compute.h header defines a function we want to test:

#i f n d e f COMPUTE H INCLUDED
#d e f i n e COMPUTE H INCLUDED
/∗ Summarizes two numbers ∗/
i n t sum( i n t , i n t ) ;
#e n d i f

3.4 Creating Test Case

For consistency, the test file name is based on the name of the header file beeing tested. Append Test prefix and replace h with c extension. TestCompute.c file has the following content:

#i n c l u d e < s t d i o . h>
#i n c l u d e ” cut . h”
#i n c l u d e ” Compute . h”
void CUT Sum ( void )

{

ASSERT(3 == sum ( 1 , 2 ) , ” Check sum ” ) ;

}

TestCompute.c does not compile yet. See it for yourself with the following command:

  make TestCompute.o

The compileation fails because the compiler can’t find cut.h file. Copy cut.h file from the cut-2.3 project directory into our project directory. Run

  make TestCompute.o

again. This should create TestCompute.o file.

4 Implementing sum Function

Create Compute.c file:

i n t sum( i n t a , i n t b)

{

return 0 ;

} Run

  make test

now. It should fail with the message similar to the following:

  TestCompute.c(7): Check sum
  TestCompute.c(7): Failed expression: 3 == sum(1,2)

This is because we did not implement sum function yet. Change Compute.c to contain the following code:

i n t sum( i n t a , i n t b)

{

return a+b ;

} and run make test again. The test should pass now.

4.1 Creating main application

Since our function operates as expected we can create a main application now:

#i n c l u d e < s t d i o . h>
#i n c l u d e ” Compute . h”
i n t main ( void )

{

p r i n t f (”% i \n ” , sum ( 3 , 2 ) ) ;
return 0 ;

} Run make application to see the result.