myserver-commit
[Top][All Lists]
Advanced

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

[myserver-commit] [2810] Added tests for `src/base/files_cache/*.cpp'


From: Giuseppe Scrivano
Subject: [myserver-commit] [2810] Added tests for `src/base/files_cache/*.cpp'
Date: Sat, 13 Sep 2008 18:44:14 +0000

Revision: 2810
          http://svn.sv.gnu.org/viewvc/?view=rev&root=myserver&revision=2810
Author:   gscrivano
Date:     2008-09-13 18:44:13 +0000 (Sat, 13 Sep 2008)

Log Message:
-----------
Added tests for `src/base/files_cache/*.cpp'

Modified Paths:
--------------
    trunk/myserver/include/base/files_cache/cached_file.h
    trunk/myserver/include/base/files_cache/cached_file_buffer.h
    trunk/myserver/include/base/files_cache/cached_file_factory.h
    trunk/myserver/src/base/files_cache/cached_file.cpp
    trunk/myserver/src/base/files_cache/cached_file_buffer.cpp
    trunk/myserver/tests/Makefile.am

Added Paths:
-----------
    trunk/myserver/tests/test_cached_file.cpp
    trunk/myserver/tests/test_cached_file_buffer.cpp
    trunk/myserver/tests/test_cached_file_factory.cpp

Modified: trunk/myserver/include/base/files_cache/cached_file.h
===================================================================
--- trunk/myserver/include/base/files_cache/cached_file.h       2008-09-13 
17:51:10 UTC (rev 2809)
+++ trunk/myserver/include/base/files_cache/cached_file.h       2008-09-13 
18:44:13 UTC (rev 2810)
@@ -1,6 +1,6 @@
 /*
 MyServer
-Copyright (C) 2006 Free Software Foundation, Inc.
+Copyright (C) 2006, 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
@@ -28,9 +28,6 @@
 
 class CachedFile : public File
 {
-protected:
-       u_long fseek;
-       CachedFileBuffer* buffer;
 public:
        CachedFile(CachedFileBuffer* buffer);
        virtual FileHandle getHandle();
@@ -51,6 +48,8 @@
 
   /*! Inherithed from Stream. */
   virtual int write(const char* buffer, u_long len, u_long *nbw);
-
+protected:
+       u_long fseek;
+       CachedFileBuffer* buffer;
 };
 #endif

Modified: trunk/myserver/include/base/files_cache/cached_file_buffer.h
===================================================================
--- trunk/myserver/include/base/files_cache/cached_file_buffer.h        
2008-09-13 17:51:10 UTC (rev 2809)
+++ trunk/myserver/include/base/files_cache/cached_file_buffer.h        
2008-09-13 18:44:13 UTC (rev 2810)
@@ -1,6 +1,6 @@
 /*
 MyServer
-Copyright (C) 2006, 2007 Free Software Foundation, Inc.
+Copyright (C) 2006, 2007, 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
@@ -31,22 +31,27 @@
 class CachedFileBuffer
 {
 public:
-       void addRef();
-       void decRef();
-       void setFactoryToNotify(CachedFileFactory *cff);
-       u_long getReferenceCounter();
-       u_long getFileSize(){return fileSize;}
-       CachedFileBuffer(const char* filename);
-       CachedFileBuffer(File* file);
-       ~CachedFileBuffer();
-       const char* getFilename(){return filename.c_str();}
-       const char* getBuffer(){return buffer;}
+  void addRef();
+  void decRef();
+  u_long getReferenceCounter();
+
+  void setFactoryToNotify(CachedFileFactory *cff);
+  CachedFileFactory* getFactoryToNotify(){return factoryToNotify;}
+
+  u_long getFileSize(){return fileSize;}
+  CachedFileBuffer(const char* filename);
+  CachedFileBuffer(File* file);
+  CachedFileBuffer(const char* buffer, u_long size);
+  ~CachedFileBuffer();
+
+  const char* getFilename(){return filename.c_str();}
+  const char* getBuffer(){return buffer;}
 protected:
-       Mutex mutex;
-       char *buffer;
-       u_long refCounter;
-       u_long fileSize;
-       CachedFileFactory *factoryToNotify;
-       string filename;
+  Mutex mutex;
+  char *buffer;
+  u_long refCounter;
+  u_long fileSize;
+  CachedFileFactory *factoryToNotify;
+  string filename;
 };
 #endif

Modified: trunk/myserver/include/base/files_cache/cached_file_factory.h
===================================================================
--- trunk/myserver/include/base/files_cache/cached_file_factory.h       
2008-09-13 17:51:10 UTC (rev 2809)
+++ trunk/myserver/include/base/files_cache/cached_file_factory.h       
2008-09-13 18:44:13 UTC (rev 2810)
@@ -1,6 +1,6 @@
 /*
 MyServer
-Copyright (C) 2006, 2007 Free Software Foundation, Inc.
+Copyright (C) 2006, 2007, 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
@@ -33,65 +33,69 @@
 class CachedFileFactory
 {
 public:
-       CachedFileFactory();
-       ~CachedFileFactory();
-       CachedFileFactory(u_long m);
-       void initialize(u_long size);
-       void clean();
-       void setSize(u_long m){size = m;}
-       File *open(const char* file);
-       void nullReferences(CachedFileBuffer* cfb);
+  CachedFileFactory();
+  ~CachedFileFactory();
+  CachedFileFactory(u_long m);
+  void initialize(u_long size);
+  void clean();
+  void setSize(u_long m){size = m;}
+  u_long getSize(){return size;}
+  u_long getUsedSize(){return usedSize;}
+  u_long getUsed(){return used;}
 
-       void setMaxSize(u_long maxSize);
-       void setMinSize(u_long minSize);
-       u_long getMaxSize();
-       u_long getMinSize();
+  File *open(const char* file);
+  void nullReferences(CachedFileBuffer* cfb);
+
+  void setMaxSize(u_long maxSize);
+  void setMinSize(u_long minSize);
+  u_long getMaxSize();
+  u_long getMinSize();
 protected:
 
-       u_long purgeRecords();
+  u_long purgeRecords();
 
 
-       Mutex mutex;
+  Mutex mutex;
 
-       /*! Max elements count for this cache.  */
-       u_long size;
+  /*! Max elements count for this cache.  */
+  u_long size;
 
-       /*! Size currently used.  */
-       u_long usedSize;
+  /*! Size currently used.  */
+  u_long usedSize;
 
-       /*! Number of times the cache was used.  */
-       u_long used;
+  /*! Number of times the cache was used.  */
+  u_long used;
 
-       /*! Cache creation time.  */
-       u_long created;
+  /*! Cache creation time.  */
+  u_long created;
 
-       /*! Max size for single file.  */
-       u_long maxSize;
+  /*! Max size for single file.  */
+  u_long maxSize;
 
-       /*! Min size for single file.  */
-       u_long minSize;
+  /*! Min size for single file.  */
+  u_long minSize;
 
-       struct CachedFileFactoryRecord
-       {
-               CachedFileBuffer* buffer;
-               /*! Number of times the cache record was used.  */
-               u_long used;
+  struct CachedFileFactoryRecord
+  {
+    CachedFileBuffer* buffer;
+    /*! Number of times the cache record was used.  */
+    u_long used;
 
-               /*! Cache record creation time.  */
-               u_long created;
+    /*! Cache record creation time.  */
+    u_long created;
 
-               /*! Last mtime for this file.  */
-               time_t mtime;
+    /*! Last mtime for this file.  */
+    time_t mtime;
 
-               /*! Last time we checked for the mtime of the buffered file.  */
-               u_long lastModTimeCheck;
+    /*! Last time we checked for the mtime of the buffered file.  */
+    u_long lastModTimeCheck;
 
-               /*! This entry is not valid and will be removed when refCount = 
0.  */
-               bool invalidCache;
-       };
+    /*! This entry is not valid and will be removed when refCount = 0.  */
+    bool invalidCache;
+  };
 
-       list<CachedFileFactoryRecord*> buffersToRemove;
-       HashMap<char*, CachedFileFactoryRecord*> buffers;
+  list<CachedFileFactoryRecord*> buffersToRemove;
+  HashMap<char*, CachedFileFactoryRecord*> buffers;
 };
 
 #endif

Modified: trunk/myserver/src/base/files_cache/cached_file.cpp
===================================================================
--- trunk/myserver/src/base/files_cache/cached_file.cpp 2008-09-13 17:51:10 UTC 
(rev 2809)
+++ trunk/myserver/src/base/files_cache/cached_file.cpp 2008-09-13 18:44:13 UTC 
(rev 2810)
@@ -179,7 +179,7 @@
   if(initialByte <= buffer->getFileSize())
     fseek = initialByte;
   else
-    return 1;
+    return -1;
 
   return 0;
 }

Modified: trunk/myserver/src/base/files_cache/cached_file_buffer.cpp
===================================================================
--- trunk/myserver/src/base/files_cache/cached_file_buffer.cpp  2008-09-13 
17:51:10 UTC (rev 2809)
+++ trunk/myserver/src/base/files_cache/cached_file_buffer.cpp  2008-09-13 
18:44:13 UTC (rev 2810)
@@ -117,6 +117,23 @@
 }
 
 /*!
+ *Load a file in the buffer from a memory location.
+ *\param buffer The buffer to copy.
+ *\param size The memory buffer size.
+ */
+CachedFileBuffer::CachedFileBuffer(const char* buffer, u_long size)
+{
+  u_long nbr;
+  factoryToNotify = 0;
+  refCounter = 0;
+
+  fileSize = size;
+  this->buffer = new char[size];
+
+  memcpy(this->buffer, buffer, size);
+}
+
+/*!
  *Load a file in the buffer.
  *\param file The file object.
  */

Modified: trunk/myserver/tests/Makefile.am
===================================================================
--- trunk/myserver/tests/Makefile.am    2008-09-13 17:51:10 UTC (rev 2809)
+++ trunk/myserver/tests/Makefile.am    2008-09-13 18:44:13 UTC (rev 2810)
@@ -2,5 +2,5 @@
 #
 
 bin_PROGRAMS = tests_suite
-tests_suite_SOURCES = main.cpp test_base64.cpp test_connection.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_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_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_cached_file.cpp
===================================================================
--- trunk/myserver/tests/test_cached_file.cpp                           (rev 0)
+++ trunk/myserver/tests/test_cached_file.cpp   2008-09-13 18:44:13 UTC (rev 
2810)
@@ -0,0 +1,111 @@
+/*
+ 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 <cppunit/CompilerOutputter.h>
+#include <cppunit/ui/text/TestRunner.h>
+#include <cppunit/extensions/HelperMacros.h>
+#include <include/base/files_cache/cached_file.h>
+#include <string.h>
+
+#include <string>
+
+using namespace std;
+
+class TestCachedFile : public CppUnit::TestFixture
+{
+  CPPUNIT_TEST_SUITE( TestCachedFile );
+
+  CPPUNIT_TEST(testFilePointer);
+  CPPUNIT_TEST(testGetHandle);
+  CPPUNIT_TEST(testOpenFile);
+  CPPUNIT_TEST(testCreateTemporaryFile);
+  CPPUNIT_TEST(testWrite);
+  CPPUNIT_TEST(testRead);
+
+  CPPUNIT_TEST_SUITE_END();
+  
+  CachedFileBuffer *cfb;
+  CachedFile *cf;
+
+public:
+  void setUp()
+  {
+    const char* buffer = "hello world!!!";
+
+    cfb = new CachedFileBuffer(buffer, strlen(buffer));
+
+    cf = new CachedFile(cfb);
+  }
+
+  void tearDown()
+  {
+    delete cf;
+    delete cfb;
+  }
+
+
+  void testFilePointer()
+  {
+    CPPUNIT_ASSERT_EQUAL(cf->setFilePointer(3), 0);
+    CPPUNIT_ASSERT(cf->setFilePointer(100000));
+  }
+
+
+  void testGetHandle()
+  {
+    CPPUNIT_ASSERT_EQUAL(cf->getHandle(), (FileHandle)-1);
+  }
+
+  void testOpenFile()
+  {
+    string filename;
+    CPPUNIT_ASSERT(cf->openFile(NULL, 0));
+    CPPUNIT_ASSERT(cf->openFile(filename, 0));
+  }
+
+  void testCreateTemporaryFile()
+  {
+    CPPUNIT_ASSERT(cf->createTemporaryFile(NULL));
+  }
+
+  void testWrite()
+  {
+    CPPUNIT_ASSERT(cf->writeToFile(NULL, 0, NULL));
+  }
+  
+  void testRead()
+  {
+    u_long nbr = 0;
+    u_long size = cf->getFileSize();
+
+    CPPUNIT_ASSERT(size > 0);
+
+    char *buffer =new char[size];
+
+    int ret = cf->readFromFile(buffer, size, &nbr);
+
+    CPPUNIT_ASSERT_EQUAL(ret, 0);
+    CPPUNIT_ASSERT(nbr > 0);
+
+    delete [] buffer;
+  }
+
+
+
+};
+
+CPPUNIT_TEST_SUITE_REGISTRATION( TestCachedFile );

Added: trunk/myserver/tests/test_cached_file_buffer.cpp
===================================================================
--- trunk/myserver/tests/test_cached_file_buffer.cpp                            
(rev 0)
+++ trunk/myserver/tests/test_cached_file_buffer.cpp    2008-09-13 18:44:13 UTC 
(rev 2810)
@@ -0,0 +1,86 @@
+/*
+ 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 <cppunit/CompilerOutputter.h>
+#include <cppunit/ui/text/TestRunner.h>
+#include <cppunit/extensions/HelperMacros.h>
+#include <include/base/files_cache/cached_file_buffer.h>
+#include <include/base/files_cache/cached_file_factory.h>
+#include <string.h>
+
+#include <string>
+
+using namespace std;
+
+class TestCachedFileBuffer : public CppUnit::TestFixture
+{
+  CPPUNIT_TEST_SUITE( TestCachedFileBuffer );
+
+  CPPUNIT_TEST(testGetFileSize);
+  CPPUNIT_TEST(testFactoryToNotify);
+  CPPUNIT_TEST(testRefCounter);
+  CPPUNIT_TEST_SUITE_END();
+  
+  CachedFileBuffer *cfb;
+public:
+  void setUp()
+  {
+    const char* buffer = "hello world!!!";
+
+    cfb = new CachedFileBuffer(buffer, strlen(buffer));
+  }
+
+  void tearDown()
+  {
+    delete cfb;
+  }
+
+  void testGetFileSize()
+  {
+    CPPUNIT_ASSERT(cfb->getFileSize());
+  }
+
+
+  void testFactoryToNotify()
+  {
+    CachedFileFactory cff;
+    
+    cfb->setFactoryToNotify(&cff);
+    CPPUNIT_ASSERT_EQUAL(cfb->getFactoryToNotify(), &cff);
+
+    cfb->setFactoryToNotify(NULL);
+    CPPUNIT_ASSERT_EQUAL(cfb->getFactoryToNotify(), (CachedFileFactory*)NULL);
+  }
+
+  void testRefCounter()
+  {
+    for(u_long i = 1; i <= 20; i++)
+    {
+      cfb->addRef();
+      CPPUNIT_ASSERT_EQUAL(cfb->getReferenceCounter(), i);
+    }
+
+    for(u_long i = 1; i <= 20; i++)
+    {
+      cfb->decRef();
+      CPPUNIT_ASSERT_EQUAL(cfb->getReferenceCounter(), 20 - i);
+    }
+
+  }
+};
+
+CPPUNIT_TEST_SUITE_REGISTRATION( TestCachedFileBuffer );

Added: trunk/myserver/tests/test_cached_file_factory.cpp
===================================================================
--- trunk/myserver/tests/test_cached_file_factory.cpp                           
(rev 0)
+++ trunk/myserver/tests/test_cached_file_factory.cpp   2008-09-13 18:44:13 UTC 
(rev 2810)
@@ -0,0 +1,93 @@
+/*
+ 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 <cppunit/CompilerOutputter.h>
+#include <cppunit/extensions/TestFactoryRegistry.h>
+#include <cppunit/ui/text/TestRunner.h>
+#include <cppunit/extensions/HelperMacros.h>
+#include <include/base/files_cache/cached_file_factory.h>
+#include <string.h>
+
+#include <string>
+
+using namespace std;
+
+class TestCachedFileFactory : public CppUnit::TestFixture
+{
+  CPPUNIT_TEST_SUITE( TestCachedFileFactory );
+
+  CPPUNIT_TEST(testInitialize);
+  CPPUNIT_TEST(testSize);
+  CPPUNIT_TEST(testMaxSize);
+  CPPUNIT_TEST(testMinSize);
+
+  CPPUNIT_TEST_SUITE_END();
+  CachedFileFactory *cff;
+
+public:
+  void setUp()
+  {
+    cff = new CachedFileFactory();
+  }
+
+  void tearDown()
+  {
+    delete cff;
+  }
+
+  void testInitialize()
+  {
+    cff->initialize(20);
+
+    CPPUNIT_ASSERT_EQUAL(cff->getSize(), 20ul);
+    CPPUNIT_ASSERT_EQUAL(cff->getUsed(), 0ul);
+    CPPUNIT_ASSERT_EQUAL(cff->getUsedSize(), 0ul);
+    CPPUNIT_ASSERT_EQUAL(cff->getMaxSize(), 0ul);
+    CPPUNIT_ASSERT_EQUAL(cff->getMinSize(), 0ul);
+    CPPUNIT_ASSERT_EQUAL(cff->getMaxSize(), 0ul);
+  }
+
+
+  void testSize()
+  {
+    for(u_long i = 0; i < 100; i += 10)
+    {
+      cff->setSize(i);
+      CPPUNIT_ASSERT_EQUAL(cff->getSize(), i);
+    }
+  }
+
+  void testMaxSize()
+  {
+    for(u_long i = 0; i < 100; i += 10)
+    {
+      cff->setMaxSize(i);
+      CPPUNIT_ASSERT_EQUAL(cff->getMaxSize(), i);
+    }
+  }
+
+  void testMinSize()
+  {
+    for(u_long i = 0; i < 100; i += 10)
+    {
+      cff->setMinSize(i);
+      CPPUNIT_ASSERT_EQUAL(cff->getMinSize(), i);
+    }
+  }
+};
+
+CPPUNIT_TEST_SUITE_REGISTRATION( TestCachedFileFactory );






reply via email to

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