UnitTests.cpp 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. #include "TextEditor.h"
  2. void TextEditor::UnitTests()
  3. {
  4. SetText(" \t \t \t \t\n");
  5. // --- GetCharacterColumn --- //
  6. {
  7. // Returns column given line and character index in that line.
  8. // Column is on the left side of character
  9. assert(GetCharacterColumn(0, 0) == 0);
  10. assert(GetCharacterColumn(0, 1) == 1);
  11. assert(GetCharacterColumn(0, 2) == 4);
  12. assert(GetCharacterColumn(0, 3) == 5);
  13. assert(GetCharacterColumn(0, 4) == 6);
  14. assert(GetCharacterColumn(0, 5) == 8);
  15. assert(GetCharacterColumn(0, 6) == 9);
  16. assert(GetCharacterColumn(0, 7) == 10);
  17. assert(GetCharacterColumn(0, 8) == 11);
  18. assert(GetCharacterColumn(0, 9) == 12);
  19. assert(GetCharacterColumn(0, 10) == 13);
  20. assert(GetCharacterColumn(0, 11) == 16);
  21. assert(GetCharacterColumn(0, 12) == 16); // out of range
  22. // empty line
  23. assert(GetCharacterColumn(1, 0) == 0);
  24. assert(GetCharacterColumn(1, 1) == 0); // out of range
  25. assert(GetCharacterColumn(1, 2) == 0); // out of range
  26. // nonexistent line
  27. assert(GetCharacterColumn(2, 0) == 0);
  28. assert(GetCharacterColumn(2, 1) == 0);
  29. assert(GetCharacterColumn(2, 2) == 0);
  30. }
  31. // --- GetCharacterIndexL --- //
  32. {
  33. // Returns character index from coordinates, if coordinates are in the middle of a tab character it returns character index of that tab character
  34. assert(GetCharacterIndexL({ 0, 0 }) == 0);
  35. assert(GetCharacterIndexL({ 0, 1 }) == 1);
  36. assert(GetCharacterIndexL({ 0, 2 }) == 1);
  37. assert(GetCharacterIndexL({ 0, 3 }) == 1);
  38. assert(GetCharacterIndexL({ 0, 4 }) == 2);
  39. assert(GetCharacterIndexL({ 0, 5 }) == 3);
  40. assert(GetCharacterIndexL({ 0, 6 }) == 4);
  41. assert(GetCharacterIndexL({ 0, 7 }) == 4);
  42. assert(GetCharacterIndexL({ 0, 8 }) == 5);
  43. assert(GetCharacterIndexL({ 0, 9 }) == 6);
  44. assert(GetCharacterIndexL({ 0, 10 }) == 7);
  45. assert(GetCharacterIndexL({ 0, 11 }) == 8);
  46. assert(GetCharacterIndexL({ 0, 12 }) == 9);
  47. assert(GetCharacterIndexL({ 0, 13 }) == 10);
  48. assert(GetCharacterIndexL({ 0, 14 }) == 10);
  49. assert(GetCharacterIndexL({ 0, 15 }) == 10);
  50. assert(GetCharacterIndexL({ 0, 16 }) == 11);
  51. assert(GetCharacterIndexL({ 0, 17 }) == 11); // out of range
  52. assert(GetCharacterIndexL({ 0, 18 }) == 11); // out of range
  53. // empty line
  54. assert(GetCharacterIndexL({ 1, 0 }) == 0);
  55. assert(GetCharacterIndexL({ 1, 1 }) == 0); // out of range
  56. assert(GetCharacterIndexL({ 1, 2 }) == 0); // out of range
  57. // nonexistent line
  58. assert(GetCharacterIndexL({ 2, 0 }) == -1);
  59. assert(GetCharacterIndexL({ 2, 1 }) == -1);
  60. assert(GetCharacterIndexL({ 2, 2 }) == -1);
  61. }
  62. // --- GetCharacterIndexR --- //
  63. {
  64. // Returns character index from coordinates, if coordinates are in the middle of a tab character it returns character index of next character
  65. assert(GetCharacterIndexR({ 0, 0 }) == 0);
  66. assert(GetCharacterIndexR({ 0, 1 }) == 1);
  67. assert(GetCharacterIndexR({ 0, 2 }) == 2);
  68. assert(GetCharacterIndexR({ 0, 3 }) == 2);
  69. assert(GetCharacterIndexR({ 0, 4 }) == 2);
  70. assert(GetCharacterIndexR({ 0, 5 }) == 3);
  71. assert(GetCharacterIndexR({ 0, 6 }) == 4);
  72. assert(GetCharacterIndexR({ 0, 7 }) == 5);
  73. assert(GetCharacterIndexR({ 0, 8 }) == 5);
  74. assert(GetCharacterIndexR({ 0, 9 }) == 6);
  75. assert(GetCharacterIndexR({ 0, 10 }) == 7);
  76. assert(GetCharacterIndexR({ 0, 11 }) == 8);
  77. assert(GetCharacterIndexR({ 0, 12 }) == 9);
  78. assert(GetCharacterIndexR({ 0, 13 }) == 10);
  79. assert(GetCharacterIndexR({ 0, 14 }) == 11);
  80. assert(GetCharacterIndexR({ 0, 15 }) == 11);
  81. assert(GetCharacterIndexR({ 0, 16 }) == 11);
  82. assert(GetCharacterIndexR({ 0, 17 }) == 11); // out of range
  83. assert(GetCharacterIndexR({ 0, 18 }) == 11); // out of range
  84. // empty line
  85. assert(GetCharacterIndexR({ 1, 0 }) == 0);
  86. assert(GetCharacterIndexR({ 1, 1 }) == 0); // out of range
  87. assert(GetCharacterIndexR({ 1, 2 }) == 0); // out of range
  88. // nonexistent line
  89. assert(GetCharacterIndexR({ 2, 0 }) == -1);
  90. assert(GetCharacterIndexR({ 2, 1 }) == -1);
  91. assert(GetCharacterIndexR({ 2, 2 }) == -1);
  92. }
  93. // --- GetText --- //
  94. {
  95. // Gets text from aStart to aEnd, tabs are counted on the start position
  96. std::string text = GetText({ 0, 0 }, { 0, 1 });
  97. assert(text.compare(" ") == 0);
  98. text = GetText({ 0, 1 }, { 0, 2 });
  99. assert(text.compare("\t") == 0);
  100. text = GetText({ 0, 2 }, { 0, 3 });
  101. assert(text.compare("") == 0);
  102. text = GetText({ 0, 3 }, { 0, 4 });
  103. assert(text.compare("") == 0);
  104. text = GetText({ 0, 4 }, { 0, 5 });
  105. assert(text.compare(" ") == 0);
  106. text = GetText({ 0, 5 }, { 0, 6 });
  107. assert(text.compare(" ") == 0);
  108. text = GetText({ 0, 6 }, { 0, 7 });
  109. assert(text.compare("\t") == 0);
  110. text = GetText({ 0, 7 }, { 0, 8 });
  111. assert(text.compare("") == 0);
  112. text = GetText({ 0, 0 }, { 0, 8 });
  113. assert(text.compare(" \t \t") == 0);
  114. text = GetText({ 0, 0 }, { 0, 7 });
  115. assert(text.compare(" \t \t") == 0);
  116. text = GetText({ 0, 0 }, { 0, 6 });
  117. assert(text.compare(" \t ") == 0);
  118. text = GetText({ 0, 4 }, { 0, 12 });
  119. assert(text.compare(" \t \t") == 0);
  120. text = GetText({ 0, 4 }, { 0, 13 });
  121. assert(text.compare(" \t \t ") == 0);
  122. text = GetText({ 0, 4 }, { 0, 14 });
  123. assert(text.compare(" \t \t \t") == 0);
  124. text = GetText({ 0, 4 }, { 0, 15 });
  125. assert(text.compare(" \t \t \t") == 0);
  126. text = GetText({ 0, 4 }, { 0, 16 });
  127. assert(text.compare(" \t \t \t") == 0);
  128. text = GetText({ 0, 0 }, { 1, 0 });
  129. assert(text.compare(" \t \t \t \t\n") == 0);
  130. }
  131. // --- DeleteRange --- //
  132. {
  133. // Deletes from start to end coordinates, any overlapping tabs will be deleted, doesn't allow out of range lines
  134. DeleteRange({ 0, 0 }, { 0, 0 });
  135. assert(GetText() == " \t \t \t \t\n");
  136. DeleteRange({ 0, 0 }, { 0, 1 });
  137. assert(GetText() == "\t \t \t \t\n");
  138. DeleteRange({ 0, 0 }, { 0, 2 });
  139. assert(GetText() == " \t \t \t\n");
  140. DeleteRange({ 0, 12 }, { 0, 12 });
  141. assert(GetText() == " \t \t \t\n");
  142. DeleteRange({ 1, 0 }, { 1, 0 });
  143. assert(GetText() == " \t \t \t\n");
  144. DeleteRange({ 0, 11 }, { 0, 12 });
  145. assert(GetText() == " \t \t \n");
  146. DeleteRange({ 0, 2 }, { 0, 3 });
  147. assert(GetText() == " \t \n");
  148. DeleteRange({ 0, 6 }, { 0, 7 });
  149. assert(GetText() == " \n");
  150. SetText("a\nb\nc\nd\ne");
  151. DeleteRange({ 0, 0 }, { 2, 1 });
  152. assert(GetText() == "\nd\ne");
  153. DeleteRange({ 1, 1 }, { 2, 0 });
  154. assert(GetText() == "\nde");
  155. DeleteRange({ 1, 1 }, { 1, 15 }); // out of range column
  156. assert(GetText() == "\nd");
  157. SetText("asdf\nzxcv\nqwer\npo");
  158. DeleteRange({ 1, 2 }, { 1, 200 }); // out of range column
  159. assert(GetText() == "asdf\nzx\nqwer\npo");
  160. DeleteRange({ 0, 500 }, { 2, 500 }); // out of range column
  161. assert(GetText() == "asdf\npo");
  162. }
  163. // --- RemoveGlyphsFromLine --- //
  164. {
  165. //
  166. }
  167. SetText("asdf asdf\nasdf\nasdf\tasdf\n zxcv zxcv");
  168. // --- FindNextOccurrence --- //
  169. {
  170. Coordinates outStart, outEnd;
  171. assert(FindNextOccurrence("asdf", 4, { 0, 0 }, outStart, outEnd) && outStart == Coordinates(0, 0) && outEnd == Coordinates(0, 4));
  172. assert(FindNextOccurrence("asdf", 4, { 0, 1 }, outStart, outEnd) && outStart == Coordinates(0, 5) && outEnd == Coordinates(0, 9));
  173. assert(FindNextOccurrence("asdf", 4, { 0, 5 }, outStart, outEnd) && outStart == Coordinates(0, 5) && outEnd == Coordinates(0, 9));
  174. assert(FindNextOccurrence("asdf", 4, { 0, 6 }, outStart, outEnd) && outStart == Coordinates(1, 0) && outEnd == Coordinates(1, 4));
  175. assert(FindNextOccurrence("asdf", 4, { 3, 3 }, outStart, outEnd) && outStart == Coordinates(0, 0) && outEnd == Coordinates(0, 4)); // go to line 0 if reach end of file
  176. assert(FindNextOccurrence("zxcv", 4, { 3, 10 }, outStart, outEnd) && outStart == Coordinates(3, 1) && outEnd == Coordinates(3, 5)); // from behind in same line
  177. assert(!FindNextOccurrence("lalal", 4, { 3, 5 }, outStart, outEnd)); // not found
  178. }
  179. }