File: ParamedStringTest.cc

package info (click to toggle)
aria2 1.37.0%2Bdebian-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 15,128 kB
  • sloc: cpp: 115,006; ansic: 9,140; makefile: 1,466; ruby: 475; python: 373; sh: 260; xml: 176
file content (208 lines) | stat: -rw-r--r-- 7,122 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
#include "paramed_string.h"

#include <iostream>

#include <cppunit/extensions/HelperMacros.h>

namespace aria2 {

class ParamedStringTest : public CppUnit::TestFixture {

  CPPUNIT_TEST_SUITE(ParamedStringTest);
  CPPUNIT_TEST(testExpand);
  CPPUNIT_TEST_SUITE_END();

public:
  void testExpand();
};

CPPUNIT_TEST_SUITE_REGISTRATION(ParamedStringTest);

void ParamedStringTest::testExpand()
{
  std::vector<std::string> res;

  std::string s = "alpha:{01,02,03}:bravo:{001,002}";
  paramed_string::expand(s.begin(), s.end(), std::back_inserter(res));
  CPPUNIT_ASSERT_EQUAL((size_t)6, res.size());
  CPPUNIT_ASSERT_EQUAL(std::string("alpha:01:bravo:001"), res[0]);
  CPPUNIT_ASSERT_EQUAL(std::string("alpha:01:bravo:002"), res[1]);
  CPPUNIT_ASSERT_EQUAL(std::string("alpha:02:bravo:001"), res[2]);
  CPPUNIT_ASSERT_EQUAL(std::string("alpha:02:bravo:002"), res[3]);
  CPPUNIT_ASSERT_EQUAL(std::string("alpha:03:bravo:001"), res[4]);
  CPPUNIT_ASSERT_EQUAL(std::string("alpha:03:bravo:002"), res[5]);
  res.clear();

  s = "alpha:[1-3]:bravo";
  paramed_string::expand(s.begin(), s.end(), std::back_inserter(res));
  CPPUNIT_ASSERT_EQUAL((size_t)3, res.size());
  CPPUNIT_ASSERT_EQUAL(std::string("alpha:1:bravo"), res[0]);
  CPPUNIT_ASSERT_EQUAL(std::string("alpha:2:bravo"), res[1]);
  CPPUNIT_ASSERT_EQUAL(std::string("alpha:3:bravo"), res[2]);
  res.clear();

  s = "alpha:[5-12:3]:bravo";
  paramed_string::expand(s.begin(), s.end(), std::back_inserter(res));
  CPPUNIT_ASSERT_EQUAL((size_t)3, res.size());
  CPPUNIT_ASSERT_EQUAL(std::string("alpha:5:bravo"), res[0]);
  CPPUNIT_ASSERT_EQUAL(std::string("alpha:8:bravo"), res[1]);
  CPPUNIT_ASSERT_EQUAL(std::string("alpha:11:bravo"), res[2]);
  res.clear();

  s = "alpha:[05-12:3]:bravo";
  paramed_string::expand(s.begin(), s.end(), std::back_inserter(res));
  CPPUNIT_ASSERT_EQUAL((size_t)3, res.size());
  CPPUNIT_ASSERT_EQUAL(std::string("alpha:05:bravo"), res[0]);
  CPPUNIT_ASSERT_EQUAL(std::string("alpha:08:bravo"), res[1]);
  CPPUNIT_ASSERT_EQUAL(std::string("alpha:11:bravo"), res[2]);
  res.clear();

  s = "alpha:[99-00]:bravo";
  paramed_string::expand(s.begin(), s.end(), std::back_inserter(res));
  CPPUNIT_ASSERT_EQUAL((size_t)1, res.size());
  CPPUNIT_ASSERT_EQUAL(std::string("alpha::bravo"), res[0]);
  res.clear();

  s = "alpha:[65535-65535:65535]:bravo";
  paramed_string::expand(s.begin(), s.end(), std::back_inserter(res));
  CPPUNIT_ASSERT_EQUAL((size_t)1, res.size());
  CPPUNIT_ASSERT_EQUAL(std::string("alpha:65535:bravo"), res[0]);
  res.clear();

  // Invalid loop range
  s = "alpha:[1-]:bravo";
  try {
    paramed_string::expand(s.begin(), s.end(), std::back_inserter(res));
    CPPUNIT_FAIL("Exception must be thrown.");
  }
  catch (const Exception& e) {
  }

  // Invalid loop range
  s = "alpha:[-1]:bravo";
  try {
    paramed_string::expand(s.begin(), s.end(), std::back_inserter(res));
    CPPUNIT_FAIL("Exception must be thrown.");
  }
  catch (const Exception& e) {
  }

  // Invalid loop range
  s = "alpha:[1-3a]:bravo";
  try {
    paramed_string::expand(s.begin(), s.end(), std::back_inserter(res));
    CPPUNIT_FAIL("Exception must be thrown.");
  }
  catch (const Exception& e) {
  }

  // Missing loop step
  s = "alpha:[1-2:]:bravo";
  try {
    paramed_string::expand(s.begin(), s.end(), std::back_inserter(res));
    CPPUNIT_FAIL("Exception must be thrown.");
  }
  catch (const Exception& e) {
  }

  // Range overflow
  s = "alpha:[0-65536]:bravo";
  try {
    paramed_string::expand(s.begin(), s.end(), std::back_inserter(res));
    CPPUNIT_FAIL("Exception must be thrown.");
  }
  catch (const Exception& e) {
  }

  // Step overflow
  s = "alpha:[0-1:65536]:bravo";
  try {
    paramed_string::expand(s.begin(), s.end(), std::back_inserter(res));
    CPPUNIT_FAIL("Exception must be thrown.");
  }
  catch (const Exception& e) {
  }

  s = "alpha:[c-e]:bravo";
  paramed_string::expand(s.begin(), s.end(), std::back_inserter(res));
  CPPUNIT_ASSERT_EQUAL((size_t)3, res.size());
  CPPUNIT_ASSERT_EQUAL(std::string("alpha:c:bravo"), res[0]);
  CPPUNIT_ASSERT_EQUAL(std::string("alpha:d:bravo"), res[1]);
  CPPUNIT_ASSERT_EQUAL(std::string("alpha:e:bravo"), res[2]);
  res.clear();

  s = "alpha:[C-E]:bravo";
  paramed_string::expand(s.begin(), s.end(), std::back_inserter(res));
  CPPUNIT_ASSERT_EQUAL((size_t)3, res.size());
  CPPUNIT_ASSERT_EQUAL(std::string("alpha:C:bravo"), res[0]);
  CPPUNIT_ASSERT_EQUAL(std::string("alpha:D:bravo"), res[1]);
  CPPUNIT_ASSERT_EQUAL(std::string("alpha:E:bravo"), res[2]);
  res.clear();

  s = "alpha:[v-z:2]:bravo";
  paramed_string::expand(s.begin(), s.end(), std::back_inserter(res));
  CPPUNIT_ASSERT_EQUAL((size_t)3, res.size());
  CPPUNIT_ASSERT_EQUAL(std::string("alpha:v:bravo"), res[0]);
  CPPUNIT_ASSERT_EQUAL(std::string("alpha:x:bravo"), res[1]);
  CPPUNIT_ASSERT_EQUAL(std::string("alpha:z:bravo"), res[2]);
  res.clear();

  s = "alpha:[aa-ba]:bravo";
  paramed_string::expand(s.begin(), s.end(), std::back_inserter(res));
  CPPUNIT_ASSERT_EQUAL((size_t)27, res.size());
  CPPUNIT_ASSERT_EQUAL(std::string("alpha:aa:bravo"), res[0]);
  CPPUNIT_ASSERT_EQUAL(std::string("alpha:az:bravo"), res[25]);
  CPPUNIT_ASSERT_EQUAL(std::string("alpha:ba:bravo"), res[26]);
  res.clear();

  s = "alpha:[a-ba]:bravo";
  paramed_string::expand(s.begin(), s.end(), std::back_inserter(res));
  CPPUNIT_ASSERT_EQUAL((size_t)27, res.size());
  CPPUNIT_ASSERT_EQUAL(std::string("alpha:a:bravo"), res[0]);
  CPPUNIT_ASSERT_EQUAL(std::string("alpha:z:bravo"), res[25]);
  CPPUNIT_ASSERT_EQUAL(std::string("alpha:ba:bravo"), res[26]);
  res.clear();

  s = "alpha:[z-a]:bravo";
  paramed_string::expand(s.begin(), s.end(), std::back_inserter(res));
  CPPUNIT_ASSERT_EQUAL((size_t)1, res.size());
  CPPUNIT_ASSERT_EQUAL(std::string("alpha::bravo"), res[0]);
  res.clear();

  s = "alpha:[dsyo-dsyp]:bravo";
  paramed_string::expand(s.begin(), s.end(), std::back_inserter(res));
  CPPUNIT_ASSERT_EQUAL((size_t)2, res.size());
  CPPUNIT_ASSERT_EQUAL(std::string("alpha:dsyo:bravo"), res[0]);
  CPPUNIT_ASSERT_EQUAL(std::string("alpha:dsyp:bravo"), res[1]);
  res.clear();

  // Range overflow
  s = "alpha:[dsyo-dsyq]:bravo";
  try {
    paramed_string::expand(s.begin(), s.end(), std::back_inserter(res));
    CPPUNIT_FAIL("Exception must be thrown.");
  }
  catch (const Exception& e) {
  }

  // Invalid loop range
  s = "alpha:[a-Z]:bravo";
  try {
    paramed_string::expand(s.begin(), s.end(), std::back_inserter(res));
    CPPUNIT_FAIL("Exception must be thrown.");
  }
  catch (const Exception& e) {
  }

  // Combination of {} and []
  s = "http://{jp,us}.mirror/image_cd[000-001].iso";
  paramed_string::expand(s.begin(), s.end(), std::back_inserter(res));
  CPPUNIT_ASSERT_EQUAL((size_t)4, res.size());
  CPPUNIT_ASSERT_EQUAL(std::string("http://jp.mirror/image_cd000.iso"), res[0]);
  CPPUNIT_ASSERT_EQUAL(std::string("http://jp.mirror/image_cd001.iso"), res[1]);
  CPPUNIT_ASSERT_EQUAL(std::string("http://us.mirror/image_cd000.iso"), res[2]);
  CPPUNIT_ASSERT_EQUAL(std::string("http://us.mirror/image_cd001.iso"), res[3]);
  res.clear();
}

} // namespace aria2