summaryrefslogtreecommitdiffstats
path: root/mimelib/test_boyermor.cpp
blob: 2140c4fdf9cb53c149163cf64fc22efe97e5b8f4 (plain)
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
#include <mimelib/boyermor.h>
#include <mimelib/string.h>

#include <iostream>
using std::cerr;
using std::cout;
using std::endl;

static const char * _haystack =
  "haystackNeedleHaystackneedlehaYstackneeDlehaystack";

int main( int argc, char * argv[] ) {

  if ( argc == 3 ) { // manual test
    DwString needle( argv[1] );
    DwString haystack( argv[2] );

    DwBoyerMoore csbm( needle ); // case-sensitive
    DwBoyerMoore cisbm( needle ); // case-insensitive

    cout << "Case-sensitive search found ";
    for ( size_t idx = 0 ; ( idx = csbm.FindIn( haystack, idx ) ) != DwString::npos ; ++idx )
      cout << (int)idx << " ";
    cout << endl;
    cout << "Case-insensitive search found ";
    for ( size_t idx = 0 ; ( idx = cisbm.FindIn( haystack, idx, false ) ) != DwString::npos ; ++idx )
      cout << (int)idx << " ";
    cout << endl;
    exit( 0 );
  } else if ( argc == 1 ) { // automated test
    DwString haystack( _haystack );

    DwBoyerMoore needle_cs( "needle" );
    DwBoyerMoore needle_cis( "needle" );
    DwBoyerMoore Needle_cs( "Needle" );
    DwBoyerMoore Needle_cis( "Needle" );
    DwBoyerMoore neeDle_cs( "neeDle" );
    DwBoyerMoore neeDle_cis( "neeDle" );

    assert( needle_cs.FindIn( haystack, 0 ) == 22 );
    assert( needle_cs.FindIn( haystack, 23 ) == DwString::npos );

    assert( needle_cis.FindIn( haystack, 0, false ) == 8 );
    assert( needle_cis.FindIn( haystack, 9, false  ) == 22 );
    assert( needle_cis.FindIn( haystack, 23, false  ) == 36 );
    assert( needle_cis.FindIn( haystack, 37, false  ) == DwString::npos );

    assert( Needle_cs.FindIn( haystack, 0 ) == 8 );
    assert( Needle_cs.FindIn( haystack, 9 ) == DwString::npos );

    assert( Needle_cis.FindIn( haystack, 0, false  ) == 8 );
    assert( Needle_cis.FindIn( haystack, 9, false  ) == 22 );
    assert( Needle_cis.FindIn( haystack, 23, false  ) == 36 );
    assert( Needle_cis.FindIn( haystack, 37, false  ) == DwString::npos );

    assert( neeDle_cs.FindIn( haystack, 0 ) == 36 );
    assert( neeDle_cs.FindIn( haystack, 37 ) == DwString::npos );

    assert( neeDle_cis.FindIn( haystack, 0, false  ) == 8 );
    assert( neeDle_cis.FindIn( haystack, 9, false  ) == 22 );
    assert( neeDle_cis.FindIn( haystack, 23, false  ) == 36 );
    assert( neeDle_cis.FindIn( haystack, 37, false  ) == DwString::npos );

  } else {
    cerr << "usage: test_boyermor [ <needle> <haystack> ]" << endl;
    exit( 1 );
  }

  return 0;
};