1 
2 /**
3  *    Copyright (C) 2018-present MongoDB, Inc.
4  *
5  *    This program is free software: you can redistribute it and/or modify
6  *    it under the terms of the Server Side Public License, version 1,
7  *    as published by MongoDB, Inc.
8  *
9  *    This program is distributed in the hope that it will be useful,
10  *    but WITHOUT ANY WARRANTY; without even the implied warranty of
11  *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  *    Server Side Public License for more details.
13  *
14  *    You should have received a copy of the Server Side Public License
15  *    along with this program. If not, see
16  *    <http://www.mongodb.com/licensing/server-side-public-license>.
17  *
18  *    As a special exception, the copyright holders give permission to link the
19  *    code of portions of this program with the OpenSSL library under certain
20  *    conditions as described in each individual source file and distribute
21  *    linked combinations including the program with the OpenSSL library. You
22  *    must comply with the Server Side Public License in all respects for
23  *    all of the code used other than as permitted herein. If you modify file(s)
24  *    with this exception, you may extend this exception to your version of the
25  *    file(s), but you are not obligated to do so. If you do not wish to do so,
26  *    delete this exception statement from your version. If you delete this
27  *    exception statement from all source files in the program, then also delete
28  *    it in the license file.
29  */
30 
31 #pragma once
32 
33 #include "mongo/bson/simple_bsonelement_comparator.h"
34 #include "mongo/bson/simple_bsonobj_comparator.h"
35 #include "mongo/unittest/unittest.h"
36 
37 /**
38  * BSON comparison utility macro. Do not use directly.
39  */
40 #define ASSERT_BSON_COMPARISON(NAME, a, b, astr, bstr) \
41     ::mongo::unittest::assertComparison_##NAME(__FILE__, __LINE__, astr, bstr, a, b)
42 
43 /**
44  * Use to compare two instances of type BSONObj under the default comparator in unit tests.
45  */
46 #define ASSERT_BSONOBJ_EQ(a, b) ASSERT_BSON_COMPARISON(BSONObjEQ, a, b, #a, #b)
47 #define ASSERT_BSONOBJ_LT(a, b) ASSERT_BSON_COMPARISON(BSONObjLT, a, b, #a, #b)
48 #define ASSERT_BSONOBJ_LTE(a, b) ASSERT_BSON_COMPARISON(BSONObjLTE, a, b, #a, #b)
49 #define ASSERT_BSONOBJ_GT(a, b) ASSERT_BSON_COMPARISON(BSONObjGT, a, b, #a, #b)
50 #define ASSERT_BSONOBJ_GTE(a, b) ASSERT_BSON_COMPARISON(BSONObjGTE, a, b, #a, #b)
51 #define ASSERT_BSONOBJ_NE(a, b) ASSERT_BSON_COMPARISON(BSONObjNE, a, b, #a, #b)
52 
53 /**
54  * Use to compare two instances of type BSONElement under the default comparator in unit tests.
55  */
56 #define ASSERT_BSONELT_EQ(a, b) ASSERT_BSON_COMPARISON(BSONElementEQ, a, b, #a, #b)
57 #define ASSERT_BSONELT_LT(a, b) ASSERT_BSON_COMPARISON(BSONElementLT, a, b, #a, #b)
58 #define ASSERT_BSONELT_LTE(a, b) ASSERT_BSON_COMPARISON(BSONElementLTE, a, b, #a, #b)
59 #define ASSERT_BSONELT_GT(a, b) ASSERT_BSON_COMPARISON(BSONElementGT, a, b, #a, #b)
60 #define ASSERT_BSONELT_GTE(a, b) ASSERT_BSON_COMPARISON(BSONElementGTE, a, b, #a, #b)
61 #define ASSERT_BSONELT_NE(a, b) ASSERT_BSON_COMPARISON(BSONElementNE, a, b, #a, #b)
62 
63 namespace mongo {
64 namespace unittest {
65 
66 #define DECLARE_BSON_CMP_FUNC(BSONTYPE, NAME)                          \
67     void assertComparison_##BSONTYPE##NAME(const std::string& theFile, \
68                                            unsigned theLine,           \
69                                            StringData aExpression,     \
70                                            StringData bExpression,     \
71                                            const BSONTYPE& aValue,     \
72                                            const BSONTYPE& bValue);
73 
74 DECLARE_BSON_CMP_FUNC(BSONObj, EQ);
75 DECLARE_BSON_CMP_FUNC(BSONObj, LT);
76 DECLARE_BSON_CMP_FUNC(BSONObj, LTE);
77 DECLARE_BSON_CMP_FUNC(BSONObj, GT);
78 DECLARE_BSON_CMP_FUNC(BSONObj, GTE);
79 DECLARE_BSON_CMP_FUNC(BSONObj, NE);
80 
81 DECLARE_BSON_CMP_FUNC(BSONElement, EQ);
82 DECLARE_BSON_CMP_FUNC(BSONElement, LT);
83 DECLARE_BSON_CMP_FUNC(BSONElement, LTE);
84 DECLARE_BSON_CMP_FUNC(BSONElement, GT);
85 DECLARE_BSON_CMP_FUNC(BSONElement, GTE);
86 DECLARE_BSON_CMP_FUNC(BSONElement, NE);
87 #undef DECLARE_BSON_CMP_FUNC
88 
89 }  // namespace unittest
90 }  // namespace mongo
91