Changeset View
Changeset View
Standalone View
Standalone View
src/autotests/CharacterWidthTest.cpp
Show All 13 Lines | 1 | /* | |||
---|---|---|---|---|---|
14 | You should have received a copy of the GNU General Public License | 14 | You should have received a copy of the GNU General Public License | ||
15 | along with this program; if not, write to the Free Software | 15 | along with this program; if not, write to the Free Software | ||
16 | Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA | 16 | Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA | ||
17 | 02110-1301 USA. | 17 | 02110-1301 USA. | ||
18 | */ | 18 | */ | ||
19 | 19 | | |||
20 | // Own | 20 | // Own | ||
21 | #include "CharacterWidthTest.h" | 21 | #include "CharacterWidthTest.h" | ||
22 | #include <Character.h> | ||||
23 | | ||||
24 | // System | ||||
25 | #include <clocale> | ||||
26 | | ||||
27 | // Qt | ||||
28 | #include <QByteArray> | ||||
22 | 29 | | |||
23 | // KDE | 30 | // KDE | ||
24 | #include <qtest.h> | 31 | #include <qtest.h> | ||
25 | 32 | | |||
26 | #include "../konsole_wcwidth.h" | | |||
27 | #include "konsoleprivate_export.h" | | |||
28 | | ||||
29 | using namespace Konsole; | 33 | using namespace Konsole; | ||
30 | 34 | | |||
35 | bool CharacterWidthTest::systemUsesUtf8() | ||||
36 | { | ||||
37 | const char *ctype = setlocale(LC_CTYPE, ""); | ||||
38 | if (ctype != nullptr) { | ||||
39 | if (QString::fromLatin1(ctype).endsWith(QString::fromLatin1("UTF-8"))) | ||||
40 | return true; | ||||
41 | } | ||||
42 | return false; | ||||
43 | } | ||||
44 | | ||||
45 | static const char * cpToStr(uint cp) | ||||
46 | { | ||||
47 | static char sbuf[32]; | ||||
48 | qsnprintf(sbuf, sizeof(sbuf), "U+%04X", cp); | ||||
49 | return sbuf; | ||||
50 | } | ||||
51 | | ||||
31 | void CharacterWidthTest::testWidth_data() | 52 | void CharacterWidthTest::testWidth_data() | ||
32 | { | 53 | { | ||
33 | QTest::addColumn<uint>("character"); | 54 | QTest::addColumn<uint>("character"); | ||
34 | QTest::addColumn<int>("width"); | 55 | QTest::addColumn<int>("width"); | ||
35 | 56 | | |||
36 | /* This is a work in progress.... */ | 57 | // We can't test too much - results depend on libc version, widths changed in | ||
58 | // the past, and will change in the future. However, code points below are | ||||
59 | // probably too common to change. | ||||
60 | | ||||
61 | // Control characters | ||||
62 | for (uint cp = 0x01; cp <= 0x1F; ++cp) { | ||||
63 | QTest::newRow(cpToStr(cp)) << cp << -1; | ||||
64 | } | ||||
65 | // Delete | ||||
66 | QTest::newRow(cpToStr(uint(0x7F))) << uint(0x7F) << -1; | ||||
37 | 67 | | |||
38 | /* konsole_wcwidth uses -1 C0/C1 and DEL */ | 68 | // Basic Latin | ||
39 | QTest::newRow("0x007F") << uint(0x007F) << -1; | 69 | for (uint cp = 0x20; cp <= 0x7E; ++cp) { | ||
70 | QTest::newRow(cpToStr(cp)) << cp << 1; | ||||
71 | } | ||||
72 | | ||||
73 | if (systemUsesUtf8()) { | ||||
74 | // Hiragana | ||||
75 | for (uint cp = 0x3041; cp <= 0x3096; ++cp) { | ||||
76 | QTest::newRow(cpToStr(cp)) << cp << 2; | ||||
77 | } | ||||
78 | | ||||
79 | // Combining Diacritical Marks (subset) | ||||
80 | for (uint cp = 0x300; cp <= 0x30F; ++cp) { | ||||
81 | QTest::newRow(cpToStr(cp)) << cp << 0; | ||||
82 | } | ||||
40 | 83 | | |||
41 | QTest::newRow("0x0300") << uint(0x0300) << 0; | 84 | // Test some CP > 0xFFFF to detect truncation | ||
42 | QTest::newRow("0x070F") << uint(0x070F) << 0; | 85 | | ||
43 | QTest::newRow("0x1160") << uint(0x1160) << 0; | 86 | // Mathematical bold capital A...Z, small A...Z | ||
44 | QTest::newRow("0x10300") << uint(0x10300) << 1; | 87 | for (uint cp = 0x1D400; cp <= 0x1D433; ++cp) { | ||
45 | 88 | QTest::newRow(cpToStr(cp)) << cp << 1; | |||
46 | QTest::newRow("a") << uint('a') << 1; | 89 | } | ||
47 | QTest::newRow("0x00AD") << uint(0x00AD) << 1; | 90 | | ||
48 | QTest::newRow("0x00A0") << uint(0x00A0) << 1; | 91 | // CJK Ideograph Extension B (subset) | ||
49 | QTest::newRow("0x10FB") << uint(0x10FB) << 1; | 92 | for (uint cp = 0x20000; cp <= 0x20010; ++cp) { | ||
50 | QTest::newRow("0xFF76") << uint(0xFF76) << 1; | 93 | QTest::newRow(cpToStr(cp)) << cp << 2; | ||
51 | QTest::newRow("0x103A0") << uint(0x103A0) << 1; | 94 | } | ||
52 | QTest::newRow("0x10A06") << uint(0x10A06) << 0; | 95 | } else { | ||
53 | 96 | QWARN("System locale does not use UTF-8. Limiting the test to code points <= 0xFF"); | |||
54 | QTest::newRow("0x3000") << uint(0x3000) << 2; | 97 | } | ||
55 | QTest::newRow("0x300a") << uint(0x300a) << 2; | | |||
56 | QTest::newRow("0x300b") << uint(0x300b) << 2; | | |||
57 | QTest::newRow("0xFF01") << uint(0xFF01) << 2; | | |||
58 | QTest::newRow("0xFF5F") << uint(0xFF5F) << 2; | | |||
59 | QTest::newRow("0xFF60") << uint(0xFF60) << 2; | | |||
60 | QTest::newRow("0xFFe0") << uint(0xFFe6) << 2; | | |||
61 | } | 98 | } | ||
62 | 99 | | |||
63 | void CharacterWidthTest::testWidth() | 100 | void CharacterWidthTest::testWidth() | ||
64 | { | 101 | { | ||
65 | QFETCH(uint, character); | 102 | QFETCH(uint, character); | ||
66 | 103 | | |||
67 | QTEST(konsole_wcwidth(character), "width"); | 104 | QTEST(Character::width(character), "width"); | ||
68 | } | 105 | } | ||
69 | 106 | | |||
70 | QTEST_GUILESS_MAIN(CharacterWidthTest) | 107 | QTEST_GUILESS_MAIN(CharacterWidthTest) |