skiff_schema_ut.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. #include <library/cpp/skiff/skiff.h>
  2. #include <library/cpp/skiff/skiff_schema.h>
  3. #include <library/cpp/testing/unittest/registar.h>
  4. #include <util/stream/buffer.h>
  5. using namespace NSkiff;
  6. ////////////////////////////////////////////////////////////////////////////////
  7. template<>
  8. void Out<TSkiffSchema>(IOutputStream& s, const TSkiffSchema& schema)
  9. {
  10. s << "TSkiffSchema:" << GetShortDebugString(schema.shared_from_this());
  11. }
  12. ////////////////////////////////////////////////////////////////////////////////
  13. Y_UNIT_TEST_SUITE(TSkiffSchemaTestSuite) {
  14. Y_UNIT_TEST(TestIntEqual)
  15. {
  16. std::shared_ptr<TSkiffSchema> schema1 = CreateSimpleTypeSchema(EWireType::Uint64);
  17. schema1->SetName("schema");
  18. std::shared_ptr<TSkiffSchema> schema2 = CreateSimpleTypeSchema(EWireType::Uint64);
  19. schema2->SetName("schema");
  20. UNIT_ASSERT_VALUES_EQUAL(*schema1, *schema2);
  21. }
  22. Y_UNIT_TEST(TestTupleEqual)
  23. {
  24. std::shared_ptr<TSkiffSchema> schema1 = CreateTupleSchema({
  25. CreateSimpleTypeSchema(EWireType::Int64),
  26. CreateSimpleTypeSchema(EWireType::String32),
  27. });
  28. std::shared_ptr<TSkiffSchema> schema2 = CreateTupleSchema({
  29. CreateSimpleTypeSchema(EWireType::Int64),
  30. CreateSimpleTypeSchema(EWireType::String32),
  31. });
  32. Cerr << *schema1 << Endl;
  33. schema1->SetName("schema");
  34. UNIT_ASSERT_VALUES_UNEQUAL(*schema1, *schema2);
  35. schema2->SetName("schema");
  36. UNIT_ASSERT_VALUES_EQUAL(*schema1, *schema2);
  37. }
  38. Y_UNIT_TEST(TestHashes)
  39. {
  40. TSet<size_t> hashes;
  41. auto schema = CreateSimpleTypeSchema(EWireType::Uint64);
  42. schema->SetName("schema");
  43. hashes.insert(THash<NSkiff::TSkiffSchema>()(*schema));
  44. schema = CreateSimpleTypeSchema(EWireType::Uint64);
  45. hashes.insert(THash<NSkiff::TSkiffSchema>()(*schema));
  46. auto schema2 = CreateTupleSchema({
  47. CreateSimpleTypeSchema(EWireType::Int64),
  48. CreateSimpleTypeSchema(EWireType::String32),
  49. });
  50. schema2->SetName("s");
  51. hashes.insert(THash<NSkiff::TSkiffSchema>()(*schema2));
  52. schema2->SetName("s0");
  53. hashes.insert(THash<NSkiff::TSkiffSchema>()(*schema2));
  54. schema2->SetName("s");
  55. hashes.insert(THash<NSkiff::TSkiffSchema>()(*schema2));
  56. auto schema3 = CreateRepeatedVariant16Schema({
  57. CreateSimpleTypeSchema(EWireType::Int64),
  58. schema2,
  59. });
  60. hashes.insert(THash<NSkiff::TSkiffSchema>()(*schema3));
  61. schema3->SetName("kek");
  62. hashes.insert(THash<NSkiff::TSkiffSchema>()(*schema3));
  63. auto schema4 = CreateRepeatedVariant8Schema({
  64. CreateSimpleTypeSchema(EWireType::Int64),
  65. schema2,
  66. });
  67. hashes.insert(THash<NSkiff::TSkiffSchema>()(*schema4));
  68. schema4->SetName("kek");
  69. hashes.insert(THash<NSkiff::TSkiffSchema>()(*schema4));
  70. UNIT_ASSERT_VALUES_EQUAL(hashes.size(), 8);
  71. }
  72. Y_UNIT_TEST(TestDifferent)
  73. {
  74. TVector<std::shared_ptr<TSkiffSchema>> schemas;
  75. auto schema = CreateSimpleTypeSchema(EWireType::Uint64);
  76. schema->SetName("schema");
  77. schemas.push_back(schema);
  78. schemas.push_back(CreateSimpleTypeSchema(EWireType::Uint64));
  79. auto schema2 = CreateTupleSchema({
  80. CreateSimpleTypeSchema(EWireType::Int64),
  81. CreateSimpleTypeSchema(EWireType::String32),
  82. });
  83. schema2->SetName("s");
  84. schemas.push_back(schema2);
  85. auto schema3 = CreateTupleSchema({
  86. CreateSimpleTypeSchema(EWireType::Int64),
  87. CreateSimpleTypeSchema(EWireType::String32),
  88. });
  89. schema3->SetName("s0");
  90. schemas.push_back(schema3);
  91. auto schema4 = CreateRepeatedVariant16Schema({
  92. CreateSimpleTypeSchema(EWireType::Int64),
  93. schema2,
  94. });
  95. schemas.push_back(schema4);
  96. auto schema5 = CreateRepeatedVariant16Schema({
  97. CreateSimpleTypeSchema(EWireType::Int64),
  98. schema2,
  99. });
  100. schema5->SetName("kek");
  101. schemas.push_back(schema5);
  102. auto schema6 = CreateRepeatedVariant8Schema({
  103. CreateSimpleTypeSchema(EWireType::Int64),
  104. schema2,
  105. });
  106. schemas.push_back(schema6);
  107. for (size_t i = 0; i < schemas.size(); ++i) {
  108. for (size_t j = i + 1; j < schemas.size(); ++j) {
  109. UNIT_ASSERT_VALUES_UNEQUAL(*schemas[i], *schemas[j]);
  110. }
  111. }
  112. }
  113. }
  114. ////////////////////////////////////////////////////////////////////////////////