testChunkFileC.h 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. #pragma once
  2. #include <cxxtest/TestSuite.h>
  3. #include "chunkfile_c.h"
  4. #include <string>
  5. #include <iostream>
  6. std::string get_temp_dir()
  7. {
  8. static bool temp_dir_created = false;
  9. std::string temp_dir = "temp_chunkfiletest/" ;
  10. if (!temp_dir_created)
  11. {
  12. std::cout << "create_temp_dir" << endl;
  13. mkdir(temp_dir.c_str(),16877);
  14. temp_dir_created = true;
  15. }
  16. return temp_dir;
  17. }
  18. class TestChunkFileC : public CxxTest::TestSuite
  19. {
  20. public:
  21. void testDummy()
  22. {
  23. TS_ASSERT_EQUALS(3,3);
  24. }
  25. void testSaveData()
  26. {
  27. int data[] = {3,4,5,6,7,4,5,6};
  28. size_t data_size = sizeof(data)/sizeof(int);
  29. std::string dir = get_temp_dir();
  30. std::string file_path = dir + "data_file.dat";
  31. std::string format("my_funny_file_format");
  32. int major = 42;
  33. int minor = 69;
  34. ChunkFileW writer = Open_ChunkFileWriter(file_path.c_str(), format.c_str(), major, minor);
  35. TS_ASSERT(writer);
  36. if (!writer)
  37. return;
  38. ChunkFileWriter_Write("gaga", sizeof(int), data_size, (char*)data, writer);
  39. ChunkFileWriter_Close(writer);
  40. ChunkFileR reader = Open_ChunkFileReader(file_path.c_str(), format.c_str(), major, minor);
  41. TS_ASSERT(reader);
  42. if (!reader)
  43. return;
  44. size_t n_elements;
  45. size_t element_size = sizeof(int);
  46. int* loaded_data = NULL;
  47. ChunkFileError err = ChunkFileReader_Read("gaga",
  48. element_size,
  49. &n_elements,
  50. (char**) &loaded_data,
  51. reader);
  52. ChunkFileReader_Close(reader);
  53. TS_ASSERT_EQUALS(err, ChunkFile_OK);
  54. TS_ASSERT_EQUALS(n_elements, data_size);
  55. TS_ASSERT(loaded_data);
  56. if (!loaded_data)
  57. return;
  58. for (int i=0; i<n_elements; ++i) {
  59. TS_ASSERT_EQUALS(loaded_data[i], data[i]);
  60. }
  61. }
  62. };