sample3-inl.h revision fbaaef999ba563838ebd00874ed8a1c01fbf286d
1fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// Copyright 2005, Google Inc. 2fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// All rights reserved. 3fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// 4fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// Redistribution and use in source and binary forms, with or without 5fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// modification, are permitted provided that the following conditions are 6fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// met: 7fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// 8fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// * Redistributions of source code must retain the above copyright 9fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// notice, this list of conditions and the following disclaimer. 10fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// * Redistributions in binary form must reproduce the above 11fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// copyright notice, this list of conditions and the following disclaimer 12fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// in the documentation and/or other materials provided with the 13fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// distribution. 14fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// * Neither the name of Google Inc. nor the names of its 15fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// contributors may be used to endorse or promote products derived from 16fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// this software without specific prior written permission. 17fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// 18fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 19fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 20fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 21fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 22fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 23fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 24fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 25fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 26fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 27fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 28fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 29fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville 30fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// A sample program demonstrating using Google C++ testing framework. 31fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// 32fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// Author: wan@google.com (Zhanyong Wan) 33fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville 34fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville#ifndef GTEST_SAMPLES_SAMPLE3_INL_H_ 35fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville#define GTEST_SAMPLES_SAMPLE3_INL_H_ 36fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville 37fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville#include <stddef.h> 38fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville 39fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville 40fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// Queue is a simple queue implemented as a singled-linked list. 41fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// 42fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// The element type must support copy constructor. 43fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Savilletemplate <typename E> // E is the element type 44fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Savilleclass Queue; 45fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville 46fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// QueueNode is a node in a Queue, which consists of an element of 47fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// type E and a pointer to the next node. 48fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Savilletemplate <typename E> // E is the element type 49fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Savilleclass QueueNode { 50fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville friend class Queue<E>; 51fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville 52fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville public: 53fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville // Gets the element in this node. 54fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville const E & element() const { return element_; } 55fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville 56fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville // Gets the next node in the queue. 57fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville QueueNode * next() { return next_; } 58fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville const QueueNode * next() const { return next_; } 59fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville 60fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville private: 61fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville // Creates a node with a given element value. The next pointer is 62fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville // set to NULL. 63fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville QueueNode(const E & element) : element_(element), next_(NULL) {} 64fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville 65fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville // We disable the default assignment operator and copy c'tor. 66fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville const QueueNode & operator = (const QueueNode &); 67fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville QueueNode(const QueueNode &); 68fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville 69fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville E element_; 70fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville QueueNode * next_; 71fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville}; 72fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville 73fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Savilletemplate <typename E> // E is the element type. 74fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Savilleclass Queue { 75fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Savillepublic: 76fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville 77fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville // Creates an empty queue. 78fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville Queue() : head_(NULL), last_(NULL), size_(0) {} 79fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville 80fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville // D'tor. Clears the queue. 81fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville ~Queue() { Clear(); } 82fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville 83fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville // Clears the queue. 84fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville void Clear() { 85fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville if (size_ > 0) { 86fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville // 1. Deletes every node. 87fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville QueueNode<E> * node = head_; 88fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville QueueNode<E> * next = node->next(); 89fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville for (; ;) { 90fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville delete node; 91fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville node = next; 92fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville if (node == NULL) break; 93fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville next = node->next(); 94fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville } 95fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville 96fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville // 2. Resets the member variables. 97fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville head_ = last_ = NULL; 98fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville size_ = 0; 99fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville } 100fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville } 101fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville 102fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville // Gets the number of elements. 103fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville size_t Size() const { return size_; } 104fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville 105fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville // Gets the first element of the queue, or NULL if the queue is empty. 106fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville QueueNode<E> * Head() { return head_; } 107fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville const QueueNode<E> * Head() const { return head_; } 108fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville 109fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville // Gets the last element of the queue, or NULL if the queue is empty. 110fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville QueueNode<E> * Last() { return last_; } 111fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville const QueueNode<E> * Last() const { return last_; } 112fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville 113fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville // Adds an element to the end of the queue. A copy of the element is 114fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville // created using the copy constructor, and then stored in the queue. 115fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville // Changes made to the element in the queue doesn't affect the source 116fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville // object, and vice versa. 117fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville void Enqueue(const E & element) { 118fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville QueueNode<E> * new_node = new QueueNode<E>(element); 119fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville 120fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville if (size_ == 0) { 121fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville head_ = last_ = new_node; 122fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville size_ = 1; 123fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville } else { 124fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville last_->next_ = new_node; 125fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville last_ = new_node; 126fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville size_++; 127fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville } 128fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville } 129fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville 130fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville // Removes the head of the queue and returns it. Returns NULL if 131fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville // the queue is empty. 132fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville E * Dequeue() { 133fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville if (size_ == 0) { 134fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville return NULL; 135fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville } 136fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville 137fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville const QueueNode<E> * const old_head = head_; 138fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville head_ = head_->next_; 139fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville size_--; 140fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville if (size_ == 0) { 141fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville last_ = NULL; 142fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville } 143fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville 144fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville E * element = new E(old_head->element()); 145fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville delete old_head; 146fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville 147fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville return element; 148fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville } 149fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville 150fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville // Applies a function/functor on each element of the queue, and 151fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville // returns the result in a new queue. The original queue is not 152fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville // affected. 153fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville template <typename F> 154fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville Queue * Map(F function) const { 155fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville Queue * new_queue = new Queue(); 156fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville for (const QueueNode<E> * node = head_; node != NULL; node = node->next_) { 157fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville new_queue->Enqueue(function(node->element())); 158fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville } 159fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville 160fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville return new_queue; 161fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville } 162fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville 163fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville private: 164fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville QueueNode<E> * head_; // The first node of the queue. 165fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville QueueNode<E> * last_; // The last node of the queue. 166fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville size_t size_; // The number of elements in the queue. 167fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville 168fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville // We disallow copying a queue. 169fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville Queue(const Queue &); 170fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville const Queue & operator = (const Queue &); 171fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville }; 172fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville 173fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville#endif // GTEST_SAMPLES_SAMPLE3_INL_H_ 174