~statik/ubuntu/maverick/protobuf/A

« back to all changes in this revision

Viewing changes to gtest/test/gtest_unittest.cc

  • Committer: Bazaar Package Importer
  • Author(s): Steve Kowalik
  • Date: 2010-02-11 11:13:19 UTC
  • mfrom: (2.2.2 squeeze)
  • Revision ID: james.westby@ubuntu.com-20100211111319-zdn8hmw0gh8s4cf8
Tags: 2.2.0a-0.1ubuntu1
* Merge from Debian testing.
* Remaining Ubuntu changes:
  - Don't use python2.4.
* Ubuntu changes dropped:
  - Disable death tests on Itanium, fixed upstream.

Show diffs side-by-side

added added

removed removed

Lines of Context:
46
46
      || testing::GTEST_FLAG(list_tests)
47
47
      || testing::GTEST_FLAG(output) != "unknown"
48
48
      || testing::GTEST_FLAG(print_time)
 
49
      || testing::GTEST_FLAG(random_seed)
49
50
      || testing::GTEST_FLAG(repeat) > 0
50
51
      || testing::GTEST_FLAG(show_internal_stack_frames)
 
52
      || testing::GTEST_FLAG(shuffle)
51
53
      || testing::GTEST_FLAG(stack_trace_depth) > 0
52
54
      || testing::GTEST_FLAG(throw_on_failure);
53
55
  EXPECT_TRUE(dummy || !dummy);  // Suppresses warning that dummy is unused.
64
66
#include "src/gtest-internal-inl.h"
65
67
#undef GTEST_IMPLEMENTATION_
66
68
 
 
69
#include <limits.h>  // For INT_MAX.
67
70
#include <stdlib.h>
68
71
#include <time.h>
69
72
 
71
74
#include <pthread.h>
72
75
#endif  // GTEST_HAS_PTHREAD
73
76
 
74
 
#if GTEST_OS_LINUX
75
 
#include <string.h>
76
 
#include <signal.h>
77
 
#include <sys/stat.h>
78
 
#include <unistd.h>
79
 
#include <string>
80
 
#include <vector>
81
 
#endif  // GTEST_OS_LINUX
82
 
 
83
77
#ifdef __BORLANDC__
84
78
#include <map>
85
79
#endif
87
81
namespace testing {
88
82
namespace internal {
89
83
const char* FormatTimeInMillisAsSeconds(TimeInMillis ms);
 
84
 
90
85
bool ParseInt32Flag(const char* str, const char* flag, Int32* value);
 
86
 
91
87
}  // namespace internal
92
88
}  // namespace testing
93
89
 
112
108
using testing::GTEST_FLAG(also_run_disabled_tests);
113
109
using testing::GTEST_FLAG(break_on_failure);
114
110
using testing::GTEST_FLAG(catch_exceptions);
 
111
using testing::GTEST_FLAG(color);
115
112
using testing::GTEST_FLAG(death_test_use_fork);
116
 
using testing::GTEST_FLAG(color);
117
113
using testing::GTEST_FLAG(filter);
118
114
using testing::GTEST_FLAG(list_tests);
119
115
using testing::GTEST_FLAG(output);
120
116
using testing::GTEST_FLAG(print_time);
 
117
using testing::GTEST_FLAG(random_seed);
121
118
using testing::GTEST_FLAG(repeat);
122
119
using testing::GTEST_FLAG(show_internal_stack_frames);
 
120
using testing::GTEST_FLAG(shuffle);
123
121
using testing::GTEST_FLAG(stack_trace_depth);
124
122
using testing::GTEST_FLAG(throw_on_failure);
125
123
using testing::IsNotSubstring;
127
125
using testing::Message;
128
126
using testing::ScopedFakeTestPartResultReporter;
129
127
using testing::StaticAssertTypeEq;
 
128
using testing::TPRT_FATAL_FAILURE;
 
129
using testing::TPRT_NONFATAL_FAILURE;
 
130
using testing::TPRT_SUCCESS;
130
131
using testing::Test;
131
132
using testing::TestPartResult;
132
133
using testing::TestPartResultArray;
133
 
using testing::TPRT_FATAL_FAILURE;
134
 
using testing::TPRT_NONFATAL_FAILURE;
135
 
using testing::TPRT_SUCCESS;
136
134
using testing::UnitTest;
 
135
using testing::internal::kMaxRandomSeed;
137
136
using testing::internal::kTestTypeIdInGoogleTest;
138
137
using testing::internal::AppendUserMessage;
139
 
using testing::internal::ClearCurrentTestPartResults;
140
138
using testing::internal::CodePointToUtf8;
141
139
using testing::internal::EqFailure;
142
140
using testing::internal::FloatingPoint;
 
141
using testing::internal::GTestFlagSaver;
143
142
using testing::internal::GetCurrentOsStackTraceExceptTop;
144
 
using testing::internal::GetFailedPartCount;
 
143
using testing::internal::GetNextRandomSeed;
 
144
using testing::internal::GetRandomSeedFromFlag;
145
145
using testing::internal::GetTestTypeId;
146
146
using testing::internal::GetTypeId;
147
 
using testing::internal::GTestFlagSaver;
 
147
using testing::internal::GetUnitTestImpl;
148
148
using testing::internal::Int32;
149
149
using testing::internal::Int32FromEnvOrDie;
150
 
using testing::internal::List;
151
150
using testing::internal::ShouldRunTestOnShard;
152
151
using testing::internal::ShouldShard;
153
152
using testing::internal::ShouldUseColor;
154
153
using testing::internal::StreamableToString;
155
154
using testing::internal::String;
 
155
using testing::internal::TestCase;
156
156
using testing::internal::TestProperty;
157
157
using testing::internal::TestResult;
 
158
using testing::internal::TestResultAccessor;
158
159
using testing::internal::ThreadLocal;
159
 
using testing::internal::UnitTestImpl;
 
160
using testing::internal::Vector;
160
161
using testing::internal::WideStringToUtf8;
 
162
using testing::internal::kTestTypeIdInGoogleTest;
161
163
 
162
164
// This line tests that we can define tests in an unnamed namespace.
163
165
namespace {
164
166
 
 
167
TEST(GetRandomSeedFromFlagTest, HandlesZero) {
 
168
  const int seed = GetRandomSeedFromFlag(0);
 
169
  EXPECT_LE(1, seed);
 
170
  EXPECT_LE(seed, static_cast<int>(kMaxRandomSeed));
 
171
}
 
172
 
 
173
TEST(GetRandomSeedFromFlagTest, PreservesValidSeed) {
 
174
  EXPECT_EQ(1, GetRandomSeedFromFlag(1));
 
175
  EXPECT_EQ(2, GetRandomSeedFromFlag(2));
 
176
  EXPECT_EQ(kMaxRandomSeed - 1, GetRandomSeedFromFlag(kMaxRandomSeed - 1));
 
177
  EXPECT_EQ(static_cast<int>(kMaxRandomSeed),
 
178
            GetRandomSeedFromFlag(kMaxRandomSeed));
 
179
}
 
180
 
 
181
TEST(GetRandomSeedFromFlagTest, NormalizesInvalidSeed) {
 
182
  const int seed1 = GetRandomSeedFromFlag(-1);
 
183
  EXPECT_LE(1, seed1);
 
184
  EXPECT_LE(seed1, static_cast<int>(kMaxRandomSeed));
 
185
 
 
186
  const int seed2 = GetRandomSeedFromFlag(kMaxRandomSeed + 1);
 
187
  EXPECT_LE(1, seed2);
 
188
  EXPECT_LE(seed2, static_cast<int>(kMaxRandomSeed));
 
189
}
 
190
 
 
191
TEST(GetNextRandomSeedTest, WorksForValidInput) {
 
192
  EXPECT_EQ(2, GetNextRandomSeed(1));
 
193
  EXPECT_EQ(3, GetNextRandomSeed(2));
 
194
  EXPECT_EQ(static_cast<int>(kMaxRandomSeed),
 
195
            GetNextRandomSeed(kMaxRandomSeed - 1));
 
196
  EXPECT_EQ(1, GetNextRandomSeed(kMaxRandomSeed));
 
197
 
 
198
  // We deliberately don't test GetNextRandomSeed() with invalid
 
199
  // inputs, as that requires death tests, which are expensive.  This
 
200
  // is fine as GetNextRandomSeed() is internal and has a
 
201
  // straightforward definition.
 
202
}
 
203
 
 
204
static void ClearCurrentTestPartResults() {
 
205
  TestResultAccessor::ClearTestPartResults(
 
206
      GetUnitTestImpl()->current_test_result());
 
207
}
 
208
 
165
209
// Tests GetTypeId.
166
210
 
167
211
TEST(GetTypeIdTest, ReturnsSameValueForSameType) {
428
472
}
429
473
#endif  // !GTEST_WIDE_STRING_USES_UTF16_
430
474
 
431
 
// Tests the List template class.
432
 
 
433
 
// Tests List::PushFront().
434
 
TEST(ListTest, PushFront) {
435
 
  List<int> a;
436
 
  ASSERT_EQ(0u, a.size());
437
 
 
438
 
  // Calls PushFront() on an empty list.
 
475
// Tests the Vector class template.
 
476
 
 
477
// Tests Vector::Clear().
 
478
TEST(VectorTest, Clear) {
 
479
  Vector<int> a;
 
480
  a.PushBack(1);
 
481
  a.Clear();
 
482
  EXPECT_EQ(0, a.size());
 
483
 
 
484
  a.PushBack(2);
 
485
  a.PushBack(3);
 
486
  a.Clear();
 
487
  EXPECT_EQ(0, a.size());
 
488
}
 
489
 
 
490
// Tests Vector::PushBack().
 
491
TEST(VectorTest, PushBack) {
 
492
  Vector<char> a;
 
493
  a.PushBack('a');
 
494
  ASSERT_EQ(1, a.size());
 
495
  EXPECT_EQ('a', a.GetElement(0));
 
496
 
 
497
  a.PushBack('b');
 
498
  ASSERT_EQ(2, a.size());
 
499
  EXPECT_EQ('a', a.GetElement(0));
 
500
  EXPECT_EQ('b', a.GetElement(1));
 
501
}
 
502
 
 
503
// Tests Vector::PushFront().
 
504
TEST(VectorTest, PushFront) {
 
505
  Vector<int> a;
 
506
  ASSERT_EQ(0, a.size());
 
507
 
 
508
  // Calls PushFront() on an empty Vector.
439
509
  a.PushFront(1);
440
 
  ASSERT_EQ(1u, a.size());
441
 
  EXPECT_EQ(1, a.Head()->element());
442
 
  ASSERT_EQ(a.Head(), a.Last());
 
510
  ASSERT_EQ(1, a.size());
 
511
  EXPECT_EQ(1, a.GetElement(0));
443
512
 
444
 
  // Calls PushFront() on a singleton list.
 
513
  // Calls PushFront() on a singleton Vector.
445
514
  a.PushFront(2);
446
 
  ASSERT_EQ(2u, a.size());
447
 
  EXPECT_EQ(2, a.Head()->element());
448
 
  EXPECT_EQ(1, a.Last()->element());
 
515
  ASSERT_EQ(2, a.size());
 
516
  EXPECT_EQ(2, a.GetElement(0));
 
517
  EXPECT_EQ(1, a.GetElement(1));
449
518
 
450
 
  // Calls PushFront() on a list with more than one elements.
 
519
  // Calls PushFront() on a Vector with more than one elements.
451
520
  a.PushFront(3);
452
 
  ASSERT_EQ(3u, a.size());
453
 
  EXPECT_EQ(3, a.Head()->element());
454
 
  EXPECT_EQ(2, a.Head()->next()->element());
455
 
  EXPECT_EQ(1, a.Last()->element());
 
521
  ASSERT_EQ(3, a.size());
 
522
  EXPECT_EQ(3, a.GetElement(0));
 
523
  EXPECT_EQ(2, a.GetElement(1));
 
524
  EXPECT_EQ(1, a.GetElement(2));
456
525
}
457
526
 
458
 
// Tests List::PopFront().
459
 
TEST(ListTest, PopFront) {
460
 
  List<int> a;
 
527
// Tests Vector::PopFront().
 
528
TEST(VectorTest, PopFront) {
 
529
  Vector<int> a;
461
530
 
462
 
  // Popping on an empty list should fail.
 
531
  // Popping on an empty Vector should fail.
463
532
  EXPECT_FALSE(a.PopFront(NULL));
464
533
 
465
 
  // Popping again on an empty list should fail, and the result element
 
534
  // Popping again on an empty Vector should fail, and the result element
466
535
  // shouldn't be overwritten.
467
536
  int element = 1;
468
537
  EXPECT_FALSE(a.PopFront(&element));
471
540
  a.PushFront(2);
472
541
  a.PushFront(3);
473
542
 
474
 
  // PopFront() should pop the element in the front of the list.
 
543
  // PopFront() should pop the element in the front of the Vector.
475
544
  EXPECT_TRUE(a.PopFront(&element));
476
545
  EXPECT_EQ(3, element);
477
546
 
478
 
  // After popping the last element, the list should be empty.
 
547
  // After popping the last element, the Vector should be empty.
479
548
  EXPECT_TRUE(a.PopFront(NULL));
480
 
  EXPECT_EQ(0u, a.size());
 
549
  EXPECT_EQ(0, a.size());
481
550
}
482
551
 
483
 
// Tests inserting at the beginning using List::InsertAfter().
484
 
TEST(ListTest, InsertAfterAtBeginning) {
485
 
  List<int> a;
486
 
  ASSERT_EQ(0u, a.size());
487
 
 
488
 
  // Inserts into an empty list.
489
 
  a.InsertAfter(NULL, 1);
490
 
  ASSERT_EQ(1u, a.size());
491
 
  EXPECT_EQ(1, a.Head()->element());
492
 
  ASSERT_EQ(a.Head(), a.Last());
493
 
 
494
 
  // Inserts at the beginning of a singleton list.
495
 
  a.InsertAfter(NULL, 2);
496
 
  ASSERT_EQ(2u, a.size());
497
 
  EXPECT_EQ(2, a.Head()->element());
498
 
  EXPECT_EQ(1, a.Last()->element());
499
 
 
500
 
  // Inserts at the beginning of a list with more than one elements.
501
 
  a.InsertAfter(NULL, 3);
502
 
  ASSERT_EQ(3u, a.size());
503
 
  EXPECT_EQ(3, a.Head()->element());
504
 
  EXPECT_EQ(2, a.Head()->next()->element());
505
 
  EXPECT_EQ(1, a.Last()->element());
 
552
// Tests inserting at the beginning using Vector::Insert().
 
553
TEST(VectorTest, InsertAtBeginning) {
 
554
  Vector<int> a;
 
555
  ASSERT_EQ(0, a.size());
 
556
 
 
557
  // Inserts into an empty Vector.
 
558
  a.Insert(1, 0);
 
559
  ASSERT_EQ(1, a.size());
 
560
  EXPECT_EQ(1, a.GetElement(0));
 
561
 
 
562
  // Inserts at the beginning of a singleton Vector.
 
563
  a.Insert(2, 0);
 
564
  ASSERT_EQ(2, a.size());
 
565
  EXPECT_EQ(2, a.GetElement(0));
 
566
  EXPECT_EQ(1, a.GetElement(1));
 
567
 
 
568
  // Inserts at the beginning of a Vector with more than one elements.
 
569
  a.Insert(3, 0);
 
570
  ASSERT_EQ(3, a.size());
 
571
  EXPECT_EQ(3, a.GetElement(0));
 
572
  EXPECT_EQ(2, a.GetElement(1));
 
573
  EXPECT_EQ(1, a.GetElement(2));
506
574
}
507
575
 
508
576
// Tests inserting at a location other than the beginning using
509
 
// List::InsertAfter().
510
 
TEST(ListTest, InsertAfterNotAtBeginning) {
511
 
  // Prepares a singleton list.
512
 
  List<int> a;
513
 
  a.PushBack(1);
514
 
 
515
 
  // Inserts at the end of a singleton list.
516
 
  a.InsertAfter(a.Last(), 2);
517
 
  ASSERT_EQ(2u, a.size());
518
 
  EXPECT_EQ(1, a.Head()->element());
519
 
  EXPECT_EQ(2, a.Last()->element());
520
 
 
521
 
  // Inserts at the end of a list with more than one elements.
522
 
  a.InsertAfter(a.Last(), 3);
523
 
  ASSERT_EQ(3u, a.size());
524
 
  EXPECT_EQ(1, a.Head()->element());
525
 
  EXPECT_EQ(2, a.Head()->next()->element());
526
 
  EXPECT_EQ(3, a.Last()->element());
527
 
 
528
 
  // Inserts in the middle of a list.
529
 
  a.InsertAfter(a.Head(), 4);
530
 
  ASSERT_EQ(4u, a.size());
531
 
  EXPECT_EQ(1, a.Head()->element());
532
 
  EXPECT_EQ(4, a.Head()->next()->element());
533
 
  EXPECT_EQ(2, a.Head()->next()->next()->element());
534
 
  EXPECT_EQ(3, a.Last()->element());
535
 
}
536
 
 
 
577
// Vector::Insert().
 
578
TEST(VectorTest, InsertNotAtBeginning) {
 
579
  // Prepares a singleton Vector.
 
580
  Vector<int> a;
 
581
  a.PushBack(1);
 
582
 
 
583
  // Inserts at the end of a singleton Vector.
 
584
  a.Insert(2, a.size());
 
585
  ASSERT_EQ(2, a.size());
 
586
  EXPECT_EQ(1, a.GetElement(0));
 
587
  EXPECT_EQ(2, a.GetElement(1));
 
588
 
 
589
  // Inserts at the end of a Vector with more than one elements.
 
590
  a.Insert(3, a.size());
 
591
  ASSERT_EQ(3, a.size());
 
592
  EXPECT_EQ(1, a.GetElement(0));
 
593
  EXPECT_EQ(2, a.GetElement(1));
 
594
  EXPECT_EQ(3, a.GetElement(2));
 
595
 
 
596
  // Inserts in the middle of a Vector.
 
597
  a.Insert(4, 1);
 
598
  ASSERT_EQ(4, a.size());
 
599
  EXPECT_EQ(1, a.GetElement(0));
 
600
  EXPECT_EQ(4, a.GetElement(1));
 
601
  EXPECT_EQ(2, a.GetElement(2));
 
602
  EXPECT_EQ(3, a.GetElement(3));
 
603
}
 
604
 
 
605
// Tests Vector::GetElementOr().
 
606
TEST(VectorTest, GetElementOr) {
 
607
  Vector<char> a;
 
608
  EXPECT_EQ('x', a.GetElementOr(0, 'x'));
 
609
 
 
610
  a.PushBack('a');
 
611
  a.PushBack('b');
 
612
  EXPECT_EQ('a', a.GetElementOr(0, 'x'));
 
613
  EXPECT_EQ('b', a.GetElementOr(1, 'x'));
 
614
  EXPECT_EQ('x', a.GetElementOr(-2, 'x'));
 
615
  EXPECT_EQ('x', a.GetElementOr(2, 'x'));
 
616
}
 
617
 
 
618
// Tests Vector::Erase().
 
619
TEST(VectorDeathTest, Erase) {
 
620
  Vector<int> a;
 
621
 
 
622
  // Tests erasing from an empty vector.
 
623
  EXPECT_DEATH_IF_SUPPORTED(
 
624
      a.Erase(0),
 
625
      "Invalid Vector index 0: must be in range \\[0, -1\\]\\.");
 
626
 
 
627
  // Tests erasing from a singleton vector.
 
628
  a.PushBack(0);
 
629
 
 
630
  a.Erase(0);
 
631
  EXPECT_EQ(0, a.size());
 
632
 
 
633
  // Tests Erase parameters beyond the bounds of the vector.
 
634
  Vector<int> a1;
 
635
  a1.PushBack(0);
 
636
  a1.PushBack(1);
 
637
  a1.PushBack(2);
 
638
 
 
639
  EXPECT_DEATH_IF_SUPPORTED(
 
640
      a1.Erase(3),
 
641
      "Invalid Vector index 3: must be in range \\[0, 2\\]\\.");
 
642
  EXPECT_DEATH_IF_SUPPORTED(
 
643
      a1.Erase(-1),
 
644
      "Invalid Vector index -1: must be in range \\[0, 2\\]\\.");
 
645
 
 
646
  // Tests erasing at the end of the vector.
 
647
  Vector<int> a2;
 
648
  a2.PushBack(0);
 
649
  a2.PushBack(1);
 
650
  a2.PushBack(2);
 
651
 
 
652
  a2.Erase(2);
 
653
  ASSERT_EQ(2, a2.size());
 
654
  EXPECT_EQ(0, a2.GetElement(0));
 
655
  EXPECT_EQ(1, a2.GetElement(1));
 
656
 
 
657
  // Tests erasing in the middle of the vector.
 
658
  Vector<int> a3;
 
659
  a3.PushBack(0);
 
660
  a3.PushBack(1);
 
661
  a3.PushBack(2);
 
662
 
 
663
  a3.Erase(1);
 
664
  ASSERT_EQ(2, a3.size());
 
665
  EXPECT_EQ(0, a3.GetElement(0));
 
666
  EXPECT_EQ(2, a3.GetElement(1));
 
667
 
 
668
  // Tests erasing at the beginning of the vector.
 
669
  Vector<int> a4;
 
670
  a4.PushBack(0);
 
671
  a4.PushBack(1);
 
672
  a4.PushBack(2);
 
673
 
 
674
  a4.Erase(0);
 
675
  ASSERT_EQ(2, a4.size());
 
676
  EXPECT_EQ(1, a4.GetElement(0));
 
677
  EXPECT_EQ(2, a4.GetElement(1));
 
678
}
 
679
 
 
680
// Tests the GetElement accessor.
 
681
TEST(ListDeathTest, GetElement) {
 
682
  Vector<int> a;
 
683
  a.PushBack(0);
 
684
  a.PushBack(1);
 
685
  a.PushBack(2);
 
686
 
 
687
  EXPECT_EQ(0, a.GetElement(0));
 
688
  EXPECT_EQ(1, a.GetElement(1));
 
689
  EXPECT_EQ(2, a.GetElement(2));
 
690
  EXPECT_DEATH_IF_SUPPORTED(
 
691
      a.GetElement(3),
 
692
      "Invalid Vector index 3: must be in range \\[0, 2\\]\\.");
 
693
  EXPECT_DEATH_IF_SUPPORTED(
 
694
      a.GetElement(-1),
 
695
      "Invalid Vector index -1: must be in range \\[0, 2\\]\\.");
 
696
}
537
697
 
538
698
// Tests the String class.
539
699
 
784
944
  EXPECT_STREQ("str", ansi);
785
945
  delete [] ansi;
786
946
  const WCHAR* utf16 = String::AnsiToUtf16("str");
787
 
  EXPECT_TRUE(wcsncmp(L"str", utf16, 3) == 0);
 
947
  EXPECT_EQ(0, wcsncmp(L"str", utf16, 3));
788
948
  delete [] utf16;
789
949
}
790
950
 
793
953
  EXPECT_STREQ(".:\\ \"*?", ansi);
794
954
  delete [] ansi;
795
955
  const WCHAR* utf16 = String::AnsiToUtf16(".:\\ \"*?");
796
 
  EXPECT_TRUE(wcsncmp(L".:\\ \"*?", utf16, 3) == 0);
 
956
  EXPECT_EQ(0, wcsncmp(L".:\\ \"*?", utf16, 3));
797
957
  delete [] utf16;
798
958
}
799
959
#endif  // _WIN32_WCE
827
987
}
828
988
 
829
989
class ScopedFakeTestPartResultReporterTest : public Test {
830
 
 protected:
 
990
 public:  // Must be public and not protected due to a bug in g++ 3.4.2.
831
991
  enum FailureMode {
832
992
    FATAL_FAILURE,
833
993
    NONFATAL_FAILURE
1037
1197
 
1038
1198
#endif  // GTEST_IS_THREADSAFE && GTEST_HAS_PTHREAD
1039
1199
 
 
1200
// Tests the TestProperty class.
 
1201
 
 
1202
TEST(TestPropertyTest, ConstructorWorks) {
 
1203
  const TestProperty property("key", "value");
 
1204
  EXPECT_STREQ("key", property.key());
 
1205
  EXPECT_STREQ("value", property.value());
 
1206
}
 
1207
 
 
1208
TEST(TestPropertyTest, SetValue) {
 
1209
  TestProperty property("key", "value_1");
 
1210
  EXPECT_STREQ("key", property.key());
 
1211
  property.SetValue("value_2");
 
1212
  EXPECT_STREQ("key", property.key());
 
1213
  EXPECT_STREQ("value_2", property.value());
 
1214
}
 
1215
 
 
1216
// Tests the TestPartResult class.
 
1217
 
 
1218
TEST(TestPartResultTest, ConstructorWorks) {
 
1219
  Message message;
 
1220
  message << "something is terribly wrong";
 
1221
  message << static_cast<const char*>(testing::internal::kStackTraceMarker);
 
1222
  message << "some unimportant stack trace";
 
1223
 
 
1224
  const TestPartResult result(TPRT_NONFATAL_FAILURE,
 
1225
                              "some_file.cc",
 
1226
                              42,
 
1227
                              message.GetString().c_str());
 
1228
 
 
1229
  EXPECT_EQ(TPRT_NONFATAL_FAILURE, result.type());
 
1230
  EXPECT_STREQ("some_file.cc", result.file_name());
 
1231
  EXPECT_EQ(42, result.line_number());
 
1232
  EXPECT_STREQ(message.GetString().c_str(), result.message());
 
1233
  EXPECT_STREQ("something is terribly wrong", result.summary());
 
1234
}
 
1235
 
 
1236
TEST(TestPartResultTest, ResultAccessorsWork) {
 
1237
  const TestPartResult success(TPRT_SUCCESS, "file.cc", 42, "message");
 
1238
  EXPECT_TRUE(success.passed());
 
1239
  EXPECT_FALSE(success.failed());
 
1240
  EXPECT_FALSE(success.nonfatally_failed());
 
1241
  EXPECT_FALSE(success.fatally_failed());
 
1242
 
 
1243
  const TestPartResult nonfatal_failure(TPRT_NONFATAL_FAILURE,
 
1244
                                        "file.cc",
 
1245
                                        42,
 
1246
                                        "message");
 
1247
  EXPECT_FALSE(nonfatal_failure.passed());
 
1248
  EXPECT_TRUE(nonfatal_failure.failed());
 
1249
  EXPECT_TRUE(nonfatal_failure.nonfatally_failed());
 
1250
  EXPECT_FALSE(nonfatal_failure.fatally_failed());
 
1251
 
 
1252
  const TestPartResult fatal_failure(TPRT_FATAL_FAILURE,
 
1253
                                     "file.cc",
 
1254
                                     42,
 
1255
                                     "message");
 
1256
  EXPECT_FALSE(fatal_failure.passed());
 
1257
  EXPECT_TRUE(fatal_failure.failed());
 
1258
  EXPECT_FALSE(fatal_failure.nonfatally_failed());
 
1259
  EXPECT_TRUE(fatal_failure.fatally_failed());
 
1260
}
 
1261
 
1040
1262
// Tests the TestResult class
1041
1263
 
1042
1264
// The test fixture for testing TestResult.
1043
1265
class TestResultTest : public Test {
1044
1266
 protected:
1045
 
  typedef List<TestPartResult> TPRList;
 
1267
  typedef Vector<TestPartResult> TPRVector;
1046
1268
 
1047
1269
  // We make use of 2 TestPartResult objects,
1048
1270
  TestPartResult * pr1, * pr2;
1065
1287
    r2 = new TestResult();
1066
1288
 
1067
1289
    // In order to test TestResult, we need to modify its internal
1068
 
    // state, in particular the TestPartResult list it holds.
1069
 
    // test_part_results() returns a const reference to this list.
 
1290
    // state, in particular the TestPartResult Vector it holds.
 
1291
    // test_part_results() returns a const reference to this Vector.
1070
1292
    // We cast it to a non-const object s.t. it can be modified (yes,
1071
1293
    // this is a hack).
1072
 
    TPRList * list1, * list2;
1073
 
    list1 = const_cast<List<TestPartResult> *>(
1074
 
        & r1->test_part_results());
1075
 
    list2 = const_cast<List<TestPartResult> *>(
1076
 
        & r2->test_part_results());
 
1294
    TPRVector* results1 = const_cast<Vector<TestPartResult> *>(
 
1295
        &TestResultAccessor::test_part_results(*r1));
 
1296
    TPRVector* results2 = const_cast<Vector<TestPartResult> *>(
 
1297
        &TestResultAccessor::test_part_results(*r2));
1077
1298
 
1078
1299
    // r0 is an empty TestResult.
1079
1300
 
1080
1301
    // r1 contains a single SUCCESS TestPartResult.
1081
 
    list1->PushBack(*pr1);
 
1302
    results1->PushBack(*pr1);
1082
1303
 
1083
1304
    // r2 contains a SUCCESS, and a FAILURE.
1084
 
    list2->PushBack(*pr1);
1085
 
    list2->PushBack(*pr2);
 
1305
    results2->PushBack(*pr1);
 
1306
    results2->PushBack(*pr2);
1086
1307
  }
1087
1308
 
1088
1309
  virtual void TearDown() {
1093
1314
    delete r1;
1094
1315
    delete r2;
1095
1316
  }
 
1317
 
 
1318
  // Helper that compares two two TestPartResults.
 
1319
  static void CompareTestPartResult(const TestPartResult& expected,
 
1320
                                    const TestPartResult& actual) {
 
1321
    EXPECT_EQ(expected.type(), actual.type());
 
1322
    EXPECT_STREQ(expected.file_name(), actual.file_name());
 
1323
    EXPECT_EQ(expected.line_number(), actual.line_number());
 
1324
    EXPECT_STREQ(expected.summary(), actual.summary());
 
1325
    EXPECT_STREQ(expected.message(), actual.message());
 
1326
    EXPECT_EQ(expected.passed(), actual.passed());
 
1327
    EXPECT_EQ(expected.failed(), actual.failed());
 
1328
    EXPECT_EQ(expected.nonfatally_failed(), actual.nonfatally_failed());
 
1329
    EXPECT_EQ(expected.fatally_failed(), actual.fatally_failed());
 
1330
  }
1096
1331
};
1097
1332
 
1098
 
// Tests TestResult::test_part_results()
1099
 
TEST_F(TestResultTest, test_part_results) {
1100
 
  ASSERT_EQ(0u, r0->test_part_results().size());
1101
 
  ASSERT_EQ(1u, r1->test_part_results().size());
1102
 
  ASSERT_EQ(2u, r2->test_part_results().size());
1103
 
}
1104
 
 
1105
 
// Tests TestResult::successful_part_count()
1106
 
TEST_F(TestResultTest, successful_part_count) {
1107
 
  ASSERT_EQ(0u, r0->successful_part_count());
1108
 
  ASSERT_EQ(1u, r1->successful_part_count());
1109
 
  ASSERT_EQ(1u, r2->successful_part_count());
1110
 
}
1111
 
 
1112
 
// Tests TestResult::failed_part_count()
1113
 
TEST_F(TestResultTest, failed_part_count) {
1114
 
  ASSERT_EQ(0u, r0->failed_part_count());
1115
 
  ASSERT_EQ(0u, r1->failed_part_count());
1116
 
  ASSERT_EQ(1u, r2->failed_part_count());
1117
 
}
1118
 
 
1119
 
// Tests testing::internal::GetFailedPartCount().
1120
 
TEST_F(TestResultTest, GetFailedPartCount) {
1121
 
  ASSERT_EQ(0u, GetFailedPartCount(r0));
1122
 
  ASSERT_EQ(0u, GetFailedPartCount(r1));
1123
 
  ASSERT_EQ(1u, GetFailedPartCount(r2));
1124
 
}
1125
 
 
1126
 
// Tests TestResult::total_part_count()
 
1333
// Tests TestResult::total_part_count().
1127
1334
TEST_F(TestResultTest, total_part_count) {
1128
 
  ASSERT_EQ(0u, r0->total_part_count());
1129
 
  ASSERT_EQ(1u, r1->total_part_count());
1130
 
  ASSERT_EQ(2u, r2->total_part_count());
 
1335
  ASSERT_EQ(0, r0->total_part_count());
 
1336
  ASSERT_EQ(1, r1->total_part_count());
 
1337
  ASSERT_EQ(2, r2->total_part_count());
1131
1338
}
1132
1339
 
1133
 
// Tests TestResult::Passed()
 
1340
// Tests TestResult::Passed().
1134
1341
TEST_F(TestResultTest, Passed) {
1135
1342
  ASSERT_TRUE(r0->Passed());
1136
1343
  ASSERT_TRUE(r1->Passed());
1137
1344
  ASSERT_FALSE(r2->Passed());
1138
1345
}
1139
1346
 
1140
 
// Tests TestResult::Failed()
 
1347
// Tests TestResult::Failed().
1141
1348
TEST_F(TestResultTest, Failed) {
1142
1349
  ASSERT_FALSE(r0->Failed());
1143
1350
  ASSERT_FALSE(r1->Failed());
1144
1351
  ASSERT_TRUE(r2->Failed());
1145
1352
}
1146
1353
 
1147
 
// Tests TestResult::test_properties() has no properties when none are added.
 
1354
// Tests TestResult::GetTestPartResult().
 
1355
 
 
1356
typedef TestResultTest TestResultDeathTest;
 
1357
 
 
1358
TEST_F(TestResultDeathTest, GetTestPartResult) {
 
1359
  CompareTestPartResult(*pr1, r2->GetTestPartResult(0));
 
1360
  CompareTestPartResult(*pr2, r2->GetTestPartResult(1));
 
1361
  EXPECT_DEATH_IF_SUPPORTED(
 
1362
      r2->GetTestPartResult(2),
 
1363
      "Invalid Vector index 2: must be in range \\[0, 1\\]\\.");
 
1364
  EXPECT_DEATH_IF_SUPPORTED(
 
1365
      r2->GetTestPartResult(-1),
 
1366
      "Invalid Vector index -1: must be in range \\[0, 1\\]\\.");
 
1367
}
 
1368
 
 
1369
// Tests TestResult has no properties when none are added.
1148
1370
TEST(TestResultPropertyTest, NoPropertiesFoundWhenNoneAreAdded) {
1149
1371
  TestResult test_result;
1150
 
  ASSERT_EQ(0u, test_result.test_properties().size());
 
1372
  ASSERT_EQ(0, test_result.test_property_count());
1151
1373
}
1152
1374
 
1153
 
// Tests TestResult::test_properties() has the expected property when added.
 
1375
// Tests TestResult has the expected property when added.
1154
1376
TEST(TestResultPropertyTest, OnePropertyFoundWhenAdded) {
1155
1377
  TestResult test_result;
1156
1378
  TestProperty property("key_1", "1");
1157
 
  test_result.RecordProperty(property);
1158
 
  const List<TestProperty>& properties = test_result.test_properties();
1159
 
  ASSERT_EQ(1u, properties.size());
1160
 
  TestProperty actual_property = properties.Head()->element();
 
1379
  TestResultAccessor::RecordProperty(&test_result, property);
 
1380
  ASSERT_EQ(1, test_result.test_property_count());
 
1381
  const TestProperty& actual_property = test_result.GetTestProperty(0);
1161
1382
  EXPECT_STREQ("key_1", actual_property.key());
1162
1383
  EXPECT_STREQ("1", actual_property.value());
1163
1384
}
1164
1385
 
1165
 
// Tests TestResult::test_properties() has multiple properties when added.
 
1386
// Tests TestResult has multiple properties when added.
1166
1387
TEST(TestResultPropertyTest, MultiplePropertiesFoundWhenAdded) {
1167
1388
  TestResult test_result;
1168
1389
  TestProperty property_1("key_1", "1");
1169
1390
  TestProperty property_2("key_2", "2");
1170
 
  test_result.RecordProperty(property_1);
1171
 
  test_result.RecordProperty(property_2);
1172
 
  const List<TestProperty>& properties = test_result.test_properties();
1173
 
  ASSERT_EQ(2u, properties.size());
1174
 
  TestProperty actual_property_1 = properties.Head()->element();
 
1391
  TestResultAccessor::RecordProperty(&test_result, property_1);
 
1392
  TestResultAccessor::RecordProperty(&test_result, property_2);
 
1393
  ASSERT_EQ(2, test_result.test_property_count());
 
1394
  const TestProperty& actual_property_1 = test_result.GetTestProperty(0);
1175
1395
  EXPECT_STREQ("key_1", actual_property_1.key());
1176
1396
  EXPECT_STREQ("1", actual_property_1.value());
1177
1397
 
1178
 
  TestProperty actual_property_2 = properties.Last()->element();
 
1398
  const TestProperty& actual_property_2 = test_result.GetTestProperty(1);
1179
1399
  EXPECT_STREQ("key_2", actual_property_2.key());
1180
1400
  EXPECT_STREQ("2", actual_property_2.value());
1181
1401
}
1182
1402
 
1183
 
// Tests TestResult::test_properties() overrides values for duplicate keys.
 
1403
// Tests TestResult::RecordProperty() overrides values for duplicate keys.
1184
1404
TEST(TestResultPropertyTest, OverridesValuesForDuplicateKeys) {
1185
1405
  TestResult test_result;
1186
1406
  TestProperty property_1_1("key_1", "1");
1187
1407
  TestProperty property_2_1("key_2", "2");
1188
1408
  TestProperty property_1_2("key_1", "12");
1189
1409
  TestProperty property_2_2("key_2", "22");
1190
 
  test_result.RecordProperty(property_1_1);
1191
 
  test_result.RecordProperty(property_2_1);
1192
 
  test_result.RecordProperty(property_1_2);
1193
 
  test_result.RecordProperty(property_2_2);
 
1410
  TestResultAccessor::RecordProperty(&test_result, property_1_1);
 
1411
  TestResultAccessor::RecordProperty(&test_result, property_2_1);
 
1412
  TestResultAccessor::RecordProperty(&test_result, property_1_2);
 
1413
  TestResultAccessor::RecordProperty(&test_result, property_2_2);
1194
1414
 
1195
 
  const List<TestProperty>& properties = test_result.test_properties();
1196
 
  ASSERT_EQ(2u, properties.size());
1197
 
  TestProperty actual_property_1 = properties.Head()->element();
 
1415
  ASSERT_EQ(2, test_result.test_property_count());
 
1416
  const TestProperty& actual_property_1 = test_result.GetTestProperty(0);
1198
1417
  EXPECT_STREQ("key_1", actual_property_1.key());
1199
1418
  EXPECT_STREQ("12", actual_property_1.value());
1200
1419
 
1201
 
  TestProperty actual_property_2 = properties.Last()->element();
 
1420
  const TestProperty& actual_property_2 = test_result.GetTestProperty(1);
1202
1421
  EXPECT_STREQ("key_2", actual_property_2.key());
1203
1422
  EXPECT_STREQ("22", actual_property_2.value());
1204
1423
}
1205
1424
 
 
1425
// Tests TestResult::GetTestProperty().
 
1426
TEST(TestResultPropertyDeathTest, GetTestProperty) {
 
1427
  TestResult test_result;
 
1428
  TestProperty property_1("key_1", "1");
 
1429
  TestProperty property_2("key_2", "2");
 
1430
  TestProperty property_3("key_3", "3");
 
1431
  TestResultAccessor::RecordProperty(&test_result, property_1);
 
1432
  TestResultAccessor::RecordProperty(&test_result, property_2);
 
1433
  TestResultAccessor::RecordProperty(&test_result, property_3);
 
1434
 
 
1435
  const TestProperty& fetched_property_1 = test_result.GetTestProperty(0);
 
1436
  const TestProperty& fetched_property_2 = test_result.GetTestProperty(1);
 
1437
  const TestProperty& fetched_property_3 = test_result.GetTestProperty(2);
 
1438
 
 
1439
  EXPECT_STREQ("key_1", fetched_property_1.key());
 
1440
  EXPECT_STREQ("1", fetched_property_1.value());
 
1441
 
 
1442
  EXPECT_STREQ("key_2", fetched_property_2.key());
 
1443
  EXPECT_STREQ("2", fetched_property_2.value());
 
1444
 
 
1445
  EXPECT_STREQ("key_3", fetched_property_3.key());
 
1446
  EXPECT_STREQ("3", fetched_property_3.value());
 
1447
 
 
1448
  EXPECT_DEATH_IF_SUPPORTED(
 
1449
      test_result.GetTestProperty(3),
 
1450
      "Invalid Vector index 3: must be in range \\[0, 2\\]\\.");
 
1451
  EXPECT_DEATH_IF_SUPPORTED(
 
1452
      test_result.GetTestProperty(-1),
 
1453
      "Invalid Vector index -1: must be in range \\[0, 2\\]\\.");
 
1454
}
 
1455
 
1206
1456
// When a property using a reserved key is supplied to this function, it tests
1207
1457
// that a non-fatal failure is added, a fatal failure is not added, and that the
1208
1458
// property is not recorded.
1209
1459
void ExpectNonFatalFailureRecordingPropertyWithReservedKey(const char* key) {
1210
1460
  TestResult test_result;
1211
1461
  TestProperty property(key, "1");
1212
 
  EXPECT_NONFATAL_FAILURE(test_result.RecordProperty(property), "Reserved key");
1213
 
  ASSERT_TRUE(test_result.test_properties().IsEmpty()) << "Not recorded";
 
1462
  EXPECT_NONFATAL_FAILURE(
 
1463
      TestResultAccessor::RecordProperty(&test_result, property),
 
1464
      "Reserved key");
 
1465
  ASSERT_EQ(0, test_result.test_property_count()) << "Not recorded";
1214
1466
}
1215
1467
 
1216
1468
// Attempting to recording a property with the Reserved literal "name"
1256
1508
    GTEST_FLAG(list_tests) = false;
1257
1509
    GTEST_FLAG(output) = "";
1258
1510
    GTEST_FLAG(print_time) = true;
 
1511
    GTEST_FLAG(random_seed) = 0;
1259
1512
    GTEST_FLAG(repeat) = 1;
 
1513
    GTEST_FLAG(shuffle) = false;
1260
1514
    GTEST_FLAG(throw_on_failure) = false;
1261
1515
  }
1262
1516
 
1279
1533
    EXPECT_FALSE(GTEST_FLAG(list_tests));
1280
1534
    EXPECT_STREQ("", GTEST_FLAG(output).c_str());
1281
1535
    EXPECT_TRUE(GTEST_FLAG(print_time));
 
1536
    EXPECT_EQ(0, GTEST_FLAG(random_seed));
1282
1537
    EXPECT_EQ(1, GTEST_FLAG(repeat));
 
1538
    EXPECT_FALSE(GTEST_FLAG(shuffle));
1283
1539
    EXPECT_FALSE(GTEST_FLAG(throw_on_failure));
1284
1540
 
1285
1541
    GTEST_FLAG(also_run_disabled_tests) = true;
1291
1547
    GTEST_FLAG(list_tests) = true;
1292
1548
    GTEST_FLAG(output) = "xml:foo.xml";
1293
1549
    GTEST_FLAG(print_time) = false;
 
1550
    GTEST_FLAG(random_seed) = 1;
1294
1551
    GTEST_FLAG(repeat) = 100;
 
1552
    GTEST_FLAG(shuffle) = true;
1295
1553
    GTEST_FLAG(throw_on_failure) = true;
1296
1554
  }
1297
1555
 private:
1454
1712
 
1455
1713
// Tests that Int32FromEnvOrDie() parses the value of the var or
1456
1714
// returns the correct default.
 
1715
// Environment variables are not supported on Windows CE.
 
1716
#ifndef _WIN32_WCE
1457
1717
TEST(Int32FromEnvOrDieTest, ParsesAndReturnsValidValue) {
1458
1718
  EXPECT_EQ(333, Int32FromEnvOrDie(GTEST_FLAG_PREFIX_UPPER_ "UnsetVar", 333));
1459
1719
  SetEnv(GTEST_FLAG_PREFIX_UPPER_ "UnsetVar", "123");
1461
1721
  SetEnv(GTEST_FLAG_PREFIX_UPPER_ "UnsetVar", "-123");
1462
1722
  EXPECT_EQ(-123, Int32FromEnvOrDie(GTEST_FLAG_PREFIX_UPPER_ "UnsetVar", 333));
1463
1723
}
1464
 
 
1465
 
#if GTEST_HAS_DEATH_TEST
 
1724
#endif  // _WIN32_WCE
1466
1725
 
1467
1726
// Tests that Int32FromEnvOrDie() aborts with an error message
1468
1727
// if the variable is not an Int32.
1469
1728
TEST(Int32FromEnvOrDieDeathTest, AbortsOnFailure) {
1470
1729
  SetEnv(GTEST_FLAG_PREFIX_UPPER_ "VAR", "xxx");
1471
 
  EXPECT_DEATH({Int32FromEnvOrDie(GTEST_FLAG_PREFIX_UPPER_ "VAR", 123);},
1472
 
               ".*");
 
1730
  EXPECT_DEATH_IF_SUPPORTED(
 
1731
      Int32FromEnvOrDie(GTEST_FLAG_PREFIX_UPPER_ "VAR", 123),
 
1732
      ".*");
1473
1733
}
1474
1734
 
1475
1735
// Tests that Int32FromEnvOrDie() aborts with an error message
1476
1736
// if the variable cannot be represnted by an Int32.
1477
1737
TEST(Int32FromEnvOrDieDeathTest, AbortsOnInt32Overflow) {
1478
1738
  SetEnv(GTEST_FLAG_PREFIX_UPPER_ "VAR", "1234567891234567891234");
1479
 
  EXPECT_DEATH({Int32FromEnvOrDie(GTEST_FLAG_PREFIX_UPPER_ "VAR", 123);},
1480
 
               ".*");
 
1739
  EXPECT_DEATH_IF_SUPPORTED(
 
1740
      Int32FromEnvOrDie(GTEST_FLAG_PREFIX_UPPER_ "VAR", 123),
 
1741
      ".*");
1481
1742
}
1482
1743
 
1483
 
#endif  // GTEST_HAS_DEATH_TEST
1484
 
 
1485
 
 
1486
1744
// Tests that ShouldRunTestOnShard() selects all tests
1487
1745
// where there is 1 shard.
1488
1746
TEST(ShouldRunTestOnShardTest, IsPartitionWhenThereIsOneShard) {
1529
1787
 
1530
1788
// Tests that sharding is enabled if total_shards > 1 and
1531
1789
// we are not in a death test subprocess.
 
1790
// Environment variables are not supported on Windows CE.
 
1791
#ifndef _WIN32_WCE
1532
1792
TEST_F(ShouldShardTest, WorksWhenShardEnvVarsAreValid) {
1533
1793
  SetEnv(index_var_, "4");
1534
1794
  SetEnv(total_var_, "22");
1545
1805
  EXPECT_TRUE(ShouldShard(total_var_, index_var_, false));
1546
1806
  EXPECT_FALSE(ShouldShard(total_var_, index_var_, true));
1547
1807
}
1548
 
 
1549
 
#if GTEST_HAS_DEATH_TEST
 
1808
#endif  // _WIN32_WCE
1550
1809
 
1551
1810
// Tests that we exit in error if the sharding values are not valid.
1552
 
TEST_F(ShouldShardTest, AbortsWhenShardingEnvVarsAreInvalid) {
 
1811
 
 
1812
typedef ShouldShardTest ShouldShardDeathTest;
 
1813
 
 
1814
TEST_F(ShouldShardDeathTest, AbortsWhenShardingEnvVarsAreInvalid) {
1553
1815
  SetEnv(index_var_, "4");
1554
1816
  SetEnv(total_var_, "4");
1555
 
  EXPECT_DEATH({ShouldShard(total_var_, index_var_, false);},
1556
 
               ".*");
 
1817
  EXPECT_DEATH_IF_SUPPORTED(ShouldShard(total_var_, index_var_, false), ".*");
1557
1818
 
1558
1819
  SetEnv(index_var_, "4");
1559
1820
  SetEnv(total_var_, "-2");
1560
 
  EXPECT_DEATH({ShouldShard(total_var_, index_var_, false);},
1561
 
               ".*");
 
1821
  EXPECT_DEATH_IF_SUPPORTED(ShouldShard(total_var_, index_var_, false), ".*");
1562
1822
 
1563
1823
  SetEnv(index_var_, "5");
1564
1824
  SetEnv(total_var_, "");
1565
 
  EXPECT_DEATH({ShouldShard(total_var_, index_var_, false);},
1566
 
               ".*");
 
1825
  EXPECT_DEATH_IF_SUPPORTED(ShouldShard(total_var_, index_var_, false), ".*");
1567
1826
 
1568
1827
  SetEnv(index_var_, "");
1569
1828
  SetEnv(total_var_, "5");
1570
 
  EXPECT_DEATH({ShouldShard(total_var_, index_var_, false);},
1571
 
               ".*");
 
1829
  EXPECT_DEATH_IF_SUPPORTED(ShouldShard(total_var_, index_var_, false), ".*");
1572
1830
}
1573
1831
 
1574
 
#endif  // GTEST_HAS_DEATH_TEST
1575
 
 
1576
1832
// Tests that ShouldRunTestOnShard is a partition when 5
1577
1833
// shards are used.
1578
1834
TEST(ShouldRunTestOnShardTest, IsPartitionWhenThereAreFiveShards) {
2701
2957
// Tests that assertion macros evaluate their arguments exactly once.
2702
2958
 
2703
2959
class SingleEvaluationTest : public Test {
2704
 
 public:
 
2960
 public:  // Must be public and not protected due to a bug in g++ 3.4.2.
2705
2961
  // This helper function is needed by the FailedASSERT_STREQ test
2706
2962
  // below.  It's public to work around C++Builder's bug with scoping local
2707
2963
  // classes.
3063
3319
TEST(AssertionTest, ASSERT_EQ_NULL) {
3064
3320
  // A success.
3065
3321
  const char* p = NULL;
 
3322
  // Some older GCC versions may issue a spurious waring in this or the next
 
3323
  // assertion statement. This warning should not be suppressed with
 
3324
  // static_cast since the test verifies the ability to use bare NULL as the
 
3325
  // expected parameter to the macro.
3066
3326
  ASSERT_EQ(NULL, p);
3067
3327
 
3068
3328
  // A failure.
3398
3658
  if (true)
3399
3659
    EXPECT_FALSE(false);
3400
3660
  else
3401
 
    ;
 
3661
    ;  // NOLINT
3402
3662
 
3403
3663
  if (false)
3404
3664
    ASSERT_LT(1, 3);
3405
3665
 
3406
3666
  if (false)
3407
 
    ;
 
3667
    ;  // NOLINT
3408
3668
  else
3409
3669
    EXPECT_GT(3, 2) << "";
3410
3670
}
3431
3691
  if (true)
3432
3692
    EXPECT_THROW(ThrowAnInteger(), int);
3433
3693
  else
3434
 
    ;
 
3694
    ;  // NOLINT
3435
3695
 
3436
3696
  if (false)
3437
3697
    EXPECT_NO_THROW(ThrowAnInteger());
3439
3699
  if (true)
3440
3700
    EXPECT_NO_THROW(ThrowNothing());
3441
3701
  else
3442
 
    ;
 
3702
    ;  // NOLINT
3443
3703
 
3444
3704
  if (false)
3445
3705
    EXPECT_ANY_THROW(ThrowNothing());
3447
3707
  if (true)
3448
3708
    EXPECT_ANY_THROW(ThrowAnInteger());
3449
3709
  else
3450
 
    ;
 
3710
    ;  // NOLINT
3451
3711
}
3452
3712
#endif  // GTEST_HAS_EXCEPTIONS
3453
3713
 
3456
3716
    EXPECT_NO_FATAL_FAILURE(FAIL()) << "This should never be executed. "
3457
3717
                                    << "It's a compilation test only.";
3458
3718
  else
3459
 
    ;
 
3719
    ;  // NOLINT
3460
3720
 
3461
3721
  if (false)
3462
3722
    ASSERT_NO_FATAL_FAILURE(FAIL()) << "";
3463
3723
  else
3464
 
    ;
 
3724
    ;  // NOLINT
3465
3725
 
3466
3726
  if (true)
3467
3727
    EXPECT_NO_FATAL_FAILURE(SUCCEED());
3468
3728
  else
3469
 
    ;
 
3729
    ;  // NOLINT
3470
3730
 
3471
3731
  if (false)
3472
 
    ;
 
3732
    ;  // NOLINT
3473
3733
  else
3474
3734
    ASSERT_NO_FATAL_FAILURE(SUCCEED());
3475
3735
}
3518
3778
 
3519
3779
}  // namespace
3520
3780
 
3521
 
// Returns the number of successful parts in the current test.
3522
 
static size_t GetSuccessfulPartCount() {
3523
 
  return UnitTest::GetInstance()->impl()->current_test_result()->
3524
 
    successful_part_count();
3525
 
}
3526
 
 
3527
3781
namespace testing {
3528
3782
 
3529
3783
// Tests that Google Test tracks SUCCEED*.
3530
3784
TEST(SuccessfulAssertionTest, SUCCEED) {
3531
3785
  SUCCEED();
3532
3786
  SUCCEED() << "OK";
3533
 
  EXPECT_EQ(2u, GetSuccessfulPartCount());
 
3787
  EXPECT_EQ(2, GetUnitTestImpl()->current_test_result()->total_part_count());
3534
3788
}
3535
3789
 
3536
3790
// Tests that Google Test doesn't track successful EXPECT_*.
3537
3791
TEST(SuccessfulAssertionTest, EXPECT) {
3538
3792
  EXPECT_TRUE(true);
3539
 
  EXPECT_EQ(0u, GetSuccessfulPartCount());
 
3793
  EXPECT_EQ(0, GetUnitTestImpl()->current_test_result()->total_part_count());
3540
3794
}
3541
3795
 
3542
3796
// Tests that Google Test doesn't track successful EXPECT_STR*.
3543
3797
TEST(SuccessfulAssertionTest, EXPECT_STR) {
3544
3798
  EXPECT_STREQ("", "");
3545
 
  EXPECT_EQ(0u, GetSuccessfulPartCount());
 
3799
  EXPECT_EQ(0, GetUnitTestImpl()->current_test_result()->total_part_count());
3546
3800
}
3547
3801
 
3548
3802
// Tests that Google Test doesn't track successful ASSERT_*.
3549
3803
TEST(SuccessfulAssertionTest, ASSERT) {
3550
3804
  ASSERT_TRUE(true);
3551
 
  EXPECT_EQ(0u, GetSuccessfulPartCount());
 
3805
  EXPECT_EQ(0, GetUnitTestImpl()->current_test_result()->total_part_count());
3552
3806
}
3553
3807
 
3554
3808
// Tests that Google Test doesn't track successful ASSERT_STR*.
3555
3809
TEST(SuccessfulAssertionTest, ASSERT_STR) {
3556
3810
  ASSERT_STREQ("", "");
3557
 
  EXPECT_EQ(0u, GetSuccessfulPartCount());
 
3811
  EXPECT_EQ(0, GetUnitTestImpl()->current_test_result()->total_part_count());
3558
3812
}
3559
3813
 
3560
3814
}  // namespace testing
3616
3870
TEST(ExpectTest, EXPECT_EQ_NULL) {
3617
3871
  // A success.
3618
3872
  const char* p = NULL;
 
3873
  // Some older GCC versions may issue a spurious waring in this or the next
 
3874
  // assertion statement. This warning should not be suppressed with
 
3875
  // static_cast since the test verifies the ability to use bare NULL as the
 
3876
  // expected parameter to the macro.
3619
3877
  EXPECT_EQ(NULL, p);
3620
3878
 
3621
3879
  // A failure.
4331
4589
 
4332
4590
class TestInfoTest : public Test {
4333
4591
 protected:
4334
 
  static TestInfo * GetTestInfo(const char* test_name) {
4335
 
    return UnitTest::GetInstance()->impl()->
4336
 
      GetTestCase("TestInfoTest", "", NULL, NULL)->
4337
 
        GetTestInfo(test_name);
 
4592
  static const TestInfo* GetTestInfo(const char* test_name) {
 
4593
    const TestCase* const test_case = GetUnitTestImpl()->
 
4594
        GetTestCase("TestInfoTest", "", NULL, NULL);
 
4595
 
 
4596
    for (int i = 0; i < test_case->total_test_count(); ++i) {
 
4597
      const TestInfo* const test_info = test_case->GetTestInfo(i);
 
4598
      if (strcmp(test_name, test_info->name()) == 0)
 
4599
        return test_info;
 
4600
    }
 
4601
    return NULL;
4338
4602
  }
4339
4603
 
4340
4604
  static const TestResult* GetTestResult(
4345
4609
 
4346
4610
// Tests TestInfo::test_case_name() and TestInfo::name().
4347
4611
TEST_F(TestInfoTest, Names) {
4348
 
  TestInfo * const test_info = GetTestInfo("Names");
 
4612
  const TestInfo* const test_info = GetTestInfo("Names");
4349
4613
 
4350
4614
  ASSERT_STREQ("TestInfoTest", test_info->test_case_name());
4351
4615
  ASSERT_STREQ("Names", test_info->name());
4353
4617
 
4354
4618
// Tests TestInfo::result().
4355
4619
TEST_F(TestInfoTest, result) {
4356
 
  TestInfo * const test_info = GetTestInfo("result");
 
4620
  const TestInfo* const test_info = GetTestInfo("result");
4357
4621
 
4358
4622
  // Initially, there is no TestPartResult for this test.
4359
 
  ASSERT_EQ(0u, GetTestResult(test_info)->total_part_count());
 
4623
  ASSERT_EQ(0, GetTestResult(test_info)->total_part_count());
4360
4624
 
4361
4625
  // After the previous assertion, there is still none.
4362
 
  ASSERT_EQ(0u, GetTestResult(test_info)->total_part_count());
 
4626
  ASSERT_EQ(0, GetTestResult(test_info)->total_part_count());
4363
4627
}
4364
4628
 
4365
4629
// Tests setting up and tearing down a test case.
4438
4702
            list_tests(false),
4439
4703
            output(""),
4440
4704
            print_time(true),
 
4705
            random_seed(0),
4441
4706
            repeat(1),
 
4707
            shuffle(false),
4442
4708
            throw_on_failure(false) {}
4443
4709
 
4444
4710
  // Factory methods.
4507
4773
    return flags;
4508
4774
  }
4509
4775
 
 
4776
  // Creates a Flags struct where the gtest_random_seed flag has
 
4777
  // the given value.
 
4778
  static Flags RandomSeed(Int32 random_seed) {
 
4779
    Flags flags;
 
4780
    flags.random_seed = random_seed;
 
4781
    return flags;
 
4782
  }
 
4783
 
4510
4784
  // Creates a Flags struct where the gtest_repeat flag has the given
4511
4785
  // value.
4512
4786
  static Flags Repeat(Int32 repeat) {
4515
4789
    return flags;
4516
4790
  }
4517
4791
 
 
4792
  // Creates a Flags struct where the gtest_shuffle flag has
 
4793
  // the given value.
 
4794
  static Flags Shuffle(bool shuffle) {
 
4795
    Flags flags;
 
4796
    flags.shuffle = shuffle;
 
4797
    return flags;
 
4798
  }
 
4799
 
4518
4800
  // Creates a Flags struct where the gtest_throw_on_failure flag has
4519
4801
  // the given value.
4520
4802
  static Flags ThrowOnFailure(bool throw_on_failure) {
4532
4814
  bool list_tests;
4533
4815
  const char* output;
4534
4816
  bool print_time;
 
4817
  Int32 random_seed;
4535
4818
  Int32 repeat;
 
4819
  bool shuffle;
4536
4820
  bool throw_on_failure;
4537
4821
};
4538
4822
 
4549
4833
    GTEST_FLAG(list_tests) = false;
4550
4834
    GTEST_FLAG(output) = "";
4551
4835
    GTEST_FLAG(print_time) = true;
 
4836
    GTEST_FLAG(random_seed) = 0;
4552
4837
    GTEST_FLAG(repeat) = 1;
 
4838
    GTEST_FLAG(shuffle) = false;
4553
4839
    GTEST_FLAG(throw_on_failure) = false;
4554
4840
  }
4555
4841
 
4575
4861
    EXPECT_EQ(expected.list_tests, GTEST_FLAG(list_tests));
4576
4862
    EXPECT_STREQ(expected.output, GTEST_FLAG(output).c_str());
4577
4863
    EXPECT_EQ(expected.print_time, GTEST_FLAG(print_time));
 
4864
    EXPECT_EQ(expected.random_seed, GTEST_FLAG(random_seed));
4578
4865
    EXPECT_EQ(expected.repeat, GTEST_FLAG(repeat));
 
4866
    EXPECT_EQ(expected.shuffle, GTEST_FLAG(shuffle));
4579
4867
    EXPECT_EQ(expected.throw_on_failure, GTEST_FLAG(throw_on_failure));
4580
4868
  }
4581
4869
 
4682
4970
}
4683
4971
 
4684
4972
// Tests parsing --gtest_break_on_failure.
4685
 
TEST_F(InitGoogleTestTest, BreakOnFailureNoDef) {
 
4973
TEST_F(InitGoogleTestTest, BreakOnFailureWithoutValue) {
4686
4974
  const char* argv[] = {
4687
4975
    "foo.exe",
4688
4976
    "--gtest_break_on_failure",
4898
5186
  GTEST_TEST_PARSING_FLAGS_(argv, argv2, Flags::ListTests(false));
4899
5187
}
4900
5188
 
4901
 
// Tests parsing --gtest_break_on_failure=F.
 
5189
// Tests parsing --gtest_list_tests=F.
4902
5190
TEST_F(InitGoogleTestTest, ListTestsFalse_F) {
4903
5191
  const char* argv[] = {
4904
5192
    "foo.exe",
5059
5347
  GTEST_TEST_PARSING_FLAGS_(argv, argv2, Flags::PrintTime(false));
5060
5348
}
5061
5349
 
 
5350
// Tests parsing --gtest_random_seed=number
 
5351
TEST_F(InitGoogleTestTest, RandomSeed) {
 
5352
  const char* argv[] = {
 
5353
    "foo.exe",
 
5354
    "--gtest_random_seed=1000",
 
5355
    NULL
 
5356
  };
 
5357
 
 
5358
  const char* argv2[] = {
 
5359
    "foo.exe",
 
5360
    NULL
 
5361
  };
 
5362
 
 
5363
  GTEST_TEST_PARSING_FLAGS_(argv, argv2, Flags::RandomSeed(1000));
 
5364
}
 
5365
 
5062
5366
// Tests parsing --gtest_repeat=number
5063
5367
TEST_F(InitGoogleTestTest, Repeat) {
5064
5368
  const char* argv[] = {
5123
5427
    GTEST_TEST_PARSING_FLAGS_(argv, argv2, Flags::AlsoRunDisabledTests(false));
5124
5428
}
5125
5429
 
 
5430
// Tests parsing --gtest_shuffle.
 
5431
TEST_F(InitGoogleTestTest, ShuffleWithoutValue) {
 
5432
  const char* argv[] = {
 
5433
    "foo.exe",
 
5434
    "--gtest_shuffle",
 
5435
    NULL
 
5436
};
 
5437
 
 
5438
  const char* argv2[] = {
 
5439
    "foo.exe",
 
5440
    NULL
 
5441
  };
 
5442
 
 
5443
  GTEST_TEST_PARSING_FLAGS_(argv, argv2, Flags::Shuffle(true));
 
5444
}
 
5445
 
 
5446
// Tests parsing --gtest_shuffle=0.
 
5447
TEST_F(InitGoogleTestTest, ShuffleFalse_0) {
 
5448
  const char* argv[] = {
 
5449
    "foo.exe",
 
5450
    "--gtest_shuffle=0",
 
5451
    NULL
 
5452
  };
 
5453
 
 
5454
  const char* argv2[] = {
 
5455
    "foo.exe",
 
5456
    NULL
 
5457
  };
 
5458
 
 
5459
  GTEST_TEST_PARSING_FLAGS_(argv, argv2, Flags::Shuffle(false));
 
5460
}
 
5461
 
 
5462
// Tests parsing a --gtest_shuffle flag that has a "true"
 
5463
// definition.
 
5464
TEST_F(InitGoogleTestTest, ShuffleTrue) {
 
5465
  const char* argv[] = {
 
5466
    "foo.exe",
 
5467
    "--gtest_shuffle=1",
 
5468
    NULL
 
5469
  };
 
5470
 
 
5471
  const char* argv2[] = {
 
5472
    "foo.exe",
 
5473
    NULL
 
5474
  };
 
5475
 
 
5476
  GTEST_TEST_PARSING_FLAGS_(argv, argv2, Flags::Shuffle(true));
 
5477
}
5126
5478
 
5127
5479
// Tests parsing --gtest_throw_on_failure.
5128
 
TEST_F(InitGoogleTestTest, ThrowOnFailureNoDef) {
 
5480
TEST_F(InitGoogleTestTest, ThrowOnFailureWithoutValue) {
5129
5481
  const char* argv[] = {
5130
5482
    "foo.exe",
5131
5483
    "--gtest_throw_on_failure",
5209
5561
    // There should be no tests running at this point.
5210
5562
    const TestInfo* test_info =
5211
5563
      UnitTest::GetInstance()->current_test_info();
5212
 
    EXPECT_EQ(NULL, test_info)
 
5564
    EXPECT_TRUE(test_info == NULL)
5213
5565
        << "There should be no tests running at this point.";
5214
5566
  }
5215
5567
 
5218
5570
  static void TearDownTestCase() {
5219
5571
    const TestInfo* test_info =
5220
5572
      UnitTest::GetInstance()->current_test_info();
5221
 
    EXPECT_EQ(NULL, test_info)
 
5573
    EXPECT_TRUE(test_info == NULL)
5222
5574
        << "There should be no tests running at this point.";
5223
5575
  }
5224
5576
};
5526
5878
 
5527
5879
  SetEnv("TERM", "xterm-color");  // TERM supports colors.
5528
5880
  EXPECT_TRUE(ShouldUseColor(true));  // Stdout is a TTY.
 
5881
 
 
5882
  SetEnv("TERM", "linux");  // TERM supports colors.
 
5883
  EXPECT_TRUE(ShouldUseColor(true));  // Stdout is a TTY.
5529
5884
#endif  // GTEST_OS_WINDOWS
5530
5885
}
5531
5886