GlowFileAllocatorTest.cc 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. #include "GlowFileAllocator.h"
  2. #include "File.h"
  3. #include <sys/types.h>
  4. #include <sys/stat.h>
  5. #include <fcntl.h>
  6. #include <fstream>
  7. #include <iomanip>
  8. #include <cppunit/extensions/HelperMacros.h>
  9. class GlowFileAllocatorTest:public CppUnit::TestFixture {
  10. CPPUNIT_TEST_SUITE(GlowFileAllocatorTest);
  11. CPPUNIT_TEST(testAllocate);
  12. CPPUNIT_TEST_SUITE_END();
  13. private:
  14. public:
  15. void setUp() {}
  16. void testAllocate();
  17. };
  18. CPPUNIT_TEST_SUITE_REGISTRATION( GlowFileAllocatorTest );
  19. void GlowFileAllocatorTest::testAllocate()
  20. {
  21. string fn = "/tmp/aria2_GlowFileAllocatorTest_testAllocate";
  22. ofstream of(fn.c_str());
  23. of << "0123456789";
  24. of.close();
  25. File x("/tmp/aria2_GlowFileAllocatorTest_testAllocate");
  26. CPPUNIT_ASSERT_EQUAL((int64_t)10, x.size());
  27. int fd;
  28. if((fd = open(fn.c_str(), O_RDWR, S_IRUSR|S_IWUSR)) < 0) {
  29. CPPUNIT_FAIL("cannot open file");
  30. }
  31. GlowFileAllocator allocator;
  32. allocator.allocate(fd, 4097);
  33. if(close(fd) < 0) {
  34. CPPUNIT_FAIL("cannot close file");
  35. }
  36. ifstream is(fn.c_str());
  37. char buf[11];
  38. is >> std::setw(sizeof(buf)) >> buf;
  39. CPPUNIT_ASSERT(strcmp("0123456789", buf) == 0);
  40. File f(fn);
  41. CPPUNIT_ASSERT_EQUAL((int64_t)4097, f.size());
  42. }