File: strutils_test.cpp

package info (click to toggle)
adapterremoval 2.3.4-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 8,864 kB
  • sloc: cpp: 24,110; python: 427; makefile: 207; sh: 16
file content (195 lines) | stat: -rw-r--r-- 7,921 bytes parent folder | download | duplicates (4)
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
/*************************************************************************\
 * AdapterRemoval - cleaning next-generation sequencing reads            *
 *                                                                       *
 * Copyright (C) 2011 by Stinus Lindgreen - stinus@binf.ku.dk            *
 * Copyright (C) 2014 by Mikkel Schubert - mikkelsch@gmail.com           *
 *                                                                       *
 * If you use the program, please cite the paper:                        *
 * S. Lindgreen (2012): AdapterRemoval: Easy Cleaning of Next Generation *
 * Sequencing Reads, BMC Research Notes, 5:337                           *
 * http://www.biomedcentral.com/1756-0500/5/337/                         *
 *                                                                       *
 * This program is free software: you can redistribute it and/or modify  *
 * it under the terms of the GNU General Public License as published by  *
 * the Free Software Foundation, either version 3 of the License, or     *
 * (at your option) any later version.                                   *
 *                                                                       *
 * This program is distributed in the hope that it will be useful,       *
 * but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 * GNU General Public License for more details.                          *
 *                                                                       *
 * You should have received a copy of the GNU General Public License     *
 * along with this program.  If not, see <http://www.gnu.org/licenses/>. *
\*************************************************************************/
#include <limits>
#include <stdexcept>

#include "testing.hpp"
#include "strutils.hpp"

namespace ar
{

///////////////////////////////////////////////////////////////////////////////
// Tests for 'toupper'

TEST_CASE("ASCII letters are uppercased", "[strutils::toupper]")
{
    REQUIRE(toupper("") == "");
    REQUIRE(toupper("a1{2BZ`zAdeK") == "A1{2BZ`ZADEK");
}


///////////////////////////////////////////////////////////////////////////////
// Tests for 'indent_lines'

TEST_CASE("Empty lines are not indented", "[strutils::indent_lines]")
{
    REQUIRE(indent_lines("") == "");
    REQUIRE(indent_lines("\n") == "\n");
    REQUIRE(indent_lines("\n\n") == "\n\n");
    REQUIRE(indent_lines("\n\n\n") == "\n\n\n");
}


TEST_CASE("Singular lines are indented", "[strutils::indent_lines]")
{
    REQUIRE(indent_lines("this is a test") == "    this is a test");
    REQUIRE(indent_lines("this is a test\n") == "    this is a test\n");
}


TEST_CASE("Multiple lines are indented", "[strutils::indent_lines]")
{
    REQUIRE(indent_lines("this is a test\nline #2")
            == "    this is a test\n    line #2");
    REQUIRE(indent_lines("this is a test\nline #2\n")
            == "    this is a test\n    line #2\n");
}


TEST_CASE("Mixed lines are partially indented", "[strutils::indent_lines]")
{
    REQUIRE(indent_lines("this is a test\nline #2\n\n")
            == "    this is a test\n    line #2\n\n");
    REQUIRE(indent_lines("this is a test\n\nline #2\n")
            == "    this is a test\n\n    line #2\n");
    REQUIRE(indent_lines("this is a test\nline #2\n\n\n")
            == "    this is a test\n    line #2\n\n\n");
}


///////////////////////////////////////////////////////////////////////////////
// Tests for 'columnize_text'

TEST_CASE("Whitespace only is stripped", "[strutils::columnize_text]")
{
    REQUIRE(columnize_text("") == "");
    REQUIRE(columnize_text(" ") == "");
    REQUIRE(columnize_text("\n") == "");
    REQUIRE(columnize_text("\n\n") == "");
    REQUIRE(columnize_text("\n \n") == "");
    REQUIRE(columnize_text("\n ") == "");
    REQUIRE(columnize_text(" \n") == "");
    REQUIRE(columnize_text(" \n ") == "");
}


TEST_CASE("Whitespace between words is stripped", "[strutils::columnize_text]")
{
    REQUIRE(columnize_text("foo bar") == "foo bar");
    REQUIRE(columnize_text("foo  bar") == "foo bar");
    REQUIRE(columnize_text("foo\nbar") == "foo bar");
    REQUIRE(columnize_text("foo\nbar\n") == "foo bar");
    REQUIRE(columnize_text("\nfoo\nbar") == "foo bar");
    REQUIRE(columnize_text("foo\nbar\n\n") == "foo bar");
    REQUIRE(columnize_text("\nfoo\nbar\n") == "foo bar");
    REQUIRE(columnize_text("\n \n foo \n bar \n") == "foo bar");
}


TEST_CASE("Linebreaks are added by max width", "[strutils::columnize_text]")
{
    REQUIRE(columnize_text("foo bar\nzood", 12) == "foo bar zood");
    REQUIRE(columnize_text("foo bar\nzood", 11) == "foo bar\nzood");
    REQUIRE(columnize_text("foo bar\nzood", 7) == "foo bar\nzood");
    REQUIRE(columnize_text("foo bar\nzood", 6) == "foo\nbar\nzood");
    REQUIRE(columnize_text("foo bar\nzood", 3) == "foo\nbar\nzood");
    REQUIRE(columnize_text("foo bar\nzood", 1) == "foo\nbar\nzood");
    REQUIRE(columnize_text("foo bar\nzood", 0) == "foo\nbar\nzood");
}


TEST_CASE("Subsequent lines are indented", "[strutils::columnize_text]")
{
    REQUIRE(columnize_text("foo bar\nzood", 12, 2) == "foo bar zood");
    REQUIRE(columnize_text("foo bar\nzood", 11, 0) == "foo bar\nzood");
    REQUIRE(columnize_text("foo bar\nzood", 11, 1) == "foo bar\n zood");
    REQUIRE(columnize_text("foo bar\nzood", 11, 2) == "foo bar\n  zood");
    REQUIRE(columnize_text("foo bar\nzood", 7, 2) == "foo bar\n  zood");
    REQUIRE(columnize_text("foo bar\nzood", 6, 2) == "foo\n  bar\n  zood");
    REQUIRE(columnize_text("foo bar\nzood", 3, 2) == "foo\n  bar\n  zood");
    REQUIRE(columnize_text("foo bar\nzood", 1, 2) == "foo\n  bar\n  zood");
    REQUIRE(columnize_text("foo bar\nzood", 0, 2) == "foo\n  bar\n  zood");
}


TEST_CASE("Proper numbers are converted", "[strutils::str_to_unsigned]")
{
    REQUIRE(str_to_unsigned("0") == 0);
    REQUIRE(str_to_unsigned("1") == 1);
    REQUIRE(str_to_unsigned("2") == 2);
    REQUIRE(str_to_unsigned("2147483647") == 2147483647);
    REQUIRE(str_to_unsigned("2147483648") == 2147483648);
    REQUIRE(str_to_unsigned("2147483649") == 2147483649);
    REQUIRE(str_to_unsigned("4294967293") == 4294967293);
    REQUIRE(str_to_unsigned("4294967294") == 4294967294);
    REQUIRE(str_to_unsigned("4294967295") == 4294967295);
}


TEST_CASE("Whitespace is ignored", "[strutils::str_to_unsigned]")
{
    REQUIRE(str_to_unsigned(" 123") == 123);
    REQUIRE(str_to_unsigned("123 ") == 123);
    REQUIRE(str_to_unsigned(" 123 ") == 123);
    REQUIRE(str_to_unsigned("\n123\n") == 123);
}


TEST_CASE("Numbers outside range are rejected", "[strutils::str_to_unsigned]")
{
    REQUIRE_THROWS_AS(str_to_unsigned("-2"), std::invalid_argument);
    REQUIRE_THROWS_AS(str_to_unsigned("-1"), std::invalid_argument);
    REQUIRE_THROWS_AS(str_to_unsigned("4294967296"), std::invalid_argument);
    REQUIRE_THROWS_AS(str_to_unsigned("4294967297"), std::invalid_argument);
}


TEST_CASE("Non numeric strings are rejected", "[strutils::str_to_unsigned]")
{
    REQUIRE_THROWS_AS(str_to_unsigned(" "), std::invalid_argument);
    REQUIRE_THROWS_AS(str_to_unsigned("x"), std::invalid_argument);
    REQUIRE_THROWS_AS(str_to_unsigned("a b c"), std::invalid_argument);
    REQUIRE_THROWS_AS(str_to_unsigned("\n"), std::invalid_argument);
}


TEST_CASE("Mixed strings are rejected", "[strutils::str_to_unsigned]")
{
    REQUIRE_THROWS_AS(str_to_unsigned("1a"), std::invalid_argument);
    REQUIRE_THROWS_AS(str_to_unsigned("123x"), std::invalid_argument);
    REQUIRE_THROWS_AS(str_to_unsigned("123 x"), std::invalid_argument);
    REQUIRE_THROWS_AS(str_to_unsigned("x123"), std::invalid_argument);
    REQUIRE_THROWS_AS(str_to_unsigned("x 123"), std::invalid_argument);
}


TEST_CASE("Base 10 is assumed", "[strutils::str_to_unsigned]")
{
    REQUIRE(str_to_unsigned("010") == 10);
}


} // namespace ar