Added pointer types test case.
authorJustin Seyster <jseyster@cs.sunysb.edu>
Thu, 2 Sep 2010 22:20:20 +0000 (18:20 -0400)
committerJustin Seyster <jseyster@cs.sunysb.edu>
Thu, 2 Sep 2010 22:20:20 +0000 (18:20 -0400)
test/Makefile.am
test/plugin-pointer-types.c [new file with mode: 0644]
test/pointer-types-hooks.c [new file with mode: 0644]
test/pointer-types-target.c [new file with mode: 0644]
test/pointer-types.xml [new file with mode: 0644]

index 60cfc7955001634b7caa0aeed76ae19ecc8dd3a0..aeb742907dc0d326c7f5c6a928fa1e8d0abd0e1e 100644 (file)
@@ -2,5 +2,5 @@ if HAVE_PYTHON
 TESTS_ENVIRONMENT = $(PYTHON) $(srcdir)/run-testcase.py --with-gcc=$(CC) \
        --with-ia-lib-dir=$(top_builddir)/src/.libs \
        --with-ia-src-dir=$(top_srcdir) --with-tests-dir=$(srcdir)
-TESTS = int-types.xml float-types.xml
+TESTS = int-types.xml float-types.xml pointer-types.xml
 endif
diff --git a/test/plugin-pointer-types.c b/test/plugin-pointer-types.c
new file mode 100644 (file)
index 0000000..3a9a0f0
--- /dev/null
@@ -0,0 +1,69 @@
+#include <aop.h>
+#include <string.h>
+
+AOP_I_AM_GPL_COMPATIBLE();
+
+static void plugin_join_on_call(struct aop_joinpoint *jp, void *data)
+{
+  const char *advice_name = data;
+  const char *func;
+  const char *called;
+  struct aop_dynval *p;
+
+  func = aop_capture_function_name(jp);
+  if (func == NULL || strcmp(func, "run_test") != 0)
+    return;
+
+  called = aop_capture_called_function_name(jp);
+
+  if (called == NULL || *called != '_') {
+    p = aop_capture_param(jp, 0);
+    aop_insert_advice(jp, advice_name, AOP_INSERT_BEFORE, AOP_STR_CST(called), AOP_DYNVAL(p), AOP_TERM_ARG);
+  }
+}
+
+static unsigned int plugin_pointer()
+{
+  struct aop_pointcut *pc;
+
+  const struct aop_type *intp;
+  const struct aop_type *intpp;
+  const struct aop_type *intppp;
+  const struct aop_type *char_star;
+
+  intp = aop_t_pointer_to(aop_t_signed32());
+  intpp = aop_t_pointer_to(intp);
+  intppp = aop_t_pointer_to(intpp);
+  char_star = aop_t_pointer_to(aop_t_signed8());
+
+  pc = aop_match_function_call();
+  aop_filter_call_pc_by_param(pc, 0, intp);
+  aop_join_on(pc, plugin_join_on_call, "_advice_p");
+
+  pc = aop_match_function_call();
+  aop_filter_call_pc_by_param(pc, 0, intpp);
+  aop_join_on(pc, plugin_join_on_call, "_advice_pp");
+
+  pc = aop_match_function_call();
+  aop_filter_call_pc_by_param(pc, 0, intppp);
+  aop_join_on(pc, plugin_join_on_call, "_advice_ppp");
+
+  pc = aop_match_function_call();
+  aop_filter_call_pc_by_param(pc, 0, char_star);
+  aop_join_on(pc, plugin_join_on_call, "_advice_c_str");
+
+  pc = aop_match_function_call();
+  aop_filter_call_pc_by_param(pc, 0, aop_t_all_pointer());
+  aop_join_on(pc, plugin_join_on_call, "_advice_any_ptr");
+
+  pc = aop_match_function_call();
+  aop_filter_call_pc_by_param(pc, 0, aop_t_pointer_to(aop_t_all_pointer()));
+  aop_join_on(pc, plugin_join_on_call, "_advice_any_ptr_ptr");
+
+  return  0;
+}
+
+AOP_MAIN_PROTO aop_main()
+{
+  aop_register_pass("float", plugin_pointer);
+}
diff --git a/test/pointer-types-hooks.c b/test/pointer-types-hooks.c
new file mode 100644 (file)
index 0000000..eb2cbc6
--- /dev/null
@@ -0,0 +1,50 @@
+#include <stdio.h>
+
+extern int n;
+extern int *p1;
+extern int **p2;
+extern int ***p3;
+
+static const char *pointer_name(void *ptr)
+{
+  if (ptr == &n)
+    return "&n";
+  else if (ptr == &p1)
+    return "&p1";
+  else if (ptr == &p2)
+    return "&p2";
+  else if (ptr == &p3)
+    return "&p3";
+  else
+    return "other pointer";
+}
+
+void _advice_p(const char *func, int *p)
+{
+  printf("In int * advice (%s): %d\n", func, *p);
+}
+
+void _advice_pp(const char *func, int **p)
+{
+  printf("In int ** advice (%s): %d\n", func, **p);
+}
+
+void _advice_ppp(const char *func, int ***p)
+{
+  printf("In int *** advice (%s): %d\n", func, ***p);
+}
+
+void _advice_c_str(const char *func, const char *str)
+{
+  printf("In str advice (%s): %s\n", func, str);
+}
+
+void _advice_any_ptr(const char *func, void *p)
+{
+  printf("In void * advice (%s): %s\n", func, pointer_name(p));
+}
+
+void _advice_any_ptr_ptr(const char *func, void **p)
+{
+  printf("In void ** advice (%s): %s\n", func, pointer_name(*p));
+}
diff --git a/test/pointer-types-target.c b/test/pointer-types-target.c
new file mode 100644 (file)
index 0000000..7d1c01b
--- /dev/null
@@ -0,0 +1,39 @@
+#include <stdio.h>
+
+int n;
+int *p1;
+int **p2;
+int ***p3;
+
+void intp(int *p)
+{
+  printf("int *: %d\n", *p);
+}
+
+void intpp(int **p)
+{
+  printf("int **: %d\n", **p);
+}
+
+void intppp(int ***p)
+{
+  printf("int ***: %d\n", ***p);
+}
+
+void c_str(const char *str)
+{
+  printf("str: %s\n", str);
+}
+
+void run_test()
+{
+  n = 1337;
+  p1 = &n;
+  p2 = &p1;
+  p3 = &p2;
+
+  intp(p1);
+  intpp(p2);
+  intppp(p3);
+  c_str("h4x0r");
+}
diff --git a/test/pointer-types.xml b/test/pointer-types.xml
new file mode 100644 (file)
index 0000000..2bea475
--- /dev/null
@@ -0,0 +1,24 @@
+<?xml version="1.0" encoding="UTF-8" ?>
+<!DOCTYPE testcase SYSTEM "testcase.dtd">
+<testcase name="Pointer Types">
+  <plugin id="plugin-pointer-types" source="plugin-pointer-types.c" />
+  <run name="Pointer parameter captures" target="pointer-types-target.c" hooks="pointer-types-hooks.c">
+    <using plugin="plugin-pointer-types" />
+    <output>
+      In int * advice (intp): 1337
+      In void * advice (intp): &amp;n
+      int *: 1337
+      In int ** advice (intpp): 1337
+      In void * advice (intpp): &amp;p1
+      In void ** advice (intpp): &amp;n
+      int **: 1337
+      In int *** advice (intppp): 1337
+      In void * advice (intppp): &amp;p2
+      In void ** advice (intppp): &amp;p1
+      int ***: 1337
+      In str advice (c_str): h4x0r
+      In void * advice (c_str): other pointer
+      str: h4x0r
+    </output>
+  </run>
+</testcase>