yarchive_ut.cpp 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. #include "yarchive.h"
  2. #include <library/cpp/testing/unittest/registar.h>
  3. #include <util/string/cast.h>
  4. #include <util/stream/file.h>
  5. #include <util/system/tempfile.h>
  6. #include <util/memory/blob.h>
  7. class TArchiveTest: public TTestBase {
  8. UNIT_TEST_SUITE(TArchiveTest)
  9. UNIT_TEST(TestCreate);
  10. UNIT_TEST(TestRead);
  11. UNIT_TEST(TestOffsetOrder);
  12. UNIT_TEST_SUITE_END();
  13. private:
  14. void CreateArchive();
  15. void TestCreate();
  16. void TestRead();
  17. void TestOffsetOrder();
  18. };
  19. UNIT_TEST_SUITE_REGISTRATION(TArchiveTest);
  20. #define ARCHIVE "./test.ar"
  21. void TArchiveTest::CreateArchive() {
  22. TFixedBufferFileOutput out(ARCHIVE);
  23. TArchiveWriter w(&out);
  24. for (size_t i = 0; i < 1000; ++i) {
  25. const TString path = "/" + ToString(i);
  26. const TString data = "data" + ToString(i * 1000) + "dataend";
  27. TStringInput si(data);
  28. w.Add(path, &si);
  29. }
  30. w.Finish();
  31. out.Finish();
  32. }
  33. void TArchiveTest::TestCreate() {
  34. CreateArchive();
  35. TTempFile tmpFile(ARCHIVE);
  36. }
  37. void TArchiveTest::TestRead() {
  38. CreateArchive();
  39. TTempFile tmpFile(ARCHIVE);
  40. TBlob blob = TBlob::FromFileSingleThreaded(ARCHIVE);
  41. TArchiveReader r(blob);
  42. UNIT_ASSERT_EQUAL(r.Count(), 1000);
  43. for (size_t i = 0; i < 1000; ++i) {
  44. const TString key = "/" + ToString(i);
  45. TAutoPtr<IInputStream> is = r.ObjectByKey(key);
  46. const TString data = is->ReadAll();
  47. UNIT_ASSERT_EQUAL(data, "data" + ToString(i * 1000) + "dataend");
  48. }
  49. }
  50. void TArchiveTest::TestOffsetOrder() {
  51. CreateArchive();
  52. TTempFile tmpFile(ARCHIVE);
  53. TBlob blob1 = TBlob::FromFileSingleThreaded(ARCHIVE);
  54. TArchiveReader r(blob1);
  55. const void* prevOffset = nullptr;
  56. for (size_t i = 0; i < r.Count(); ++i) {
  57. const TString key = r.KeyByIndex(i);
  58. TBlob blob2 = r.BlobByKey(key);
  59. const void* offset = blob2.Data();
  60. if (i) {
  61. UNIT_ASSERT(prevOffset < offset);
  62. }
  63. prevOffset = offset;
  64. }
  65. }