Skip to content

Commit 0fd8d3f

Browse files
Marshall ClowGitHub Enterprise
Marshall Clow
authored and
GitHub Enterprise
committed
Clean up transparent tests in unordered containers. NFC (#5136)
1 parent 1114337 commit 0fd8d3f

6 files changed

+474
-322
lines changed

groups/bsl/bslstl/bslstl_multimap_test.t.cpp

Lines changed: 110 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -895,6 +895,9 @@ struct TransparentComparator
895895

896896
class TransparentlyComparable {
897897
// DATA
898+
#if BSLS_COMPILERFEATURES_CPLUSPLUS < 201103L
899+
mutable
900+
#endif
898901
int d_conversionCount; // number of times `operator int` has been called
899902
int d_value; // the value
900903

@@ -916,11 +919,21 @@ class TransparentlyComparable {
916919

917920
/// Return the current value of this object.
918921
operator int()
922+
#if BSLS_COMPILERFEATURES_CPLUSPLUS < 201103L
923+
const
924+
#endif
919925
{
920926
++d_conversionCount;
921927
return d_value;
922928
}
923929

930+
/// return a count of the number of times this object has been converted to
931+
/// an int.
932+
void resetConversionCount()
933+
{
934+
d_conversionCount = 0;
935+
}
936+
924937
// ACCESSORS
925938

926939
/// Return the number of times `operator int` has been called.
@@ -965,108 +978,135 @@ void testTransparentComparator(Container& container,
965978
typedef typename Container::const_iterator Iterator;
966979
typedef typename Container::size_type Count;
967980

968-
int expectedConversionCount = 0;
981+
const int expectedConversionCount = isTransparent ? 0 : 1;
969982

970983
TransparentlyComparable existingKey(initKeyValue);
971984
TransparentlyComparable nonExistingKey(initKeyValue ? -initKeyValue
972985
: -100);
973986

974-
ASSERT(existingKey.conversionCount() == expectedConversionCount);
987+
{
988+
// Testing `find`.
989+
existingKey.resetConversionCount();
990+
nonExistingKey.resetConversionCount();
975991

976-
// Testing `find`.
992+
const Iterator EXISTING_F = container.find(existingKey);
993+
ASSERT(container.end() != EXISTING_F);
994+
ASSERT(existingKey.value() == EXISTING_F->first);
995+
ASSERTV(isTransparent,
996+
expectedConversionCount, existingKey.conversionCount(),
997+
expectedConversionCount == existingKey.conversionCount());
977998

978-
const Iterator EXISTING_F = container.find(existingKey);
979-
if (!isTransparent) {
980-
++expectedConversionCount;
999+
const Iterator NON_EXISTING_F = container.find(nonExistingKey);
1000+
ASSERT(container.end() == NON_EXISTING_F);
1001+
ASSERTV(isTransparent,
1002+
expectedConversionCount, nonExistingKey.conversionCount(),
1003+
expectedConversionCount == nonExistingKey.conversionCount());
9811004
}
9821005

983-
ASSERT(container.end() != EXISTING_F);
984-
ASSERT(existingKey.value() == EXISTING_F->first);
985-
ASSERT(existingKey.conversionCount() == expectedConversionCount);
1006+
{
1007+
// Testing `contains`.
1008+
existingKey.resetConversionCount();
1009+
nonExistingKey.resetConversionCount();
9861010

987-
const Iterator NON_EXISTING_F = container.find(nonExistingKey);
988-
ASSERT(container.end() == NON_EXISTING_F);
989-
ASSERT(nonExistingKey.conversionCount() == expectedConversionCount);
1011+
const bool EXISTING_CONTAINS = container.contains(existingKey);
9901012

991-
// Testing `contains`.
1013+
ASSERT(true == EXISTING_CONTAINS);
1014+
ASSERTV(isTransparent,
1015+
expectedConversionCount, existingKey.conversionCount(),
1016+
expectedConversionCount == existingKey.conversionCount());
9921017

993-
const bool EXISTING_CONTAINS = container.contains(existingKey);
994-
if (!isTransparent) {
995-
++expectedConversionCount;
1018+
const bool NON_EXISTING_CONTAINS = container.contains(nonExistingKey);
1019+
ASSERT(false == NON_EXISTING_CONTAINS);
1020+
ASSERTV(isTransparent,
1021+
expectedConversionCount, nonExistingKey.conversionCount(),
1022+
expectedConversionCount == nonExistingKey.conversionCount());
9961023
}
9971024

998-
ASSERT(true == EXISTING_CONTAINS);
999-
ASSERT(existingKey.conversionCount() == expectedConversionCount);
1000-
1001-
const bool NON_EXISTING_CONTAINS = container.contains(nonExistingKey);
1002-
ASSERT(false == NON_EXISTING_CONTAINS);
1003-
ASSERT(nonExistingKey.conversionCount() == expectedConversionCount);
1004-
1005-
// Testing `count`.
1025+
{
1026+
// Testing `count`.
1027+
existingKey.resetConversionCount();
1028+
nonExistingKey.resetConversionCount();
10061029

1007-
const Count EXPECTED_C = initKeyValue ? initKeyValue : 1;
1008-
const Count EXISTING_C = container.count(existingKey);
1030+
const Count EXPECTED_C = initKeyValue ? initKeyValue : 1;
1031+
const Count EXISTING_C = container.count(existingKey);
1032+
ASSERT(EXPECTED_C == EXISTING_C);
1033+
ASSERTV(isTransparent,
1034+
expectedConversionCount, existingKey.conversionCount(),
1035+
expectedConversionCount == existingKey.conversionCount());
10091036

1010-
if (!isTransparent) {
1011-
++expectedConversionCount;
1037+
const Count NON_EXISTING_C = container.count(nonExistingKey);
1038+
ASSERT(0 == NON_EXISTING_C);
1039+
ASSERTV(isTransparent,
1040+
expectedConversionCount, nonExistingKey.conversionCount(),
1041+
expectedConversionCount == nonExistingKey.conversionCount());
10121042
}
10131043

1014-
ASSERT(EXPECTED_C == EXISTING_C);
1015-
ASSERT(expectedConversionCount == existingKey.conversionCount());
1016-
1017-
const Count NON_EXISTING_C = container.count(nonExistingKey);
1018-
ASSERT(0 == NON_EXISTING_C);
1019-
ASSERT(expectedConversionCount == nonExistingKey.conversionCount());
1044+
{
1045+
// Testing `lower_bound`.
1046+
existingKey.resetConversionCount();
1047+
nonExistingKey.resetConversionCount();
10201048

1021-
// Testing `lower_bound`.
1049+
const Iterator EXISTING_LB = container.lower_bound(existingKey);
1050+
ASSERT(existingKey.value() == EXISTING_LB->first);
1051+
ASSERTV(isTransparent,
1052+
expectedConversionCount, existingKey.conversionCount(),
1053+
expectedConversionCount == existingKey.conversionCount());
10221054

1023-
const Iterator EXISTING_LB = container.lower_bound(existingKey);
1024-
if (!isTransparent) {
1025-
++expectedConversionCount;
1055+
const Iterator NON_EXISTING_LB = container.lower_bound(nonExistingKey);
1056+
ASSERT(container.begin() == NON_EXISTING_LB);
1057+
ASSERTV(isTransparent,
1058+
expectedConversionCount, nonExistingKey.conversionCount(),
1059+
expectedConversionCount == nonExistingKey.conversionCount());
10261060
}
10271061

1028-
ASSERT(EXISTING_F == EXISTING_LB);
1029-
ASSERT(expectedConversionCount == existingKey.conversionCount());
1030-
1031-
const Iterator NON_EXISTING_LB = container.lower_bound(nonExistingKey);
1032-
1033-
ASSERT(container.begin() == NON_EXISTING_LB);
1034-
ASSERT(expectedConversionCount == nonExistingKey.conversionCount());
1062+
{
1063+
// Testing `upper_bound`.
1064+
existingKey.resetConversionCount();
1065+
nonExistingKey.resetConversionCount();
10351066

1036-
// Testing `upper_bound`.
1037-
TransparentlyComparable upperBoundValue(initKeyValue + 1);
1038-
const Iterator EXPECTED_UB = container.find(upperBoundValue);
1039-
const Iterator EXISTING_UB = container.upper_bound(existingKey);
1040-
if (!isTransparent) {
1041-
++expectedConversionCount;
1042-
}
1067+
TransparentlyComparable upperBoundValue(initKeyValue + 1);
1068+
const Iterator EXPECTED_UB = container.find(upperBoundValue);
1069+
const Iterator EXISTING_UB = container.upper_bound(existingKey);
10431070

1044-
ASSERT(EXPECTED_UB == EXISTING_UB);
1045-
ASSERT(expectedConversionCount == existingKey.conversionCount());
1071+
ASSERT(EXPECTED_UB == EXISTING_UB);
1072+
ASSERTV(isTransparent,
1073+
expectedConversionCount, existingKey.conversionCount(),
1074+
expectedConversionCount == existingKey.conversionCount());
10461075

1047-
const Iterator NON_EXISTING_UB = container.upper_bound(nonExistingKey);
1076+
const Iterator NON_EXISTING_UB = container.upper_bound(nonExistingKey);
10481077

1049-
ASSERT(container.begin() == NON_EXISTING_UB);
1050-
ASSERT(expectedConversionCount == nonExistingKey.conversionCount());
1078+
ASSERT(container.begin() == NON_EXISTING_UB);
1079+
ASSERTV(isTransparent,
1080+
expectedConversionCount, nonExistingKey.conversionCount(),
1081+
expectedConversionCount == nonExistingKey.conversionCount());
1082+
}
10511083

1052-
// Testing `equal_range`.
1084+
{
1085+
// Testing `equal_range`.
1086+
existingKey.resetConversionCount();
1087+
nonExistingKey.resetConversionCount();
10531088

1054-
const bsl::pair<Iterator, Iterator> EXISTING_ER =
1089+
const bsl::pair<Iterator, Iterator> EXISTING_ER =
10551090
container.equal_range(existingKey);
1056-
if (!isTransparent) {
1057-
++expectedConversionCount;
1058-
}
1091+
ASSERT(EXISTING_ER.first != EXISTING_ER.second);
10591092

1060-
ASSERT(EXISTING_LB == EXISTING_ER.first);
1061-
ASSERT(EXPECTED_UB == EXISTING_ER.second);
1062-
ASSERT(expectedConversionCount == existingKey.conversionCount());
1093+
ASSERTV(0 < static_cast<Count>(
1094+
bsl::distance(EXISTING_ER.first, EXISTING_ER.second)));
1095+
for (Iterator it = EXISTING_ER.first; it != EXISTING_ER.second; ++it) {
1096+
ASSERTV(existingKey.value() == it->first);
1097+
}
10631098

1064-
const bsl::pair<Iterator, Iterator> NON_EXISTING_ER =
1065-
container.equal_range(nonExistingKey);
1099+
ASSERTV(isTransparent,
1100+
expectedConversionCount, existingKey.conversionCount(),
1101+
expectedConversionCount == existingKey.conversionCount());
10661102

1067-
ASSERT(NON_EXISTING_LB == NON_EXISTING_ER.first);
1068-
ASSERT(NON_EXISTING_UB == NON_EXISTING_ER.second);
1069-
ASSERT(expectedConversionCount == nonExistingKey.conversionCount());
1103+
const bsl::pair<Iterator, Iterator> NON_EXISTING_ER =
1104+
container.equal_range(nonExistingKey);
1105+
ASSERT(NON_EXISTING_ER.first == NON_EXISTING_ER.second);
1106+
ASSERTV(isTransparent,
1107+
expectedConversionCount, nonExistingKey.conversionCount(),
1108+
expectedConversionCount == nonExistingKey.conversionCount());
1109+
}
10701110
}
10711111

10721112
// =============================

0 commit comments

Comments
 (0)