SingleFileAllocationIteratorTest.cc 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. #include "SingleFileAllocationIterator.h"
  2. #include <fstream>
  3. #include <cppunit/extensions/HelperMacros.h>
  4. #include "File.h"
  5. #include "DefaultDiskWriter.h"
  6. #include "a2functional.h"
  7. namespace aria2 {
  8. class SingleFileAllocationIteratorTest : public CppUnit::TestFixture {
  9. CPPUNIT_TEST_SUITE(SingleFileAllocationIteratorTest);
  10. CPPUNIT_TEST(testAllocate);
  11. CPPUNIT_TEST_SUITE_END();
  12. private:
  13. public:
  14. void setUp() {}
  15. void testAllocate();
  16. };
  17. CPPUNIT_TEST_SUITE_REGISTRATION(SingleFileAllocationIteratorTest);
  18. void SingleFileAllocationIteratorTest::testAllocate()
  19. {
  20. std::string dir = A2_TEST_OUT_DIR;
  21. std::string fname = "aria2_SingleFileAllocationIteratorTest_testAllocate";
  22. std::string fn = dir + "/" + fname;
  23. std::ofstream of(fn.c_str(), std::ios::binary);
  24. of << "0123456789";
  25. of.close();
  26. File x(fn);
  27. CPPUNIT_ASSERT_EQUAL((int64_t)10, x.size());
  28. DefaultDiskWriter writer(fn);
  29. int64_t offset = 10;
  30. int64_t totalLength = 32_k + 8_k;
  31. // we have to open file first.
  32. writer.openExistingFile();
  33. SingleFileAllocationIterator itr(&writer, offset, totalLength);
  34. itr.init();
  35. while (!itr.finished()) {
  36. itr.allocateChunk();
  37. }
  38. File f(fn);
  39. CPPUNIT_ASSERT_EQUAL((int64_t)40_k, f.size());
  40. }
  41. } // namespace aria2