Fix "unsafe narrowing" warnings in absl, 10/n.

Addresses failures with the following, in some files:
-Wshorten-64-to-32
-Wimplicit-int-conversion
-Wsign-compare
-Wsign-conversion
-Wtautological-unsigned-zero-compare

(This specific CL focuses on tests.)

Bug: chromium:1292951
PiperOrigin-RevId: 473055916
Change-Id: I40cdd6c87ba9d0a5fb2db5746cff04f14ee829c2
pull/1277/head
Abseil Team 2 years ago committed by Copybara-Service
parent 69199fcebe
commit 2594f85514
  1. 13
      absl/algorithm/container_test.cc
  2. 16
      absl/base/spinlock_test_common.cc
  3. 386
      absl/container/inlined_vector_test.cc
  4. 16
      absl/hash/hash_test.cc
  5. 6
      absl/status/statusor_test.cc
  6. 156
      absl/strings/ascii_test.cc
  7. 244
      absl/strings/string_view_test.cc
  8. 6
      absl/types/optional_test.cc
  9. 80
      absl/types/variant_test.cc

@ -67,13 +67,16 @@ bool Equals(int v1, int v2) { return v1 == v2; }
bool IsOdd(int x) { return x % 2 != 0; }
TEST_F(NonMutatingTest, Distance) {
EXPECT_EQ(container_.size(), absl::c_distance(container_));
EXPECT_EQ(sequence_.size(), absl::c_distance(sequence_));
EXPECT_EQ(vector_.size(), absl::c_distance(vector_));
EXPECT_EQ(ABSL_ARRAYSIZE(array_), absl::c_distance(array_));
EXPECT_EQ(container_.size(),
static_cast<size_t>(absl::c_distance(container_)));
EXPECT_EQ(sequence_.size(), static_cast<size_t>(absl::c_distance(sequence_)));
EXPECT_EQ(vector_.size(), static_cast<size_t>(absl::c_distance(vector_)));
EXPECT_EQ(ABSL_ARRAYSIZE(array_),
static_cast<size_t>(absl::c_distance(array_)));
// Works with a temporary argument.
EXPECT_EQ(vector_.size(), absl::c_distance(std::vector<int>(vector_)));
EXPECT_EQ(vector_.size(),
static_cast<size_t>(absl::c_distance(std::vector<int>(vector_))));
}
TEST_F(NonMutatingTest, Distance_OverloadedBeginEnd) {

@ -34,7 +34,7 @@
#include "absl/synchronization/blocking_counter.h"
#include "absl/synchronization/notification.h"
constexpr int32_t kNumThreads = 10;
constexpr uint32_t kNumThreads = 10;
constexpr int32_t kIters = 1000;
namespace absl {
@ -55,7 +55,7 @@ struct SpinLockTest {
namespace {
static constexpr int kArrayLength = 10;
static constexpr size_t kArrayLength = 10;
static uint32_t values[kArrayLength];
ABSL_CONST_INIT static SpinLock static_cooperative_spinlock(
@ -79,11 +79,11 @@ static uint32_t Hash32(uint32_t a, uint32_t c) {
return c;
}
static void TestFunction(int thread_salt, SpinLock* spinlock) {
static void TestFunction(uint32_t thread_salt, SpinLock* spinlock) {
for (int i = 0; i < kIters; i++) {
SpinLockHolder h(spinlock);
for (int j = 0; j < kArrayLength; j++) {
const int index = (j + thread_salt) % kArrayLength;
for (size_t j = 0; j < kArrayLength; j++) {
const size_t index = (j + thread_salt) % kArrayLength;
values[index] = Hash32(values[index], thread_salt);
std::this_thread::yield();
}
@ -93,7 +93,7 @@ static void TestFunction(int thread_salt, SpinLock* spinlock) {
static void ThreadedTest(SpinLock* spinlock) {
std::vector<std::thread> threads;
threads.reserve(kNumThreads);
for (int i = 0; i < kNumThreads; ++i) {
for (uint32_t i = 0; i < kNumThreads; ++i) {
threads.push_back(std::thread(TestFunction, i, spinlock));
}
for (auto& thread : threads) {
@ -101,7 +101,7 @@ static void ThreadedTest(SpinLock* spinlock) {
}
SpinLockHolder h(spinlock);
for (int i = 1; i < kArrayLength; i++) {
for (size_t i = 1; i < kArrayLength; i++) {
EXPECT_EQ(values[0], values[i]);
}
}
@ -153,7 +153,7 @@ TEST(SpinLock, WaitCyclesEncoding) {
int64_t cycles = cycle_distribution(generator);
int64_t end_time = start_time + cycles;
uint32_t lock_value = SpinLockTest::EncodeWaitCycles(start_time, end_time);
EXPECT_EQ(0, lock_value & kLockwordReservedMask);
EXPECT_EQ(0u, lock_value & kLockwordReservedMask);
int64_t decoded = SpinLockTest::DecodeWaitCycles(lock_value);
EXPECT_EQ(0, decoded & kProfileTimestampMask);
EXPECT_EQ(cycles & ~kProfileTimestampMask, decoded);

@ -126,20 +126,20 @@ using DynamicVec = absl::InlinedVector<Dynamic, 8>;
// Append 0..len-1 to *v
template <typename Container>
static void Fill(Container* v, int len, int offset = 0) {
for (int i = 0; i < len; i++) {
v->push_back(i + offset);
static void Fill(Container* v, size_t len, int offset = 0) {
for (size_t i = 0; i < len; i++) {
v->push_back(static_cast<int>(i) + offset);
}
}
static IntVec Fill(int len, int offset = 0) {
static IntVec Fill(size_t len, int offset = 0) {
IntVec v;
Fill(&v, len, offset);
return v;
}
TEST(IntVec, SimpleOps) {
for (int len = 0; len < 20; len++) {
for (size_t len = 0; len < 20; len++) {
IntVec v;
const IntVec& cv = v; // const alias
@ -147,42 +147,42 @@ TEST(IntVec, SimpleOps) {
EXPECT_EQ(len, v.size());
EXPECT_LE(len, v.capacity());
for (int i = 0; i < len; i++) {
EXPECT_EQ(i, v[i]);
EXPECT_EQ(i, v.at(i));
for (size_t i = 0; i < len; i++) {
EXPECT_EQ(static_cast<int>(i), v[i]);
EXPECT_EQ(static_cast<int>(i), v.at(i));
}
EXPECT_EQ(v.begin(), v.data());
EXPECT_EQ(cv.begin(), cv.data());
int counter = 0;
size_t counter = 0;
for (IntVec::iterator iter = v.begin(); iter != v.end(); ++iter) {
EXPECT_EQ(counter, *iter);
EXPECT_EQ(static_cast<int>(counter), *iter);
counter++;
}
EXPECT_EQ(counter, len);
counter = 0;
for (IntVec::const_iterator iter = v.begin(); iter != v.end(); ++iter) {
EXPECT_EQ(counter, *iter);
EXPECT_EQ(static_cast<int>(counter), *iter);
counter++;
}
EXPECT_EQ(counter, len);
counter = 0;
for (IntVec::const_iterator iter = v.cbegin(); iter != v.cend(); ++iter) {
EXPECT_EQ(counter, *iter);
EXPECT_EQ(static_cast<int>(counter), *iter);
counter++;
}
EXPECT_EQ(counter, len);
if (len > 0) {
EXPECT_EQ(0, v.front());
EXPECT_EQ(len - 1, v.back());
EXPECT_EQ(static_cast<int>(len - 1), v.back());
v.pop_back();
EXPECT_EQ(len - 1, v.size());
for (int i = 0; i < v.size(); ++i) {
EXPECT_EQ(i, v[i]);
EXPECT_EQ(i, v.at(i));
for (size_t i = 0; i < v.size(); ++i) {
EXPECT_EQ(static_cast<int>(i), v[i]);
EXPECT_EQ(static_cast<int>(i), v.at(i));
}
}
}
@ -191,7 +191,7 @@ TEST(IntVec, SimpleOps) {
TEST(IntVec, PopBackNoOverflow) {
IntVec v = {1};
v.pop_back();
EXPECT_EQ(v.size(), 0);
EXPECT_EQ(v.size(), 0u);
}
TEST(IntVec, AtThrows) {
@ -202,47 +202,47 @@ TEST(IntVec, AtThrows) {
}
TEST(IntVec, ReverseIterator) {
for (int len = 0; len < 20; len++) {
for (size_t len = 0; len < 20; len++) {
IntVec v;
Fill(&v, len);
int counter = len;
size_t counter = len;
for (IntVec::reverse_iterator iter = v.rbegin(); iter != v.rend(); ++iter) {
counter--;
EXPECT_EQ(counter, *iter);
EXPECT_EQ(static_cast<int>(counter), *iter);
}
EXPECT_EQ(counter, 0);
EXPECT_EQ(counter, 0u);
counter = len;
for (IntVec::const_reverse_iterator iter = v.rbegin(); iter != v.rend();
++iter) {
counter--;
EXPECT_EQ(counter, *iter);
EXPECT_EQ(static_cast<int>(counter), *iter);
}
EXPECT_EQ(counter, 0);
EXPECT_EQ(counter, 0u);
counter = len;
for (IntVec::const_reverse_iterator iter = v.crbegin(); iter != v.crend();
++iter) {
counter--;
EXPECT_EQ(counter, *iter);
EXPECT_EQ(static_cast<int>(counter), *iter);
}
EXPECT_EQ(counter, 0);
EXPECT_EQ(counter, 0u);
}
}
TEST(IntVec, Erase) {
for (int len = 1; len < 20; len++) {
for (int i = 0; i < len; ++i) {
for (size_t len = 1; len < 20; len++) {
for (size_t i = 0; i < len; ++i) {
IntVec v;
Fill(&v, len);
v.erase(v.begin() + i);
EXPECT_EQ(len - 1, v.size());
for (int j = 0; j < i; ++j) {
EXPECT_EQ(j, v[j]);
for (size_t j = 0; j < i; ++j) {
EXPECT_EQ(static_cast<int>(j), v[j]);
}
for (int j = i; j < len - 1; ++j) {
EXPECT_EQ(j + 1, v[j]);
for (size_t j = i; j < len - 1; ++j) {
EXPECT_EQ(static_cast<int>(j + 1), v[j]);
}
}
}
@ -254,7 +254,7 @@ TEST(IntVec, Hardened) {
EXPECT_EQ(v[9], 9);
#if !defined(NDEBUG) || ABSL_OPTION_HARDENED
EXPECT_DEATH_IF_SUPPORTED(v[10], "");
EXPECT_DEATH_IF_SUPPORTED(v[-1], "");
EXPECT_DEATH_IF_SUPPORTED(v[static_cast<size_t>(-1)], "");
EXPECT_DEATH_IF_SUPPORTED(v.resize(v.max_size() + 1), "");
#endif
}
@ -263,43 +263,43 @@ TEST(IntVec, Hardened) {
// should have reference counts == 0, and all others elements should have
// reference counts == 1.
TEST(RefCountedVec, EraseBeginEnd) {
for (int len = 1; len < 20; ++len) {
for (int erase_begin = 0; erase_begin < len; ++erase_begin) {
for (int erase_end = erase_begin; erase_end <= len; ++erase_end) {
for (size_t len = 1; len < 20; ++len) {
for (size_t erase_begin = 0; erase_begin < len; ++erase_begin) {
for (size_t erase_end = erase_begin; erase_end <= len; ++erase_end) {
std::vector<int> counts(len, 0);
RefCountedVec v;
for (int i = 0; i < len; ++i) {
v.push_back(RefCounted(i, &counts[i]));
for (size_t i = 0; i < len; ++i) {
v.push_back(RefCounted(static_cast<int>(i), &counts[i]));
}
int erase_len = erase_end - erase_begin;
size_t erase_len = erase_end - erase_begin;
v.erase(v.begin() + erase_begin, v.begin() + erase_end);
EXPECT_EQ(len - erase_len, v.size());
// Check the elements before the first element erased.
for (int i = 0; i < erase_begin; ++i) {
EXPECT_EQ(i, v[i].value_);
for (size_t i = 0; i < erase_begin; ++i) {
EXPECT_EQ(static_cast<int>(i), v[i].value_);
}
// Check the elements after the first element erased.
for (int i = erase_begin; i < v.size(); ++i) {
EXPECT_EQ(i + erase_len, v[i].value_);
for (size_t i = erase_begin; i < v.size(); ++i) {
EXPECT_EQ(static_cast<int>(i + erase_len), v[i].value_);
}
// Check that the elements at the beginning are preserved.
for (int i = 0; i < erase_begin; ++i) {
for (size_t i = 0; i < erase_begin; ++i) {
EXPECT_EQ(1, counts[i]);
}
// Check that the erased elements are destroyed
for (int i = erase_begin; i < erase_end; ++i) {
for (size_t i = erase_begin; i < erase_end; ++i) {
EXPECT_EQ(0, counts[i]);
}
// Check that the elements at the end are preserved.
for (int i = erase_end; i < len; ++i) {
for (size_t i = erase_end; i < len; ++i) {
EXPECT_EQ(1, counts[i]);
}
}
@ -378,21 +378,21 @@ TEST(InlinedVectorTest, ShrinkToFitGrowingVector) {
absl::InlinedVector<std::pair<std::string, int>, 1> v;
v.shrink_to_fit();
EXPECT_EQ(v.capacity(), 1);
EXPECT_EQ(v.capacity(), 1u);
v.emplace_back("answer", 42);
v.shrink_to_fit();
EXPECT_EQ(v.capacity(), 1);
EXPECT_EQ(v.capacity(), 1u);
v.emplace_back("taxicab", 1729);
EXPECT_GE(v.capacity(), 2);
EXPECT_GE(v.capacity(), 2u);
v.shrink_to_fit();
EXPECT_EQ(v.capacity(), 2);
EXPECT_EQ(v.capacity(), 2u);
v.reserve(100);
EXPECT_GE(v.capacity(), 100);
EXPECT_GE(v.capacity(), 100u);
v.shrink_to_fit();
EXPECT_EQ(v.capacity(), 2);
EXPECT_EQ(v.capacity(), 2u);
}
TEST(InlinedVectorTest, ShrinkToFitEdgeCases) {
@ -400,10 +400,10 @@ TEST(InlinedVectorTest, ShrinkToFitEdgeCases) {
absl::InlinedVector<std::pair<std::string, int>, 1> v;
v.emplace_back("answer", 42);
v.emplace_back("taxicab", 1729);
EXPECT_GE(v.capacity(), 2);
EXPECT_GE(v.capacity(), 2u);
v.pop_back();
v.shrink_to_fit();
EXPECT_EQ(v.capacity(), 1);
EXPECT_EQ(v.capacity(), 1u);
EXPECT_EQ(v[0].first, "answer");
EXPECT_EQ(v[0].second, 42);
}
@ -412,34 +412,34 @@ TEST(InlinedVectorTest, ShrinkToFitEdgeCases) {
absl::InlinedVector<std::string, 2> v(100);
v.resize(0);
v.shrink_to_fit();
EXPECT_EQ(v.capacity(), 2); // inlined capacity
EXPECT_EQ(v.capacity(), 2u); // inlined capacity
}
{
absl::InlinedVector<std::string, 2> v(100);
v.resize(1);
v.shrink_to_fit();
EXPECT_EQ(v.capacity(), 2); // inlined capacity
EXPECT_EQ(v.capacity(), 2u); // inlined capacity
}
{
absl::InlinedVector<std::string, 2> v(100);
v.resize(2);
v.shrink_to_fit();
EXPECT_EQ(v.capacity(), 2);
EXPECT_EQ(v.capacity(), 2u);
}
{
absl::InlinedVector<std::string, 2> v(100);
v.resize(3);
v.shrink_to_fit();
EXPECT_EQ(v.capacity(), 3);
EXPECT_EQ(v.capacity(), 3u);
}
}
TEST(IntVec, Insert) {
for (int len = 0; len < 20; len++) {
for (int pos = 0; pos <= len; pos++) {
for (size_t len = 0; len < 20; len++) {
for (ptrdiff_t pos = 0; pos <= static_cast<ptrdiff_t>(len); pos++) {
{
// Single element
std::vector<int> std_v;
@ -527,16 +527,16 @@ TEST(IntVec, Insert) {
TEST(RefCountedVec, InsertConstructorDestructor) {
// Make sure the proper construction/destruction happen during insert
// operations.
for (int len = 0; len < 20; len++) {
for (size_t len = 0; len < 20; len++) {
SCOPED_TRACE(len);
for (int pos = 0; pos <= len; pos++) {
for (size_t pos = 0; pos <= len; pos++) {
SCOPED_TRACE(pos);
std::vector<int> counts(len, 0);
int inserted_count = 0;
RefCountedVec v;
for (int i = 0; i < len; ++i) {
for (size_t i = 0; i < len; ++i) {
SCOPED_TRACE(i);
v.push_back(RefCounted(i, &counts[i]));
v.push_back(RefCounted(static_cast<int>(i), &counts[i]));
}
EXPECT_THAT(counts, Each(Eq(1)));
@ -553,20 +553,20 @@ TEST(RefCountedVec, InsertConstructorDestructor) {
}
TEST(IntVec, Resize) {
for (int len = 0; len < 20; len++) {
for (size_t len = 0; len < 20; len++) {
IntVec v;
Fill(&v, len);
// Try resizing up and down by k elements
static const int kResizeElem = 1000000;
for (int k = 0; k < 10; k++) {
for (size_t k = 0; k < 10; k++) {
// Enlarging resize
v.resize(len + k, kResizeElem);
EXPECT_EQ(len + k, v.size());
EXPECT_LE(len + k, v.capacity());
for (int i = 0; i < len + k; i++) {
for (size_t i = 0; i < len + k; i++) {
if (i < len) {
EXPECT_EQ(i, v[i]);
EXPECT_EQ(static_cast<int>(i), v[i]);
} else {
EXPECT_EQ(kResizeElem, v[i]);
}
@ -576,26 +576,26 @@ TEST(IntVec, Resize) {
v.resize(len, kResizeElem);
EXPECT_EQ(len, v.size());
EXPECT_LE(len, v.capacity());
for (int i = 0; i < len; i++) {
EXPECT_EQ(i, v[i]);
for (size_t i = 0; i < len; i++) {
EXPECT_EQ(static_cast<int>(i), v[i]);
}
}
}
}
TEST(IntVec, InitWithLength) {
for (int len = 0; len < 20; len++) {
for (size_t len = 0; len < 20; len++) {
IntVec v(len, 7);
EXPECT_EQ(len, v.size());
EXPECT_LE(len, v.capacity());
for (int i = 0; i < len; i++) {
for (size_t i = 0; i < len; i++) {
EXPECT_EQ(7, v[i]);
}
}
}
TEST(IntVec, CopyConstructorAndAssignment) {
for (int len = 0; len < 20; len++) {
for (size_t len = 0; len < 20; len++) {
IntVec v;
Fill(&v, len);
EXPECT_EQ(len, v.size());
@ -604,7 +604,7 @@ TEST(IntVec, CopyConstructorAndAssignment) {
IntVec v2(v);
EXPECT_TRUE(v == v2) << PrintToString(v) << PrintToString(v2);
for (int start_len = 0; start_len < 20; start_len++) {
for (size_t start_len = 0; start_len < 20; start_len++) {
IntVec v3;
Fill(&v3, start_len, 99); // Add dummy elements that should go away
v3 = v;
@ -614,7 +614,7 @@ TEST(IntVec, CopyConstructorAndAssignment) {
}
TEST(IntVec, AliasingCopyAssignment) {
for (int len = 0; len < 20; ++len) {
for (size_t len = 0; len < 20; ++len) {
IntVec original;
Fill(&original, len);
IntVec dup = original;
@ -624,9 +624,9 @@ TEST(IntVec, AliasingCopyAssignment) {
}
TEST(IntVec, MoveConstructorAndAssignment) {
for (int len = 0; len < 20; len++) {
for (size_t len = 0; len < 20; len++) {
IntVec v_in;
const int inlined_capacity = v_in.capacity();
const size_t inlined_capacity = v_in.capacity();
Fill(&v_in, len);
EXPECT_EQ(len, v_in.size());
EXPECT_LE(len, v_in.capacity());
@ -643,7 +643,7 @@ TEST(IntVec, MoveConstructorAndAssignment) {
EXPECT_FALSE(v_out.data() == old_data);
}
}
for (int start_len = 0; start_len < 20; start_len++) {
for (size_t start_len = 0; start_len < 20; start_len++) {
IntVec v_out;
Fill(&v_out, start_len, 99); // Add dummy elements that should go away
IntVec v_temp(v_in);
@ -682,10 +682,10 @@ class NotTriviallyDestructible {
};
TEST(AliasingTest, Emplace) {
for (int i = 2; i < 20; ++i) {
for (size_t i = 2; i < 20; ++i) {
absl::InlinedVector<NotTriviallyDestructible, 10> vec;
for (int j = 0; j < i; ++j) {
vec.push_back(NotTriviallyDestructible(j));
for (size_t j = 0; j < i; ++j) {
vec.push_back(NotTriviallyDestructible(static_cast<int>(j)));
}
vec.emplace(vec.begin(), vec[0]);
EXPECT_EQ(vec[0], vec[1]);
@ -697,12 +697,12 @@ TEST(AliasingTest, Emplace) {
}
TEST(AliasingTest, InsertWithCount) {
for (int i = 1; i < 20; ++i) {
for (size_t i = 1; i < 20; ++i) {
absl::InlinedVector<NotTriviallyDestructible, 10> vec;
for (int j = 0; j < i; ++j) {
vec.push_back(NotTriviallyDestructible(j));
for (size_t j = 0; j < i; ++j) {
vec.push_back(NotTriviallyDestructible(static_cast<int>(j)));
}
for (int n = 0; n < 5; ++n) {
for (size_t n = 0; n < 5; ++n) {
// We use back where we can because it's guaranteed to become invalidated
vec.insert(vec.begin(), n, vec.back());
auto b = vec.begin();
@ -760,22 +760,22 @@ TEST(OverheadTest, Storage) {
}
TEST(IntVec, Clear) {
for (int len = 0; len < 20; len++) {
for (size_t len = 0; len < 20; len++) {
SCOPED_TRACE(len);
IntVec v;
Fill(&v, len);
v.clear();
EXPECT_EQ(0, v.size());
EXPECT_EQ(0u, v.size());
EXPECT_EQ(v.begin(), v.end());
}
}
TEST(IntVec, Reserve) {
for (int len = 0; len < 20; len++) {
for (size_t len = 0; len < 20; len++) {
IntVec v;
Fill(&v, len);
for (int newlen = 0; newlen < 100; newlen++) {
for (size_t newlen = 0; newlen < 100; newlen++) {
const int* start_rep = v.data();
v.reserve(newlen);
const int* final_rep = v.data();
@ -842,9 +842,9 @@ TEST(StringVec, SelfMove) {
}
TEST(IntVec, Swap) {
for (int l1 = 0; l1 < 20; l1++) {
for (size_t l1 = 0; l1 < 20; l1++) {
SCOPED_TRACE(l1);
for (int l2 = 0; l2 < 20; l2++) {
for (size_t l2 = 0; l2 < 20; l2++) {
SCOPED_TRACE(l2);
IntVec a = Fill(l1, 0);
IntVec b = Fill(l2, 100);
@ -854,13 +854,13 @@ TEST(IntVec, Swap) {
}
EXPECT_EQ(l1, b.size());
EXPECT_EQ(l2, a.size());
for (int i = 0; i < l1; i++) {
for (size_t i = 0; i < l1; i++) {
SCOPED_TRACE(i);
EXPECT_EQ(i, b[i]);
EXPECT_EQ(static_cast<int>(i), b[i]);
}
for (int i = 0; i < l2; i++) {
for (size_t i = 0; i < l2; i++) {
SCOPED_TRACE(i);
EXPECT_EQ(100 + i, a[i]);
EXPECT_EQ(100 + static_cast<int>(i), a[i]);
}
}
}
@ -869,46 +869,48 @@ TEST(IntVec, Swap) {
TYPED_TEST_P(InstanceTest, Swap) {
using Instance = TypeParam;
using InstanceVec = absl::InlinedVector<Instance, 8>;
for (int l1 = 0; l1 < 20; l1++) {
for (size_t l1 = 0; l1 < 20; l1++) {
SCOPED_TRACE(l1);
for (int l2 = 0; l2 < 20; l2++) {
for (size_t l2 = 0; l2 < 20; l2++) {
SCOPED_TRACE(l2);
InstanceTracker tracker;
InstanceVec a, b;
const size_t inlined_capacity = a.capacity();
auto min_len = std::min(l1, l2);
auto max_len = std::max(l1, l2);
for (int i = 0; i < l1; i++) a.push_back(Instance(i));
for (int i = 0; i < l2; i++) b.push_back(Instance(100 + i));
EXPECT_EQ(tracker.instances(), l1 + l2);
for (size_t i = 0; i < l1; i++)
a.push_back(Instance(static_cast<int>(i)));
for (size_t i = 0; i < l2; i++)
b.push_back(Instance(100 + static_cast<int>(i)));
EXPECT_EQ(tracker.instances(), static_cast<int>(l1 + l2));
tracker.ResetCopiesMovesSwaps();
{
using std::swap;
swap(a, b);
}
EXPECT_EQ(tracker.instances(), l1 + l2);
EXPECT_EQ(tracker.instances(), static_cast<int>(l1 + l2));
if (a.size() > inlined_capacity && b.size() > inlined_capacity) {
EXPECT_EQ(tracker.swaps(), 0); // Allocations are swapped.
EXPECT_EQ(tracker.moves(), 0);
} else if (a.size() <= inlined_capacity && b.size() <= inlined_capacity) {
EXPECT_EQ(tracker.swaps(), min_len);
EXPECT_EQ(tracker.swaps(), static_cast<int>(min_len));
EXPECT_EQ((tracker.moves() ? tracker.moves() : tracker.copies()),
max_len - min_len);
static_cast<int>(max_len - min_len));
} else {
// One is allocated and the other isn't. The allocation is transferred
// without copying elements, and the inlined instances are copied/moved.
EXPECT_EQ(tracker.swaps(), 0);
EXPECT_EQ((tracker.moves() ? tracker.moves() : tracker.copies()),
min_len);
static_cast<int>(min_len));
}
EXPECT_EQ(l1, b.size());
EXPECT_EQ(l2, a.size());
for (int i = 0; i < l1; i++) {
EXPECT_EQ(i, b[i].value());
for (size_t i = 0; i < l1; i++) {
EXPECT_EQ(static_cast<int>(i), b[i].value());
}
for (int i = 0; i < l2; i++) {
EXPECT_EQ(100 + i, a[i].value());
for (size_t i = 0; i < l2; i++) {
EXPECT_EQ(100 + static_cast<int>(i), a[i].value());
}
}
}
@ -937,9 +939,9 @@ TEST(IntVec, EqualAndNotEqual) {
a.clear();
b.clear();
for (int i = 0; i < 100; i++) {
a.push_back(i);
b.push_back(i);
for (size_t i = 0; i < 100; i++) {
a.push_back(static_cast<int>(i));
b.push_back(static_cast<int>(i));
EXPECT_TRUE(a == b);
EXPECT_FALSE(a != b);
@ -978,26 +980,26 @@ TYPED_TEST_P(InstanceTest, CountConstructorsDestructors) {
using Instance = TypeParam;
using InstanceVec = absl::InlinedVector<Instance, 8>;
InstanceTracker tracker;
for (int len = 0; len < 20; len++) {
for (size_t len = 0; len < 20; len++) {
SCOPED_TRACE(len);
tracker.ResetCopiesMovesSwaps();
InstanceVec v;
const size_t inlined_capacity = v.capacity();
for (int i = 0; i < len; i++) {
v.push_back(Instance(i));
for (size_t i = 0; i < len; i++) {
v.push_back(Instance(static_cast<int>(i)));
}
EXPECT_EQ(tracker.instances(), len);
EXPECT_EQ(tracker.instances(), static_cast<int>(len));
EXPECT_GE(tracker.copies() + tracker.moves(),
len); // More due to reallocation.
static_cast<int>(len)); // More due to reallocation.
tracker.ResetCopiesMovesSwaps();
// Enlarging resize() must construct some objects
tracker.ResetCopiesMovesSwaps();
v.resize(len + 10, Instance(100));
EXPECT_EQ(tracker.instances(), len + 10);
EXPECT_EQ(tracker.instances(), static_cast<int>(len) + 10);
if (len <= inlined_capacity && len + 10 > inlined_capacity) {
EXPECT_EQ(tracker.copies() + tracker.moves(), 10 + len);
EXPECT_EQ(tracker.copies() + tracker.moves(), 10 + static_cast<int>(len));
} else {
// Only specify a minimum number of copies + moves. We don't want to
// depend on the reallocation policy here.
@ -1008,29 +1010,30 @@ TYPED_TEST_P(InstanceTest, CountConstructorsDestructors) {
// Shrinking resize() must destroy some objects
tracker.ResetCopiesMovesSwaps();
v.resize(len, Instance(100));
EXPECT_EQ(tracker.instances(), len);
EXPECT_EQ(tracker.instances(), static_cast<int>(len));
EXPECT_EQ(tracker.copies(), 0);
EXPECT_EQ(tracker.moves(), 0);
// reserve() must not increase the number of initialized objects
SCOPED_TRACE("reserve");
v.reserve(len + 1000);
EXPECT_EQ(tracker.instances(), len);
EXPECT_EQ(tracker.copies() + tracker.moves(), len);
EXPECT_EQ(tracker.instances(), static_cast<int>(len));
EXPECT_EQ(tracker.copies() + tracker.moves(), static_cast<int>(len));
// pop_back() and erase() must destroy one object
if (len > 0) {
tracker.ResetCopiesMovesSwaps();
v.pop_back();
EXPECT_EQ(tracker.instances(), len - 1);
EXPECT_EQ(tracker.instances(), static_cast<int>(len) - 1);
EXPECT_EQ(tracker.copies(), 0);
EXPECT_EQ(tracker.moves(), 0);
if (!v.empty()) {
tracker.ResetCopiesMovesSwaps();
v.erase(v.begin());
EXPECT_EQ(tracker.instances(), len - 2);
EXPECT_EQ(tracker.copies() + tracker.moves(), len - 2);
EXPECT_EQ(tracker.instances(), static_cast<int>(len) - 2);
EXPECT_EQ(tracker.copies() + tracker.moves(),
static_cast<int>(len) - 2);
}
}
@ -1087,12 +1090,12 @@ TYPED_TEST_P(InstanceTest, CountConstructorsDestructorsOnMoveConstruction) {
tracker.ResetCopiesMovesSwaps();
{
InstanceVec v_copy(std::move(v));
if (len > inlined_capacity) {
if (static_cast<size_t>(len) > inlined_capacity) {
// Allocation is moved as a whole.
EXPECT_EQ(tracker.instances(), len);
EXPECT_EQ(tracker.live_instances(), len);
// Tests an implementation detail, don't rely on this in your code.
EXPECT_EQ(v.size(), 0); // NOLINT misc-use-after-move
EXPECT_EQ(v.size(), 0u); // NOLINT misc-use-after-move
EXPECT_EQ(tracker.copies(), 0);
EXPECT_EQ(tracker.moves(), 0);
} else {
@ -1158,7 +1161,7 @@ TYPED_TEST_P(InstanceTest, CountConstructorsDestructorsOnMoveAssignment) {
tracker.ResetCopiesMovesSwaps();
InstanceVec longer, shorter;
const int inlined_capacity = longer.capacity();
const size_t inlined_capacity = longer.capacity();
for (int i = 0; i < len; i++) {
longer.push_back(Instance(i));
shorter.push_back(Instance(i));
@ -1177,7 +1180,7 @@ TYPED_TEST_P(InstanceTest, CountConstructorsDestructorsOnMoveAssignment) {
src_len = len;
longer = std::move(shorter);
}
if (src_len > inlined_capacity) {
if (static_cast<size_t>(src_len) > inlined_capacity) {
// Allocation moved as a whole.
EXPECT_EQ(tracker.instances(), src_len);
EXPECT_EQ(tracker.live_instances(), src_len);
@ -1210,10 +1213,10 @@ TEST(CountElemAssign, SimpleTypeWithInlineBacking) {
absl::InlinedVector<int, 2> v(original_contents.begin(),
original_contents.end());
v.assign(2, 123);
EXPECT_THAT(v, AllOf(SizeIs(2), ElementsAre(123, 123)));
EXPECT_THAT(v, AllOf(SizeIs(2u), ElementsAre(123, 123)));
if (original_size <= 2) {
// If the original had inline backing, it should stay inline.
EXPECT_EQ(2, v.capacity());
EXPECT_EQ(2u, v.capacity());
}
}
}
@ -1227,7 +1230,7 @@ TEST(CountElemAssign, SimpleTypeWithAllocation) {
absl::InlinedVector<int, 2> v(original_contents.begin(),
original_contents.end());
v.assign(3, 123);
EXPECT_THAT(v, AllOf(SizeIs(3), ElementsAre(123, 123, 123)));
EXPECT_THAT(v, AllOf(SizeIs(3u), ElementsAre(123, 123, 123)));
EXPECT_LE(v.size(), v.capacity());
}
}
@ -1242,10 +1245,10 @@ TYPED_TEST_P(InstanceTest, CountElemAssignInlineBacking) {
absl::InlinedVector<Instance, 2> v(original_contents.begin(),
original_contents.end());
v.assign(2, Instance(123));
EXPECT_THAT(v, AllOf(SizeIs(2), ElementsAre(ValueIs(123), ValueIs(123))));
EXPECT_THAT(v, AllOf(SizeIs(2u), ElementsAre(ValueIs(123), ValueIs(123))));
if (original_size <= 2) {
// If the original had inline backing, it should stay inline.
EXPECT_EQ(2, v.capacity());
EXPECT_EQ(2u, v.capacity());
}
}
}
@ -1260,7 +1263,7 @@ void InstanceCountElemAssignWithAllocationTest() {
absl::InlinedVector<Instance, 2> v(original_contents.begin(),
original_contents.end());
v.assign(3, Instance(123));
EXPECT_THAT(v, AllOf(SizeIs(3), ElementsAre(ValueIs(123), ValueIs(123),
EXPECT_THAT(v, AllOf(SizeIs(3u), ElementsAre(ValueIs(123), ValueIs(123),
ValueIs(123))));
EXPECT_LE(v.size(), v.capacity());
}
@ -1276,16 +1279,17 @@ TEST(RangedConstructor, SimpleType) {
std::vector<int> source_v = {4, 5, 6};
// First try to fit in inline backing
absl::InlinedVector<int, 4> v(source_v.begin(), source_v.end());
EXPECT_EQ(3, v.size());
EXPECT_EQ(4, v.capacity()); // Indication that we're still on inlined storage
EXPECT_EQ(3u, v.size());
EXPECT_EQ(4u,
v.capacity()); // Indication that we're still on inlined storage
EXPECT_EQ(4, v[0]);
EXPECT_EQ(5, v[1]);
EXPECT_EQ(6, v[2]);
// Now, force a re-allocate
absl::InlinedVector<int, 2> realloc_v(source_v.begin(), source_v.end());
EXPECT_EQ(3, realloc_v.size());
EXPECT_LT(2, realloc_v.capacity());
EXPECT_EQ(3u, realloc_v.size());
EXPECT_LT(2u, realloc_v.capacity());
EXPECT_EQ(4, realloc_v[0]);
EXPECT_EQ(5, realloc_v[1]);
EXPECT_EQ(6, realloc_v[2]);
@ -1300,8 +1304,8 @@ void InstanceRangedConstructorTestForContainer() {
tracker.ResetCopiesMovesSwaps();
absl::InlinedVector<Instance, inlined_capacity> v(source_v.begin(),
source_v.end());
EXPECT_EQ(2, v.size());
EXPECT_LT(1, v.capacity());
EXPECT_EQ(2u, v.size());
EXPECT_LT(1u, v.capacity());
EXPECT_EQ(0, v[0].value());
EXPECT_EQ(1, v[1].value());
EXPECT_EQ(tracker.copies(), 2);
@ -1360,12 +1364,12 @@ TEST(RangedAssign, SimpleType) {
// Original contents are [12345, 12345, ...]
std::vector<int> original_contents(original_size, 12345);
for (size_t target_size = 0; target_size <= 5; ++target_size) {
for (int target_size = 0; target_size <= 5; ++target_size) {
SCOPED_TRACE(target_size);
// New contents are [3, 4, ...]
std::vector<int> new_contents;
for (size_t i = 0; i < target_size; ++i) {
for (int i = 0; i < target_size; ++i) {
new_contents.push_back(i + 3);
}
@ -1377,7 +1381,7 @@ TEST(RangedAssign, SimpleType) {
EXPECT_LE(new_contents.size(), v.capacity());
if (target_size <= 3 && original_size <= 3) {
// Storage should stay inline when target size is small.
EXPECT_EQ(3, v.capacity());
EXPECT_EQ(3u, v.capacity());
}
EXPECT_THAT(v, ElementsAreArray(new_contents));
}
@ -1410,7 +1414,7 @@ void InstanceRangedAssignTestForContainer() {
// TODO(bsamwel): Test with an input iterator.
std::vector<Instance> new_contents_in;
for (size_t i = 0; i < target_size; ++i) {
new_contents_in.push_back(Instance(i + 3));
new_contents_in.push_back(Instance(static_cast<int>(i) + 3));
}
SourceContainer new_contents(new_contents_in.begin(),
new_contents_in.end());
@ -1423,7 +1427,7 @@ void InstanceRangedAssignTestForContainer() {
EXPECT_LE(new_contents.size(), v.capacity());
if (target_size <= 3 && original_size <= 3) {
// Storage should stay inline when target size is small.
EXPECT_EQ(3, v.capacity());
EXPECT_EQ(3u, v.capacity());
}
EXPECT_TRUE(std::equal(v.begin(), v.end(), new_contents.begin(),
InstanceValuesEqual<Instance>));
@ -1447,12 +1451,12 @@ TYPED_TEST_P(InstanceTest, RangedAssign) {
TEST(InitializerListConstructor, SimpleTypeWithInlineBacking) {
EXPECT_THAT((absl::InlinedVector<int, 4>{4, 5, 6}),
AllOf(SizeIs(3), CapacityIs(4), ElementsAre(4, 5, 6)));
AllOf(SizeIs(3u), CapacityIs(4u), ElementsAre(4, 5, 6)));
}
TEST(InitializerListConstructor, SimpleTypeWithReallocationRequired) {
EXPECT_THAT((absl::InlinedVector<int, 2>{4, 5, 6}),
AllOf(SizeIs(3), CapacityIs(Gt(2)), ElementsAre(4, 5, 6)));
AllOf(SizeIs(3u), CapacityIs(Gt(2u)), ElementsAre(4, 5, 6)));
}
TEST(InitializerListConstructor, DisparateTypesInList) {
@ -1465,14 +1469,14 @@ TEST(InitializerListConstructor, DisparateTypesInList) {
TEST(InitializerListConstructor, ComplexTypeWithInlineBacking) {
EXPECT_THAT((absl::InlinedVector<CopyableMovableInstance, 1>{
CopyableMovableInstance(0)}),
AllOf(SizeIs(1), CapacityIs(1), ElementsAre(ValueIs(0))));
AllOf(SizeIs(1u), CapacityIs(1u), ElementsAre(ValueIs(0))));
}
TEST(InitializerListConstructor, ComplexTypeWithReallocationRequired) {
EXPECT_THAT(
(absl::InlinedVector<CopyableMovableInstance, 1>{
EXPECT_THAT((absl::InlinedVector<CopyableMovableInstance, 1>{
CopyableMovableInstance(0), CopyableMovableInstance(1)}),
AllOf(SizeIs(2), CapacityIs(Gt(1)), ElementsAre(ValueIs(0), ValueIs(1))));
AllOf(SizeIs(2u), CapacityIs(Gt(1u)),
ElementsAre(ValueIs(0), ValueIs(1))));
}
TEST(InitializerListAssign, SimpleTypeFitsInlineBacking) {
@ -1482,14 +1486,14 @@ TEST(InitializerListAssign, SimpleTypeFitsInlineBacking) {
absl::InlinedVector<int, 2> v1(original_size, 12345);
const size_t original_capacity_v1 = v1.capacity();
v1.assign({3});
EXPECT_THAT(
v1, AllOf(SizeIs(1), CapacityIs(original_capacity_v1), ElementsAre(3)));
EXPECT_THAT(v1, AllOf(SizeIs(1u), CapacityIs(original_capacity_v1),
ElementsAre(3)));
absl::InlinedVector<int, 2> v2(original_size, 12345);
const size_t original_capacity_v2 = v2.capacity();
v2 = {3};
EXPECT_THAT(
v2, AllOf(SizeIs(1), CapacityIs(original_capacity_v2), ElementsAre(3)));
EXPECT_THAT(v2, AllOf(SizeIs(1u), CapacityIs(original_capacity_v2),
ElementsAre(3)));
}
}
@ -1498,13 +1502,13 @@ TEST(InitializerListAssign, SimpleTypeDoesNotFitInlineBacking) {
SCOPED_TRACE(original_size);
absl::InlinedVector<int, 2> v1(original_size, 12345);
v1.assign({3, 4, 5});
EXPECT_THAT(v1, AllOf(SizeIs(3), ElementsAre(3, 4, 5)));
EXPECT_LE(3, v1.capacity());
EXPECT_THAT(v1, AllOf(SizeIs(3u), ElementsAre(3, 4, 5)));
EXPECT_LE(3u, v1.capacity());
absl::InlinedVector<int, 2> v2(original_size, 12345);
v2 = {3, 4, 5};
EXPECT_THAT(v2, AllOf(SizeIs(3), ElementsAre(3, 4, 5)));
EXPECT_LE(3, v2.capacity());
EXPECT_THAT(v2, AllOf(SizeIs(3u), ElementsAre(3, 4, 5)));
EXPECT_LE(3u, v2.capacity());
}
}
@ -1533,7 +1537,7 @@ TYPED_TEST_P(InstanceTest, InitializerListAssign) {
absl::InlinedVector<Instance, 2> v(original_size, Instance(12345));
const size_t original_capacity = v.capacity();
v.assign({Instance(3)});
EXPECT_THAT(v, AllOf(SizeIs(1), CapacityIs(original_capacity),
EXPECT_THAT(v, AllOf(SizeIs(1u), CapacityIs(original_capacity),
ElementsAre(ValueIs(3))));
}
for (size_t original_size = 0; original_size <= 4; ++original_size) {
@ -1541,8 +1545,8 @@ TYPED_TEST_P(InstanceTest, InitializerListAssign) {
absl::InlinedVector<Instance, 2> v(original_size, Instance(12345));
v.assign({Instance(3), Instance(4), Instance(5)});
EXPECT_THAT(
v, AllOf(SizeIs(3), ElementsAre(ValueIs(3), ValueIs(4), ValueIs(5))));
EXPECT_LE(3, v.capacity());
v, AllOf(SizeIs(3u), ElementsAre(ValueIs(3), ValueIs(4), ValueIs(5))));
EXPECT_LE(3u, v.capacity());
}
}
@ -1588,54 +1592,54 @@ TEST(AllocatorSupportTest, CountAllocations) {
MyAlloc alloc(&allocated);
{
AllocVec ABSL_ATTRIBUTE_UNUSED v(ia, ia + 4, alloc);
EXPECT_THAT(allocated, 0);
EXPECT_THAT(allocated, Eq(0));
}
EXPECT_THAT(allocated, 0);
EXPECT_THAT(allocated, Eq(0));
{
AllocVec ABSL_ATTRIBUTE_UNUSED v(ia, ia + ABSL_ARRAYSIZE(ia), alloc);
EXPECT_THAT(allocated, v.size() * sizeof(int));
EXPECT_THAT(allocated, Eq(static_cast<int64_t>(v.size() * sizeof(int))));
}
EXPECT_THAT(allocated, 0);
EXPECT_THAT(allocated, Eq(0));
{
AllocVec v(4, 1, alloc);
EXPECT_THAT(allocated, 0);
EXPECT_THAT(allocated, Eq(0));
int64_t allocated2 = 0;
MyAlloc alloc2(&allocated2);
AllocVec v2(v, alloc2);
EXPECT_THAT(allocated2, 0);
EXPECT_THAT(allocated2, Eq(0));
int64_t allocated3 = 0;
MyAlloc alloc3(&allocated3);
AllocVec v3(std::move(v), alloc3);
EXPECT_THAT(allocated3, 0);
EXPECT_THAT(allocated3, Eq(0));
}
EXPECT_THAT(allocated, 0);
{
AllocVec v(8, 2, alloc);
EXPECT_THAT(allocated, v.size() * sizeof(int));
EXPECT_THAT(allocated, Eq(static_cast<int64_t>(v.size() * sizeof(int))));
int64_t allocated2 = 0;
MyAlloc alloc2(&allocated2);
AllocVec v2(v, alloc2);
EXPECT_THAT(allocated2, v2.size() * sizeof(int));
EXPECT_THAT(allocated2, Eq(static_cast<int64_t>(v2.size() * sizeof(int))));
int64_t allocated3 = 0;
MyAlloc alloc3(&allocated3);
AllocVec v3(std::move(v), alloc3);
EXPECT_THAT(allocated3, v3.size() * sizeof(int));
EXPECT_THAT(allocated3, Eq(static_cast<int64_t>(v3.size() * sizeof(int))));
}
EXPECT_EQ(allocated, 0);
{
// Test shrink_to_fit deallocations.
AllocVec v(8, 2, alloc);
EXPECT_EQ(allocated, 8 * sizeof(int));
EXPECT_EQ(allocated, static_cast<int64_t>(8 * sizeof(int)));
v.resize(5);
EXPECT_EQ(allocated, 8 * sizeof(int));
EXPECT_EQ(allocated, static_cast<int64_t>(8 * sizeof(int)));
v.shrink_to_fit();
EXPECT_EQ(allocated, 5 * sizeof(int));
EXPECT_EQ(allocated, static_cast<int64_t>(5 * sizeof(int)));
v.resize(4);
EXPECT_EQ(allocated, 5 * sizeof(int));
EXPECT_EQ(allocated, static_cast<int64_t>(5 * sizeof(int)));
v.shrink_to_fit();
EXPECT_EQ(allocated, 0);
}
@ -1654,13 +1658,17 @@ TEST(AllocatorSupportTest, SwapBothAllocated) {
AllocVec v1(ia1, ia1 + ABSL_ARRAYSIZE(ia1), a1);
AllocVec v2(ia2, ia2 + ABSL_ARRAYSIZE(ia2), a2);
EXPECT_LT(v1.capacity(), v2.capacity());
EXPECT_THAT(allocated1, v1.capacity() * sizeof(int));
EXPECT_THAT(allocated2, v2.capacity() * sizeof(int));
EXPECT_THAT(allocated1,
Eq(static_cast<int64_t>(v1.capacity() * sizeof(int))));
EXPECT_THAT(allocated2,
Eq(static_cast<int64_t>(v2.capacity() * sizeof(int))));
v1.swap(v2);
EXPECT_THAT(v1, ElementsAreArray(ia2));
EXPECT_THAT(v2, ElementsAreArray(ia1));
EXPECT_THAT(allocated1, v2.capacity() * sizeof(int));
EXPECT_THAT(allocated2, v1.capacity() * sizeof(int));
EXPECT_THAT(allocated1,
Eq(static_cast<int64_t>(v2.capacity() * sizeof(int))));
EXPECT_THAT(allocated2,
Eq(static_cast<int64_t>(v1.capacity() * sizeof(int))));
}
EXPECT_THAT(allocated1, 0);
EXPECT_THAT(allocated2, 0);
@ -1678,13 +1686,15 @@ TEST(AllocatorSupportTest, SwapOneAllocated) {
MyAlloc a2(&allocated2);
AllocVec v1(ia1, ia1 + ABSL_ARRAYSIZE(ia1), a1);
AllocVec v2(ia2, ia2 + ABSL_ARRAYSIZE(ia2), a2);
EXPECT_THAT(allocated1, v1.capacity() * sizeof(int));
EXPECT_THAT(allocated2, 0);
EXPECT_THAT(allocated1,
Eq(static_cast<int64_t>(v1.capacity() * sizeof(int))));
EXPECT_THAT(allocated2, Eq(0));
v1.swap(v2);
EXPECT_THAT(v1, ElementsAreArray(ia2));
EXPECT_THAT(v2, ElementsAreArray(ia1));
EXPECT_THAT(allocated1, v2.capacity() * sizeof(int));
EXPECT_THAT(allocated2, 0);
EXPECT_THAT(allocated1,
Eq(static_cast<int64_t>(v2.capacity() * sizeof(int))));
EXPECT_THAT(allocated2, Eq(0));
EXPECT_TRUE(v2.get_allocator() == a1);
EXPECT_TRUE(v1.get_allocator() == a2);
}
@ -1746,7 +1756,7 @@ TEST(AllocatorSupportTest, ScopedAllocatorWorksAllocated) {
}
TEST(AllocatorSupportTest, SizeAllocConstructor) {
constexpr int inlined_size = 4;
constexpr size_t inlined_size = 4;
using Alloc = CountingAllocator<int>;
using AllocVec = absl::InlinedVector<int, inlined_size, Alloc>;
@ -1756,7 +1766,7 @@ TEST(AllocatorSupportTest, SizeAllocConstructor) {
auto v = AllocVec(len, Alloc(&allocated));
// Inline storage used; allocator should not be invoked
EXPECT_THAT(allocated, 0);
EXPECT_THAT(allocated, Eq(0));
EXPECT_THAT(v, AllOf(SizeIs(len), Each(0)));
}
@ -1766,7 +1776,7 @@ TEST(AllocatorSupportTest, SizeAllocConstructor) {
auto v = AllocVec(len, Alloc(&allocated));
// Out of line storage used; allocation of 8 elements expected
EXPECT_THAT(allocated, len * sizeof(int));
EXPECT_THAT(allocated, Eq(static_cast<int64_t>(len * sizeof(int))));
EXPECT_THAT(v, AllOf(SizeIs(len), Each(0)));
}
}
@ -1801,9 +1811,9 @@ TEST(InlinedVectorTest, AbslHashValueWorks) {
// Generate a variety of vectors some of these are small enough for the inline
// space but are stored out of line.
for (int i = 0; i < 10; ++i) {
for (size_t i = 0; i < 10; ++i) {
V v;
for (int j = 0; j < i; ++j) {
for (int j = 0; j < static_cast<int>(i); ++j) {
v.push_back(j);
}
cases.push_back(v);

@ -222,7 +222,7 @@ TEST(HashValueTest, PointerAlignment) {
// Limit the scope to the bits we would be using for Swisstable.
constexpr size_t kMask = (1 << (kLog2NumValues + 7)) - 1;
size_t stuck_bits = (~bits_or | bits_and) & kMask;
EXPECT_EQ(stuck_bits, 0) << "0x" << std::hex << stuck_bits;
EXPECT_EQ(stuck_bits, 0u) << "0x" << std::hex << stuck_bits;
}
}
@ -737,10 +737,10 @@ TEST(HashValueTest, CombinePiecewiseBuffer) {
//
// This test is run on a buffer that is a multiple of the stride size, and one
// that isn't.
for (size_t big_buffer_size : {1024 * 2 + 512, 1024 * 3}) {
for (size_t big_buffer_size : {1024u * 2 + 512u, 1024u * 3}) {
SCOPED_TRACE(big_buffer_size);
std::string big_buffer;
for (int i = 0; i < big_buffer_size; ++i) {
for (size_t i = 0; i < big_buffer_size; ++i) {
// Arbitrary string
big_buffer.push_back(32 + (i * (i / 3)) % 64);
}
@ -1135,10 +1135,10 @@ TEST(HashTest, HashNonUniquelyRepresentedType) {
unsigned char buffer2[kNumStructs * sizeof(StructWithPadding)];
std::memset(buffer2, 255, sizeof(buffer2));
auto* s2 = reinterpret_cast<StructWithPadding*>(buffer2);
for (int i = 0; i < kNumStructs; ++i) {
for (size_t i = 0; i < kNumStructs; ++i) {
SCOPED_TRACE(i);
s1[i].c = s2[i].c = '0' + i;
s1[i].i = s2[i].i = i;
s1[i].c = s2[i].c = static_cast<char>('0' + i);
s1[i].i = s2[i].i = static_cast<int>(i);
ASSERT_FALSE(memcmp(buffer1 + i * sizeof(StructWithPadding),
buffer2 + i * sizeof(StructWithPadding),
sizeof(StructWithPadding)) == 0)
@ -1226,7 +1226,9 @@ struct ValueWithBoolConversion {
namespace std {
template <>
struct hash<ValueWithBoolConversion> {
size_t operator()(ValueWithBoolConversion v) { return v.i; }
size_t operator()(ValueWithBoolConversion v) {
return static_cast<size_t>(v.i);
}
};
} // namespace std

@ -1521,7 +1521,7 @@ static absl::StatusOr<int> MakeStatus() { return 100; }
TEST(StatusOr, TestIgnoreError) { MakeStatus().IgnoreError(); }
TEST(StatusOr, EqualityOperator) {
constexpr int kNumCases = 4;
constexpr size_t kNumCases = 4;
std::array<absl::StatusOr<int>, kNumCases> group1 = {
absl::StatusOr<int>(1), absl::StatusOr<int>(2),
absl::StatusOr<int>(absl::InvalidArgumentError("msg")),
@ -1530,8 +1530,8 @@ TEST(StatusOr, EqualityOperator) {
absl::StatusOr<int>(1), absl::StatusOr<int>(2),
absl::StatusOr<int>(absl::InvalidArgumentError("msg")),
absl::StatusOr<int>(absl::InternalError("msg"))};
for (int i = 0; i < kNumCases; ++i) {
for (int j = 0; j < kNumCases; ++j) {
for (size_t i = 0; i < kNumCases; ++i) {
for (size_t j = 0; j < kNumCases; ++j) {
if (i == j) {
EXPECT_TRUE(group1[i] == group2[j]);
EXPECT_FALSE(group1[i] != group2[j]);

@ -27,103 +27,99 @@ namespace {
TEST(AsciiIsFoo, All) {
for (int i = 0; i < 256; i++) {
if ((i >= 'a' && i <= 'z') || (i >= 'A' && i <= 'Z'))
EXPECT_TRUE(absl::ascii_isalpha(i)) << ": failed on " << i;
const auto c = static_cast<unsigned char>(i);
if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'))
EXPECT_TRUE(absl::ascii_isalpha(c)) << ": failed on " << c;
else
EXPECT_TRUE(!absl::ascii_isalpha(i)) << ": failed on " << i;
EXPECT_TRUE(!absl::ascii_isalpha(c)) << ": failed on " << c;
}
for (int i = 0; i < 256; i++) {
if ((i >= '0' && i <= '9'))
EXPECT_TRUE(absl::ascii_isdigit(i)) << ": failed on " << i;
const auto c = static_cast<unsigned char>(i);
if ((c >= '0' && c <= '9'))
EXPECT_TRUE(absl::ascii_isdigit(c)) << ": failed on " << c;
else
EXPECT_TRUE(!absl::ascii_isdigit(i)) << ": failed on " << i;
EXPECT_TRUE(!absl::ascii_isdigit(c)) << ": failed on " << c;
}
for (int i = 0; i < 256; i++) {
if (absl::ascii_isalpha(i) || absl::ascii_isdigit(i))
EXPECT_TRUE(absl::ascii_isalnum(i)) << ": failed on " << i;
const auto c = static_cast<unsigned char>(i);
if (absl::ascii_isalpha(c) || absl::ascii_isdigit(c))
EXPECT_TRUE(absl::ascii_isalnum(c)) << ": failed on " << c;
else
EXPECT_TRUE(!absl::ascii_isalnum(i)) << ": failed on " << i;
EXPECT_TRUE(!absl::ascii_isalnum(c)) << ": failed on " << c;
}
for (int i = 0; i < 256; i++) {
const auto c = static_cast<unsigned char>(i);
if (i != '\0' && strchr(" \r\n\t\v\f", i))
EXPECT_TRUE(absl::ascii_isspace(i)) << ": failed on " << i;
EXPECT_TRUE(absl::ascii_isspace(c)) << ": failed on " << c;
else
EXPECT_TRUE(!absl::ascii_isspace(i)) << ": failed on " << i;
EXPECT_TRUE(!absl::ascii_isspace(c)) << ": failed on " << c;
}
for (int i = 0; i < 256; i++) {
const auto c = static_cast<unsigned char>(i);
if (i >= 32 && i < 127)
EXPECT_TRUE(absl::ascii_isprint(i)) << ": failed on " << i;
EXPECT_TRUE(absl::ascii_isprint(c)) << ": failed on " << c;
else
EXPECT_TRUE(!absl::ascii_isprint(i)) << ": failed on " << i;
EXPECT_TRUE(!absl::ascii_isprint(c)) << ": failed on " << c;
}
for (int i = 0; i < 256; i++) {
if (absl::ascii_isprint(i) && !absl::ascii_isspace(i) &&
!absl::ascii_isalnum(i))
EXPECT_TRUE(absl::ascii_ispunct(i)) << ": failed on " << i;
else
EXPECT_TRUE(!absl::ascii_ispunct(i)) << ": failed on " << i;
const auto c = static_cast<unsigned char>(i);
if (absl::ascii_isprint(c) && !absl::ascii_isspace(c) &&
!absl::ascii_isalnum(c)) {
EXPECT_TRUE(absl::ascii_ispunct(c)) << ": failed on " << c;
} else {
EXPECT_TRUE(!absl::ascii_ispunct(c)) << ": failed on " << c;
}
}
for (int i = 0; i < 256; i++) {
const auto c = static_cast<unsigned char>(i);
if (i == ' ' || i == '\t')
EXPECT_TRUE(absl::ascii_isblank(i)) << ": failed on " << i;
EXPECT_TRUE(absl::ascii_isblank(c)) << ": failed on " << c;
else
EXPECT_TRUE(!absl::ascii_isblank(i)) << ": failed on " << i;
EXPECT_TRUE(!absl::ascii_isblank(c)) << ": failed on " << c;
}
for (int i = 0; i < 256; i++) {
const auto c = static_cast<unsigned char>(i);
if (i < 32 || i == 127)
EXPECT_TRUE(absl::ascii_iscntrl(i)) << ": failed on " << i;
EXPECT_TRUE(absl::ascii_iscntrl(c)) << ": failed on " << c;
else
EXPECT_TRUE(!absl::ascii_iscntrl(i)) << ": failed on " << i;
EXPECT_TRUE(!absl::ascii_iscntrl(c)) << ": failed on " << c;
}
for (int i = 0; i < 256; i++) {
if (absl::ascii_isdigit(i) || (i >= 'A' && i <= 'F') ||
(i >= 'a' && i <= 'f'))
EXPECT_TRUE(absl::ascii_isxdigit(i)) << ": failed on " << i;
else
EXPECT_TRUE(!absl::ascii_isxdigit(i)) << ": failed on " << i;
const auto c = static_cast<unsigned char>(i);
if (absl::ascii_isdigit(c) || (i >= 'A' && i <= 'F') ||
(i >= 'a' && i <= 'f')) {
EXPECT_TRUE(absl::ascii_isxdigit(c)) << ": failed on " << c;
} else {
EXPECT_TRUE(!absl::ascii_isxdigit(c)) << ": failed on " << c;
}
}
for (int i = 0; i < 256; i++) {
const auto c = static_cast<unsigned char>(i);
if (i > 32 && i < 127)
EXPECT_TRUE(absl::ascii_isgraph(i)) << ": failed on " << i;
EXPECT_TRUE(absl::ascii_isgraph(c)) << ": failed on " << c;
else
EXPECT_TRUE(!absl::ascii_isgraph(i)) << ": failed on " << i;
EXPECT_TRUE(!absl::ascii_isgraph(c)) << ": failed on " << c;
}
for (int i = 0; i < 256; i++) {
const auto c = static_cast<unsigned char>(i);
if (i >= 'A' && i <= 'Z')
EXPECT_TRUE(absl::ascii_isupper(i)) << ": failed on " << i;
EXPECT_TRUE(absl::ascii_isupper(c)) << ": failed on " << c;
else
EXPECT_TRUE(!absl::ascii_isupper(i)) << ": failed on " << i;
EXPECT_TRUE(!absl::ascii_isupper(c)) << ": failed on " << c;
}
for (int i = 0; i < 256; i++) {
const auto c = static_cast<unsigned char>(i);
if (i >= 'a' && i <= 'z')
EXPECT_TRUE(absl::ascii_islower(i)) << ": failed on " << i;
EXPECT_TRUE(absl::ascii_islower(c)) << ": failed on " << c;
else
EXPECT_TRUE(!absl::ascii_islower(i)) << ": failed on " << i;
EXPECT_TRUE(!absl::ascii_islower(c)) << ": failed on " << c;
}
for (int i = 0; i < 128; i++) {
EXPECT_TRUE(absl::ascii_isascii(i)) << ": failed on " << i;
for (unsigned char c = 0; c < 128; c++) {
EXPECT_TRUE(absl::ascii_isascii(c)) << ": failed on " << c;
}
for (int i = 128; i < 256; i++) {
EXPECT_TRUE(!absl::ascii_isascii(i)) << ": failed on " << i;
}
// The official is* functions don't accept negative signed chars, but
// our absl::ascii_is* functions do.
for (int i = 0; i < 256; i++) {
signed char sc = static_cast<signed char>(static_cast<unsigned char>(i));
EXPECT_EQ(absl::ascii_isalpha(i), absl::ascii_isalpha(sc)) << i;
EXPECT_EQ(absl::ascii_isdigit(i), absl::ascii_isdigit(sc)) << i;
EXPECT_EQ(absl::ascii_isalnum(i), absl::ascii_isalnum(sc)) << i;
EXPECT_EQ(absl::ascii_isspace(i), absl::ascii_isspace(sc)) << i;
EXPECT_EQ(absl::ascii_ispunct(i), absl::ascii_ispunct(sc)) << i;
EXPECT_EQ(absl::ascii_isblank(i), absl::ascii_isblank(sc)) << i;
EXPECT_EQ(absl::ascii_iscntrl(i), absl::ascii_iscntrl(sc)) << i;
EXPECT_EQ(absl::ascii_isxdigit(i), absl::ascii_isxdigit(sc)) << i;
EXPECT_EQ(absl::ascii_isprint(i), absl::ascii_isprint(sc)) << i;
EXPECT_EQ(absl::ascii_isgraph(i), absl::ascii_isgraph(sc)) << i;
EXPECT_EQ(absl::ascii_isupper(i), absl::ascii_isupper(sc)) << i;
EXPECT_EQ(absl::ascii_islower(i), absl::ascii_islower(sc)) << i;
EXPECT_EQ(absl::ascii_isascii(i), absl::ascii_isascii(sc)) << i;
const auto c = static_cast<unsigned char>(i);
EXPECT_TRUE(!absl::ascii_isascii(c)) << ": failed on " << c;
}
}
@ -137,19 +133,20 @@ TEST(AsciiIsFoo, SameAsIsFoo) {
#endif
for (int i = 0; i < 256; i++) {
EXPECT_EQ(isalpha(i) != 0, absl::ascii_isalpha(i)) << i;
EXPECT_EQ(isdigit(i) != 0, absl::ascii_isdigit(i)) << i;
EXPECT_EQ(isalnum(i) != 0, absl::ascii_isalnum(i)) << i;
EXPECT_EQ(isspace(i) != 0, absl::ascii_isspace(i)) << i;
EXPECT_EQ(ispunct(i) != 0, absl::ascii_ispunct(i)) << i;
EXPECT_EQ(isblank(i) != 0, absl::ascii_isblank(i)) << i;
EXPECT_EQ(iscntrl(i) != 0, absl::ascii_iscntrl(i)) << i;
EXPECT_EQ(isxdigit(i) != 0, absl::ascii_isxdigit(i)) << i;
EXPECT_EQ(isprint(i) != 0, absl::ascii_isprint(i)) << i;
EXPECT_EQ(isgraph(i) != 0, absl::ascii_isgraph(i)) << i;
EXPECT_EQ(isupper(i) != 0, absl::ascii_isupper(i)) << i;
EXPECT_EQ(islower(i) != 0, absl::ascii_islower(i)) << i;
EXPECT_EQ(isascii(i) != 0, absl::ascii_isascii(i)) << i;
const auto c = static_cast<unsigned char>(i);
EXPECT_EQ(isalpha(c) != 0, absl::ascii_isalpha(c)) << c;
EXPECT_EQ(isdigit(c) != 0, absl::ascii_isdigit(c)) << c;
EXPECT_EQ(isalnum(c) != 0, absl::ascii_isalnum(c)) << c;
EXPECT_EQ(isspace(c) != 0, absl::ascii_isspace(c)) << c;
EXPECT_EQ(ispunct(c) != 0, absl::ascii_ispunct(c)) << c;
EXPECT_EQ(isblank(c) != 0, absl::ascii_isblank(c)) << c;
EXPECT_EQ(iscntrl(c) != 0, absl::ascii_iscntrl(c)) << c;
EXPECT_EQ(isxdigit(c) != 0, absl::ascii_isxdigit(c)) << c;
EXPECT_EQ(isprint(c) != 0, absl::ascii_isprint(c)) << c;
EXPECT_EQ(isgraph(c) != 0, absl::ascii_isgraph(c)) << c;
EXPECT_EQ(isupper(c) != 0, absl::ascii_isupper(c)) << c;
EXPECT_EQ(islower(c) != 0, absl::ascii_islower(c)) << c;
EXPECT_EQ(isascii(c) != 0, absl::ascii_isascii(c)) << c;
}
#ifndef __ANDROID__
@ -166,25 +163,20 @@ TEST(AsciiToFoo, All) {
#endif
for (int i = 0; i < 256; i++) {
if (absl::ascii_islower(i))
EXPECT_EQ(absl::ascii_toupper(i), 'A' + (i - 'a')) << i;
const auto c = static_cast<unsigned char>(i);
if (absl::ascii_islower(c))
EXPECT_EQ(absl::ascii_toupper(c), 'A' + (i - 'a')) << c;
else
EXPECT_EQ(absl::ascii_toupper(i), static_cast<char>(i)) << i;
EXPECT_EQ(absl::ascii_toupper(c), static_cast<char>(i)) << c;
if (absl::ascii_isupper(i))
EXPECT_EQ(absl::ascii_tolower(i), 'a' + (i - 'A')) << i;
if (absl::ascii_isupper(c))
EXPECT_EQ(absl::ascii_tolower(c), 'a' + (i - 'A')) << c;
else
EXPECT_EQ(absl::ascii_tolower(i), static_cast<char>(i)) << i;
EXPECT_EQ(absl::ascii_tolower(c), static_cast<char>(i)) << c;
// These CHECKs only hold in a C locale.
EXPECT_EQ(static_cast<char>(tolower(i)), absl::ascii_tolower(i)) << i;
EXPECT_EQ(static_cast<char>(toupper(i)), absl::ascii_toupper(i)) << i;
// The official to* functions don't accept negative signed chars, but
// our absl::ascii_to* functions do.
signed char sc = static_cast<signed char>(static_cast<unsigned char>(i));
EXPECT_EQ(absl::ascii_tolower(i), absl::ascii_tolower(sc)) << i;
EXPECT_EQ(absl::ascii_toupper(i), absl::ascii_toupper(sc)) << i;
EXPECT_EQ(static_cast<char>(tolower(i)), absl::ascii_tolower(c)) << c;
EXPECT_EQ(static_cast<char>(toupper(i)), absl::ascii_toupper(c)) << c;
}
#ifndef __ANDROID__
// restore the old locale.

@ -82,7 +82,7 @@ TEST(StringViewTest, Ctor) {
// Null.
absl::string_view s10;
EXPECT_TRUE(s10.data() == nullptr);
EXPECT_EQ(0, s10.length());
EXPECT_EQ(0u, s10.length());
}
{
@ -90,17 +90,17 @@ TEST(StringViewTest, Ctor) {
const char* hello = "hello";
absl::string_view s20(hello);
EXPECT_TRUE(s20.data() == hello);
EXPECT_EQ(5, s20.length());
EXPECT_EQ(5u, s20.length());
// const char* with length.
absl::string_view s21(hello, 4);
EXPECT_TRUE(s21.data() == hello);
EXPECT_EQ(4, s21.length());
EXPECT_EQ(4u, s21.length());
// Not recommended, but valid C++
absl::string_view s22(hello, 6);
EXPECT_TRUE(s22.data() == hello);
EXPECT_EQ(6, s22.length());
EXPECT_EQ(6u, s22.length());
}
{
@ -108,7 +108,7 @@ TEST(StringViewTest, Ctor) {
std::string hola = "hola";
absl::string_view s30(hola);
EXPECT_TRUE(s30.data() == hola.data());
EXPECT_EQ(4, s30.length());
EXPECT_EQ(4u, s30.length());
// std::string with embedded '\0'.
hola.push_back('\0');
@ -116,7 +116,7 @@ TEST(StringViewTest, Ctor) {
hola.push_back('\0');
absl::string_view s31(hola);
EXPECT_TRUE(s31.data() == hola.data());
EXPECT_EQ(8, s31.length());
EXPECT_EQ(8u, s31.length());
}
{
@ -165,7 +165,7 @@ TEST(StringViewTest, STLComparator) {
map.insert(std::make_pair(p1, 0));
map.insert(std::make_pair(p2, 1));
map.insert(std::make_pair(p3, 2));
EXPECT_EQ(map.size(), 3);
EXPECT_EQ(map.size(), 3u);
TestMap::const_iterator iter = map.begin();
EXPECT_EQ(iter->second, 1);
@ -183,7 +183,7 @@ TEST(StringViewTest, STLComparator) {
EXPECT_TRUE(new_iter != map.end());
map.erase(new_iter);
EXPECT_EQ(map.size(), 2);
EXPECT_EQ(map.size(), 2u);
iter = map.begin();
EXPECT_EQ(iter->second, 2);
@ -261,11 +261,11 @@ TEST(StringViewTest, ComparisonOperators) {
TEST(StringViewTest, ComparisonOperatorsByCharacterPosition) {
std::string x;
for (int i = 0; i < 256; i++) {
for (size_t i = 0; i < 256; i++) {
x += 'a';
std::string y = x;
COMPARE(true, ==, x, y);
for (int j = 0; j < i; j++) {
for (size_t j = 0; j < i; j++) {
std::string z = x;
z[j] = 'b'; // Differs in position 'j'
COMPARE(false, ==, x, z);
@ -341,12 +341,12 @@ TEST(StringViewTest, STL1) {
EXPECT_EQ(*(c.rend() - 1), 'x');
EXPECT_TRUE(a.rbegin() + 26 == a.rend());
EXPECT_EQ(a.size(), 26);
EXPECT_EQ(b.size(), 3);
EXPECT_EQ(c.size(), 3);
EXPECT_EQ(d.size(), 6);
EXPECT_EQ(e.size(), 0);
EXPECT_EQ(f.size(), 7);
EXPECT_EQ(a.size(), 26u);
EXPECT_EQ(b.size(), 3u);
EXPECT_EQ(c.size(), 3u);
EXPECT_EQ(d.size(), 6u);
EXPECT_EQ(e.size(), 0u);
EXPECT_EQ(f.size(), 7u);
EXPECT_TRUE(!d.empty());
EXPECT_TRUE(d.begin() != d.end());
@ -356,17 +356,17 @@ TEST(StringViewTest, STL1) {
EXPECT_TRUE(e.begin() == e.end());
char buf[4] = { '%', '%', '%', '%' };
EXPECT_EQ(a.copy(buf, 4), 4);
EXPECT_EQ(a.copy(buf, 4), 4u);
EXPECT_EQ(buf[0], a[0]);
EXPECT_EQ(buf[1], a[1]);
EXPECT_EQ(buf[2], a[2]);
EXPECT_EQ(buf[3], a[3]);
EXPECT_EQ(a.copy(buf, 3, 7), 3);
EXPECT_EQ(a.copy(buf, 3, 7), 3u);
EXPECT_EQ(buf[0], a[7]);
EXPECT_EQ(buf[1], a[8]);
EXPECT_EQ(buf[2], a[9]);
EXPECT_EQ(buf[3], a[3]);
EXPECT_EQ(c.copy(buf, 99), 3);
EXPECT_EQ(c.copy(buf, 99), 3u);
EXPECT_EQ(buf[0], c[0]);
EXPECT_EQ(buf[1], c[1]);
EXPECT_EQ(buf[2], c[2]);
@ -393,22 +393,22 @@ TEST(StringViewTest, STL2) {
7);
d = absl::string_view();
EXPECT_EQ(d.size(), 0);
EXPECT_EQ(d.size(), 0u);
EXPECT_TRUE(d.empty());
EXPECT_TRUE(d.data() == nullptr);
EXPECT_TRUE(d.begin() == d.end());
EXPECT_EQ(a.find(b), 0);
EXPECT_EQ(a.find(b), 0u);
EXPECT_EQ(a.find(b, 1), absl::string_view::npos);
EXPECT_EQ(a.find(c), 23);
EXPECT_EQ(a.find(c, 9), 23);
EXPECT_EQ(a.find(c), 23u);
EXPECT_EQ(a.find(c, 9), 23u);
EXPECT_EQ(a.find(c, absl::string_view::npos), absl::string_view::npos);
EXPECT_EQ(b.find(c), absl::string_view::npos);
EXPECT_EQ(b.find(c, absl::string_view::npos), absl::string_view::npos);
EXPECT_EQ(a.find(d), 0);
EXPECT_EQ(a.find(e), 0);
EXPECT_EQ(a.find(d, 12), 12);
EXPECT_EQ(a.find(e, 17), 17);
EXPECT_EQ(a.find(d), 0u);
EXPECT_EQ(a.find(e), 0u);
EXPECT_EQ(a.find(d, 12), 12u);
EXPECT_EQ(a.find(e, 17), 17u);
absl::string_view g("xx not found bb");
EXPECT_EQ(a.find(g), absl::string_view::npos);
// empty string nonsense
@ -427,17 +427,17 @@ TEST(StringViewTest, STL2) {
EXPECT_EQ(e.find(d, 4), std::string().find(std::string(), 4));
EXPECT_EQ(e.find(e, 4), std::string().find(std::string(), 4));
EXPECT_EQ(a.find('a'), 0);
EXPECT_EQ(a.find('c'), 2);
EXPECT_EQ(a.find('z'), 25);
EXPECT_EQ(a.find('a'), 0u);
EXPECT_EQ(a.find('c'), 2u);
EXPECT_EQ(a.find('z'), 25u);
EXPECT_EQ(a.find('$'), absl::string_view::npos);
EXPECT_EQ(a.find('\0'), absl::string_view::npos);
EXPECT_EQ(f.find('\0'), 3);
EXPECT_EQ(f.find('3'), 2);
EXPECT_EQ(f.find('5'), 5);
EXPECT_EQ(g.find('o'), 4);
EXPECT_EQ(g.find('o', 4), 4);
EXPECT_EQ(g.find('o', 5), 8);
EXPECT_EQ(f.find('\0'), 3u);
EXPECT_EQ(f.find('3'), 2u);
EXPECT_EQ(f.find('5'), 5u);
EXPECT_EQ(g.find('o'), 4u);
EXPECT_EQ(g.find('o', 4), 4u);
EXPECT_EQ(g.find('o', 5), 8u);
EXPECT_EQ(a.find('b', 5), absl::string_view::npos);
// empty string nonsense
EXPECT_EQ(d.find('\0'), absl::string_view::npos);
@ -449,8 +449,8 @@ TEST(StringViewTest, STL2) {
EXPECT_EQ(d.find('x', 4), absl::string_view::npos);
EXPECT_EQ(e.find('x', 7), absl::string_view::npos);
EXPECT_EQ(a.find(b.data(), 1, 0), 1);
EXPECT_EQ(a.find(c.data(), 9, 0), 9);
EXPECT_EQ(a.find(b.data(), 1, 0), 1u);
EXPECT_EQ(a.find(c.data(), 9, 0), 9u);
EXPECT_EQ(a.find(c.data(), absl::string_view::npos, 0),
absl::string_view::npos);
EXPECT_EQ(b.find(c.data(), absl::string_view::npos, 0),
@ -460,16 +460,16 @@ TEST(StringViewTest, STL2) {
EXPECT_EQ(e.find(b.data(), 7, 0), absl::string_view::npos);
EXPECT_EQ(a.find(b.data(), 1), absl::string_view::npos);
EXPECT_EQ(a.find(c.data(), 9), 23);
EXPECT_EQ(a.find(c.data(), 9), 23u);
EXPECT_EQ(a.find(c.data(), absl::string_view::npos), absl::string_view::npos);
EXPECT_EQ(b.find(c.data(), absl::string_view::npos), absl::string_view::npos);
// empty string nonsense
EXPECT_EQ(d.find(b.data(), 4), absl::string_view::npos);
EXPECT_EQ(e.find(b.data(), 7), absl::string_view::npos);
EXPECT_EQ(a.rfind(b), 0);
EXPECT_EQ(a.rfind(b, 1), 0);
EXPECT_EQ(a.rfind(c), 23);
EXPECT_EQ(a.rfind(b), 0u);
EXPECT_EQ(a.rfind(b, 1), 0u);
EXPECT_EQ(a.rfind(c), 23u);
EXPECT_EQ(a.rfind(c, 22), absl::string_view::npos);
EXPECT_EQ(a.rfind(c, 1), absl::string_view::npos);
EXPECT_EQ(a.rfind(c, 0), absl::string_view::npos);
@ -477,8 +477,8 @@ TEST(StringViewTest, STL2) {
EXPECT_EQ(b.rfind(c, 0), absl::string_view::npos);
EXPECT_EQ(a.rfind(d), std::string(a).rfind(std::string()));
EXPECT_EQ(a.rfind(e), std::string(a).rfind(std::string()));
EXPECT_EQ(a.rfind(d, 12), 12);
EXPECT_EQ(a.rfind(e, 17), 17);
EXPECT_EQ(a.rfind(d, 12), 12u);
EXPECT_EQ(a.rfind(e, 17), 17u);
EXPECT_EQ(a.rfind(g), absl::string_view::npos);
EXPECT_EQ(d.rfind(b), absl::string_view::npos);
EXPECT_EQ(e.rfind(b), absl::string_view::npos);
@ -494,28 +494,28 @@ TEST(StringViewTest, STL2) {
EXPECT_EQ(d.rfind(e), std::string().rfind(std::string()));
EXPECT_EQ(e.rfind(e), std::string().rfind(std::string()));
EXPECT_EQ(g.rfind('o'), 8);
EXPECT_EQ(g.rfind('o'), 8u);
EXPECT_EQ(g.rfind('q'), absl::string_view::npos);
EXPECT_EQ(g.rfind('o', 8), 8);
EXPECT_EQ(g.rfind('o', 7), 4);
EXPECT_EQ(g.rfind('o', 8), 8u);
EXPECT_EQ(g.rfind('o', 7), 4u);
EXPECT_EQ(g.rfind('o', 3), absl::string_view::npos);
EXPECT_EQ(f.rfind('\0'), 3);
EXPECT_EQ(f.rfind('\0', 12), 3);
EXPECT_EQ(f.rfind('3'), 2);
EXPECT_EQ(f.rfind('5'), 5);
EXPECT_EQ(f.rfind('\0'), 3u);
EXPECT_EQ(f.rfind('\0', 12), 3u);
EXPECT_EQ(f.rfind('3'), 2u);
EXPECT_EQ(f.rfind('5'), 5u);
// empty string nonsense
EXPECT_EQ(d.rfind('o'), absl::string_view::npos);
EXPECT_EQ(e.rfind('o'), absl::string_view::npos);
EXPECT_EQ(d.rfind('o', 4), absl::string_view::npos);
EXPECT_EQ(e.rfind('o', 7), absl::string_view::npos);
EXPECT_EQ(a.rfind(b.data(), 1, 0), 1);
EXPECT_EQ(a.rfind(c.data(), 22, 0), 22);
EXPECT_EQ(a.rfind(c.data(), 1, 0), 1);
EXPECT_EQ(a.rfind(c.data(), 0, 0), 0);
EXPECT_EQ(b.rfind(c.data(), 0, 0), 0);
EXPECT_EQ(d.rfind(b.data(), 4, 0), 0);
EXPECT_EQ(e.rfind(b.data(), 7, 0), 0);
EXPECT_EQ(a.rfind(b.data(), 1, 0), 1u);
EXPECT_EQ(a.rfind(c.data(), 22, 0), 22u);
EXPECT_EQ(a.rfind(c.data(), 1, 0), 1u);
EXPECT_EQ(a.rfind(c.data(), 0, 0), 0u);
EXPECT_EQ(b.rfind(c.data(), 0, 0), 0u);
EXPECT_EQ(d.rfind(b.data(), 4, 0), 0u);
EXPECT_EQ(e.rfind(b.data(), 7, 0), 0u);
}
// Continued from STL2
@ -533,18 +533,18 @@ TEST(StringViewTest, STL2FindFirst) {
absl::string_view g("xx not found bb");
d = absl::string_view();
EXPECT_EQ(a.find_first_of(b), 0);
EXPECT_EQ(a.find_first_of(b, 0), 0);
EXPECT_EQ(a.find_first_of(b, 1), 1);
EXPECT_EQ(a.find_first_of(b, 2), 2);
EXPECT_EQ(a.find_first_of(b), 0u);
EXPECT_EQ(a.find_first_of(b, 0), 0u);
EXPECT_EQ(a.find_first_of(b, 1), 1u);
EXPECT_EQ(a.find_first_of(b, 2), 2u);
EXPECT_EQ(a.find_first_of(b, 3), absl::string_view::npos);
EXPECT_EQ(a.find_first_of(c), 23);
EXPECT_EQ(a.find_first_of(c, 23), 23);
EXPECT_EQ(a.find_first_of(c, 24), 24);
EXPECT_EQ(a.find_first_of(c, 25), 25);
EXPECT_EQ(a.find_first_of(c), 23u);
EXPECT_EQ(a.find_first_of(c, 23), 23u);
EXPECT_EQ(a.find_first_of(c, 24), 24u);
EXPECT_EQ(a.find_first_of(c, 25), 25u);
EXPECT_EQ(a.find_first_of(c, 26), absl::string_view::npos);
EXPECT_EQ(g.find_first_of(b), 13);
EXPECT_EQ(g.find_first_of(c), 0);
EXPECT_EQ(g.find_first_of(b), 13u);
EXPECT_EQ(g.find_first_of(c), 0u);
EXPECT_EQ(a.find_first_of(f), absl::string_view::npos);
EXPECT_EQ(f.find_first_of(a), absl::string_view::npos);
// empty string nonsense
@ -557,19 +557,19 @@ TEST(StringViewTest, STL2FindFirst) {
EXPECT_EQ(d.find_first_of(e), absl::string_view::npos);
EXPECT_EQ(e.find_first_of(e), absl::string_view::npos);
EXPECT_EQ(a.find_first_not_of(b), 3);
EXPECT_EQ(a.find_first_not_of(c), 0);
EXPECT_EQ(a.find_first_not_of(b), 3u);
EXPECT_EQ(a.find_first_not_of(c), 0u);
EXPECT_EQ(b.find_first_not_of(a), absl::string_view::npos);
EXPECT_EQ(c.find_first_not_of(a), absl::string_view::npos);
EXPECT_EQ(f.find_first_not_of(a), 0);
EXPECT_EQ(a.find_first_not_of(f), 0);
EXPECT_EQ(a.find_first_not_of(d), 0);
EXPECT_EQ(a.find_first_not_of(e), 0);
EXPECT_EQ(f.find_first_not_of(a), 0u);
EXPECT_EQ(a.find_first_not_of(f), 0u);
EXPECT_EQ(a.find_first_not_of(d), 0u);
EXPECT_EQ(a.find_first_not_of(e), 0u);
// empty string nonsense
EXPECT_EQ(a.find_first_not_of(d), 0);
EXPECT_EQ(a.find_first_not_of(e), 0);
EXPECT_EQ(a.find_first_not_of(d, 1), 1);
EXPECT_EQ(a.find_first_not_of(e, 1), 1);
EXPECT_EQ(a.find_first_not_of(d), 0u);
EXPECT_EQ(a.find_first_not_of(e), 0u);
EXPECT_EQ(a.find_first_not_of(d, 1), 1u);
EXPECT_EQ(a.find_first_not_of(e, 1), 1u);
EXPECT_EQ(a.find_first_not_of(d, a.size() - 1), a.size() - 1);
EXPECT_EQ(a.find_first_not_of(e, a.size() - 1), a.size() - 1);
EXPECT_EQ(a.find_first_not_of(d, a.size()), absl::string_view::npos);
@ -588,11 +588,11 @@ TEST(StringViewTest, STL2FindFirst) {
absl::string_view h("====");
EXPECT_EQ(h.find_first_not_of('='), absl::string_view::npos);
EXPECT_EQ(h.find_first_not_of('=', 3), absl::string_view::npos);
EXPECT_EQ(h.find_first_not_of('\0'), 0);
EXPECT_EQ(g.find_first_not_of('x'), 2);
EXPECT_EQ(f.find_first_not_of('\0'), 0);
EXPECT_EQ(f.find_first_not_of('\0', 3), 4);
EXPECT_EQ(f.find_first_not_of('\0', 2), 2);
EXPECT_EQ(h.find_first_not_of('\0'), 0u);
EXPECT_EQ(g.find_first_not_of('x'), 2u);
EXPECT_EQ(f.find_first_not_of('\0'), 0u);
EXPECT_EQ(f.find_first_not_of('\0', 3), 4u);
EXPECT_EQ(f.find_first_not_of('\0', 2), 2u);
// empty string nonsense
EXPECT_EQ(d.find_first_not_of('x'), absl::string_view::npos);
EXPECT_EQ(e.find_first_not_of('x'), absl::string_view::npos);
@ -619,19 +619,19 @@ TEST(StringViewTest, STL2FindLast) {
d = absl::string_view();
EXPECT_EQ(h.find_last_of(a), absl::string_view::npos);
EXPECT_EQ(g.find_last_of(a), g.size() - 1);
EXPECT_EQ(a.find_last_of(b), 2);
EXPECT_EQ(a.find_last_of(b), 2u);
EXPECT_EQ(a.find_last_of(c), a.size() - 1);
EXPECT_EQ(f.find_last_of(i), 6);
EXPECT_EQ(a.find_last_of('a'), 0);
EXPECT_EQ(a.find_last_of('b'), 1);
EXPECT_EQ(a.find_last_of('z'), 25);
EXPECT_EQ(a.find_last_of('a', 5), 0);
EXPECT_EQ(a.find_last_of('b', 5), 1);
EXPECT_EQ(f.find_last_of(i), 6u);
EXPECT_EQ(a.find_last_of('a'), 0u);
EXPECT_EQ(a.find_last_of('b'), 1u);
EXPECT_EQ(a.find_last_of('z'), 25u);
EXPECT_EQ(a.find_last_of('a', 5), 0u);
EXPECT_EQ(a.find_last_of('b', 5), 1u);
EXPECT_EQ(a.find_last_of('b', 0), absl::string_view::npos);
EXPECT_EQ(a.find_last_of('z', 25), 25);
EXPECT_EQ(a.find_last_of('z', 25), 25u);
EXPECT_EQ(a.find_last_of('z', 24), absl::string_view::npos);
EXPECT_EQ(f.find_last_of(i, 5), 5);
EXPECT_EQ(f.find_last_of(i, 6), 6);
EXPECT_EQ(f.find_last_of(i, 5), 5u);
EXPECT_EQ(f.find_last_of(i, 6), 6u);
EXPECT_EQ(f.find_last_of(a, 4), absl::string_view::npos);
// empty string nonsense
EXPECT_EQ(f.find_last_of(d), absl::string_view::npos);
@ -652,18 +652,18 @@ TEST(StringViewTest, STL2FindLast) {
EXPECT_EQ(e.find_last_of(f, 4), absl::string_view::npos);
EXPECT_EQ(a.find_last_not_of(b), a.size() - 1);
EXPECT_EQ(a.find_last_not_of(c), 22);
EXPECT_EQ(a.find_last_not_of(c), 22u);
EXPECT_EQ(b.find_last_not_of(a), absl::string_view::npos);
EXPECT_EQ(b.find_last_not_of(b), absl::string_view::npos);
EXPECT_EQ(f.find_last_not_of(i), 4);
EXPECT_EQ(a.find_last_not_of(c, 24), 22);
EXPECT_EQ(a.find_last_not_of(b, 3), 3);
EXPECT_EQ(f.find_last_not_of(i), 4u);
EXPECT_EQ(a.find_last_not_of(c, 24), 22u);
EXPECT_EQ(a.find_last_not_of(b, 3), 3u);
EXPECT_EQ(a.find_last_not_of(b, 2), absl::string_view::npos);
// empty string nonsense
EXPECT_EQ(f.find_last_not_of(d), f.size() - 1);
EXPECT_EQ(f.find_last_not_of(e), f.size() - 1);
EXPECT_EQ(f.find_last_not_of(d, 4), 4);
EXPECT_EQ(f.find_last_not_of(e, 4), 4);
EXPECT_EQ(f.find_last_not_of(d, 4), 4u);
EXPECT_EQ(f.find_last_not_of(e, 4), 4u);
EXPECT_EQ(d.find_last_not_of(d), absl::string_view::npos);
EXPECT_EQ(d.find_last_not_of(e), absl::string_view::npos);
EXPECT_EQ(e.find_last_not_of(d), absl::string_view::npos);
@ -679,10 +679,10 @@ TEST(StringViewTest, STL2FindLast) {
EXPECT_EQ(h.find_last_not_of('x'), h.size() - 1);
EXPECT_EQ(h.find_last_not_of('='), absl::string_view::npos);
EXPECT_EQ(b.find_last_not_of('c'), 1);
EXPECT_EQ(h.find_last_not_of('x', 2), 2);
EXPECT_EQ(b.find_last_not_of('c'), 1u);
EXPECT_EQ(h.find_last_not_of('x', 2), 2u);
EXPECT_EQ(h.find_last_not_of('=', 2), absl::string_view::npos);
EXPECT_EQ(b.find_last_not_of('b', 1), 0);
EXPECT_EQ(b.find_last_not_of('b', 1), 0u);
// empty string nonsense
EXPECT_EQ(d.find_last_not_of('x'), absl::string_view::npos);
EXPECT_EQ(e.find_last_not_of('x'), absl::string_view::npos);
@ -734,7 +734,7 @@ TEST(StringViewTest, TruncSubstr) {
TEST(StringViewTest, UTF8) {
std::string utf8 = "\u00E1";
std::string utf8_twice = utf8 + " " + utf8;
int utf8_len = strlen(utf8.data());
size_t utf8_len = strlen(utf8.data());
EXPECT_EQ(utf8_len, absl::string_view(utf8_twice).find_first_of(" "));
EXPECT_EQ(utf8_len, absl::string_view(utf8_twice).find_first_of(" \t"));
}
@ -879,12 +879,12 @@ TEST(StringViewTest, FrontBackEmpty) {
TEST(StringViewTest, NULLInput) {
absl::string_view s;
EXPECT_EQ(s.data(), nullptr);
EXPECT_EQ(s.size(), 0);
EXPECT_EQ(s.size(), 0u);
#ifdef ABSL_HAVE_STRING_VIEW_FROM_NULLPTR
s = absl::string_view(nullptr);
EXPECT_EQ(s.data(), nullptr);
EXPECT_EQ(s.size(), 0);
EXPECT_EQ(s.size(), 0u);
// .ToString() on a absl::string_view with nullptr should produce the empty
// string.
@ -959,7 +959,7 @@ TEST(StringViewTest, NullSafeStringView) {
{
absl::string_view s = absl::NullSafeStringView(nullptr);
EXPECT_EQ(nullptr, s.data());
EXPECT_EQ(0, s.size());
EXPECT_EQ(0u, s.size());
EXPECT_EQ(absl::string_view(), s);
}
{
@ -975,7 +975,7 @@ TEST(StringViewTest, ConstexprNullSafeStringView) {
{
constexpr absl::string_view s = absl::NullSafeStringView(nullptr);
EXPECT_EQ(nullptr, s.data());
EXPECT_EQ(0, s.size());
EXPECT_EQ(0u, s.size());
EXPECT_EQ(absl::string_view(), s);
}
#if !defined(_MSC_VER) || _MSC_VER >= 1910
@ -990,7 +990,7 @@ TEST(StringViewTest, ConstexprNullSafeStringView) {
}
{
constexpr absl::string_view s = absl::NullSafeStringView("hello");
EXPECT_EQ(s.size(), 5);
EXPECT_EQ(s.size(), 5u);
EXPECT_EQ("hello", s);
}
#endif
@ -1036,7 +1036,7 @@ TEST(StringViewTest, ConstexprCompiles) {
#ifdef ABSL_HAVE_CONSTEXPR_STRING_VIEW_FROM_CSTR
constexpr absl::string_view cstr_strlen("foo");
EXPECT_EQ(cstr_strlen.length(), 3);
EXPECT_EQ(cstr_strlen.length(), 3u);
constexpr absl::string_view cstr_strlen2 = "bar";
EXPECT_EQ(cstr_strlen2, "bar");
@ -1111,7 +1111,7 @@ TEST(StringViewTest, ConstexprCompiles) {
EXPECT_NE(cstr_ptr, nullptr);
constexpr size_t sp_npos = sp.npos;
EXPECT_EQ(sp_npos, -1);
EXPECT_EQ(sp_npos, static_cast<size_t>(-1));
}
constexpr char ConstexprMethodsHelper() {
@ -1179,7 +1179,7 @@ TEST(StringViewTest, BoundsCheck) {
// Abseil's string_view implementation has bounds-checking in debug mode.
absl::string_view h = "hello";
ABSL_EXPECT_DEATH_IF_SUPPORTED(h[5], "");
ABSL_EXPECT_DEATH_IF_SUPPORTED(h[-1], "");
ABSL_EXPECT_DEATH_IF_SUPPORTED(h[static_cast<size_t>(-1)], "");
#endif
#endif
}
@ -1201,17 +1201,17 @@ TEST(FindOneCharTest, EdgeCases) {
a.remove_prefix(1);
a.remove_suffix(1);
EXPECT_EQ(0, a.find('x'));
EXPECT_EQ(0, a.find('x', 0));
EXPECT_EQ(4, a.find('x', 1));
EXPECT_EQ(4, a.find('x', 4));
EXPECT_EQ(0u, a.find('x'));
EXPECT_EQ(0u, a.find('x', 0));
EXPECT_EQ(4u, a.find('x', 1));
EXPECT_EQ(4u, a.find('x', 4));
EXPECT_EQ(absl::string_view::npos, a.find('x', 5));
EXPECT_EQ(4, a.rfind('x'));
EXPECT_EQ(4, a.rfind('x', 5));
EXPECT_EQ(4, a.rfind('x', 4));
EXPECT_EQ(0, a.rfind('x', 3));
EXPECT_EQ(0, a.rfind('x', 0));
EXPECT_EQ(4u, a.rfind('x'));
EXPECT_EQ(4u, a.rfind('x', 5));
EXPECT_EQ(4u, a.rfind('x', 4));
EXPECT_EQ(0u, a.rfind('x', 3));
EXPECT_EQ(0u, a.rfind('x', 0));
// Set a = "yyy".
a.remove_prefix(1);
@ -1239,8 +1239,8 @@ TEST(HugeStringView, TwoPointTwoGB) {
#if !defined(NDEBUG) && !defined(ABSL_USES_STD_STRING_VIEW)
TEST(NonNegativeLenTest, NonNegativeLen) {
ABSL_EXPECT_DEATH_IF_SUPPORTED(absl::string_view("xyz", -1),
"len <= kMaxSize");
ABSL_EXPECT_DEATH_IF_SUPPORTED(
absl::string_view("xyz", static_cast<size_t>(-1)), "len <= kMaxSize");
}
TEST(LenExceedsMaxSizeTest, LenExceedsMaxSize) {

@ -988,8 +988,8 @@ TEST(optionalTest, PointerStuff) {
EXPECT_EQ("foo", *opt);
const auto& opt_const = opt;
EXPECT_EQ("foo", *opt_const);
EXPECT_EQ(opt->size(), 3);
EXPECT_EQ(opt_const->size(), 3);
EXPECT_EQ(opt->size(), 3u);
EXPECT_EQ(opt_const->size(), 3u);
constexpr absl::optional<ConstexprType> opt1(1);
static_assert((*opt1).x == ConstexprType::kCtorInt, "");
@ -1523,7 +1523,7 @@ TEST(optionalTest, Hash) {
for (int i = 0; i < 100; ++i) {
hashcodes.insert(hash(i));
}
EXPECT_GT(hashcodes.size(), 90);
EXPECT_GT(hashcodes.size(), 90u);
static_assert(is_hash_enabled_for<absl::optional<int>>::value, "");
static_assert(is_hash_enabled_for<absl::optional<Hashable>>::value, "");

@ -281,7 +281,7 @@ TEST(VariantTest, TestDefaultConstructor) {
using X = variant<int>;
constexpr variant<int> x{};
ASSERT_FALSE(x.valueless_by_exception());
ASSERT_EQ(0, x.index());
ASSERT_EQ(0u, x.index());
EXPECT_EQ(0, absl::get<0>(x));
EXPECT_TRUE(std::is_nothrow_default_constructible<X>::value);
}
@ -290,7 +290,7 @@ TEST(VariantTest, TestDefaultConstructor) {
using X = variant<NonNoexceptDefaultConstructible>;
X x{};
ASSERT_FALSE(x.valueless_by_exception());
ASSERT_EQ(0, x.index());
ASSERT_EQ(0u, x.index());
EXPECT_EQ(5, absl::get<0>(x).value);
EXPECT_FALSE(std::is_nothrow_default_constructible<X>::value);
}
@ -299,7 +299,7 @@ TEST(VariantTest, TestDefaultConstructor) {
using X = variant<int, NonNoexceptDefaultConstructible>;
X x{};
ASSERT_FALSE(x.valueless_by_exception());
ASSERT_EQ(0, x.index());
ASSERT_EQ(0u, x.index());
EXPECT_EQ(0, absl::get<0>(x));
EXPECT_TRUE(std::is_nothrow_default_constructible<X>::value);
}
@ -308,7 +308,7 @@ TEST(VariantTest, TestDefaultConstructor) {
using X = variant<NonNoexceptDefaultConstructible, int>;
X x{};
ASSERT_FALSE(x.valueless_by_exception());
ASSERT_EQ(0, x.index());
ASSERT_EQ(0u, x.index());
EXPECT_EQ(5, absl::get<0>(x).value);
EXPECT_FALSE(std::is_nothrow_default_constructible<X>::value);
}
@ -480,7 +480,7 @@ TEST(VariantTest, InPlaceType) {
ASSERT_TRUE(absl::holds_alternative<std::string>(v2));
EXPECT_EQ("ABC", absl::get<std::string>(v2));
Var v3(in_place_type_t<std::string>(), "ABC", 2);
Var v3(in_place_type_t<std::string>(), "ABC", 2u);
ASSERT_TRUE(absl::holds_alternative<std::string>(v3));
EXPECT_EQ("AB", absl::get<std::string>(v3));
@ -503,7 +503,7 @@ TEST(VariantTest, InPlaceTypeVariableTemplate) {
ASSERT_TRUE(absl::holds_alternative<std::string>(v2));
EXPECT_EQ("ABC", absl::get<std::string>(v2));
Var v3(in_place_type<std::string>, "ABC", 2);
Var v3(in_place_type<std::string>, "ABC", 2u);
ASSERT_TRUE(absl::holds_alternative<std::string>(v3));
EXPECT_EQ("AB", absl::get<std::string>(v3));
@ -544,7 +544,7 @@ TEST(VariantTest, InPlaceIndex) {
ASSERT_TRUE(absl::holds_alternative<std::string>(v2));
EXPECT_EQ("ABC", absl::get<std::string>(v2));
Var v3(in_place_index_t<1>(), "ABC", 2);
Var v3(in_place_index_t<1>(), "ABC", 2u);
ASSERT_TRUE(absl::holds_alternative<std::string>(v3));
EXPECT_EQ("AB", absl::get<std::string>(v3));
@ -571,7 +571,7 @@ TEST(VariantTest, InPlaceIndexVariableTemplate) {
ASSERT_TRUE(absl::holds_alternative<std::string>(v2));
EXPECT_EQ("ABC", absl::get<std::string>(v2));
Var v3(in_place_index<1>, "ABC", 2);
Var v3(in_place_index<1>, "ABC", 2u);
ASSERT_TRUE(absl::holds_alternative<std::string>(v3));
EXPECT_EQ("AB", absl::get<std::string>(v3));
@ -688,11 +688,11 @@ TEST(VariantTest, TestSelfAssignment) {
EXPECT_EQ(long_str, foo);
variant<int, std::string> so = long_str;
ASSERT_EQ(1, so.index());
ASSERT_EQ(1u, so.index());
EXPECT_EQ(long_str, absl::get<1>(so));
so = *&so;
ASSERT_EQ(1, so.index());
ASSERT_EQ(1u, so.index());
EXPECT_EQ(long_str, absl::get<1>(so));
}
@ -968,16 +968,16 @@ TEST(VariantTest, Index) {
using Var = variant<int, std::string, double>;
Var v = 1;
EXPECT_EQ(0, v.index());
EXPECT_EQ(0u, v.index());
v = "str";
EXPECT_EQ(1, v.index());
EXPECT_EQ(1u, v.index());
v = 0.;
EXPECT_EQ(2, v.index());
EXPECT_EQ(2u, v.index());
Var v2 = v;
EXPECT_EQ(2, v2.index());
EXPECT_EQ(2u, v2.index());
v2.emplace<int>(3);
EXPECT_EQ(0, v2.index());
EXPECT_EQ(0u, v2.index());
}
TEST(VariantTest, NotValuelessByException) {
@ -1002,11 +1002,11 @@ TEST(VariantTest, IndexValuelessByException) {
using Var = variant<MoveCanThrow, std::string, double>;
Var v(absl::in_place_index<0>);
EXPECT_EQ(0, v.index());
EXPECT_EQ(0u, v.index());
ToValuelessByException(v);
EXPECT_EQ(absl::variant_npos, v.index());
v = "str";
EXPECT_EQ(1, v.index());
EXPECT_EQ(1u, v.index());
}
TEST(VariantTest, ValuelessByException) {
@ -1084,18 +1084,18 @@ TEST(VariantTest, MemberSwap) {
TEST(VariantTest, VariantSize) {
{
using Size1Variant = absl::variant<int>;
EXPECT_EQ(1, absl::variant_size<Size1Variant>::value);
EXPECT_EQ(1, absl::variant_size<const Size1Variant>::value);
EXPECT_EQ(1, absl::variant_size<volatile Size1Variant>::value);
EXPECT_EQ(1, absl::variant_size<const volatile Size1Variant>::value);
EXPECT_EQ(1u, absl::variant_size<Size1Variant>::value);
EXPECT_EQ(1u, absl::variant_size<const Size1Variant>::value);
EXPECT_EQ(1u, absl::variant_size<volatile Size1Variant>::value);
EXPECT_EQ(1u, absl::variant_size<const volatile Size1Variant>::value);
}
{
using Size3Variant = absl::variant<int, float, int>;
EXPECT_EQ(3, absl::variant_size<Size3Variant>::value);
EXPECT_EQ(3, absl::variant_size<const Size3Variant>::value);
EXPECT_EQ(3, absl::variant_size<volatile Size3Variant>::value);
EXPECT_EQ(3, absl::variant_size<const volatile Size3Variant>::value);
EXPECT_EQ(3u, absl::variant_size<Size3Variant>::value);
EXPECT_EQ(3u, absl::variant_size<const Size3Variant>::value);
EXPECT_EQ(3u, absl::variant_size<volatile Size3Variant>::value);
EXPECT_EQ(3u, absl::variant_size<const volatile Size3Variant>::value);
}
}
@ -1799,14 +1799,14 @@ TEST(VariantTest, VisitSimple) {
EXPECT_EQ("B", piece);
struct StrLen {
int operator()(const char* s) const { return strlen(s); }
int operator()(const std::string& s) const { return s.size(); }
size_t operator()(const char* s) const { return strlen(s); }
size_t operator()(const std::string& s) const { return s.size(); }
};
v = "SomeStr";
EXPECT_EQ(7, absl::visit(StrLen{}, v));
EXPECT_EQ(7u, absl::visit(StrLen{}, v));
v = std::string("VeryLargeThisTime");
EXPECT_EQ(17, absl::visit(StrLen{}, v));
EXPECT_EQ(17u, absl::visit(StrLen{}, v));
}
TEST(VariantTest, VisitRValue) {
@ -1979,7 +1979,7 @@ TEST(VariantTest, MonostateBasic) {
TEST(VariantTest, VariantMonostateDefaultConstruction) {
absl::variant<absl::monostate, NonDefaultConstructible> var;
EXPECT_EQ(var.index(), 0);
EXPECT_EQ(var.index(), 0u);
}
////////////////////////////////
@ -2100,7 +2100,7 @@ TEST(VariantTest, Hash) {
for (int i = 0; i < 100; ++i) {
hashcodes.insert(hash(i));
}
EXPECT_GT(hashcodes.size(), 90);
EXPECT_GT(hashcodes.size(), 90u);
// test const-qualified
static_assert(type_traits_internal::IsHashable<variant<const int>>::value,
@ -2312,9 +2312,9 @@ TEST(VariantTest, TestRvalueConversion) {
EXPECT_EQ(42, absl::get<int32_t>(variant2));
variant2 =
ConvertVariantTo<variant<int32_t, uint32_t>>(variant<uint32_t>(42));
ConvertVariantTo<variant<int32_t, uint32_t>>(variant<uint32_t>(42u));
ASSERT_TRUE(absl::holds_alternative<uint32_t>(variant2));
EXPECT_EQ(42, absl::get<uint32_t>(variant2));
EXPECT_EQ(42u, absl::get<uint32_t>(variant2));
#endif // !ABSL_USES_STD_VARIANT
variant<Convertible1, Convertible2> variant3(
@ -2361,10 +2361,10 @@ TEST(VariantTest, TestLvalueConversion) {
ASSERT_TRUE(absl::holds_alternative<int32_t>(variant2));
EXPECT_EQ(42, absl::get<int32_t>(variant2));
variant<uint32_t> source6(42);
variant<uint32_t> source6(42u);
variant2 = ConvertVariantTo<variant<int32_t, uint32_t>>(source6);
ASSERT_TRUE(absl::holds_alternative<uint32_t>(variant2));
EXPECT_EQ(42, absl::get<uint32_t>(variant2));
EXPECT_EQ(42u, absl::get<uint32_t>(variant2));
#endif
variant<Convertible2, Convertible1> source7((Convertible1()));
@ -2455,8 +2455,8 @@ TEST(VariantTest, TestRvalueConversionViaConvertVariantTo) {
EXPECT_THAT(absl::get_if<int32_t>(&variant2), Pointee(42));
variant2 =
ConvertVariantTo<variant<int32_t, uint32_t>>(variant<uint32_t>(42));
EXPECT_THAT(absl::get_if<uint32_t>(&variant2), Pointee(42));
ConvertVariantTo<variant<int32_t, uint32_t>>(variant<uint32_t>(42u));
EXPECT_THAT(absl::get_if<uint32_t>(&variant2), Pointee(42u));
#endif
variant<Convertible1, Convertible2> variant3(
@ -2499,9 +2499,9 @@ TEST(VariantTest, TestLvalueConversionViaConvertVariantTo) {
ConvertVariantTo<variant<int32_t, uint32_t>>(source5));
EXPECT_THAT(absl::get_if<int32_t>(&variant2), Pointee(42));
variant<uint32_t> source6(42);
variant<uint32_t> source6(42u);
variant2 = ConvertVariantTo<variant<int32_t, uint32_t>>(source6);
EXPECT_THAT(absl::get_if<uint32_t>(&variant2), Pointee(42));
EXPECT_THAT(absl::get_if<uint32_t>(&variant2), Pointee(42u));
#endif // !ABSL_USES_STD_VARIANT
variant<Convertible2, Convertible1> source7((Convertible1()));
@ -2570,7 +2570,7 @@ TEST(VariantTest, TestVectorOfMoveonlyVariant) {
vec.reserve(3);
auto another_vec = absl::move(vec);
// As a sanity check, verify vector contents.
ASSERT_EQ(2, another_vec.size());
ASSERT_EQ(2u, another_vec.size());
EXPECT_EQ(42, *absl::get<std::unique_ptr<int>>(another_vec[0]));
EXPECT_EQ("Hello", absl::get<std::string>(another_vec[1]));
}

Loading…
Cancel
Save