~ubuntu-branches/ubuntu/quantal/genometools/quantal-backports

« back to all changes in this revision

Viewing changes to src/match/rdj-ensure-output.h

  • Committer: Package Import Robot
  • Author(s): Sascha Steinbiss
  • Date: 2012-07-09 14:10:23 UTC
  • Revision ID: package-import@ubuntu.com-20120709141023-juuu4spm6chqsf9o
Tags: upstream-1.4.1
ImportĀ upstreamĀ versionĀ 1.4.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
  Copyright (c) 2010-2011 Giorgio Gonnella <gonnella@zbh.uni-hamburg.de>
 
3
  Copyright (c) 2010-2011 Center for Bioinformatics, University of Hamburg
 
4
 
 
5
  Permission to use, copy, modify, and distribute this software for any
 
6
  purpose with or without fee is hereby granted, provided that the above
 
7
  copyright notice and this permission notice appear in all copies.
 
8
 
 
9
  THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 
10
  WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 
11
  MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 
12
  ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 
13
  WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 
14
  ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 
15
  OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 
16
*/
 
17
 
 
18
#ifndef RDJ_ENSURE_OUTPUT_H
 
19
#define RDJ_ENSURE_OUTPUT_H
 
20
 
 
21
#include <string.h>
 
22
#include "core/ensure.h"
 
23
#include "core/fa.h"
 
24
#include "core/xansi_api.h"
 
25
 
 
26
/* defines macros to make easier to test output to a temporary file */
 
27
 
 
28
#define GT_ENSURE_OUTPUT_DECLARE(MAXOUTSIZE)                               \
 
29
        GtStr *fnamestr;                                                   \
 
30
        char *fname;                                                       \
 
31
        FILE *handle;                                                      \
 
32
        GtFile *outfp;                                                     \
 
33
        size_t outsize;                                                    \
 
34
        char buffer[MAXOUTSIZE]
 
35
 
 
36
#define GT_ENSURE_OUTPUT_GETFILE(GTFILE)                                   \
 
37
        fnamestr = gt_str_new();                                           \
 
38
        handle = gt_xtmpfp(fnamestr);                                      \
 
39
        (GTFILE) = gt_file_new_from_fileptr(handle)
 
40
 
 
41
#define GT_ENSURE_OUTPUT_TESTFILE(GTFILE, EXPOUT)                          \
 
42
        (void)fflush(handle);                                              \
 
43
        gt_file_xrewind(GTFILE);                                           \
 
44
        outsize = (size_t)gt_file_xread((GTFILE), buffer, strlen(EXPOUT)); \
 
45
        gt_ensure(had_err, outsize == strlen(EXPOUT));                     \
 
46
        gt_ensure(had_err, memcmp((EXPOUT), buffer, outsize) == 0);        \
 
47
        if (had_err != 0)                                                  \
 
48
        {                                                                  \
 
49
          fprintf(stderr, "\nExpected file content: \n%s\n",               \
 
50
                  EXPOUT);                                                 \
 
51
          fprintf(stderr, "\nFile content actually read "                  \
 
52
                         "(up to %lu bytes):\n%s\n",                       \
 
53
               (unsigned long) strlen(EXPOUT), buffer);                    \
 
54
        }
 
55
 
 
56
#define GT_ENSURE_OUTPUT_RMFILE(GTFILE)                                    \
 
57
        gt_file_delete(GTFILE);                                            \
 
58
        fname = gt_str_get(fnamestr);                                      \
 
59
        gt_xremove(fname);                                                 \
 
60
        gt_str_delete(fnamestr)
 
61
 
 
62
#define GT_ENSURE_OUTPUT(FNCALL, EXPOUT)                                   \
 
63
        GT_ENSURE_OUTPUT_GETFILE(outfp);                                   \
 
64
        FNCALL;                                                            \
 
65
        GT_ENSURE_OUTPUT_TESTFILE(outfp, EXPOUT);                          \
 
66
        GT_ENSURE_OUTPUT_RMFILE(outfp)
 
67
 
 
68
/*
 
69
  Compares the output of the function call FNCALL
 
70
  to the char* EXPOUT (size and content).
 
71
 
 
72
  Usage: GT_ENSURE_OUTPUT_DECLARE in the declarations,
 
73
         then GT_ENSURE_OUTPUT to run the test (also multiple times);
 
74
         FNCALL should print something to the GtFile* outfp
 
75
         or to the FILE* handle
 
76
 
 
77
  Assumes:
 
78
  - int had_err is the return value variable of the unit test method
 
79
  - GtError *err (required by the gt_ensure macro)
 
80
  - reserved variables: fnamestr, fname, handle, outfp, outsize, buffer
 
81
*/
 
82
 
 
83
#endif