File: test_eval_expression.c

package info (click to toggle)
gridengine 8.1.9%2Bdfsg-10
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 56,880 kB
  • sloc: ansic: 432,689; java: 87,068; cpp: 31,958; sh: 29,429; jsp: 7,757; perl: 6,336; xml: 5,828; makefile: 4,701; csh: 3,928; ruby: 2,221; tcl: 1,676; lisp: 669; yacc: 519; python: 503; lex: 361; javascript: 200
file content (222 lines) | stat: -rw-r--r-- 9,564 bytes parent folder | download | duplicates (6)
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
/*___INFO__MARK_BEGIN__*/
/*************************************************************************
 *
 *  The Contents of this file are made available subject to the terms of
 *  the Sun Industry Standards Source License Version 1.2
 *
 *  Sun Microsystems Inc., March, 2006
 *
 *
 *  Sun Industry Standards Source License Version 1.2
 *  =================================================
 *  The contents of this file are subject to the Sun Industry Standards
 *  Source License Version 1.2 (the "License"); You may not use this file
 *  except in compliance with the License. You may obtain a copy of the
 *  License at http://gridengine.sunsource.net/Gridengine_SISSL_license.html
 *
 *  Software provided under this License is provided on an "AS IS" basis,
 *  WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING,
 *  WITHOUT LIMITATION, WARRANTIES THAT THE SOFTWARE IS FREE OF DEFECTS,
 *  MERCHANTABLE, FIT FOR A PARTICULAR PURPOSE, OR NON-INFRINGING.
 *  See the License for the specific provisions governing your rights and
 *  obligations concerning the Software.
 *
 *   The Initial Developer of the Original Code is: Sun Microsystems, Inc.
 *
 *   Copyright: 2006 by Sun Microsystems, Inc.
 *
 *   All Rights Reserved.
 *
 ************************************************************************/
/*___INFO__MARK_END__*/

/*----------------------------------------------------
 *
 * The litte test program matches an "attribute" against
 * a regular exression which follows the above grammar and the
 * regular expression from the Grid Engine sge_types(1) man page
 * and prints "TRUE" or "FALSE"
 * For Expect test returns 0 .. TRUE
 *                         1 .. FALSE
 *                        -1 .. ERROR
 *
 *----------------------------------------------------*/
#include <stdio.h>
#include <string.h>
#include <strings.h>
#include <stdlib.h>
#include <string.h>

#include "uti/sge_rmon.h"
#include "uti/sge_bootstrap.h"
#include "uti/sge_string.h"

#include "sgeobj/sge_feature.h"
#include "sgeobj/sge_eval_expression.h"

#define T 0
#define F 1
#define ERROR -1
#define RESULT(match) (match == -1) ? "ERROR" : (match == 0) ? "TRUE" : "FALSE"

/* Local functions and variables */
static int test_match(u_long32 , const char *, const char *, int );
static int test_tolower( char *, char *, int );

/*-----------------------------------------------------------
 * call:   test_eval_expression or test_eval_expression expr value
 *         return 0, if all test are ok
 *-----------------------------------------------------------*/
int main(int argc, char *argv[]) {
   int ret;
   
   DENTER_MAIN(TOP_LAYER, "test_evel_expression");
   bootstrap_mt_init();
   feature_mt_init();

   ret = 0;
   if(argc!=3){
      ret=ret|test_tolower("TESTDATA*[]dda","TESTDATA*[]dda",T);
      ret=ret|test_tolower("TESTDATA*[]dda","testdata*[]dda",T);
      ret=ret|test_tolower("TESTDATA*[]dda","TESTDATA*[]DDA",T);

      /* Basic Tests */
      /* 1  a & b */
      ret=ret|test_match(TYPE_STR, "a & b", "a", F);
      ret=ret|test_match(TYPE_STR, "a & b", "b", F);
      ret=ret|test_match(TYPE_STR, "a* & b*", "a", F);
      ret=ret|test_match(TYPE_STR, "a* & b*", "b", F);
      /* 2  a & !b */
      ret=ret|test_match(TYPE_STR, "a & !b", "a", T);
      ret=ret|test_match(TYPE_STR, "a & !b", "b", F);
      ret=ret|test_match(TYPE_STR, "a* & !b*", "a", T);
      ret=ret|test_match(TYPE_STR, "a* & !b*", "b", F);
      /* 3  a*/
      ret=ret|test_match(TYPE_STR, "a", "a", T);
      ret=ret|test_match(TYPE_STR, "a*", "a", T);
      /* 4 !a & b */
      ret=ret|test_match(TYPE_STR, "!a & b", "a", F);
      ret=ret|test_match(TYPE_STR, "!a & b", "b", T);
      ret=ret|test_match(TYPE_STR, "!a* & b*", "a", F);
      ret=ret|test_match(TYPE_STR, "!a* & b*", "b", T);
      /* 6 (!a & b) | (a & !b) */
      ret=ret|test_match(TYPE_STR, "(!a & b) | (a & !b)", "a", T);
      ret=ret|test_match(TYPE_STR, "(!a & b) | (a & !b)", "b", T);
      ret=ret|test_match(TYPE_STR, "(!a* & b*) | (a* & !b*)", "a", T);
      ret=ret|test_match(TYPE_STR, "(!a* & b*) | (a* & !b*)", "b", T);
      /* 7 a | b */
      ret=ret|test_match(TYPE_STR, "a | b", "a", T);
      ret=ret|test_match(TYPE_STR, "a | b", "b", T);
      ret=ret|test_match(TYPE_STR, "a* | b*", "a", T);
      ret=ret|test_match(TYPE_STR, "a* | b*", "b", T);
      /* 8 !(a | b) */
      ret=ret|test_match(TYPE_STR, "!(a | b)", "a", F);
      ret=ret|test_match(TYPE_STR, "!(a | b)", "a", F);
      ret=ret|test_match(TYPE_STR, "!(a* | b*)", "a", F);
      ret=ret|test_match(TYPE_STR, "!(a* | b*)", "a", F);
      /* 9  (!a | b) & (a | !b) */
      ret=ret|test_match(TYPE_STR, "(!a | b) & (a | !b)", "a", F);
      ret=ret|test_match(TYPE_STR, "(!a | b) & (a | !b)", "b", F);
      ret=ret|test_match(TYPE_STR, "(!a* | b*) & (a* | !b*)", "a", F);
      ret=ret|test_match(TYPE_STR, "(!a* | b*) & (a* | !b*)", "b", F);
      /* 11  a | !b */
      ret=ret|test_match(TYPE_STR, "a | !b", "a", T);
      ret=ret|test_match(TYPE_STR, "a | !b", "b", F);
      ret=ret|test_match(TYPE_STR, "a* | !b*", "a", T);
      ret=ret|test_match(TYPE_STR, "a* | !b*", "b", F);
      /* 13  !a | b */
      ret=ret|test_match(TYPE_STR, "!a | b", "a", F);
      ret=ret|test_match(TYPE_STR, "!a | b", "b", T);
      ret=ret|test_match(TYPE_STR, "!a* | b*", "a", F);
      ret=ret|test_match(TYPE_STR, "!a* | b*", "b", T);
      /* 14  !(a & b) */
      ret=ret|test_match(TYPE_STR, "!(a & b)", "a", T);
      ret=ret|test_match(TYPE_STR, "!(a & b)", "b", T);
      ret=ret|test_match(TYPE_STR, "!(a* & b*)", "a", T);
      ret=ret|test_match(TYPE_STR, "!(a* & b*)", "b", T);
      
      /* Regular tests */
      ret=ret|test_match(TYPE_CSTR, "solaris", "solaris", T);
      ret=ret|test_match(TYPE_CSTR, "!solaris", "solaris", F);
      ret=ret|test_match(TYPE_CSTR, "*amd64&sol*", "sol-amd64", T);
      ret=ret|test_match(TYPE_CSTR, "(sol-*64|linux|hp*)&!sol-sparc", "hp11", T);
      ret=ret|test_match(TYPE_CSTR, "(sol-*64|linux|hp*)&!sol-sparc", "sol-sparc64", T);
      ret=ret|test_match(TYPE_CSTR, "(sol-*64|linux|hp*)&!sol-sparc", "sol-sparc", F);
      
      ret=ret|test_match(TYPE_CSTR, "!(sola*|lin*|hp*)&!sola*&!*sparc64&(!sole*|!lin*|!hp*)", "sol-sparc", T);
      
      ret=ret|test_match(TYPE_CSTR, "(((test)))", "test", T);
      ret=ret|test_match(TYPE_CSTR, "(((test)&pet*))", "test", F);
      /* Errors tests */
      ret=ret|test_match(TYPE_STR, "(sol-*64|linux|hp*)&!sol-sparc!&", "sol-sparc", ERROR);
      ret=ret|test_match(TYPE_STR, "a b c", "      ", F);
      ret=ret|test_match(TYPE_STR, "a|b c", "a", ERROR);
      ret=ret|test_match(TYPE_STR, "a&", "a", ERROR);
      ret=ret|test_match(TYPE_STR, "a|", "a", ERROR);
      ret=ret|test_match(TYPE_STR, "a&a&", "a", ERROR);
      ret=ret|test_match(TYPE_STR, "a|a|", "a", ERROR);
      ret=ret|test_match(TYPE_STR, "(a b c", "a", ERROR);
      ret=ret|test_match(TYPE_STR, "a)&b", "a", ERROR);
      ret=ret|test_match(TYPE_STR, "(a)&b)|c", "a", ERROR);
      /* test for case sensitive */

      ret=ret|test_match(TYPE_CSTR, "a", "A", T);
      ret=ret|test_match(TYPE_CSTR, "A", "a", T);
      ret=ret|test_match(TYPE_CSTR, "a*", "A", T);
      ret=ret|test_match(TYPE_CSTR, "A*", "a", T);
      ret=ret|test_match(TYPE_CSTR, "a&b|a", "A", T);
      ret=ret|test_match(TYPE_CSTR, "A&B|A", "a", T);
      ret=ret|test_match(TYPE_CSTR, "a*&b*|a*", "A", T);
      ret=ret|test_match(TYPE_CSTR, "A*&B*|A*", "a", T);
      /* test for host names */
      ret=ret|test_match(TYPE_HOST, "Latte*", "latte3.czech.sun.com", T);
      ret=ret|test_match(TYPE_HOST, "latte* & !*3.czech.sun.com", "latte3.czech.sun.com", F);
      ret=ret|test_match(TYPE_HOST, "Latte* | Mocca*", "latte3.czech.sun.com", T);
      ret=ret|test_match(TYPE_HOST, "!(a*|b*|c*|d*|e*|f*|g*|h*|i*|j*|k*|l*|m*|n*|o*|p*|q*|r*|s*|t*|u*|v*|w*|x*|y*|z*|baaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa*)", "bla", F);

      
      fprintf(stdout, "For evaluation a single expression try: test_eval_expression <expr> <value> \n");
      fprintf(stdout, "Complex set of tests result is: %s \n", RESULT(ret));
   } else {
      ret=sge_eval_expression(TYPE_RESTR, argv[1], argv[2], NULL);
      fprintf(stdout, "eval_expr(%s,%s) => %s\n", argv[1], argv[2], RESULT(ret) );
   }
   
   DEXIT;
   return ret;
}

static int test_match(u_long32 type, const char *expression, const char *value, int expected) {
   int match;
   match = sge_eval_expression(type, expression, value, NULL);
   if(match!=expected) {
      fprintf(stderr, "!!!UNEXPECTED RESULT!!!: %s => eval_expr(%s,%s), expected: %s \n",
      RESULT(match) , expression, value, RESULT(expected) );
      return 1;
   } /* else {
    fprintf(stdout, "eval_expr(%s,%s) => %s\n", expression, value, RESULT(match) );
    }  */
   return 0;
}

static int test_tolower(char *expression, char *value, int expected) {
   int match;
   char *t1;
   char *t2;
   t1=strdup(expression);
   t2=strdup(value);
   sge_strtolower(t1,255);
   sge_strtolower(t2,255);
   match = strcmp(t1,t2);
   sge_free(&t1);
   sge_free(&t2);
   if(match!=expected) {
      fprintf(stderr, "!!!UNEXPECTED RESULT!!!: %s => strcmp(sge_strtolower(%s),sge_strtolower(%s)), expected: %s \n",
      RESULT(match) , expression, value, RESULT(expected) );
      return 1;
   } /* else {
    fprintf(stdout, "eval_expr(%s,%s) => %s\n", expression, value, RESULT(match) );
    }  */
   return 0;
}