[gxml/gsoc2013: 112/150] tests/valgrind: add memory testing files, like suppression files, and .c and .vala tests



commit 89120db64f90aa9ae37a2812b3d6e36baf696166
Author: Richard Schwarting <aquarichy gmail com>
Date:   Sun Jul 28 01:30:11 2013 -0400

    tests/valgrind: add memory testing files, like suppression files, and .c and .vala tests

 test/valgrind/Makefile                  |   49 ++++++++++++++++++++
 test/valgrind/attributes.vala           |   25 ++++++++++
 test/valgrind/document.c                |   12 +++++
 test/valgrind/example.vala              |   76 +++++++++++++++++++++++++++++++
 test/valgrind/gfile.c                   |   13 +++++
 test/valgrind/gio.supp                  |   29 ++++++++++++
 test/valgrind/glib.supp                 |   16 +++++++
 test/valgrind/gobject.c                 |   12 +++++
 test/valgrind/gtype.supp                |   45 ++++++++++++++++++
 test/valgrind/libxml2.c                 |   28 +++++++++++
 test/valgrind/libxml2.supp              |   43 +++++++++++++++++
 test/valgrind/scope.vala                |   25 ++++++++++
 test/valgrind/small.vala                |   24 ++++++++++
 test/valgrind/small2.vala               |   24 ++++++++++
 test/valgrind/small2a.c                 |   28 +++++++++++
 test/valgrind/small2b.c                 |   25 ++++++++++
 test/valgrind/unlink_unattached_nodes.c |   45 ++++++++++++++++++
 17 files changed, 519 insertions(+), 0 deletions(-)
---
diff --git a/test/valgrind/Makefile b/test/valgrind/Makefile
new file mode 100644
index 0000000..6509fc4
--- /dev/null
+++ b/test/valgrind/Makefile
@@ -0,0 +1,49 @@
+VALAC=valac
+VALAFLAGS=-g --save-temps --vapidir=../../gxml --pkg gxml
+CC=gcc
+CFLAGS=-g `pkg-config --cflags --libs glib-2.0 gobject-2.0 gxml gio-2.0`
+
+all: unlink_unattached_nodes small2a small2b gobject document example attributes small small2 scope libxml2 
supp
+
+example: example.vala
+       $(VALAC) $(VALAFLAGS) example.vala -o example
+
+attributes: attributes.vala
+       $(VALAC) $(VALAFLAGS) attributes.vala -o attributes
+
+small: small.vala
+       $(VALAC) $(VALAFLAGS) small.vala -o small
+
+small2: small2.vala
+       $(VALAC) $(VALAFLAGS) small2.vala -o small2
+
+scope: scope.vala
+       $(VALAC) $(VALAFLAGS) scope.vala -o scope
+
+unlink_unattached_nodes: unlink_unattached_nodes.c
+
+small2a: small2a.c
+
+small2b: small2b.c
+
+gfile: gfile.c
+
+gobject: gobject.c
+
+document: document.c
+
+libxml2: libxml2.c
+
+clean:
+       rm example small small2 small2a small2b gobject document scope attributes unlink_unattached_nodes 
libxml2 *~ \#*\#
+
+runnew:
+       (cd ../.. && make && make install); make clean; make
+       make run
+
+run:
+       G_SLICE=always-malloc valgrind --leak-check=full --show-reachable=yes --suppressions=libxml2.supp 
--suppressions=gio.supp --suppressions=gtype.supp --suppressions=glib.supp --num-callers=40 ./example
+
+# we compile it for users of GXml
+supp:
+       cat *.supp > ../../gxml.supp
diff --git a/test/valgrind/attributes.vala b/test/valgrind/attributes.vala
new file mode 100644
index 0000000..ada7879
--- /dev/null
+++ b/test/valgrind/attributes.vala
@@ -0,0 +1,25 @@
+using GXml;
+
+int main (string[] args) {
+       // string[] authors = { "John Green", "Jane Austen", "J.D. Salinger" };
+       // string[] titles = { "The Fault in Our Stars", "Pride & Prejudice", "Nine Stories" };
+
+       try {
+               Document doc = new Document ();
+               Element root = doc.create_element ("Bookshelf");
+               doc.append_child (root);
+
+               Element owner = doc.create_element ("Owner");
+               root.append_child (owner);
+               owner.set_attribute ("fullname", "John Green");
+
+               // Element books = doc.create_element ("Books");
+               // root.append_child (books);
+
+               //stdout.printf ("create_a_document:\n%s\n", doc.to_string (true, 8));
+       } catch (GLib.Error e) {
+               stderr.printf ("%s\n", e.message);
+       }
+
+       return 0;
+}
diff --git a/test/valgrind/document.c b/test/valgrind/document.c
new file mode 100644
index 0000000..aff8154
--- /dev/null
+++ b/test/valgrind/document.c
@@ -0,0 +1,12 @@
+#include <glib.h>
+#include <glib-object.h>
+#include <gxml/gxml.h>
+
+int main (void) {
+  GXmlDocument *doc;
+
+  doc = gxml_document_new ();
+  g_object_unref (doc);
+
+  return 0;
+}
diff --git a/test/valgrind/example.vala b/test/valgrind/example.vala
new file mode 100755
index 0000000..f0d566f
--- /dev/null
+++ b/test/valgrind/example.vala
@@ -0,0 +1,76 @@
+using GXml;
+
+void create_a_document () {
+       string[] authors = { "John Green", "Jane Austen", "J.D. Salinger" };
+       string[] titles = { "The Fault in Our Stars", "Pride & Prejudice", "Nine Stories" };
+
+       Document doc = new Document ();
+       Element root = doc.create_element ("Bookshelf");
+       doc.append_child (root);
+       Element owner = doc.create_element ("Owner");
+       root.append_child (owner);
+       owner.set_attribute ("fullname", "John Green");
+
+       Element books = doc.create_element ("Books");
+       root.append_child (books);
+
+       for (int i = 0; i < authors.length; i++) {
+               Element book = doc.create_element ("Book");
+               book.set_attribute ("author", authors[i]);
+               book.set_attribute ("title", titles[i]);
+               books.append_child (book);
+       }
+
+       stdout.printf ("create_a_document:\n%s\n", doc.to_string (true, 8));
+}
+
+void create_a_document_from_a_string () {
+       string xml;
+       Document doc;
+
+       xml = """<?xml version="1.0"?>
+<Bookshelf>
+<Owner fullname="John Green"/>
+<Books>
+<Book author="John Green" title="The Fault in Our Stars"/>
+<Book author="Jane Austen" title="Pride &amp; Prejudice"/>
+<Book author="J.D. Salinger" title="Nine Stories"/>
+</Books>
+</Bookshelf>""";
+
+       doc = new Document.from_string (xml);
+       stdout.printf ("create_a_document_from_a_string:\n%s\n", doc.to_string (true, 8));
+}
+
+void create_a_document_from_a_file () {
+       File f = File.new_for_path ("bookshelf.xml");
+       Cancellable can = new Cancellable ();
+       Document doc;
+
+       doc = new Document.from_gfile (f, can);
+       stdout.printf ("create_a_document_from_a_file:\n%s\n", doc.to_string (true, 8));
+}
+
+void create_a_document_from_a_path () {
+       Document doc;
+
+       doc = new Document.from_path ("bookshelf.xml");
+       stdout.printf ("create_a_document_from_a_path:\n%s\n", doc.to_string (true, 8));
+}
+
+void saving_a_document_to_a_path () {
+       Document doc;
+
+       doc = new Document.from_path ("bookshelf.xml");
+       doc.save_to_path ("bookshelf2.xml");
+}
+
+int main (string[] args) {
+       create_a_document ();
+       create_a_document_from_a_string ();
+       create_a_document_from_a_file ();
+       create_a_document_from_a_path ();
+       saving_a_document_to_a_path ();
+
+       return 0;
+}
diff --git a/test/valgrind/gfile.c b/test/valgrind/gfile.c
new file mode 100644
index 0000000..7c55015
--- /dev/null
+++ b/test/valgrind/gfile.c
@@ -0,0 +1,13 @@
+#include <glib.h>
+#include <gio/gio.h>
+
+int main (void) {
+  GFile *file;
+
+  file = g_file_new_for_path ("bookshelf.xml");
+
+  g_object_unref (file);
+
+
+  return 0;
+}
diff --git a/test/valgrind/gio.supp b/test/valgrind/gio.supp
new file mode 100644
index 0000000..b61d4b8
--- /dev/null
+++ b/test/valgrind/gio.supp
@@ -0,0 +1,29 @@
+{
+  label
+  Memcheck:Leak
+  ...
+  fun:gobject_init_ctor
+  fun:_dl_init
+}
+{
+  label:
+  Memcheck:Leak
+  ...
+  fun:_g_io_module_get_default
+  fun:g_file_new_for_path
+}
+{
+  label:
+  Memcheck:Leak
+  ...
+  fun:try_implementation
+  fun:_g_io_module_get_default
+}
+{
+  label
+  Memcheck:Leak
+  ...
+  fun:g_private_get
+  fun:g_cancellable_push_current
+  fun:g_input_stream_read
+}
diff --git a/test/valgrind/glib.supp b/test/valgrind/glib.supp
new file mode 100644
index 0000000..d6f735b
--- /dev/null
+++ b/test/valgrind/glib.supp
@@ -0,0 +1,16 @@
+{
+  label
+  Memcheck:Leak
+  ...
+  fun:g_type_class_ref
+  fun:g_object_newv
+}
+{
+  label
+  Memcheck:Leak
+  ...
+  fun:g_thread_proxy
+  fun:start_thread
+  fun:clone
+}
+  
\ No newline at end of file
diff --git a/test/valgrind/gobject.c b/test/valgrind/gobject.c
new file mode 100644
index 0000000..32e9138
--- /dev/null
+++ b/test/valgrind/gobject.c
@@ -0,0 +1,12 @@
+#include <glib.h>
+#include <glib-object.h>
+#include <gxml/gxml.h>
+
+int main (void) {
+  GObject *obj;
+
+  obj = g_object_newv (G_TYPE_OBJECT, 0, NULL);
+  g_object_unref (obj);
+
+  return 0;
+}
diff --git a/test/valgrind/gtype.supp b/test/valgrind/gtype.supp
new file mode 100644
index 0000000..de4f837
--- /dev/null
+++ b/test/valgrind/gtype.supp
@@ -0,0 +1,45 @@
+{
+  label
+  Memcheck:Leak
+  ...
+  fun:g_type_register_static*
+  fun:*_get_type
+}
+{
+  label
+  Memcheck:Leak
+  ...
+  fun:g_type_add_interface_static
+  fun:*_get_type
+}
+{
+  label
+  Memcheck:Leak
+  ...
+  fun:g_type_interface_add_prerequisite
+  fun:*_get_type
+}
+{
+  label
+  Memcheck:Leak
+  ...
+  fun:g_type_create_instance
+  fun:g_object_constructor
+}
+{
+  label
+  Memcheck:Leak
+  ...
+  fun:g_type_class_ref
+  fun:g_type_create_instance
+}
+{
+  label
+  Memcheck:Leak
+  fun:memalign
+  ...
+  fun:g_type_create_instance
+  fun:*_construct_*
+}
+
+
diff --git a/test/valgrind/libxml2.c b/test/valgrind/libxml2.c
new file mode 100644
index 0000000..5c9a4eb
--- /dev/null
+++ b/test/valgrind/libxml2.c
@@ -0,0 +1,28 @@
+#include <libxml/tree.h>
+#include <stdio.h>
+#include <string.h>
+
+int main (void) {
+  char *str = "<Bookshelf><Book>Text</Book></Bookshelf>";
+  xmlDoc *doc = xmlReadMemory (str, strlen (str), NULL, NULL, 0);
+
+  // to string
+  xmlChar *mem;
+  int size;
+  xmlDocDumpFormatMemoryEnc (doc, &mem, &size, NULL, 0);
+  printf ("[%s]\n", (char*)mem);
+  xmlFree (mem);
+
+  // set attribute
+  xmlNode *root = xmlDocGetRootElement (doc);
+  xmlAttr *attr = xmlSetProp (root, "name", "Alice");
+  
+  xmlFreeDoc (doc);
+
+  /* In C, you'd call this when you were done with the library;
+     TODO: do we want to expose this in Vala?  We can't automatically run
+     it when we're done using the library from Vala, sadly. */
+  //xmlCleanupParser ();
+
+  return 0;
+}
diff --git a/test/valgrind/libxml2.supp b/test/valgrind/libxml2.supp
new file mode 100644
index 0000000..dceb99f
--- /dev/null
+++ b/test/valgrind/libxml2.supp
@@ -0,0 +1,43 @@
+{
+  label
+  Memcheck:Leak
+  ...
+  fun:xmlInitCharEncodingHandlers
+}
+{
+  label
+  Memcheck:Leak
+  ...
+  fun:xmlInitParserCtxt
+}
+{
+  label
+  Memcheck:Leak
+  ...
+  fun:xmlInitParser__internal_alias*
+}
+{
+  label
+  Memcheck:Leak
+  ...
+  fun:xmlNewCharEncodingHandler
+  fun:xmlGetCharEncodingHandler
+}
+{
+  label
+  Memcheck:Leak
+  ...
+  fun:__xmlInitializeDict
+  ...
+  fun:xmlDocDumpFormatMemory*
+}
+{
+  label
+  Memcheck:Leak
+  ...
+  fun:__xmlInitializeDict
+  ...
+  fun:xmlNewDocProp
+}
+
+ 
diff --git a/test/valgrind/scope.vala b/test/valgrind/scope.vala
new file mode 100644
index 0000000..710e704
--- /dev/null
+++ b/test/valgrind/scope.vala
@@ -0,0 +1,25 @@
+using GXml;
+
+void create_a_document () {
+       try {
+               Element owner;
+
+               {
+                       Document doc = new Document ();
+                       Element root = doc.create_element ("Bookshelf");
+                       doc.append_child (root);
+                       owner = doc.create_element ("Owner");
+                       root.append_child (owner);
+                       // owner.set_attribute ("fullname", "John Green");
+               }
+               stdout.printf ("Element owner:\n%s\n", owner.to_string (true, 8));
+       } catch (GLib.Error e) {
+               stderr.printf ("%s\n", e.message);
+       }
+}
+
+int main (string[] args) {
+       create_a_document ();
+
+       return 0;
+}
diff --git a/test/valgrind/small.vala b/test/valgrind/small.vala
new file mode 100755
index 0000000..e716ab3
--- /dev/null
+++ b/test/valgrind/small.vala
@@ -0,0 +1,24 @@
+using GXml;
+
+void create_a_document () {
+       try {
+               Document doc = new Document ();
+
+               // Element root = doc.create_element ("Bookshelf");
+               // doc.append_child (root);
+
+               // Element owner = doc.create_element ("Owner");
+               // root.append_child (owner);
+               // owner.set_attribute ("fullname", "John Green");
+
+               stdout.printf ("create_a_document:\n%s\n", doc.to_string (true, 8));
+       } catch (GLib.Error e) {
+               stderr.printf ("%s\n", e.message);
+       }
+}
+
+int main (string[] args) {
+       create_a_document ();
+
+       return 0;
+}
diff --git a/test/valgrind/small2.vala b/test/valgrind/small2.vala
new file mode 100644
index 0000000..daea909
--- /dev/null
+++ b/test/valgrind/small2.vala
@@ -0,0 +1,24 @@
+using GXml;
+
+void create_a_document () {
+       try {
+               Document doc = new Document ();
+
+               Element root = doc.create_element ("Bookshelf");
+               doc.append_child (root);
+
+               Element owner = doc.create_element ("Owner");
+               //root.append_child (owner);
+               // owner.set_attribute ("fullname", "John Green");
+
+               stdout.printf ("create_a_document:\n%s\n", doc.to_string (true, 8));
+       } catch (GLib.Error e) {
+               stderr.printf ("%s\n", e.message);
+       }
+}
+
+int main (string[] args) {
+       create_a_document ();
+
+       return 0;
+}
diff --git a/test/valgrind/small2a.c b/test/valgrind/small2a.c
new file mode 100644
index 0000000..cd1664a
--- /dev/null
+++ b/test/valgrind/small2a.c
@@ -0,0 +1,28 @@
+#include <glib.h>
+#include <glib-object.h>
+#include <gxml/gxml.h>
+
+/**
+ * In this one, we DO attach the owner node.
+ */
+int main (int argc, char ** argv) {
+  GXmlDocument* doc;
+  GXmlElement* root;
+  GXmlElement* owner;
+       
+  doc = gxml_document_new ();
+  root = gxml_document_create_element (doc, "Bookshelf");
+  gxml_node_append_child (GXML_NODE (doc), GXML_NODE (root));
+  owner = gxml_document_create_element (doc, "Owner");
+  // do attach owner
+  gxml_node_append_child (GXML_NODE (root), GXML_NODE (owner));
+       
+  g_object_unref (owner);
+  g_object_unref (root);
+  g_object_unref (doc);
+
+  return 0;
+}
+
+
+
diff --git a/test/valgrind/small2b.c b/test/valgrind/small2b.c
new file mode 100644
index 0000000..df41670
--- /dev/null
+++ b/test/valgrind/small2b.c
@@ -0,0 +1,25 @@
+#include <glib.h>
+#include <glib-object.h>
+#include <gxml/gxml.h>
+
+/**
+ * In this one, we do NOT attach the owner node.
+ */
+int main () {
+  GXmlDocument* doc;
+  GXmlElement* root;
+  GXmlElement* owner;
+
+  doc = gxml_document_new ();
+  root = gxml_document_create_element (doc, "Bookshelf");
+  gxml_node_append_child (GXML_NODE (doc), GXML_NODE (root));
+  owner = gxml_document_create_element (doc, "Owner");
+  // don't attach owner
+
+  g_object_unref (owner);
+  g_object_unref (root);
+  g_object_unref (doc);
+
+  return 0;
+}
+  
diff --git a/test/valgrind/unlink_unattached_nodes.c b/test/valgrind/unlink_unattached_nodes.c
new file mode 100644
index 0000000..4969eb8
--- /dev/null
+++ b/test/valgrind/unlink_unattached_nodes.c
@@ -0,0 +1,45 @@
+#include <glib.h>
+#include <glib-object.h>
+#include <gxml/gxml.h>
+
+/* This test creates some nodes that are not attached to the tree.
+   Run with valgrind --leak-check=full --show-reachable=yes to make
+   sure that they still get cleaned up.
+
+   (At present, doc calls free on the document, which gets rid of the
+   tree. I'm testing having an additional list of new nodes that were
+   created and are perhaps outside the tree.  All the nodes below,
+   whether in the tree or not, will be in the new-nodes list which
+   we'll try to get rid of.  The crux of the idea is using
+   xmlUnlinkNode in a separate xmlList.  Let's see how this goes!);
+*/
+
+int main () {
+  GXmlDocument* doc;
+  GXmlElement* root;
+  GXmlElement* owner1;
+  GXmlElement* owner2;
+  GXmlElement* book;
+
+  doc = gxml_document_new ();
+
+  /* This node belongs to the doc and is attached to the tree */
+  root = gxml_document_create_element (doc, "Bookshelf");
+  gxml_node_append_child (GXML_NODE (doc), GXML_NODE (root));
+  
+  /* owner1, owner2 belong to doc, but aren't part of the document tree */
+  owner1 = gxml_document_create_element (doc, "Owner1"); 
+  owner2 = gxml_document_create_element (doc, "Owner2");
+  /* book is outside the tree as well, but is a child of owner2 */
+  book = gxml_document_create_element (doc, "Book");
+  gxml_node_append_child (GXML_NODE (owner2), GXML_NODE (book));
+
+  g_object_unref (doc);
+  g_object_unref (root);
+  g_object_unref (owner1);
+  g_object_unref (owner2);
+  g_object_unref (book);
+
+  return 0;
+}
+  


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]