MemoryBufferFile_test.cc 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. #include "catch.hpp"
  2. #include "MemoryBufferFile.hh"
  3. #include "File.hh"
  4. using namespace openmsx;
  5. TEST_CASE("MemoryBufferFile")
  6. {
  7. uint8_t buffer[] = {
  8. 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
  9. };
  10. uint8_t tmp[100];
  11. // create memory-backed-file
  12. File file = memory_buffer_file(buffer);
  13. CHECK(file.getPos() == 0);
  14. CHECK(file.isReadOnly());
  15. CHECK(file.getSize() == 10);
  16. // seek and read small part
  17. file.seek(2);
  18. CHECK(file.getPos() == 2);
  19. file.read(tmp, 3);
  20. CHECK(file.getPos() == 5);
  21. CHECK(tmp[0] == 3);
  22. CHECK(tmp[1] == 4);
  23. CHECK(tmp[2] == 5);
  24. // seek beyond end of file is ok, but reading there is not
  25. file.seek(100);
  26. CHECK(file.getPos() == 100);
  27. CHECK_THROWS(file.read(tmp, 2));
  28. // try to read more than file size
  29. file.seek(0);
  30. CHECK(file.getPos() == 0);
  31. CHECK_THROWS(file.read(tmp, 100));
  32. CHECK(file.getPos() == 0);
  33. // read full file
  34. file.seek(0);
  35. file.read(tmp, file.getSize());
  36. for (int i = 0; i < 10; ++i) {
  37. CHECK(tmp[i] == (i + 1));
  38. }
  39. CHECK(file.getPos() == file.getSize());
  40. // writing is not supported
  41. CHECK_THROWS(file.write(tmp, file.getSize()));
  42. }