~statik/ubuntu/maverick/protobuf/A

« back to all changes in this revision

Viewing changes to gtest/src/gtest-internal-inl.h

  • 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:
45
45
#error "It must not be included except by Google Test itself."
46
46
#endif  // GTEST_IMPLEMENTATION_
47
47
 
 
48
#ifndef _WIN32_WCE
48
49
#include <errno.h>
 
50
#endif  // !_WIN32_WCE
49
51
#include <stddef.h>
50
 
#include <stdlib.h>   // For strtoll/_strtoul64.
 
52
#include <stdlib.h>  // For strtoll/_strtoul64/malloc/free.
 
53
#include <string.h>  // For memmove.
51
54
 
52
55
#include <string>
53
56
 
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";
89
94
 
 
95
// A valid random seed must be in [1, kMaxRandomSeed].
 
96
const int kMaxRandomSeed = 99999;
 
97
 
 
98
// Returns the current time in milliseconds.
 
99
TimeInMillis GetTimeInMillis();
 
100
 
 
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);
 
107
 
 
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;
 
114
}
 
115
 
 
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;
 
125
}
 
126
 
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 {
104
141
    list_tests_ = GTEST_FLAG(list_tests);
105
142
    output_ = GTEST_FLAG(output);
106
143
    print_time_ = GTEST_FLAG(print_time);
 
144
    random_seed_ = GTEST_FLAG(random_seed);
107
145
    repeat_ = GTEST_FLAG(repeat);
 
146
    shuffle_ = GTEST_FLAG(shuffle);
108
147
    throw_on_failure_ = GTEST_FLAG(throw_on_failure);
109
148
  }
110
149
 
121
160
    GTEST_FLAG(list_tests) = list_tests_;
122
161
    GTEST_FLAG(output) = output_;
123
162
    GTEST_FLAG(print_time) = print_time_;
 
163
    GTEST_FLAG(random_seed) = random_seed_;
124
164
    GTEST_FLAG(repeat) = repeat_;
 
165
    GTEST_FLAG(shuffle) = shuffle_;
125
166
    GTEST_FLAG(throw_on_failure) = throw_on_failure_;
126
167
  }
127
168
 private:
138
179
  String output_;
139
180
  bool print_time_;
140
181
  bool pretty_;
 
182
  internal::Int32 random_seed_;
141
183
  internal::Int32 repeat_;
 
184
  bool shuffle_;
142
185
  bool throw_on_failure_;
143
186
} GTEST_ATTRIBUTE_UNUSED_;
144
187
 
196
239
// method. Assumes that 0 <= shard_index < total_shards.
197
240
bool ShouldRunTestOnShard(int total_shards, int shard_index, int test_id);
198
241
 
199
 
// List is a simple singly-linked list container.
200
 
//
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.
204
 
//
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.
208
 
//
209
 
// The element type must support copy constructor.
210
 
 
211
 
// Forward declare List
212
 
template <typename E>  // E is the element type.
213
 
class List;
214
 
 
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.
219
 
class ListNode {
220
 
  friend class List<E>;
221
 
 
222
 
 private:
223
 
 
224
 
  E element_;
225
 
  ListNode * next_;
226
 
 
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.
229
 
  //
230
 
  // Creates a node with a given element value.  The next pointer is
231
 
  // set to NULL.
232
 
  //
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) {}
236
 
 
237
 
  // We disallow copying ListNode
238
 
  GTEST_DISALLOW_COPY_AND_ASSIGN_(ListNode);
239
 
 
240
 
 public:
241
 
 
242
 
  // Gets the element in this node.
243
 
  E & element() { return element_; }
244
 
  const E & element() const { return element_; }
245
 
 
246
 
  // Gets the next node in the list.
247
 
  ListNode * next() { return next_; }
248
 
  const ListNode * next() const { return next_; }
249
 
};
250
 
 
251
 
 
252
 
// List is a simple singly-linked list container.
253
 
template <typename E>  // E is the element type.
254
 
class List {
255
 
 public:
256
 
 
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
 
243
// elements.
 
244
//
 
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.
 
249
//
 
250
// The element type must support copy constructor and operator=.
 
251
template <typename E>  // E is the element type.
 
252
class Vector {
 
253
 public:
 
254
  // Creates an empty Vector.
 
255
  Vector() : elements_(NULL), capacity_(0), size_(0) {}
259
256
 
260
257
  // D'tor.
261
 
  virtual ~List();
 
258
  virtual ~Vector() { Clear(); }
262
259
 
263
 
  // Clears the list.
 
260
  // Clears the Vector.
264
261
  void Clear() {
265
 
    if ( size_ > 0 ) {
266
 
      // 1. Deletes every node.
267
 
      ListNode<E> * node = head_;
268
 
      ListNode<E> * next = node->next();
269
 
      for ( ; ; ) {
270
 
        delete node;
271
 
        node = next;
272
 
        if ( node == NULL ) break;
273
 
        next = node->next();
 
262
    if (elements_ != NULL) {
 
263
      for (int i = 0; i < size_; i++) {
 
264
        delete elements_[i];
274
265
      }
275
266
 
276
 
      // 2. Resets the member variables.
277
 
      head_ = last_ = NULL;
278
 
      size_ = 0;
 
267
      free(elements_);
 
268
      elements_ = NULL;
 
269
      capacity_ = size_ = 0;
279
270
    }
280
271
  }
281
272
 
282
273
  // Gets the number of elements.
283
274
  int size() const { return size_; }
284
275
 
285
 
  // Returns true if the list is empty.
286
 
  bool IsEmpty() const { return size() == 0; }
287
 
 
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_; }
291
 
 
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_; }
295
 
 
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);
302
 
 
303
 
    if ( size_ == 0 ) {
304
 
      head_ = last_ = new_node;
305
 
      size_ = 1;
306
 
    } else {
307
 
      last_->next_ = new_node;
308
 
      last_ = new_node;
309
 
      size_++;
310
 
    }
311
 
  }
312
 
 
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);
316
 
 
317
 
    if ( size_ == 0 ) {
318
 
      head_ = last_ = new_node;
319
 
      size_ = 1;
320
 
    } else {
321
 
      new_node->next_ = head_;
322
 
      head_ = new_node;
323
 
      size_++;
324
 
    }
325
 
  }
326
 
 
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_); }
 
281
 
 
282
  // Adds an element to the beginning of this Vector.
 
283
  void PushFront(const E& element) { Insert(element, 0); }
 
284
 
 
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;
333
 
 
334
 
    if (result != NULL) {
335
 
      *result = head_->element_;
336
 
    }
337
 
 
338
 
    ListNode<E>* const old_head = head_;
 
290
    if (size_ == 0)
 
291
      return false;
 
292
 
 
293
    if (result != NULL)
 
294
      *result = GetElement(0);
 
295
 
 
296
    Erase(0);
 
297
    return true;
 
298
  }
 
299
 
 
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,
 
302
  // size()].
 
303
  void Insert(const E& element, int index) {
 
304
    GrowIfNeeded();
 
305
    MoveElements(index, size_ - index, index + 1);
 
306
    elements_[index] = new E(element);
 
307
    size_++;
 
308
  }
 
309
 
 
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) << "].";
 
316
 
 
317
    delete elements_[index];
 
318
    MoveElements(index + 1, size_ - index - 1, index);
339
319
    size_--;
340
 
    if (size_ == 0) {
341
 
      head_ = last_ = NULL;
342
 
    } else {
343
 
      head_ = head_->next_;
344
 
    }
345
 
    delete old_head;
346
 
 
347
 
    return true;
348
 
  }
349
 
 
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) {
355
 
    if (node == NULL) {
356
 
      PushFront(element);
357
 
      return Head();
358
 
    }
359
 
 
360
 
    ListNode<E>* const new_node = new ListNode<E>(element);
361
 
    new_node->next_ = node->next_;
362
 
    node->next_ = new_node;
363
 
    size_++;
364
 
    if (node == last_) {
365
 
      last_ = new_node;
366
 
    }
367
 
 
368
 
    return new_node;
369
320
  }
370
321
 
371
322
  // Returns the number of elements that satisfy a given predicate.
374
325
  template <typename P>  // P is the type of the predicate function/functor
375
326
  int CountIf(P predicate) const {
376
327
    int count = 0;
377
 
    for ( const ListNode<E> * node = Head();
378
 
          node != NULL;
379
 
          node = node->next() ) {
380
 
      if ( predicate(node->element()) ) {
 
328
    for (int i = 0; i < size_; i++) {
 
329
      if (predicate(*(elements_[i]))) {
381
330
        count++;
382
331
      }
383
332
    }
385
334
    return count;
386
335
  }
387
336
 
388
 
  // Applies a function/functor to each element in the list.  The
 
337
  // Applies a function/functor to each element in the Vector.  The
389
338
  // parameter 'functor' is a function/functor that accepts a 'const
390
339
  // E &', where E is the element type.  This method does not change
391
340
  // the elements.
392
341
  template <typename F>  // F is the type of the function/functor
393
342
  void ForEach(F functor) const {
394
 
    for ( const ListNode<E> * node = Head();
395
 
          node != NULL;
396
 
          node = node->next() ) {
397
 
      functor(node->element());
 
343
    for (int i = 0; i < size_; i++) {
 
344
      functor(*(elements_[i]));
398
345
    }
399
346
  }
400
347
 
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();
408
 
          node != NULL;
409
 
          node = node->next() ) {
410
 
      if ( predicate(node->element()) ) {
411
 
        return node;
 
353
  const E* FindIf(P predicate) const {
 
354
    for (int i = 0; i < size_; i++) {
 
355
      if (predicate(*elements_[i])) {
 
356
        return elements_[i];
412
357
      }
413
358
    }
414
 
 
415
359
    return NULL;
416
360
  }
417
361
 
418
362
  template <typename P>
419
 
  ListNode<E> * FindIf(P predicate) {
420
 
    for ( ListNode<E> * node = Head();
421
 
          node != NULL;
422
 
          node = node->next() ) {
423
 
      if ( predicate(node->element() ) ) {
424
 
        return node;
 
363
  E* FindIf(P predicate) {
 
364
    for (int i = 0; i < size_; i++) {
 
365
      if (predicate(*elements_[i])) {
 
366
        return elements_[i];
425
367
      }
426
368
    }
427
 
 
428
369
    return NULL;
429
370
  }
430
371
 
 
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) << "].";
 
378
 
 
379
    return *(elements_[i]);
 
380
  }
 
381
 
 
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]);
 
386
  }
 
387
 
431
388
 private:
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.
435
 
 
436
 
  // We disallow copying List.
437
 
  GTEST_DISALLOW_COPY_AND_ASSIGN_(List);
438
 
};
439
 
 
440
 
// The virtual destructor of List.
441
 
template <typename E>
442
 
List<E>::~List() {
443
 
  Clear();
444
 
}
 
389
  // Grows the buffer if it is not big enough to hold one more element.
 
390
  void GrowIfNeeded() {
 
391
    if (size_ < capacity_)
 
392
      return;
 
393
 
 
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])));
 
403
  }
 
404
 
 
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]));
 
408
  }
 
409
 
 
410
  E** elements_;
 
411
  int capacity_;  // The number of elements allocated for elements_.
 
412
  int size_;      // The number of elements; in the range [0, capacity_].
 
413
 
 
414
  // We disallow copying Vector.
 
415
  GTEST_DISALLOW_COPY_AND_ASSIGN_(Vector);
 
416
};  // class Vector
445
417
 
446
418
// A function for deleting an object.  Handy for being used as a
447
419
// functor.
450
422
  delete x;
451
423
}
452
424
 
453
 
// A copyable object representing a user specified test property which can be
454
 
// output as a key/value string pair.
455
 
//
456
 
// Don't inherit from TestProperty as its destructor is not virtual.
457
 
class TestProperty {
458
 
 public:
459
 
  // C'tor.  TestProperty does NOT have a default constructor.
460
 
  // Always use this constructor (with parameters) to create a
461
 
  // TestProperty object.
462
 
  TestProperty(const char* key, const char* value) :
463
 
    key_(key), value_(value) {
464
 
  }
465
 
 
466
 
  // Gets the user supplied key.
467
 
  const char* key() const {
468
 
    return key_.c_str();
469
 
  }
470
 
 
471
 
  // Gets the user supplied value.
472
 
  const char* value() const {
473
 
    return value_.c_str();
474
 
  }
475
 
 
476
 
  // Sets a new value, overriding the one supplied in the constructor.
477
 
  void SetValue(const char* new_value) {
478
 
    value_ = new_value;
479
 
  }
480
 
 
481
 
 private:
482
 
  // The key supplied by the user.
483
 
  String key_;
484
 
  // The value supplied by the user.
485
 
  String value_;
486
 
};
487
 
 
488
425
// A predicate that checks the key of a TestProperty against a known key.
489
426
//
490
427
// TestPropertyKeyIs is copyable.
505
442
  String key_;
506
443
};
507
444
 
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
511
 
// the Test.
512
 
//
513
 
// TestResult is not copyable.
514
 
class TestResult {
515
 
 public:
516
 
  // Creates an empty TestResult.
517
 
  TestResult();
518
 
 
519
 
  // D'tor.  Do not inherit from TestResult.
520
 
  ~TestResult();
521
 
 
522
 
  // Gets the list of TestPartResults.
523
 
  const internal::List<TestPartResult> & test_part_results() const {
524
 
    return test_part_results_;
525
 
  }
526
 
 
527
 
  // Gets the list of TestProperties.
528
 
  const internal::List<internal::TestProperty> & test_properties() const {
529
 
    return test_properties_;
530
 
  }
531
 
 
532
 
  // Gets the number of successful test parts.
533
 
  int successful_part_count() const;
534
 
 
535
 
  // Gets the number of failed test parts.
536
 
  int failed_part_count() const;
537
 
 
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;
541
 
 
542
 
  // Returns true iff the test passed (i.e. no test part failed).
543
 
  bool Passed() const { return !Failed(); }
544
 
 
545
 
  // Returns true iff the test failed.
546
 
  bool Failed() const { return failed_part_count() > 0; }
547
 
 
548
 
  // Returns true iff the test fatally failed.
549
 
  bool HasFatalFailure() const;
550
 
 
551
 
  // Returns true iff the test has a non-fatal failure.
552
 
  bool HasNonfatalFailure() const;
553
 
 
554
 
  // Returns the elapsed time, in milliseconds.
555
 
  TimeInMillis elapsed_time() const { return elapsed_time_; }
556
 
 
557
 
  // Sets the elapsed time.
558
 
  void set_elapsed_time(TimeInMillis elapsed) { elapsed_time_ = elapsed; }
559
 
 
560
 
  // Adds a test part result to the list.
561
 
  void AddTestPartResult(const TestPartResult& test_part_result);
562
 
 
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
567
 
  // key.
568
 
  void RecordProperty(const internal::TestProperty& test_property);
569
 
 
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);
574
 
 
575
 
  // Returns the death test count.
576
 
  int death_test_count() const { return death_test_count_; }
577
 
 
578
 
  // Increments the death test count, returning the new count.
579
 
  int increment_death_test_count() { return ++death_test_count_; }
580
 
 
581
 
  // Clears the test part results.
582
 
  void ClearTestPartResults() { test_part_results_.Clear(); }
583
 
 
584
 
  // Clears the object.
585
 
  void Clear();
586
 
 private:
587
 
  // Protects mutable state of the property list and of owned properties, whose
588
 
  // values may be updated.
589
 
  internal::Mutex test_properites_mutex_;
590
 
 
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_;
599
 
 
600
 
  // We disallow copying TestResult.
601
 
  GTEST_DISALLOW_COPY_AND_ASSIGN_(TestResult);
602
 
};  // class TestResult
603
 
 
604
445
class TestInfoImpl {
605
446
 public:
606
447
  TestInfoImpl(TestInfo* parent, const char* test_case_name,
643
484
  TypeId fixture_class_id() const { return fixture_class_id_; }
644
485
 
645
486
  // Returns the test result.
646
 
  internal::TestResult* result() { return &result_; }
647
 
  const internal::TestResult* result() const { return &result_; }
 
487
  TestResult* result() { return &result_; }
 
488
  const TestResult* result() const { return &result_; }
648
489
 
649
490
  // Creates the test object, runs it, records its result, and then
650
491
  // deletes it.
680
521
 
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_;
 
524
  TestResult result_;
684
525
 
685
526
  GTEST_DISALLOW_COPY_AND_ASSIGN_(TestInfoImpl);
686
527
};
687
528
 
688
 
}  // namespace internal
689
 
 
690
 
// A test case, which consists of a list of TestInfos.
691
 
//
692
 
// TestCase is not copyable.
693
 
class TestCase {
694
 
 public:
695
 
  // Creates a TestCase with the given name.
696
 
  //
697
 
  // TestCase does NOT have a default constructor.  Always use this
698
 
  // constructor to create a TestCase object.
699
 
  //
700
 
  // Arguments:
701
 
  //
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);
708
 
 
709
 
  // Destructor of TestCase.
710
 
  virtual ~TestCase();
711
 
 
712
 
  // Gets the name of the TestCase.
713
 
  const char* name() const { return name_.c_str(); }
714
 
 
715
 
  // Returns the test case comment.
716
 
  const char* comment() const { return comment_.c_str(); }
717
 
 
718
 
  // Returns true if any test in this test case should run.
719
 
  bool should_run() const { return should_run_; }
720
 
 
721
 
  // Sets the should_run member.
722
 
  void set_should_run(bool should) { should_run_ = should; }
723
 
 
724
 
  // Gets the (mutable) list of TestInfos in this TestCase.
725
 
  internal::List<TestInfo*>& test_info_list() { return *test_info_list_; }
726
 
 
727
 
  // Gets the (immutable) list of TestInfos in this TestCase.
728
 
  const internal::List<TestInfo *> & test_info_list() const {
729
 
    return *test_info_list_;
730
 
  }
731
 
 
732
 
  // Gets the number of successful tests in this test case.
733
 
  int successful_test_count() const;
734
 
 
735
 
  // Gets the number of failed tests in this test case.
736
 
  int failed_test_count() const;
737
 
 
738
 
  // Gets the number of disabled tests in this test case.
739
 
  int disabled_test_count() const;
740
 
 
741
 
  // Get the number of tests in this test case that should run.
742
 
  int test_to_run_count() const;
743
 
 
744
 
  // Gets the number of all tests in this test case.
745
 
  int total_test_count() const;
746
 
 
747
 
  // Returns true iff the test case passed.
748
 
  bool Passed() const { return !Failed(); }
749
 
 
750
 
  // Returns true iff the test case failed.
751
 
  bool Failed() const { return failed_test_count() > 0; }
752
 
 
753
 
  // Returns the elapsed time, in milliseconds.
754
 
  internal::TimeInMillis elapsed_time() const { return elapsed_time_; }
755
 
 
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);
759
 
 
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);
763
 
 
764
 
  // Clears the results of all tests in this test case.
765
 
  void ClearResult();
766
 
 
767
 
  // Clears the results of all tests in the given test case.
768
 
  static void ClearTestCaseResult(TestCase* test_case) {
769
 
    test_case->ClearResult();
770
 
  }
771
 
 
772
 
  // Runs every test in this TestCase.
773
 
  void Run();
774
 
 
775
 
  // Runs every test in the given TestCase.
776
 
  static void RunTestCase(TestCase * test_case) { test_case->Run(); }
777
 
 
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();
782
 
  }
783
 
 
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();
788
 
  }
789
 
 
790
 
  // Returns true iff test is disabled.
791
 
  static bool TestDisabled(const TestInfo * test_info) {
792
 
    return test_info->impl()->is_disabled();
793
 
  }
794
 
 
795
 
  // Returns true if the given test should run.
796
 
  static bool ShouldRunTest(const TestInfo *test_info) {
797
 
    return test_info->impl()->should_run();
798
 
  }
799
 
 
800
 
 private:
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.
812
 
  bool should_run_;
813
 
  // Elapsed time, in milliseconds.
814
 
  internal::TimeInMillis elapsed_time_;
815
 
 
816
 
  // We disallow copying TestCases.
817
 
  GTEST_DISALLOW_COPY_AND_ASSIGN_(TestCase);
818
 
};
819
 
 
820
 
namespace internal {
821
 
 
822
529
// Class UnitTestOptions.
823
530
//
824
531
// This class contains functions for processing options the user
1028
735
    return failed_test_case_count() > 0 || ad_hoc_test_result()->Failed();
1029
736
  }
1030
737
 
 
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);
 
742
  }
 
743
 
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();
1034
747
 
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_;
1038
 
  }
 
749
  const TestResult* ad_hoc_test_result() const { return &ad_hoc_test_result_; }
1039
750
 
1040
751
  // Sets the unit test result printer.
1041
752
  //
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);
1046
757
 
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
1105
816
    // before main() is reached.
1106
817
    if (original_working_dir_.IsEmpty()) {
1107
818
      original_working_dir_.Set(FilePath::GetCurrentDir());
1108
 
      if (original_working_dir_.IsEmpty()) {
1109
 
        printf("%s\n", "Failed to get the current working directory.");
1110
 
        abort();
1111
 
      }
 
819
      GTEST_CHECK_(!original_working_dir_.IsEmpty())
 
820
          << "Failed to get the current working directory.";
1112
821
    }
1113
822
 
1114
823
    GetTestCase(test_info->test_case_name(),
1179
888
  TestInfo* current_test_info() { return current_test_info_; }
1180
889
  const TestInfo* current_test_info() const { return current_test_info_; }
1181
890
 
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_;
1187
896
  }
1188
897
 
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_; }
1191
900
 
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();
1195
904
  }
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();
1198
907
  }
1199
908
 
1213
922
  friend class ReplaceDeathTestFactory;
1214
923
#endif  // GTEST_HAS_DEATH_TEST
1215
924
 
 
925
  // Gets the random seed used at the start of the current test run.
 
926
  int random_seed() const { return random_seed_; }
 
927
 
1216
928
 private:
1217
929
  friend class ::testing::UnitTest;
1218
930
 
1238
950
  internal::ThreadLocal<TestPartResultReporterInterface*>
1239
951
      per_thread_test_part_result_reporter_;
1240
952
 
1241
 
  // The list of environments that need to be set-up/torn-down
 
953
  // The vector of environments that need to be set-up/torn-down
1242
954
  // before/after the tests are run.  environments_in_reverse_order_
1243
955
  // simply mirrors environments_ in reverse order.
1244
 
  internal::List<Environment*> environments_;
1245
 
  internal::List<Environment*> environments_in_reverse_order_;
 
956
  internal::Vector<Environment*> environments_;
 
957
  internal::Vector<Environment*> environments_in_reverse_order_;
1246
958
 
1247
 
  internal::List<TestCase*> test_cases_;  // The list of TestCases.
 
959
  internal::Vector<TestCase*> test_cases_;  // The vector of TestCases.
1248
960
 
1249
961
#if GTEST_HAS_PARAM_TEST
1250
962
  // ParameterizedTestRegistry object used to register value-parameterized
1255
967
  bool parameterized_tests_registered_;
1256
968
#endif  // GTEST_HAS_PARAM_TEST
1257
969
 
1258
 
  // Points to the last death test case registered.  Initially NULL.
1259
 
  internal::ListNode<TestCase*>* last_death_test_case_;
 
970
  // Index of the last death test case registered.  Initially -1.
 
971
  int last_death_test_case_;
1260
972
 
1261
973
  // This points to the TestCase for the currently running test.  It
1262
974
  // changes as Google Test goes through one test case after another.
1263
975
  // When no test is running, this is set to NULL and Google Test
1264
 
  // stores assertion results in ad_hoc_test_result_.  Initally NULL.
 
976
  // stores assertion results in ad_hoc_test_result_.  Initially NULL.
1265
977
  TestCase* current_test_case_;
1266
978
 
1267
979
  // This points to the TestInfo for the currently running test.  It
1278
990
  // If an assertion is encountered when no TEST or TEST_F is running,
1279
991
  // Google Test attributes the assertion result to an imaginary "ad hoc"
1280
992
  // test, and records the result in ad_hoc_test_result_.
1281
 
  internal::TestResult ad_hoc_test_result_;
 
993
  TestResult ad_hoc_test_result_;
1282
994
 
1283
995
  // The unit test result printer.  Will be deleted when the UnitTest
1284
996
  // object is destructed.  By default, a plain text printer is used,
1292
1004
  // desired.
1293
1005
  OsStackTraceGetterInterface* os_stack_trace_getter_;
1294
1006
 
 
1007
  // The random number seed used at the beginning of the test run.
 
1008
  int random_seed_;
 
1009
 
1295
1010
  // How long the test took to run, in milliseconds.
1296
1011
  TimeInMillis elapsed_time_;
1297
1012
 
1303
1018
#endif  // GTEST_HAS_DEATH_TEST
1304
1019
 
1305
1020
  // A per-thread stack of traces created by the SCOPED_TRACE() macro.
1306
 
  internal::ThreadLocal<internal::List<TraceInfo> > gtest_trace_stack_;
 
1021
  internal::ThreadLocal<internal::Vector<TraceInfo> > gtest_trace_stack_;
1307
1022
 
1308
1023
  GTEST_DISALLOW_COPY_AND_ASSIGN_(UnitTestImpl);
1309
1024
};  // class UnitTestImpl
1314
1029
  return UnitTest::GetInstance()->impl();
1315
1030
}
1316
1031
 
1317
 
// Clears all test part results of the current test.
1318
 
inline void ClearCurrentTestPartResults() {
1319
 
  GetUnitTestImpl()->current_test_result()->ClearTestPartResults();
1320
 
}
1321
 
 
1322
1032
// Internal helper functions for implementing the simple regular
1323
1033
// expression matcher.
1324
1034
bool IsInSet(char ch, const char* str);
1411
1121
}
1412
1122
#endif  // GTEST_HAS_DEATH_TEST
1413
1123
 
 
1124
// TestResult contains some private methods that should be hidden from
 
1125
// Google Test user but are required for testing. This class allow our tests
 
1126
// to access them.
 
1127
class TestResultAccessor {
 
1128
 public:
 
1129
  static void RecordProperty(TestResult* test_result,
 
1130
                             const TestProperty& property) {
 
1131
    test_result->RecordProperty(property);
 
1132
  }
 
1133
 
 
1134
  static bool Passed(const TestResult& result) { return result.Passed(); }
 
1135
 
 
1136
  static void ClearTestPartResults(TestResult* test_result) {
 
1137
    test_result->ClearTestPartResults();
 
1138
  }
 
1139
 
 
1140
  static const Vector<testing::TestPartResult>& test_part_results(
 
1141
      const TestResult& test_result) {
 
1142
    return test_result.test_part_results();
 
1143
  }
 
1144
};
 
1145
 
1414
1146
}  // namespace internal
1415
1147
}  // namespace testing
1416
1148