myserver-commit
[Top][All Lists]
Advanced

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

[myserver-commit] [2830] Added tests for the `MemoryStream' class.


From: Giuseppe Scrivano
Subject: [myserver-commit] [2830] Added tests for the `MemoryStream' class.
Date: Sat, 20 Sep 2008 17:01:59 +0000

Revision: 2830
          http://svn.sv.gnu.org/viewvc/?view=rev&root=myserver&revision=2830
Author:   gscrivano
Date:     2008-09-20 17:01:59 +0000 (Sat, 20 Sep 2008)

Log Message:
-----------
Added tests for the `MemoryStream' class.  Now `MemoryStream::read' uses 
`memcpy'.

Modified Paths:
--------------
    trunk/myserver/include/filter/memory_stream.h
    trunk/myserver/src/filter/memory_stream.cpp
    trunk/myserver/tests/Makefile.am

Added Paths:
-----------
    trunk/myserver/tests/test_mem_stream.cpp

Modified: trunk/myserver/include/filter/memory_stream.h
===================================================================
--- trunk/myserver/include/filter/memory_stream.h       2008-09-20 13:50:26 UTC 
(rev 2829)
+++ trunk/myserver/include/filter/memory_stream.h       2008-09-20 17:01:59 UTC 
(rev 2830)
@@ -25,10 +25,6 @@
 
 class MemoryStream : public Stream
 {
-private:
-  int internalData;
-  MemBuf *data;
-  int readSeek;
 public:
   virtual int read(char* buffer, u_long len, u_long*);
   virtual int write(const char* buffer, u_long len, u_long*);
@@ -39,6 +35,10 @@
   MemoryStream();
   MemoryStream(MemBuf*);
   virtual ~MemoryStream();
+private:
+  int internalData;
+  MemBuf *data;
+  int readSeek;
 };
 
 

Modified: trunk/myserver/src/filter/memory_stream.cpp
===================================================================
--- trunk/myserver/src/filter/memory_stream.cpp 2008-09-20 13:50:26 UTC (rev 
2829)
+++ trunk/myserver/src/filter/memory_stream.cpp 2008-09-20 17:01:59 UTC (rev 
2830)
@@ -1,6 +1,6 @@
 /*
 MyServer
-Copyright (C) 2002, 2003, 2004 Free Software Foundation, Inc.
+Copyright (C) 2002, 2003, 2004, 2008 Free Software Foundation, Inc.
 This program is free software; you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation; either version 3 of the License, or
@@ -22,20 +22,21 @@
 #include <sstream>
 using namespace std;
 
+#include <string.h>
+
 /*!
  *Inherited from Stream.
  */
-int MemoryStream::read(char* buffer,u_long len, u_long *nbr)
+int MemoryStream::read(char* buffer, u_long len, u_long *nbr)
 {
   char *b;
-  u_long towrite;
-  towrite = *nbr = std::min(len, static_cast<u_long>(data->getLength() - 
readSeek));
+  *nbr = std::min(len, static_cast<u_long>(data->getLength() - readSeek));
   b = data->getBuffer() + readSeek;
-  while(towrite--)
-  {
-    *buffer++ = *b++;
-  }
+
+  memcpy(buffer, b, *nbr);
+
   readSeek += (*nbr);
+
   return 0;
 }
 

Modified: trunk/myserver/tests/Makefile.am
===================================================================
--- trunk/myserver/tests/Makefile.am    2008-09-20 13:50:26 UTC (rev 2829)
+++ trunk/myserver/tests/Makefile.am    2008-09-20 17:01:59 UTC (rev 2830)
@@ -2,5 +2,5 @@
 #
 
 bin_PROGRAMS = tests_suite
-tests_suite_SOURCES = main.cpp test_base64.cpp test_cached_file.cpp 
test_cached_file_buffer.cpp test_cached_file_factory.cpp test_connection.cpp 
test_connections_scheduler.cpp test_files_utility.cpp test_filter_chain.cpp 
test_ftp.cpp test_gzip.cpp test_hashmap.cpp test_homedir.cpp 
test_http_request.cpp test_http_response.cpp test_md5.cpp test_mem_buff.cpp 
test_multicast.cpp test_mutex.cpp test_recursive_mutex.cpp test_regex.cpp 
test_pipe.cpp test_safetime.cpp test_semaphore.cpp test_thread.cpp 
test_utility.cpp 
+tests_suite_SOURCES = main.cpp test_base64.cpp test_cached_file.cpp 
test_cached_file_buffer.cpp test_cached_file_factory.cpp test_connection.cpp 
test_connections_scheduler.cpp test_files_utility.cpp test_filter_chain.cpp 
test_ftp.cpp test_gzip.cpp test_hashmap.cpp test_homedir.cpp 
test_http_request.cpp test_http_response.cpp test_md5.cpp test_mem_buff.cpp 
test_mem_stream.cpp test_multicast.cpp test_mutex.cpp test_recursive_mutex.cpp 
test_regex.cpp test_pipe.cpp test_safetime.cpp test_semaphore.cpp 
test_thread.cpp test_utility.cpp 
 tests_suite_LDADD = ../src/libmyserver.a $(CPPUNIT_LDFLAGS) $(PTHREAD_LIB) 
$(IDN_LIB) $(XNET_LIB) $(EVENT_LIB) $(DL_LIB) $(OPENSSL_LIB) $(ZLIB_LIB) 
$(XML_LIBS) $(LDFLAGS)

Added: trunk/myserver/tests/test_mem_stream.cpp
===================================================================
--- trunk/myserver/tests/test_mem_stream.cpp                            (rev 0)
+++ trunk/myserver/tests/test_mem_stream.cpp    2008-09-20 17:01:59 UTC (rev 
2830)
@@ -0,0 +1,133 @@
+/*
+ MyServer
+ Copyright (C) 2008 Free Software Foundation, Inc.
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 3 of the License, or
+ (at your option) any later version.
+ 
+ This program is distributed in the hope that it will be useful, 
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ GNU General Public License for more details.
+ 
+ You should have received a copy of the GNU General Public License
+ along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include <ctype.h>
+
+#include <include/filter/memory_stream.h>
+
+#include <cppunit/CompilerOutputter.h>
+#include <cppunit/extensions/TestFactoryRegistry.h>
+#include <cppunit/ui/text/TestRunner.h>
+#include <cppunit/extensions/HelperMacros.h>
+#include <string.h>
+
+class TestMemStream : public CppUnit::TestFixture
+{
+  CPPUNIT_TEST_SUITE( TestMemStream );
+  CPPUNIT_TEST( testRead );
+  CPPUNIT_TEST( testWrite );
+  CPPUNIT_TEST( testFlush );
+  CPPUNIT_TEST( testRefresh );
+  CPPUNIT_TEST( testAvailableToRead );
+  CPPUNIT_TEST_SUITE_END();
+
+  MemoryStream *stream;
+
+public:
+  void setUp()
+  {
+    stream = new MemoryStream();
+  }
+
+  void tearDown()
+  {
+    delete stream;
+  }
+
+
+  /* Helper method.  */
+  u_long addSomeData(MemoryStream* s)
+  {
+    u_long nbw;
+
+    char data[512] = "This program is free software; you can redistribute it 
and/or modify\n\
+                      it under the terms of the GNU General Public License as 
published by\n\
+                      the Free Software Foundation; either version 3 of the 
License, or\n\
+                      (at your option) any later version.\n";
+
+    s->write(data, 512, &nbw);
+
+    return nbw;
+  }
+  
+  void testAvailableToRead()
+  {
+    u_long nbr;
+    u_long size = addSomeData(stream);
+
+    char buffer[20];
+
+    CPPUNIT_ASSERT_EQUAL(size, (u_long)stream->availableToRead());
+  } 
+
+  void testRead()
+  {
+    u_long nbr;
+    u_long size = addSomeData(stream);
+
+    char buffer[20];
+
+    CPPUNIT_ASSERT_EQUAL(size, (u_long)stream->availableToRead());
+
+    stream->read(buffer, 20, &nbr);
+    
+    CPPUNIT_ASSERT_EQUAL(size - 20u, (u_long)stream->availableToRead());
+  }
+
+
+  void testRefresh()
+  {
+    u_long nbr;
+    u_long size = addSomeData(stream);
+
+    char buffer[20];
+
+    CPPUNIT_ASSERT_EQUAL(size, (u_long)stream->availableToRead());
+
+    stream->read(buffer, 20, &nbr);
+    stream->refresh();
+
+    CPPUNIT_ASSERT_EQUAL((u_long)stream->availableToRead(), 0ul);
+  }
+
+
+  void testWrite()
+  {
+    u_long nbw;
+
+    int ret = stream->write("hello world!", 12, &nbw);
+
+    CPPUNIT_ASSERT_EQUAL(ret, 0);
+
+    CPPUNIT_ASSERT_EQUAL((u_long)stream->availableToRead(), 12ul);
+  }
+
+  void testFlush()
+  {
+    u_long nbw;
+    u_long size = addSomeData(stream);
+    int ret = stream->flush(&nbw);
+    
+    CPPUNIT_ASSERT(nbw >= 0);
+    CPPUNIT_ASSERT_EQUAL(ret, 0);
+  }
+
+
+};
+
+
+CPPUNIT_TEST_SUITE_REGISTRATION( TestMemStream );






reply via email to

[Prev in Thread] Current Thread [Next in Thread]