BadVPN – Blame information for rev 1
?pathlinks?
Rev | Author | Line No. | Line |
---|---|---|---|
1 | office | 1 | /** |
2 | * @file predicate_test.c |
||
3 | * @author Ambroz Bizjak <ambrop7@gmail.com> |
||
4 | * |
||
5 | * @section LICENSE |
||
6 | * |
||
7 | * Redistribution and use in source and binary forms, with or without |
||
8 | * modification, are permitted provided that the following conditions are met: |
||
9 | * 1. Redistributions of source code must retain the above copyright |
||
10 | * notice, this list of conditions and the following disclaimer. |
||
11 | * 2. Redistributions in binary form must reproduce the above copyright |
||
12 | * notice, this list of conditions and the following disclaimer in the |
||
13 | * documentation and/or other materials provided with the distribution. |
||
14 | * 3. Neither the name of the author nor the |
||
15 | * names of its contributors may be used to endorse or promote products |
||
16 | * derived from this software without specific prior written permission. |
||
17 | * |
||
18 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND |
||
19 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
||
20 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
||
21 | * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY |
||
22 | * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
||
23 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
||
24 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND |
||
25 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
||
26 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
||
27 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
||
28 | */ |
||
29 | |||
30 | #include <stdio.h> |
||
31 | #include <string.h> |
||
32 | |||
33 | #include <predicate/BPredicate.h> |
||
34 | #include <base/BLog.h> |
||
35 | |||
36 | static int func_hello (void *user, void **args) |
||
37 | { |
||
38 | return 1; |
||
39 | } |
||
40 | |||
41 | static int func_neg (void *user, void **args) |
||
42 | { |
||
43 | int arg = *((int *)args[0]); |
||
44 | |||
45 | return !arg; |
||
46 | } |
||
47 | |||
48 | static int func_conj (void *user, void **args) |
||
49 | { |
||
50 | int arg1 = *((int *)args[0]); |
||
51 | int arg2 = *((int *)args[1]); |
||
52 | |||
53 | return (arg1 && arg2); |
||
54 | } |
||
55 | |||
56 | static int func_strcmp (void *user, void **args) |
||
57 | { |
||
58 | char *arg1 = (char *)args[0]; |
||
59 | char *arg2 = (char *)args[1]; |
||
60 | |||
61 | return (!strcmp(arg1, arg2)); |
||
62 | } |
||
63 | |||
64 | static int func_error (void *user, void **args) |
||
65 | { |
||
66 | return -1; |
||
67 | } |
||
68 | |||
69 | int main (int argc, char **argv) |
||
70 | { |
||
71 | if (argc != 2) { |
||
72 | fprintf(stderr, "Usage: %s <predicate>\n", argv[0]); |
||
73 | return 1; |
||
74 | } |
||
75 | |||
76 | // init logger |
||
77 | BLog_InitStdout(); |
||
78 | |||
79 | // init predicate |
||
80 | BPredicate pr; |
||
81 | if (!BPredicate_Init(&pr, argv[1])) { |
||
82 | fprintf(stderr, "BPredicate_Init failed\n"); |
||
83 | return 1; |
||
84 | } |
||
85 | |||
86 | // init functions |
||
87 | BPredicateFunction f_hello; |
||
88 | BPredicateFunction_Init(&f_hello, &pr, "hello", NULL, 0, func_hello, NULL); |
||
89 | int arr1[] = {PREDICATE_TYPE_BOOL}; |
||
90 | BPredicateFunction f_neg; |
||
91 | BPredicateFunction_Init(&f_neg, &pr, "neg", arr1, 1, func_neg, NULL); |
||
92 | int arr2[] = {PREDICATE_TYPE_BOOL, PREDICATE_TYPE_BOOL}; |
||
93 | BPredicateFunction f_conj; |
||
94 | BPredicateFunction_Init(&f_conj, &pr, "conj", arr2, 2, func_conj, NULL); |
||
95 | int arr3[] = {PREDICATE_TYPE_STRING, PREDICATE_TYPE_STRING}; |
||
96 | BPredicateFunction f_strcmp; |
||
97 | BPredicateFunction_Init(&f_strcmp, &pr, "strcmp", arr3, 2, func_strcmp, NULL); |
||
98 | BPredicateFunction f_error; |
||
99 | BPredicateFunction_Init(&f_error, &pr, "error", NULL, 0, func_error, NULL); |
||
100 | |||
101 | // evaluate |
||
102 | int result = BPredicate_Eval(&pr); |
||
103 | printf("%d\n", result); |
||
104 | |||
105 | // free functions |
||
106 | BPredicateFunction_Free(&f_hello); |
||
107 | BPredicateFunction_Free(&f_neg); |
||
108 | BPredicateFunction_Free(&f_conj); |
||
109 | BPredicateFunction_Free(&f_strcmp); |
||
110 | BPredicateFunction_Free(&f_error); |
||
111 | |||
112 | // free predicate |
||
113 | BPredicate_Free(&pr); |
||
114 | |||
115 | return 0; |
||
116 | } |