summaryrefslogtreecommitdiff
path: root/src/mongo/bson/mutable/mutable_bson_test_utils.cpp
blob: 6c332af92b536be148c453c2ca3e60b7cf72a3ee (plain)
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221

/**
 *    Copyright (C) 2018-present MongoDB, Inc.
 *
 *    This program is free software: you can redistribute it and/or modify
 *    it under the terms of the Server Side Public License, version 1,
 *    as published by MongoDB, Inc.
 *
 *    This program is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *    Server Side Public License for more details.
 *
 *    You should have received a copy of the Server Side Public License
 *    along with this program. If not, see
 *    <http://www.mongodb.com/licensing/server-side-public-license>.
 *
 *    As a special exception, the copyright holders give permission to link the
 *    code of portions of this program with the OpenSSL library under certain
 *    conditions as described in each individual source file and distribute
 *    linked combinations including the program with the OpenSSL library. You
 *    must comply with the Server Side Public License in all respects for
 *    all of the code used other than as permitted herein. If you modify file(s)
 *    with this exception, you may extend this exception to your version of the
 *    file(s), but you are not obligated to do so. If you do not wish to do so,
 *    delete this exception statement from your version. If you delete this
 *    exception statement from all source files in the program, then also delete
 *    it in the license file.
 */

#include "mongo/platform/basic.h"

#include "mongo/bson/mutable/mutable_bson_test_utils.h"

#include <algorithm>
#include <ostream>
#include <vector>

#include "mongo/bson/bsonobj.h"
#include "mongo/bson/mutable/algorithm.h"
#include "mongo/bson/mutable/const_element.h"
#include "mongo/bson/mutable/document.h"
#include "mongo/unittest/unittest.h"

namespace mongo {
namespace mutablebson {

namespace {

inline void assertSameSign(int lhs, int rhs) {
    if (lhs == 0) {
        ASSERT_EQUALS(rhs, 0);
    } else if (lhs < 0) {
        ASSERT_LESS_THAN(rhs, 0);
    } else {
        ASSERT_GREATER_THAN(rhs, 0);
    }
}

inline void assertOppositeSign(int lhs, int rhs) {
    if (lhs == 0) {
        ASSERT_EQUALS(rhs, 0);
    } else if (lhs < 0) {
        ASSERT_GREATER_THAN(rhs, 0);
    } else {
        ASSERT_LESS_THAN(rhs, 0);
    }
}

void addChildrenToVector(ConstElement elt, std::vector<ConstElement>* accumulator) {
    ConstElement current = elt.leftChild();
    while (current.ok()) {
        accumulator->push_back(current);
        current = current.rightSibling();
    }
}

bool checkDocNoOrderingImpl(ConstElement lhs, ConstElement rhs) {
    const BSONType lhsType = lhs.getType();
    const BSONType rhsType = rhs.getType();

    if (lhsType == mongo::Object) {
        if (rhsType != mongo::Object)
            return false;

        // For objects, sort the children by field name, then compare in that order.

        std::vector<ConstElement> lhsChildren;
        addChildrenToVector(lhs, &lhsChildren);
        std::vector<ConstElement> rhsChildren;
        addChildrenToVector(rhs, &rhsChildren);
        if (lhsChildren.size() != rhsChildren.size())
            return false;

        // NOTE: if you have repeated field names, this is not necessarily going to
        // work. This is unlikely to be a problem in practice, but we could write a
        // more sophisticated comparator if we need to: perhaps one that ordered first
        // by field name, then by type, then by woCompare. Performance isn't important
        // here.
        std::sort(lhsChildren.begin(), lhsChildren.end(), FieldNameLessThan());
        std::sort(rhsChildren.begin(), rhsChildren.end(), FieldNameLessThan());

        typedef std::vector<ConstElement>::const_iterator iter;
        iter lhsWhere = lhsChildren.begin();
        iter rhsWhere = rhsChildren.begin();
        const iter lhsEnd = lhsChildren.end();

        for (; lhsWhere != lhsEnd; ++lhsWhere, ++rhsWhere) {
            if (lhsWhere->getFieldName() != rhsWhere->getFieldName())
                return false;

            if (!checkDocNoOrderingImpl(*lhsWhere, *rhsWhere))
                return false;
        }

        return true;

    } else if (lhsType == mongo::Array) {
        if (rhsType != mongo::Array)
            return false;

        // For arrays, since they are ordered, we don't need the sorting step.
        const size_t lhsChildren = countChildren(lhs);
        const size_t rhsChildren = countChildren(rhs);

        if (lhsChildren != rhsChildren)
            return false;

        if (lhsChildren == 0)
            return true;

        ConstElement lhsChild = lhs.leftChild();
        ConstElement rhsChild = rhs.leftChild();

        while (lhsChild.ok()) {
            if (!checkDocNoOrderingImpl(lhsChild, rhsChild))
                return false;

            lhsChild = lhsChild.rightSibling();
            rhsChild = rhsChild.rightSibling();
        }

        return true;

    } else {
        // This is some leaf type. We've already checked or ignored field names, so
        // don't recheck it here.
        return lhs.compareWithElement(rhs, nullptr, false) == 0;
    }
}

}  // namespace

// TODO: We should really update this to be an ASSERT_ something, so that we can print out
// the expected and actual documents.
bool checkDoc(const Document& lhs, const BSONObj& rhs) {
    // Get the fundamental result via BSONObj's woCompare path. This is the best starting
    // point, because we think that Document::getObject and the serialization mechanism is
    // pretty well sorted.
    BSONObj fromLhs = lhs.getObject();
    const int primaryResult = fromLhs.woCompare(rhs);

    // Validate primary result via other comparison paths.
    const int secondaryResult = lhs.compareWithBSONObj(rhs, nullptr);

    assertSameSign(primaryResult, secondaryResult);

    // Check that mutables serialized result matches against its origin.
    ASSERT_EQUALS(0, lhs.compareWithBSONObj(fromLhs, nullptr));

    return (primaryResult == 0);
}

bool checkDoc(const Document& lhs, const Document& rhs) {
    const int primaryResult = lhs.compareWith(rhs, nullptr);

    const BSONObj fromLhs = lhs.getObject();
    const BSONObj fromRhs = rhs.getObject();

    const int result_d_o = lhs.compareWithBSONObj(fromRhs, nullptr);
    const int result_o_d = rhs.compareWithBSONObj(fromLhs, nullptr);

    assertSameSign(primaryResult, result_d_o);
    assertOppositeSign(primaryResult, result_o_d);

    ASSERT_EQUALS(0, lhs.compareWithBSONObj(fromLhs, nullptr));
    ASSERT_EQUALS(0, rhs.compareWithBSONObj(fromRhs, nullptr));

    return (primaryResult == 0);
}

std::ostream& operator<<(std::ostream& stream, const ConstElement& elt) {
    stream << elt.toString();
    return stream;
}

std::ostream& operator<<(std::ostream& stream, const Document& doc) {
    stream << doc.toString();
    return stream;
}

std::ostream& operator<<(std::ostream& stream, const Element& elt) {
    stream << elt.toString();
    return stream;
}

bool checkEqualNoOrdering(const Document& lhs, const Document& rhs) {
    return checkDocNoOrderingImpl(lhs.root(), rhs.root());
}

std::ostream& operator<<(std::ostream& stream, const UnorderedWrapper_Doc& uw_d) {
    return stream << uw_d.doc;
}

std::ostream& operator<<(std::ostream& stream, const UnorderedWrapper_Obj& uw_o) {
    const Document d(uw_o.obj);
    return stream << d;
}

}  // namespace mutablebson
}  // namespace mongo