summaryrefslogtreecommitdiffstats
path: root/src/base/test/utf8.cpp
blob: 2e60f64e8907fadb64ddd9656648739d17a7676d (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
#include "XmlExportable.h"
#include <iostream>
#include <string>

using namespace Rosegarden;
using std::cout;
using std::cerr;
using std::endl;
using std::string;


string binary(unsigned char c)
{
    string s;
    for (int i = 0; i < 8; ++i) {
	s = ((c & 0x1) ? '1' : '0') + s;
	c >>= 1;
    }
    return s;
}


int main(int argc, char **argv)
{
    string valid[] = {
	"ニュース",
	"주요 뉴스",
	"Nyheter",
	"天气",
	"Notícias",
    };

    string escapable[] = {
	"ニュ&ース",
	"주요 <뉴스>",
	"\"Nyheter\"",
	"\'Notícias\'",
    };

    string invalid[] = {
	"����ース",
	"주� � 뉴스",
	"Nyhe\004ter",
	"�天气",
	"Not�cias",
    };

    cout << "Testing valid strings -- should be no errors here" << endl;

    for (int i = 0; i < sizeof(valid)/sizeof(valid[0]); ++i) {
	string encoded = XmlExportable::encode(valid[i]);
	if (encoded != valid[i]) {
	    cerr << "Encoding failed:" << endl;
	    for (int j = 0; j < valid[i].length(); ++j) {
		cerr << (char)valid[i][j] << " ("
		     << binary(valid[i][j]) << ")" << endl;
	    }
	    exit(1);
	}
    }

    cout << "Testing escapable strings -- should be no errors here" << endl;

    for (int i = 0; i < sizeof(escapable)/sizeof(escapable[0]); ++i) {
	string encoded = XmlExportable::encode(escapable[i]);
	if (encoded == escapable[i]) {
	    cerr << "Escaping failed:" << endl;
	    for (int j = 0; j < escapable[i].length(); ++j) {
		cerr << (char)escapable[i][j] << " ("
		     << binary(escapable[i][j]) << ")" << endl;
	    }
	    exit(1);
	}
    }

    cout << "Testing invalid strings -- should be "
	 << (sizeof(invalid)/sizeof(invalid[0]))
	 << " errors here (but no fatal ones)" << endl;

    for (int i = 0; i < sizeof(invalid)/sizeof(invalid[0]); ++i) {
	string encoded = XmlExportable::encode(invalid[i]);
	if (encoded == invalid[i]) {
	    cerr << "Encoding succeeded but should have failed:" << endl;
	    for (int j = 0; j < invalid[i].length(); ++j) {
		cerr << (char)invalid[i][j] << " ("
		     << binary(invalid[i][j]) << ")" << endl;
	    }
	    exit(1);
	}
    }

    exit(0);
}