TextEncodingTest.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. 
  2. #include "../testTools.h"
  3. // These tests will fail if the source code document or stored files change their encoding of line breaks.
  4. String expected_latin1 =
  5. R"QUOTE(Hello my friend
  6. Hej min vän
  7. Halló, vinur minn
  8. Hei ystäväni
  9. Hola mi amigo
  10. Ciao amico
  11. )QUOTE";
  12. // Warning!
  13. // String literals containing characters above value 255 must be stored explicitly in unicode literals using U"" instead of "".
  14. // Because string literals do not begin with a byte order mark to say which encoding is being used.
  15. // Also make sure to save the source code document using a byte order mark so that the C++ compiler receives the correct symbol.
  16. String unicodeContent =
  17. UR"QUOTE(Hello my friend
  18. Hej min vän
  19. Halló, vinur minn
  20. Hei ystäväni
  21. Hola mi amigo
  22. Ciao amico
  23. 你好我的朋友
  24. こんにちは、友よ
  25. नमस्ते मेरो साथी
  26. Talofa laʻu uo
  27. Xin chào bạn của tôi
  28. העלא מיין פרייַנד
  29. 안녕 내 친구
  30. سلام دوست من
  31. ਹੈਲੋ ਮੇਰੇ ਦੋਸਤ
  32. ওহে, বন্ধু আমার
  33. សួស្តី​សម្លាញ់
  34. Γεια σου φίλε μου
  35. Привет, мой друг
  36. здраво пријатељу
  37. Բարեւ իմ ընկեր
  38. ආයුබෝවන් මාගේ යාළුවා
  39. ಹಲೋ ನನ್ನ ಸ್ನೇಹಿತನೇ
  40. Silav hevalê min
  41. اهلا صديقي
  42. 𐐷
  43. )QUOTE";
  44. String expected_utf8 = unicodeContent + U"\nThis is UTF-8";
  45. String expected_utf16le = unicodeContent + U"\nThis is UTF-16 Little Endian";
  46. String expected_utf16be = unicodeContent + U"\nThis is UTF-16 Big Endian";
  47. void printCharacterCode(uint32_t value) {
  48. for (int i = 0; i < 32; i++) {
  49. if (value & 0b10000000000000000000000000000000) {
  50. printText(U"1");
  51. } else {
  52. printText(U"0");
  53. }
  54. value = value << 1;
  55. }
  56. }
  57. // Method for printing the character codes of a string for debugging
  58. void compareCharacterCodes(String textA, String textB) {
  59. int lengthA = string_length(textA);
  60. int lengthB = string_length(textB);
  61. int minLength = lengthA < lengthB ? lengthA : lengthB;
  62. printText("Character codes for strings of length ", lengthA, U" and ", lengthB, U":\n");
  63. for (int i = 0; i < minLength; i++) {
  64. uint32_t codeA = (uint32_t)textA[i];
  65. uint32_t codeB = (uint32_t)textB[i];
  66. printCharacterCode(codeA);
  67. if (codeA == codeB) {
  68. printText(U" == ");
  69. } else {
  70. printText(U" != ");
  71. }
  72. printCharacterCode(codeB);
  73. printText(U" (", textA[i], U") (", textB[i], U")\n");
  74. }
  75. if (lengthA > lengthB) {
  76. for (int i = minLength; i < lengthA; i++) {
  77. uint32_t codeA = (uint32_t)textA[i];
  78. printCharacterCode(codeA);
  79. printText(U" (", textA[i], U")\n");
  80. }
  81. } else {
  82. printText(U" ");
  83. for (int i = minLength; i < lengthB; i++) {
  84. uint32_t codeB = (uint32_t)textB[i];
  85. printCharacterCode(codeB);
  86. printText(U" (", textB[i], U")\n");
  87. }
  88. }
  89. }
  90. START_TEST(TextEncoding)
  91. String folderPath = string_combine(U"test", file_separator(), U"tests", file_separator(), U"resources", file_separator());
  92. { // Text encodings stored in memory
  93. // TODO: Test string_loadFromMemory using random character codes from the extended 0x10000..0x10FFFF range
  94. }
  95. { // Loading strings of different encodings
  96. String fileLatin1 = string_load(folderPath + U"Latin1.txt", true);
  97. //compareCharacterCodes(fileLatin1, expected_latin1);
  98. ASSERT_MATCH(fileLatin1, expected_latin1);
  99. String fileUTF8 = string_load(folderPath + U"BomUtf8.txt", true);
  100. //compareCharacterCodes(fileUTF8, expected_utf8);
  101. ASSERT_MATCH(fileUTF8, expected_utf8);
  102. String fileUTF16LE = string_load(folderPath + U"BomUtf16Le.txt", true);
  103. //compareCharacterCodes(fileUTF16LE, expected_utf16le);
  104. ASSERT_MATCH(fileUTF16LE, expected_utf16le);
  105. String fileUTF16BE = string_load(folderPath + U"BomUtf16Be.txt", true);
  106. //compareCharacterCodes(fileUTF16BE, expected_utf16be);
  107. ASSERT_MATCH(fileUTF16BE, expected_utf16be);
  108. }
  109. { // Saving and loading text to files using every combination of character and line encoding
  110. String originalContent = U"Hello my friend\n你好我的朋友\n𐐷𤭢\n";
  111. String latin1Expected = U"Hello my friend\n??????\n??\n";
  112. String tempPath = folderPath + U"Temporary.txt";
  113. for (int i = 0; i < 2; i++) {
  114. LineEncoding lineEncoding = (i == 0) ? LineEncoding::CrLf : LineEncoding::Lf;
  115. // Latin-1 should store up to 8 bits correctly, and write ? for complex characters
  116. string_save(tempPath, originalContent, CharacterEncoding::Raw_Latin1, lineEncoding);
  117. String latin1Loaded = string_load(tempPath, true);
  118. //compareCharacterCodes(latin1Loaded, latin1Expected);
  119. ASSERT_MATCH(latin1Loaded, latin1Expected);
  120. // UFT-8 should store up to 21 bits correctly
  121. string_save(tempPath, unicodeContent, CharacterEncoding::BOM_UTF8, lineEncoding);
  122. ASSERT_MATCH(string_load(tempPath, true), unicodeContent);
  123. // UFT-16 should store up to 20 bits correctly
  124. string_save(tempPath, unicodeContent, CharacterEncoding::BOM_UTF16BE, lineEncoding);
  125. ASSERT_MATCH(string_load(tempPath, true), unicodeContent);
  126. string_save(tempPath, unicodeContent, CharacterEncoding::BOM_UTF16LE, lineEncoding);
  127. ASSERT_MATCH(string_load(tempPath, true), unicodeContent);
  128. string_save(tempPath, U"This file is used when testing text encoding.");
  129. }
  130. }
  131. END_TEST