~ubuntu-branches/ubuntu/precise/code-saturne/precise

« back to all changes in this revision

Viewing changes to tests/mei_test_main.c

  • Committer: Package Import Robot
  • Author(s): Sylvestre Ledru
  • Date: 2011-11-24 00:00:08 UTC
  • mfrom: (6.1.9 sid)
  • Revision ID: package-import@ubuntu.com-20111124000008-2vo99e38267942q5
Tags: 2.1.0-3
Install a missing file

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*============================================================================
 
2
 * Test program for mei
 
3
 *============================================================================*/
 
4
 
 
5
/*
 
6
  This file is part of Code_Saturne, a general-purpose CFD tool.
 
7
 
 
8
  Copyright (C) 1998-2011 EDF S.A.
 
9
 
 
10
  This program is free software; you can redistribute it and/or modify it under
 
11
  the terms of the GNU General Public License as published by the Free Software
 
12
  Foundation; either version 2 of the License, or (at your option) any later
 
13
  version.
 
14
 
 
15
  This program is distributed in the hope that it will be useful, but WITHOUT
 
16
  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 
17
  FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
 
18
  details.
 
19
 
 
20
  You should have received a copy of the GNU General Public License along with
 
21
  this program; if not, write to the Free Software Foundation, Inc., 51 Franklin
 
22
  Street, Fifth Floor, Boston, MA 02110-1301, USA.
 
23
*/
 
24
 
 
25
/*----------------------------------------------------------------------------*/
 
26
 
 
27
/*----------------------------------------------------------------------------
 
28
 * Standard C library headers
 
29
 *----------------------------------------------------------------------------*/
 
30
 
 
31
#include <stdio.h>
 
32
#include <string.h>
 
33
#include <stdlib.h>
 
34
 
 
35
/*----------------------------------------------------------------------------
 
36
 * BFT library headers
 
37
 *----------------------------------------------------------------------------*/
 
38
 
 
39
#include <bft_mem_usage.h>
 
40
#include <bft_mem.h>
 
41
#include <bft_error.h>
 
42
 
 
43
/*----------------------------------------------------------------------------
 
44
 *  Local headers
 
45
 *----------------------------------------------------------------------------*/
 
46
 
 
47
#include "mei_evaluate.h"
 
48
 
 
49
/*============================================================================
 
50
 * External function prototype
 
51
 *============================================================================*/
 
52
 
 
53
extern int graphik(mei_node_t*);
 
54
 
 
55
/*============================================================================
 
56
 * Private function definitions
 
57
 *============================================================================*/
 
58
 
 
59
/*----------------------------------------------------------------------------
 
60
 * Handle errors
 
61
 *----------------------------------------------------------------------------*/
 
62
 
 
63
static void
 
64
_base_error_handler(const char  *file_name,
 
65
                    int          line_num,
 
66
                    int          sys_err_code,
 
67
                    const char  *format,
 
68
                    va_list      arg_ptr)
 
69
{
 
70
  fflush(stdout);
 
71
  fflush(stderr);
 
72
 
 
73
  fprintf(stderr, "\n");
 
74
 
 
75
  if (sys_err_code != 0)
 
76
    fprintf(stderr, "\nSystem error: %s\n", strerror(sys_err_code));
 
77
 
 
78
  fprintf(stderr, "\n%s:%d: Fatal error.\n\n", file_name, line_num);
 
79
 
 
80
  vfprintf(stderr, format, arg_ptr);
 
81
 
 
82
  fprintf(stderr, "\n\n");
 
83
 
 
84
  exit(EXIT_FAILURE);
 
85
}
 
86
 
 
87
/*----------------------------------------------------------------------------
 
88
 * Initialize memory handling
 
89
 *----------------------------------------------------------------------------*/
 
90
 
 
91
static void
 
92
_base_mem_init(void)
 
93
{
 
94
  char  *log_name = NULL;
 
95
 
 
96
  /* Initialization of memory usage count */
 
97
 
 
98
  bft_mem_usage_init();
 
99
 
 
100
  /* Initialization of memory management */
 
101
 
 
102
  log_name = getenv("CS_FIC_MEM");
 
103
 
 
104
  bft_mem_init(log_name);
 
105
}
 
106
 
 
107
/*----------------------------------------------------------------------------
 
108
 * Finalize memory handling
 
109
 *----------------------------------------------------------------------------*/
 
110
 
 
111
static void
 
112
_base_mem_finalize(void)
 
113
{
 
114
  int        ind_bil, itot;
 
115
  double     valreal[2];
 
116
 
 
117
  int        ind_val[2] = {1, 1};
 
118
  char       unit[]    = {'k', 'm', 'g', 't', 'p'};
 
119
 
 
120
  const char  * type_bil[] = {"Total measured memory usage:            ",
 
121
                              "Theoretical instrumented dynamic memory:"};
 
122
 
 
123
  /* Memory summary */
 
124
 
 
125
  printf("\nMemory usage summary:\n\n");
 
126
 
 
127
  valreal[0] = (double) bft_mem_usage_max_pr_size();
 
128
  valreal[1] = (double) bft_mem_size_max();
 
129
 
 
130
  /* We will ignore non-consistent measures */
 
131
 
 
132
  if (valreal[2] < valreal[1] || valreal[2] < valreal[3])
 
133
    ind_val[2] = 0;
 
134
 
 
135
  for (ind_bil = 0; ind_bil < 2; ind_bil++) {
 
136
    if (valreal[ind_bil] < 1.0)
 
137
      ind_val[ind_bil] = 0;
 
138
  }
 
139
 
 
140
 /* Similar handling for several instrumentation methods */
 
141
 
 
142
  for (ind_bil = 0; ind_bil < 2; ind_bil++) {
 
143
 
 
144
    /* If an instrumentation method returns an apparently consistent
 
145
       result, print it. */
 
146
 
 
147
    if (ind_val[ind_bil] == 1) {
 
148
 
 
149
      for (itot = 0;
 
150
           valreal[ind_bil] > 1024. && unit[itot] != 'p';
 
151
           itot++)
 
152
        valreal[ind_bil] /= 1024.;
 
153
 
 
154
      /* Impressions */
 
155
 
 
156
      printf ("  %s %12.3f %co\n",
 
157
              type_bil[ind_bil], valreal[ind_bil], unit[itot]);
 
158
 
 
159
    }
 
160
  }
 
161
 
 
162
  /* Finalize memory handling */
 
163
 
 
164
  bft_mem_end();
 
165
 
 
166
  /* Finalize memory usage count */
 
167
 
 
168
  bft_mem_usage_end();
 
169
}
 
170
 
 
171
/*============================================================================
 
172
 * Main program
 
173
 *============================================================================*/
 
174
 
 
175
int main(void)
 
176
{
 
177
  int iok;
 
178
  const char *v[] = {"X", "Y", "Z"};
 
179
  const char *w[] = {"yy", "zz"};
 
180
 
 
181
  mei_tree_t *e1;
 
182
  mei_tree_t *e2;
 
183
  hash_table_t *sym;
 
184
 
 
185
  /* Initialize memory handling */
 
186
 
 
187
  _base_mem_init();
 
188
  bft_error_handler_set(_base_error_handler);
 
189
 
 
190
  /* Check functionality */
 
191
  /*---------------------*/
 
192
 
 
193
  printf("\n------------------------------------------------------------------\n");
 
194
 
 
195
  /* return two empty interpreter */
 
196
 
 
197
  e1 = mei_tree_new("x=(+2.5); y=8.7; zz=y+x; yy  = zz+0.8; \n");
 
198
  e2 = mei_tree_new("y = cos(-pi) ; tt = K+y ; abs(tt);");
 
199
 
 
200
 
 
201
  /* try to build the two interpreter */
 
202
 
 
203
  printf("\nBuilding interpreter for: %s\n", "x=(+2.5); y=8.7; zz=y+x; yy  = zz+0.8; \n");
 
204
  if (mei_tree_builder(e1)) {
 
205
    mei_tree_destroy(e1);
 
206
    printf("failed...\n");
 
207
  } else
 
208
    printf("OK\n");
 
209
 
 
210
  printf("\n------------------------------------------------------------------\n");
 
211
 
 
212
  printf("\nBuilding interpreter for: %s\n", e2->string);
 
213
  if (mei_tree_builder(e2)) {
 
214
    mei_tree_destroy(e2);
 
215
    printf("failed...\n");
 
216
  } else
 
217
    printf("OK\n");
 
218
 
 
219
  printf("\n------------------------------------------------------------------\n");
 
220
 
 
221
  /* complete the symbol table with required */
 
222
 
 
223
  /* return an empty interpreter */
 
224
  e2 = mei_tree_new("y = cos(-pi) ; tt = K+y ; abs(tt);");
 
225
 
 
226
  mei_tree_insert(e2, "K", -5.3e2);
 
227
 
 
228
  /* try to re-build the interpreter --> memory problem */
 
229
 
 
230
  printf("\nBuilding interpreter for: %s\n", e2->string);
 
231
  if (mei_tree_builder(e2)) {
 
232
    mei_tree_destroy(e2);
 
233
    printf("failed...\n");
 
234
  } else
 
235
    printf("OK\n");
 
236
 
 
237
  printf("\n------------------------------------------------------------------\n");
 
238
 
 
239
  printf("\nFind symbols in: %s\n", e1->string);
 
240
 
 
241
  printf("Try to find %s\n", "toto");
 
242
 
 
243
  if (mei_tree_find_symbol(e1, "toto"))
 
244
    printf("not found...\n");
 
245
  else
 
246
    printf("found\n");
 
247
 
 
248
  printf("Try to find %s\n", "yy");
 
249
 
 
250
  if (mei_tree_find_symbol(e1, "yy"))
 
251
    printf("not found...\n");
 
252
  else
 
253
    printf("found\n");
 
254
 
 
255
  printf("\nFind symbols in: %s\n", e1->string);
 
256
 
 
257
  printf("Try to find X Y Z\n");
 
258
 
 
259
  if (mei_tree_find_symbols(e1, 3, v))
 
260
    printf("not found...\n");
 
261
  else
 
262
    printf("found\n");
 
263
 
 
264
  printf("Try to find yy zz\n");
 
265
 
 
266
  if (mei_tree_find_symbols(e1, 2, w))
 
267
    printf("not found...\n");
 
268
  else
 
269
    printf("found\n");
 
270
 
 
271
  printf("\n------------------------------------------------------------------\n");
 
272
 
 
273
  printf("\nInterpret expression: \n%s\n", e1->string);
 
274
  mei_evaluate(e1);
 
275
  printf("Evaluate: %s = %f\n", "zz", mei_tree_lookup(e1, "zz"));
 
276
  printf("Evaluate: %s = %f\n", "yy", mei_tree_lookup(e1, "yy"));
 
277
 
 
278
  printf("\nInterpret expression: \n%s\n", e2->string);
 
279
  printf("Evaluate: [%s] = %f\n", e2->string, mei_evaluate(e2));
 
280
 
 
281
  mei_tree_destroy(e1);
 
282
  mei_tree_destroy(e2);
 
283
 
 
284
  printf("\n------------------------------------------------------------------\n");
 
285
 
 
286
  printf("\nBuild a shared table of symbols: \n");
 
287
 
 
288
  sym = mei_table_symbols_new();
 
289
  mei_symbol_table_insert(sym, "x", 3.2);
 
290
  e1 = mei_tree_new_with_shared_symbols("y= 0.8; x+y;", sym);
 
291
 
 
292
  if (!mei_tree_builder(e1)) {
 
293
    /* graphik(e1->node); */
 
294
    printf("\nExpression: \n%s\n", e1->string);
 
295
    printf("Evaluate: %f\n", mei_evaluate(e1));
 
296
  }
 
297
 
 
298
  mei_tree_insert(e1, "x", 1.2);
 
299
  e2 = mei_tree_new_with_shared_symbols("z=0.6; k = x*2; k+z;", sym);
 
300
 
 
301
  if (!mei_tree_builder(e2)) {
 
302
    graphik(e2->node);
 
303
    printf("\nExpression: \n%s\n", e2->string);
 
304
    printf("Evaluate: %f\n", mei_evaluate(e2));
 
305
  }
 
306
 
 
307
  mei_tree_destroy(e1);
 
308
  mei_tree_destroy(e2);
 
309
 
 
310
  printf("\n------------------------------------------------------------------\n");
 
311
 
 
312
  e1 = mei_tree_new("abc = -1.1; \nwhile (abc < 3) {\n  print abc;\n  abc = abc + 1;\n};");
 
313
  if (!mei_tree_builder(e1)) {
 
314
    graphik(e1->node);
 
315
    printf("\nExpression: \n%s\n", e1->string);
 
316
    printf("Evaluate: %f\n", mei_evaluate(e1));
 
317
  }
 
318
  mei_tree_destroy(e1);
 
319
 
 
320
 
 
321
  printf("\n------------------------------------------------------------------\n");
 
322
 
 
323
  /* Check error detection */
 
324
  /*-----------------------*/
 
325
 
 
326
  e1 = mei_tree_new("x = A+3; A=5;");
 
327
  printf("\nExpression: \n%s\n", e1->string);
 
328
  iok = mei_tree_builder(e1);
 
329
  mei_tree_destroy(e1);
 
330
 
 
331
  printf("\n------------------------------------------------------------------\n");
 
332
 
 
333
  e1 = mei_tree_new("cst = 4; # toto \n u = cos(cst);");
 
334
  printf("\nExpression: \n%s\n", e1->string);
 
335
  if (!mei_tree_builder(e1)) {
 
336
    mei_evaluate(e1);
 
337
    printf("Evaluate: u = %f\n", mei_tree_lookup(e1, "u"));
 
338
  }
 
339
  mei_tree_destroy(e1);
 
340
 
 
341
  printf("\n------------------------------------------------------------------\n");
 
342
 
 
343
  e1 = mei_tree_new("%u = cos(pi);");
 
344
  printf("\nInterpret expression: \n%s\n", e1->string);
 
345
  if (!mei_tree_builder(e1)) {
 
346
    mei_evaluate(e1);
 
347
    printf("Evaluate: u = %f\n", mei_tree_lookup(e1, "u"));
 
348
  }
 
349
  mei_tree_destroy(e1);
 
350
 
 
351
  printf("\n------------------------------------------------------------------\n");
 
352
 
 
353
  e1 = mei_tree_new("v = max(pi,+3);");
 
354
  printf("\nInterpret expression: \n%s\n", e1->string);
 
355
  if (!mei_tree_builder(e1)) {
 
356
    graphik(e1->node);
 
357
    mei_evaluate(e1);
 
358
    printf("Evaluate: v = %f\n", mei_tree_lookup(e1, "v"));
 
359
  }
 
360
  mei_tree_destroy(e1);
 
361
 
 
362
  printf("\n------------------------------------------------------------------\n");
 
363
 
 
364
  e1 = mei_tree_new("v = 1+2*+3;");
 
365
  printf("\nInterpret expression: \n%s\n", e1->string);
 
366
  if (!mei_tree_builder(e1)) {
 
367
    graphik(e1->node);
 
368
    mei_evaluate(e1);
 
369
    printf("Evaluate: v = %f\n", mei_tree_lookup(e1, "v"));
 
370
  }
 
371
  mei_tree_destroy(e1);
 
372
 
 
373
  printf("\n------------------------------------------------------------------\n");
 
374
 
 
375
  e1 = mei_tree_new("v = +1000.9 -( 0.049245 +0.0041579*Temp.C)*Temp.C;");
 
376
  mei_tree_insert(e1, "Temp.C", 18.0);
 
377
 
 
378
  printf("\nInterpret expression: \n%s\n", e1->string);
 
379
  if (!mei_tree_builder(e1)) {
 
380
    graphik(e1->node);
 
381
    mei_evaluate(e1);
 
382
    printf("Evaluate: v = %f\n", mei_tree_lookup(e1, "v"));
 
383
  }
 
384
  mei_tree_destroy(e1);
 
385
 
 
386
  printf("\n------------------------------------------------------------------\n");
 
387
 
 
388
  e1 = mei_tree_new("v = 1.5652e-3 + (-3.3003e-5 + 2.5135e-7*Temp-C)*Temp-C;");
 
389
  mei_tree_insert(e1, "Temp-C", 18.0);
 
390
 
 
391
  printf("\nInterpret expression: \n%s\n", e1->string);
 
392
  if (!mei_tree_builder(e1)) {
 
393
    graphik(e1->node);
 
394
    mei_evaluate(e1);
 
395
    printf("Evaluate: v = %f\n", mei_tree_lookup(e1, "v"));
 
396
  }
 
397
  mei_tree_destroy(e1);
 
398
 
 
399
  printf("\n------------------------------------------------------------------\n");
 
400
 
 
401
  e1 = mei_tree_new("v = 1.3806e-4 + 3.1768e-7*Temp_C;");
 
402
  mei_tree_insert(e1, "Temp_C", 18.0);
 
403
 
 
404
  printf("\nInterpret expression: \n%s\n", e1->string);
 
405
  if (!mei_tree_builder(e1)) {
 
406
    graphik(e1->node);
 
407
    mei_evaluate(e1);
 
408
    printf("Evaluate: v = %f\n", mei_tree_lookup(e1, "v"));
 
409
  }
 
410
  mei_tree_destroy(e1);
 
411
 
 
412
 
 
413
  printf("\n------------------------------------------------------------------\n");
 
414
 
 
415
  /* http://www.cs.mtu.edu/~shene/COURSES/cs201/NOTES/chap03/logical.html */
 
416
 
 
417
  e1 = mei_tree_new("v = ! Something && Another;");
 
418
  mei_tree_insert(e1, "Something", 1);
 
419
  mei_tree_insert(e1, "Another", 0);
 
420
 
 
421
  printf("\nInterpret expression: \n%s\n", e1->string);
 
422
  if (!mei_tree_builder(e1)) {
 
423
    graphik(e1->node);
 
424
    mei_evaluate(e1);
 
425
    printf("Evaluate: v = %f\n", mei_tree_lookup(e1, "v"));
 
426
    if (mei_tree_lookup(e1, "v")) printf("Test failed");
 
427
  }
 
428
  mei_tree_destroy(e1);
 
429
 
 
430
  printf("\n------------------------------------------------------------------\n");
 
431
 
 
432
  /* http://www.cs.mtu.edu/~shene/COURSES/cs201/NOTES/chap03/logical.html */
 
433
 
 
434
  e1 = mei_tree_new("v = ! (Something && Another);");
 
435
  mei_tree_insert(e1, "Something", 1);
 
436
  mei_tree_insert(e1, "Another", 0);
 
437
 
 
438
  printf("\nInterpret expression: \n%s\n", e1->string);
 
439
  if (!mei_tree_builder(e1)) {
 
440
    graphik(e1->node);
 
441
    mei_evaluate(e1);
 
442
    printf("Evaluate: v = %f\n", mei_tree_lookup(e1, "v"));
 
443
    if (!mei_tree_lookup(e1, "v")) printf("Test failed");
 
444
  }
 
445
  mei_tree_destroy(e1);
 
446
 
 
447
  printf("\n------------------------------------------------------------------\n");
 
448
 
 
449
  /* http://www.cs.mtu.edu/~shene/COURSES/cs201/NOTES/chap03/logical.html */
 
450
 
 
451
  e1 = mei_tree_new("v = ! a || ! b && c;");
 
452
  mei_tree_insert(e1, "a", 1);
 
453
  mei_tree_insert(e1, "b", 1);
 
454
  mei_tree_insert(e1, "c", 0);
 
455
 
 
456
  printf("\nInterpret expression: \n%s\n", e1->string);
 
457
  if (!mei_tree_builder(e1)) {
 
458
    graphik(e1->node);
 
459
    mei_evaluate(e1);
 
460
    printf("Evaluate: v = %f\n", mei_tree_lookup(e1, "v"));
 
461
    if (mei_tree_lookup(e1, "v")) printf("Test failed");
 
462
  }
 
463
  mei_tree_destroy(e1);
 
464
 
 
465
  printf("\n------------------------------------------------------------------\n");
 
466
 
 
467
  /* http://www.cs.mtu.edu/~shene/COURSES/cs201/NOTES/chap03/logical.html */
 
468
 
 
469
  e1 = mei_tree_new("v = n^2 + 1 > 10 && ! n < 3;");
 
470
  mei_tree_insert(e1, "n", 4);
 
471
 
 
472
  printf("\nInterpret expression: \n%s\n", e1->string);
 
473
  if (!mei_tree_builder(e1)) {
 
474
    graphik(e1->node);
 
475
    mei_evaluate(e1);
 
476
    printf("Evaluate: v = %f\n", mei_tree_lookup(e1, "v"));
 
477
    if (!mei_tree_lookup(e1, "v")) printf("Test failed");
 
478
  }
 
479
  mei_tree_destroy(e1);
 
480
 
 
481
  printf("\n------------------------------------------------------------------\n");
 
482
 
 
483
  printf("\n--------- WARNING: this test must be the last one ----------------\n");
 
484
  printf("\n--------- because it is corrupted the parser.     ----------------\n");
 
485
 
 
486
  e1 = mei_tree_new("cst = (2*(pi)/2.0); u = coos(cst); c=uu;");
 
487
  printf("\nExpression: \n%s\n", e1->string);
 
488
  iok = mei_tree_builder(e1);
 
489
  mei_tree_destroy(e1);
 
490
 
 
491
  printf("\n------------------------------------------------------------------\n");
 
492
 
 
493
  /* Finalization of memory management */
 
494
  _base_mem_finalize();
 
495
 
 
496
  return 0;
 
497
}
 
498