predicate_test.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. /**
  2. * @file predicate_test.c
  3. * @author Ambroz Bizjak <ambrop7@gmail.com>
  4. *
  5. * @section LICENSE
  6. *
  7. * This file is part of BadVPN.
  8. *
  9. * BadVPN is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License version 2
  11. * as published by the Free Software Foundation.
  12. *
  13. * BadVPN is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License along
  19. * with this program; if not, write to the Free Software Foundation, Inc.,
  20. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  21. */
  22. #include <stdio.h>
  23. #include <string.h>
  24. #include <predicate/BPredicate.h>
  25. #include <system/BLog.h>
  26. static int func_hello (void *user, void **args)
  27. {
  28. return 1;
  29. }
  30. static int func_neg (void *user, void **args)
  31. {
  32. int arg = *((int *)args[0]);
  33. return !arg;
  34. }
  35. static int func_conj (void *user, void **args)
  36. {
  37. int arg1 = *((int *)args[0]);
  38. int arg2 = *((int *)args[1]);
  39. return (arg1 && arg2);
  40. }
  41. static int func_strcmp (void *user, void **args)
  42. {
  43. char *arg1 = args[0];
  44. char *arg2 = args[1];
  45. return (!strcmp(arg1, arg2));
  46. }
  47. static int func_error (void *user, void **args)
  48. {
  49. return -1;
  50. }
  51. int main (int argc, char **argv)
  52. {
  53. if (argc != 2) {
  54. fprintf(stderr, "Usage: %s <predicate>\n", argv[0]);
  55. return 1;
  56. }
  57. // init logger
  58. BLog_InitStdout();
  59. // init predicate
  60. BPredicate pr;
  61. if (!BPredicate_Init(&pr, argv[1])) {
  62. fprintf(stderr, "BPredicate_Init failed\n");
  63. return 1;
  64. }
  65. // init functions
  66. BPredicateFunction f_hello;
  67. BPredicateFunction_Init(&f_hello, &pr, "hello", NULL, 0, func_hello, NULL);
  68. BPredicateFunction f_neg;
  69. BPredicateFunction_Init(&f_neg, &pr, "neg", (int []){PREDICATE_TYPE_BOOL}, 1, func_neg, NULL);
  70. BPredicateFunction f_conj;
  71. BPredicateFunction_Init(&f_conj, &pr, "conj", (int []){PREDICATE_TYPE_BOOL, PREDICATE_TYPE_BOOL}, 2, func_conj, NULL);
  72. BPredicateFunction f_strcmp;
  73. BPredicateFunction_Init(&f_strcmp, &pr, "strcmp", (int []){PREDICATE_TYPE_STRING, PREDICATE_TYPE_STRING}, 2, func_strcmp, NULL);
  74. BPredicateFunction f_error;
  75. BPredicateFunction_Init(&f_error, &pr, "error", NULL, 0, func_error, NULL);
  76. // evaluate
  77. int result = BPredicate_Eval(&pr);
  78. printf("%d\n", result);
  79. // free functions
  80. BPredicateFunction_Free(&f_hello);
  81. BPredicateFunction_Free(&f_neg);
  82. BPredicateFunction_Free(&f_conj);
  83. BPredicateFunction_Free(&f_strcmp);
  84. BPredicateFunction_Free(&f_error);
  85. // free predicate
  86. BPredicate_Free(&pr);
  87. return 0;
  88. }