aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/skiff/unittests/skiff_schema_ut.cpp
blob: c20a560dfccd92358e6db71c43f37cae9092760a (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
#include <library/cpp/skiff/skiff.h>
#include <library/cpp/skiff/skiff_schema.h>

#include <library/cpp/testing/unittest/registar.h>

#include <util/stream/buffer.h>

using namespace NSkiff;

////////////////////////////////////////////////////////////////////////////////

template<>
void Out<TSkiffSchema>(IOutputStream& s, const TSkiffSchema& schema)
{
    s << "TSkiffSchema:" << GetShortDebugString(schema.shared_from_this());
}

////////////////////////////////////////////////////////////////////////////////

Y_UNIT_TEST_SUITE(TSkiffSchemaTestSuite) {
    Y_UNIT_TEST(TestIntEqual)
    {
        std::shared_ptr<TSkiffSchema> schema1 = CreateSimpleTypeSchema(EWireType::Uint64);
        schema1->SetName("schema");

        std::shared_ptr<TSkiffSchema> schema2 = CreateSimpleTypeSchema(EWireType::Uint64);
        schema2->SetName("schema");

        UNIT_ASSERT_VALUES_EQUAL(*schema1, *schema2);
    }

    Y_UNIT_TEST(TestTupleEqual)
    {
        std::shared_ptr<TSkiffSchema> schema1 = CreateTupleSchema({
            CreateSimpleTypeSchema(EWireType::Int64),
            CreateSimpleTypeSchema(EWireType::String32),
        });

        std::shared_ptr<TSkiffSchema> schema2 = CreateTupleSchema({
            CreateSimpleTypeSchema(EWireType::Int64),
            CreateSimpleTypeSchema(EWireType::String32),
        });

        Cerr << *schema1 << Endl;

        schema1->SetName("schema");
        UNIT_ASSERT_VALUES_UNEQUAL(*schema1, *schema2);

        schema2->SetName("schema");
        UNIT_ASSERT_VALUES_EQUAL(*schema1, *schema2);
    }

    Y_UNIT_TEST(TestHashes)
    {
        TSet<size_t> hashes;

        auto schema = CreateSimpleTypeSchema(EWireType::Uint64);
        schema->SetName("schema");
        hashes.insert(THash<NSkiff::TSkiffSchema>()(*schema));

        schema = CreateSimpleTypeSchema(EWireType::Uint64);
        hashes.insert(THash<NSkiff::TSkiffSchema>()(*schema));

        auto schema2 = CreateTupleSchema({
            CreateSimpleTypeSchema(EWireType::Int64),
            CreateSimpleTypeSchema(EWireType::String32),
        });
        schema2->SetName("s");
        hashes.insert(THash<NSkiff::TSkiffSchema>()(*schema2));

        schema2->SetName("s0");
        hashes.insert(THash<NSkiff::TSkiffSchema>()(*schema2));

        schema2->SetName("s");
        hashes.insert(THash<NSkiff::TSkiffSchema>()(*schema2));

        auto schema3 = CreateRepeatedVariant16Schema({
            CreateSimpleTypeSchema(EWireType::Int64),
            schema2,
        });
        hashes.insert(THash<NSkiff::TSkiffSchema>()(*schema3));

        schema3->SetName("kek");
        hashes.insert(THash<NSkiff::TSkiffSchema>()(*schema3));

        auto schema4 = CreateRepeatedVariant8Schema({
            CreateSimpleTypeSchema(EWireType::Int64),
            schema2,
        });
        hashes.insert(THash<NSkiff::TSkiffSchema>()(*schema4));

        schema4->SetName("kek");
        hashes.insert(THash<NSkiff::TSkiffSchema>()(*schema4));

        UNIT_ASSERT_VALUES_EQUAL(hashes.size(), 8);
    }

    Y_UNIT_TEST(TestDifferent)
    {
        TVector<std::shared_ptr<TSkiffSchema>> schemas;

        auto schema = CreateSimpleTypeSchema(EWireType::Uint64);
        schema->SetName("schema");
        schemas.push_back(schema);
        schemas.push_back(CreateSimpleTypeSchema(EWireType::Uint64));

        auto schema2 = CreateTupleSchema({
            CreateSimpleTypeSchema(EWireType::Int64),
            CreateSimpleTypeSchema(EWireType::String32),
        });
        schema2->SetName("s");
        schemas.push_back(schema2);

        auto schema3 = CreateTupleSchema({
            CreateSimpleTypeSchema(EWireType::Int64),
            CreateSimpleTypeSchema(EWireType::String32),
        });
        schema3->SetName("s0");
        schemas.push_back(schema3);

        auto schema4 = CreateRepeatedVariant16Schema({
            CreateSimpleTypeSchema(EWireType::Int64),
            schema2,
        });
        schemas.push_back(schema4);

        auto schema5 = CreateRepeatedVariant16Schema({
            CreateSimpleTypeSchema(EWireType::Int64),
            schema2,
        });
        schema5->SetName("kek");
        schemas.push_back(schema5);

        auto schema6 = CreateRepeatedVariant8Schema({
            CreateSimpleTypeSchema(EWireType::Int64),
            schema2,
        });
        schemas.push_back(schema6);

        for (size_t i = 0; i < schemas.size(); ++i) {
            for (size_t j = i + 1; j < schemas.size(); ++j) {
                UNIT_ASSERT_VALUES_UNEQUAL(*schemas[i], *schemas[j]);
            }
        }
    }
}

////////////////////////////////////////////////////////////////////////////////