aboutsummaryrefslogtreecommitdiff
path: root/gfsm/gfsm/src/libgfsm/tests/pathtest.c
blob: 86a3ad3838e4fed8cbd471386ada1f23dec40d59 (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
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
#include <gfsm.h>


//#define USE_VTABLE 1
//#define USE_ALLOCATORS 1
//#define DELAY_ALLOCATOR_FREE 1

#define NEW_FST     1

//#define MAKE_SLIST 1
//#define MAKE_LIST 1
//#define MAKE_FST    1
//#define COMPILE_FST    1
#define LOAD_FST    1
//#define LOAD_EMPTY
//#define LOAD_NOFINAL

#define LOAD_NITERS 1
//#define LOAD_NITERS 10
//#define LOAD_NITERS 32768
//#define LOAD_NITERS 65535
//#define LOAD_NITERS 131072
//#define LOAD_NITERS 262144
//#define LOAD_NITERS 524288
//#define LOAD_NITERS 1048576


#define NEW_ABET    1
#define LOAD_ABET   1

//#define DO_INPUT    1
//#define DO_LOOKUP   1
//#define DO_PATHS    1
//#define DO_STRINGS  1
//#define DO_PTRARRAY 1

#define NITERS 0
//#define NITERS 1
//#define NITERS 10
//#define NITERS 1024
//#define NITERS 65536
//#define NITERS 131072
//#define NITERS 262144
//#define NITERS 524288
//#define NITERS 1048576

//#define PRINT_CHUNK_INFO 1
//#define DO_PROFILE 1

const char *progname = "pathtest";
const char *labfile  = "test.lab";

#if defined(LOAD_EMPTY)
const char *fstfile  = "empty.gfst";
const char *tfstfile  = "empty.tfst";
#elif defined(LOAD_NOFINAL)
const char *fstfile  = "nofinal.gfst";
const char *tfstfile  = "nofinal.tfst";
#else
const char *fstfile  = "lkptest.gfst";
const char *tfstfile  = "lkptest.tfst";
#endif


gfsmLabelVector *input = NULL;
gfsmAlphabet  *abet = NULL;
gfsmAutomaton *fst = NULL;
gfsmAutomaton *result = NULL;
gfsmSet       *paths = NULL;
GSList        *strings = NULL;
GPtrArray     *ptrarray = NULL;
gfsmError     *err = NULL;
GSList       *sltmp = NULL;
gfsmState     *st = NULL;
gfsmArc       *arc=NULL;
gfsmArcList   *al=NULL;
char line[256];


gpointer my_malloc(gsize n_bytes)
{
  return (gpointer)malloc(n_bytes);
}

gpointer my_realloc(gpointer mem, gsize n_bytes)
{
  return (gpointer)realloc(mem, n_bytes);
}

void my_free(gpointer mem)
{
  free(mem);
}

GMemVTable my_vtable = 
  {
    my_malloc,
    my_realloc,
    my_free,
    NULL,
    NULL,
    NULL
  };

#define MEMOP(code) \
  fprintf(stderr,"%s\n", #code); \
  code;

int main(int argc, char **argv) {
  int i;

  //-- memory debugging
#if defined(DO_PROFILE)
  g_mem_set_vtable(glib_mem_profiler_table);
#elif defined(USE_VTABLE)
  g_mem_set_vtable(&my_vtable);
#endif

  //-- setup gfsm allocators
#if defined(USE_ALLOCATORS)
  MEMOP(gfsm_allocators_enable());
#endif

  //-- load or make fst
#ifdef NEW_FST
  MEMOP(fst = gfsm_automaton_new(););

#if defined(MAKE_SLIST)
  //-- this is the culprit!
  MEMOP(al = g_slist_prepend(NULL,NULL));
  MEMOP(g_slist_free(al));
#elif defined(MAKE_LIST)
  {
    GList *l=NULL;
    MEMOP(l=g_list_prepend(NULL,NULL));
    MEMOP(g_list_free(l));
  }
#elif defined(MAKE_FST)
  MEMOP(st = gfsm_automaton_get_state(fst,0)); //-- ok
  MEMOP(gfsm_automaton_set_final_state(fst, 0, TRUE)); //-- ok

  MEMOP(gfsm_automaton_add_arc(fst,0,0,1,1,0)); //-- NOT ok!

  //-- alloc
  //MEMOP(arc=gfsm_arc_new_full(0,1,1,0)); //--ok
  //MEMOP(st->arcs = g_slist_prepend((gpointer)arc,st->arcs)); //-- ok w/ allocator

  //-- free
  //MEMOP(g_slist_free(st->arcs); st->arcs=NULL;); //-- ok w/ allocator
  //MEMOP(gfsm_arc_free(arc)); //-- /ok

#elif defined(COMPILE_FST)
  fprintf(stderr,"gfsm_automaton_compile_filename(\"%s\");\n", tfstfile);
  if (!gfsm_automaton_compile_filename(fst,tfstfile,&err)) {
    fprintf(stderr,"%s: compile failed for '%s': %s\n", progname, tfstfile, (err ? err->message : "?"));
    exit(3);
  }
  //g_mem_profile();
#elif defined(LOAD_FST)
  fprintf(stderr,"gfsm_automaton_load_bin_filename(\"%s\"); //---[x %d]---\n", fstfile, LOAD_NITERS);
  for (i=0; i < LOAD_NITERS; i++) {
    if (fst) gfsm_automaton_free(fst);
    fst = gfsm_automaton_new();
    if (!gfsm_automaton_load_bin_filename(fst,fstfile,&err)) {
      fprintf(stderr,"%s: load failed for '%s': %s\n", progname, fstfile, (err ? err->message : "?"));
      exit(3);
    }
    g_blow_chunks();
  }
  //g_mem_profile();
#endif // make or load FST
#endif // NEW_FST

  //-- load labels
#ifdef NEW_ABET
  MEMOP(abet = gfsm_string_alphabet_new(););
#ifdef LOAD_ABET
  fprintf(stderr,"gfsm_alphabet_load_filename(\"%s\");\n", labfile);
  if (!gfsm_alphabet_load_filename(abet,labfile,&err)) {
    fprintf(stderr,"%s: load failed for labels file '%s': %s\n",
	       progname, labfile, (err ? err->message : "?"));
    exit(2);
  }
  //g_mem_profile();
#endif //-- LOAD_ABET
#endif //-- NEW_ABET

  //-- setup input vector
#ifdef DO_INPUT
  MEMOP(input = g_ptr_array_new());
  MEMOP(g_ptr_array_add(input,(gpointer)2));
  MEMOP(g_ptr_array_add(input,(gpointer)2));
  MEMOP(g_ptr_array_add(input,(gpointer)3));
#endif //-- DEFINE_INPUT

  //-- guts
  fprintf(stderr, "\n--bench[%d] :lookup=%d, paths=%d, strings=%d, ptrarray=%d--\n\n",
	  NITERS,
#ifdef DO_LOOKUP
	  1
#else
	  0
#endif
	  ,
#ifdef DO_PATHS
	  1
#else
	  0
#endif
	  ,
#ifdef DO_STRINGS
	  1
#else
	  0
#endif
	  ,
#ifdef DO_PTRARRAY
	  1
#else
	  0
#endif
	  );

  for (i=0; i < NITERS; i++) {
#ifdef DO_LOOKUP
    result  = gfsm_automaton_lookup(fst, input, result);
#endif
#ifdef DO_PATHS
    paths   = gfsm_automaton_paths(result,paths);
#endif
#ifdef DO_STRINGS
    strings = gfsm_paths_to_strings(paths, abet, NULL, fst->sr, TRUE, TRUE, strings);
#endif
#ifdef DO_PTRARRAY
    ptrarray=g_ptr_array_sized_new(gfsm_set_size(paths));
    gfsm_set_to_ptr_array(paths, ptrarray);
#endif

    //-- cleanup
    for (sltmp=strings; sltmp; sltmp=sltmp->next) { g_free(sltmp->data);  }
    if (ptrarray) g_ptr_array_free(ptrarray,TRUE);
    if (strings) g_slist_free(strings);
    if (paths)   gfsm_set_clear(paths);
    g_blow_chunks();
  }

  //-- pop gfsm allocators (too early: segfaults)
  /*
#if defined(USE_ALLOCATORS) && !defined(DELAY_ALLOCATOR_FREE)
  MEMOP(gfsm_allocators_disable());
#endif
  */

  //-- cleanup
  if (result) { MEMOP(gfsm_automaton_free(result)); }
  if (paths)  { MEMOP(gfsm_set_free(paths)); }
  if (input) { MEMOP(g_ptr_array_free(input,TRUE)); }
  if (fst) { MEMOP(gfsm_automaton_free(fst)); }
  if (abet) { MEMOP(gfsm_alphabet_free(abet);); }

  //-- pop gfsm allocators
#if defined(USE_ALLOCATORS) && !defined(DELAY_ALLOCATOR_FREE)
  MEMOP(gfsm_allocators_free());
#endif

  //-- memory debugging
#ifdef PRINT_CHUNK_INFO
  printf("\n<CHUNKS:1>--------\n");
  g_blow_chunks();
  g_mem_chunk_info();
#endif
  //
#ifdef DO_PROFILE
  printf("\n<PROF:1>--------\n");
  g_blow_chunks();
  g_mem_profile();
#endif

#if defined(USE_ALLOCATORS) && defined(DELAY_ALLOCATOR_FREE)
  MEMOP(gfsm_allocators_free());
#endif

 {
   printf("OK to exit? ");
   scanf("%s", &line);
 }

  return 0;
}