-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathtest_utf8_limit_str_size.cpp
More file actions
242 lines (183 loc) · 6.89 KB
/
test_utf8_limit_str_size.cpp
File metadata and controls
242 lines (183 loc) · 6.89 KB
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
/* SpecUtils: a library to parse, save, and manipulate gamma spectrum data files.
Copyright 2018 National Technology & Engineering Solutions of Sandia, LLC
(NTESS). Under the terms of Contract DE-NA0003525 with NTESS, the U.S.
Government retains certain rights in this software.
For questions contact William Johnson via email at wcjohns@sandia.gov, or
alternative emails of interspec@sandia.gov.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library 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
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include <string>
#include <ostream>
#include <vector>
#include <fstream>
#include <cmath>
#include <climits>
#include <iostream>
#define DOCTEST_CONFIG_IMPLEMENT_WITH_MAIN
#include "doctest.h"
#include "SpecUtils/StringAlgo.h"
using namespace std;
TEST_CASE( "Test utf8 limit str size" )
{
string teststr = "";
SpecUtils::utf8_limit_str_size( teststr, 0 );
CHECK_EQ( teststr, "" );
teststr = "";
SpecUtils::utf8_limit_str_size( teststr, 1 );
CHECK_EQ( teststr, "" );
teststr = "";
SpecUtils::utf8_limit_str_size( teststr, 5 );
CHECK_EQ( teststr, "" );
teststr = "AAAA";
SpecUtils::utf8_limit_str_size( teststr, 0 );
CHECK_EQ( teststr, "" );
teststr = "AAAA";
SpecUtils::utf8_limit_str_size( teststr, 1 );
CHECK_EQ( teststr, "A" );
teststr = "AAAA";
SpecUtils::utf8_limit_str_size( teststr, 4 );
CHECK_EQ( teststr, "AAAA" );
teststr = "AAA";
SpecUtils::utf8_limit_str_size( teststr, 3 );
CHECK_EQ( teststr, "AAA" );
teststr = "AAA";
SpecUtils::utf8_limit_str_size( teststr, 4 );
CHECK_EQ( teststr, "AAA" );
teststr = u8"ⓧ"; //E2 93 A7
SpecUtils::utf8_limit_str_size( teststr, 0 );
CHECK_EQ( teststr, "" );
teststr = u8"ⓧ";
SpecUtils::utf8_limit_str_size( teststr, 1 );
CHECK_EQ( teststr, "" );
teststr = u8"ⓧ";
SpecUtils::utf8_limit_str_size( teststr, 2 );
CHECK_EQ( teststr, "" );
teststr = u8"ⓧ";
SpecUtils::utf8_limit_str_size( teststr, 3 );
CHECK_EQ( teststr, u8"ⓧ" );
teststr = u8"aⓧ";
SpecUtils::utf8_limit_str_size( teststr, 1 );
CHECK_EQ( teststr, "a" );
teststr = u8"ⓧⓧ";
SpecUtils::utf8_limit_str_size( teststr, 3 );
CHECK_EQ( teststr, u8"ⓧ" );
teststr = u8"ⓧⓧ";
SpecUtils::utf8_limit_str_size( teststr, 6 );
CHECK_EQ( teststr, u8"ⓧⓧ" );
teststr = u8"ⓧⓧaaa";
SpecUtils::utf8_limit_str_size( teststr, 6 );
CHECK_EQ( teststr, u8"ⓧⓧ" );
teststr = u8"ⓧⓧaaa";
SpecUtils::utf8_limit_str_size( teststr, 7 );
CHECK_EQ( teststr, u8"ⓧⓧa" );
teststr = u8"aaⓧⓧaaa";
SpecUtils::utf8_limit_str_size( teststr, 5 );
CHECK_EQ( teststr, u8"aaⓧ" );
teststr = u8"aaⓧⓧaaa";
SpecUtils::utf8_limit_str_size( teststr, 6 );
CHECK_EQ( teststr, u8"aaⓧ" );
teststr = u8"aaⓧⓧaaa";
SpecUtils::utf8_limit_str_size( teststr, 7 );
CHECK_EQ( teststr, u8"aaⓧ" );
teststr = u8"aõ";
SpecUtils::utf8_limit_str_size( teststr, 3 );
CHECK_EQ( teststr, u8"aõ" );
teststr = u8"õa";
SpecUtils::utf8_limit_str_size( teststr, 3 );
CHECK_EQ( teststr, u8"õa" );
teststr = u8"õ";
SpecUtils::utf8_limit_str_size( teststr, 3 );
CHECK_EQ( teststr, u8"õ" );
teststr = u8"õ";
SpecUtils::utf8_limit_str_size( teststr, 2 );
CHECK_EQ( teststr, u8"õ" );
teststr = u8"õ";
SpecUtils::utf8_limit_str_size( teststr, 1 );
CHECK_EQ( teststr, u8"" );
teststr = u8"÷õ";
SpecUtils::utf8_limit_str_size( teststr, 2 );
CHECK_EQ( teststr, u8"÷" );
teststr = u8"÷õ";
SpecUtils::utf8_limit_str_size( teststr, 3 );
CHECK_EQ( teststr, u8"÷" );
teststr = u8"÷õ";
SpecUtils::utf8_limit_str_size( teststr, 4 );
CHECK_EQ( teststr, u8"÷õ" );
teststr = u8"÷õ";
SpecUtils::utf8_limit_str_size( teststr, 5 );
CHECK_EQ( teststr, u8"÷õ" );
teststr = u8"÷aõ";
SpecUtils::utf8_limit_str_size( teststr, 5 );
CHECK_EQ( teststr, u8"÷aõ" );
teststr = u8"÷aõa";
SpecUtils::utf8_limit_str_size( teststr, 5 );
CHECK_EQ( teststr, u8"÷aõ" );
teststr = u8"÷aõa";
SpecUtils::utf8_limit_str_size( teststr, 3 );
CHECK_EQ( teststr, u8"÷a" );
teststr = u8"÷aõa";
SpecUtils::utf8_limit_str_size( teststr, 2 );
CHECK_EQ( teststr, u8"÷" );
teststr = u8"÷aõa";
SpecUtils::utf8_limit_str_size( teststr, 1 );
CHECK_EQ( teststr, u8"" );
teststr = u8"a÷aõa";
SpecUtils::utf8_limit_str_size( teststr, 1 );
CHECK_EQ( teststr, u8"a" );
/*
//Below is for testing that lines read in from two files match after limiting their
// size to 45 characters. But not currently being used.
string indir;
const int argc = framework::master_test_suite().argc;
for( int i = 1; i < argc-1; ++i )
{
if( framework::master_test_suite().argv[i] == string("--indir") )
indir = framework::master_test_suite().argv[i+1];
}
string test_in_file, test_out_file;
const string potential_input_paths[] = { ".", indir, "../../testing/", "../../../testing/" };
for( const string dir : potential_input_paths )
{
const string potential = SpecUtils::append_path( dir, "test_data/txt/utf8_limit_str_size_INPUT.txt" );
if( SpecUtils::is_file(potential) )
{
test_in_file = potential;
test_out_file = SpecUtils::append_path( dir, "test_data/txt/utf8_limit_str_size_OUTPUT.txt" );
}
}
if( test_in_file.empty() )
throw runtime_error( "Could not find 'utf8_limit_str_size_INPUT.txt'"
" - you may need to specify the '--indir' command line argument" );
ifstream input_check, output_check;
vector<string> input_vector,output_vector;
input_check.open( test_in_file.c_str(), ios::in | ios::binary );
if( !input_check.is_open() )
throw runtime_error( "Failed to open input file " + test_in_file );
output_check.open ( test_out_file.c_str(), ios::in | ios::binary );
if( !output_check.is_open() )
throw runtime_error( "Failed to open output file " + test_out_file );
string line;
while( SpecUtils::safe_get_line(input_check, line) )
{
SpecUtils::utf8_limit_str_size( line , 45 );
input_vector.push_back( line );
}
while( SpecUtils::safe_get_line(output_check, line) )
output_vector.push_back( line );
REQUIRE_EQ( input_vector.size(), output_vector.size() );
for( size_t i = 0; i < input_vector.size(); ++i )
{
CHECK_EQ( input_vector[i], output_vector[i] );
}
*/
}