summaryrefslogtreecommitdiffstats
path: root/unicode/wordbreaktest.C
blob: 5df1e236f2edf49988e082292dc701a2b071e8f4 (plain)
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
#include	"unicode_config.h"
#include	"courier-unicode.h"

#include	<iostream>
#include	<fstream>
#include	<sstream>
#include	<iomanip>
#include	<algorithm>
#include	<functional>
#include	<cstdlib>
#include	<list>
#include	<vector>

class collect_wordbreakflags : public unicode::wordbreak_callback_base {

public:

	std::vector<bool> flags;

	template<typename iter_type> void operator()(iter_type b, iter_type e)
	{
		unicode::wordbreak_callback_base::operator()(b, e);
	}

	using unicode::wordbreak_callback_base::operator<<;

private:
	int callback(bool flag)
	{
		flags.push_back(flag);
		return 0;
	}
};

static void testsuite()
{
	std::string buf;
	int linenum=0;

	std::ifstream fp("WordBreakTest.txt");

	if (!fp.is_open())
		exit(1);

	while (1)
	{
		buf.clear();

		if (std::getline(fp, buf).eof() && buf.empty())
			break;

		++linenum;

		buf.erase(std::find(buf.begin(), buf.end(), '#'), buf.end());

		if (buf.empty())
			continue;

		std::list<std::string> words;

		for (std::string::iterator b=buf.begin(), e=buf.end(); b != e;)
		{
			if (isspace(*b))
			{
				++b;
				continue;
			}

			std::string::iterator p=b;

			while (b != e)
			{
				if (isspace(*b))
					break;
				++b;
			}

			words.push_back(std::string(p, b));
		}

		std::u32string ubuf;
		std::vector<bool> status;

		while (1)
		{
			if (!words.empty() && words.front().size() > 1)
			{
				bool flag=false;
				std::string s=words.front();

				words.pop_front();

				if ((unsigned char)s[0] ==
				    (unsigned char)0xc3 &&
				    (unsigned char)s[1] == (unsigned char)0xb7)
					flag=true;

				if (words.empty())
					break;

				status.push_back(flag);

				std::istringstream i(words.front());

				uint32_t uc;

				i >> std::hex >> uc;

				words.pop_front();

				if (!i.fail())
				{
					ubuf.push_back(uc);
					continue;
				}
			}

			std::cerr << "Parse error, line " << linenum
				  << ": " << buf << std::endl;
			exit(1);
		}

		//if (linenum != 986)
		//	continue;

		collect_wordbreakflags flags;

		flags(ubuf.begin(), ubuf.end());
		flags.finish();

		if (status != flags.flags)
		{
			std::cerr << "Regression, line " << linenum
				  << ": " << buf << std::endl;
			std::cerr << "Calculated:";

			for (auto f:flags.flags)
			{
				std::cerr << " "
					  << (f ? "÷":"×");
			}
			std::cerr << " ÷ " << std::endl;
			std::cerr << std::endl;
			exit(1);
		}
	}
}

int main(int argc, char **argv)
{
	testsuite();
	return 0;
}