Skip to content

Commit

Permalink
Clean up transparent tests in unordered containers. NFC (#5136)
Browse files Browse the repository at this point in the history
  • Loading branch information
Marshall Clow authored and GitHub Enterprise committed Dec 30, 2024
1 parent 1114337 commit 0fd8d3f
Show file tree
Hide file tree
Showing 6 changed files with 474 additions and 322 deletions.
180 changes: 110 additions & 70 deletions groups/bsl/bslstl/bslstl_multimap_test.t.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -895,6 +895,9 @@ struct TransparentComparator

class TransparentlyComparable {
// DATA
#if BSLS_COMPILERFEATURES_CPLUSPLUS < 201103L
mutable
#endif
int d_conversionCount; // number of times `operator int` has been called
int d_value; // the value

Expand All @@ -916,11 +919,21 @@ class TransparentlyComparable {

/// Return the current value of this object.
operator int()
#if BSLS_COMPILERFEATURES_CPLUSPLUS < 201103L
const
#endif
{
++d_conversionCount;
return d_value;
}

/// return a count of the number of times this object has been converted to
/// an int.
void resetConversionCount()
{
d_conversionCount = 0;
}

// ACCESSORS

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

int expectedConversionCount = 0;
const int expectedConversionCount = isTransparent ? 0 : 1;

TransparentlyComparable existingKey(initKeyValue);
TransparentlyComparable nonExistingKey(initKeyValue ? -initKeyValue
: -100);

ASSERT(existingKey.conversionCount() == expectedConversionCount);
{
// Testing `find`.
existingKey.resetConversionCount();
nonExistingKey.resetConversionCount();

// Testing `find`.
const Iterator EXISTING_F = container.find(existingKey);
ASSERT(container.end() != EXISTING_F);
ASSERT(existingKey.value() == EXISTING_F->first);
ASSERTV(isTransparent,
expectedConversionCount, existingKey.conversionCount(),
expectedConversionCount == existingKey.conversionCount());

const Iterator EXISTING_F = container.find(existingKey);
if (!isTransparent) {
++expectedConversionCount;
const Iterator NON_EXISTING_F = container.find(nonExistingKey);
ASSERT(container.end() == NON_EXISTING_F);
ASSERTV(isTransparent,
expectedConversionCount, nonExistingKey.conversionCount(),
expectedConversionCount == nonExistingKey.conversionCount());
}

ASSERT(container.end() != EXISTING_F);
ASSERT(existingKey.value() == EXISTING_F->first);
ASSERT(existingKey.conversionCount() == expectedConversionCount);
{
// Testing `contains`.
existingKey.resetConversionCount();
nonExistingKey.resetConversionCount();

const Iterator NON_EXISTING_F = container.find(nonExistingKey);
ASSERT(container.end() == NON_EXISTING_F);
ASSERT(nonExistingKey.conversionCount() == expectedConversionCount);
const bool EXISTING_CONTAINS = container.contains(existingKey);

// Testing `contains`.
ASSERT(true == EXISTING_CONTAINS);
ASSERTV(isTransparent,
expectedConversionCount, existingKey.conversionCount(),
expectedConversionCount == existingKey.conversionCount());

const bool EXISTING_CONTAINS = container.contains(existingKey);
if (!isTransparent) {
++expectedConversionCount;
const bool NON_EXISTING_CONTAINS = container.contains(nonExistingKey);
ASSERT(false == NON_EXISTING_CONTAINS);
ASSERTV(isTransparent,
expectedConversionCount, nonExistingKey.conversionCount(),
expectedConversionCount == nonExistingKey.conversionCount());
}

ASSERT(true == EXISTING_CONTAINS);
ASSERT(existingKey.conversionCount() == expectedConversionCount);

const bool NON_EXISTING_CONTAINS = container.contains(nonExistingKey);
ASSERT(false == NON_EXISTING_CONTAINS);
ASSERT(nonExistingKey.conversionCount() == expectedConversionCount);

// Testing `count`.
{
// Testing `count`.
existingKey.resetConversionCount();
nonExistingKey.resetConversionCount();

const Count EXPECTED_C = initKeyValue ? initKeyValue : 1;
const Count EXISTING_C = container.count(existingKey);
const Count EXPECTED_C = initKeyValue ? initKeyValue : 1;
const Count EXISTING_C = container.count(existingKey);
ASSERT(EXPECTED_C == EXISTING_C);
ASSERTV(isTransparent,
expectedConversionCount, existingKey.conversionCount(),
expectedConversionCount == existingKey.conversionCount());

if (!isTransparent) {
++expectedConversionCount;
const Count NON_EXISTING_C = container.count(nonExistingKey);
ASSERT(0 == NON_EXISTING_C);
ASSERTV(isTransparent,
expectedConversionCount, nonExistingKey.conversionCount(),
expectedConversionCount == nonExistingKey.conversionCount());
}

ASSERT(EXPECTED_C == EXISTING_C);
ASSERT(expectedConversionCount == existingKey.conversionCount());

const Count NON_EXISTING_C = container.count(nonExistingKey);
ASSERT(0 == NON_EXISTING_C);
ASSERT(expectedConversionCount == nonExistingKey.conversionCount());
{
// Testing `lower_bound`.
existingKey.resetConversionCount();
nonExistingKey.resetConversionCount();

// Testing `lower_bound`.
const Iterator EXISTING_LB = container.lower_bound(existingKey);
ASSERT(existingKey.value() == EXISTING_LB->first);
ASSERTV(isTransparent,
expectedConversionCount, existingKey.conversionCount(),
expectedConversionCount == existingKey.conversionCount());

const Iterator EXISTING_LB = container.lower_bound(existingKey);
if (!isTransparent) {
++expectedConversionCount;
const Iterator NON_EXISTING_LB = container.lower_bound(nonExistingKey);
ASSERT(container.begin() == NON_EXISTING_LB);
ASSERTV(isTransparent,
expectedConversionCount, nonExistingKey.conversionCount(),
expectedConversionCount == nonExistingKey.conversionCount());
}

ASSERT(EXISTING_F == EXISTING_LB);
ASSERT(expectedConversionCount == existingKey.conversionCount());

const Iterator NON_EXISTING_LB = container.lower_bound(nonExistingKey);

ASSERT(container.begin() == NON_EXISTING_LB);
ASSERT(expectedConversionCount == nonExistingKey.conversionCount());
{
// Testing `upper_bound`.
existingKey.resetConversionCount();
nonExistingKey.resetConversionCount();

// Testing `upper_bound`.
TransparentlyComparable upperBoundValue(initKeyValue + 1);
const Iterator EXPECTED_UB = container.find(upperBoundValue);
const Iterator EXISTING_UB = container.upper_bound(existingKey);
if (!isTransparent) {
++expectedConversionCount;
}
TransparentlyComparable upperBoundValue(initKeyValue + 1);
const Iterator EXPECTED_UB = container.find(upperBoundValue);
const Iterator EXISTING_UB = container.upper_bound(existingKey);

ASSERT(EXPECTED_UB == EXISTING_UB);
ASSERT(expectedConversionCount == existingKey.conversionCount());
ASSERT(EXPECTED_UB == EXISTING_UB);
ASSERTV(isTransparent,
expectedConversionCount, existingKey.conversionCount(),
expectedConversionCount == existingKey.conversionCount());

const Iterator NON_EXISTING_UB = container.upper_bound(nonExistingKey);
const Iterator NON_EXISTING_UB = container.upper_bound(nonExistingKey);

ASSERT(container.begin() == NON_EXISTING_UB);
ASSERT(expectedConversionCount == nonExistingKey.conversionCount());
ASSERT(container.begin() == NON_EXISTING_UB);
ASSERTV(isTransparent,
expectedConversionCount, nonExistingKey.conversionCount(),
expectedConversionCount == nonExistingKey.conversionCount());
}

// Testing `equal_range`.
{
// Testing `equal_range`.
existingKey.resetConversionCount();
nonExistingKey.resetConversionCount();

const bsl::pair<Iterator, Iterator> EXISTING_ER =
const bsl::pair<Iterator, Iterator> EXISTING_ER =
container.equal_range(existingKey);
if (!isTransparent) {
++expectedConversionCount;
}
ASSERT(EXISTING_ER.first != EXISTING_ER.second);

ASSERT(EXISTING_LB == EXISTING_ER.first);
ASSERT(EXPECTED_UB == EXISTING_ER.second);
ASSERT(expectedConversionCount == existingKey.conversionCount());
ASSERTV(0 < static_cast<Count>(
bsl::distance(EXISTING_ER.first, EXISTING_ER.second)));
for (Iterator it = EXISTING_ER.first; it != EXISTING_ER.second; ++it) {
ASSERTV(existingKey.value() == it->first);
}

const bsl::pair<Iterator, Iterator> NON_EXISTING_ER =
container.equal_range(nonExistingKey);
ASSERTV(isTransparent,
expectedConversionCount, existingKey.conversionCount(),
expectedConversionCount == existingKey.conversionCount());

ASSERT(NON_EXISTING_LB == NON_EXISTING_ER.first);
ASSERT(NON_EXISTING_UB == NON_EXISTING_ER.second);
ASSERT(expectedConversionCount == nonExistingKey.conversionCount());
const bsl::pair<Iterator, Iterator> NON_EXISTING_ER =
container.equal_range(nonExistingKey);
ASSERT(NON_EXISTING_ER.first == NON_EXISTING_ER.second);
ASSERTV(isTransparent,
expectedConversionCount, nonExistingKey.conversionCount(),
expectedConversionCount == nonExistingKey.conversionCount());
}
}

// =============================
Expand Down
Loading

0 comments on commit 0fd8d3f

Please sign in to comment.