<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE refentry 
  PUBLIC "-//OASIS//DTD DocBook XML V4.1.2//EN"
  "http://www.oasis-open.org/docbook/xml/4.1.2/docbookx.dtd">
<refentry id="tutorial">
<refmeta>
  <refentrytitle role='top_of_page' id='tutorial.top_of_page'>Tutorial</refentrytitle>
  <refmiscinfo>CUTTER Library</refmiscinfo>
</refmeta>
<refnamediv>
  <refname>Tutorial</refname>
  <refpurpose>How to use Cutter</refpurpose>
</refnamediv>
<refsect1>
  <title>Introduction</title>
  <para>We write a program (library) that implements a stack in C. We write a program with writing tests. To write tests, we use Cutter that is a unit testing framework for C.</para>
  <para>We use GNU build system (GNU Autoconf/GNU Automake/GNU Libtool) for build system. GNU build system lessens disparities on build environment. For this reason, we can build our program and tests on several environment easily.</para>
  <para>It&#39;s better that a program works on several environment without many costs. If tests of the program works on the environment too, we can verify the program works well on the environment easily. It&#39;s important that both a program and tests are works well on several environment easily.</para>
  <para>Cutter requires only GLib. GLib is a very portable library that works on not only UNIX-like system but also Windows and Mac OS X. Cutter provides many useful test support features with portability due to GLib. Cutter is a testing framework and respects to xUnit style.</para>
  <para>We will learn how to use Cutter with writing a stack implementation. We assume that Cutter is already installed into your system.</para>
  <para>There are source codes of this program in sample/stack/.</para>
</refsect1>

<refsect1>
  <title>Directory hierarchy</title>
  <para>First, we need to setup a directory for our stack program. We use &#39;stack&#39; as the directory name.</para>
  <programlisting>% mkdir -p /tmp/stack
% cd /tmp/stack</programlisting>
  <para>Next, we make some directories: config/ that is for build auxiliary files, src/ that is for our source files and test/ that is for tests.</para>
  <programlisting>[stack]% mkdir config src test</programlisting>
  <para>After the above, we get the following directory hierarchy:</para>
  <programlisting>stack/ -+- config/ for build auxiliary files
        |
        +- src/ for source files
        |
        +- test/ for tests</programlisting>
</refsect1>

<refsect1>
  <title>Use GNU build system</title>
  <para>In GNU build system start-up, some commands are ran and they generates some files automatically. They usually are run from an authgen.sh shell script. We follow the convention.</para>
  <para>autogen.sh:</para>
  <programlisting>#!/bin/sh

run()
{
    $@
    if test $? -ne 0; then
        echo &quot;Failed $@&quot;
        exit 1
    fi
}

run aclocal ${ACLOCAL_ARGS}
run libtoolize --copy --force
run autoheader
run automake --add-missing --foreign --copy
run autoconf</programlisting>
  <para>Don&#39;t forget to make the autogen.sh executable.</para>
  <programlisting>[stack]% chmod +x autogen.sh</programlisting>
  <para>run() is a convenience function to confirm a result of ran command. The following list shows what is done by them:</para>
  <itemizedlist>
  <listitem>
  <para>aclocal: collects macros that is used by Automake into aclocal.m4.</para>
</listitem>
  <listitem>
  <para>libtoolize: prepares files that is needed by libtool.</para>
</listitem>
  <listitem>
  <para>autoheader: generates config.h.in that is used by configure script.</para>
</listitem>
  <listitem>
  <para>automake: generates Makefile.in that is used by configure script.</para>
</listitem>
  <listitem>
  <para>autoconf: generates configure scripts.</para>
</listitem>
</itemizedlist>
  <para>If we installed Cutter into different prefix with aclocal&#39;s install prefix, you need to set ACLOCAL_ARGS environment variable. The environment variable is referred from autogen.sh. If we installed Cutter with $HOME/local prefix, here is an example command to set the environment variable:</para>
  <programlisting>[stack]% export ACLOCAL_ARGS=&quot;-I $HOME/local/share/aclocal&quot;</programlisting>
  <para>The following is a result of autogen.sh at this point:</para>
  <programlisting>[stack]% ./autogen.sh
aclocal: `configure.ac&#39; or `configure.in&#39; is required
Failed aclocal</programlisting>
  <para>We need to prepare configure.ac that is for Autoconf.</para>
  <refsect2>
  <title>configure.ac</title>
  <para>The following is a minimum configure.ac for our autogen.sh.</para>
  <para>configure.ac:</para>
  <programlisting>AC_PREREQ(2.59)

AC_INIT(stack, 0.0.1, you@example.com)
AC_CONFIG_AUX_DIR([config])
AC_CONFIG_HEADER([src/config.h])

AM_INIT_AUTOMAKE($PACKAGE_NAME, $PACKAGE_VERSION)

AC_PROG_LIBTOOL

AC_CONFIG_FILES([Makefile])

AC_OUTPUT</programlisting>
  <para>The following is a result of autogen after preparing configure.ac.</para>
  <programlisting>[stack]% ./autogen.sh
Putting files in AC_CONFIG_AUX_DIR, `config&#39;.
configure.ac:7: installing `config/install-sh&#39;
configure.ac:7: installing `config/missing&#39;
automake: no `Makefile.am&#39; found for any configure output
Failed automake --add-missing --foreign --copy</programlisting>
  <para>We need to prepare Makefile.am for Automake.</para>
</refsect2>

  <refsect2>
  <title>Makefile.am</title>
  <para>An empty Makefile.am is enough if the Makefile.am is just only for autogen.sh.</para>
  <programlisting>[stack]% touch Makefile.am
[stack]% ./autogen.sh
Putting files in AC_CONFIG_AUX_DIR, `config&#39;.</programlisting>
  <para>A configure script can be generated. We can do &#39;configure; make; make install&#39; like many popular softwares at this point:</para>
  <programlisting>[stack]% ./configure
...
[stack]% make
[stack]% make install</programlisting>
  <para>But for now, nothing is to happen because we doesn&#39;t have any items that are needed to build or install.</para>
</refsect2>

</refsect1>

<refsect1>
  <title>First test writing</title>
  <para>We can write a test because we got a minimal build environment. First, we test that a newly created statck should be empty. The following code representes this test in C:</para>
  <programlisting>void
test_new_stack (void)
{
    Stack *stack;
    stack = stack_new();
    if (stack_is_empty(stack))
        PASS;
    else
        FAIL;
}</programlisting>
  <para>We change this test code to be able to run as a test code for Cutter.</para>
  <refsect2>
  <title>Write a test program</title>
  <para>A test program is put into test/. In this tutorial, we make a test program as test/test-stack.c.</para>
  <para>First, we need to include cutter.h to use Cutter.</para>
  <para>test/test-stack.c:</para>
  <programlisting>#include &lt;cutter.h&gt;</programlisting>
  <para>And we need to include stack.h that declares API for test target stack implementation. (stack.h will be made later.)</para>
  <para>test/test-stack.c:</para>
  <programlisting>#include &lt;stack.h&gt;</programlisting>
  <para>Next, we write a test with the stack API:</para>
  <para>test/test-stack.c:</para>
  <programlisting>void test_new_stack (void);

void
test_new_stack (void)
{
    Stack *stack;
    stack = stack_new();
    cut_assert(stack_is_empty(stack));
}</programlisting>
  <para>cut_assert() is a macro that fails if the first argument is 0, passes otherwise. Writing tests with Cutter means that writing a program that verifies a target program works as we expected at the specific situation.</para>
  <para>The following test code is a whole test code to test &quot;a newly created stack should be empty&quot;.</para>
  <para>test/test-stack.c:</para>
  <programlisting>#include &lt;cutter.h&gt;
#include &lt;stack.h&gt;

void test_new_stack (void);

void
test_new_stack (void)
{
    Stack *stack;
    stack = stack_new();
    cut_assert(stack_is_empty(stack));
}</programlisting>
</refsect2>

  <refsect2>
  <title>Build a test</title>
  <para>Each test programs for Cutter are shared libraries. To build the above test program as shared library, we change Makefile.am.</para>
  <refsect3>
  <title>Build configuration in test/</title>
  <para>Makefile.am is empty for now.</para>
  <para>First, put the following configuration to use ACLOCAL_ARGS environment variable for autogen.sh with aclocal invoked via make:</para>
  <para>Makefile.am:</para>
  <programlisting>ACLOCAL_AMFLAGS = $$ACLOCAL_ARGS</programlisting>
  <para>Next, to build test/test-stack.c in test/ directory, we need to specify that there is test/ directory as sub directory in Makefile.am.</para>
  <para>Makefile.am:</para>
  <programlisting>...
SUBDIRS = test</programlisting>
  <para>make will detect Makefile.am is changed and update Makefile and so on automatically after we change Makefile.am and run make.</para>
  <programlisting>[stack]% make
 cd . &amp;&amp; /bin/sh /tmp/stack/config/missing --run automake-1.10 --foreign  Makefile
 cd . &amp;&amp; /bin/sh ./config.status Makefile 
config.status: creating Makefile
Making all in test
config.status: creating Makefile
Making all in test
make[1]: Entering directory `/tmp/stack/test&#39;
make[1]: *** No rule to make target `all&#39;.  Stop.
make[1]: Leaving directory `/tmp/stack/test&#39;
make: *** [all-recursive] Error 1</programlisting>
  <para>We find that make go down to test/ to build. But make is failed in test/ because test/Makefile doesn&#39;t exist.</para>
  <para>To build in test/, we will make test/Makefile.am and indicate configure.ac to generate test/Makefile.</para>
  <para>An empty test/Makefile.am is OK for just protecting make failure in test/.</para>
  <programlisting>[stack]% touch test/Makefile.am</programlisting>
  <para>Next, we indicate configure.ac to generate test/Makefile. Now, make will be done successfully.</para>
  <para>configure.ac:</para>
  <programlisting>...
AC_CONFIG_FILES([Makefile
                 test/Makefile])
...</programlisting>
  <para>If we run make again, make re-runs configure and test/Makefile is generated. Now make doesn&#39;t fail in test/.</para>
  <programlisting>[stack]% make
...
config.status: creating test/Makefile
config.status: creating src/config.h
config.status: src/config.h is unchanged
config.status: executing depfiles commands
Making all in test
make[1]: Entering directory `/tmp/stack/test&#39;
make[1]: Nothing to be done for `all&#39;.
make[1]: Leaving directory `/tmp/stack/test&#39;
make[1]: Entering directory `/tmp/stack&#39;
make[1]: Nothing to be done for `all-am&#39;.
make[1]: Leaving directory `/tmp/stack&#39;</programlisting>
</refsect3>

  <refsect3>
  <title>Build test/test_stack.so</title>
  <para>We will edit test/Makefile.am to build test/test-stack.c as a shared library. A shared library for test should be named as &quot;test_&quot; prefix. (It&#39;s OK if &quot;lib&quot; is prepended to &quot;test_&quot; prefix.) We use &quot;noinst_&quot; because a test program isn&#39;t needed to be installed.</para>
  <para>test/Makefile.am:</para>
  <programlisting>noinst_LTLIBRARIES = test_stack.la</programlisting>
  <para>Shared libraries for test are loaded dynamically by cutter that is a command included in Cutter to run test. Shared libraries that are loaded dynamically should be builded libtool with -module option. -rpath option is also required by -module option. Because of them LDFLAGS becomes the following. The reason why -avoid-version is specified is that shared libraries for test aren&#39;t needed to have version number. -no-undefined option tells libtool that it reports a error when there is any undefined symbol. On some environments, shared library isn&#39;t generated without -no-undefined option. (e.g. a case that generating DLL on Windows.)</para>
  <para>test/Makefile.am:</para>
  <programlisting>...
LDFLAGS = -module -rpath $(libdir) -avoid-version -no-undefined</programlisting>
  <para>To build test/test_stack.la, test/test-stack.c is used. (test_stack.so is generated into test/.libs/.) We need to specify this.</para>
  <para>test/Makefile.am:</para>
  <programlisting>...
test_stack_la_SOURCES = test-stack.c</programlisting>
  <para>Now, we can build test/test_stack.la.</para>
  <programlisting>[stack]% make
...
 cd .. &amp;&amp; /bin/sh /tmp/stack/config/missing --run automake-1.10 --foreign  test/Makefile
test/Makefile.am: required file `config/depcomp&#39; not found
test/Makefile.am:   `automake --add-missing&#39; can install `depcomp&#39;
make[1]: *** [Makefile.in] Error 1
make[1]: Leaving directory `/tmp/stack/test&#39;
make: *** [all-recursive] Error 1</programlisting>
  <para>To generate config/depcomp, we need to run automake with --add-missing option. To do this, we can use autogen.sh. Don&#39;t forget to re-run configure.</para>
  <programlisting>[stack]% ./autogen.sh
[stack]% ./configure</programlisting>
  <para>Now, we can build test/test_stack.la with make.</para>
  <programlisting>[stack]% make
...
test-stack.c:1:20: error: cutter.h: No such file or directory
test-stack.c:2:19: error: stack.h: No such file or directory
test-stack.c: In function &#39;test_new_stack&#39;:
test-stack.c:9: error: &#39;Stack&#39; undeclared (first use in this function)
test-stack.c:9: error: (Each undeclared identifier is reported only once
test-stack.c:9: error: for each function it appears in.)
test-stack.c:9: error: &#39;stack&#39; undeclared (first use in this function)
make[1]: *** [test-stack.lo] Error 1
make[1]: Leaving directory `/tmp/stack/test&#39;
make: *** [all-recursive] Error 1</programlisting>
  <para>But there are the above errors because we don&#39;t setup to use Cutter yet. And we can&#39;t include stack.h because we don&#39;t have a stack implementation yet.</para>
</refsect3>

  <refsect3>
  <title>Use Cutter</title>
  <para>We will support cutter.h including. Cutter provides a macro file for aclocal. Because of this, we can use Cutter with GNU build system.</para>
  <para>First, we add a code to detect Cutter into configure.ac.</para>
  <para>configure.ac:</para>
  <programlisting>...
AC_CHECK_CUTTER

AC_CONFIG_FILES([Makefile
                 test/Makefile])
...</programlisting>
  <para>We use detected Cutter information in test/Makefile.am:</para>
  <para>test/Makefile.am:</para>
  <programlisting>...
INCLUDES = $(CUTTER_CFLAGS)
LIBS = $(CUTTER_LIBS)
...</programlisting>
  <para>The followings are the current whole configure.ac, Makefile.am and test/Makefile.am:</para>
  <para>configure.ac:</para>
  <programlisting>AC_PREREQ(2.59)

AC_INIT(stack, 0.0.1, you@example.com)
AC_CONFIG_AUX_DIR([config])
AC_CONFIG_HEADER([src/config.h])

AM_INIT_AUTOMAKE($PACKAGE_NAME, $PACKAGE_VERSION)

AC_PROG_LIBTOOL

AC_CHECK_CUTTER

AC_CONFIG_FILES([Makefile
                 test/Makefile])

AC_OUTPUT</programlisting>
  <para>Makefile.am:</para>
  <programlisting>ACLOCAL_AMFLAGS = $$ACLOCAL_ARGS

SUBDIRS = test</programlisting>
  <para>test/Makefile.am:</para>
  <programlisting>noinst_LTLIBRARIES = test_stack.la

INCLUDES = $(CUTTER_CFLAGS)
LIBS = $(CUTTER_LIBS)

LDFLAGS = -module -rpath $(libdir) -avoid-version -no-undefined

test_stack_la_SOURCES = test-stack.c</programlisting>
  <para>AC_CHECK_CUTTER macro uses pkg-config which is a popular package information management tool. If we installed Cutter with different prefix of pkg-config, we need to set PKG_CONFIG_PATH environment variable. The environment variable is referred by pkg-config to find .pc file. If we installed Cutter with $HOME/local prefix, here is an example command to set the environment variable:</para>
  <programlisting>[stack]% export PKG_CONFIG_PATH=$HOME/local/lib/pkgconfig</programlisting>
  <para>We run make again and make runs configure automatically and builds with Cutter configuration after the above changes.</para>
  <programlisting>[stack]% make
...
test-stack.c:2:19: error: stack.h: No such file or directory
test-stack.c: In function &#39;test_new_stack&#39;:
test-stack.c:9: error: &#39;Stack&#39; undeclared (first use in this function)
test-stack.c:9: error: (Each undeclared identifier is reported only once
test-stack.c:9: error: for each function it appears in.)
test-stack.c:9: error: &#39;stack&#39; undeclared (first use in this function)
make[1]: *** [test-stack.lo] Error 1
make[1]: Leaving directory `/tmp/stack/test&#39;
make: *** [all-recursive] Error 1</programlisting>
  <para>An error that reports &quot;cutter.h can&#39;t be included&quot; is gone away.</para>
</refsect3>

  <refsect3>
  <title>Make stack API</title>
  <para>We will fix an error that stack.h can&#39;t be included.</para>
  <para>We put stack.h into src/stack.h because we make a stack implementation in src/.</para>
  <programlisting>[stack]% touch src/stack.h</programlisting>
  <para>To include stack.h from test program, we configure include path:</para>
  <para>test/Makefile.am:</para>
  <programlisting>...
INCLUDES = $(CUTTER_CFLAGS) -I$(top_srcdir)/src
...</programlisting>
  <para>We will find that an error that stack.h can&#39;t be included is gone away if we run make again.</para>
  <programlisting>[stack]% make
...
test-stack.c: In function &#39;test_new_stack&#39;:
test-stack.c:9: error: &#39;Stack&#39; undeclared (first use in this function)
test-stack.c:9: error: (Each undeclared identifier is reported only once
test-stack.c:9: error: for each function it appears in.)
test-stack.c:9: error: &#39;stack&#39; undeclared (first use in this function)
make[1]: *** [test-stack.lo] Error 1
make[1]: Leaving directory `/tmp/stack/test&#39;
make: *** [all-recursive] Error 1</programlisting>
  <para>There is only an error that Stack type isn&#39;t declared.</para>
</refsect3>

  <refsect3>
  <title>Declare Stack type</title>
  <para>To build our test program, we declare Stack type in src/stack.h.</para>
  <para>src/stack.h:</para>
  <programlisting>#ifndef __STACK_H__
#define __STACK_H__

typedef struct _Stack Stack;

#endif</programlisting>
  <para>We get a warning because stack_new() isn&#39;t declared but we can build a shared library.</para>
  <programlisting>[stack]% make
...
test-stack.c: In function &#39;test_new_stack&#39;:
test-stack.c:10: warning: assignment makes pointer from integer without a cast
...
[stack]% file test/.libs/test_stack.so
test/.libs/test_stack.so: ELF 64-bit LSB shared object, x86-64, version 1 (SYSV), dynamically linked, not stripped</programlisting>
  <para>NOTE: We can&#39;t generate a shared library (DLL) on Cygwin when we have unresolved symbols. We can go to the next step on Cygwin without caring the command result.</para>
</refsect3>

  <refsect3>
  <title>Declare stack_new()/stack_is_empty()</title>
  <para>To suppress a warning, we declare stack_new() and stack_is_empty().</para>
  <para>src/stack.h:</para>
  <programlisting>...
Stack *stack_new      (void);
int    stack_is_empty (Stack *stack);
...</programlisting>
  <para>We can confirm that make don&#39;t report any warnings now.</para>
  <programlisting>[stack]% make</programlisting>
</refsect3>

</refsect2>

  <refsect2>
  <title>Run test</title>
  <para>Now, we can run a test because we got a shared library.</para>
  <programlisting>[stack]% cutter test/
cutter: symbol lookup error: test/.libs/test_stack.so: undefined symbol: stack_new</programlisting>
  <para>Loading our test is failed due to undefined stack_new() but we can confirm that our test is loaded.</para>
  <para>NOTE: We get a &quot;0 tests are ran and no failure&quot; result report on Cygwin because we can&#39;t generate a DLL on Cygwin when we have unresolved symbols. We will implement stack and resolve all symbols. We can generate a DLL and run test after implementing stack. We can go to the next step on Cygwin without caring the command result.</para>
  <refsect3>
  <title>Automate running test</title>
  <para>GNU build system use &#39;make check&#39; to run test. We follow the convention in our stack implementation.</para>
  <para>First, we make a script test/run-test.sh that runs our test. A path of cutter command is passed from environment variable CUTTER.</para>
  <para>test/run-test.sh:</para>
  <programlisting>#!/bin/sh

export BASE_DIR=&quot;`dirname $0`&quot;
$CUTTER -s $BASE_DIR &quot;$@&quot; $BASE_DIR</programlisting>
  <para>Don&#39;t forget to make the test/run-test.sh executable.</para>
  <programlisting>[stack]% chmod +x test/run-test.sh</programlisting>
  <para>We need to specify that we use test/run-test.sh as a test runner script to test/Makefile.am.</para>
  <para>test/Makefile.am:</para>
  <programlisting>TESTS = run-test.sh
TESTS_ENVIRONMENT = CUTTER=&quot;$(CUTTER)&quot;
...</programlisting>
  <para>We pass a path of cutter command via environment variable CUTTER in TESTS_ENVIRONMENT. A path of cutter command is detected by AC_CHECK_CUTTER in configure.ac.</para>
  <para>We can confirm that &#39;make -s check&#39; runs our test. -s option is for silence mode. A test result can be confirmed more easier.</para>
  <programlisting>[stack]% make -s check
Making check in test
cutter: symbol lookup error: ./.libs/test_stack.so: undefined symbol: stack_new
FAIL: run-test.sh
================================
1 of 1 tests failed
Please report to you@example.com
================================
...</programlisting>
  <para>NOTE: As mentioned the above, we doesn&#39;t get an error on Cygwin because we can&#39;t generate a DLL for now. We doesn&#39;t need to care it. We can go to the next.</para>
</refsect3>

  <refsect3>
  <title>Make test/run-test.sh workable alone</title>
  <para>In &#39;make -s check&#39;, there are outputs that isn&#39;t test result like build logs. They hid test result that is interested by us. So we want test/run-test.sh to work without invoking from &#39;make -s check&#39;.</para>
  <para>test/run-test.sh needs to detect a path of cutter command automatically if environment variable CUTTER isn&#39;t set. And test/run-test.sh needs to run make to rebuild necessary files if test/run-test.sh isn&#39;t invoked from &#39;make check&#39;.</para>
  <para>test/run-test.sh:</para>
  <programlisting>#!/bin/sh

export BASE_DIR=&quot;`dirname $0`&quot;
top_dir=&quot;$BASE_DIR/..&quot;

if test -z &quot;$NO_MAKE&quot;; then
    make -C $top_dir &gt; /dev/null || exit 1
fi

if test -z &quot;$CUTTER&quot;; then
    CUTTER=&quot;`make -s -C $BASE_DIR echo-cutter`&quot;
fi

$CUTTER -s $BASE_DIR &quot;$@&quot; $BASE_DIR</programlisting>
  <para>To support the test/run-test.sh, test/Makefile.am has some works.</para>
  <para>test/Makefile.am:</para>
  <programlisting>...
TESTS_ENVIRONMENT = NO_MAKE=yes CUTTER=&quot;$(CUTTER)&quot;
...
echo-cutter:
	@echo $(CUTTER)</programlisting>
  <para>The following is the whole of test/Makefile.am.</para>
  <para>test/Makefile.am:</para>
  <programlisting>TESTS = run-test.sh
TESTS_ENVIRONMENT = NO_MAKE=yes CUTTER=&quot;$(CUTTER)&quot;

noinst_LTLIBRARIES = test_stack.la

INCLUDES = $(CUTTER_CFLAGS) -I$(top_srcdir)/src
LIBS = $(CUTTER_LIBS)

LDFLAGS = -module -rpath $(libdir) -avoid-version -no-undefined

test_stack_la_SOURCES = test-stack.c

echo-cutter:
	@echo $(CUTTER)</programlisting>
  <para>We can confirm that test/run-test.sh runs test even if it&#39;s not invoked from &#39;make -s check&#39;.</para>
  <programlisting>[stack]% test/run-test.sh
cutter: symbol lookup error: test/.libs/test_stack.so: undefined symbol: stack_new</programlisting>
  <para>NOTE: We doesn&#39;t get the error on Cygwin.</para>
  <para>We will use test/run-test.sh instead of &#39;make -s check&#39; from now. Test result that is what we are interested in will not be hid because test/run-test.sh just outputs build errors and/or warnings and test result.</para>
  <para>We spent some times to build testing environment before we implement stack. It reduces costs to run test. If costs to run test isn&#39;t low, we will not run test gradually. It may cause quality loss.</para>
  <para>Building testing environment at first delays start time of implementing a main program. But we need to keep quality of a main program by running test until a main program is developed and maintained. We will be able to collect costs that is spent for building testing environment. It&#39;s important that building testing environment at first to be developing a high-quality program comfortably.</para>
</refsect3>

</refsect2>

  <refsect2>
  <title>Implement stack</title>
  <para>We will start implementing stack because we built testing environment.</para>
</refsect2>

  <refsect3>
  <title>A straightforward stack_new() implementation</title>
  <para>We will define stack_new() and resolve run-time error.</para>
  <para>We implement stack in src/stack.c. It&#39;s a straightforward stack_new() implementation:</para>
  <para>src/stack.c:</para>
  <programlisting>#include &lt;stdlib.h&gt;
#include &quot;stack.h&quot;

Stack *
stack_new (void)
{
    return NULL;
}</programlisting>
</refsect3>

  <refsect3>
  <title>Build src/libstack.la</title>
  <para>We will build src/stack.c with make. src/ should be included into build targets like test/.</para>
  <para>Makefile.am:</para>
  <programlisting>ACLOCAL_AMFLAGS = $$ACLOCAL_ARGS

SUBDIRS = src test</programlisting>
  <para>configure.ac:</para>
  <programlisting>...
AC_CONFIG_FILES([Makefile
                 src/Makefile
                 test/Makefile])
...</programlisting>
  <para>The above configurations are for what we want to do.</para>
  <programlisting>[stack]% test/run-test.sh
configure.ac:19: required file `src/Makefile.in&#39; not found
make: *** [Makefile.in] Error 1</programlisting>
  <para>To resolve the above error, we need to make src/Makefile.am.</para>
  <programlisting>[stack]% touch src/Makefile.am
[stack]% test/run-test.sh
cutter: symbol lookup error: test/.libs/test_stack.so: undefined symbol: stack_new</programlisting>
  <para>NOTE: We doesn&#39;t get the error on Cygwin.</para>
  <para>make doesn&#39;t report error but we still have an error that stack_new() is undefined. Because we don&#39;t build src/stack.c and test program also doesn&#39;t link libstack.so yet.</para>
  <para>The following configurations in src/Makefile.am are for build libstack.so from src/stack.c.</para>
  <para>src/Makefile.am:</para>
  <programlisting>lib_LTLIBRARIES = libstack.la

LDFLAGS = -no-undefined

libstack_la_SOURCES = stack.c</programlisting>
  <para>make will generate libstack.so.</para>
  <programlisting>[stack]% make
...
make[1]: Entering directory `/tmp/stack/src&#39;
Makefile:275: .deps/stack.Plo: No such file or directory
make[1]: *** No rule to make target `.deps/stack.Plo&#39;.  Stop.
...</programlisting>
  <para>To resolve the above error, we need to re-run configure.</para>
  <programlisting>[stack]% ./configure</programlisting>
  <para>make will generate src/.libs/libstack.so.0.0.0 now.</para>
  <programlisting>[stack]% make
[stack]% file src/.libs/libstack.so.0.0.0
src/.libs/libstack.so.0.0.0: ELF 64-bit LSB shared object, x86-64, version 1 (SYSV), dynamically linked, not stripped</programlisting>
  <para>NOTE: We will generate src/.libs/cyglibstack.dll on Cygwin.</para>
</refsect3>

  <refsect3>
  <title>Link src/libstack.la</title>
  <para>libstack.so is generated but it&#39;s not linked into test program. So there is still run-time error.</para>
  <programlisting>[stack]% test/run-test.sh
cutter: symbol lookup error: test/.libs/test_stack.so: undefined symbol: stack_new</programlisting>
  <para>NOTE: We doesn&#39;t get the error on Cygwin.</para>
  <para>To link libstack.so, we will change test/Makefile.am like the following.</para>
  <para>test/Makefile.am:</para>
  <programlisting>...
LIBS = $(CUTTER_LIBS) $(top_builddir)/src/libstack.la
...</programlisting>
  <para>We need to add src/.libs/ to PATH environment variable before run cutter to find DLL generated under src/.libs/ on Cygwin:</para>
  <para>test/run-test.sh:</para>
  <programlisting>...
case `uname` in
    CYGWIN*)
        PATH=&quot;$top_dir/src/.libs:$PATH&quot;
        ;;
    Darwin)
        DYLD_LIBRARY_PATH=&quot;$top_dir/src/.libs:$DYLD_LIBRARY_PATH&quot;
        export DYLD_LIBRARY_PATH
        ;;
    *BSD)
        LD_LIBRARY_PATH=&quot;$top_dir/src/.libs:$LD_LIBRARY_PATH&quot;
        export LD_LIBRARY_PATH
        ;;
    *)
        :
        ;;
esac

$CUTTER -s $BASE_DIR &quot;$@&quot; $BASE_DIR</programlisting>
  <para>We need to run &#39;make clean&#39; to re-link our test program.</para>
  <programlisting>[stack]% make clean
[stack]% make
[stack]% test/run-test.sh
cutter: symbol lookup error: test/.libs/test_stack.so: undefined symbol: stack_is_empty</programlisting>
  <para>An error message is changed to stack_is_empty() isn&#39;t found from stack_new() isn&#39;t found. We can confirm that libstack.so is linked correctly by this change.</para>
  <para>NOTE: We doesn&#39;t get the error on Cygwin.</para>
</refsect3>

  <refsect3>
  <title>Implement stack_is_empty()</title>
  <para>We test a result of stack_is_empty() in our test program:</para>
  <para>test/test-stack.c:</para>
  <programlisting>...
cut_assert(stack_is_empty(stack));
...</programlisting>
  <para>That means that stack_is_empty() should return true. So stack_is_empty() implementation in src/stack.c should return true.</para>
  <para>src/stack.c:</para>
  <programlisting>...
#define TRUE 1
#define FALSE 0
...
int
stack_is_empty (Stack *stack)
{
    return TRUE;
}</programlisting>
  <para>The following is the whole of src/stack.c.</para>
  <para>src/stack.c:</para>
  <programlisting>#include &lt;stdlib.h&gt;
#include &quot;stack.h&quot;

#define TRUE 1
#define FALSE 0

Stack *
stack_new (void)
{
    return NULL;
}

int
stack_is_empty (Stack *stack)
{
    return TRUE;
}</programlisting>
  <para>Our test should pass because the stack_is_empty() implementation always returns true.</para>
  <programlisting>[stack]% test/run-test.sh
.

Finished in 0.000028 seconds

1 test(s), 1 assertion(s), 0 failure(s), 0 error(s), 0 pending(s), 0 omission(s), 0 notification(s)
100% passed</programlisting>
  <para>Great! This is the first success!!!</para>
  <para>Displayed a &quot;.&quot; means that a test is passed. The current number of tests is just one. So one &quot;.&quot; means all tests are passed.</para>
  <para>The above result may be displayed in green. This means that we may go to the next step because our all tests are passed.</para>
  <para>We confirmed that test is worked. We will complete stack implementation with writing tests.</para>
</refsect3>

</refsect1>

<refsect1>
  <title>Implement push</title>
  <para>We will implement push. We only accept integer for values in stack in this implementation.</para>
  <refsect2>
  <title>Test for push</title>
  <para>A stack should have 1 item and not be empty after we push a value. The following is a test for this.</para>
  <para>test/test-stack.c:</para>
  <programlisting>...
void test_push (void);
...
void
test_push (void)
{
    Stack *stack;

    stack = stack_new();
    cut_assert_equal_int(0, stack_get_size(stack));
    stack_push(stack, 100);
    cut_assert_equal_int(1, stack_get_size(stack));
    cut_assert(!stack_is_empty(stack));
}</programlisting>
  <para>We will get an error that says stack_get_size() isn&#39;t undefined if we run test.</para>
  <programlisting>[stack]% test/run-test.sh
cutter: symbol lookup error: ./test/.libs/test_stack.so: undefined symbol: stack_get_size</programlisting>
  <para>We will implement push to pass this test.</para>
  <para>NOTE: We doesn&#39;t get the error on Cygwin.</para>
</refsect2>

  <refsect2>
  <title>Implement cut_stack_push()</title>
  <para>We will implement stack_get_size() and stack_push() to be able to run test even if tests aren&#39;t passed.</para>
  <para>First, we add declarations to src/stack.h.</para>
  <para>src/stack.h:</para>
  <programlisting>...
int    stack_get_size (Stack *stack);
void   stack_push     (Stack *stack, int value);
...</programlisting>
  <para>And we add definitions to src/stack.c.</para>
  <para>src/stack.c:</para>
  <programlisting>...
int
stack_get_size (Stack *stack)
{
    return 0;
}

void
stack_push (Stack *stack, int value)
{
}</programlisting>
  <para>The reason why stack_get_size() returns 0 is the first stack_get_size() call is expected to return 0 like the following.</para>
  <para>test/test-stack.c:</para>
  <programlisting>...
stack = stack_new();
cut_assert_equal_int(0, stack_get_size(stack));
...</programlisting>
  <para>We run test because push is implemented.</para>
  <programlisting>[stack]% test/run-test.sh
.F

1) Failure: test_push
&lt;1 == stack_get_size(stack)&gt;
expected: &lt;1&gt;
 but was: &lt;0&gt;
test/test-stack.c:23: test_push()

Finished in 0.000113 seconds

2 test(s), 2 assertion(s), 1 failure(s), 0 error(s), 0 pending(s), 0 omission(s), 0 notification(s)
50% passed</programlisting>
  <para>&quot;F&quot; means that a test is Failed. The result may be showed in red. It indicates that it&#39;s dangerous to go to next stage because all of the current tests aren&#39;t passed. In other words, we should improve push implementation to pass the current tests before we implement pop.</para>
  <para>The message form cutter command shows that the test is failed because return value of stack_get_size(stack) is 0 not 1 in test_push() function at the 23th line in test/test-stack.c. The target line is the following.</para>
  <para>test/test-stack.c:23:</para>
  <programlisting>cut_assert_equal_int(1, stack_get_size(stack));</programlisting>
  <para>It&#39;s failed because our stack_get_size() implementation always return 0. We should increment an internal counter after stack_push() is called.</para>
</refsect2>

  <refsect2>
  <title>Free memory</title>
  <para>stack_new() always returns NULL for now. Stack needs to allocate memory to have an internal counter. Stack should free memory that is unused if stack allocate memory.</para>
  <para>For example, test_new_stack() should do like the following.</para>
  <programlisting>void
test_new_stack (void)
{
    Stack *stack;
    stack = stack_new();
    cut_assert(stack_is_empty(stack));
    stack_free(stack);
}</programlisting>
  <para>But stack_free() will never be called if cut_assert() where it&#39;s the above of the stack_free() fails. Because cut_assert() returns the test function immediately if the expression (stack_is_empty(stack)) is false. (It will not cause big harm because most test programs are short-lived.)</para>
  <para>Cutter supports registering functions that are surely called before/after test. They are cut_setup() and cut_teardown(). They are called even if test is failed. We can use them for freeing memory allocated in test surely.</para>
  <para>To freeing allocated memory for test_new_stack() surely, we can use cut_setup() and cut_teardown() like the following.</para>
  <para>test/test-stack.c:</para>
  <programlisting>...
static Stack *stack;

void
cut_setup (void)
{
    stack = NULL;
}

void
cut_teardown (void)
{
    if (stack)
        stack_free(stack);
}

void
test_new_stack (void)
{
    stack = stack_new();
    cut_assert(stack_is_empty(stack));
}
...</programlisting>
  <para>We can also modify test_push() to freeing allocated memory in tests by using static stack variable instead of local stack variable.</para>
  <para>test/test-stack.c:</para>
  <programlisting>...
void
test_push (void)
{
    stack = stack_new();
    cut_assert_equal_int(0, stack_get_size(stack));
    stack_push(stack, 100);
    cut_assert_equal_int(1, stack_get_size(stack));
    cut_assert(!stack_is_empty(stack));
}
...</programlisting>
  <para>Here is whole of the test/test-stack.c that uses cut_setup()/cut_teardown().</para>
  <para>test/test-stack.c:</para>
  <programlisting>#include &lt;cutter.h&gt;
#include &lt;stack.h&gt;

void test_new_stack (void);
void test_push (void);

static Stack *stack;

void
cut_setup (void)
{
    stack = NULL;
}

void
cut_teardown (void)
{
    if (stack)
        stack_free(stack);
}

void
test_new_stack (void)
{
    stack = stack_new();
    cut_assert(stack_is_empty(stack));
}

void
test_push (void)
{
    stack = stack_new();
    cut_assert_equal_int(0, stack_get_size(stack));
    stack_push(stack, 100);
    cut_assert_equal_int(1, stack_get_size(stack));
    cut_assert(!stack_is_empty(stack));
}</programlisting>
  <para>We can confirm that a result of test isn&#39;t changed after this change.</para>
  <programlisting>[stack]% test/run-test.sh
.F

1) Failure: test_push
&lt;1 == stack_get_size(stack)&gt;
expected: &lt;1&gt;
 but was: &lt;0&gt;
test/test-stack.c:35: test_push()

Finished in 0.000084 seconds

2 test(s), 2 assertion(s), 1 failure(s), 0 error(s), 0 pending(s), 0 omission(s), 0 notification(s)
50% passed</programlisting>
</refsect2>

  <refsect2>
  <title>Implement stack_new() and stack_free()</title>
  <para>We will implement stack_new() that allocate memory and stack_free() that free allocated memory.</para>
  <para>First, we will declares stack_free() in src/stack.h.</para>
  <para>src/stack.h:</para>
  <programlisting>...
void   stack_free     (Stack *stack);
...</programlisting>
  <para>Next, we will define Stack type in src/stack.c. Stack type has a field that hold stack size.</para>
  <para>src/stack.c:</para>
  <programlisting>...
struct _Stack {
    int size;
};
...</programlisting>
  <para>stack_new() allocates memory for Stack and stack_free() frees memory allocated by stack_new().</para>
  <para>src/stack.c:</para>
  <programlisting>...
Stack *
stack_new (void)
{
    Stack *stack;

    stack = malloc(sizeof(Stack));
    if (!stack)
        return NULL;

    stack-&gt;size = 0;
    return stack;
}

void
stack_free (Stack *stack)
{
    free(stack);
}
...</programlisting>
  <para>We can confirm that test works same as before the changes.</para>
  <programlisting>[stack]% test/run-test.sh
.F

1) Failure: test_push
&lt;1 == stack_get_size(stack)&gt;
expected: &lt;1&gt;
 but was: &lt;0&gt;
test/test-stack.c:35: test_push()

Finished in 0.000113 seconds

2 test(s), 2 assertion(s), 1 failure(s), 0 error(s), 0 pending(s), 0 omission(s), 0 notification(s)
50% passed</programlisting>
</refsect2>

  <refsect2>
  <title>Really implement stack_push()</title>
  <para>We will really implement stack_push() and stack_get_size() to pass our tests because a stack can have a stack size.</para>
  <para>src/stack.c:</para>
  <programlisting>...
int
stack_get_size (Stack *stack)
{
    return stack-&gt;size;
}

void
stack_push (Stack *stack, int value)
{
    stack-&gt;size++;
}</programlisting>
  <para>Stack increments it&#39;s size each push and returns the size. A test for stack_get_size() that is failed until now will be passed.</para>
  <programlisting>[stack]% test/run-test.sh
.F

1) Failure: test_push
expected: &lt;!stack_is_empty(stack)&gt; is not FALSE/NULL
test/test-stack.c:36: test_push()

Finished in 0.000113 seconds

2 test(s), 3 assertion(s), 1 failure(s), 0 error(s), 0 pending(s), 0 omission(s), 0 notification(s)
50% passed</programlisting>
  <para>The test for stack_get_size() is passed as our expectation but there is still a failure. It&#39;s a test for stack_is_empty() in test/test-stack.c at the 36th line.</para>
  <para>test/test-stack.c:36:</para>
  <programlisting>cut_assert(!stack_is_empty(stack));</programlisting>
  <para>A stack should not be empty after push.</para>
</refsect2>

  <refsect2>
  <title>Really implement stack_is_empty()</title>
  <para>A stack should be empty only when a stack size is 0. So stack_is_empty() is changed to the following.</para>
  <para>src/stack.c:</para>
  <programlisting>...
int
stack_is_empty (Stack *stack)
{
    return stack-&gt;size == 0;
}
...</programlisting>
  <para>We can run test again and confirm that all of tests are passed.</para>
  <programlisting>% test/run-test.sh
..

Finished in 0.000036 seconds

2 test(s), 4 assertion(s), 0 failure(s), 0 error(s), 0 pending(s), 0 omission(s), 0 notification(s)
100% passed</programlisting>
  <para>A test for push is passed and the rest of tests are kept to pass. A result message is back to green because all of tests are passed. We can feel safe to go to the next stage; We will implement pop.</para>
</refsect2>

</refsect1>

<refsect1>
  <title>Implement pop</title>
  <para>We will implement pop that retrieve a value that is inserted by push.</para>
  <refsect2>
  <title>Test for pop</title>
  <para>Pop returns a value that is inserted by the last push. Pop reduces stack size and finally a stack is empty. The following test represents expected push/pop behavior.</para>
  <para>test/test-stack.c:</para>
  <programlisting>...
void test_pop (void);
...
void
test_pop (void)
{
    stack = stack_new();

    stack_push(stack, 10);
    stack_push(stack, 20);
    stack_push(stack, 30);

    cut_assert_equal_int(3, stack_get_size(stack));
    cut_assert_equal_int(30, stack_pop(stack));
    cut_assert_equal_int(2, stack_get_size(stack));
    cut_assert_equal_int(20, stack_pop(stack));
    cut_assert_equal_int(1, stack_get_size(stack));

    stack_push(stack, 40);
    cut_assert_equal_int(2, stack_get_size(stack));
    cut_assert_equal_int(40, stack_pop(stack));
    cut_assert_equal_int(1, stack_get_size(stack));
    cut_assert_equal_int(10, stack_pop(stack));
    cut_assert_equal_int(0, stack_get_size(stack));
    cut_assert(stack_is_empty(stack));
}</programlisting>
  <para>We can run test.</para>
  <programlisting>[stack]% test/run-test.sh
..cutter: symbol lookup error: test/.libs/test_stack.so: undefined symbol: stack_pop</programlisting>
  <para>There is an error that reports stack_pop() isn&#39;t defined. We can confirm that existed two tests are passed because there are two &quot;.&quot; before the error message.</para>
  <para>NOTE: We doesn&#39;t get the error on Cygwin.</para>
</refsect2>

  <refsect2>
  <title>Implement stack_pop()</title>
  <para>First, we declare stack_pop() in src/stack.h.</para>
  <para>src/stack.h:</para>
  <programlisting>...
int    stack_pop      (Stack *stack);
...</programlisting>
  <para>Next, we define stack_pop() in src/stack.c.</para>
  <para>src/stack.c:</para>
  <programlisting>...
int
stack_pop (Stack *stack)
{
    return 30;
}</programlisting>
  <para>stack_pop() always returns 30 because the first stack_pop() call is required to return 30:</para>
  <para>test/test-stack.c:50:</para>
  <programlisting>cut_assert_equal_int(30, stack_pop(stack));</programlisting>
  <para>We can confirm that test can be run and a test for pop doesn&#39;t report any error.</para>
  <programlisting>[stack]% test/run-test.sh
..F

1) Failure: test_pop
&lt;2 == stack_get_size(stack)&gt;
expected: &lt;2&gt;
 but was: &lt;3&gt;
test/test-stack.c:51: test_pop()

Finished in 0.000307 seconds

3 test(s), 6 assertion(s), 1 failure(s), 0 error(s), 0 pending(s), 0 omission(s), 0 notification(s)
66.6667% passed</programlisting>
  <para>A test for pop is run but failed because the current stack_pop() implementation doesn&#39;t change stack size. The failure is occurred in test/test-stack.c at the 50th line and the reason is stack_get_size() in the target line returns 3 not expected 2.</para>
  <para>test/test-stack.c:51:</para>
  <programlisting>cut_assert_equal_int(2, stack_get_size(stack));</programlisting>
</refsect2>

  <refsect2>
  <title>Allocate memory for data</title>
  <para>We can confirm that the test can be run. We will implement stack_pop() to pass the test.</para>
  <para>A stack needs to save pushed data to retrieve by pop.  A stack needs to have a new field to hold pushed data and stack_push()/stack_pop() allocates/frees memory for pushed data dynamically.</para>
  <para>First, we will add a new field in Stack.  stack_new() initializes the field and stack_free() frees the field.</para>
  <para>src/stack.c:</para>
  <programlisting>...
struct _Stack {
    int size;
    int *data;
};

Stack *
stack_new (void)
{
    ...
    stack-&gt;data = NULL;
    ...
}

void
stack_free (Stack *stack)
{
    free(stack-&gt;data);
    free(stack);
}
...</programlisting>
  <para>At this point, we don&#39;t change any process that effects external program. So we can confirm that the test should be failed the same as before.</para>
  <programlisting>[stack]% test/run-test.sh
..F

1) Failure: test_pop
&lt;2 == stack_get_size(stack)&gt;
expected: &lt;2&gt;
 but was: &lt;3&gt;
test/test-stack.c:51: test_pop()

Finished in 0.000097 seconds

3 test(s), 6 assertion(s), 1 failure(s), 0 error(s), 0 pending(s), 0 omission(s), 0 notification(s)
66.6667% passed</programlisting>
</refsect2>

  <refsect2>
  <title>Really implement stack_pop()</title>
  <para>We added a new field to hold pushed data. stack_push()/stack_pop() can allocate needed memory to the field and save data.</para>
  <para>src/stack.c:</para>
  <programlisting>...
void
stack_push (Stack *stack, int value)
{
    int *new_data;

    stack-&gt;size++;
    new_data = realloc(stack-&gt;data, sizeof(*stack-&gt;data) * stack-&gt;size);
    if (!new_data) {
        free(stack-&gt;data);
        stack-&gt;data = NULL;
        stack-&gt;size = 0;
        return;
    }
    stack-&gt;data = new_data;

    stack-&gt;data[stack-&gt;size - 1] = value;
}

int
stack_pop (Stack *stack)
{
    int value;
    int *new_data;

    stack-&gt;size--;
    value = stack-&gt;data[stack-&gt;size];

    new_data = realloc(stack-&gt;data, sizeof(*stack-&gt;data) * stack-&gt;size);
    if (stack-&gt;size &gt; 0 &amp;&amp; !new_data) {
        free(stack-&gt;data);
        stack-&gt;data = NULL;
        stack-&gt;size = 0;
        return value;
    }
    stack-&gt;data = new_data;

    return value;
}</programlisting>
  <para>We can confirm that the test for pop is passed.</para>
  <programlisting>[stack]% test/run-test.sh
...

Finished in 0.000076 seconds

3 test(s), 15 assertion(s), 0 failure(s), 0 error(s), 0 pending(s), 0 omission(s), 0 notification(s)
100% passed</programlisting>
</refsect2>

</refsect1>

<refsect1>
  <title>Eliminate duplications</title>
  <para>stack_push() and stack_pop() implementations has duplications that are dynamic memory allocation process and error handling process when memory allocation is failed. It&#39;s generally not good that duplications exist because they may increase maintenance cost and so on.</para>
  <para>In this section, we will eliminate duplications without changing existing semantics. We can confirm that existing semantics aren&#39;t changed by running our tests.</para>
  <refsect2>
  <title>Eliminate a duplication in memory allocation process</title>
  <para>First, we will eliminate a duplication in memory allocation process like the following:</para>
  <para>src/stack.c:</para>
  <programlisting>new_data = realloc(stack-&gt;data, sizeof(*stack-&gt;data) * stack-&gt;size);</programlisting>
  <para>We will extract the above part as stack_realloc().</para>
  <para>src/stack.c:</para>
  <programlisting>...
static int *
stack_realloc (Stack *stack)
{
    return realloc(stack-&gt;data, sizeof(*stack-&gt;data) * stack-&gt;size);
}

void
stack_push (Stack *stack, int value)
{
    ...
    new_data = stack_realloc(stack);
    ...
}

int
stack_pop (Stack *stack)
{
    ...
    new_data = stack_realloc(stack);
    ...
}</programlisting>
  <para>We can confirm that existing semantics aren&#39;t changed by running tests.</para>
  <programlisting>[stack]% test/run-test.sh
...

Finished in 0.000078 seconds

3 test(s), 15 assertion(s), 0 failure(s), 0 error(s), 0 pending(s), 0 omission(s), 0 notification(s)
100% passed</programlisting>
  <para>We can go to the next because the result is green.</para>
</refsect2>

  <refsect2>
  <title>Eliminate a duplication in error handling process</title>
  <para>Next, we will eliminate a duplication in error handling process for memory allocation failure. The current implementation is the following:</para>
  <para>src/stack.c:</para>
  <programlisting>...
void
stack_push (Stack *stack, int value)
{
    ...
    new_data = stack_realloc(stack);
    if (!new_data) {
        free(stack-&gt;data);
        stack-&gt;data = NULL;
        stack-&gt;size = 0;
        return;
    }
    ...
}

int
stack_pop (Stack *stack)
{
    ...
    new_data = stack_realloc(stack);
    if (stack-&gt;size &gt; 0 &amp;&amp; !new_data) {
        free(stack-&gt;data);
        stack-&gt;data = NULL;
        stack-&gt;size = 0;
        return value;
    }
    ...
}</programlisting>
  <para>We will move the above error handling process to stack_realloc() and stack_realloc() returns whether memory allocation is succeeded or failed instead of allocated memory.</para>
  <para>src/stack.c:</para>
  <programlisting>...
static int
stack_realloc (Stack *stack)
{
    int *new_data;

    new_data = realloc(stack-&gt;data, sizeof(*stack-&gt;data) * stack-&gt;size);
    if (stack-&gt;size &gt; 0 &amp;&amp; !new_data) {
        free(stack-&gt;data);
        stack-&gt;data = NULL;
        stack-&gt;size = 0;
        return FALSE;
    }
    stack-&gt;data = new_data;

    return TRUE;
}

void
stack_push (Stack *stack, int value)
{
    stack-&gt;size++;
    if (!stack_realloc(stack))
        return;
    stack-&gt;data[stack-&gt;size - 1] = value;
}

int
stack_pop (Stack *stack)
{
    int value;

    stack-&gt;size--;
    value = stack-&gt;data[stack-&gt;size];
    stack_realloc(stack);
    return value;
}</programlisting>
  <para>We should confirm that the changes doesn&#39;t change existing semantics.</para>
  <programlisting>[stack]% test/run-test.sh
...

Finished in 0.000076 seconds

3 test(s), 15 assertion(s), 0 failure(s), 0 error(s), 0 pending(s), 0 omission(s), 0 notification(s)
100% passed</programlisting>
  <para>w We confirmed that we can improve our program by eliminating duplications in our program without changing existing semantics.</para>
</refsect2>

</refsect1>


<refsect1>
  <title>Conclusion</title>
  <para>This documentation shows how to setup a build environment system with GNU build system, write tests with Cutter and improve a program that has tests by using a small stack implementation.</para>
  <refsect2>
  <title>Merit</title>
  <para>GNU build system provides us portability.</para>
  <para>Cutter provides us a method to write tests easily. Existing testing frameworks for C require to use macros to define a test or to register tests explicitly. We need to write many other things except writing tests. Cutter resolves this problem. Cutter doesn&#39;t require to use original macros to define a test. We can write a test as just a normal function. We can also write no test registration code.</para>
  <para>We only used cut_assert() and cut_assert_equal_int() but Cutter provides many assertions to verify actual value is expected value like cut_assert_equal_string(). We will be able to write tests simply by them because we doesn&#39;t need to write our assertions for primitive types.</para>
  <para>Cutter doesn&#39;t show needless information in test result message but show useful information as much as possible. It supports that we can find useful information easily and fix problems easily and rapidly. Cutter also tries to show backtraces on segmentation fault that is often caused for a program written by C for providing many information to fix problems.</para>
  <para>It&#39;s very helpful for maintenance that improving internal structure of a program without changing existing semantics. We can easily confirm that existing semantics isn&#39;t changed with automated tests.</para>
  <para>Automated tests also helps us when a new feature is developed. We can confirm that existing semantics isn&#39;t broken by codes for a new feature. Automated tests are useful for maintenance, developing new features and keeping high-quality.</para>
</refsect2>
  <refsect2>
  <title>Stack test</title>
  <para>The following tests are the final version.</para>
  <para>test/test-stack.c</para>
  <programlisting>#include &lt;cutter.h&gt;
#include &lt;stack.h&gt;

void test_new_stack (void);
void test_push (void);
void test_pop (void);

static Stack *stack;

void
cut_setup (void)
{
    stack = NULL;
}

void
cut_teardown (void)
{
    if (stack)
        stack_free(stack);
}

void
test_new_stack (void)
{
    stack = stack_new();
    cut_assert(stack_is_empty(stack));
}

void
test_push (void)
{
    stack = stack_new();
    cut_assert_equal_int(0, stack_get_size(stack));
    stack_push(stack, 100);
    cut_assert_equal_int(1, stack_get_size(stack));
    cut_assert(!stack_is_empty(stack));
}

void
test_pop (void)
{
    stack = stack_new();

    stack_push(stack, 10);
    stack_push(stack, 20);
    stack_push(stack, 30);

    cut_assert_equal_int(3, stack_get_size(stack));
    cut_assert_equal_int(30, stack_pop(stack));
    cut_assert_equal_int(2, stack_get_size(stack));
    cut_assert_equal_int(20, stack_pop(stack));
    cut_assert_equal_int(1, stack_get_size(stack));

    stack_push(stack, 40);
    cut_assert_equal_int(2, stack_get_size(stack));
    cut_assert_equal_int(40, stack_pop(stack));
    cut_assert_equal_int(1, stack_get_size(stack));
    cut_assert_equal_int(10, stack_pop(stack));
    cut_assert_equal_int(0, stack_get_size(stack));
    cut_assert(stack_is_empty(stack));
}</programlisting>
</refsect2>
  <refsect2>
  <title>Stack implementation</title>
  <para>The following codes are the final version. This stack implementation has some issues that error notification, performance tunings and so on because it&#39;s straightforward. But the implementation has basic features that is shown by test.</para>
  <para>src/stack.c:</para>
  <programlisting>#include &lt;stdlib.h&gt;
#include &quot;stack.h&quot;

#define TRUE 1
#define FALSE 0

struct _Stack {
    int size;
    int *data;
};

Stack *
stack_new (void)
{
    Stack *stack;

    stack = malloc(sizeof(Stack));
    if (!stack)
        return NULL;

    stack-&gt;size = 0;
    stack-&gt;data = NULL;
    return stack;
}

void
stack_free (Stack *stack)
{
    free(stack-&gt;data);
    free(stack);
}

int
stack_is_empty (Stack *stack)
{
    return stack-&gt;size == 0;
}

int
stack_get_size (Stack *stack)
{
    return stack-&gt;size;
}

static int
stack_realloc (Stack *stack)
{
    int *new_data;

    new_data = realloc(stack-&gt;data, sizeof(*stack-&gt;data) * stack-&gt;size);
    if (stack-&gt;size &gt; 0 &amp;&amp; !new_data) {
        free(stack-&gt;data);
        stack-&gt;data = NULL;
        stack-&gt;size = 0;
        return FALSE;
    }
    stack-&gt;data = new_data;

    return TRUE;
}

void
stack_push (Stack *stack, int value)
{
    stack-&gt;size++;
    if (!stack_realloc(stack))
        return;
    stack-&gt;data[stack-&gt;size - 1] = value;
}

int
stack_pop (Stack *stack)
{
    int value;

    stack-&gt;size--;
    value = stack-&gt;data[stack-&gt;size];
    stack_realloc(stack);
    return value;
}</programlisting>
</refsect2>
  <refsect2>
  <title>Support no Cutter installed environment</title>
  <para>In this tutorial, test/test-stack.c build is failed on no Cutter installed environment. That is make fails. If you are a developer, you must run test. So this behavior is reasonable.</para>
  <para>But it&#39;s better that this stack library can be built without Cutter for users that just want to use this stack implementation as a library. They will use a released library that is tested by developers.</para>
  <para>The following is a way to support no Cutter installed environment.</para>
  <para>First, we change AC_CHECK_CUTTER call in configure.ac to work autogen.sh (to be exact, aclocal) without cutter.m4. (If autogen.sh is ran only by developers, this change isn&#39;t needed. In the case, aclocal fails because AC_CHECK_CUTTER isn&#39;t defined.)</para>
  <para>configure.ac:</para>
  <programlisting>...
m4_ifdef([AC_CHECK_CUTTER], [AC_CHECK_CUTTER], [ac_cv_use_cutter=&quot;no&quot;])
...</programlisting>
  <para>We use ac_cv_use_cutter as a variable name because AC_CHECK_CUTTER uses the same variable name. The variable becomes &quot;no&quot; if configure can&#39;t detect Cutter. On no cutter.m4 environment (no Cutter environment when autogen.sh is ran), we always can&#39;t detect Cutter.</para>
  <para>Next, we define a condition that can be used in Makefile.am after AC_CHECK_CUTTER. The condition shows whether we detect Cutter or not.</para>
  <para>configure.ac:</para>
  <programlisting>...
m4_ifdef([AC_CHECK_CUTTER], [AC_CHECK_CUTTER], [ac_cv_use_cutter=&quot;no&quot;])
AM_CONDITIONAL([WITH_CUTTER], [test &quot;$ac_cv_use_cutter&quot; != &quot;no&quot;])
...</programlisting>
  <para>Last, we build test/test-stack.c and run test/run-test.sh only if WITH_CUTTER is true:</para>
  <para>test/Makefile.am:</para>
  <programlisting>if WITH_CUTTER
TESTS = run-test.sh
TESTS_ENVIRONMENT = NO_MAKE=yes CUTTER=&quot;$(CUTTER)&quot;

noinst_LTLIBRARIES = test_stack.la
endif
...</programlisting>
  <para>The followings are the whole of configure.ac and test/Makefile.am:</para>
  <para>configure.ac:</para>
  <programlisting>AC_PREREQ(2.59)

AC_INIT(stack, 0.0.1, you@example.com)
AC_CONFIG_AUX_DIR([config])
AC_CONFIG_HEADER([src/config.h])

AM_INIT_AUTOMAKE($PACKAGE_NAME, $PACKAGE_VERSION)

AC_PROG_LIBTOOL

m4_ifdef([AC_CHECK_CUTTER], [AC_CHECK_CUTTER], [ac_cv_use_cutter=&quot;no&quot;])
AM_CONDITIONAL([WITH_CUTTER], [test &quot;$ac_cv_use_cutter&quot; != &quot;no&quot;])

m4_ifdef([AC_CHECK_COVERAGE], [AC_CHECK_COVERAGE])

AC_CONFIG_FILES([Makefile
                 src/Makefile
                 test/Makefile])

AC_OUTPUT</programlisting>
  <para>test/Makefile.am:</para>
  <programlisting>if WITH_CUTTER
TESTS = run-test.sh
TESTS_ENVIRONMENT = NO_MAKE=yes CUTTER=&quot;$(CUTTER)&quot;

noinst_LTLIBRARIES = test_stack.la
endif

INCLUDES = -I$(top_srcdir)/src
LIBS = $(CUTTER_LIBS) $(top_builddir)/src/libstack.la

AM_CFLAGS = $(CUTTER_CFLAGS)

LDFLAGS = -module -rpath $(libdir) -avoid-version -no-undefined

test_stack_la_SOURCES = test-stack.c

echo-cutter:
	@echo $(CUTTER)</programlisting>
</refsect2>
  <refsect2>
  <title>See also</title>
  <itemizedlist>
  <listitem>
  <para>xUnit: It&#39;s a library that supports a test style that uses assertXXX for verifying an actual value is an expected value. It also called testing framework. Cutter is one of xUnit testing framework. xUnit is implemented in many language:</para>
  <itemizedlist>
  <listitem>
  <para>SUnit (Smalltalk)</para>
</listitem>
  <listitem>
  <para>JUnit (Java)</para>
</listitem>
  <listitem>
  <para>Test::Unit (Ruby)</para>
</listitem>
  <listitem>
  <para>PyUnit (Pytnon)</para>
</listitem>
  <listitem>
  <para>...</para>
</listitem>
</itemizedlist>
</listitem>
  <listitem>
  <para>Extreme Programming (XP): It&#39;s a programming methodology to develop high-quality software. It&#39;s heavy on testing.</para>
</listitem>
</itemizedlist>
</refsect2>
</refsect1>
</refentry>
