File: test.cpp

package info (click to toggle)
vfu 4.06-3
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 2,088 kB
  • ctags: 2,184
  • sloc: cpp: 14,482; ansic: 4,091; perl: 581; makefile: 540; sh: 57
file content (398 lines) | stat: -rw-r--r-- 10,120 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
/*
 *
 * (c) Vladi Belperchinov-Shabanski "Cade" <cade@biscom.net> 1998-2003
 *
 * SEE `README',`LICENSE' OR `COPYING' FILE FOR LICENSE AND OTHER DETAILS!
 *
 * $Id: test.cpp,v 1.17 2004/12/29 02:44:21 cade Exp $
 *
 */

#include <stdio.h>
#include "vstrlib.h"

void test1()
{
  VString str = "Hello";
  str += " World"; // str is `Hello World' now
  str_reverse( str ); // str is `dlroW olleH' now
  str_low( str ); // lower case

  VArray va = str_split( " +", str ); // array contains `dlroW' at pos 0 and `olleH' at 1
  
  va.reverse(); // array reversed: `dlroW' at pos 1 and `olleH' at 0
  
  int z;
  for( z = 0; z < va.count(); z++ )
    {
    str_reverse( va[z] ); // reverses each string element
    }
  
  
  str = str_join( va, " " ); // joins into temporary string

  printf( "************************ test 1 result is: %s\n", str.data() ); // this should print `hello world'
}

void test2()
{
  VArray va;
  va.push( "hello" ); // pos 0
  va.push( "world" ); // pos 1
  
  va.ins( 1, "your" ); // pos 1 shifted
  
  va[1] = "my"; // replaces `your'
  va[3] = "!";  // set outside the size, array is extended
  
  VString str = va.pop(); // pops last element, str is now `!'
  
  str = str_join( va, "-" ); // joins to given string
  
  str_tr( str, "-", " " ); // replaces dashes with spaces
  
  str_replace( str, " my ", " " ); // removes ` my '
  
  printf( "************************ test 2 result is: %s\n", str.data() ); // this should print `hello world'
}

void test3()
{
  VTrie tr; // hash-like
  VArray va;
  
  // inserting keys and values
  tr[ "tralala" ] = "data1";
  tr[ "opala"   ] = "data2";
  tr[ "keynext" ] =  "data3";

  // inserting elements into the array
  va.push( "this" );
  va.push( "just" );
  va.push( "test" );
  va.push( "simple" );

  // adding string to the first element of the array
  va[1] += " x2";

  // the array is converted to trie (hash) and merged into `tr'
  tr += va; // same as: tr.merge( &va );

  // clear the array--remove all elements
  va.undef();

  // take keys from `tr' as array and store them into va, returns count
  // i.e. i = tr.count();
  int i;
  va = tr.keys();

  printf( "keys count = %d\n", va.count() );

  // printing the array and trie data
  for( i = 0; i < va.count(); i++ )
    {
    printf( "%d -> %s (%s)\n", i, va[i].data(), tr[ va[i] ].data() );
    }

  VArray v1;

  printf( "--------------------\n" );
  v1 = tr;    // same as: v1.undef; v1.merge( &tr );
  v1.print(); // print array data

  VRegexp re( "a([0-9]+)" ); // compiling new regexp

  if( re.m( "tralala85." ) ) // match against regexp
    printf( "sub 1 = %s\n", re[1].data() ); // re[1] returns `85'

  if( re.m( "tralala85.", "(la)+" ) ) // match against regexp
    {
    printf( "sub 0 = %s\n", re[0].data() ); // `lala'
    printf( "sub 1 = %s\n", re[1].data() ); // `la'
    }

  printf( "--------------------\n" );
  v1 = str_split( " +", "tralala  opala and another   one" ); // splits on spaces
  v1.print();

  printf( "joined: %s\n", (const char*)str_join( v1, "---" ) ); // join the same data back

  printf( "--------------------\n" );
  v1 = str_split( " +", "tralala  opala and another   one", 3 ); // splits data on spaces up to 3 elements
  v1.print();

  printf( "--------------------\n" );
  v1[1] = "hack this one here"; // set (overwrite) element 1
  str_sleft( v1[2], 11 ); // reset element 2 to the left 11 chars only
  v1[0] = 12345; // convert integer into string
  v1.print();

  printf( "--------------------\n" );
  VArray aa[3]; // array of arrays

  aa[0] = str_split( " ", "this is just a simple test" );
  aa[1] = str_split( " ", "never ending story" );
  aa[2] = str_split( " ", "star-wars rulez" );

  aa[0][1] = "was"; // first array, second element, replaces `is' with `was'
  aa[2][0] = "slackware"; // third array, first element, `star-wars' is now `slackware'

  // expands the array from 3 to 11 elements
  aa[1][10] = "king of the hill";

  for( i = 0; i < 3; i++ )
    {
    printf("---\n");
    aa[i].print();
    }
  
  printf( "---box test-----------------------------\n" );
  i = 20000;
  while( i-- )
  {
  v1.push( "this" );
  v1.push( "just" );
  v1.push( "test" );
  v1.push( "simple" );
  }
  
  v1.print();
  VArray vv = v1; // this makes vv data aliased to the data of v1
  vv.print(); // actually print the v1's data which is shared right now
  vv.set( 0, "---" ); // vv makes own copy of the array data
  vv.print(); // vv's data is no more aliased to v1's
  printf( "************************ test 3 ends here\n" );
}

void test4()
{
  // this is regression test, please ignore it...

  int i;
  int ii;
  
  VArray va;
  ii = 20;
  i = ii;
  while( i-- )
    {
    va = str_split( ",", "this is, just a simple. but fixed, nonsense test, voila :)" );
    printf( "%d%% va count = %d\n", (100*i)/ii, va.count() );
    }
  
  VString set;
  VString cat;
  VString setn;
  VString catn;
  VString sete;
  VString setp;

  i = 2000;
  
  while( i-- )
    {
    set.set( "this is, just a simple. but fixed, nonsense test, voila :)" );
    cat.cat( "this is, just a simple. but fixed, nonsense test, voila :)" );
    setn.setn( "this is, just a simple. but fixed, nonsense test, voila :)", 20 );
    catn.catn( "this is, just a simple. but fixed, nonsense test, voila :)", 20 );
    
    sete = "this is, just a simple. but fixed, nonsense test, voila :)";
    setp += "this is, just a simple. but fixed, nonsense test, voila :)";
    }

  printf( "set  = %d\n", str_len( set  ) );
  printf( "cat  = %d\n", str_len( cat  ) );
  printf( "setn = %d\n", str_len( setn ) );
  printf( "catn = %d\n", str_len( catn ) );
  printf( "sete = %d\n", str_len( sete ) );
  printf( "setp = %d\n", str_len( setp ) );

  printf( "--------------------\n" );
  
  i = 2000;
  while( i-- )
    {
    set = "this is, just a simple. but fixed, nonsense test, voila :)";
    setn = set;
    str_del( set, 20, 10 );
    str_ins( set, 30, "***opa***" );
    str_replace( setn, "i", "[I]" );
    }
  printf( "set  = %s\n", set.data() );
  printf( "setn = %s\n", setn.data() );
  
  printf( "---array sort-------\n" );
  va.undef();
  va = str_split( "[, \t]+", "this is, just a simple. but fixed, nonsense test, voila :)" );
  va.sort();
  va.print();
  printf( "--------------------\n" );
  va.sort( 1 );
  va.print();
  printf( "--------------------\n" );
  
}

void test5()
{
  VTrie tr; // hash-like
  VArray va;
  
  // inserting keys and values
  tr[ "key1" ] = "data1";
  tr[ "key2" ] = "data2";
  tr[ "key3" ] = "data3";
  
  tr.print();
  tr.reverse();
  tr.print();
  tr.reverse();
  tr.print();

  VCharSet cs;
  
  cs.push( 'a' );
  printf( "char_set: %d, %d\n", cs.in( 'a' ), cs.in( 'z' ) );
  cs.undef( 'a' );
  printf( "char_set: %d, %d\n", cs.in( 'a' ), cs.in( 'z' ) );
  cs.undef();

  int i = 2000;
  while( i-- )
    {
    cs.push( i );
    }
  cs.undef();  

  
  printf( "************************ test 5 ends here\n" );
}

void test6()
{
  VRegexp re;
  VArray va;
  
  re.comp( "^([^!]+)!(.+)=apquxz(.+)$" );
  int i = re.m( "abc!pqr=apquxz.ixr.zzz.ac.uk" );
  i--;
  while( i >= 0 )
    {
    va.push( re[i] );
    i--;
    }
  va.print();
  
  va.undef();
  va += "/this/is/samle/file.tail";
  va += "/file.tail";
  va += "/this/is/./samle/file.tail/";
  va += "/this/..../is/../samle/.file.tail";
  va += "/.file.tail";
  va += "/";
  
  const char* ps;
  
  va.reset();
  while( ( ps = va.next() ) )
    {
    printf( "------------------------------------\n" );
    printf( "file is: %s\n", ps );
    printf( "path is: %s\n", (const char*)str_file_path( ps ) );
    printf( "name is: %s\n", (const char*)str_file_name( ps ) );
    printf( "ext  is: %s\n", (const char*)str_file_ext( ps ) );
    printf( "n+ex is: %s\n", (const char*)str_file_name_ext( ps ) );
    printf( "reduced path is: %s\n", (const char*)str_reduce_path( ps ) );
    printf( "dot reduce sample is: %s\n", (const char*)str_dot_reduce( ps, 10 ) );
    }
    
  va.fsave( "/tmp/a.aaa" );  
  va.fload( "/tmp/a.aaa" );  
  va.print();
}

void test7()
{
  VTrie tr; // hash-like
  VTrie tr2; // hash-like
  VArray va;
  
  // inserting keys and values
  tr[ "key1" ] = "data1";
  tr[ "key2" ] = "data2";
  tr[ "key3" ] = "data3";
  
  tr.print();
  printf( "---------------------------------1---\n" );
  tr.reverse();
  tr.print();
  printf( "---------------------------------2---\n" );
  tr.reverse();
  tr.print();
  printf( "---------------------------------3---\n" );
  
  tr2 = str_split( " ", "this is simple one way test" );
  tr2.print();
  printf( "---------------------------------4---\n" );
  
  tr2 += tr;
  tr2.print();
  printf( "---------------------------------5---\n" );
  
  va = tr2;
  va.print();
  printf( "---------------------------------6---\n" );
}

void test8()
{
  VString v1;
  VString v2;
  
  v1 = "this is simple test ";
  v1 *= 1024;
  
  printf( "v1 len: %d\n", str_len( v1 ) );
  
  v2.compact( 1 ); // makes v2 compact, i.e. it will get as much memory as it
                   // needs. otherwise it will get fixed amount of blocks
  
  v2 = v1; // data is shared between v1 and v2. any change to v1 or v2 will
           // detach this data and both will get own copy
  
  v2[0] = ' '; // this will create own data for v2
  
  str_tr( v2, "ti", "TI" ); // capitalize T and I
  
  v2 = ""; // this will free all data allocated by v2
  
  printf( "copy 7,6: [%s]", (const char*)str_copy( v2, v1, 8, 6 ) );
  printf( "copy 10: [%s]", (const char*)str_copy( v2, v1, -10 ) );
  
  printf( "************************ test 5 ends here\n" );
}

int main( int argc, char* argv[] )
{
  //#define PAT "9892009"
  
  
  #define PAT "MARINOW Uliqn P. prof. dtn kw.Wladaq ul.Witoshki granit 11"
  //#define PAT "marinow uliqn p. prof. dtn kw.wladaq ul.witoshki granit 11"
  printf( "found at pos %ld\n", file_pattern_search( PAT, strlen(PAT),
                               "/tmp/ss.txt", "",
                               mem_quick_search ) );
  
  //printf( "expand=[%s]\n", (const char*)tilde_expand( "~root/" ) );
  
  /**/
  test1();
  test2();
  test3();
  test4();
  test5();
  test6();
  test7();
  //*/
  return 0;
}