You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
rosegarden/src/base/test/utf8.cpp

95 lines
2.0 KiB

This file contains invisible Unicode characters!

This file contains invisible Unicode characters that may be processed differently from what appears below. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to reveal hidden characters.

#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);
}