/examples/tests.d

http://github.com/baryluk/cords · D · 478 lines · 178 code · 60 blank · 240 comment · 9 complexity · 09f3a2c42fbed5e0064778183b37f6c8 MD5 · raw file

  1. module m;
  2. import std.stdio;
  3. import utils.log: logging;
  4. import utils.timer : Timer;
  5. import std.string;
  6. import iolists;
  7. import cords;
  8. import iolists_mutable;
  9. const int n = 2000;
  10. const int m = 200;
  11. int main(string[] args) {
  12. logging = true;
  13. //cordy1();
  14. writefln();
  15. writefln();
  16. // cordy_t_manual();
  17. writefln();
  18. writefln();
  19. cordy_t();
  20. return 0;
  21. }
  22. /+
  23. void cordy_l() {
  24. auto rr = gen();
  25. writefln("prepend tests:\n");
  26. test(rr, 100, false);
  27. writefln("\nappend&prepend tests:\n");
  28. auto r2 = test(rr, 100, true);
  29. {
  30. writefln("\ncord slicing");
  31. scope Timer = new Timer();
  32. auto r3 = r2[6231 .. 19123];
  33. }
  34. auto x = new cord_l!(char)("sd");
  35. auto y1 = x ~ "gg";
  36. auto y2 = "gg" ~ x;
  37. writefln("x= %s", x);
  38. writefln("y1= %s", y1);
  39. writefln("y2= %s", y2);
  40. auto xx = x ~ x;
  41. writefln("xx= %s", xx);
  42. auto x3 = y1 ~ x;
  43. writefln("x3= %s", x3);
  44. auto x10 = xx ~ "b" ~ x3;
  45. writefln("x10= %s", x10);
  46. writefln("x10'= %s", x10.remove(4, 5));
  47. }
  48. +/
  49. char[] gen() {
  50. char[] r;
  51. r.length = 1000;
  52. r[] = 'a';
  53. return r;
  54. }
  55. CordI test(char[] rr, int n, bool pr) {
  56. writefln("cord");
  57. scope tr = new Timer();
  58. auto r2 = Cord();
  59. for(int i = 0; i < n; i++) {
  60. r2 ~= rr;
  61. if (pr) r2.prepend(rr);
  62. }
  63. delete tr;
  64. writefln("cord to string");
  65. scope tr2 = new Timer();
  66. auto xxc = r2.toString();
  67. delete tr2;
  68. writefln("string");
  69. scope ts = new Timer();
  70. char[] sr2;
  71. for(int i = 0; i < n; i++) {
  72. sr2 ~= rr;
  73. if (pr) sr2 = rr ~ sr2;
  74. }
  75. delete ts;
  76. return r2.to_cord();
  77. }
  78. //version=ps; // print structure
  79. //version=pt; // print ruler
  80. /** Tree cord using unions of structs and switch */
  81. /+
  82. void cordy_t() {
  83. auto x1 = new cord2();
  84. writefln("x1");
  85. version(ps) x1.print_structure();
  86. version (ps) writefln();
  87. auto x2 = new cord2("df");
  88. writefln("x2");
  89. version(ps) x2.print_structure();
  90. version (ps) writefln();
  91. auto x3 = new cord2("bf");
  92. writefln("x3");
  93. version(ps) x3.print_structure();
  94. version (ps) writefln();
  95. auto x4 = x2~x3;
  96. writefln("x4");
  97. version(ps) x4.print_structure();
  98. version (ps) writefln();
  99. auto x5 = x4~x4;
  100. writefln("x5");
  101. version(ps) x5.print_structure();
  102. version (ps) writefln();
  103. auto x6 = x5~x5;
  104. writefln("x6");
  105. version(ps) x6.print_structure();
  106. version (ps) writefln();
  107. auto x7 = x6~x6;
  108. writefln("x7");
  109. version(ps) x7.print_structure();
  110. version (ps) writefln();
  111. auto x8 = x7 ~ "ALA";
  112. writefln("x8");
  113. version(ps) x8.print_structure();
  114. version (ps) writefln();
  115. auto x9 = x8 ~ "KOT";
  116. writefln("x9");
  117. version(ps) x9.print_structure();
  118. version (ps) writefln();
  119. auto x9a = x8 ~ new cord2("KOT");
  120. writefln("x9a");
  121. version (ps) x9a.print_structure();
  122. version (ps) writefln();
  123. writefln("cord_zenek");
  124. scope tr = new Timer();
  125. for (int i = 0; i < n; i++) {
  126. x9a = x9a ~ x6;
  127. if (i % m == 0) x9a = x6 ~ x9a;
  128. }
  129. delete tr;
  130. writefln("cord_zenek toString");
  131. tr = new Timer();
  132. auto sx9a = x9a.toString();
  133. auto sx6 = x6.toString();
  134. delete tr;
  135. assert(sx9a.length == x9a.length);
  136. assert(sx6.length == x6.length);
  137. /*
  138. writefln("char[] opCatAssign");
  139. tr = new Timer();
  140. for (int i = 0; i < n; i++) {
  141. sx9a ~= sx6;
  142. if (i % m == 0) sx9a = sx6 ~ sx9a;
  143. }
  144. delete tr;
  145. */
  146. /*
  147. sx9a = x9a.toString();
  148. sx6 = x6.toString();
  149. writefln("char[] opCat");
  150. tr = new Timer();
  151. for (int i = 0; i < n; i++) {
  152. sx9a = sx9a ~ sx6;
  153. if (i % m == 0) sx9a = sx6 ~ sx9a;
  154. }
  155. delete tr;
  156. */
  157. /*
  158. auto lx9a = new cord(sx9a);
  159. auto lx6 = new cord(sx6);
  160. writefln("cord_l opCatAssign");
  161. tr = new Timer();
  162. for (int i = 0; i < n; i++) {
  163. //lx9a = lx9a ~ lx6;
  164. lx9a ~= lx6;
  165. if (i % m == 0) lx9a = lx6 ~ lx9a;
  166. }
  167. delete tr;
  168. */
  169. ss();
  170. }
  171. void ss() {
  172. writefln();
  173. writefln("Substring tests");
  174. writefln();
  175. auto x = new cord2("abcdefghijk123456789");
  176. version(ps) x.print_structure();
  177. x.print();
  178. auto x1 = x[5..13];
  179. version(ps) x1.print_structure();
  180. x1.print();
  181. auto x2 = x ~ x1;
  182. version(ps) x2.print_structure();
  183. x2.print();
  184. auto x3 = x2[13..24];
  185. version(ps) x3.print_structure();
  186. x3.print();
  187. x3 = x2[0..20];
  188. version(ps) x3.print_structure();
  189. x3.print();
  190. x3 = x2[20..28];
  191. version(ps) x3.print_structure();
  192. x3.print();
  193. x3 = x2[0..28];
  194. version(ps) x3.print_structure();
  195. x3.print();
  196. ins();
  197. }
  198. void ins() {
  199. writefln();
  200. writefln("Inserts tests");
  201. writefln();
  202. auto t = new cord2("123456789012345678901234567890");
  203. auto x = new cord2("abcdefghijk123456789");
  204. writefln("Start:");
  205. version(ps) x.print_structure();
  206. version (pt) t.print();
  207. x.print();
  208. auto x1 = (x[3..5] = "PLOKIJ");
  209. writefln("4th and 5th change to PLOKIJ:");
  210. version (pt) t.print();
  211. x1.print();
  212. version(ps) x1.print_structure();
  213. writefln("Orginal:");
  214. version(ps) x.print_structure();
  215. version (pt) t.print();
  216. x.print();
  217. x1 = (x1[5..5] = "Y");
  218. writefln("After 5-th insert Y:");
  219. version(ps) x1.print_structure();
  220. version (pt) t.print();
  221. x1.print();
  222. x1 = (x[3..10] = "");
  223. writefln("Remove 4th - 10th:");
  224. version(ps) x1.print_structure();
  225. version (pt) t.print();
  226. x1.print();
  227. }
  228. ++/
  229. /** Tree cord with inheritance */
  230. void cordy_t() {
  231. auto x1 = RBCord();
  232. writefln("x1");
  233. version(ps) x1.print_structure();
  234. version (ps) writefln();
  235. auto x2 = RBCord("df");
  236. writefln("x2");
  237. version(ps) x2.print_structure();
  238. version (ps) writefln();
  239. auto x3 = RBCord("bf");
  240. writefln("x3");
  241. version(ps) x3.print_structure();
  242. version (ps) writefln();
  243. auto x4 = x2~x3;
  244. writefln("x4");
  245. version(ps) x4.print_structure();
  246. version (ps) writefln();
  247. auto x5 = x4~x4;
  248. writefln("x5");
  249. version(ps) x5.print_structure();
  250. version (ps) writefln();
  251. auto x6 = x5~x5;
  252. writefln("x6");
  253. version(ps) x6.print_structure();
  254. version (ps) writefln();
  255. auto x7 = x6~x6;
  256. writefln("x7");
  257. version(ps) x7.print_structure();
  258. version (ps) writefln();
  259. auto x8 = x7 ~ "ALA";
  260. writefln("x8");
  261. version(ps) x8.print_structure();
  262. version (ps) writefln();
  263. auto x9 = x8 ~ "KOT";
  264. writefln("x9");
  265. version(ps) x9.print_structure();
  266. version (ps) writefln();
  267. auto x9a = x8 ~ RBCord("KOT");
  268. writefln("x9a");
  269. version (ps) x9a.print_structure();
  270. version (ps) writefln();
  271. writefln("cord_t");
  272. scope tr = new Timer();
  273. for (int i = 0; i < n; i++) {
  274. writefln(i);
  275. auto x9f = x9a ~ x6;
  276. auto x9ff = x9f;
  277. if (i % m == 0) {
  278. x9ff = x6 ~ x9a;
  279. }
  280. x9a = x9ff;
  281. }
  282. delete tr;
  283. writefln("cord_t toArray()");
  284. tr = new Timer();
  285. auto sx9a = x9a.toArray();
  286. auto sx6 = x6.toArray();
  287. delete tr;
  288. assert(sx9a.length == x9a.length);
  289. assert(sx6.length == x6.length);
  290. ss_i();
  291. }
  292. void ss_i() {
  293. writefln();
  294. writefln("Substring tests (cord_t)");
  295. writefln();
  296. auto x = RBCord("abcdefghijk123456789");
  297. version(ps) x.print_structure();
  298. x.print();
  299. auto x1 = x[5..13];
  300. version(ps) x1.print_structure();
  301. x1.print();
  302. auto x2 = x ~ x1;
  303. version(ps) x2.print_structure();
  304. x2.print();
  305. auto x3 = x2[13..24];
  306. version(ps) x3.print_structure();
  307. x3.print();
  308. auto x3a = x2[0..20];
  309. version(ps) x3a.print_structure();
  310. x3a.print();
  311. auto x3b = x2[20..28];
  312. version(ps) x3b.print_structure();
  313. x3b.print();
  314. auto x3c = x2[0..28];
  315. version(ps) x3c.print_structure();
  316. x3c.print();
  317. ins_i();
  318. }
  319. void ins_i() {
  320. writefln();
  321. writefln("Inserts tests (cord_t)");
  322. writefln();
  323. auto t = RBCord("123456789012345678901234567890");
  324. auto x = RBCord("abcdefghijk123456789");
  325. writefln("Start:");
  326. version(ps) x.print_structure();
  327. version (pt) t.print();
  328. x.print();
  329. auto x1 = (x[3..5] = "PLOKIJ");
  330. writefln("4th and 5th change to PLOKIJ:");
  331. version (pt) t.print();
  332. x1.print();
  333. version(ps) x1.print_structure();
  334. writefln("Orginal:");
  335. version(ps) x.print_structure();
  336. version (pt) t.print();
  337. x.print();
  338. auto x1a = (x1[5..5] = "Y");
  339. writefln("After 5-th insert Y:");
  340. version(ps) x1a.print_structure();
  341. version (pt) t.print();
  342. x1a.print();
  343. auto x1b = (x[3..10] = "");
  344. writefln("Remove 4th - 10th:");
  345. version(ps) x1b.print_structure();
  346. version (pt) t.print();
  347. x1b.print();
  348. iter();
  349. }
  350. void iter() {
  351. writefln();
  352. writefln("Single iter (cord_t)");
  353. writefln();
  354. auto a1 = RBCord("12345");
  355. auto a2 = RBCord("plokij");
  356. // auto a3 = (("start"~a1~a2)~"midle"~(a1~a2 ~ "end") )~ "after";
  357. auto a3 = ((a1~a2)~"midle"~(a1~a2 ~ "end") )~ "after";
  358. a3.print();
  359. version (ps) a3.print_structure();
  360. //for (auto a3i = a3.getIterator(); !a3i.isLast(); a3i.opInc()) {
  361. // //writefln("path:");
  362. // //a3i.print_path();
  363. // writefln("a3[%d] = %s", a3i.get_index, a3i.fetch);
  364. //}
  365. }