[glibmm] Add Gio::MemoryInputStream test



commit e762a455ad3f69fd21b057b65b7ae3e2259e6610
Author: Kjell Ahlstedt <kjell ahlstedt bredband net>
Date:   Wed Feb 12 15:11:44 2014 +0100

    Add Gio::MemoryInputStream test
    
    * tests/giomm_memoryinputstream/main.cc: New file.
    * tests/Makefile.am: Add the MemoryInputStream test.
    Bug #609946

 tests/Makefile.am                     |    4 ++
 tests/giomm_memoryinputstream/main.cc |   86 +++++++++++++++++++++++++++++++++
 2 files changed, 90 insertions(+), 0 deletions(-)
---
diff --git a/tests/Makefile.am b/tests/Makefile.am
index be9c4c6..c2b0458 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -20,6 +20,7 @@ AUTOMAKE_OPTIONS = subdir-objects
 check_PROGRAMS =                               \
        giomm_ioerror/test                      \
        giomm_ioerror_and_iodbuserror/test      \
+       giomm_memoryinputstream/test                    \
        giomm_simple/test                       \
        giomm_asyncresult_sourceobject/test     \
        giomm_tls_client/test                   \
@@ -65,6 +66,9 @@ giomm_ioerror_test_LDADD   = $(giomm_ldadd)
 giomm_ioerror_and_iodbuserror_test_SOURCES = giomm_ioerror_and_iodbuserror/main.cc
 giomm_ioerror_and_iodbuserror_test_LDADD   = $(giomm_ldadd)
 
+giomm_memoryinputstream_test_SOURCES = giomm_memoryinputstream/main.cc
+giomm_memoryinputstream_test_LDADD   = $(giomm_ldadd)
+
 giomm_simple_test_SOURCES  = giomm_simple/main.cc
 giomm_simple_test_LDADD    = $(giomm_ldadd)
 
diff --git a/tests/giomm_memoryinputstream/main.cc b/tests/giomm_memoryinputstream/main.cc
new file mode 100644
index 0000000..6611290
--- /dev/null
+++ b/tests/giomm_memoryinputstream/main.cc
@@ -0,0 +1,86 @@
+#include <giomm.h>
+#include <iostream>
+#include <cstring>
+
+//Use this line if you want debug output:
+//std::ostream& ostr = std::cout;
+
+//This seems nicer and more useful than putting an ifdef around the use of ostr:
+std::stringstream debug;
+std::ostream& ostr = debug;
+
+namespace
+{
+  int n_called1 = 0;
+  int n_called2 = 0;
+
+  void destroy_func1(void* data)
+  {
+    ++n_called1;
+    char* cdata = static_cast<char*>(data);
+    // cdata is not null-terminated.
+    ostr << "Deleting " << std::string(cdata, cdata+6);
+    delete[] cdata;
+  }
+
+  void destroy_func2(void* data, const Glib::ustring& intro)
+  {
+    ++n_called2;
+    char* cdata = static_cast<char*>(data);
+    // cdata is not null-terminated.
+    ostr << intro << std::string(cdata, cdata+6);
+    delete[] cdata;
+  }
+}
+
+int main(int, char**)
+{
+  Glib::init();
+  Gio::init();
+
+  gchar buffer[1000];
+  std::memset(buffer, 0, sizeof buffer);
+  try
+  {
+    Glib::RefPtr<Gio::MemoryInputStream> stream = Gio::MemoryInputStream::create();
+    if (!stream)
+    {
+      std::cerr << "Could not create a MemoryInputStream." << std::endl;
+      return EXIT_FAILURE; 
+    }
+
+    // Add data that shall not be deleted by stream.
+    static const char data1[] = "Data not owned by stream.\n";
+    stream->add_data(data1, sizeof data1 - 1, 0);
+
+    // Add data that shall be deleted by destroy_func1().
+    char* data2 = new char[7];
+    std::strcpy(data2, "data2\n");
+    stream->add_data(data2, 6, destroy_func1);
+
+    // Add data that shall be deleted by destroy_func2().
+    char* data3 = new char[7];
+    std::strcpy(data3, "data3\n");
+    stream->add_data(data3, 6, sigc::bind(sigc::ptr_fun(destroy_func2), "Now deleting "));
+
+    const gsize bytes_read = stream->read(buffer, sizeof buffer - 1);
+
+    if (bytes_read)
+      ostr << "Memory contents read: " << buffer << std::endl;
+    else
+    {
+      std::cerr << "Gio::InputStream::read() read 0 bytes." << std::endl;
+      return EXIT_FAILURE; 
+    }
+  }
+  catch (const Glib::Exception& ex)
+  {
+    std::cerr << "Exception caught: " << ex.what() << std::endl;
+    return EXIT_FAILURE; 
+  }
+
+  if (std::strcmp(buffer, "Data not owned by stream.\ndata2\ndata3\n") == 0 &&
+      n_called1 == 1 && n_called2 == 1)
+    return EXIT_SUCCESS;
+  return EXIT_FAILURE; 
+}


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