summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorPhilip Withnall <philip.withnall@collabora.co.uk>2014-05-06 15:49:14 +0100
committerPhilip Withnall <philip.withnall@collabora.co.uk>2014-05-06 15:49:14 +0100
commit0563b1921997c9f494f721071b40bb0e2e8a3922 (patch)
treeb828c47125b6218616285ed6395b557c3bf89e94 /tests
parent4817af62e0420e33ae8ccf856efc471dfd48952b (diff)
tests: Add tests for existing assertion extraction code
Test that extracting non-NULL checks from g_return[_val]_if_fail() calls works.
Diffstat (limited to 'tests')
-rw-r--r--tests/Makefile.am6
-rw-r--r--tests/assertion-extraction-return.c145
-rw-r--r--tests/assertion-extraction.c131
-rw-r--r--tests/assertion-return.head.c8
-rw-r--r--tests/assertion-return.tail.c18
-rw-r--r--tests/assertion.head.c8
-rw-r--r--tests/assertion.tail.c16
7 files changed, 332 insertions, 0 deletions
diff --git a/tests/Makefile.am b/tests/Makefile.am
index 943afdc..2adc412 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -6,6 +6,8 @@ TEST_EXTENSIONS = .c
C_LOG_COMPILER = ./wrapper-compiler-errors
TESTS = \
+ assertion-extraction.c \
+ assertion-extraction-return.c \
gvariant-builder.c \
gvariant-get.c \
gvariant-get-child.c \
@@ -16,6 +18,10 @@ TESTS = \
$(NULL)
templates = \
+ assertion.head.c \
+ assertion.tail.c \
+ assertion-return.head.c \
+ assertion-return.tail.c \
generic.head.c \
generic.tail.c \
gvariant.head.c \
diff --git a/tests/assertion-extraction-return.c b/tests/assertion-extraction-return.c
new file mode 100644
index 0000000..a700706
--- /dev/null
+++ b/tests/assertion-extraction-return.c
@@ -0,0 +1,145 @@
+/* Template: assertion-return */
+
+/*
+ * No error
+ */
+{
+ // Nothing to see here, so all parameters are nullable.
+}
+
+/*
+ * warning: null passed to a callee which requires a non-null argument
+ * assertion_val_func (NULL, 1, obj);
+ * ~~~~ ^
+ * null passed to a callee which requires a non-null argument
+ * assertion_val_func (NULL, 3, NULL);
+ * ~~~~ ^
+ */
+{
+ g_return_val_if_fail (some_str != NULL, NULL);
+}
+
+/*
+ * null passed to a callee which requires a non-null argument
+ * assertion_val_func ("str", 2, NULL);
+ * ~~~~^
+ * null passed to a callee which requires a non-null argument
+ * assertion_val_func (NULL, 3, NULL);
+ * ~~~~^
+ */
+{
+ g_return_val_if_fail (some_obj != NULL, NULL);
+}
+
+/*
+ * null passed to a callee which requires a non-null argument
+ * assertion_val_func (NULL, 1, obj);
+ * ~~~~ ^
+ * null passed to a callee which requires a non-null argument
+ * assertion_val_func ("str", 2, NULL);
+ * ~~~~^
+ * null passed to a callee which requires a non-null argument
+ * assertion_val_func (NULL, 3, NULL);
+ * ~~~~^
+ * null passed to a callee which requires a non-null argument
+ * assertion_val_func (NULL, 3, NULL);
+ * ~~~~ ^
+ */
+{
+ g_return_val_if_fail (some_str != NULL, NULL);
+ g_return_val_if_fail (some_obj != NULL, NULL);
+}
+
+/*
+ * null passed to a callee which requires a non-null argument
+ * assertion_val_func ("str", 2, NULL);
+ * ~~~~^
+ * null passed to a callee which requires a non-null argument
+ * assertion_val_func (NULL, 3, NULL);
+ * ~~~~^
+ */
+{
+ gpointer some_priv = (gpointer) some_obj; // simulated setup of private data
+
+ g_return_val_if_fail (some_obj != NULL, NULL);
+}
+
+/*
+ * null passed to a callee which requires a non-null argument
+ * assertion_val_func ("str", 2, NULL);
+ * ~~~~^
+ * null passed to a callee which requires a non-null argument
+ * assertion_val_func (NULL, 3, NULL);
+ * ~~~~^
+ */
+{
+ g_return_val_if_fail ("unrelated" == "assertion", NULL);
+ g_return_val_if_fail (some_obj != NULL, NULL);
+}
+
+/*
+ * null passed to a callee which requires a non-null argument
+ * assertion_val_func (NULL, 1, obj);
+ * ~~~~ ^
+ * null passed to a callee which requires a non-null argument
+ * assertion_val_func (NULL, 3, NULL);
+ * ~~~~ ^
+ */
+{
+ // A non-explicit non-NULL check.
+ g_return_val_if_fail (some_str, NULL);
+}
+
+/*
+ * null passed to a callee which requires a non-null argument
+ * assertion_val_func (NULL, 1, obj);
+ * ~~~~ ^
+ * null passed to a callee which requires a non-null argument
+ * assertion_val_func (NULL, 3, NULL);
+ * ~~~~ ^
+ */
+{
+ // A really weird non-NULL check.
+ g_return_val_if_fail (some_str != 0, NULL);
+}
+
+/*
+ * null passed to a callee which requires a non-null argument
+ * assertion_val_func (NULL, 1, obj);
+ * ~~~~ ^
+ * null passed to a callee which requires a non-null argument
+ * assertion_val_func ("str", 2, NULL);
+ * ~~~~^
+ * null passed to a callee which requires a non-null argument
+ * assertion_val_func (NULL, 3, NULL);
+ * ~~~~^
+ * null passed to a callee which requires a non-null argument
+ * assertion_val_func (NULL, 3, NULL);
+ * ~~~~ ^
+ */
+{
+ // Dual non-NULL checks.
+ g_return_val_if_fail (some_str && some_obj, NULL);
+}
+
+/*
+ * No error
+ */
+{
+ // Can’t statically analyse this at the moment.
+ g_return_val_if_fail (some_str || some_obj, NULL);
+}
+
+/*
+ * null passed to a callee which requires a non-null argument
+ * assertion_val_func (NULL, 1, obj);
+ * ~~~~ ^
+ * null passed to a callee which requires a non-null argument
+ * assertion_val_func (NULL, 3, NULL);
+ * ~~~~ ^
+ */
+{
+ GList *random_variable;
+
+ g_return_val_if_fail (some_str, FALSE);
+}
diff --git a/tests/assertion-extraction.c b/tests/assertion-extraction.c
new file mode 100644
index 0000000..4d93465
--- /dev/null
+++ b/tests/assertion-extraction.c
@@ -0,0 +1,131 @@
+/* Template: assertion */
+
+/*
+ * No error
+ */
+{
+ // Nothing to see here, so all parameters are nullable.
+}
+
+/*
+ * warning: null passed to a callee which requires a non-null argument
+ * assertion_func (NULL, 1, obj);
+ * ~~~~ ^
+ * null passed to a callee which requires a non-null argument
+ * assertion_func (NULL, 3, NULL);
+ * ~~~~ ^
+ */
+{
+ g_return_if_fail (some_str != NULL);
+}
+
+/*
+ * null passed to a callee which requires a non-null argument
+ * assertion_func ("str", 2, NULL);
+ * ~~~~^
+ * null passed to a callee which requires a non-null argument
+ * assertion_func (NULL, 3, NULL);
+ * ~~~~^
+ */
+{
+ g_return_if_fail (some_obj != NULL);
+}
+
+/*
+ * null passed to a callee which requires a non-null argument
+ * assertion_func (NULL, 1, obj);
+ * ~~~~ ^
+ * null passed to a callee which requires a non-null argument
+ * assertion_func ("str", 2, NULL);
+ * ~~~~^
+ * null passed to a callee which requires a non-null argument
+ * assertion_func (NULL, 3, NULL);
+ * ~~~~^
+ * null passed to a callee which requires a non-null argument
+ * assertion_func (NULL, 3, NULL);
+ * ~~~~ ^
+ */
+{
+ g_return_if_fail (some_str != NULL);
+ g_return_if_fail (some_obj != NULL);
+}
+
+/*
+ * null passed to a callee which requires a non-null argument
+ * assertion_func ("str", 2, NULL);
+ * ~~~~^
+ * null passed to a callee which requires a non-null argument
+ * assertion_func (NULL, 3, NULL);
+ * ~~~~^
+ */
+{
+ gpointer some_priv = (gpointer) some_obj; // simulated setup of private data
+
+ g_return_if_fail (some_obj != NULL);
+}
+
+/*
+ * null passed to a callee which requires a non-null argument
+ * assertion_func ("str", 2, NULL);
+ * ~~~~^
+ * null passed to a callee which requires a non-null argument
+ * assertion_func (NULL, 3, NULL);
+ * ~~~~^
+ */
+{
+ g_return_if_fail ("unrelated" == "assertion");
+ g_return_if_fail (some_obj != NULL);
+}
+
+/*
+ * null passed to a callee which requires a non-null argument
+ * assertion_func (NULL, 1, obj);
+ * ~~~~ ^
+ * null passed to a callee which requires a non-null argument
+ * assertion_func (NULL, 3, NULL);
+ * ~~~~ ^
+ */
+{
+ // A non-explicit non-NULL check.
+ g_return_if_fail (some_str);
+}
+
+/*
+ * null passed to a callee which requires a non-null argument
+ * assertion_func (NULL, 1, obj);
+ * ~~~~ ^
+ * null passed to a callee which requires a non-null argument
+ * assertion_func (NULL, 3, NULL);
+ * ~~~~ ^
+ */
+{
+ // A really weird non-NULL check.
+ g_return_if_fail (some_str != 0);
+}
+
+/*
+ * null passed to a callee which requires a non-null argument
+ * assertion_func (NULL, 1, obj);
+ * ~~~~ ^
+ * null passed to a callee which requires a non-null argument
+ * assertion_func ("str", 2, NULL);
+ * ~~~~^
+ * null passed to a callee which requires a non-null argument
+ * assertion_func (NULL, 3, NULL);
+ * ~~~~^
+ * null passed to a callee which requires a non-null argument
+ * assertion_func (NULL, 3, NULL);
+ * ~~~~ ^
+ */
+{
+ // Dual non-NULL checks.
+ g_return_if_fail (some_str && some_obj);
+}
+
+/*
+ * No error
+ */
+{
+ // Can’t statically analyse this at the moment.
+ g_return_if_fail (some_str || some_obj);
+}
diff --git a/tests/assertion-return.head.c b/tests/assertion-return.head.c
new file mode 100644
index 0000000..6af675d
--- /dev/null
+++ b/tests/assertion-return.head.c
@@ -0,0 +1,8 @@
+#include <stdio.h>
+
+#include <glib.h>
+#include <glib-object.h>
+
+static gpointer
+assertion_val_func (const gchar *some_str, guint some_int, GObject *some_obj)
+{
diff --git a/tests/assertion-return.tail.c b/tests/assertion-return.tail.c
new file mode 100644
index 0000000..0dff47e
--- /dev/null
+++ b/tests/assertion-return.tail.c
@@ -0,0 +1,18 @@
+ /* End of assertion_val_func(). */
+
+ return NULL;
+}
+
+int
+main (void)
+{
+ GObject *obj = g_malloc (5);
+
+ /* Various NULL and non-NULL calls to the function. */
+ assertion_val_func ("str", 0, obj);
+ assertion_val_func (NULL, 1, obj);
+ assertion_val_func ("str", 2, NULL);
+ assertion_val_func (NULL, 3, NULL);
+
+ g_free (obj);
+}
diff --git a/tests/assertion.head.c b/tests/assertion.head.c
new file mode 100644
index 0000000..7f9eee6
--- /dev/null
+++ b/tests/assertion.head.c
@@ -0,0 +1,8 @@
+#include <stdio.h>
+
+#include <glib.h>
+#include <glib-object.h>
+
+static void
+assertion_func (const gchar *some_str, guint some_int, GObject *some_obj)
+{
diff --git a/tests/assertion.tail.c b/tests/assertion.tail.c
new file mode 100644
index 0000000..4a50091
--- /dev/null
+++ b/tests/assertion.tail.c
@@ -0,0 +1,16 @@
+ /* End of assertion_func(). */
+}
+
+int
+main (void)
+{
+ GObject *obj = g_malloc (5);
+
+ /* Various NULL and non-NULL calls to the function. */
+ assertion_func ("str", 0, obj);
+ assertion_func (NULL, 1, obj);
+ assertion_func ("str", 2, NULL);
+ assertion_func (NULL, 3, NULL);
+
+ g_free (obj);
+}