aboutsummaryrefslogtreecommitdiffstats
path: root/fsa/src/alltest/fsa_test.cpp
blob: 6a9bfa7f7830273276e822507a83af2ed21b5e39 (plain) (blame)
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
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
#ifndef _GNU_SOURCE
#define _GNU_SOURCE
#endif

#include <stdio.h>
#include <string>

#include <vespa/fsa/fsa.h>

using namespace fsa;

int main(int, char**)
{
  FSA *f = new FSA("__testfsa__.__fsa__", FILE_ACCESS_MMAP);
  FSA::State *fs = new FSA::State(*f);

  std::string s("cucu");
  fs->start(s);
  fs->delta('m');
  fs->delta("ber");
  if(fs->isFinal()){
    printf("start/delta test: string(\"cucu\")+'m'+\"ber\" is accepted\n");
    printf("                  data size: %d\n",fs->dataSize());
    printf("                  data string: \"%-*.*s\"\n",fs->dataSize(),fs->dataSize(),fs->data());
  }
  else {
    printf("start/delta test failed.\n");
  }

  const unsigned char *pb = fs->lookup("cucumber");
  if(pb!=NULL){
    printf("lookup test: \"cucumber\" -> \"%s\"\n",pb);
  }
  else{
    printf("lookup test: \"cucumber\" not found.\n");
  }


  FSA::HashedState *fs1 = new FSA::HashedState(*f);


  fs1->delta("pe");

  FSA::HashedState *fs2 = new FSA::HashedState(*fs1);
  FSA::HashedState *fs3 = new FSA::HashedState(*fs1);



  fs1->delta("a");
  fs2->delta("ach");
  fs3->delta("ar");

  if(fs1->isFinal() && fs2->isFinal()){
    printf("copy hashed state test:\n");
    printf("    \"pe\"+\"a\":    hash=%d, data_size=%d, data string=\"%-*.*s\"\n",
           fs1->hash(),fs1->dataSize(),fs1->dataSize(),fs1->dataSize(),fs1->data());
    printf("    \"pe\"+\"ach\":  hash=%d, data_size=%d, data string=\"%-*.*s\"\n",
           fs2->hash(),fs2->dataSize(),fs2->dataSize(),fs2->dataSize(),fs2->data());
    printf("    \"pe\"+\"ar\":   hash=%d, data_size=%d, data string=\"%-*.*s\"\n",
           fs3->hash(),fs3->dataSize(),fs3->dataSize(),fs3->dataSize(),fs3->data());

  }
  else {
    printf("copy hashed state test failed.\n");
  }

  printf("revLookup test:\n");
  unsigned int i=0;
  std::string res;
  while(i<100){
    res=fs2->revLookup(i);
    if(res.size()==0)
      break;
    fs2->lookup(res);
    printf("    %d -> %s -> %d\n",i,res.c_str(),fs2->hash());
    i++;
  }

  printf("iterator test:\n");
  fs1->start('p');
  printf("  possible continuations from \"p\":\n");
  for(FSA::iterator it(*fs1); it!=fs1->end(); ++it){
    printf("    \"p\" + \"%s\"\n",it->str().c_str());
  }

  delete fs;
  delete fs1;
  delete fs2;
  delete fs3;


  printf("counter/memory state test\n");
  FSA::CounterState *cs = new FSA::CounterState(*f);
  FSA::MemoryState *ms = new FSA::MemoryState(*f);

  cs->start("cucu");
  ms->start("cucu");
  printf("    \"cucu\" -> %s:%d\n",ms->memory().c_str(),cs->counter());

  cs->start("cucumber");
  ms->start("cucumber");
  printf("    \"cucumber\" -> %s:%d\n",ms->memory().c_str(),cs->counter());

  cs->start("cucumber slumber");
  ms->start("cucumber slumber");
  printf("    \"cucumber slumber\" -> %s:%d\n",ms->memory().c_str(),cs->counter());

  delete cs;
  delete ms;
  delete f;

  return 0;
}