Skip to content

Commit 9a4182e

Browse files
committed
Fix: Avoid signed-unsigned warnings in tests
This patch fixes the last of the signed-unsigned comparison warnings in our unit tests. We mostly do this by changing the types that are being compared, rather than introducing new casts. However, when we need to introduce a new cast, we choose to cast the reference value we are comparing to, rather than the return value from the component-under-test. Signed-off-by: Patrick M. Niedzielski <[email protected]>
1 parent 384ab89 commit 9a4182e

File tree

7 files changed

+104
-100
lines changed

7 files changed

+104
-100
lines changed

src/groups/bmq/bmqa/bmqa_message.t.cpp

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -64,20 +64,19 @@ int generateRandomInteger(int min, int max)
6464
/// `numSubQueueInfos` number of randomly generated SubQueueInfos. Note that
6565
/// `subQueueInfos` will be cleared.
6666
void generateSubQueueInfos(bmqp::Protocol::SubQueueInfosArray* subQueueInfos,
67-
int numSubQueueInfos)
67+
size_t numSubQueueInfos)
6868
{
6969
BSLS_ASSERT_SAFE(subQueueInfos);
7070
BSLS_ASSERT_SAFE(numSubQueueInfos >= 0);
7171

7272
subQueueInfos->clear();
7373

74-
for (int i = 0; i < numSubQueueInfos; ++i) {
74+
for (size_t i = 0; i < numSubQueueInfos; ++i) {
7575
const unsigned int subQueueId = generateRandomInteger(0, 120);
7676
subQueueInfos->push_back(bmqp::SubQueueInfo(subQueueId));
7777
}
7878

79-
BSLS_ASSERT_SAFE(subQueueInfos->size() ==
80-
static_cast<unsigned int>(numSubQueueInfos));
79+
BSLS_ASSERT_SAFE(subQueueInfos->size() == numSubQueueInfos);
8180
}
8281
}
8382

@@ -170,7 +169,7 @@ static void test2_validPushMessagePrint()
170169
bmqp::BlobPoolUtil::createBlobPool(
171170
&bufferFactory,
172171
bmqtst::TestHelperUtil::allocator()));
173-
bmqa::Event event;
172+
bmqa::Event event;
174173

175174
EventImplSp& implPtr = reinterpret_cast<EventImplSp&>(event);
176175
implPtr = bsl::make_shared<bmqimp::Event>(
@@ -181,35 +180,34 @@ static void test2_validPushMessagePrint()
181180
const bmqt::MessageGUID guid;
182181
const char* buffer = "abcdefghijklmnopqrstuvwxyz";
183182
const int flags = 0;
184-
const int numSubQueueInfos =
183+
const size_t numSubQueueInfos =
185184
bmqp::Protocol::SubQueueInfosArray::static_size + 4;
186185

187186
bmqp::Protocol::SubQueueInfosArray subQueueInfos(
188187
bmqtst::TestHelperUtil::allocator());
189188
bdlbb::Blob payload(&bufferFactory, bmqtst::TestHelperUtil::allocator());
190189
bdlbb::BlobUtil::append(&payload, buffer, bsl::strlen(buffer));
191-
BMQTST_ASSERT_EQ(static_cast<unsigned int>(payload.length()),
192-
bsl::strlen(buffer));
190+
BMQTST_ASSERT_EQ(payload.length(), static_cast<int>(bsl::strlen(buffer)));
193191

194192
// Create PushEventBuilder
195193
bmqp::PushEventBuilder peb(blobSpPool.get(),
196194
bmqtst::TestHelperUtil::allocator());
197-
BMQTST_ASSERT_EQ(sizeof(bmqp::EventHeader),
198-
static_cast<size_t>(peb.eventSize()));
199-
BMQTST_ASSERT_EQ(sizeof(bmqp::EventHeader),
200-
static_cast<size_t>(peb.blob()->length()));
195+
BMQTST_ASSERT_EQ(static_cast<int>(sizeof(bmqp::EventHeader)),
196+
peb.eventSize());
197+
BMQTST_ASSERT_EQ(static_cast<int>(sizeof(bmqp::EventHeader)),
198+
peb.blob()->length());
201199
BMQTST_ASSERT_EQ(0, peb.messageCount());
202200

203201
// Add SubQueueInfo option
204202
generateSubQueueInfos(&subQueueInfos, numSubQueueInfos);
205203
bmqt::EventBuilderResult::Enum rc = peb.addSubQueueInfosOption(
206204
subQueueInfos);
207205
BMQTST_ASSERT_EQ(bmqt::EventBuilderResult::e_SUCCESS, rc);
208-
BMQTST_ASSERT_EQ(sizeof(bmqp::EventHeader),
209-
static_cast<size_t>(peb.eventSize()));
206+
BMQTST_ASSERT_EQ(static_cast<int>(sizeof(bmqp::EventHeader)),
207+
peb.eventSize());
210208
// 'eventSize()' excludes unpacked messages
211-
BMQTST_ASSERT_LT(sizeof(bmqp::EventHeader),
212-
static_cast<size_t>(peb.blob()->length()));
209+
BMQTST_ASSERT_LT(static_cast<int>(sizeof(bmqp::EventHeader)),
210+
peb.blob()->length());
213211
// But the option is written to the underlying blob
214212
rc = peb.packMessage(payload,
215213
queueId,
@@ -347,7 +345,8 @@ static void test3_messageProperties()
347345
BMQTST_ASSERT_EQ(0, message.loadProperties(&out2));
348346

349347
BMQTST_ASSERT_EQ(0, out2.setPropertyAsString("y", mod));
350-
BMQTST_ASSERT_EQ(out1.totalSize() + sizeof(mod) - sizeof(y),
348+
BMQTST_ASSERT_EQ(out1.totalSize() + static_cast<int>(sizeof(mod)) -
349+
static_cast<int>(sizeof(y)),
351350
out2.totalSize());
352351

353352
BMQTST_ASSERT_EQ(out2.getPropertyAsString("z"), z);
@@ -387,7 +386,8 @@ static void test3_messageProperties()
387386

388387
BMQTST_ASSERT_EQ(y, out4.getPropertyAsString("y"));
389388
BMQTST_ASSERT_EQ(0, out4.setPropertyAsString("y", mod));
390-
BMQTST_ASSERT_EQ(out1.totalSize() + sizeof(mod) - sizeof(y),
389+
BMQTST_ASSERT_EQ(out1.totalSize() + static_cast<int>(sizeof(mod)) -
390+
static_cast<int>(sizeof(y)),
391391
out4.totalSize());
392392

393393
BMQTST_ASSERT_EQ(out4.getPropertyAsString("z"), z);
@@ -469,11 +469,11 @@ static void test4_subscriptionHandle()
469469
bmqt::EventBuilderResult::Enum rc = peb.addSubQueueInfosOption(
470470
subQueueInfos);
471471
BMQTST_ASSERT_EQ(bmqt::EventBuilderResult::e_SUCCESS, rc);
472-
BMQTST_ASSERT_EQ(sizeof(bmqp::EventHeader),
473-
static_cast<size_t>(peb.eventSize()));
472+
BMQTST_ASSERT_EQ(static_cast<int>(sizeof(bmqp::EventHeader)),
473+
peb.eventSize());
474474
// 'eventSize()' excludes unpacked messages
475-
BMQTST_ASSERT_LT(sizeof(bmqp::EventHeader),
476-
static_cast<size_t>(peb.blob()->length()));
475+
BMQTST_ASSERT_LT(static_cast<int>(sizeof(bmqp::EventHeader)),
476+
peb.blob()->length());
477477
// But the option is written to the underlying blob
478478

479479
// Add message

src/groups/bmq/bmqc/bmqc_orderedhashmap.t.cpp

Lines changed: 39 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -213,17 +213,17 @@ static void test3_insert()
213213

214214
#if defined(BSLS_PLATFORM_OS_SOLARIS)
215215
// Avoid timeout on Solaris
216-
const int k_NUM_ELEMENTS = 100 * 1000; // 100K
216+
const size_t k_NUM_ELEMENTS = 100 * 1000; // 100K
217217
#elif defined(__has_feature)
218218
// Avoid timeout under MemorySanitizer
219-
const int k_NUM_ELEMENTS = __has_feature(memory_sanitizer)
220-
? 100 * 1000 // 100K
221-
: 1000 * 1000; // 1M
219+
const size_t k_NUM_ELEMENTS = __has_feature(memory_sanitizer)
220+
? 100 * 1000 // 100K
221+
: 1000 * 1000; // 1M
222222
#elif defined(__SANITIZE_MEMORY__)
223223
// GCC-supported macros for checking MSAN
224-
const int k_NUM_ELEMENTS = 100 * 1000; // 100K
224+
const size_t k_NUM_ELEMENTS = 100 * 1000; // 100K
225225
#else
226-
const int k_NUM_ELEMENTS = 1000 * 1000; // 1M
226+
const size_t k_NUM_ELEMENTS = 1000 * 1000; // 1M
227227
#endif
228228

229229
// Insert 1M elements
@@ -275,30 +275,30 @@ static void test4_rinsert()
275275
bmqtst::TestHelper::printTestName("RINSERT");
276276

277277
// rinsert() test
278-
typedef bmqc::OrderedHashMap<int, int> MyMapType;
279-
typedef MyMapType::iterator IterType;
280-
typedef MyMapType::const_iterator ConstIterType;
281-
typedef bsl::pair<IterType, bool> RcType;
278+
typedef bmqc::OrderedHashMap<size_t, size_t> MyMapType;
279+
typedef MyMapType::iterator IterType;
280+
typedef MyMapType::const_iterator ConstIterType;
281+
typedef bsl::pair<IterType, bool> RcType;
282282

283283
MyMapType map(bmqtst::TestHelperUtil::allocator());
284284

285285
#if defined(BSLS_PLATFORM_OS_SOLARIS)
286286
// Avoid timeout on Solaris
287-
const int k_NUM_ELEMENTS = 100 * 1000; // 100K
287+
const size_t k_NUM_ELEMENTS = 100 * 1000; // 100K
288288
#elif defined(__has_feature)
289289
// Avoid timeout under MemorySanitizer
290-
const int k_NUM_ELEMENTS = __has_feature(memory_sanitizer)
291-
? 100 * 1000 // 100K
292-
: 1000 * 1000; // 1M
290+
const size_t k_NUM_ELEMENTS = __has_feature(memory_sanitizer)
291+
? 100 * 1000 // 100K
292+
: 1000 * 1000; // 1M
293293
#elif defined(__SANITIZE_MEMORY__)
294294
// GCC-supported macros for checking MSAN
295-
const int k_NUM_ELEMENTS = 100 * 1000; // 100K
295+
const size_t k_NUM_ELEMENTS = 100 * 1000; // 100K
296296
#else
297-
const int k_NUM_ELEMENTS = 1000 * 1000; // 1M
297+
const size_t k_NUM_ELEMENTS = 1000 * 1000; // 1M
298298
#endif
299299

300300
// Insert 1M elements
301-
for (int i = 0; i < k_NUM_ELEMENTS; ++i) {
301+
for (size_t i = 0; i < k_NUM_ELEMENTS; ++i) {
302302
RcType rc = map.rinsert(bsl::make_pair(i, i + 1));
303303
BMQTST_ASSERT_EQ_D(i, true, rc.second);
304304
BMQTST_ASSERT_EQ_D(i, true, rc.first != map.end());
@@ -307,12 +307,12 @@ static void test4_rinsert()
307307
BMQTST_ASSERT_EQ_D(i, true, 1.5 >= map.load_factor());
308308
}
309309

310-
BMQTST_ASSERT_EQ(map.size(), static_cast<size_t>(k_NUM_ELEMENTS));
310+
BMQTST_ASSERT_EQ(map.size(), k_NUM_ELEMENTS);
311311

312312
// Iterate and confirm
313313
{
314314
const MyMapType& cmap = map;
315-
int i = k_NUM_ELEMENTS - 1;
315+
size_t i = k_NUM_ELEMENTS - 1;
316316
for (ConstIterType cit = cmap.begin(); cit != cmap.end(); ++cit) {
317317
BMQTST_ASSERT_EQ_D(i, i, cit->first);
318318
BMQTST_ASSERT_EQ_D(i, (i + 1), cit->second);
@@ -323,7 +323,7 @@ static void test4_rinsert()
323323
// Reverse iterate using --(end()) and confirm
324324
{
325325
const MyMapType& cmap = map;
326-
int i = 0;
326+
size_t i = 0;
327327
ConstIterType cit = --(cmap.end()); // last element
328328
for (; cit != cmap.begin(); --cit) {
329329
BMQTST_ASSERT_EQ_D(i, true, i < k_NUM_ELEMENTS);
@@ -450,9 +450,9 @@ static void test6_clear()
450450
bmqtst::TestHelper::printTestName("CLEAR");
451451

452452
// clear
453-
typedef bmqc::OrderedHashMap<int, int> MyMapType;
454-
typedef MyMapType::iterator IterType;
455-
typedef bsl::pair<IterType, bool> RcType;
453+
typedef bmqc::OrderedHashMap<size_t, size_t> MyMapType;
454+
typedef MyMapType::iterator IterType;
455+
typedef bsl::pair<IterType, bool> RcType;
456456

457457
MyMapType map(bmqtst::TestHelperUtil::allocator());
458458
BMQTST_ASSERT_EQ(true, map.empty());
@@ -466,10 +466,10 @@ static void test6_clear()
466466
BMQTST_ASSERT_EQ(0U, map.size());
467467
BMQTST_ASSERT_EQ(true, map.load_factor() == 0.0);
468468

469-
const int k_NUM_ELEMENTS = 100;
469+
const size_t k_NUM_ELEMENTS = 100;
470470

471471
// Insert elements
472-
for (int i = 0; i < k_NUM_ELEMENTS; ++i) {
472+
for (size_t i = 0; i < k_NUM_ELEMENTS; ++i) {
473473
RcType rc = map.insert(bsl::make_pair(i, i + 1));
474474
BMQTST_ASSERT_EQ_D(i, rc.second, true);
475475
BMQTST_ASSERT_EQ_D(i, true, rc.first != map.end());
@@ -479,7 +479,7 @@ static void test6_clear()
479479

480480
BMQTST_ASSERT_EQ(false, map.empty());
481481
BMQTST_ASSERT_EQ(true, map.begin() != map.end());
482-
BMQTST_ASSERT_EQ(static_cast<int>(k_NUM_ELEMENTS), map.size());
482+
BMQTST_ASSERT_EQ(k_NUM_ELEMENTS, map.size());
483483

484484
map.clear();
485485
BMQTST_ASSERT_EQ(true, map.empty());
@@ -496,16 +496,16 @@ static void test7_erase()
496496
bmqtst::TestHelper::printTestName("ERASE");
497497

498498
// erase
499-
typedef bmqc::OrderedHashMap<int, int> MyMapType;
500-
typedef MyMapType::iterator IterType;
501-
typedef MyMapType::const_iterator ConstIterType;
502-
typedef bsl::pair<IterType, bool> RcType;
499+
typedef bmqc::OrderedHashMap<size_t, size_t> MyMapType;
500+
typedef MyMapType::iterator IterType;
501+
typedef MyMapType::const_iterator ConstIterType;
502+
typedef bsl::pair<IterType, bool> RcType;
503503

504-
const int k_NUM_ELEMENTS = 100;
505-
MyMapType map(bmqtst::TestHelperUtil::allocator());
504+
const size_t k_NUM_ELEMENTS = 100;
505+
MyMapType map(bmqtst::TestHelperUtil::allocator());
506506

507507
// Insert elements
508-
for (int i = 0; i < k_NUM_ELEMENTS; ++i) {
508+
for (size_t i = 0; i < k_NUM_ELEMENTS; ++i) {
509509
RcType rc = map.insert(bsl::make_pair(i, i));
510510
BMQTST_ASSERT_EQ_D(i, rc.second, true);
511511
BMQTST_ASSERT_EQ_D(i, true, rc.first != map.end());
@@ -767,9 +767,9 @@ static void test13_previousEndIterator()
767767
bmqtst::TestHelper::printTestName("PREVIOUS END ITERATOR");
768768
// is pointing to the newly inserted element.
769769

770-
typedef bmqc::OrderedHashMap<int, int> MyMapType;
771-
typedef MyMapType::iterator IterType;
772-
typedef MyMapType::const_iterator ConstIterType;
770+
typedef bmqc::OrderedHashMap<size_t, size_t> MyMapType;
771+
typedef MyMapType::iterator IterType;
772+
typedef MyMapType::const_iterator ConstIterType;
773773

774774
MyMapType map(bmqtst::TestHelperUtil::allocator());
775775
const MyMapType& cmap = map;
@@ -780,7 +780,7 @@ static void test13_previousEndIterator()
780780
IterType endIt = map.end();
781781
ConstIterType endCit = cmap.end();
782782

783-
int i = 0;
783+
size_t i = 0;
784784
bsl::pair<IterType, bool> rc = map.insert(bsl::make_pair(i, i * i));
785785

786786
BMQTST_ASSERT_EQ(true, rc.first == endIt);
@@ -954,8 +954,7 @@ static void test15_eraseRange()
954954
BMQTST_ASSERT_EQ(true, map.empty());
955955
}
956956

957-
BSLA_MAYBE_UNUSED
958-
static void testN1_insertPerformanceOrdered()
957+
BSLA_MAYBE_UNUSED static void testN1_insertPerformanceOrdered()
959958
// ------------------------------------------------------------------------
960959
// INSERT PERFORMANCE
961960
// ------------------------------------------------------------------------
@@ -982,8 +981,7 @@ static void testN1_insertPerformanceOrdered()
982981
}
983982
}
984983

985-
BSLA_MAYBE_UNUSED
986-
static void testN1_insertPerformanceUnordered()
984+
BSLA_MAYBE_UNUSED static void testN1_insertPerformanceUnordered()
987985
// ------------------------------------------------------------------------
988986
// INSERT PERFORMANCE
989987
// ------------------------------------------------------------------------

0 commit comments

Comments
 (0)