file_ut.cpp 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. #include "file.h"
  2. #include <library/cpp/testing/unittest/registar.h>
  3. #include <util/system/tempfile.h>
  4. static const char* TmpFileName = "./fileio";
  5. static const char* TmpFileContents = "To do good to Mankind is the chivalrous plan";
  6. static const char* TmpFileSubstring = strstr(TmpFileContents, "chivalrous");
  7. Y_UNIT_TEST_SUITE(TFileTest) {
  8. Y_UNIT_TEST(InputTest) {
  9. TTempFile tmp(TmpFileName);
  10. {
  11. TUnbufferedFileOutput output(TmpFileName);
  12. output.Write(TmpFileContents, strlen(TmpFileContents));
  13. }
  14. {
  15. TUnbufferedFileInput input(TmpFileName);
  16. TString s = input.ReadAll();
  17. UNIT_ASSERT_VALUES_EQUAL(s, TmpFileContents);
  18. }
  19. {
  20. TUnbufferedFileInput input(TmpFileName);
  21. input.Skip(TmpFileSubstring - TmpFileContents);
  22. TString s = input.ReadAll();
  23. UNIT_ASSERT_VALUES_EQUAL(s, "chivalrous plan");
  24. }
  25. {
  26. TUnbufferedFileOutput output(TFile::ForAppend(TmpFileName));
  27. output.Write(TmpFileContents, strlen(TmpFileContents));
  28. }
  29. {
  30. TUnbufferedFileInput input(TmpFileName);
  31. TString s = input.ReadAll();
  32. UNIT_ASSERT_VALUES_EQUAL(s, TString::Join(TmpFileContents, TmpFileContents));
  33. }
  34. }
  35. Y_UNIT_TEST(EmptyMapTest) {
  36. TTempFile tmp(TmpFileName);
  37. {
  38. TUnbufferedFileOutput output(TmpFileName);
  39. /* Write nothing. */
  40. }
  41. {
  42. TMappedFileInput input(TmpFileName);
  43. TString s = input.ReadAll();
  44. UNIT_ASSERT(s.empty());
  45. }
  46. }
  47. #ifdef _unix_
  48. Y_UNIT_TEST(PipeReadLineTest) {
  49. int fds[2];
  50. UNIT_ASSERT(pipe(fds) == 0);
  51. TFile readEnd(fds[0]);
  52. TFileInput fileInput(readEnd);
  53. UNIT_ASSERT_VALUES_EQUAL(write(fds[1], "hello\n", 6), 6);
  54. TString line;
  55. UNIT_ASSERT(fileInput.ReadLine(line));
  56. UNIT_ASSERT_STRINGS_EQUAL(line, "hello");
  57. close(fds[1]);
  58. }
  59. #endif
  60. }