File: unit-tests.html

package info (click to toggle)
cyrus-imapd 3.12.1-1
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 60,760 kB
  • sloc: ansic: 280,403; perl: 146,834; javascript: 9,624; sh: 5,730; yacc: 2,660; cpp: 2,263; makefile: 2,103; lex: 675; xml: 621; awk: 303; python: 273; asm: 262
file content (696 lines) | stat: -rw-r--r-- 59,575 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
<!DOCTYPE html>
<html class="writer-html5" lang="en" >
<head>
  <meta charset="utf-8" /><meta name="generator" content="Docutils 0.19: https://docutils.sourceforge.io/" />

  <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  <title>Unit Tests &mdash; Cyrus IMAP 3.12.1 documentation</title>
      <link rel="stylesheet" href="../../_static/pygments.css" type="text/css" />
      <link rel="stylesheet" href="../../_static/css/theme.css" type="text/css" />
      <link rel="stylesheet" href="../../_static/graphviz.css" type="text/css" />
      <link rel="stylesheet" href="../../_static/cyrus.css" type="text/css" />
  
        <script data-url_root="../../" id="documentation_options" src="../../_static/documentation_options.js"></script>
        <script src="../../_static/jquery.js"></script>
        <script src="../../_static/underscore.js"></script>
        <script src="../../_static/_sphinx_javascript_frameworks_compat.js"></script>
        <script src="../../_static/doctools.js"></script>
        <script src="../../_static/sphinx_highlight.js"></script>
    <script src="../../_static/js/theme.js"></script>
    <link rel="index" title="Index" href="../../genindex.html" />
    <link rel="search" title="Search" href="../../search.html" />
    <link rel="next" title="Support/Community" href="../../support.html" />
    <link rel="prev" title="Cyrus IMAP Server: var directory structure" href="guidance/var_directory_structure.html" /> 
</head>

<body class="wy-body-for-nav"> 
  <div class="wy-grid-for-nav">
    <nav data-toggle="wy-nav-shift" class="wy-nav-side">
      <div class="wy-side-scroll">
        <div class="wy-side-nav-search" >

          
          
          <a href="../../index.html" class="icon icon-home">
            Cyrus IMAP
          </a>
              <div class="version">
                3.12.1
              </div>
<div role="search">
  <form id="rtd-search-form" class="wy-form" action="../../search.html" method="get">
    <input type="text" name="q" placeholder="Search docs" aria-label="Search docs" />
    <input type="hidden" name="check_keywords" value="yes" />
    <input type="hidden" name="area" value="default" />
  </form>
</div>
        </div><div class="wy-menu wy-menu-vertical" data-spy="affix" role="navigation" aria-label="Navigation menu">
              <p class="caption" role="heading"><span class="caption-text">Cyrus IMAP</span></p>
<ul class="current">
<li class="toctree-l1"><a class="reference internal" href="../../download.html">Download</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../quickstart.html">Quickstart Guide</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../overview.html">Overview</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../setup.html">Setup</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../operations.html">Operations</a></li>
<li class="toctree-l1 current"><a class="reference internal" href="../../developers.html">Developers</a><ul class="current">
<li class="toctree-l2"><a class="reference internal" href="../../contribute.html">We need your help</a></li>
<li class="toctree-l2"><a class="reference internal" href="documentation.html">Contribute docs</a></li>
<li class="toctree-l2"><a class="reference internal" href="../developer.html">Contribute code and tests</a></li>
<li class="toctree-l2"><a class="reference internal" href="cyrusworks.html">Cyrus.Works</a></li>
<li class="toctree-l2"><a class="reference internal" href="../../developers.html#cyrus-internals">Cyrus Internals</a></li>
<li class="toctree-l2 current"><a class="reference internal" href="../../developers.html#unit-tests">Unit Tests</a><ul class="current">
<li class="toctree-l3 current"><a class="current reference internal" href="#">Unit Tests</a><ul>
<li class="toctree-l4"><a class="reference internal" href="#table-of-contents">Table of Contents</a></li>
<li class="toctree-l4"><a class="reference internal" href="#introduction">1. Introduction</a></li>
<li class="toctree-l4"><a class="reference internal" href="#what-is-a-unit-test">2. What Is A Unit Test?</a></li>
<li class="toctree-l4"><a class="reference internal" href="#running-the-tests">3. Running The Tests</a></li>
<li class="toctree-l4"><a class="reference internal" href="#debugging-a-test">3.6 Debugging A Test</a></li>
<li class="toctree-l4"><a class="reference internal" href="#adding-your-own-tests">4. Adding Your Own Tests</a></li>
<li class="toctree-l4"><a class="reference internal" href="#where-to-put-your-tests">4.1 Where To Put Your Tests</a></li>
<li class="toctree-l4"><a class="reference internal" href="#adding-a-new-suite">4.1 Adding A New Suite</a></li>
<li class="toctree-l4"><a class="reference internal" href="#adding-a-test-to-a-suite">4.2 Adding A Test To A Suite</a></li>
<li class="toctree-l4"><a class="reference internal" href="#suite-setup-and-teardown">4.3 Suite Setup And Teardown</a></li>
</ul>
</li>
</ul>
</li>
</ul>
</li>
<li class="toctree-l1"><a class="reference internal" href="../../support.html">Support/Community</a></li>
</ul>
<p class="caption" role="heading"><span class="caption-text">Cyrus SASL</span></p>
<ul>
<li class="toctree-l1"><a class="reference external" href="http://www.cyrusimap.org/sasl">Cyrus SASL</a></li>
</ul>

        </div>
      </div>
    </nav>

    <section data-toggle="wy-nav-shift" class="wy-nav-content-wrap"><nav class="wy-nav-top" aria-label="Mobile navigation menu" >
          <i data-toggle="wy-nav-top" class="fa fa-bars"></i>
          <a href="../../index.html">Cyrus IMAP</a>
      </nav>

      <div class="wy-nav-content">
        <div class="rst-content">
          <div role="navigation" aria-label="Page navigation">
  <ul class="wy-breadcrumbs">
      <li><a href="../../index.html" class="icon icon-home" aria-label="Home"></a></li>
          <li class="breadcrumb-item"><a href="../../developers.html">Developers</a></li>
      <li class="breadcrumb-item active">Unit Tests</li>
      <li class="wy-breadcrumbs-aside">
              <a href="https://github.com/cyrusimap/cyrus-imapd/blob/master/docsrc/imap/developer/unit-tests.rst" class="fa fa-github"> Edit on GitHub</a>
      </li>
  </ul>
  <hr/>
</div>
          <div role="main" class="document" itemscope="itemscope" itemtype="http://schema.org/Article">
           <div itemprop="articleBody">
             
  <span class="target" id="imap-developer-unit-tests"></span><section id="unit-tests">
<h1>Unit Tests<a class="headerlink" href="#unit-tests" title="Permalink to this heading"></a></h1>
<section id="table-of-contents">
<h2>Table of Contents<a class="headerlink" href="#table-of-contents" title="Permalink to this heading"></a></h2>
<ul class="simple">
<li><p><a class="reference external" href="#introduction">1. Introduction</a></p></li>
<li><p><a class="reference external" href="#what-is-a-unit-test">2. What Is A Unit Test?</a></p></li>
<li><p><a class="reference external" href="#running-the-tests">3. Running The Tests</a></p>
<ul>
<li><p><a class="reference external" href="#setting-up-the-machine">3.1. Setting Up The Machine</a></p></li>
<li><p><a class="reference external" href="#configure-scripts">3.2 Configure Script</a></p></li>
<li><p><a class="reference external" href="#running-the-tests">3.3 Make</a></p></li>
<li><p><a class="reference external" href="#using-valgrind">3.4 Using Valgrind</a></p></li>
<li><p><a class="reference external" href="#the-tests-are-failing">3.5 The Tests Are Failing</a></p></li>
<li><p><a class="reference external" href="#debugging-a-test">3.6 Debugging A Test</a></p></li>
</ul>
</li>
<li><p><a class="reference external" href="#adding-your-own-tests">4. Adding Your Own Tests</a></p>
<ul>
<li><p><a class="reference external" href="#where-to-put-your-tests">4.1 Where To Put Your Tests</a></p></li>
<li><p><a class="reference external" href="#adding-a-new-suite">4.1 Adding A New Suite</a></p></li>
<li><p><a class="reference external" href="#adding-a-test-to-a-suite">4.2 Adding A Test To A Suite</a></p></li>
<li><p><a class="reference external" href="#suite-init-and-cleanup">4.3 Suite Init And Cleanup</a></p></li>
</ul>
</li>
</ul>
</section>
<section id="introduction">
<h2>1. Introduction<a class="headerlink" href="#introduction" title="Permalink to this heading"></a></h2>
<p>Recently, a set of regression unit tests has been added to Cyrus. This
document explains the purpose implementation of those unit tests, and
gives an example of how to add more unit tests (because there are never
enough unit tests!).</p>
</section>
<section id="what-is-a-unit-test">
<h2>2. What Is A Unit Test?<a class="headerlink" href="#what-is-a-unit-test" title="Permalink to this heading"></a></h2>
<p>The <a class="reference external" href="http://en.wikipedia.org/wiki/Unit_test">definition on Wikipedia</a>
sheds some light:</p>
<blockquote>
<div><p>...<strong>unit testing</strong> is a method by which individual units of
source code are tested to determine if they are fit for use. A unit
is the smallest testable part of an application.</p>
</div></blockquote>
<p>In other words, unit testing is about verifying that small pieces of
code, like individual functions, modules, or classes, work in isolation.
It is <strong>not</strong> about testing the system as a whole.</p>
<p>The tests implemented here are also <strong>regression tests</strong>, which in
<a class="reference external" href="http://en.wikipedia.org/wiki/Regression_testing">Wikipedia's words</a>
means:</p>
<blockquote>
<div><p><strong>Regression testing</strong> is any type of software testing that seeks to
uncover software errors after changes to the program (e.g. bugfixes
or new functionality) have been made, by retesting the program. The
intent of regression testing is to assure that a change, such as a
bugfix, did not introduce new bugs.</p>
</div></blockquote>
<p>In other words, the tests are designed to be easy to run and to work out
fully automatically whether they have passed or failed, so that they can
be run usefully by people who didn't write them.</p>
</section>
<section id="running-the-tests">
<h2>3. Running The Tests<a class="headerlink" href="#running-the-tests" title="Permalink to this heading"></a></h2>
<p>This section takes you through the process of running Cyrus' unit tests.</p>
<section id="setting-up-the-machine">
<h3>3.1. Setting Up The Machine<a class="headerlink" href="#setting-up-the-machine" title="Permalink to this heading"></a></h3>
<p>Cyrus' unit tests are all located in a new directory,
<code class="docutils literal notranslate"><span class="pre">cyrus-imapd/cunit/</span></code>. They're written in C, like the remainder of
Cyrus, and use the <a class="reference external" href="http://cunit.sourceforge.net/">CUnit library from
SourceForge</a>, with some home grown
wrappers and other improvements to make our lives easier.</p>
<p>Your first step is step is to ensure that the CUnit library (including
the headers) is installed. Some modern operating systems already have
CUnit, for example on Ubuntu you can just do:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">me</span><span class="nd">@ubuntu</span><span class="o">&gt;</span> <span class="n">sudo</span> <span class="n">apt</span><span class="o">-</span><span class="n">get</span> <span class="n">install</span> <span class="n">libcunit1</span><span class="o">-</span><span class="n">dev</span>
</pre></div>
</div>
<p>Alternately, you can download the CUnit source, build it and install it.
It's not a complicated or difficult library, this shouldn't take long.
When you've done, install it in <code class="docutils literal notranslate"><span class="pre">/usr/include</span></code> and <code class="docutils literal notranslate"><span class="pre">/usr/lib</span></code>.</p>
</section>
<section id="configure-script">
<h3>3.2 Configure Script<a class="headerlink" href="#configure-script" title="Permalink to this heading"></a></h3>
<p>Because of the dependency on the CUnit library, the tests are disabled
by default; this means you need enable them with an option to the
<code class="docutils literal notranslate"><span class="pre">configure</span></code> script:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">me</span><span class="nd">@mybox</span><span class="o">&gt;</span> <span class="o">./</span><span class="n">configure</span> <span class="o">--</span><span class="n">enable</span><span class="o">-</span><span class="n">unit</span><span class="o">-</span><span class="n">tests</span>
<span class="o">...</span>
<span class="n">checking</span> <span class="k">for</span> <span class="n">CU</span>\<span class="n">_initialize</span>\<span class="n">_registry</span> <span class="ow">in</span> <span class="o">-</span><span class="n">lcunit</span><span class="o">...</span> <span class="n">yes</span>
<span class="n">checking</span> <span class="n">CUnit</span><span class="o">/</span><span class="n">CUnit</span><span class="o">.</span><span class="n">h</span> <span class="n">usability</span><span class="o">...</span> <span class="n">yes</span>
<span class="n">checking</span> <span class="n">CUnit</span><span class="o">/</span><span class="n">CUnit</span><span class="o">.</span><span class="n">h</span> <span class="n">presence</span><span class="o">...</span> <span class="n">yes</span>
<span class="n">checking</span> <span class="k">for</span> <span class="n">CUnit</span><span class="o">/</span><span class="n">CUnit</span><span class="o">.</span><span class="n">h</span><span class="o">...</span> <span class="n">yes</span>
<span class="o">...</span>
</pre></div>
</div>
</section>
<section id="make">
<h3>3.3 Make<a class="headerlink" href="#make" title="Permalink to this heading"></a></h3>
<p>First you need to build Cyrus itself, using the traditional <code class="docutils literal notranslate"><span class="pre">all:</span></code>
target.</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">me</span><span class="nd">@mybox</span><span class="o">&gt;</span> <span class="n">make</span> <span class="nb">all</span>
<span class="o">...</span>
</pre></div>
</div>
<p>Then, use the new <code class="docutils literal notranslate"><span class="pre">check:</span></code> target to build and run the unit tests.</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span>me@mybox&gt; make check
 cd . &amp;&amp; /bin/bash /home/me/cyrus-imapd/missing --run automake-1.11 --foreign Makefile
 cd . &amp;&amp; /bin/bash ./config.status Makefile depfiles (a)
config.status: creating Makefile
config.status: executing depfiles commands
...
make[3]: Entering directory \`/home/me/cyrus-imapd&#39;
make[3]: \`sieve/test&#39; is up to date.
cunit/cunit.pl --project cunit/default.cunit --generate-wrapper cunit/mboxname.testc (b)
gcc -DHAVE\_CONFIG\_H ... -c -o cunit/mboxname.o cunit/mboxname.testc-cunit.c
rm -f cunit/mboxname.testc-cunit.c
/bin/bash ./libtool --tag=CC --mode=link gcc -fPIC -g -O2 -o
cunit/unit cunit/unit.o ... lib/libcyrus\_min.la ... (c)
...
Running unit tests (d)

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

...
Suite: mboxname (e)
  Test: dir\_hash\_c ... passed
  Test: to\_parts ... passed
  Test: to\_userid ... passed
  Test: to\_usermbox ... passed
...
--Run Summary: Type      Total     Ran  Passed  Failed (f)
               suites       34      34     n/a       0
               tests       323     323     323       0
               asserts 1079745 1079745 1079745       0
make[1]: Leaving directory `/home/me/cyrus-imapd/cunit&#39;
</pre></div>
</div>
<p>Let's take a closer look at what's happening here.</p>
<ol class="loweralpha simple">
<li><p>The <code class="docutils literal notranslate"><span class="pre">check:</span></code> target causes automake to re-run itself. This is
normal automake behaviour. Note that the older build system used to
run make recursively in sub-directories, the newer automake-based
system builds everything from the top directory.</p></li>
<li><p>The <code class="docutils literal notranslate"><span class="pre">cunit/</span></code> directory contains a number of C source files
(called, for reasons too complicated to explain here,
<em>whatever</em>.testc) with test code in them. For each of those, a small
wrapper C source file is generated and then compiled into an object
file.</p></li>
<li><p>Finally, all the compiled object files are linked into an
executable, with a <code class="docutils literal notranslate"><span class="pre">main()</span></code> routine from <code class="docutils literal notranslate"><span class="pre">unit.c</span></code>, and a number
of libraries and object files from other parts of the Cyrus tree.</p></li>
<li><p>The resulting executable is then run.</p></li>
<li><p>The test executable runs all the built tests one by one, telling us
which ones passed and which ones failed as it runs them. You can
also run it manually with the name of a test as an argument, and it
will run only the named test.</p></li>
<li><p>At the end, the text executable prints a summary of how many tests
it ran and how many passed and failed. The key thing to look at here
is the rightmost column, it should be all zero.</p></li>
</ol>
</section>
<section id="using-valgrind">
<h3>3.4 Using Valgrind<a class="headerlink" href="#using-valgrind" title="Permalink to this heading"></a></h3>
<p>Some failure modes are subtle, and cannot be detected in the C code
itself; this is where <a class="reference external" href="http://valgrind.org/">the Valgrind program</a>
comes in very handy. It detects buffer overruns and memory leaks and
various other kinds of subtle errors.</p>
<p>To run the unit tests with Valgrind, use the new <code class="docutils literal notranslate"><span class="pre">valgrind:</span></code> target.</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span>me@mybox&gt; make valgrind
...
valgrind --tool=memcheck --leak-check=full ./unit -v (a)
==2999== Memcheck, a memory error detector
==2999== Copyright (C) 2002-2010, and GNU GPL&#39;d, by Julian Seward et al.
==2999== Using Valgrind-3.6.0.SVN-Debian and LibVEX; [...]
==2999== Command: ./unit -v
==2999==
...
--Run Summary: Type      Total     Ran  Passed  Failed   (b)
               suites        9       9     n/a       0
               tests        51      51      50       1
               asserts     474     474     473       1
...
==2999== HEAP SUMMARY:   (c)
==2999==     in use at exit: 4,489 bytes in 134 blocks
==2999==   total heap usage: 715 allocs, 581 frees, 352,763 bytes allocated
==2999==
==2999== 4 bytes in 1 blocks are definitely lost in loss record 3 of 50
==2999==    at 0x4C2815C: malloc (vg_replace_malloc.c:236)
==2999==    by 0x44A0CA: xmalloc (xmalloc.c:57)
==2999==    by 0x4399D8: strconcat (util.c:631)
==2999==    by 0x40C059: test_uncast_null (strconcat.c:51)
==2999==    by 0x61B32A9: ??? (in /usr/lib/libcunit.so.1.0.1)
==2999==    by 0x61B36ED: ??? (in /usr/lib/libcunit.so.1.0.1)
==2999==    by 0x61B3827: CU_run_all_tests (in /usr/lib/libcunit.so.1.0.1)
==2999==    by 0x4066CC: run_tests (unit.c:144)
==2999==    by 0x406806: main (unit.c:283)
==2999==
...
</pre></div>
</div>
<p>Here's an explanation of what's happening in the example.</p>
<ol class="loweralpha simple">
<li><p>The test executable is run as before, but using the <code class="docutils literal notranslate"><span class="pre">valgrind</span></code>
program. The first thing we see is Valgrind's banner message.</p></li>
<li><p>The test executable proceeds as normal and eventually emits it's run
summary, then exits.</p></li>
<li><p>After the test executable exits, Valgrind checks for memory leaks
and prints both a summary of all leaks and a stack trace showing
where each block of leaked memory was allocated.</p></li>
</ol>
<p>I'd just like to say that I love Valgrind and I think it's immensely
useful. I would have made running the tests under Valgrind the only
option for the <code class="docutils literal notranslate"><span class="pre">check:</span></code> target, except that Valgrind is not available
on all of Cyrus' supported platforms.</p>
</section>
<section id="the-tests-are-failing">
<h3>3.5 The Tests Are Failing<a class="headerlink" href="#the-tests-are-failing" title="Permalink to this heading"></a></h3>
<p>So you've noticed that some of the tests are failing. Let me make the
guiding principle of unit testing as clear as possible: <strong>THE UNIT TESTS
SHOULD NOT FAIL</strong>. All of the tests are designed to pass all the time,
in everyone's environment. The unit tests are run automatically every
twelve hours on the Cyrus <a class="reference external" href="http://ci.cyrusimap.org/">Continuous Integration
server</a>, and a failing test fails the whole
build and makes people unhappy.</p>
<p>There are a few rules which you should follow to help us all get the
most benefit out of unit testing</p>
<ul class="simple">
<li><p>If you see a test failing, investigate it.</p></li>
<li><p>If you can't investigate, complain on the mailing list or raise a bug
so that somebody else can investigate.</p></li>
<li><p>When writing tests, write them to work in all environments and all
combinations of <code class="docutils literal notranslate"><span class="pre">configure</span></code> script options. It's ok to have a test
which is empty in some circumstances; it's not ok to have a test that
fails.</p></li>
<li><p>When adding code, write new tests for the new code.</p></li>
<li><p>When modifying code, write new tests for the new behaviour.</p></li>
<li><p>When looking at old code, also take a look at the <a class="reference external" href="http://ci.cyrusimap.org/job/cyrus-imapd-master/887/cobertura/">coverage
report</a>
and consider writing tests for the existing code.</p></li>
</ul>
</section>
</section>
<section id="debugging-a-test">
<h2>3.6 Debugging A Test<a class="headerlink" href="#debugging-a-test" title="Permalink to this heading"></a></h2>
<p>With the new Cyrus build system, the file <code class="docutils literal notranslate"><span class="pre">cunit/unit</span></code> is no longer an
executable, it's a shell script which sets up some environment variables
before running the real executable which is hidden away. This makes
debugging a failing test somewhat challenging. The solution is:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">me</span><span class="nd">@mybox</span><span class="o">&gt;</span> <span class="p">(</span> <span class="n">cd</span> <span class="n">cunit</span> <span class="p">;</span> <span class="n">libtool</span> <span class="o">--</span><span class="n">mode</span><span class="o">=</span><span class="n">execute</span> <span class="n">gdb</span> <span class="o">--</span><span class="n">args</span> <span class="n">unit</span> <span class="o">-</span><span class="n">t</span> <span class="n">crc32</span> <span class="p">)</span>
<span class="o">...</span>
<span class="n">Reading</span> <span class="n">symbols</span> <span class="kn">from</span> <span class="o">/</span><span class="n">home</span><span class="o">/</span><span class="n">me</span><span class="o">/</span><span class="n">cyrus</span><span class="o">-</span><span class="n">imapd</span><span class="o">/</span><span class="n">cunit</span><span class="o">/.</span><span class="n">libs</span><span class="o">/</span><span class="n">lt</span><span class="o">-</span><span class="n">unit</span><span class="o">...</span><span class="n">done</span><span class="o">.</span>
<span class="p">(</span><span class="n">gdb</span><span class="p">)</span> <span class="nb">list</span> <span class="n">crc32</span><span class="o">.</span><span class="n">testc</span><span class="p">:</span><span class="mi">1</span>
<span class="mi">1</span>       <span class="o">/*</span> <span class="n">Unit</span> <span class="n">test</span> <span class="k">for</span> <span class="n">lib</span><span class="o">/</span><span class="n">crc32</span><span class="o">.</span><span class="n">c</span> <span class="o">*/</span>
<span class="mi">2</span>       <span class="c1">#include &quot;cunit/cyrunit.h&quot;</span>
<span class="mi">3</span>       <span class="c1">#include &quot;crc32.h&quot;</span>
<span class="o">...</span>
<span class="p">(</span><span class="n">gdb</span><span class="p">)</span> <span class="k">break</span> <span class="n">test_map</span>
<span class="n">Breakpoint</span> <span class="mi">1</span> <span class="n">at</span> <span class="mh">0x44a2f8</span><span class="p">:</span> <span class="n">file</span> <span class="o">./</span><span class="n">cunit</span><span class="o">/</span><span class="n">crc32</span><span class="o">.</span><span class="n">testc</span><span class="p">,</span> <span class="n">line</span> <span class="mf">11.</span>
<span class="p">(</span><span class="n">gdb</span><span class="p">)</span> <span class="n">run</span>
<span class="n">Starting</span> <span class="n">program</span><span class="p">:</span> <span class="o">/</span><span class="n">home</span><span class="o">/</span><span class="n">me</span><span class="o">/</span><span class="n">cyrus</span><span class="o">-</span><span class="n">imapd</span><span class="o">/</span><span class="n">cunit</span><span class="o">/.</span><span class="n">libs</span><span class="o">/</span><span class="n">lt</span><span class="o">-</span><span class="n">unit</span> <span class="o">-</span><span class="n">t</span> <span class="o">-</span><span class="n">v</span> <span class="n">crc32</span>
<span class="p">[</span><span class="n">Thread</span> <span class="n">debugging</span> <span class="n">using</span> <span class="n">libthread_db</span> <span class="n">enabled</span><span class="p">]</span>

    <span class="n">CUnit</span> <span class="o">-</span> <span class="n">A</span> <span class="n">Unit</span> <span class="n">testing</span> <span class="n">framework</span> <span class="k">for</span> <span class="n">C</span> <span class="o">-</span> <span class="n">Version</span> <span class="mf">2.1</span><span class="o">-</span><span class="mi">0</span>
    <span class="n">http</span><span class="p">:</span><span class="o">//</span><span class="n">cunit</span><span class="o">.</span><span class="n">sourceforge</span><span class="o">.</span><span class="n">net</span><span class="o">/</span>

<span class="n">Suite</span><span class="p">:</span> <span class="n">crc32</span>
  <span class="n">Test</span><span class="p">:</span> <span class="nb">map</span> <span class="o">...</span>
<span class="n">Breakpoint</span> <span class="mi">1</span><span class="p">,</span> <span class="n">test_map</span> <span class="p">()</span> <span class="n">at</span> <span class="o">./</span><span class="n">cunit</span><span class="o">/</span><span class="n">crc32</span><span class="o">.</span><span class="n">testc</span><span class="p">:</span><span class="mi">11</span>
<span class="mi">11</span>          <span class="n">c</span> <span class="o">=</span> <span class="n">crc32_map</span><span class="p">(</span><span class="n">TEXT</span><span class="p">,</span> <span class="n">sizeof</span><span class="p">(</span><span class="n">TEXT</span><span class="p">)</span><span class="o">-</span><span class="mi">1</span><span class="p">);</span>
<span class="p">(</span><span class="n">gdb</span><span class="p">)</span>
</pre></div>
</div>
<p>Note the <strong>-t</strong> option. This turns off test timeouts, which is very
useful for manual debugging.</p>
</section>
<section id="adding-your-own-tests">
<h2>4. Adding Your Own Tests<a class="headerlink" href="#adding-your-own-tests" title="Permalink to this heading"></a></h2>
<p>Adding your own tests is quite simple. Here's how.</p>
</section>
<section id="where-to-put-your-tests">
<h2>4.1 Where To Put Your Tests<a class="headerlink" href="#where-to-put-your-tests" title="Permalink to this heading"></a></h2>
<p>The unit test code in Cyrus is contained in a set of C source files in
the <code class="docutils literal notranslate"><span class="pre">cunit</span></code> directory. For reasons too complex to go into here, these
are named <em>whatever</em>.testc instead of the more usual <em>whatever</em>.c. If
you look closely, you will see that each of those C source files maps to
a &quot;Suite&quot; in CUnit parlance. For example, <code class="docutils literal notranslate"><span class="pre">cunit/glob.testc</span></code> is listed
as the Suite &quot;glob&quot; in CUnit's runtime output.</p>
<p>Typically, each Suite tests a single module or a related set of
functions; for example, <code class="docutils literal notranslate"><span class="pre">cunit/glob.testc</span></code> contains tests for the glob
module in <code class="docutils literal notranslate"><span class="pre">lib/glob.c</span></code>.</p>
<p>So, if you want to add a new test for a module which already has some
existing tests, the sensible thing to do is to <a class="reference external" href="#adding-a-test-to-a-suite">add a new test to the
existing suite</a>. Otherwise, you'll need to
<a class="reference external" href="#adding-a-new-suite">add a new Suite</a>.</p>
</section>
<section id="adding-a-new-suite">
<h2>4.1 Adding A New Suite<a class="headerlink" href="#adding-a-new-suite" title="Permalink to this heading"></a></h2>
<p>Each Suite is a single C source file in the <code class="docutils literal notranslate"><span class="pre">cunit/</span></code> directory. Your
first step is to create a new C source file. For this example, you'll
create a new Suite to test the CRC32 routines which live in
<code class="docutils literal notranslate"><span class="pre">lib/crc32.c</span></code>.</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">me</span><span class="nd">@mybox</span><span class="o">&gt;</span> <span class="n">vi</span> <span class="n">cunit</span><span class="o">/</span><span class="n">crc32</span><span class="o">.</span><span class="n">testc</span>
<span class="o">...</span>
</pre></div>
</div>
<p>The file should contain something like this.</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="o">/*</span> <span class="n">Unit</span> <span class="n">test</span> <span class="k">for</span> <span class="n">lib</span><span class="o">/</span><span class="n">crc32</span><span class="o">.</span><span class="n">c</span> <span class="o">*/</span>
<span class="c1">#include &quot;cunit/cyrunit.h&quot;  (a)</span>
<span class="c1">#include &quot;crc32.h&quot;  (b)</span>

<span class="n">static</span> <span class="n">void</span> <span class="n">test_map</span><span class="p">(</span><span class="n">void</span><span class="p">)</span>  <span class="p">(</span><span class="n">c</span><span class="p">)</span>
<span class="p">{</span>
    <span class="n">static</span> <span class="n">const</span> <span class="n">char</span> <span class="n">TEXT</span><span class="p">[]</span> <span class="o">=</span> <span class="s2">&quot;lorem ipsum&quot;</span><span class="p">;</span>  <span class="p">(</span><span class="n">d</span><span class="p">)</span>
    <span class="n">static</span> <span class="n">uint32_t</span> <span class="n">CRC32</span> <span class="o">=</span> <span class="mh">0x0</span><span class="p">;</span>
    <span class="n">uint32_t</span> <span class="n">c</span><span class="p">;</span>  <span class="p">(</span><span class="n">e</span><span class="p">)</span>

    <span class="n">c</span> <span class="o">=</span> <span class="n">crc32_map</span><span class="p">(</span><span class="n">TEXT</span><span class="p">,</span> <span class="n">sizeof</span><span class="p">(</span><span class="n">TEXT</span><span class="p">)</span><span class="o">-</span><span class="mi">1</span><span class="p">);</span>  <span class="p">(</span><span class="n">f</span><span class="p">)</span>
    <span class="n">CU_ASSERT_EQUAL</span><span class="p">(</span><span class="n">c</span><span class="p">,</span> <span class="n">CRC32</span><span class="p">);</span>  <span class="p">(</span><span class="n">g</span><span class="p">)</span>
<span class="p">}</span>
</pre></div>
</div>
<p>Here's an explanation of what all these bits are for.</p>
<ol class="loweralpha simple">
<li><p>You need to include the header <code class="docutils literal notranslate"><span class="pre">&quot;cunit/cyrunit.h&quot;</span></code>, which is a thin
Cyrus wrapper around the CUnit's library's header,
<code class="docutils literal notranslate"><span class="pre">&lt;CUnit/CUnit.h&gt;</span></code> with some extra conveniences.</p></li>
<li><p>You should also include any headers you need for declarations of the
functions which you'll be testing. Note that the Cyrus <code class="docutils literal notranslate"><span class="pre">lib/</span></code> and
<code class="docutils literal notranslate"><span class="pre">imap/</span></code> directories are already in the include path, so any header
in there can be included without the directory prefix, e.g.
<code class="docutils literal notranslate"><span class="pre">&quot;crc32.h&quot;</span></code> for <code class="docutils literal notranslate"><span class="pre">lib/crc32.h</span></code>.</p></li>
<li><p>You need to have at least one function which looks like this: it
takes no arguments, returns void, and is named <code class="docutils literal notranslate"><span class="pre">test_whatever</span></code>. It
may be <code class="docutils literal notranslate"><span class="pre">static</span></code> or <code class="docutils literal notranslate"><span class="pre">extern</span></code>, but I recommend <code class="docutils literal notranslate"><span class="pre">static</span></code>.
Functions with this signature are automatically discovered in the
source code by the Cyrus unit test infrastructure, so all you have
to do is write the function. Later, a CUnit test named &quot;whatever&quot;
will be created automatically for your <code class="docutils literal notranslate"><span class="pre">test_whatever</span></code> function.</p></li>
<li><p>Here's a good place to define the test inputs and expected outputs.
Note that for this example you have no idea of the actual correct
output. The right thing to do there is to manually calculate the
expected result from first principles, or to use a different piece
of software which you believe to be working. For this example, let's
just use a known incorrect value and see what happens.</p></li>
<li><p>Here's a good place for local variables you need during the test.</p></li>
<li><p>Call the function under test (<code class="docutils literal notranslate"><span class="pre">crc32_map()</span></code> in this example) with
known inputs, and capture the results in a local variable <code class="docutils literal notranslate"><span class="pre">c</span></code>.</p></li>
<li><p>Compare the actual result in <code class="docutils literal notranslate"><span class="pre">c</span></code> with the expected result in
<code class="docutils literal notranslate"><span class="pre">CRC32</span></code>. The <code class="docutils literal notranslate"><span class="pre">CU_ASSERT_EQUAL()</span></code> macro checks that it's two
arguments are equal (using an integer comparison), and if they're
different it prints a message and records a failure. Note that
unlike the libc <code class="docutils literal notranslate"><span class="pre">assert()</span></code> macro, control will continue even if
the assert fails. The CUnit library provides a whole family of
similar macros, see <a class="reference external" href="http://cunit.sourceforge.net/doc/writing_tests.html#assertions">the online CUnit
documentation</a>
for more details.</p></li>
</ol>
<p>Now you need to tell the Cyrus build system about your new Suite.</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">me</span><span class="nd">@mybox</span><span class="o">&gt;</span> <span class="n">vi</span> <span class="n">Makefile</span><span class="o">.</span><span class="n">am</span>
<span class="o">...</span>
</pre></div>
</div>
<p>You need to add the filename of your new test to the definition of the
<code class="docutils literal notranslate"><span class="pre">cunit_TESTS</span></code> variable.</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">cunit_TESTS</span> <span class="o">=</span> \
    <span class="n">cunit</span><span class="o">/</span><span class="n">aaa</span><span class="o">-</span><span class="n">db</span><span class="o">.</span><span class="n">testc</span> \
    <span class="n">cunit</span><span class="o">/</span><span class="n">annotate</span><span class="o">.</span><span class="n">testc</span> \
    <span class="n">cunit</span><span class="o">/</span><span class="n">backend</span><span class="o">.</span><span class="n">testc</span> \
    <span class="n">cunit</span><span class="o">/</span><span class="n">binhex</span><span class="o">.</span><span class="n">testc</span> \
    <span class="n">cunit</span><span class="o">/</span><span class="n">bitvector</span><span class="o">.</span><span class="n">testc</span> \
    <span class="n">cunit</span><span class="o">/</span><span class="n">buf</span><span class="o">.</span><span class="n">testc</span> \
    <span class="n">cunit</span><span class="o">/</span><span class="n">byteorder</span><span class="o">.</span><span class="n">testc</span> \
    <span class="n">cunit</span><span class="o">/</span><span class="n">charset</span><span class="o">.</span><span class="n">testc</span> \
    <span class="n">cunit</span><span class="o">/</span><span class="n">crc32</span><span class="o">.</span><span class="n">testc</span> \
    <span class="n">cunit</span><span class="o">/</span><span class="n">dlist</span><span class="o">.</span><span class="n">testc</span> \
    <span class="n">cunit</span><span class="o">/</span><span class="n">duplicate</span><span class="o">.</span><span class="n">testc</span> \
</pre></div>
</div>
<p>At this point you should be able to just rebuild and rerun using <strong>make
check</strong>. You can also just rebuild without rerunning by using the
command <strong>make cunit/unit</strong>.</p>
<p>Note that sometimes this doesn't quite work right, and you may be able
to work around this problem using the command <strong>rm
cunit/default.cunit</strong>.</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">me</span><span class="nd">@mybox</span><span class="o">&gt;</span> <span class="n">make</span> <span class="n">check</span>
<span class="o">...</span>
<span class="o">../</span><span class="n">cunit</span><span class="o">/</span><span class="n">cunit</span><span class="o">.</span><span class="n">pl</span> <span class="p">[</span><span class="o">...</span><span class="p">]</span> <span class="o">--</span><span class="n">add</span><span class="o">-</span><span class="n">sources</span> <span class="p">[</span><span class="o">...</span><span class="p">]</span> <span class="n">crc32</span><span class="o">.</span><span class="n">testc</span>
<span class="o">...</span>
<span class="o">../</span><span class="n">cunit</span><span class="o">/</span><span class="n">cunit</span><span class="o">.</span><span class="n">pl</span> <span class="p">[</span><span class="o">...</span><span class="p">]</span> <span class="o">--</span><span class="n">generate</span><span class="o">-</span><span class="n">wrapper</span> <span class="n">crc32</span><span class="o">.</span><span class="n">testc</span>
<span class="n">gcc</span> <span class="o">-</span><span class="n">c</span> <span class="p">[</span><span class="o">...</span><span class="p">]</span> <span class="o">-</span><span class="n">g</span> <span class="o">-</span><span class="n">O2</span> <span class="o">.</span><span class="n">cunit</span><span class="o">-</span><span class="n">crc32</span><span class="o">.</span><span class="n">c</span>
<span class="n">gcc</span> <span class="p">[</span><span class="o">...</span><span class="p">]</span> <span class="o">-</span><span class="n">o</span> <span class="n">unit</span> <span class="p">[</span><span class="o">...</span><span class="p">]</span> <span class="o">.</span><span class="n">cunit</span><span class="o">-</span><span class="n">crc32</span><span class="o">.</span><span class="n">o</span> <span class="o">...</span>
<span class="n">Running</span> <span class="n">unit</span> <span class="n">tests</span>

    <span class="n">CUnit</span> <span class="o">-</span> <span class="n">A</span> <span class="n">Unit</span> <span class="n">testing</span> <span class="n">framework</span> <span class="k">for</span> <span class="n">C</span> <span class="o">-</span> <span class="n">Version</span> <span class="mf">2.1</span><span class="o">-</span><span class="mi">0</span>
    <span class="n">http</span><span class="p">:</span><span class="o">//</span><span class="n">cunit</span><span class="o">.</span><span class="n">sourceforge</span><span class="o">.</span><span class="n">net</span><span class="o">/</span>

<span class="o">...</span>
<span class="n">Suite</span><span class="p">:</span> <span class="n">crc32</span>
  <span class="n">Test</span><span class="p">:</span> <span class="nb">map</span> <span class="o">...</span> <span class="n">FAILED</span>
    <span class="mf">1.</span> <span class="n">crc32</span><span class="o">.</span><span class="n">testc</span><span class="p">:</span><span class="mi">12</span>  <span class="o">-</span> <span class="n">CU_ASSERT_EQUAL</span><span class="p">(</span><span class="n">c</span><span class="o">=</span><span class="mi">1926722702</span><span class="p">,</span><span class="n">CRC32</span><span class="o">=</span><span class="mi">0</span><span class="p">)</span>
</pre></div>
</div>
<p>Note how the test failure told us which in source file and at what line
number the failure occurred, and what the actual and expected values
were. Let's go and fix that up now.</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">static</span> <span class="n">const</span> <span class="n">char</span> <span class="n">TEXT</span><span class="p">[]</span> <span class="o">=</span> <span class="s2">&quot;lorem ipsum&quot;</span><span class="p">;</span>
<span class="n">static</span> <span class="n">uint32</span>\<span class="n">_t</span> <span class="n">CRC32</span> <span class="o">=</span> <span class="mh">0x72d7748e</span><span class="p">;</span>
</pre></div>
</div>
<p>Re-run <code class="docutils literal notranslate"><span class="pre">make</span> <span class="pre">check</span></code> and you'll see your test being rebuilt and rerun,
and this time passing.</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">me</span><span class="nd">@mybox</span><span class="o">&gt;</span> <span class="n">make</span> <span class="n">check</span>
<span class="o">...</span>
<span class="o">../</span><span class="n">cunit</span><span class="o">/</span><span class="n">cunit</span><span class="o">.</span><span class="n">pl</span> <span class="p">[</span><span class="o">...</span><span class="p">]</span> <span class="o">--</span><span class="n">generate</span><span class="o">-</span><span class="n">wrapper</span> <span class="n">crc32</span><span class="o">.</span><span class="n">testc</span>
<span class="n">gcc</span> <span class="o">-</span><span class="n">c</span> <span class="p">[</span><span class="o">...</span><span class="p">]</span> <span class="o">-</span><span class="n">g</span> <span class="o">-</span><span class="n">O2</span> <span class="o">.</span><span class="n">cunit</span><span class="o">-</span><span class="n">crc32</span><span class="o">.</span><span class="n">c</span>
<span class="n">gcc</span> <span class="p">[</span><span class="o">...</span><span class="p">]</span> <span class="o">-</span><span class="n">o</span> <span class="n">unit</span> <span class="p">[</span><span class="o">...</span><span class="p">]</span> <span class="o">.</span><span class="n">cunit</span><span class="o">-</span><span class="n">crc32</span><span class="o">.</span><span class="n">o</span>
<span class="o">...</span>
<span class="n">Running</span> <span class="n">unit</span> <span class="n">tests</span>

    <span class="n">CUnit</span> <span class="o">-</span> <span class="n">A</span> <span class="n">Unit</span> <span class="n">testing</span> <span class="n">framework</span> <span class="k">for</span> <span class="n">C</span> <span class="o">-</span> <span class="n">Version</span> <span class="mf">2.1</span><span class="o">-</span><span class="mi">0</span>
    <span class="n">http</span><span class="p">:</span><span class="o">//</span><span class="n">cunit</span><span class="o">.</span><span class="n">sourceforge</span><span class="o">.</span><span class="n">net</span><span class="o">/</span>

<span class="o">...</span>
<span class="n">Suite</span><span class="p">:</span> <span class="n">crc32</span>
  <span class="n">Test</span><span class="p">:</span> <span class="nb">map</span> <span class="o">...</span> <span class="n">passed</span>
</pre></div>
</div>
</section>
<section id="adding-a-test-to-a-suite">
<h2>4.2 Adding A Test To A Suite<a class="headerlink" href="#adding-a-test-to-a-suite" title="Permalink to this heading"></a></h2>
<p>Adding a new test to an existing test is easy: all you have to do is add
a new function to an existing C source file in the <code class="docutils literal notranslate"><span class="pre">cunit/</span></code> directory.
As an example, let's add a test for the <code class="docutils literal notranslate"><span class="pre">crc_iovec()</span></code> function.</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">me</span><span class="nd">@mybox</span><span class="o">&gt;</span> <span class="n">vi</span> <span class="n">cunit</span><span class="o">/</span><span class="n">crc32</span><span class="o">.</span><span class="n">testc</span>
<span class="o">...</span>

<span class="n">static</span> <span class="n">void</span> <span class="n">test_iovec</span><span class="p">(</span><span class="n">void</span><span class="p">)</span>  <span class="p">(</span><span class="n">a</span><span class="p">)</span>
<span class="p">{</span>
    <span class="n">static</span> <span class="n">const</span> <span class="n">char</span> <span class="n">TEXT1</span><span class="p">[]</span> <span class="o">=</span> <span class="s2">&quot;lorem&quot;</span><span class="p">;</span>  <span class="p">(</span><span class="n">b</span><span class="p">)</span>
    <span class="n">static</span> <span class="n">const</span> <span class="n">char</span> <span class="n">TEXT2</span><span class="p">[]</span> <span class="o">=</span> <span class="s2">&quot; ipsum&quot;</span><span class="p">;</span>
    <span class="n">static</span> <span class="n">uint32_t</span> <span class="n">CRC32</span> <span class="o">=</span> <span class="mh">0x72d7748e</span><span class="p">;</span>
    <span class="n">uint32_t</span> <span class="n">c</span><span class="p">;</span>  <span class="p">(</span><span class="n">c</span><span class="p">)</span>
    <span class="n">struct</span> <span class="n">iovec</span> <span class="n">iov</span><span class="p">[</span><span class="mi">2</span><span class="p">];</span>

    <span class="n">memset</span><span class="p">(</span><span class="n">iov</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="n">sizeof</span><span class="p">(</span><span class="n">iov</span><span class="p">));</span>  <span class="p">(</span><span class="n">d</span><span class="p">)</span>
    <span class="n">iov</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span><span class="o">.</span><span class="n">iov_base</span> <span class="o">=</span> <span class="n">TEXT1</span><span class="p">;</span>
    <span class="n">iov</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span><span class="o">.</span><span class="n">iov_len</span> <span class="o">=</span> <span class="n">sizeof</span><span class="p">(</span><span class="n">TEXT1</span><span class="p">)</span><span class="o">-</span><span class="mi">1</span><span class="p">;</span>
    <span class="n">iov</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span><span class="o">.</span><span class="n">iov_base</span> <span class="o">=</span> <span class="n">TEXT2</span><span class="p">;</span>
    <span class="n">iov</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span><span class="o">.</span><span class="n">iov_len</span> <span class="o">=</span> <span class="n">sizeof</span><span class="p">(</span><span class="n">TEXT2</span><span class="p">)</span><span class="o">-</span><span class="mi">1</span><span class="p">;</span>

    <span class="n">c</span> <span class="o">=</span> <span class="n">crc32_iovec</span><span class="p">(</span><span class="n">iov</span><span class="p">,</span> <span class="mi">2</span><span class="p">);</span>  <span class="p">(</span><span class="n">e</span><span class="p">)</span>
    <span class="n">CU_ASSERT_EQUAL</span><span class="p">(</span><span class="n">c</span><span class="p">,</span> <span class="n">CRC32</span><span class="p">);</span>  <span class="p">(</span><span class="n">f</span><span class="p">)</span>
<span class="p">}</span>
</pre></div>
</div>
<p>Here's an explanation of what all these bits are for.</p>
<ol class="loweralpha simple">
<li><p>Your new test function should look like this: it takes no arguments,
returns void, and is named <code class="docutils literal notranslate"><span class="pre">test_whatever</span></code>. It may be <code class="docutils literal notranslate"><span class="pre">static</span></code>
or <code class="docutils literal notranslate"><span class="pre">extern</span></code>, but I recommend <code class="docutils literal notranslate"><span class="pre">static</span></code>. Functions with this
signature are automatically discovered in the source code by the
Cyrus unit test infrastructure, so all you have to do is write the
function. Later, a CUnit test named &quot;whatever&quot; will be created
automatically for your <code class="docutils literal notranslate"><span class="pre">test_whatever</span></code> function. Note that the
opening curly brace must be on the next line or the unit test
infrastructure will not find the function.</p></li>
<li><p>Here's a good place to define the test inputs and expected outputs.</p></li>
<li><p>Here's a good place for local variables you need during the test.</p></li>
<li><p>Here you set up the input conditions for the function under test.</p></li>
<li><p>Call the function under test with your known inputs, and capture the
results in a local variable, here <code class="docutils literal notranslate"><span class="pre">c</span></code>.</p></li>
<li><p>Compare the actual result in <code class="docutils literal notranslate"><span class="pre">c</span></code> with the expected result in
<code class="docutils literal notranslate"><span class="pre">CRC32</span></code>. The <code class="docutils literal notranslate"><span class="pre">CU_ASSERT_EQUAL()</span></code> macro checks that it's two
arguments are equal (using an integer comparison), and if they're
different it prints a message and records a failure. Note that
unlike the libc <code class="docutils literal notranslate"><span class="pre">assert()</span></code> macro, control will continue even if
the assert fails. The CUnit library provides a whole family of
similar macros, see <a class="reference external" href="http://cunit.sourceforge.net/doc/writing_tests.html#assertions">the online CUnit
documentation</a>
for more details.</p></li>
</ol>
<p>Now run <code class="docutils literal notranslate"><span class="pre">make</span> <span class="pre">check</span></code> and you'll see your test being built and run.</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">me</span><span class="nd">@mybox</span><span class="o">&gt;</span> <span class="n">make</span> <span class="n">check</span>
<span class="o">...</span>
<span class="o">../</span><span class="n">cunit</span><span class="o">/</span><span class="n">cunit</span><span class="o">.</span><span class="n">pl</span> <span class="p">[</span><span class="o">...</span><span class="p">]</span> <span class="o">--</span><span class="n">generate</span><span class="o">-</span><span class="n">wrapper</span> <span class="n">crc32</span><span class="o">.</span><span class="n">testc</span>
<span class="n">gcc</span> <span class="o">-</span><span class="n">c</span> <span class="p">[</span><span class="o">...</span><span class="p">]</span> <span class="o">-</span><span class="n">g</span> <span class="o">-</span><span class="n">O2</span> <span class="o">.</span><span class="n">cunit</span><span class="o">-</span><span class="n">crc32</span><span class="o">.</span><span class="n">c</span>
<span class="n">gcc</span> <span class="p">[</span><span class="o">...</span><span class="p">]</span> <span class="o">-</span><span class="n">o</span> <span class="n">unit</span> <span class="p">[</span><span class="o">...</span><span class="p">]</span> <span class="o">.</span><span class="n">cunit</span><span class="o">-</span><span class="n">crc32</span><span class="o">.</span><span class="n">o</span>
<span class="o">...</span>
<span class="n">Running</span> <span class="n">unit</span> <span class="n">tests</span>


     <span class="n">CUnit</span> <span class="o">-</span> <span class="n">A</span> <span class="n">Unit</span> <span class="n">testing</span> <span class="n">framework</span> <span class="k">for</span> <span class="n">C</span> <span class="o">-</span> <span class="n">Version</span> <span class="mf">2.1</span><span class="o">-</span><span class="mi">0</span>
     <span class="n">http</span><span class="p">:</span><span class="o">//</span><span class="n">cunit</span><span class="o">.</span><span class="n">sourceforge</span><span class="o">.</span><span class="n">net</span><span class="o">/</span>

<span class="o">...</span>
<span class="n">Suite</span><span class="p">:</span> <span class="n">crc32</span>
  <span class="n">Test</span><span class="p">:</span> <span class="nb">map</span> <span class="o">...</span> <span class="n">passed</span>
  <span class="n">Test</span><span class="p">:</span> <span class="n">iovec</span> <span class="o">...</span> <span class="n">passed</span>
</pre></div>
</div>
</section>
<section id="suite-setup-and-teardown">
<h2>4.3 Suite Setup And Teardown<a class="headerlink" href="#suite-setup-and-teardown" title="Permalink to this heading"></a></h2>
<p>Sometimes the behaviour of the functions under test depend on external
influences such as environment variables, global variables, or the
presence of certain files.</p>
<p>These kinds of functions need special treatment to ensure that their
behaviour is locked down during the running of your tests. Otherwise,
all sorts of strange behaviour may confuse the results of the tests. For
example, a test might succeed the first time it's run in a given
directory and fail the next time. Or a test might succeed when run by
the author of the test but fail when run by another user.</p>
<p>CUnit provides a special arrangement which helps you in such cases: the
suite initialisation and cleanup functions. These are two functions that
you write and which live in the suite source. They are called from CUnit
respectively before any of the tests in the suite is run, and after all
tests from that suite are run.</p>
<p>Here's how to use them. The suite setup function should set up any
global state that the functions under test rely on, in such a way that
their state is predictable and always the same no matter who runs the
test or when or how many times. Similarly the suite teardown function
should clean up any state which might possibly interfere with other test
suites. Note that some suites will need a setup function but not
necessarily a teardown function.</p>
<p>Adding these functions is very easy: you just write functions of the
appropriate signature (names, arguments and return type) and the Cyrus
unit test infrastructure will automatically discover them and arrange
for them to be called. The functions should look like (actual example
taken from <code class="docutils literal notranslate"><span class="pre">cunit/mboxname.testc</span></code>) this:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">static</span> <span class="n">enum</span> <span class="n">enum_value</span> <span class="n">old_config_virtdomains</span><span class="p">;</span>

<span class="n">static</span> <span class="nb">int</span> <span class="n">set_up</span><span class="p">(</span><span class="n">void</span><span class="p">)</span>
<span class="p">{</span>
    <span class="n">old_config_virtdomains</span> <span class="o">=</span> <span class="n">config_virtdomains</span><span class="p">;</span>
    <span class="n">config_virtdomains</span> <span class="o">=</span> <span class="n">IMAP_ENUM_VIRTDOMAINS_ON</span><span class="p">;</span>
    <span class="k">return</span> <span class="mi">0</span><span class="p">;</span>
<span class="p">}</span>

<span class="n">static</span> <span class="nb">int</span> <span class="n">tear_down</span><span class="p">(</span><span class="n">void</span><span class="p">)</span>
<span class="p">{</span>
    <span class="n">config_virtdomains</span> <span class="o">=</span> <span class="n">old_config_virtdomains</span><span class="p">;</span>
    <span class="k">return</span> <span class="mi">0</span><span class="p">;</span>
<span class="p">}</span>
</pre></div>
</div>
<p>The functions should return 0 on success, and non-zero on error. They
must not call and <code class="docutils literal notranslate"><span class="pre">CU_*</span></code> functions or macros.</p>
<p>Good luck and good testing!</p>
</section>
</section>


           </div>
          </div>
          <footer><div class="rst-footer-buttons" role="navigation" aria-label="Footer">
        <a href="guidance/var_directory_structure.html" class="btn btn-neutral float-left" title="Cyrus IMAP Server: var directory structure" accesskey="p" rel="prev"><span class="fa fa-arrow-circle-left" aria-hidden="true"></span> Previous</a>
        <a href="../../support.html" class="btn btn-neutral float-right" title="Support/Community" accesskey="n" rel="next">Next <span class="fa fa-arrow-circle-right" aria-hidden="true"></span></a>
    </div>

  <hr/>

  <div role="contentinfo">
    <p>&#169; Copyright 1993–2025, The Cyrus Team.</p>
  </div>

  Built with <a href="https://www.sphinx-doc.org/">Sphinx</a> using a
    <a href="https://github.com/readthedocs/sphinx_rtd_theme">theme</a>
    provided by <a href="https://readthedocs.org">Read the Docs</a>.
   

</footer>
        </div>
      </div>
    </section>
  </div>
  <script>
      jQuery(function () {
          SphinxRtdTheme.Navigation.enable(true);
      });
  </script>
 



</body>
</html>