Imported Upstream version 0.66.1
[hcoop/debian/courier-authlib.git] / libs / unicode / linebreaktest.C
CommitLineData
b0322a85
CE
1#include "unicode_config.h"
2#include "unicode.h"
3
4#include <iostream>
5#include <fstream>
6#include <sstream>
7#include <iomanip>
8#include <algorithm>
9#include <functional>
10#include <cstdlib>
11#include <list>
12#include <vector>
13
14static void testsuite()
15{
16 std::string buf;
17 int linenum=0;
18
19 std::ifstream fp("LineBreakTest.txt");
20
21 if (!fp.is_open())
22 exit(1);
23
24 while (1)
25 {
26 buf.clear();
27
28 if (std::getline(fp, buf).eof() && buf.empty())
29 break;
30
31 ++linenum;
32
33 buf.erase(std::find(buf.begin(), buf.end(), '#'), buf.end());
34
35 if (buf.empty())
36 continue;
37
38 std::list<std::string> words;
39
40 for (std::string::iterator b=buf.begin(), e=buf.end(); b != e;)
41 {
42 if (isspace(*b))
43 {
44 ++b;
45 continue;
46 }
47
48 std::string::iterator p=b;
49
50 while (b != e)
51 {
52 if (isspace(*b))
53 break;
54 ++b;
55 }
56
57 words.push_back(std::string(p, b));
58 }
59
60 std::vector<unicode_char> ubuf;
61 std::vector<int> status;
62
63 while (1)
64 {
65 if (!words.empty() && words.front().size() > 1)
66 {
67 int expected=UNICODE_LB_MANDATORY;
68
69 std::string s=words.front();
70
71 words.pop_front();
72
73 if ((unsigned char)s[0] ==
74 (unsigned char)0xc3)
75 switch ( (unsigned char)s[1] ) {
76 case (unsigned char)0x97:
77 expected=UNICODE_LB_NONE;
78 break;
79 case (unsigned char)0xb7:
80 expected=UNICODE_LB_ALLOWED;
81 break;
82 }
83
84 if (words.empty())
85 break;
86
87 status.push_back(expected);
88
89 std::istringstream i(words.front());
90
91 unicode_char uc;
92
93 i >> std::hex >> uc;
94
95 words.pop_front();
96
97 if (!i.fail())
98 {
99 ubuf.push_back(uc);
100 continue;
101 }
102 }
103
104 std::cerr << "Parse error, line " << linenum
105 << ": " << buf << std::endl;
106 exit(1);
107 }
108
109 std::vector<int> computed_status;
110
111 typedef std::vector<unicode_char>::const_iterator ubuf_iter;
112 typedef mail::linebreak_iter<ubuf_iter> lb_iter;
113
114 std::copy(lb_iter(ubuf.begin(), ubuf.end()), lb_iter(),
115 std::back_insert_iterator<std::vector<int> >
116 (computed_status));
117
118 std::replace(computed_status.begin(),
119 computed_status.end(),
120 UNICODE_LB_MANDATORY,
121 UNICODE_LB_ALLOWED);
122
123 if (computed_status != status)
124 {
125 std::cerr << "Regression, line " << linenum
126 << ": " << buf << std::endl;
127 exit(1);
128 }
129 }
130}
131
132static void testlinebreakc()
133{
134 static unicode_char str[]={'$', '(', 0x0300, 0x0301, 0x0302, 0x0303,
135 0x0304, 0x0305, 0x0306, 0x0307, '1', '.',
136 '2', ' ', 'A'};
137
138 typedef std::vector<std::pair<int, unicode_char> > linebreakvec_t;
139
140 linebreakvec_t linebreakvec;
141
142 std::copy(mail::linebreakc_iter<unicode_char *>(str,
143 str + sizeof(str)
144 /sizeof(str[0])),
145 mail::linebreakc_iter<unicode_char *>(),
146 std::back_insert_iterator<linebreakvec_t>
147 (linebreakvec));
148
149 if (linebreakvec.size() == sizeof(str)/sizeof(str[0]))
150 {
151 size_t i;
152
153 for (i=0; i<sizeof(str)/sizeof(str[0]); ++i)
154 {
155 if (str[i] != linebreakvec[i].second ||
156 linebreakvec[i].first !=
157 (i < sizeof(str)/sizeof(str[0])-1
158 ? UNICODE_LB_NONE:UNICODE_LB_ALLOWED))
159 break;
160 }
161
162 if (i == sizeof(str)/sizeof(str[0]))
163 return;
164 }
165 std::cerr << "Line break test 1 failed" << std::endl;
166}
167
168int main(int argc, char **argv)
169{
170 testsuite();
171 testlinebreakc();
172
173 std::string convteststr="0000000000000000000000000000000\xe3\x82\xa2";
174
175 std::vector<unicode_char> uc;
176
177 mail::iconvert::tou
178 ::convert(convteststr, "utf-8", uc);
179
180 std::vector<unicode_char>::iterator e(uc.end()),
181 b(std::find_if(uc.begin(), e,
182 std::not1(std::bind2nd(std::equal_to<unicode_char>
183 (),
184 unicode_char('0')))));
185
186 if (b == e || *b++ != 0x30A2 || b != e)
187 {
188 std::cerr << "mail::iconvert::tou::convert failed"
189 << std::endl;
190 exit(1);
191 }
192
193 if (mail::iconvert::fromu::convert(uc, "utf-8") != convteststr)
194 {
195 std::cerr << "mail::iconvert::fromu::convert failed"
196 << std::endl;
197 exit(1);
198 }
199
200 return 0;
201}