Apply clang-tidy modernize-use-nullptr to googletest.

Now that googletest has moved to C++11, it should no longer
use NULL or 0 for the null pointer. This patch converts all
such usages to nullptr using clang-tidy.

This prevents LLVM from issuing -Wzero-as-null-pointer-constant
warnings.

PiperOrigin-RevId: 215814400
This commit is contained in:
Abseil Team
2018-10-04 18:28:05 -04:00
committed by Gennadiy Civil
parent c660240ad0
commit 7a6bc673ea
58 changed files with 705 additions and 1107 deletions

View File

@@ -100,7 +100,7 @@ TEST(ListenersTest, DoesNotLeak) {
// specified.
TEST(ListenersTest, LeaksWater) {
Water* water = new Water;
EXPECT_TRUE(water != NULL);
EXPECT_TRUE(water != nullptr);
}
} // namespace

View File

@@ -35,7 +35,7 @@
// Clones a 0-terminated C string, allocating memory using new.
const char* MyString::CloneCString(const char* a_c_string) {
if (a_c_string == NULL) return NULL;
if (a_c_string == nullptr) return nullptr;
const size_t len = strlen(a_c_string);
char* const clone = new char[ len + 1 ];

View File

@@ -50,15 +50,15 @@ class MyString {
// C'tors
// The default c'tor constructs a NULL string.
MyString() : c_string_(NULL) {}
MyString() : c_string_(nullptr) {}
// Constructs a MyString by cloning a 0-terminated C string.
explicit MyString(const char* a_c_string) : c_string_(NULL) {
explicit MyString(const char* a_c_string) : c_string_(nullptr) {
Set(a_c_string);
}
// Copy c'tor
MyString(const MyString& string) : c_string_(NULL) {
MyString(const MyString& string) : c_string_(nullptr) {
Set(string.c_string_);
}
@@ -71,9 +71,7 @@ class MyString {
// Gets the 0-terminated C string this MyString object represents.
const char* c_string() const { return c_string_; }
size_t Length() const {
return c_string_ == NULL ? 0 : strlen(c_string_);
}
size_t Length() const { return c_string_ == nullptr ? 0 : strlen(c_string_); }
// Sets the 0-terminated C string this MyString object represents.
void Set(const char* c_string);

View File

@@ -66,7 +66,7 @@ TEST(MyString, DefaultConstructor) {
// we have to live with this fact.
//
// </TechnicalDetails>
EXPECT_STREQ(NULL, s.c_string());
EXPECT_STREQ(nullptr, s.c_string());
EXPECT_EQ(0u, s.Length());
}
@@ -101,7 +101,7 @@ TEST(MyString, Set) {
EXPECT_EQ(0, strcmp(s.c_string(), kHelloString));
// Can we set the MyString to NULL?
s.Set(NULL);
EXPECT_STREQ(NULL, s.c_string());
s.Set(nullptr);
EXPECT_STREQ(nullptr, s.c_string());
}
} // namespace

View File

@@ -58,7 +58,8 @@ class QueueNode {
private:
// Creates a node with a given element value. The next pointer is
// set to NULL.
explicit QueueNode(const E& an_element) : element_(an_element), next_(NULL) {}
explicit QueueNode(const E& an_element)
: element_(an_element), next_(nullptr) {}
// We disable the default assignment operator and copy c'tor.
const QueueNode& operator = (const QueueNode&);
@@ -72,7 +73,7 @@ template <typename E> // E is the element type.
class Queue {
public:
// Creates an empty queue.
Queue() : head_(NULL), last_(NULL), size_(0) {}
Queue() : head_(nullptr), last_(nullptr), size_(0) {}
// D'tor. Clears the queue.
~Queue() { Clear(); }
@@ -86,12 +87,12 @@ class Queue {
for (; ;) {
delete node;
node = next;
if (node == NULL) break;
if (node == nullptr) break;
next = node->next();
}
// 2. Resets the member variables.
head_ = last_ = NULL;
head_ = last_ = nullptr;
size_ = 0;
}
}
@@ -128,14 +129,14 @@ class Queue {
// the queue is empty.
E* Dequeue() {
if (size_ == 0) {
return NULL;
return nullptr;
}
const QueueNode<E>* const old_head = head_;
head_ = head_->next_;
size_--;
if (size_ == 0) {
last_ = NULL;
last_ = nullptr;
}
E* element = new E(old_head->element());
@@ -150,7 +151,8 @@ class Queue {
template <typename F>
Queue* Map(F function) const {
Queue* new_queue = new Queue();
for (const QueueNode<E>* node = head_; node != NULL; node = node->next_) {
for (const QueueNode<E>* node = head_; node != nullptr;
node = node->next_) {
new_queue->Enqueue(function(node->element()));
}

View File

@@ -99,8 +99,8 @@ class QueueTestSmpl3 : public testing::Test {
ASSERT_EQ(q->Size(), new_q->Size());
// Verifies the relationship between the elements of the two queues.
for ( const QueueNode<int> * n1 = q->Head(), * n2 = new_q->Head();
n1 != NULL; n1 = n1->next(), n2 = n2->next() ) {
for (const QueueNode<int>*n1 = q->Head(), *n2 = new_q->Head();
n1 != nullptr; n1 = n1->next(), n2 = n2->next()) {
EXPECT_EQ(2 * n1->element(), n2->element());
}
@@ -125,16 +125,16 @@ TEST_F(QueueTestSmpl3, DefaultConstructor) {
// Tests Dequeue().
TEST_F(QueueTestSmpl3, Dequeue) {
int * n = q0_.Dequeue();
EXPECT_TRUE(n == NULL);
EXPECT_TRUE(n == nullptr);
n = q1_.Dequeue();
ASSERT_TRUE(n != NULL);
ASSERT_TRUE(n != nullptr);
EXPECT_EQ(1, *n);
EXPECT_EQ(0u, q1_.Size());
delete n;
n = q2_.Dequeue();
ASSERT_TRUE(n != NULL);
ASSERT_TRUE(n != nullptr);
EXPECT_EQ(2, *n);
EXPECT_EQ(1u, q2_.Size());
delete n;

View File

@@ -63,15 +63,13 @@ class QuickTest : public testing::Test {
protected:
// Remember that SetUp() is run immediately before a test starts.
// This is a good place to record the start time.
virtual void SetUp() {
start_time_ = time(NULL);
}
virtual void SetUp() { start_time_ = time(nullptr); }
// TearDown() is invoked immediately after a test finishes. Here we
// check if the test was too slow.
virtual void TearDown() {
// Gets the time when the test finishes
const time_t end_time = time(NULL);
const time_t end_time = time(nullptr);
// Asserts that the test took no more than ~5 seconds. Did you
// know that you can use assertions in SetUp() and TearDown() as
@@ -176,16 +174,16 @@ TEST_F(QueueTest, DefaultConstructor) {
// Tests Dequeue().
TEST_F(QueueTest, Dequeue) {
int* n = q0_.Dequeue();
EXPECT_TRUE(n == NULL);
EXPECT_TRUE(n == nullptr);
n = q1_.Dequeue();
EXPECT_TRUE(n != NULL);
EXPECT_TRUE(n != nullptr);
EXPECT_EQ(1, *n);
EXPECT_EQ(0u, q1_.Size());
delete n;
n = q2_.Dequeue();
EXPECT_TRUE(n != NULL);
EXPECT_TRUE(n != nullptr);
EXPECT_EQ(2, *n);
EXPECT_EQ(1u, q2_.Size());
delete n;

View File

@@ -69,7 +69,7 @@ class PrimeTableTestSmpl7 : public TestWithParam<CreatePrimeTableFunc*> {
virtual void SetUp() { table_ = (*GetParam())(); }
virtual void TearDown() {
delete table_;
table_ = NULL;
table_ = nullptr;
}
protected:

View File

@@ -50,8 +50,9 @@ class HybridPrimeTable : public PrimeTable {
public:
HybridPrimeTable(bool force_on_the_fly, int max_precalculated)
: on_the_fly_impl_(new OnTheFlyPrimeTable),
precalc_impl_(force_on_the_fly ? NULL :
new PreCalculatedPrimeTable(max_precalculated)),
precalc_impl_(force_on_the_fly
? nullptr
: new PreCalculatedPrimeTable(max_precalculated)),
max_precalculated_(max_precalculated) {}
virtual ~HybridPrimeTable() {
delete on_the_fly_impl_;
@@ -59,7 +60,7 @@ class HybridPrimeTable : public PrimeTable {
}
virtual bool IsPrime(int n) const {
if (precalc_impl_ != NULL && n < max_precalculated_)
if (precalc_impl_ != nullptr && n < max_precalculated_)
return precalc_impl_->IsPrime(n);
else
return on_the_fly_impl_->IsPrime(n);
@@ -67,7 +68,7 @@ class HybridPrimeTable : public PrimeTable {
virtual int GetNextPrime(int p) const {
int next_prime = -1;
if (precalc_impl_ != NULL && p < max_precalculated_)
if (precalc_impl_ != nullptr && p < max_precalculated_)
next_prime = precalc_impl_->GetNextPrime(p);
return next_prime != -1 ? next_prime : on_the_fly_impl_->GetNextPrime(p);
@@ -106,7 +107,7 @@ class PrimeTableTest : public TestWithParam< ::testing::tuple<bool, int> > {
}
virtual void TearDown() {
delete table_;
table_ = NULL;
table_ = nullptr;
}
HybridPrimeTable* table_;
};