12a4077a9186d6f6dff104bbd1a73484aae6d5c01Nicolas Catania/* -*- c++ -*- */
22a4077a9186d6f6dff104bbd1a73484aae6d5c01Nicolas Catania/*
32a4077a9186d6f6dff104bbd1a73484aae6d5c01Nicolas Catania * Copyright (C) 2009 The Android Open Source Project
42a4077a9186d6f6dff104bbd1a73484aae6d5c01Nicolas Catania * All rights reserved.
52a4077a9186d6f6dff104bbd1a73484aae6d5c01Nicolas Catania *
62a4077a9186d6f6dff104bbd1a73484aae6d5c01Nicolas Catania * Redistribution and use in source and binary forms, with or without
72a4077a9186d6f6dff104bbd1a73484aae6d5c01Nicolas Catania * modification, are permitted provided that the following conditions
82a4077a9186d6f6dff104bbd1a73484aae6d5c01Nicolas Catania * are met:
92a4077a9186d6f6dff104bbd1a73484aae6d5c01Nicolas Catania *  * Redistributions of source code must retain the above copyright
102a4077a9186d6f6dff104bbd1a73484aae6d5c01Nicolas Catania *    notice, this list of conditions and the following disclaimer.
112a4077a9186d6f6dff104bbd1a73484aae6d5c01Nicolas Catania *  * Redistributions in binary form must reproduce the above copyright
122a4077a9186d6f6dff104bbd1a73484aae6d5c01Nicolas Catania *    notice, this list of conditions and the following disclaimer in
132a4077a9186d6f6dff104bbd1a73484aae6d5c01Nicolas Catania *    the documentation and/or other materials provided with the
142a4077a9186d6f6dff104bbd1a73484aae6d5c01Nicolas Catania *    distribution.
152a4077a9186d6f6dff104bbd1a73484aae6d5c01Nicolas Catania *
162a4077a9186d6f6dff104bbd1a73484aae6d5c01Nicolas Catania * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
172a4077a9186d6f6dff104bbd1a73484aae6d5c01Nicolas Catania * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
182a4077a9186d6f6dff104bbd1a73484aae6d5c01Nicolas Catania * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
192a4077a9186d6f6dff104bbd1a73484aae6d5c01Nicolas Catania * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
202a4077a9186d6f6dff104bbd1a73484aae6d5c01Nicolas Catania * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
212a4077a9186d6f6dff104bbd1a73484aae6d5c01Nicolas Catania * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
222a4077a9186d6f6dff104bbd1a73484aae6d5c01Nicolas Catania * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
232a4077a9186d6f6dff104bbd1a73484aae6d5c01Nicolas Catania * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
242a4077a9186d6f6dff104bbd1a73484aae6d5c01Nicolas Catania * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
252a4077a9186d6f6dff104bbd1a73484aae6d5c01Nicolas Catania * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
262a4077a9186d6f6dff104bbd1a73484aae6d5c01Nicolas Catania * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
272a4077a9186d6f6dff104bbd1a73484aae6d5c01Nicolas Catania * SUCH DAMAGE.
282a4077a9186d6f6dff104bbd1a73484aae6d5c01Nicolas Catania */
292a4077a9186d6f6dff104bbd1a73484aae6d5c01Nicolas Catania
302a4077a9186d6f6dff104bbd1a73484aae6d5c01Nicolas Catania#ifndef ANDROID_ASTL_TESTS_COMMON__
312a4077a9186d6f6dff104bbd1a73484aae6d5c01Nicolas Catania#define ANDROID_ASTL_TESTS_COMMON__
322a4077a9186d6f6dff104bbd1a73484aae6d5c01Nicolas Catania#include <cstdio>
332a4077a9186d6f6dff104bbd1a73484aae6d5c01Nicolas Catania
342a4077a9186d6f6dff104bbd1a73484aae6d5c01Nicolas Catania// Classes and macros used in tests.
352a4077a9186d6f6dff104bbd1a73484aae6d5c01Nicolas Catanianamespace {
362a4077a9186d6f6dff104bbd1a73484aae6d5c01Nicolas Cataniaconst size_t kMaxSizeT = ~((size_t)0);
372a4077a9186d6f6dff104bbd1a73484aae6d5c01Nicolas Cataniaconst int kPassed = 0;
382a4077a9186d6f6dff104bbd1a73484aae6d5c01Nicolas Cataniaconst int kFailed = 1;
392a4077a9186d6f6dff104bbd1a73484aae6d5c01Nicolas Catania#define FAIL_UNLESS(v) if (!android::v()) return kFailed;
402a4077a9186d6f6dff104bbd1a73484aae6d5c01Nicolas Catania
412a4077a9186d6f6dff104bbd1a73484aae6d5c01Nicolas Catania#define EXPECT_TRUE(expr)                                   \
422a4077a9186d6f6dff104bbd1a73484aae6d5c01Nicolas Catania    if (!(expr)) {                                          \
432a4077a9186d6f6dff104bbd1a73484aae6d5c01Nicolas Catania        std::fprintf(stderr, "%d: %s\n", __LINE__, #expr);	\
442a4077a9186d6f6dff104bbd1a73484aae6d5c01Nicolas Catania        return false;                                       \
452a4077a9186d6f6dff104bbd1a73484aae6d5c01Nicolas Catania    }
462a4077a9186d6f6dff104bbd1a73484aae6d5c01Nicolas Catania
47dfec9fcb74ce3381af05f54e6ebc2667a6bfb6b8Nicolas Catania#define EXPECT_FALSE(expr) EXPECT_TRUE(!(expr))
48dfec9fcb74ce3381af05f54e6ebc2667a6bfb6b8Nicolas Catania
49dfec9fcb74ce3381af05f54e6ebc2667a6bfb6b8Nicolas Catania
502a4077a9186d6f6dff104bbd1a73484aae6d5c01Nicolas Catania#ifndef ARRAYSIZE
512a4077a9186d6f6dff104bbd1a73484aae6d5c01Nicolas Catania#define ARRAYSIZE(array) (sizeof(array)/sizeof(array[0]))
522a4077a9186d6f6dff104bbd1a73484aae6d5c01Nicolas Catania#endif
532a4077a9186d6f6dff104bbd1a73484aae6d5c01Nicolas Catania
542a4077a9186d6f6dff104bbd1a73484aae6d5c01Nicolas Catania// Cannot be copied.
552a4077a9186d6f6dff104bbd1a73484aae6d5c01Nicolas Cataniastruct NoCopy {
562a4077a9186d6f6dff104bbd1a73484aae6d5c01Nicolas Catania  private:
572a4077a9186d6f6dff104bbd1a73484aae6d5c01Nicolas Catania    NoCopy(const NoCopy& nc) {}
582a4077a9186d6f6dff104bbd1a73484aae6d5c01Nicolas Catania};
592a4077a9186d6f6dff104bbd1a73484aae6d5c01Nicolas Catania
602a4077a9186d6f6dff104bbd1a73484aae6d5c01Nicolas Catania// Count the number of assignement.
612a4077a9186d6f6dff104bbd1a73484aae6d5c01Nicolas Cataniastruct CopyCounter {
622a4077a9186d6f6dff104bbd1a73484aae6d5c01Nicolas Catania    static size_t mCount;
632a4077a9186d6f6dff104bbd1a73484aae6d5c01Nicolas Catania
642a4077a9186d6f6dff104bbd1a73484aae6d5c01Nicolas Catania    CopyCounter() { }
652a4077a9186d6f6dff104bbd1a73484aae6d5c01Nicolas Catania    CopyCounter& operator=(const CopyCounter& cc) {return *this; }
662a4077a9186d6f6dff104bbd1a73484aae6d5c01Nicolas Catania    CopyCounter(const CopyCounter& nc) {++mCount;}
672a4077a9186d6f6dff104bbd1a73484aae6d5c01Nicolas Catania  private:
682a4077a9186d6f6dff104bbd1a73484aae6d5c01Nicolas Catania};
692a4077a9186d6f6dff104bbd1a73484aae6d5c01Nicolas Catania
702a4077a9186d6f6dff104bbd1a73484aae6d5c01Nicolas Cataniaclass CtorDtorCounter {
712a4077a9186d6f6dff104bbd1a73484aae6d5c01Nicolas Catania  public:
722a4077a9186d6f6dff104bbd1a73484aae6d5c01Nicolas Catania    static size_t mCtorCount;
732a4077a9186d6f6dff104bbd1a73484aae6d5c01Nicolas Catania    static size_t mCopyCtorCount;
742a4077a9186d6f6dff104bbd1a73484aae6d5c01Nicolas Catania    static size_t mAssignCount;
752a4077a9186d6f6dff104bbd1a73484aae6d5c01Nicolas Catania    static size_t mDtorCount;
762a4077a9186d6f6dff104bbd1a73484aae6d5c01Nicolas Catania
772a4077a9186d6f6dff104bbd1a73484aae6d5c01Nicolas Catania    CtorDtorCounter() {++mCtorCount;}
782a4077a9186d6f6dff104bbd1a73484aae6d5c01Nicolas Catania    CtorDtorCounter(const CtorDtorCounter& nc) {++mCopyCtorCount;}
792a4077a9186d6f6dff104bbd1a73484aae6d5c01Nicolas Catania    CtorDtorCounter& operator=(const CtorDtorCounter& nc) {++mAssignCount; return *this;}
802a4077a9186d6f6dff104bbd1a73484aae6d5c01Nicolas Catania    ~CtorDtorCounter() {++mDtorCount;}
812a4077a9186d6f6dff104bbd1a73484aae6d5c01Nicolas Catania    static void reset() {mCtorCount = 0; mCopyCtorCount = 0; mAssignCount = 0; mDtorCount = 0;}
82fe47cee745a2b91cd9a2b98f7a82c9ad9fec726fNicolas Catania    static void printf() {
83fe47cee745a2b91cd9a2b98f7a82c9ad9fec726fNicolas Catania        std::fprintf(stderr, "CtorDtorCounter: %d %d %d %d\n",
84fe47cee745a2b91cd9a2b98f7a82c9ad9fec726fNicolas Catania                     mCtorCount, mCopyCtorCount, mAssignCount, mDtorCount);
85fe47cee745a2b91cd9a2b98f7a82c9ad9fec726fNicolas Catania    }
862a4077a9186d6f6dff104bbd1a73484aae6d5c01Nicolas Catania  private:
872a4077a9186d6f6dff104bbd1a73484aae6d5c01Nicolas Catania};
882a4077a9186d6f6dff104bbd1a73484aae6d5c01Nicolas Catania
892a4077a9186d6f6dff104bbd1a73484aae6d5c01Nicolas Cataniasize_t CopyCounter::mCount;
902a4077a9186d6f6dff104bbd1a73484aae6d5c01Nicolas Cataniasize_t CtorDtorCounter::mCtorCount;
912a4077a9186d6f6dff104bbd1a73484aae6d5c01Nicolas Cataniasize_t CtorDtorCounter::mCopyCtorCount;
922a4077a9186d6f6dff104bbd1a73484aae6d5c01Nicolas Cataniasize_t CtorDtorCounter::mAssignCount;
932a4077a9186d6f6dff104bbd1a73484aae6d5c01Nicolas Cataniasize_t CtorDtorCounter::mDtorCount;
942a4077a9186d6f6dff104bbd1a73484aae6d5c01Nicolas Catania
95808f34a8cab52569bfca26cec6fd96740aa2ea25Nicolas Catania// These class allocate chunks to detect memory leaks.
96808f34a8cab52569bfca26cec6fd96740aa2ea25Nicolas Cataniatemplate<typename T> struct A {
97808f34a8cab52569bfca26cec6fd96740aa2ea25Nicolas Catania  public:
98808f34a8cab52569bfca26cec6fd96740aa2ea25Nicolas Catania    A() {mChunk = new T[2046];}
99808f34a8cab52569bfca26cec6fd96740aa2ea25Nicolas Catania    A(const A<T>& a) {mChunk = new T[2046];}
100808f34a8cab52569bfca26cec6fd96740aa2ea25Nicolas Catania    virtual ~A() {delete [] mChunk;}
101808f34a8cab52569bfca26cec6fd96740aa2ea25Nicolas Catania    A& operator=(const A& o) { return *this;}
102808f34a8cab52569bfca26cec6fd96740aa2ea25Nicolas Catania    T *mChunk;
103808f34a8cab52569bfca26cec6fd96740aa2ea25Nicolas Catania};
104808f34a8cab52569bfca26cec6fd96740aa2ea25Nicolas Catania
105808f34a8cab52569bfca26cec6fd96740aa2ea25Nicolas Cataniastruct B {
106808f34a8cab52569bfca26cec6fd96740aa2ea25Nicolas Catania  public:
107808f34a8cab52569bfca26cec6fd96740aa2ea25Nicolas Catania    B() {mChunk = new char[2046];}
108808f34a8cab52569bfca26cec6fd96740aa2ea25Nicolas Catania    B(const B& b) {mChunk = new char[2046];}
109808f34a8cab52569bfca26cec6fd96740aa2ea25Nicolas Catania    virtual ~B() {delete [] mChunk;}
110808f34a8cab52569bfca26cec6fd96740aa2ea25Nicolas Catania    B& operator=(const B& o) { return *this;}
111808f34a8cab52569bfca26cec6fd96740aa2ea25Nicolas Catania    char *mChunk;
112808f34a8cab52569bfca26cec6fd96740aa2ea25Nicolas Catania};
113808f34a8cab52569bfca26cec6fd96740aa2ea25Nicolas Catania
1142a4077a9186d6f6dff104bbd1a73484aae6d5c01Nicolas Catania}  // anonymous namespace
1152a4077a9186d6f6dff104bbd1a73484aae6d5c01Nicolas Catania
1162a4077a9186d6f6dff104bbd1a73484aae6d5c01Nicolas Catania
1172a4077a9186d6f6dff104bbd1a73484aae6d5c01Nicolas Catania#endif  // ANDROID_ASTL_TEST_COMMON__
118