File: debug_helper.h

package info (click to toggle)
seqan2 2.4.0%2Bdfsg-11
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 223,504 kB
  • sloc: cpp: 256,886; ansic: 91,672; python: 8,339; sh: 995; xml: 570; makefile: 251; awk: 51
file content (243 lines) | stat: -rw-r--r-- 7,144 bytes parent folder | download | duplicates (6)
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
// ==========================================================================
//                 SeqAn - The Library for Sequence Analysis
// ==========================================================================
// Copyright (c) 2006-2018, Knut Reinert, FU Berlin
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
//
//     * Redistributions of source code must retain the above copyright
//       notice, this list of conditions and the following disclaimer.
//     * Redistributions in binary form must reproduce the above copyright
//       notice, this list of conditions and the following disclaimer in the
//       documentation and/or other materials provided with the distribution.
//     * Neither the name of Knut Reinert or the FU Berlin nor the names of
//       its contributors may be used to endorse or promote products derived
//       from this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
// ARE DISCLAIMED. IN NO EVENT SHALL KNUT REINERT OR THE FU BERLIN BE LIABLE
// FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
// LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
// OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
// DAMAGE.
//
// ==========================================================================
// Author: Andreas Gogol-Döring <andreas.doering@mdc-berlin.de>
// ==========================================================================
// Helper code for debugging and testing.
// ==========================================================================

#ifndef SEQAN_INCLUDE_SEQAN_BASIC_DEBUG_HELPER_H_
#define SEQAN_INCLUDE_SEQAN_BASIC_DEBUG_HELPER_H_

#include <cstdio>
#include <fstream>

namespace seqan {

// ============================================================================
// Forwards
// ============================================================================

// ============================================================================
// Tags, Classes, Enums
// ============================================================================

// ============================================================================
// Metafunctions
// ============================================================================

// ============================================================================
// Functions
// ============================================================================

// TODO(holtgrew): Document, make public.

// compare two files, do not translate linebreaks
inline bool
_compareBinaryFiles(const char * file1, const char * file2)
{
//IOREV see above
    bool ret = false;

    FILE * fl1 = fopen(file1, "rb");
    if (!fl1) return ret;

    FILE * fl2 = fopen(file2, "rb");
    if (!fl2)
    {
        fclose(fl1);

        return ret;
    }

    while (!feof(fl1) && !feof(fl2))
    {
        if (fgetc(fl1) != fgetc(fl2)) goto End;
    }

    ret = feof(fl1) && feof(fl2);

End:
    fclose(fl2);
    fclose(fl1);

    return ret;

}
//____________________________________________________________________________

//one line break is either \r, \n, or \r\n.
//a single line break is skipped.
//the second line break is transformed into \n
inline void
_compareTextFilesReadChar(FILE * fl, char & c, int & num_lb, bool & is_eof)
{
//IOREV see above
    num_lb = 0;
    is_eof = false;

    c = fgetc(fl);
    while ((c == '\r') || (c == '\n'))
    {
        ++num_lb;
        if (c == '\r')
        {
            c = fgetc(fl);
            if (feof(fl)) is_eof = true;
            else
            {
                if (c == '\n')
                {
                    c = fgetc(fl);
                    if (feof(fl)) is_eof = true;
                }
            }
        }
        else if (c == '\n')
        {
            c = fgetc(fl);
            if (feof(fl)) is_eof = true;
        }
    }
}

// compare two files, translate linebreaks
inline bool
_compareTextFiles(const char * file1, const char * file2)
{
//IOREV see above
    FILE * fl1 = fopen(file1, "rb");
    if (!fl1) return false;

    FILE * fl2 = fopen(file2, "rb");
    if (!fl2)
    {
        fclose(fl1);
        return false;
    }

    bool ret = false;

    int num_lb1, num_lb2;
    bool is_eof1, is_eof2;
    char c1, c2;

    while (!feof(fl1) && !feof(fl2))
    {
        _compareTextFilesReadChar(fl1, c1, num_lb1, is_eof1);
        _compareTextFilesReadChar(fl2, c2, num_lb2, is_eof2);

        if (num_lb1 != num_lb2)
        {
            goto End;
        }
        if (is_eof1 ^ is_eof2)
        {
            goto End;
        }
        if (c1 != c2)
        {
            goto End;
        }
    }

    ret = feof(fl1) && feof(fl2);

End:
    fclose(fl2);
    fclose(fl1);

    return ret;

}


// compare two files, translate linebreaks
// more helpful output in case of a difference
inline bool
_compareTextFilesAlt(const char * file1, const char * file2)
{
    std::ifstream fl1(file1);
    std::ifstream fl2(file2);

    if (!fl1.good())
    {
        std::cerr << "Couldn't open file \"" << file1 << "\"" << std::endl;
        return false;
    }
    if (!fl2.good())
    {
        std::cerr << "Couldn't open file \"" << file2 << "\"" << std::endl;
        return false;
    }

    std::string line1;
    std::string line2;

    for (uint64_t lineNo = 1; !fl1.eof() && !fl2.eof(); ++lineNo)
    {
        getline(fl1, line1);
        getline(fl2, line2);

        // Normalize line endings.
        if (line1.size() > 2u && line1[line1.size() - 2] == '\r' && line1[line1.size() - 2] == '\n')
        {
            line1[line1.size() - 2] = '\n';
            line1.erase(line1.size() - 1);
        }
        if (line2.size() > 2u && line2[line2.size() - 2] == '\r' && line2[line2.size() - 2] == '\n')
        {
            line2[line2.size() - 2] = '\n';
            line2.erase(line2.size() - 1);
        }

        if (line1 != line2)
        {
            std::cerr << "The following files are different:" << std::endl;
            std::cerr << '\t' << file1 << std::endl;
            std::cerr << '\t' << file2 << std::endl;
            std::cerr << "Line " << lineNo << " of the text files differ:" << std::endl;
            std::cerr << line1 << std::endl;
            std::cerr << line2 << std::endl;
            return false;
        }
    }
    if (fl1.eof() != fl2.eof())
    {
        std::cerr << "File sizes differ" << std::endl;
        return false;
    }
    return true;
}

}  // namespace seqan

#endif  // #ifndef SEQAN_INCLUDE_SEQAN_BASIC_DEBUG_HELPER_H_