// Copyright 2005, Google Inc.
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
// * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above
// copyright notice, this list of conditions and the following disclaimer
// in the documentation and/or other materials provided with the
// distribution.
// * Neither the name of Google Inc. nor the names of its
// contributors may be used to endorse or promote products derived from
// this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//
// Author: wan@google.com (Zhanyong Wan)
//
// Tests for Google Test itself. This verifies that the basic constructs of
// Google Test work.
#include <gtest/gtest.h>
// Verifies that the command line flag variables can be accessed
// in code once <gtest/gtest.h> has been #included.
// Do not move it after other #includes.
TEST(CommandLineFlagsTest, CanBeAccessedInCodeOnceGTestHIsIncluded) {
bool dummy = testing::GTEST_FLAG(also_run_disabled_tests)
|| testing::GTEST_FLAG(break_on_failure)
|| testing::GTEST_FLAG(catch_exceptions)
|| testing::GTEST_FLAG(color) != "unknown"
|| testing::GTEST_FLAG(filter) != "unknown"
|| testing::GTEST_FLAG(list_tests)
|| testing::GTEST_FLAG(output) != "unknown"
|| testing::GTEST_FLAG(print_time)
|| testing::GTEST_FLAG(random_seed)
|| testing::GTEST_FLAG(repeat) > 0
|| testing::GTEST_FLAG(show_internal_stack_frames)
|| testing::GTEST_FLAG(shuffle)
|| testing::GTEST_FLAG(stack_trace_depth) > 0
|| testing::GTEST_FLAG(throw_on_failure);
EXPECT_TRUE(dummy || !dummy); // Suppresses warning that dummy is unused.
}
#include <gtest/gtest-spi.h>
// Indicates that this translation unit is part of Google Test's
// implementation. It must come before gtest-internal-inl.h is
// included, or there will be a compiler error. This trick is to
// prevent a user from accidentally including gtest-internal-inl.h in
// his code.
#define GTEST_IMPLEMENTATION_ 1
#include "src/gtest-internal-inl.h"
#undef GTEST_IMPLEMENTATION_
#include <limits.h> // For INT_MAX.
#include <stdlib.h>
#include <time.h>
#if GTEST_HAS_PTHREAD
#include <pthread.h>
#endif // GTEST_HAS_PTHREAD
#ifdef __BORLANDC__
#include <map>
#endif
namespace testing {
namespace internal {
const char* FormatTimeInMillisAsSeconds(TimeInMillis ms);
bool ParseInt32Flag(const char* str, const char* flag, Int32* value);
} // namespace internal
} // namespace testing
using testing::internal::FormatTimeInMillisAsSeconds;
using testing::internal::ParseInt32Flag;
namespace testing {
GTEST_DECLARE_string_(output);
GTEST_DECLARE_string_(color);
namespace internal {
bool ShouldUseColor(bool stdout_is_tty);
} // namespace internal
} // namespace testing
using testing::AssertionFailure;
using testing::AssertionResult;
using testing::AssertionSuccess;
using testing::DoubleLE;
using testing::FloatLE;
using testing::GTEST_FLAG(also_run_disabled_tests);
using testing::GTEST_FLAG(break_on_failure);
using testing::GTEST_FLAG(catch_exceptions);
using testing::GTEST_FLAG(color);
using testing::GTEST_FLAG(death_test_use_fork);
using testing::GTEST_FLAG(filter);
using testing::GTEST_FLAG(list_tests);
using testing::GTEST_FLAG(output);
using testing::GTEST_FLAG(print_time);
using testing::GTEST_FLAG(random_seed);
using testing::GTEST_FLAG(repeat);
using testing::GTEST_FLAG(show_internal_stack_frames);
using testing::GTEST_FLAG(shuffle);
using testing::GTEST_FLAG(stack_trace_depth);
using testing::GTEST_FLAG(throw_on_failure);
using testing::IsNotSubstring;
using testing::IsSubstring;
using testing::Message;
using testing::ScopedFakeTestPartResultReporter;
using testing::StaticAssertTypeEq;
using testing::TPRT_FATAL_FAILURE;
using testing::TPRT_NONFATAL_FAILURE;
using testing::TPRT_SUCCESS;
using testing::Test;
using testing::TestPartResult;
using testing::TestPartResultArray;
using testing::UnitTest;
using testing::internal::kMaxRandomSeed;
using testing::internal::kTestTypeIdInGoogleTest;
using testing::internal::AppendUserMessage;
using testing::internal::CodePointToUtf8;
using testing::internal::EqFailure;
using testing::internal::FloatingPoint;
using testing::internal::GTestFlagSaver;
using testing::internal::GetCurrentOsStackTraceExceptTop;
using testing::internal::GetNextRandomSeed;
using testing::internal::GetRandomSeedFromFlag;
using testing::internal::GetTestTypeId;
using testing::internal::GetTypeId;
using testing::internal::GetUnitTestImpl;
using testing::internal::Int32;
using testing::internal::Int32FromEnvOrDie;
using testing::internal::ShouldRunTestOnShard;
using testing::internal::ShouldShard;
using testing::internal::ShouldUseColor;
using testing::internal::StreamableToString;
using testing::internal::String;
using testing::internal::TestCase;
using testing::internal::TestProperty;
using testing::internal::TestResult;
using testing::internal::TestResultAccessor;
using testing::internal::ThreadLocal;
using testing::internal::Vector;
using testing::internal::WideStringToUtf8;
using testing::internal::kTestTypeIdInGoogleTest;
// This line tests that we can define tests in an unnamed namespace.
namespace {
TEST(GetRandomSeedFromFlagTest, HandlesZero) {
const int seed = GetRandomSeedFromFlag(0);
EXPECT_LE(1, seed);
EXPECT_LE(seed, static_cast<int>(kMaxRandomSeed));
}
TEST(GetRandomSeedFromFlagTest, PreservesValidSeed) {
EXPECT_EQ(1, GetRandomSeedFromFlag(1));
EXPECT_EQ(2, GetRandomSeedFromFlag(2));
EXPECT_EQ(kMaxRandomSeed - 1, GetRandomSeedFromFlag(kMaxRandomSeed - 1));
EXPECT_EQ(static_cast<int>(kMaxRandomSeed),
GetRandomSeedFromFlag(kMaxRandomSeed));
}
TEST(GetRandomSeedFromFlagTest, NormalizesInvalidSeed) {
const int seed1 = GetRandomSeedFromFlag(-1);
EXPECT_LE(1, seed1);
EXPECT_LE(seed1, static_cast<int>(kMaxRandomSeed));
const int seed2 = GetRandomSeedFromFlag(kMaxRandomSeed + 1);
EXPECT_LE(1, seed2);
EXPECT_LE(seed2, static_cast<int>(kMaxRandomSeed));
}
TEST(GetNextRandomSeedTest, WorksForValidInput) {
EXPECT_EQ(2, GetNextRandomSeed(1));
EXPECT_EQ(3, GetNextRandomSeed(2));
EXPECT_EQ(static_cast<int>(kMaxRandomSeed),
GetNextRandomSeed(kMaxRandomSeed - 1));
EXPECT_EQ(1, GetNextRandomSeed(kMaxRandomSeed));
// We deliberately don't test GetNextRandomSeed() with invalid
// inputs, as that requires death tests, which are expensive. This
// is fine as GetNextRandomSeed() is internal and has a
// straightforward definition.
}
static void ClearCurrentTestPartResults() {
TestResultAccessor::ClearTestPartResults(
GetUnitTestImpl()->current_test_result());
}
// Tests GetTypeId.
TEST(GetTypeIdTest, ReturnsSameValueForSameType) {
EXPECT_EQ(GetTypeId<int>(), GetTypeId<int>());
EXPECT_EQ(GetTypeId<Test>(), GetTypeId<Test>());
}
class SubClassOfTest : public Test {};
class AnotherSubClassOfTest : public Test {};
TEST(GetTypeIdTest, ReturnsDifferentValuesForDifferentTypes) {
EXPECT_NE(GetTypeId<int>(), GetTypeId<const int>());
EXPECT_NE(GetTypeId<int>(), GetTypeId<char>());
EXPECT_NE(GetTypeId<int>(), GetTestTypeId());
EXPECT_NE(GetTypeId<SubClassOfTest>(), GetTestTypeId());
EXPECT_NE(GetTypeId<AnotherSubClassOfTest>(), GetTestTypeId());
EXPECT_NE(GetTypeId<AnotherSubClassOfTest>(), GetTypeId<SubClassOfTest>());
}
// Verifies that GetTestTypeId() returns the same value, no matter it
// is called from inside Google Test or outside of it.
TEST(GetTestTypeIdTest, ReturnsTheSameValueInsideOrOutsideOfGoogleTest) {
EXPECT_EQ(kTestTypeIdInGoogleTest, GetTestTypeId());
}
// Tests FormatTimeInMillisAsSeconds().
TEST(FormatTimeInMillisAsSecondsTest, FormatsZero) {
EXPECT_STREQ("0", FormatTimeInMillisAsSeconds(0));
}
TEST(FormatTimeInMillisAsSecondsTest, FormatsPositiveNumber) {
EXPECT_STREQ("0.003", FormatTimeInMillisAsSeconds(3));
EXPECT_STREQ("0.01", FormatTimeInMillisAsSeconds(10));
EXPECT_STREQ("0.2", FormatTimeInMillisAsSeconds(200));
EXPECT_STREQ("1.2", FormatTimeInMillisAsSeconds(1200));
EXPECT_STREQ("3", FormatTimeInMillisAsSeconds(3000));
}
TEST(FormatTimeInMillisAsSecondsTest, FormatsNegativeNumber) {
EXPECT_STREQ("-0.003", FormatTimeInMillisAsSeconds(-3));
EXPECT_STREQ("-0.01", FormatTimeInMillisAsSeconds(-10));
EXPECT_STREQ("-0.2", FormatTimeInMillisAsSeconds(-200));
EXPECT_STREQ("-1.2", FormatTimeInMillisAsSeconds(-1200));
EXPECT_STREQ("-3", FormatTimeInMillisAsSeconds(-3000));
}
#if !GTEST_OS_SYMBIAN
// NULL testing does not work with Symbian compilers.
#ifdef __BORLANDC__
// Silences warnings: "Condition is always true", "Unreachable code"
#pragma option push -w-ccc -w-rch
#endif
// Tests that GTEST_IS_NULL_LITERAL_(x) is true when x is a null
// pointer literal.
TEST(NullLiteralTest, IsTrueForNullLiterals) {
EXPECT_TRUE(GTEST_IS_NULL_LITERAL_(NULL));
EXPECT_TRUE(GTEST_IS_NULL_LITERAL_(0));
EXPECT_TRUE(GTEST_IS_NULL_LITERAL_(0U));
EXPECT_TRUE(GTEST_IS_NULL_LITERAL_(0L));
EXPECT_TRUE(GTEST_IS_NULL_LITERAL_(false));
#ifndef __BORLANDC__
// Some compilers may fail to detect some null pointer literals;
// as long as users of the framework don't use such literals, this
// is harmless.
EXPECT_TRUE(GTEST_IS_NULL_LITERAL_(1 - 1));
EXPECT_TRUE(GTEST_IS_NULL_LITERAL_(true && false));
#endif
}
// Tests that GTEST_IS_NULL_LITERAL_(x) is false when x is not a null
// pointer literal.
TEST(NullLiteralTest, IsFalseForNonNullLiterals) {
EXPECT_FALSE(GTEST_IS_NULL_LITERAL_(1));
EXPECT_FALSE(GTEST_IS_NULL_LITERAL_(0.0));
EXPECT_FALSE(GTEST_IS_NULL_LITERAL_('a'));
EXPECT_FALSE(GTEST_IS_NULL_LITERAL_(static_cast<void*>(NULL)));
}
#ifdef __BORLANDC__
// Restores warnings after previous "#pragma option push" supressed them
#pragma option pop
#endif
#endif // !GTEST_OS_SYMBIAN
//
// Tests CodePointToUtf8().
// Tests that the NUL character L'\0' is encoded correctly.
TEST(CodePointToUtf8Test, CanEncodeNul) {
char buffer[32];
EXPECT_STREQ("", CodePointToUtf8(L'\0', buffer));
}
// Tests that ASCII characters are encoded correctly.
TEST(CodePointToUtf8Test, CanEncodeAscii) {
char buffer[32];
EXPECT_STREQ("a", CodePointToUtf8(L'a', buffer));
EXPECT_STREQ("Z", CodePointToUtf8(L'Z', buffer));
EXPECT_STREQ("&", CodePointToUtf8(L'&', buffer));
EXPECT_STREQ("\x7F", CodePointToUtf8(L'\x7F', buffer));
}
// Tests that Unicode code-points that have 8 to 11 bits are encoded
// as 110xxxxx 10xxxxxx.
TEST(CodePointToUtf8Test, CanEncode8To11Bits) {
char buffer[32];
// 000 1101 0011 => 110-00011 10-010011
EXPECT_STREQ("\xC3\x93", CodePointToUtf8(L'\xD3', buffer));
// 101 0111 0110 => 110-10101 10-110110
EXPECT_STREQ("\xD5\xB6", CodePointToUtf8(L'\x576', buffer));
}
// Tests that Unicode code-points that have 12 to 16 bits are encoded
// as 1110xxxx 10xxxxxx 10xxxxxx.
TEST(CodePointToUtf8Test, CanEncode12To16Bits) {
char buffer[32];
// 0000 1000 1101 0011 => 1110-0000 10-100011 10-010011
EXPECT_STREQ("\xE0\xA3\x93", CodePointToUtf8(L'\x8D3', buffer));
// 1100 0111 0100 1101 => 1110-1100 10-011101 10-001101
EXPECT_STREQ("\xEC\x9D\x8D", CodePointToUtf8(L'\xC74D', buffer));
}
#if !GTEST_WIDE_STRING_USES_UTF16_
// Tests in this group require a wchar_t to hold > 16 bits, and thus
// are skipped on Windows, Cygwin, and Symbian, where a wchar_t is
// 16-bit wide. This code may not compile on those systems.
// Tests that Unicode code-points that have 17 to 21 bits are encoded
// as 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx.
TEST(CodePointToUtf8Test, CanEncode17To21Bits) {
char buffer[32];
// 0 0001 0000 1000 1101 0011 => 11110-000 10-010000 10-100011 10-010011
EXPECT_STREQ("\xF0\x90\xA3\x93", CodePointToUtf8(L'\x108D3', buffer));
// 0 0001 0000 0100 0000 0000 => 11110-000 10-010000 10-010000 10-000000
EXPECT_STREQ("\xF0\x90\x90\x80", CodePointToUtf8(L'\x10400', buffer));
// 1 0000 1000 0110 0011 0100 => 11110-100 10-001000 10-011000 10-110100
EXPECT_STREQ("\xF4\x88\x98\xB4", CodePointToUtf8(L'\x108634', buffer));
}
// Tests that encoding an invalid code-point generates the expected result.
TEST(CodePointToUtf8Test, CanEncodeInvalidCodePoint) {
char buffer[32];
EXPECT_STREQ("(Invalid Unicode 0x1234ABCD)",
CodePointToUtf8(L'\x1234ABCD', buffer));
}
#endif // !GTEST_WIDE_STRING_USES_UTF16_
// Tests WideStringToUtf8().
// Tests that the NUL character L'\0' is encoded correctly.
TEST(WideStringToUtf8Test, CanEncodeNul) {
EXPECT_STREQ("", WideStringToUtf8(L"", 0).c_str());
EXPECT_STREQ("", WideStringToUtf8(L"", -1).c_str());
}
// Tests that ASCII strings are encoded correctly.
TEST(WideStringToUtf8Test, CanEncodeAscii) {
EXPECT_STREQ("a", WideStringToUtf8(L"a", 1).c_str());
EXPECT_STREQ("ab", WideStringToUtf8(L"ab", 2).c_str());
EXPECT_STREQ("a", WideStringToUtf8(L"a", -1).c_str());
EXPECT_STREQ("ab", WideStringToUtf8(L"ab", -1).c_str());
}
// Tests that Unicode code-points that have 8 to 11 bits are encoded
// as 110xxxxx 10xxxxxx.
TEST(WideStringToUtf8Test, CanEncode8To11Bits) {
// 000 1101 0011 => 110-00011 10-010011
EXPECT_STREQ("\xC3\x93", WideStringToUtf8(L"\xD3", 1).c_str());
EXPECT_STREQ("\xC3\x93", WideStringToUtf8(L"\xD3", -1).c_str());
// 101 0111 0110 => 110-10101 10-110110
EXPECT_STREQ("\xD5\xB6", WideStringToUtf8(L"\x576", 1).c_str());
EXPECT_STREQ("\xD5\xB6", WideStringToUtf8(L"\x576", -1).c_str());
}
// Tests that Unicode code-points that have 12 to 16 bits are encoded
// as 1110xxxx 10xxxxxx 10xxxxxx.
TEST(WideStringToUtf8Test, CanEncode12To16Bits) {
// 0000 1000 1101 0011 => 1110-0000 10-100011 10-010011
EXPECT_STREQ("\xE0\xA3\x93", WideStringToUtf8(L"\x8D3", 1).c_str());
EXPECT_STREQ("\xE0\xA3\x93", WideStringToUtf8(L"\x8D3", -1).c_str());
// 1100 0111 0100 1101 => 1110-1100 10-011101 10-001101
EXPECT_STREQ("\xEC\x9D\x8D", WideStringToUtf8(L"\xC74D", 1).c_str());
EXPECT_STREQ("\xEC\x9D\x8D", WideStringToUtf8(L"\xC74D", -1).c_str());
}
// Tests that the conversion stops when the function encounters \0 character.
TEST(WideStringToUtf8Test, StopsOnNulCharacter) {
EXPECT_STREQ("ABC", WideStringToUtf8(L"ABC\0XYZ", 100).c_str());
}
// Tests that the conversion stops when the function reaches the limit
// specified by the 'length' parameter.
TEST(WideStringToUtf8Test, StopsWhenLengthLimitReached) {
EXPECT_STREQ("ABC", WideStringToUtf8(L"ABCDEF", 3).c_str());
}
#if !GTEST_WIDE_STRING_USES_UTF16_
// Tests that Unicode code-points that have 17 to 21 bits are encoded
// as 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx. This code may not compile
// on the systems using UTF-16 encoding.
TEST(WideStringToUtf8Test, CanEncode17To21Bits) {
// 0 0001 0000 1000 1101 0011 => 11110-000 10-010000 10-100011 10-010011
EXPECT_STREQ("\xF0\x90\xA3\x93", WideStringToUtf8(L"\x108D3", 1).c_str());
EXPECT_STREQ("\xF0\x90\xA3\x93", WideStringToUtf8(L"\x108D3", -1).c_str());
// 1 0000 1000 0110 0011 0100 => 11110-100 10-001000 10-011000 10-110100
EXPECT_STREQ("\xF4\x88\x98\xB4", WideStringToUtf8(L"\x108634", 1).c_str());
EXPECT_STREQ("\xF4\x88\x98\xB4", WideStringToUtf8(L"\x108634", -1).c_str());
}
// Tests that encoding an invalid code-point generates the expected result.
TEST(WideStringToUtf8Test, CanEncodeInvalidCodePoint) {
EXPECT_STREQ("(Invalid Unicode 0xABCDFF)",
WideStringToUtf8(L"\xABCDFF", -1).c_str());
}
#else // !GTEST_WIDE_STRING_USES_UTF16_
// Tests that surrogate pairs are encoded correctly on the systems using
// UTF-16 encoding in the wide strings.
TEST(WideStringToUtf8Test, CanEncodeValidUtf16SUrrogatePairs) {
EXPECT_STREQ("\xF0\x90\x90\x80",
WideStringToUtf8(L"\xD801\xDC00", -1).c_str());
}
// Tests that encoding an invalid UTF-16 surrogate pair
// generates the expected result.
TEST(WideStringToUtf8Test, CanEncodeInvalidUtf16SurrogatePair) {
// Leading surrogate is at the end of the string.
EXPECT_STREQ("\xED\xA0\x80", WideStringToUtf8(L"\xD800", -1).c_str());
// Leading surrogate is not followed by the trailing surrogate.
EXPECT_STREQ("\xED\xA0\x80$", WideStringToUtf8(L"\xD800$", -1).c_str());
// Trailing surrogate appearas without a leading surrogate.
EXPECT_STREQ("\xED\xB0\x80PQR", WideStringToUtf8(L"\xDC00PQR", -1).c_str());
}
#endif // !GTEST_WIDE_STRING_USES_UTF16_
// Tests that codepoint concatenation works correctly.
#if !GTEST_WIDE_STRING_USES_UTF16_
TEST(WideStringToUtf8Test, ConcatenatesCodepointsCorrectly) {
EXPECT_STREQ(
"\xF4\x88\x98\xB4"
"\xEC\x9D\x8D"
"\n"
"\xD5\xB6"
"\xE0\xA3\x93"
"\xF4\x88\x98\xB4",
WideStringToUtf8(L"\x108634\xC74D\n\x576\x8D3\x108634", -1).c_str());
}
#else
TEST(WideStringToUtf8Test, ConcatenatesCodepointsCorrectly) {
EXPECT_STREQ(
"\xEC\x9D\x8D" "\n" "\xD5\xB6" "\xE0\xA3\x93",
WideStringToUtf8(L"\xC74D\n\x576\x8D3", -1).c_str());
}
#endif // !GTEST_WIDE_STRING_USES_UTF16_
// Tests the Vector class template.
// Tests Vector::Clear().
TEST(VectorTest, Clear) {
Vector<int> a;
a.PushBack(1);
a.Clear();
EXPECT_EQ(0, a.size());
a.PushBack(2);
a.PushBack(3);
a.Clear();
EXPECT_EQ(0, a.size());
}
// Tests Vector::PushBack().
TEST(VectorTest, PushBack) {
Vector<char> a;
a.PushBack('a');
ASSERT_EQ(1, a.size());
EXPECT_EQ('a', a.GetElement(0));
a.PushBack('b');
ASSERT_EQ(2, a.size());
EXPECT_EQ('a', a.GetElement(0));
EXPECT_EQ('b', a.GetElement(1));
}
// Tests Vector::PushFront().
TEST(VectorTest, PushFront) {
Vector<int> a;
ASSERT_EQ(0, a.size());
// Calls PushFront() on an empty Vector.
a.PushFront(1);
ASSERT_EQ(1, a.size());
EXPECT_EQ(1, a.GetElement(0));
// Calls PushFront() on a singleton Vector.
a.PushFront(2);
ASSERT_EQ(2, a.size());
EXPECT_EQ(2, a.GetElement(0));
EXPECT_EQ(1, a.GetElement(1));
// Calls PushFront() on a Vector with more than one elements.
a.PushFront(3);
ASSERT_EQ(3, a.size());
EXPECT_EQ(3, a.GetElement(0));
EXPECT_EQ(2, a.GetElement(1));
EXPECT_EQ(1, a.GetElement(2));
}
// Tests Vector::PopFront().
TEST(VectorTest, PopFront) {
Vector<int> a;
// Popping on an empty Vector should fail.
EXPECT_FALSE(a.PopFront(NULL));
// Popping again on an empty Vector should fail, and the result element
// shouldn't be overwritten.
int element = 1;
EXPECT_FALSE(a.PopFront(&element));
EXPECT_EQ(1, element);
a.PushFront(2);
a.PushFront(3);
// PopFront() should pop the element in the front of the Vector.
EXPECT_TRUE(a.PopFront(&element));
EXPECT_EQ(3, element);
// After popping the last element, the Vector should be empty.
EXPECT_TRUE(a.PopFront(NULL));
EXPECT_EQ(0, a.size());
}
// Tests inserting at the beginning using Vector::Insert().
TEST(VectorTest, InsertAtBeginning) {
Vector<int> a;
ASSERT_EQ(0, a.size());
// Inserts into an empty Vector.
a.Insert(1, 0);
ASSERT_EQ(1, a.size());
EXPECT_EQ(1, a.GetElement(0));
// Inserts at the beginning of a singleton Vector.
a.Insert(2, 0);
ASSERT_EQ(2, a.size());
EXPECT_EQ(2, a.GetElement(0));
EXPECT_EQ(1, a.GetElement(1));
// Inserts at the beginning of a Vector with more than one elements.
a.Insert(3, 0);
ASSERT_EQ(3, a.size());
EXPECT_EQ(3, a.GetElement(0));
EXPECT_EQ(2, a.GetElement(1));
EXPECT_EQ(1, a.GetElement(2));
}
// Tests inserting at a location other than the beginning using
// Vector::Insert().
TEST(VectorTest, InsertNotAtBeginning) {
// Prepares a singleton Vector.
Vector<int> a;
a.PushBack(1);
// Inserts at the end of a singleton Vector.
a.Insert(2, a.size());
ASSERT_EQ(2, a.size());
EXPECT_EQ(1, a.GetElement(0));
EXPECT_EQ(2, a.GetElement(1));
// Inserts at the end of a Vector with more than one elements.
a.Insert(3, a.size());
ASSERT_EQ(3, a.size());
EXPECT_EQ(1, a.GetElement(0));
EXPECT_EQ(2, a.GetElement(1));
EXPECT_EQ(3, a.GetElement(2));
// Inserts in the middle of a Vector.
a.Insert(4, 1);
ASSERT_EQ(4, a.size());
EXPECT_EQ(1, a.GetElement(0));
EXPECT_EQ(4, a.GetElement(1));
EXPECT_EQ(2, a.GetElement(2));
EXPECT_EQ(3, a.GetElement(3));
}
// Tests Vector::GetElementOr().
TEST(VectorTest, GetElementOr) {
Vector<char> a;
EXPECT_EQ('x', a.GetElementOr(0, 'x'));
a.PushBack('a');
a.PushBack('b');
EXPECT_EQ('a', a.GetElementOr(0, 'x'));
EXPECT_EQ('b', a.GetElementOr(1, 'x'));
EXPECT_EQ('x', a.GetElementOr(-2, 'x'));
EXPECT_EQ('x', a.GetElementOr(2, 'x'));
}
// Tests Vector::Erase().
TEST(VectorDeathTest, Erase) {
Vector<int> a;
// Tests erasing from an empty vector.
EXPECT_DEATH_IF_SUPPORTED(
a.Erase(0),
"Invalid Vector index 0: must be in range \\[0, -1\\]\\.");
// Tests erasing from a singleton vector.
a.PushBack(0);
a.Erase(0);
EXPECT_EQ(0, a.size());
// Tests Erase parameters beyond the bounds of the vector.
Vector<int> a1;
a1.PushBack(0);
a1.PushBack(1);
a1.PushBack(2);
EXPECT_DEATH_IF_SUPPORTED(
a1.Erase(3),
"Invalid Vector index 3: must be in range \\[0, 2\\]\\.");
EXPECT_DEATH_IF_SUPPORTED(
a1.Erase(-1),
"Invalid Vector index -1: must be in range \\[0, 2\\]\\.");
// Tests erasing at the end of the vector.
Vector<int> a2;
a2.PushBack(0);
a2.PushBack(1);
a2.PushBack(2);
a2.Erase(2);
ASSERT_EQ(2, a2.size());
EXPECT_EQ(0, a2.GetElement(0));
EXPECT_EQ(1, a2.GetElement(1));
// Tests erasing in the middle of the vector.
Vector<int> a3;
a3.PushBack(0);
a3.PushBack(1);
a3.PushBack(2);
a3.Erase(1);
ASSERT_EQ(2, a3.size());
EXPECT_EQ(0, a3.GetElement(0));
EXPECT_EQ(2, a3.GetElement(1));
// Tests erasing at the beginning of the vector.
Vector<int> a4;
a4.PushBack(0);
a4.PushBack(1);
a4.PushBack(2);
a4.Erase(0);
ASSERT_EQ(2, a4.size());
EXPECT_EQ(1, a4.GetElement(0));
EXPECT_EQ(2, a4.GetElement(1));
}
// Tests the GetElement accessor.
TEST(ListDeathTest, GetElement) {
Vector<int> a;
a.PushBack(0);
a.PushBack(1);
a.PushBack(2);
EXPECT_EQ(0, a.GetElement(0));
EXPECT_EQ(1, a.GetElement(1));
EXPECT_EQ(2, a.GetElement(2));
EXPECT_DEATH_IF_SUPPORTED(
a.GetElement(3),
"Invalid Vector index 3: must be in range \\[0, 2\\]\\.");
EXPECT_DEATH_IF_SUPPORTED(
a.GetElement(-1),
"Invalid Vector index -1: must be in range \\[0, 2\\]\\.");
}
// Tests the String class.
// Tests String's constructors.
TEST(StringTest, Constructors) {
// Default ctor.
String s1;
// We aren't using EXPECT_EQ(NULL, s1.c_str()) because comparing
// pointers with NULL isn't supported on all platforms.
EXPECT_TRUE(NULL == s1.c_str());
// Implicitly constructs from a C-string.
String s2 = "Hi";
EXPECT_STREQ("Hi", s2.c_str());
// Constructs from a C-string and a length.
String s3("hello", 3);
EXPECT_STREQ("hel", s3.c_str());
// Copy ctor.
String s4 = s3;
EXPECT_STREQ("hel", s4.c_str());
}
#if GTEST_HAS_STD_STRING
TEST(StringTest, ConvertsFromStdString) {
// An empty std::string.
const std::string src1("");
const String dest1 = src1;
EXPECT_STREQ("", dest1.c_str());
// A normal std::string.
const std::string src2("Hi");
const String dest2 = src2;
EXPECT_STREQ("Hi", dest2.c_str());
// An std::string with an embedded NUL character.
const char src3[] = "Hello\0world.";
const String dest3 = std::string(src3, sizeof(src3));
EXPECT_STREQ("Hello", dest3.c_str());
}
TEST(StringTest, ConvertsToStdString) {
// An empty String.
const String src1("");
const std::string dest1 = src1;
EXPECT_EQ("", dest1);
// A normal String.
const String src2("Hi");
const std::string dest2 = src2;
EXPECT_EQ("Hi", dest2);
}
#endif // GTEST_HAS_STD_STRING
#if GTEST_HAS_GLOBAL_STRING
TEST(StringTest, ConvertsFromGlobalString) {
// An empty ::string.
const ::string src1("");
const String dest1 = src1;
EXPECT_STREQ("", dest1.c_str());
// A normal ::string.
const ::string src2("Hi");
const String dest2 = src2;
EXPECT_STREQ("Hi", dest2.c_str());
// An ::string with an embedded NUL character.
const char src3[] = "Hello\0world.";
const String dest3 = ::string(src3, sizeof(src3));
EXPECT_STREQ("Hello", dest3.c_str());
}
TEST(StringTest, ConvertsToGlobalString) {
// An empty String.
const String src1("");
const ::string dest1 = src1;
EXPECT_EQ("", dest1);
// A normal String.
const String src2("Hi");
const ::string dest2 = src2;
EXPECT_EQ("Hi", dest2);
}
#endif // GTEST_HAS_GLOBAL_STRING
// Tests String::ShowCString().
TEST(StringTest, ShowCString) {
EXPECT_STREQ("(null)", String::ShowCString(NULL));
EXPECT_STREQ("", String::ShowCString(""));
EXPECT_STREQ("foo", String::ShowCString("foo"));
}
// Tests String::ShowCStringQuoted().
TEST(StringTest, ShowCStringQuoted) {
EXPECT_STREQ("(null)",
String::ShowCStringQuoted(NULL).c_str());
EXPECT_STREQ("\"\"",
String::ShowCStringQuoted("").c_str());
EXPECT_STREQ("\"foo\"",
String::ShowCStringQuoted("foo").c_str());
}
// Tests String::operator==().
TEST(StringTest, Equals) {
const String null(NULL);
EXPECT_TRUE(null == NULL); // NOLINT
EXPECT_FALSE(null == ""); // NOLINT
EXPECT_FALSE(null == "bar"); // NOLINT
const String empty("");
EXPECT_FALSE(empty == NULL); // NOLINT
EXPECT_TRUE(empty == ""); // NOLINT
EXPECT_FALSE(empty == "bar"); // NOLINT
const String foo("foo");
EXPECT_FALSE(foo == NULL); // NOLINT
EXPECT_FALSE(foo == ""); // NOLINT
EXPECT_FALSE(foo == "bar"); // NOLINT
EXPECT_TRUE(foo == "foo"); // NOLINT
}
// Tests String::operator!=().
TEST(StringTest, NotEquals) {
const String null(NULL);
EXPECT_FALSE(null != NULL); // NOLINT
EXPECT_TRUE(null != ""); // NOLINT
EXPECT_TRUE(null != "bar"); // NOLINT
const String empty("");
EXPECT_TRUE(empty != NULL); // NOLINT
EXPECT_FALSE(empty != ""); // NOLINT
EXPECT_TRUE(empty != "bar"); // NOLINT
const String foo("foo");
EXPECT_TRUE(foo != NULL); // NOLINT
EXPECT_TRUE(foo != ""); // NOLINT
EXPECT_TRUE(foo != "bar"); // NOLINT
EXPECT_FALSE(foo != "foo"); // NOLINT
}
// Tests String::EndsWith().
TEST(StringTest, EndsWith) {
EXPECT_TRUE(String("foobar").EndsWith("bar"));
EXPECT_TRUE(String("foobar").EndsWith(""));