84
87
const char kListTestsFlag[] = "list_tests";
85
88
const char kOutputFlag[] = "output";
86
89
const char kPrintTimeFlag[] = "print_time";
90
const char kRandomSeedFlag[] = "random_seed";
87
91
const char kRepeatFlag[] = "repeat";
92
const char kShuffleFlag[] = "shuffle";
88
93
const char kThrowOnFailureFlag[] = "throw_on_failure";
95
// A valid random seed must be in [1, kMaxRandomSeed].
96
const int kMaxRandomSeed = 99999;
98
// Returns the current time in milliseconds.
99
TimeInMillis GetTimeInMillis();
101
// Returns a random seed in range [1, kMaxRandomSeed] based on the
102
// given --gtest_random_seed flag value.
103
inline int GetRandomSeedFromFlag(Int32 random_seed_flag) {
104
const unsigned int raw_seed = (random_seed_flag == 0) ?
105
static_cast<unsigned int>(GetTimeInMillis()) :
106
static_cast<unsigned int>(random_seed_flag);
108
// Normalizes the actual seed to range [1, kMaxRandomSeed] such that
109
// it's easy to type.
110
const int normalized_seed =
111
static_cast<int>((raw_seed - 1U) %
112
static_cast<unsigned int>(kMaxRandomSeed)) + 1;
113
return normalized_seed;
116
// Returns the first valid random seed after 'seed'. The behavior is
117
// undefined if 'seed' is invalid. The seed after kMaxRandomSeed is
118
// considered to be 1.
119
inline int GetNextRandomSeed(int seed) {
120
GTEST_CHECK_(1 <= seed && seed <= kMaxRandomSeed)
121
<< "Invalid random seed " << seed << " - must be in [1, "
122
<< kMaxRandomSeed << "].";
123
const int next_seed = seed + 1;
124
return (next_seed > kMaxRandomSeed) ? 1 : next_seed;
90
127
// This class saves the values of all Google Test flags in its c'tor, and
91
128
// restores them in its d'tor.
92
129
class GTestFlagSaver {
196
239
// method. Assumes that 0 <= shard_index < total_shards.
197
240
bool ShouldRunTestOnShard(int total_shards, int shard_index, int test_id);
199
// List is a simple singly-linked list container.
201
// We cannot use std::list as Microsoft's implementation of STL has
202
// problems when exception is disabled. There is a hack to work
203
// around this, but we've seen cases where the hack fails to work.
205
// TODO(wan): switch to std::list when we have a reliable fix for the
206
// STL problem, e.g. when we upgrade to the next version of Visual
207
// C++, or (more likely) switch to STLport.
209
// The element type must support copy constructor.
211
// Forward declare List
212
template <typename E> // E is the element type.
215
// ListNode is a node in a singly-linked list. It consists of an
216
// element and a pointer to the next node. The last node in the list
217
// has a NULL value for its next pointer.
218
template <typename E> // E is the element type.
220
friend class List<E>;
227
// The c'tor is private s.t. only in the ListNode class and in its
228
// friend class List we can create a ListNode object.
230
// Creates a node with a given element value. The next pointer is
233
// ListNode does NOT have a default constructor. Always use this
234
// constructor (with parameter) to create a ListNode object.
235
explicit ListNode(const E & element) : element_(element), next_(NULL) {}
237
// We disallow copying ListNode
238
GTEST_DISALLOW_COPY_AND_ASSIGN_(ListNode);
242
// Gets the element in this node.
243
E & element() { return element_; }
244
const E & element() const { return element_; }
246
// Gets the next node in the list.
247
ListNode * next() { return next_; }
248
const ListNode * next() const { return next_; }
252
// List is a simple singly-linked list container.
253
template <typename E> // E is the element type.
257
// Creates an empty list.
258
List() : head_(NULL), last_(NULL), size_(0) {}
242
// Vector is an ordered container that supports random access to the
245
// We cannot use std::vector, as Visual C++ 7.1's implementation of
246
// STL has problems compiling when exceptions are disabled. There is
247
// a hack to work around the problems, but we've seen cases where the
248
// hack fails to work.
250
// The element type must support copy constructor and operator=.
251
template <typename E> // E is the element type.
254
// Creates an empty Vector.
255
Vector() : elements_(NULL), capacity_(0), size_(0) {}
258
virtual ~Vector() { Clear(); }
260
// Clears the Vector.
266
// 1. Deletes every node.
267
ListNode<E> * node = head_;
268
ListNode<E> * next = node->next();
272
if ( node == NULL ) break;
262
if (elements_ != NULL) {
263
for (int i = 0; i < size_; i++) {
276
// 2. Resets the member variables.
277
head_ = last_ = NULL;
269
capacity_ = size_ = 0;
282
273
// Gets the number of elements.
283
274
int size() const { return size_; }
285
// Returns true if the list is empty.
286
bool IsEmpty() const { return size() == 0; }
288
// Gets the first element of the list, or NULL if the list is empty.
289
ListNode<E> * Head() { return head_; }
290
const ListNode<E> * Head() const { return head_; }
292
// Gets the last element of the list, or NULL if the list is empty.
293
ListNode<E> * Last() { return last_; }
294
const ListNode<E> * Last() const { return last_; }
296
// Adds an element to the end of the list. A copy of the element is
297
// created using the copy constructor, and then stored in the list.
298
// Changes made to the element in the list doesn't affect the source
299
// object, and vice versa.
300
void PushBack(const E & element) {
301
ListNode<E> * new_node = new ListNode<E>(element);
304
head_ = last_ = new_node;
307
last_->next_ = new_node;
313
// Adds an element to the beginning of this list.
314
void PushFront(const E& element) {
315
ListNode<E>* const new_node = new ListNode<E>(element);
318
head_ = last_ = new_node;
321
new_node->next_ = head_;
327
// Removes an element from the beginning of this list. If the
276
// Adds an element to the end of the Vector. A copy of the element
277
// is created using the copy constructor, and then stored in the
278
// Vector. Changes made to the element in the Vector doesn't affect
279
// the source object, and vice versa.
280
void PushBack(const E & element) { Insert(element, size_); }
282
// Adds an element to the beginning of this Vector.
283
void PushFront(const E& element) { Insert(element, 0); }
285
// Removes an element from the beginning of this Vector. If the
328
286
// result argument is not NULL, the removed element is stored in the
329
287
// memory it points to. Otherwise the element is thrown away.
330
// Returns true iff the list wasn't empty before the operation.
288
// Returns true iff the vector wasn't empty before the operation.
331
289
bool PopFront(E* result) {
332
if (size_ == 0) return false;
334
if (result != NULL) {
335
*result = head_->element_;
338
ListNode<E>* const old_head = head_;
294
*result = GetElement(0);
300
// Inserts an element at the given index. It's the caller's
301
// responsibility to ensure that the given index is in the range [0,
303
void Insert(const E& element, int index) {
305
MoveElements(index, size_ - index, index + 1);
306
elements_[index] = new E(element);
310
// Erases the element at the specified index, or aborts the program if the
311
// index is not in range [0, size()).
312
void Erase(int index) {
313
GTEST_CHECK_(0 <= index && index < size_)
314
<< "Invalid Vector index " << index << ": must be in range [0, "
315
<< (size_ - 1) << "].";
317
delete elements_[index];
318
MoveElements(index + 1, size_ - index - 1, index);
341
head_ = last_ = NULL;
343
head_ = head_->next_;
350
// Inserts an element after a given node in the list. It's the
351
// caller's responsibility to ensure that the given node is in the
352
// list. If the given node is NULL, inserts the element at the
353
// front of the list.
354
ListNode<E>* InsertAfter(ListNode<E>* node, const E& element) {
360
ListNode<E>* const new_node = new ListNode<E>(element);
361
new_node->next_ = node->next_;
362
node->next_ = new_node;
371
322
// Returns the number of elements that satisfy a given predicate.
403
350
// function/functor that accepts a 'const E &', where E is the
404
351
// element type. This method does not change the elements.
405
352
template <typename P> // P is the type of the predicate function/functor.
406
const ListNode<E> * FindIf(P predicate) const {
407
for ( const ListNode<E> * node = Head();
409
node = node->next() ) {
410
if ( predicate(node->element()) ) {
353
const E* FindIf(P predicate) const {
354
for (int i = 0; i < size_; i++) {
355
if (predicate(*elements_[i])) {
418
362
template <typename P>
419
ListNode<E> * FindIf(P predicate) {
420
for ( ListNode<E> * node = Head();
422
node = node->next() ) {
423
if ( predicate(node->element() ) ) {
363
E* FindIf(P predicate) {
364
for (int i = 0; i < size_; i++) {
365
if (predicate(*elements_[i])) {
372
// Returns the i-th element of the list, or aborts the program if i
373
// is not in range [0, size()).
374
const E& GetElement(int i) const {
375
GTEST_CHECK_(0 <= i && i < size_)
376
<< "Invalid Vector index " << i << ": must be in range [0, "
377
<< (size_ - 1) << "].";
379
return *(elements_[i]);
382
// Returns the i-th element of the list, or default_value if i is not
383
// in range [0, size()).
384
E GetElementOr(int i, E default_value) const {
385
return (i < 0 || i >= size_) ? default_value : *(elements_[i]);
432
ListNode<E>* head_; // The first node of the list.
433
ListNode<E>* last_; // The last node of the list.
434
int size_; // The number of elements in the list.
436
// We disallow copying List.
437
GTEST_DISALLOW_COPY_AND_ASSIGN_(List);
440
// The virtual destructor of List.
441
template <typename E>
389
// Grows the buffer if it is not big enough to hold one more element.
390
void GrowIfNeeded() {
391
if (size_ < capacity_)
394
// Exponential bump-up is necessary to ensure that inserting N
395
// elements is O(N) instead of O(N^2). The factor 3/2 means that
396
// no more than 1/3 of the slots are wasted.
397
const int new_capacity = 3*(capacity_/2 + 1);
398
GTEST_CHECK_(new_capacity > capacity_) // Does the new capacity overflow?
399
<< "Cannot grow a Vector with " << capacity_ << " elements already.";
400
capacity_ = new_capacity;
401
elements_ = static_cast<E**>(
402
realloc(elements_, capacity_*sizeof(elements_[0])));
405
// Moves the give consecutive elements to a new index in the Vector.
406
void MoveElements(int source, int count, int dest) {
407
memmove(elements_ + dest, elements_ + source, count*sizeof(elements_[0]));
411
int capacity_; // The number of elements allocated for elements_.
412
int size_; // The number of elements; in the range [0, capacity_].
414
// We disallow copying Vector.
415
GTEST_DISALLOW_COPY_AND_ASSIGN_(Vector);
446
418
// A function for deleting an object. Handy for being used as a
508
// The result of a single Test. This includes a list of
509
// TestPartResults, a list of TestProperties, a count of how many
510
// death tests there are in the Test, and how much time it took to run
513
// TestResult is not copyable.
516
// Creates an empty TestResult.
519
// D'tor. Do not inherit from TestResult.
522
// Gets the list of TestPartResults.
523
const internal::List<TestPartResult> & test_part_results() const {
524
return test_part_results_;
527
// Gets the list of TestProperties.
528
const internal::List<internal::TestProperty> & test_properties() const {
529
return test_properties_;
532
// Gets the number of successful test parts.
533
int successful_part_count() const;
535
// Gets the number of failed test parts.
536
int failed_part_count() const;
538
// Gets the number of all test parts. This is the sum of the number
539
// of successful test parts and the number of failed test parts.
540
int total_part_count() const;
542
// Returns true iff the test passed (i.e. no test part failed).
543
bool Passed() const { return !Failed(); }
545
// Returns true iff the test failed.
546
bool Failed() const { return failed_part_count() > 0; }
548
// Returns true iff the test fatally failed.
549
bool HasFatalFailure() const;
551
// Returns true iff the test has a non-fatal failure.
552
bool HasNonfatalFailure() const;
554
// Returns the elapsed time, in milliseconds.
555
TimeInMillis elapsed_time() const { return elapsed_time_; }
557
// Sets the elapsed time.
558
void set_elapsed_time(TimeInMillis elapsed) { elapsed_time_ = elapsed; }
560
// Adds a test part result to the list.
561
void AddTestPartResult(const TestPartResult& test_part_result);
563
// Adds a test property to the list. The property is validated and may add
564
// a non-fatal failure if invalid (e.g., if it conflicts with reserved
565
// key names). If a property is already recorded for the same key, the
566
// value will be updated, rather than storing multiple values for the same
568
void RecordProperty(const internal::TestProperty& test_property);
570
// Adds a failure if the key is a reserved attribute of Google Test
571
// testcase tags. Returns true if the property is valid.
572
// TODO(russr): Validate attribute names are legal and human readable.
573
static bool ValidateTestProperty(const internal::TestProperty& test_property);
575
// Returns the death test count.
576
int death_test_count() const { return death_test_count_; }
578
// Increments the death test count, returning the new count.
579
int increment_death_test_count() { return ++death_test_count_; }
581
// Clears the test part results.
582
void ClearTestPartResults() { test_part_results_.Clear(); }
584
// Clears the object.
587
// Protects mutable state of the property list and of owned properties, whose
588
// values may be updated.
589
internal::Mutex test_properites_mutex_;
591
// The list of TestPartResults
592
internal::List<TestPartResult> test_part_results_;
593
// The list of TestProperties
594
internal::List<internal::TestProperty> test_properties_;
595
// Running count of death tests.
596
int death_test_count_;
597
// The elapsed time, in milliseconds.
598
TimeInMillis elapsed_time_;
600
// We disallow copying TestResult.
601
GTEST_DISALLOW_COPY_AND_ASSIGN_(TestResult);
602
}; // class TestResult
604
445
class TestInfoImpl {
606
447
TestInfoImpl(TestInfo* parent, const char* test_case_name,
681
522
// This field is mutable and needs to be reset before running the
682
523
// test for the second time.
683
internal::TestResult result_;
685
526
GTEST_DISALLOW_COPY_AND_ASSIGN_(TestInfoImpl);
688
} // namespace internal
690
// A test case, which consists of a list of TestInfos.
692
// TestCase is not copyable.
695
// Creates a TestCase with the given name.
697
// TestCase does NOT have a default constructor. Always use this
698
// constructor to create a TestCase object.
702
// name: name of the test case
703
// set_up_tc: pointer to the function that sets up the test case
704
// tear_down_tc: pointer to the function that tears down the test case
705
TestCase(const char* name, const char* comment,
706
Test::SetUpTestCaseFunc set_up_tc,
707
Test::TearDownTestCaseFunc tear_down_tc);
709
// Destructor of TestCase.
712
// Gets the name of the TestCase.
713
const char* name() const { return name_.c_str(); }
715
// Returns the test case comment.
716
const char* comment() const { return comment_.c_str(); }
718
// Returns true if any test in this test case should run.
719
bool should_run() const { return should_run_; }
721
// Sets the should_run member.
722
void set_should_run(bool should) { should_run_ = should; }
724
// Gets the (mutable) list of TestInfos in this TestCase.
725
internal::List<TestInfo*>& test_info_list() { return *test_info_list_; }
727
// Gets the (immutable) list of TestInfos in this TestCase.
728
const internal::List<TestInfo *> & test_info_list() const {
729
return *test_info_list_;
732
// Gets the number of successful tests in this test case.
733
int successful_test_count() const;
735
// Gets the number of failed tests in this test case.
736
int failed_test_count() const;
738
// Gets the number of disabled tests in this test case.
739
int disabled_test_count() const;
741
// Get the number of tests in this test case that should run.
742
int test_to_run_count() const;
744
// Gets the number of all tests in this test case.
745
int total_test_count() const;
747
// Returns true iff the test case passed.
748
bool Passed() const { return !Failed(); }
750
// Returns true iff the test case failed.
751
bool Failed() const { return failed_test_count() > 0; }
753
// Returns the elapsed time, in milliseconds.
754
internal::TimeInMillis elapsed_time() const { return elapsed_time_; }
756
// Adds a TestInfo to this test case. Will delete the TestInfo upon
757
// destruction of the TestCase object.
758
void AddTestInfo(TestInfo * test_info);
760
// Finds and returns a TestInfo with the given name. If one doesn't
761
// exist, returns NULL.
762
TestInfo* GetTestInfo(const char* test_name);
764
// Clears the results of all tests in this test case.
767
// Clears the results of all tests in the given test case.
768
static void ClearTestCaseResult(TestCase* test_case) {
769
test_case->ClearResult();
772
// Runs every test in this TestCase.
775
// Runs every test in the given TestCase.
776
static void RunTestCase(TestCase * test_case) { test_case->Run(); }
778
// Returns true iff test passed.
779
static bool TestPassed(const TestInfo * test_info) {
780
const internal::TestInfoImpl* const impl = test_info->impl();
781
return impl->should_run() && impl->result()->Passed();
784
// Returns true iff test failed.
785
static bool TestFailed(const TestInfo * test_info) {
786
const internal::TestInfoImpl* const impl = test_info->impl();
787
return impl->should_run() && impl->result()->Failed();
790
// Returns true iff test is disabled.
791
static bool TestDisabled(const TestInfo * test_info) {
792
return test_info->impl()->is_disabled();
795
// Returns true if the given test should run.
796
static bool ShouldRunTest(const TestInfo *test_info) {
797
return test_info->impl()->should_run();
801
// Name of the test case.
802
internal::String name_;
803
// Comment on the test case.
804
internal::String comment_;
805
// List of TestInfos.
806
internal::List<TestInfo*>* test_info_list_;
807
// Pointer to the function that sets up the test case.
808
Test::SetUpTestCaseFunc set_up_tc_;
809
// Pointer to the function that tears down the test case.
810
Test::TearDownTestCaseFunc tear_down_tc_;
811
// True iff any test in this test case should run.
813
// Elapsed time, in milliseconds.
814
internal::TimeInMillis elapsed_time_;
816
// We disallow copying TestCases.
817
GTEST_DISALLOW_COPY_AND_ASSIGN_(TestCase);
822
529
// Class UnitTestOptions.
824
531
// This class contains functions for processing options the user
1028
735
return failed_test_case_count() > 0 || ad_hoc_test_result()->Failed();
738
// Gets the i-th test case among all the test cases. i can range from 0 to
739
// total_test_case_count() - 1. If i is not in that range, returns NULL.
740
const TestCase* GetTestCase(int i) const {
741
return test_cases_.GetElementOr(i, NULL);
1031
744
// Returns the TestResult for the test that's currently running, or
1032
745
// the TestResult for the ad hoc test if no test is running.
1033
internal::TestResult* current_test_result();
746
TestResult* current_test_result();
1035
748
// Returns the TestResult for the ad hoc test.
1036
const internal::TestResult* ad_hoc_test_result() const {
1037
return &ad_hoc_test_result_;
749
const TestResult* ad_hoc_test_result() const { return &ad_hoc_test_result_; }
1040
751
// Sets the unit test result printer.
1042
753
// Does nothing if the input and the current printer object are the
1043
754
// same; otherwise, deletes the old printer object and makes the
1044
755
// input the current printer.
1045
void set_result_printer(UnitTestEventListenerInterface * result_printer);
756
void set_result_printer(UnitTestEventListenerInterface* result_printer);
1047
758
// Returns the current unit test result printer if it is not NULL;
1048
759
// otherwise, creates an appropriate result printer, makes it the
1179
888
TestInfo* current_test_info() { return current_test_info_; }
1180
889
const TestInfo* current_test_info() const { return current_test_info_; }
1182
// Returns the list of environments that need to be set-up/torn-down
891
// Returns the vector of environments that need to be set-up/torn-down
1183
892
// before/after the tests are run.
1184
internal::List<Environment*>* environments() { return &environments_; }
1185
internal::List<Environment*>* environments_in_reverse_order() {
893
internal::Vector<Environment*>* environments() { return &environments_; }
894
internal::Vector<Environment*>* environments_in_reverse_order() {
1186
895
return &environments_in_reverse_order_;
1189
internal::List<TestCase*>* test_cases() { return &test_cases_; }
1190
const internal::List<TestCase*>* test_cases() const { return &test_cases_; }
898
internal::Vector<TestCase*>* test_cases() { return &test_cases_; }
899
const internal::Vector<TestCase*>* test_cases() const { return &test_cases_; }
1192
901
// Getters for the per-thread Google Test trace stack.
1193
internal::List<TraceInfo>* gtest_trace_stack() {
902
internal::Vector<TraceInfo>* gtest_trace_stack() {
1194
903
return gtest_trace_stack_.pointer();
1196
const internal::List<TraceInfo>* gtest_trace_stack() const {
905
const internal::Vector<TraceInfo>* gtest_trace_stack() const {
1197
906
return gtest_trace_stack_.pointer();