circular_list_test.dart 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. import 'package:flutter_test/flutter_test.dart';
  2. import 'package:xterm/src/utils/circular_list.dart';
  3. void main() {
  4. group("CircularList Tests", () {
  5. test("normal creation test", () {
  6. final cl = CircularList<int>(1000);
  7. expect(cl, isNotNull);
  8. expect(cl.maxLength, 1000);
  9. });
  10. test("change max value", () {
  11. final cl = CircularList<int>(2000);
  12. expect(cl.maxLength, 2000);
  13. cl.maxLength = 3000;
  14. expect(cl.maxLength, 3000);
  15. });
  16. test("circle works", () {
  17. final cl = CircularList<int>(10);
  18. expect(cl.maxLength, 10);
  19. cl.pushAll(List<int>.generate(10, (index) => index));
  20. expect(cl.length, 10);
  21. expect(cl[0], 0);
  22. expect(cl[9], 9);
  23. cl.push(10);
  24. expect(cl.length, 10);
  25. expect(cl[0], 1);
  26. expect(cl[9], 10);
  27. });
  28. test("change max value after circle", () {
  29. final cl = CircularList<int>(10);
  30. cl.pushAll(List<int>.generate(15, (index) => index));
  31. expect(cl.length, 10);
  32. expect(cl[0], 5);
  33. expect(cl[9], 14);
  34. cl.maxLength = 20;
  35. expect(cl.length, 10);
  36. expect(cl[0], 5);
  37. expect(cl[9], 14);
  38. cl.pushAll(List<int>.generate(5, (index) => 15 + index));
  39. expect(cl[0], 5);
  40. expect(cl[9], 14);
  41. expect(cl[14], 19);
  42. });
  43. test("setting the length erases trail", () {
  44. final cl = CircularList<int>(10);
  45. cl.pushAll(List<int>.generate(10, (index) => index));
  46. expect(cl.length, 10);
  47. expect(cl[0], 0);
  48. expect(cl[9], 9);
  49. cl.length = 5;
  50. expect(cl.length, 5);
  51. expect(cl[0], 0);
  52. expect(() => cl[5], throwsRangeError);
  53. });
  54. test("foreach works", () {
  55. final cl = CircularList<int>(10);
  56. cl.pushAll(List<int>.generate(10, (index) => index));
  57. final collectedItems = List<int>.empty(growable: true);
  58. cl.forEach((item) {
  59. collectedItems.add(item);
  60. });
  61. expect(collectedItems.length, 10);
  62. expect(collectedItems[0], 0);
  63. expect(collectedItems[9], 9);
  64. });
  65. test("index operator set works", () {
  66. final cl = CircularList<int>(10);
  67. cl.pushAll(List<int>.generate(10, (index) => index));
  68. expect(cl.length, 10);
  69. expect(cl[5], 5);
  70. cl[5] = 50;
  71. expect(cl[5], 50);
  72. });
  73. test("clear works", () {
  74. final cl = CircularList<int>(10);
  75. cl.pushAll(List<int>.generate(10, (index) => index));
  76. expect(cl[5], 5);
  77. cl.clear();
  78. expect(cl.length, 0);
  79. expect(() => cl[5], throwsRangeError);
  80. });
  81. test("pop works", () {
  82. final cl = CircularList<int>(10);
  83. cl.pushAll(List<int>.generate(10, (index) => index));
  84. expect(cl.length, 10);
  85. expect(cl[9], 9);
  86. final val = cl.pop();
  87. expect(val, 9);
  88. expect(cl.length, 9);
  89. expect(() => cl[9], throwsRangeError);
  90. expect(cl[8], 8);
  91. });
  92. test("pop on empty throws", () {
  93. final cl = CircularList<int>(10);
  94. expect(() => cl.pop(), throwsA(anything));
  95. });
  96. test("remove one works", () {
  97. final cl = CircularList<int>(10);
  98. cl.pushAll(List<int>.generate(10, (index) => index));
  99. expect(cl.length, 10);
  100. expect(cl[5], 5);
  101. cl.remove(5);
  102. expect(cl.length, 9);
  103. expect(cl[5], 6);
  104. });
  105. test("remove multiple works", () {
  106. final cl = CircularList<int>(10);
  107. cl.pushAll(List<int>.generate(10, (index) => index));
  108. expect(cl.length, 10);
  109. expect(cl[5], 5);
  110. cl.remove(5, 3);
  111. expect(cl.length, 7);
  112. expect(cl[5], 8);
  113. });
  114. test("remove circle works", () {
  115. final cl = CircularList<int>(10);
  116. cl.pushAll(List<int>.generate(15, (index) => index));
  117. expect(cl.length, 10);
  118. expect(cl[0], 5);
  119. cl.remove(0, 9);
  120. expect(cl.length, 1);
  121. expect(cl[0], 14);
  122. });
  123. test("remove too much works", () {
  124. final cl = CircularList<int>(10);
  125. cl.pushAll(List<int>.generate(10, (index) => index));
  126. expect(cl.length, 10);
  127. expect(cl[5], 5);
  128. cl.remove(5, 10);
  129. expect(cl.length, 5);
  130. expect(cl[0], 0);
  131. });
  132. test("insert works", () {
  133. final cl = CircularList<int>(10);
  134. cl.pushAll(List<int>.generate(5, (index) => index));
  135. expect(cl.length, 5);
  136. expect(cl[0], 0);
  137. cl.insert(0, 100);
  138. expect(cl.length, 6);
  139. expect(cl[0], 100);
  140. expect(cl[1], 0);
  141. });
  142. test("insert circular works", () {
  143. final cl = CircularList<int>(10);
  144. cl.pushAll(List<int>.generate(10, (index) => index));
  145. expect(cl.length, 10);
  146. expect(cl[0], 0);
  147. expect(cl[1], 1);
  148. expect(cl[9], 9);
  149. cl.insert(1, 100);
  150. expect(cl.length, 10);
  151. expect(cl[0], 100); //circle leads to 100 moving one index down
  152. expect(cl[1], 1);
  153. });
  154. test("insert circular immediately remove works", () {
  155. final cl = CircularList<int>(10);
  156. cl.pushAll(List<int>.generate(10, (index) => index));
  157. expect(cl.length, 10);
  158. expect(cl[0], 0);
  159. expect(cl[1], 1);
  160. expect(cl[9], 9);
  161. cl.insert(0, 100);
  162. expect(cl.length, 10);
  163. expect(cl[0], 0); //the inserted 100 fell over immediately
  164. expect(cl[1], 1);
  165. });
  166. test("insert all works", () {
  167. final cl = CircularList<int>(10);
  168. cl.pushAll(List<int>.generate(10, (index) => index));
  169. expect(cl.length, 10);
  170. expect(cl[0], 0);
  171. expect(cl[1], 1);
  172. expect(cl[9], 9);
  173. cl.insertAll(2, List<int>.generate(2, (index) => 20 + index));
  174. expect(cl.length, 10);
  175. expect(cl[0], 20);
  176. expect(cl[1], 21);
  177. expect(cl[3], 3);
  178. expect(cl[9], 9);
  179. });
  180. test("trim start works", () {
  181. final cl = CircularList<int>(10);
  182. cl.pushAll(List<int>.generate(10, (index) => index));
  183. expect(cl.length, 10);
  184. expect(cl[0], 0);
  185. expect(cl[1], 1);
  186. expect(cl[9], 9);
  187. cl.trimStart(5);
  188. expect(cl.length, 5);
  189. expect(cl[0], 5);
  190. expect(cl[1], 6);
  191. expect(cl[4], 9);
  192. });
  193. test("trim start with more than length works", () {
  194. final cl = CircularList<int>(10);
  195. cl.pushAll(List<int>.generate(10, (index) => index));
  196. expect(cl.length, 10);
  197. expect(cl[0], 0);
  198. expect(cl[1], 1);
  199. expect(cl[9], 9);
  200. cl.trimStart(15);
  201. expect(cl.length, 0);
  202. });
  203. test("shift elements works", () {
  204. final cl = CircularList<int>(20);
  205. cl.pushAll(List<int>.generate(20, (index) => index));
  206. expect(cl.length, 20);
  207. expect(cl[0], 0);
  208. expect(cl[1], 1);
  209. expect(cl[9], 9);
  210. cl.shiftElements(5, 3, 2);
  211. expect(cl.length, 20);
  212. expect(cl[0], 0); // untouched
  213. expect(cl[1], 1); // untouched
  214. expect(cl[5], 5); // moved
  215. expect(cl[6], 6); // moved
  216. expect(cl[7], 5); // moved (7) and target (5)
  217. expect(cl[8], 6); // target (6)
  218. expect(cl[9], 7); // target (7)
  219. expect(cl[10], 10); // untouched
  220. expect(cl[11], 11); // untouched
  221. });
  222. test("shift elements over bounds throws", () {
  223. final cl = CircularList<int>(10);
  224. cl.pushAll(List<int>.generate(10, (index) => index));
  225. expect(cl.length, 10);
  226. expect(cl[0], 0);
  227. expect(cl[1], 1);
  228. expect(cl[9], 9);
  229. expect(() => cl.shiftElements(8, 2, 3), throwsA(anything));
  230. expect(() => cl.shiftElements(2, 3, -3), throwsA(anything));
  231. });
  232. });
  233. }