|
| 1 | +/** |
| 2 | + * Copyright (C) 2019-present MongoDB, Inc. |
| 3 | + * |
| 4 | + * This program is free software: you can redistribute it and/or modify |
| 5 | + * it under the terms of the Server Side Public License, version 1, |
| 6 | + * as published by MongoDB, Inc. |
| 7 | + * |
| 8 | + * This program is distributed in the hope that it will be useful, |
| 9 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 11 | + * Server Side Public License for more details. |
| 12 | + * |
| 13 | + * You should have received a copy of the Server Side Public License |
| 14 | + * along with this program. If not, see |
| 15 | + * <http://www.mongodb.com/licensing/server-side-public-license>. |
| 16 | + * |
| 17 | + * As a special exception, the copyright holders give permission to link the |
| 18 | + * code of portions of this program with the OpenSSL library under certain |
| 19 | + * conditions as described in each individual source file and distribute |
| 20 | + * linked combinations including the program with the OpenSSL library. You |
| 21 | + * must comply with the Server Side Public License in all respects for |
| 22 | + * all of the code used other than as permitted herein. If you modify file(s) |
| 23 | + * with this exception, you may extend this exception to your version of the |
| 24 | + * file(s), but you are not obligated to do so. If you do not wish to do so, |
| 25 | + * delete this exception statement from your version. If you delete this |
| 26 | + * exception statement from all source files in the program, then also delete |
| 27 | + * it in the license file. |
| 28 | + */ |
| 29 | + |
| 30 | +#include "mongo/platform/basic.h" |
| 31 | + |
| 32 | +#include "mongo/platform/source_location_test.h" |
| 33 | + |
| 34 | +namespace mongo { |
| 35 | +namespace { |
| 36 | + |
| 37 | +constexpr SourceLocation makeLocalFunctionSourceLocationForTest() { |
| 38 | + return MONGO_SOURCE_LOCATION(); |
| 39 | +} |
| 40 | + |
| 41 | +const SourceLocationHolder kLocation = MONGO_SOURCE_LOCATION_NO_FUNC(); |
| 42 | + |
| 43 | +struct StructWithDefaultInitContextMember { |
| 44 | + const SourceLocationHolder location = MONGO_SOURCE_LOCATION_NO_FUNC(); |
| 45 | +}; |
| 46 | + |
| 47 | +#define CALL_MONGO_SOURCE_LOCATION() MONGO_SOURCE_LOCATION() |
| 48 | + |
| 49 | +TEST(SourceLocation, CorrectLineNumber) { |
| 50 | + using LineT = std::invoke_result_t<decltype(&SourceLocation::line), SourceLocation>; |
| 51 | + ASSERT_EQ(MONGO_SOURCE_LOCATION().line(), static_cast<LineT>(__LINE__)); |
| 52 | +} |
| 53 | + |
| 54 | +TEST(SourceLocation, InlineVariable) { |
| 55 | + SourceLocationHolder inlineLocation1 = MONGO_SOURCE_LOCATION(); |
| 56 | + SourceLocationHolder inlineLocation2 = MONGO_SOURCE_LOCATION(); |
| 57 | + SourceLocationHolder inlineLocation3 = MONGO_SOURCE_LOCATION(); |
| 58 | + |
| 59 | + // Each location should have the same filename |
| 60 | + ASSERT_EQ(inlineLocation1.file_name(), inlineLocation2.file_name()); |
| 61 | + ASSERT_EQ(inlineLocation1.file_name(), inlineLocation3.file_name()); |
| 62 | + |
| 63 | + // The line numbers for each location should increase monotonically when inline |
| 64 | + ASSERT_LT(inlineLocation1.line(), inlineLocation2.line()); |
| 65 | + ASSERT_LT(inlineLocation2.line(), inlineLocation3.line()); |
| 66 | + |
| 67 | + unittest::log() << inlineLocation1; |
| 68 | + unittest::log() << inlineLocation2; |
| 69 | + unittest::log() << inlineLocation3; |
| 70 | +} |
| 71 | + |
| 72 | +TEST(SourceLocation, LocalFunction) { |
| 73 | + SourceLocationHolder inlineLocation1 = MONGO_SOURCE_LOCATION(); |
| 74 | + SourceLocationHolder localFunctionLocation1 = makeLocalFunctionSourceLocationForTest(); |
| 75 | + SourceLocationHolder localFunctionLocation2 = makeLocalFunctionSourceLocationForTest(); |
| 76 | + |
| 77 | + // The inline location should have the same file name but a later line |
| 78 | + ASSERT_EQ(inlineLocation1.file_name(), localFunctionLocation1.file_name()); |
| 79 | + ASSERT_GT(inlineLocation1.line(), localFunctionLocation1.line()); |
| 80 | + |
| 81 | + // The two local function locations should be identical |
| 82 | + ASSERT_EQ(localFunctionLocation1, localFunctionLocation2); |
| 83 | + |
| 84 | + unittest::log() << inlineLocation1; |
| 85 | + unittest::log() << localFunctionLocation1; |
| 86 | + unittest::log() << localFunctionLocation2; |
| 87 | +} |
| 88 | + |
| 89 | +TEST(SourceLocation, HeaderFunction) { |
| 90 | + SourceLocationHolder inlineLocation1 = MONGO_SOURCE_LOCATION(); |
| 91 | + SourceLocationHolder headerLocation1 = makeHeaderSourceLocationForTest(); |
| 92 | + SourceLocationHolder headerLocation2 = makeHeaderSourceLocationForTest(); |
| 93 | + |
| 94 | + // The inline location should have a different file name |
| 95 | + ASSERT_NE(inlineLocation1.file_name(), headerLocation1.file_name()); |
| 96 | + |
| 97 | + // The two header locations should be identical |
| 98 | + ASSERT_EQ(headerLocation1, headerLocation2); |
| 99 | + |
| 100 | + unittest::log() << inlineLocation1; |
| 101 | + unittest::log() << headerLocation1; |
| 102 | + unittest::log() << headerLocation2; |
| 103 | +} |
| 104 | + |
| 105 | +TEST(SourceLocation, GlobalVariable) { |
| 106 | + SourceLocationHolder inlineLocation1 = MONGO_SOURCE_LOCATION(); |
| 107 | + |
| 108 | + // The inline location should have the same file name but a later line |
| 109 | + ASSERT_EQ(inlineLocation1.file_name(), kLocation.file_name()); |
| 110 | + ASSERT_GT(inlineLocation1.line(), kLocation.line()); |
| 111 | + |
| 112 | + unittest::log() << inlineLocation1; |
| 113 | + unittest::log() << kLocation; |
| 114 | +} |
| 115 | + |
| 116 | +TEST(SourceLocation, DefaultStructMember) { |
| 117 | + SourceLocationHolder inlineLocation1 = MONGO_SOURCE_LOCATION(); |
| 118 | + StructWithDefaultInitContextMember obj1; |
| 119 | + StructWithDefaultInitContextMember obj2; |
| 120 | + |
| 121 | + // The inline location should have the same file name but a later line |
| 122 | + ASSERT_EQ(inlineLocation1.file_name(), obj1.location.file_name()); |
| 123 | + ASSERT_GT(inlineLocation1.line(), obj1.location.line()); |
| 124 | + |
| 125 | + // The two default ctor'd struct member locations should be identical |
| 126 | + ASSERT_EQ(obj1.location, obj2.location); |
| 127 | + |
| 128 | + unittest::log() << inlineLocation1; |
| 129 | + unittest::log() << obj1.location; |
| 130 | + unittest::log() << obj2.location; |
| 131 | +} |
| 132 | + |
| 133 | +TEST(SourceLocation, Macro) { |
| 134 | + SourceLocationHolder inlineLocation1 = MONGO_SOURCE_LOCATION(); |
| 135 | + SourceLocationHolder inlineLocation2 = CALL_MONGO_SOURCE_LOCATION(); |
| 136 | + |
| 137 | + // Each location should have the same filename |
| 138 | + ASSERT_EQ(inlineLocation1.file_name(), inlineLocation2.file_name()); |
| 139 | + |
| 140 | + // The line numbers for each location should increase monotonically when inline |
| 141 | + ASSERT_LT(inlineLocation1.line(), inlineLocation2.line()); |
| 142 | + |
| 143 | + unittest::log() << inlineLocation1; |
| 144 | + unittest::log() << inlineLocation2; |
| 145 | +} |
| 146 | + |
| 147 | +TEST(SourceLocation, Constexpr) { |
| 148 | + constexpr SourceLocationHolder inlineLocation1 = MONGO_SOURCE_LOCATION(); |
| 149 | + constexpr SourceLocationHolder inlineLocation2 = MONGO_SOURCE_LOCATION(); |
| 150 | + static_assert((inlineLocation1.line() + 1) == inlineLocation2.line()); |
| 151 | + static_assert(inlineLocation1.column() == inlineLocation2.column()); |
| 152 | + static_assert(areEqual(inlineLocation1.file_name(), inlineLocation2.file_name())); |
| 153 | + static_assert(areEqual(inlineLocation1.function_name(), inlineLocation2.function_name())); |
| 154 | + |
| 155 | + constexpr auto localFunctionLocation = makeLocalFunctionSourceLocationForTest(); |
| 156 | + static_assert(inlineLocation1.line() > localFunctionLocation.line()); |
| 157 | + static_assert(areEqual(inlineLocation1.file_name(), localFunctionLocation.file_name())); |
| 158 | + static_assert( |
| 159 | + !areEqual(inlineLocation1.function_name(), localFunctionLocation.function_name())); |
| 160 | + |
| 161 | + constexpr auto headerLocation = makeHeaderSourceLocationForTest(); |
| 162 | + static_assert(!areEqual(inlineLocation1.file_name(), headerLocation.file_name())); |
| 163 | + static_assert(!areEqual(inlineLocation1.function_name(), headerLocation.function_name())); |
| 164 | +} |
| 165 | + |
| 166 | +} // namespace |
| 167 | +} // namespace mongo |
0 commit comments