bin_file.cpp 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. #include<__vic/bin_file.h>
  2. #include<iostream>
  3. #include<exception>
  4. #include<cstdio>
  5. #include<cassert>
  6. void copy_test()
  7. {
  8. __vic::bin_file in("bin_file.cpp", __vic::bin_file::in);
  9. assert(in.is_open());
  10. __vic::bin_file out("bin_file.copy", __vic::bin_file::out);
  11. assert(out.is_open());
  12. char buf[BUFSIZ];
  13. while(size_t n = in.read_some(buf, sizeof buf))
  14. out.write_all(buf, n);
  15. in.close();
  16. assert(!in.is_open());
  17. out.close();
  18. assert(!out.is_open());
  19. // TODO: assert content of the files is identical
  20. assert(std::remove("bin_file.copy") == 0);
  21. }
  22. void throw_if_closed_test()
  23. {
  24. __vic::bin_file file("file_doesnt_exist", __vic::bin_file::in);
  25. if(!file.is_open()) file.open_in("bin_file.cpp");
  26. file.throw_if_closed("Cannot open file");
  27. }
  28. void throw_last_error_test()
  29. {
  30. __vic::bin_file file;
  31. if(!file.open_in("bin_file.cpp"))
  32. file.throw_last_error("Cannot open file");
  33. }
  34. void run_tests()
  35. {
  36. copy_test();
  37. }
  38. int main()
  39. {
  40. try
  41. {
  42. run_tests();
  43. return 0;
  44. }
  45. catch(const std::exception &ex)
  46. {
  47. std::cerr << ex.what() << '\n';
  48. }
  49. return 1;
  50. }