PageRenderTime 46ms CodeModel.GetById 11ms app.highlight 31ms RepoModel.GetById 1ms app.codeStats 0ms

/examples/tests.d

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