[Avida-SVN] r1213 - in extras: . source/targets/TestAvida source/testsuites

avidaedward at myxo.css.msu.edu avidaedward at myxo.css.msu.edu
Sat Jan 27 22:16:19 PST 2007


Author: avidaedward
Date: 2007-01-28 01:16:19 -0500 (Sun, 28 Jan 2007)
New Revision: 1213

Added:
   extras/source/testsuites/nConsoleCatcher.cpp
Modified:
   extras/
   extras/SConscript
   extras/source/targets/TestAvida/TestAvida.cpp
Log:
 r1178 at clearly:  kaben | 2006-12-22 22:44:38 -0500
 Completed brainstorm and testing of new class cConsoleCatcher. Need to
 move to its own c++ files.



Property changes on: extras
___________________________________________________________________
Name: svk:merge
   - 079b078a-dbed-46b9-b3da-37668d4295ca:/avida/local/extras:1173
   + 079b078a-dbed-46b9-b3da-37668d4295ca:/avida/local/extras:1178

Modified: extras/SConscript
===================================================================
--- extras/SConscript	2007-01-28 06:16:09 UTC (rev 1212)
+++ extras/SConscript	2007-01-28 06:16:19 UTC (rev 1213)
@@ -111,6 +111,7 @@
   'source/testsuites/nAnalyze.cpp',
   'source/testsuites/nAnalyzeGenotype.cpp',
   'source/testsuites/nChangeList.cpp',
+  'source/testsuites/nConsoleCatcher.cpp',
   'source/testsuites/nDataEntry.cpp',
   'source/testsuites/nDataFile.cpp',
   'source/testsuites/nFile.cpp',

Modified: extras/source/targets/TestAvida/TestAvida.cpp
===================================================================
--- extras/source/targets/TestAvida/TestAvida.cpp	2007-01-28 06:16:09 UTC (rev 1212)
+++ extras/source/targets/TestAvida/TestAvida.cpp	2007-01-28 06:16:19 UTC (rev 1213)
@@ -15,6 +15,7 @@
 namespace nAnalyze { void PhysicalLink(); }
 namespace nAnalyzeGenotype { void PhysicalLink(); }
 namespace nChangeList { void PhysicalLink(); }
+namespace nConsoleCatcher { void PhysicalLink(); }
 namespace nDataEntry { void PhysicalLink(); }
 namespace nDataFile { void PhysicalLink(); }
 namespace nFile { void PhysicalLink(); }
@@ -33,6 +34,7 @@
   nAnalyze::PhysicalLink();
   nAnalyzeGenotype::PhysicalLink();
   nChangeList::PhysicalLink();
+  nConsoleCatcher::PhysicalLink();
   nDataEntry::PhysicalLink();
   nDataFile::PhysicalLink();
   nFile::PhysicalLink();

Added: extras/source/testsuites/nConsoleCatcher.cpp
===================================================================
--- extras/source/testsuites/nConsoleCatcher.cpp	2007-01-28 06:16:09 UTC (rev 1212)
+++ extras/source/testsuites/nConsoleCatcher.cpp	2007-01-28 06:16:19 UTC (rev 1213)
@@ -0,0 +1,207 @@
+//#include "cConsoleCatcher.h"
+
+#include "cTestLib.h"
+#include <iostream>
+#include <sstream>
+#include <assert.h>
+#include <signal.h>
+using namespace std;
+
+namespace nConsoleCatcher {
+/* Brainstorms. {{{1 */
+  /* cConsoleCatcher_Brainstorm_HelloWorld {{{2 */
+  namespace Brainstorm_HelloWorld {
+    void test(const cStringList &attrs){
+      if(!attrs.HasString("HelloWorld")){
+        cout << "XXX Skipping HelloWorld test." << endl;
+        return;
+      }
+      cout << "XXX This is a test stub. It needs filling-in. @kgn" << endl;
+      /* Make sure testing is working with successes and failures. @kgn */
+      TEST(true);
+      TEST(false);
+    }
+    cAddTestSuite t("cConsoleCatcher_Brainstorm_HelloWorld", test);
+  }
+  /* cConsoleCatcher_Brainstorm_Design {{{2 */
+  namespace Brainstorm_Design {
+    /*
+    XXX Questions:
+    - What if lots of output is being captured?
+      - Possible answers:
+        - ignore it
+        - discard previous by overwriting
+        - something complicated like storage to temporary file
+          - In which case it might be better to return a cStringList,
+            but still, a single line could grow huge.
+      - How much can be stored in a cString?
+        - A: 4096 characters.
+    */
+    /*
+    XXX Answered:
+    - Better to return cString or cStringList?
+      - A: Decided to return a cString, it's less complex.
+    */
+    /*
+    XXX Done:
+    - cString myConsoleCatcher::Capture(ostream &out);
+    - cString myConsoleCatcher::Release(void);
+    - Verify myConsoleCatcher::Release releases previous stream, if any;
+    - Verify myConsoleCatcher::Release release of null stream doesn't
+      crash;
+    - Verify myConsoleCatcher::Release release of null stream returns
+      empty string;
+    - Verify myConsoleCatcher::myConsoleCatcher() zeros stored onput
+      stream and streambuf;
+    */
+
+    struct cConsoleCatcher {
+      /* Temporary storage of the caught stream. */
+      ostream *m_stream;
+      /* Temporary storage of the caught stream's original streambuf. */
+      streambuf *m_streambuf;
+      /* Capturing stream receives caught stream's redirected output. */
+      stringstream m_capturing_stream;
+
+      cConsoleCatcher()
+      : m_stream(0)
+      , m_streambuf(0)
+      , m_capturing_stream("")
+      {}
+
+      cString Capture(ostream &out){
+        cString out_string(Release());
+
+        m_stream = &out;
+        m_streambuf = m_stream->rdbuf(m_capturing_stream.rdbuf());
+
+        return out_string;
+      }
+      cString Release(){
+        // Pull everything from the stream and put into a string.
+        cString out_string(m_capturing_stream.str().c_str());
+
+        if(m_stream && m_streambuf){
+          m_stream->rdbuf(m_streambuf);
+          m_stream = 0;
+          m_streambuf = 0;
+        }
+
+        // Clear the stream.
+        m_capturing_stream.str("");
+
+        return out_string;
+      }
+    };
+
+    void test(){
+      cout << "XXX This is a test stub. It needs filling-in. @kgn" << endl;
+      // Second iteration : cConsoleCatcher
+      {
+        cConsoleCatcher cc;
+
+        /*
+        myConsoleCatcher constructor should zero temporary storage for
+        caught stream.
+        */
+        {
+          TEST(0 == cc.m_stream);
+          TEST(0 == cc.m_streambuf);
+        }
+
+        /* Basics : Capture and Release a stream. */
+        {
+          cc.Capture(cout);
+          {
+            TEST(&cout == cc.m_stream);
+            TEST(0 != cc.m_streambuf);
+          }
+          cout << "hello, world.";
+          cString captured(cc.Release());
+          {
+            TEST(0 == cc.m_stream);
+            TEST(0 == cc.m_streambuf);
+          }
+          //cout << "captured : \"" << captured << "\"." << endl;
+
+          TEST(0 <= captured.Find("hello, world"));
+
+          TEST(0 > captured.Find("blah"));
+        }
+
+        /* What happens upon Capture-Capture-Release? */
+        {
+          cc.Capture(cout);
+          cout << "to cout";
+          cString captured_cout(cc.Capture(clog));
+          clog << "to clog";
+          cString captured_clog(cc.Release());
+
+          //cout << "captured_cout : \"" << captured_cout << "\"." << endl;
+          //cout << "captured_clog : \"" << captured_clog << "\"." << endl;
+
+          TEST(0 <= captured_cout.Find("to cout"));
+          TEST(0 <= captured_clog.Find("to clog"));
+
+          TEST(0 > captured_cout.Find("to nowhere"));
+          TEST(0 > captured_clog.Find("to nowhere"));
+        }
+
+        /* What happens upon Capture-Release-Release? */
+        {
+          cc.Capture(cout);
+          cout << "to cout";
+          cString captured_cout(cc.Release());
+          /* This should not be captured. */
+          clog << "(this text should go direct to console)" << endl;
+          cString captured_clog(cc.Release());
+
+          //cout << "captured_cout : \"" << captured_cout << "\"." << endl;
+          //cout << "captured_clog : \"" << captured_clog << "\"." << endl;
+
+          TEST(0 <= captured_cout.Find("to cout"));
+          TEST(0 >  captured_clog.Find("to clog"));
+        }
+      }
+
+      {
+        cConsoleCatcher cc;
+        /* What happens upon Release without prior capture? */
+        {
+          cString null_captured(cc.Release());
+
+          //cout << "null_captured : \"" << null_captured << "\"." << endl;
+
+          TEST(null_captured == "");
+          TEST(null_captured != "blah");
+        }
+      }
+    }
+    cAddTestSuite t("cConsoleCatcher_Brainstorm_Design", test);
+  }
+
+/* Unit Tests. {{{1 */
+  /* cConsoleCatcher_UnitTest_HelloWorld {{{2 */
+  namespace UnitTest_HelloWorld {
+    void test(const cStringList &attrs){
+      if(!attrs.HasString("HelloWorld")){
+        cout << "XXX Skipping HelloWorld test." << endl;
+        return;
+      }
+      cout << "XXX This is a test stub. It needs filling-in. @kgn" << endl;
+      /* Make sure testing is working with successes and failures. @kgn */
+      TEST(true);
+      TEST(false);
+    }
+    cAddTestSuite t("cConsoleCatcher_UnitTest_HelloWorld", test);
+  }
+
+  // }}}1
+  /*
+  This function does nothing;
+  but if the compiler sees that this function is called, then the
+  compiler will also connect the above tests to the testing library.
+  */
+  void PhysicalLink(){}
+}
+


Property changes on: extras/source/testsuites/nConsoleCatcher.cpp
___________________________________________________________________
Name: svn:mime-type
   + text/cpp




More information about the Avida-cvs mailing list