predicate_test.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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. #include <stdio.h>
  30. #include <string.h>
  31. #include <predicate/BPredicate.h>
  32. #include <base/BLog.h>
  33. static int func_hello (void *user, void **args)
  34. {
  35. return 1;
  36. }
  37. static int func_neg (void *user, void **args)
  38. {
  39. int arg = *((int *)args[0]);
  40. return !arg;
  41. }
  42. static int func_conj (void *user, void **args)
  43. {
  44. int arg1 = *((int *)args[0]);
  45. int arg2 = *((int *)args[1]);
  46. return (arg1 && arg2);
  47. }
  48. static int func_strcmp (void *user, void **args)
  49. {
  50. char *arg1 = args[0];
  51. char *arg2 = args[1];
  52. return (!strcmp(arg1, arg2));
  53. }
  54. static int func_error (void *user, void **args)
  55. {
  56. return -1;
  57. }
  58. int main (int argc, char **argv)
  59. {
  60. if (argc != 2) {
  61. fprintf(stderr, "Usage: %s <predicate>\n", argv[0]);
  62. return 1;
  63. }
  64. // init logger
  65. BLog_InitStdout();
  66. // init predicate
  67. BPredicate pr;
  68. if (!BPredicate_Init(&pr, argv[1])) {
  69. fprintf(stderr, "BPredicate_Init failed\n");
  70. return 1;
  71. }
  72. // init functions
  73. BPredicateFunction f_hello;
  74. BPredicateFunction_Init(&f_hello, &pr, "hello", NULL, 0, func_hello, NULL);
  75. BPredicateFunction f_neg;
  76. BPredicateFunction_Init(&f_neg, &pr, "neg", (int []){PREDICATE_TYPE_BOOL}, 1, func_neg, NULL);
  77. BPredicateFunction f_conj;
  78. BPredicateFunction_Init(&f_conj, &pr, "conj", (int []){PREDICATE_TYPE_BOOL, PREDICATE_TYPE_BOOL}, 2, func_conj, NULL);
  79. BPredicateFunction f_strcmp;
  80. BPredicateFunction_Init(&f_strcmp, &pr, "strcmp", (int []){PREDICATE_TYPE_STRING, PREDICATE_TYPE_STRING}, 2, func_strcmp, NULL);
  81. BPredicateFunction f_error;
  82. BPredicateFunction_Init(&f_error, &pr, "error", NULL, 0, func_error, NULL);
  83. // evaluate
  84. int result = BPredicate_Eval(&pr);
  85. printf("%d\n", result);
  86. // free functions
  87. BPredicateFunction_Free(&f_hello);
  88. BPredicateFunction_Free(&f_neg);
  89. BPredicateFunction_Free(&f_conj);
  90. BPredicateFunction_Free(&f_strcmp);
  91. BPredicateFunction_Free(&f_error);
  92. // free predicate
  93. BPredicate_Free(&pr);
  94. return 0;
  95. }