# HG changeset patch # User David Anderson # Date 1377584283 25200 # Node ID cfe8009088372a8252fd3b6f0324341df4a4c77e # Parent 0ac292cc1467cf728bcd5782c96263ab5730e018 Add tests. diff -r 0ac292cc1467 -r cfe800908837 AMBuildScript --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/AMBuildScript Mon Aug 26 23:18:03 2013 -0700 @@ -0,0 +1,159 @@ +# vim: set ts=2 sw=2 tw=99 noet ft=python: +import os +import sys +from ambuild.command import SymlinkCommand + +class KE: + def __init__(self): + self.compiler = Cpp.Compiler() + + if AMBuild.mode == 'config': + #Detect compilers + self.compiler.DetectAll(AMBuild) + + #Set up defines + cxx = self.compiler.cxx + if isinstance(cxx, Cpp.CompatGCC): + if isinstance(cxx, Cpp.GCC): + self.vendor = 'gcc' + elif isinstance(cxx, Cpp.Clang): + self.vendor = 'clang' + self.compiler.AddToListVar('CFLAGS', '-pipe') + self.compiler.AddToListVar('CFLAGS', '-Wall') + self.compiler.AddToListVar('CFLAGS', '-Werror') + + if AMBuild.options.arch == 'x86': + self.compiler.AddToListVar('CFLAGS', '-m32') + self.compiler.AddToListVar('POSTLINKFLAGS', '-m32') + elif AMBuild.options.arch == 'x64': + self.compiler.AddToListVar('CFLAGS', '-m64') + self.compiler.AddToListVar('POSTLINKFLAGS', '-m64') + elif AMBuild.options.arch: + raise Exception('Unknown architecture: {0}'.format(AMBuild.options.arch)) + + # Disable some stuff we don't use, that gives us better binary + # compatibility on Linux. + self.compiler.AddToListVar('CXXFLAGS', '-fno-exceptions') + self.compiler.AddToListVar('CXXFLAGS', '-fno-rtti') + self.compiler.AddToListVar('CXXFLAGS', '-fno-threadsafe-statics') + + # We don't really care about these. + self.compiler.AddToListVar('CXXFLAGS', '-Wno-non-virtual-dtor') + self.compiler.AddToListVar('CXXFLAGS', '-Wno-overloaded-virtual') + + # The compiler is too aggressive about what what should be a valid offsetof. + self.compiler.AddToListVar('CXXFLAGS', '-Wno-invalid-offsetof') + + # This would otherwise forbid ((x = y) == NULL) in clang apparently. + if self.vendor == 'clang' and cxx.majorVersion > 1: + self.compiler.AddToListVar('CXXFLAGS', '-Wno-null-arithmetic') + if self.vendor == 'clang' and \ + cxx.majorVersion >= 3 and \ + cxx.minorVersion >= 3: + self.compiler.AddToListVar('CXXFLAGS', '-Wno-unused-private-field') + + if (self.vendor == 'gcc' and cxx.majorVersion >= 4 and cxx.minorVersion >= 7) or \ + (self.vendor == 'clang' and cxx.majorVersion >= 3): + self.compiler.AddToListVar('CXXFLAGS', '-Wno-delete-non-virtual-dtor') + + self.compiler.AddToListVar('POSTLINKFLAGS', '-lpthread') + self.compiler.AddToListVar('POSTLINKFLAGS', '-lrt') + elif isinstance(cxx, Cpp.MSVC): + self.vendor = 'msvc' + if AMBuild.options.debug == '1': + self.compiler.AddToListVar('CFLAGS', '/MTd') + self.compiler.AddToListVar('POSTLINKFLAGS', '/NODEFAULTLIB:libcmt') + else: + self.compiler.AddToListVar('CFLAGS', '/MT') + self.compiler.AddToListVar('CDEFINES', 'WIN32') + self.compiler.AddToListVar('CDEFINES', '_CRT_SECURE_NO_DEPRECATE') + self.compiler.AddToListVar('CDEFINES', '_CRT_SECURE_NO_WARNINGS') + self.compiler.AddToListVar('CDEFINES', '_CRT_NONSTDC_NO_DEPRECATE') + self.compiler.AddToListVar('CXXFLAGS', '/EHsc') + self.compiler.AddToListVar('CXXFLAGS', '/GR-') + self.compiler.AddToListVar('CFLAGS', '/W3') + self.compiler.AddToListVar('CFLAGS', '/nologo') + self.compiler.AddToListVar('CFLAGS', '/Zi') + self.compiler.AddToListVar('CXXFLAGS', '/TP') + self.compiler.AddToListVar('POSTLINKFLAGS', '/DEBUG') + + self.compiler.AddToListVar('POSTLINKFLAGS', 'kernel32.lib') + self.compiler.AddToListVar('POSTLINKFLAGS', 'user32.lib') + self.compiler.AddToListVar('POSTLINKFLAGS', 'gdi32.lib') + self.compiler.AddToListVar('POSTLINKFLAGS', 'winspool.lib') + self.compiler.AddToListVar('POSTLINKFLAGS', 'comdlg32.lib') + self.compiler.AddToListVar('POSTLINKFLAGS', 'advapi32.lib') + self.compiler.AddToListVar('POSTLINKFLAGS', 'shell32.lib') + self.compiler.AddToListVar('POSTLINKFLAGS', 'ole32.lib') + self.compiler.AddToListVar('POSTLINKFLAGS', 'oleaut32.lib') + self.compiler.AddToListVar('POSTLINKFLAGS', 'uuid.lib') + self.compiler.AddToListVar('POSTLINKFLAGS', 'odbc32.lib') + self.compiler.AddToListVar('POSTLINKFLAGS', 'odbccp32.lib') + + #Optimization + if AMBuild.options.opt == '1': + self.compiler.AddToListVar('CDEFINES', 'NDEBUG') + if self.vendor == 'gcc' or self.vendor == 'clang': + self.compiler.AddToListVar('CFLAGS', '-O3') + elif self.vendor == 'msvc': + self.compiler.AddToListVar('CFLAGS', '/Ox') + self.compiler.AddToListVar('POSTLINKFLAGS', '/OPT:ICF') + self.compiler.AddToListVar('POSTLINKFLAGS', '/OPT:REF') + + #Debugging + if AMBuild.options.debug == '1': + if self.vendor == 'gcc' or self.vendor == 'clang': + self.compiler.AddToListVar('CFLAGS', '-g3') + self.compiler.AddToListVar('CFLAGS', '-O0') + elif self.vendor == 'msvc': + self.compiler.AddToListVar('CFLAGS', '/Od') + self.compiler.AddToListVar('CFLAGS', '/RTC1') + + #Platform-specifics + if AMBuild.target['platform'] == 'linux': + if self.vendor == 'gcc': + self.compiler.AddToListVar('POSTLINKFLAGS', '-static-libgcc') + if self.vendor == 'clang': + self.compiler.AddToListVar('POSTLINKFLAGS', '-lgcc_eh') + elif AMBuild.target['platform'] == 'darwin': + self.compiler.AddToListVar('POSTLINKFLAGS', '-mmacosx-version-min=10.5') + if AMBuild.options.arch == 'x86': + self.compiler.AddToListVar('POSTLINKFLAGS', ['-arch', 'i386']) + elif AMBuild.options.arch == 'x64': + self.compiler.AddToListVar('POSTLINKFLAGS', ['-arch', 'x86_64']) + self.compiler.AddToListVar('POSTLINKFLAGS', '-lstdc++') + + #Finish up + self.compiler.ToConfig(AMBuild, 'compiler') + AMBuild.cache.CacheVariable('vendor', self.vendor) + self.targetMap = { } + AMBuild.cache.CacheVariable('targetMap', self.targetMap) + else: + self.compiler.FromConfig(AMBuild, 'compiler') + self.targetMap = AMBuild.cache['targetMap'] + + def DefaultCompiler(self): + return self.compiler.Clone() + + def JobMatters(self, jobname): + file = sys._getframe().f_code.co_filename + if AMBuild.mode == 'config': + self.targetMap[jobname] = file + return True + if len(AMBuild.args) == 0: + return True + if not jobname in AMBuild.args: + return False + +ke = KE() +globals = { + 'KE': ke +} + +FileList = [ + ['tests', 'AMBuild.tests'], + ] + +for parts in FileList: + AMBuild.Include(os.path.join(*parts), globals) + diff -r 0ac292cc1467 -r cfe800908837 configure.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/configure.py Mon Aug 26 23:18:03 2013 -0700 @@ -0,0 +1,28 @@ +# vim: set ts=2 sw=2 tw=99 noet ft=python: +# +# Copyright (C) 2004-2012 David Anderson +# +# This file is part of SourcePawn. +# +# SourcePawn is free software: you can redistribute it and/or modify it under +# the terms of the GNU General Public License as published by the Free +# Software Foundation, either version 3 of the License, or (at your option) +# any later version. +# +# SourcePawn is distributed in the hope that it will be useful, but WITHOUT ANY +# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS +# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License along with +# SourcePawn. If not, see http://www.gnu.org/licenses/. +# +import sys +import ambuild.runner as runner + +run = runner.Runner() +run.options.add_option('--enable-debug', action='store_const', const='1', dest='debug', + help='Enable debugging symbols') +run.options.add_option('--enable-optimize', action='store_const', const='1', dest='opt', + help='Enable optimization') +run.options.add_option('--arch', dest='arch', help='Architecture (x86, x64)') +run.Configure(sys.path[0]) diff -r 0ac292cc1467 -r cfe800908837 include/am-inlinelist.h --- a/include/am-inlinelist.h Mon Aug 26 20:41:58 2013 -0700 +++ b/include/am-inlinelist.h Mon Aug 26 23:18:03 2013 -0700 @@ -31,11 +31,14 @@ #define _include_amtl_inline_list_h_ #include +#include namespace ke { template class InlineList; +// Objects can recursively inherit from InlineListNode in order to have +// membership in an InlineList. template class InlineListNode { @@ -43,14 +46,14 @@ public: InlineListNode() - : next_(NULL), - prev_(NULL) + : next_(NULL), + prev_(NULL) { } InlineListNode(InlineListNode *next, InlineListNode *prev) - : next_(next), - prev_(prev) + : next_(next), + prev_(prev) { } @@ -59,6 +62,14 @@ InlineListNode *prev_; }; +// An InlineList is a linked list that threads link pointers through objects, +// rather than allocating node memory. A node can be in at most one list at +// any time. +// +// Since InlineLists are designed to be very cheap, there is no requirement +// that elements be removed from a list once the list is destructed. However, +// for as long as the list is alive, all of its contained nodes must also +// be alive. template class InlineList { @@ -66,10 +77,24 @@ Node head_; + // Work around a clang bug where we can't initialize with &head_ in the ctor. + inline Node *head() { + return &head_; + } + public: InlineList() - : head_(&head_, &head_) + : head_(head(), head()) + { + } + + ~InlineList() { +#if !defined(NDEBUG) + // Remove all items to clear their next/prev fields. + while (begin() != end()) + remove(*begin()); +#endif } public: @@ -86,7 +111,7 @@ iterator & operator ++() { iter_ = iter_->next; - return *iter_; + return *this; } iterator operator ++(int) { iterator old(*this); @@ -105,14 +130,6 @@ bool operator ==(const iterator &where) const { return iter_ == where.iter_; } - iterator prev() const { - iterator p(iter_->prev_); - return p; - } - iterator next() const { - iterator p(iter_->next_); - return p; - } }; iterator begin() { @@ -123,12 +140,20 @@ return iterator(&head_); } - void erase(Node *t) { + void remove(Node *t) { t->prev_->next_ = t->next_; t->next_->prev_ = t->prev_; + +#if !defined(NDEBUG) + t->next_ = NULL; + t->prev_ = NULL; +#endif } - void insert(Node *t) { + void append(Node *t) { + assert(!t->next_); + assert(!t->prev_); + t->prev_ = head_.prev_; t->next_ = &head_; head_.prev_->next_ = t; diff -r 0ac292cc1467 -r cfe800908837 include/am-linkedlist.h --- a/include/am-linkedlist.h Mon Aug 26 20:41:58 2013 -0700 +++ b/include/am-linkedlist.h Mon Aug 26 23:18:03 2013 -0700 @@ -34,6 +34,7 @@ #include #include #include +#include namespace ke { @@ -58,6 +59,10 @@ : obj(o) { } + Node(Moveable o) + : obj(o) + { + } T obj; Node *next; @@ -68,29 +73,28 @@ LinkedList(AllocPolicy = AllocPolicy()) : length_(0) { + head()->prev = head(); + head()->next = head(); } ~LinkedList() { clear(); } bool append(const T &obj) { - Node *node = allocNode(obj); - if (!node) - return false; - - node->prev = head()->prev; - node->next = head(); - head()->prev->next = node; - head()->prev = node; - - length_++; + return insertBefore(end(), obj) != end(); + } + bool append(Moveable obj) { + return insertBefore(end(), obj) != end(); } bool prepend(const T &obj) { - return insert(begin(), obj); + return insertBefore(begin(), obj) != begin(); + } + bool prepend(Moveable obj) { + return insertBefore(begin(), obj) != begin(); } - size_t size() const { + size_t length() const { return length_; } @@ -137,6 +141,13 @@ new (node) Node(obj); return node; } + Node *allocNode(Moveable obj) { + Node *node = (Node *)this->malloc(sizeof(Node)); + if (!node) + return NULL; + new (node) Node(obj); + return node; + } void freeNode(Node *node) { node->obj.~T(); @@ -221,29 +232,9 @@ Node *this_; }; - public: - iterator begin() const { - return iterator(head()->next); - } - iterator end() const { - return iterator(head()); - } - iterator erase(iterator &where) { - Node *pNode = where.this_; - iterator iter(where); - iter++; - - pNode->prev->next = pNode->next; - pNode->next->prev = pNode->prev; - - freeNode(pNode); - length_--; - - return iter; - } - iterator insert(iterator where, const T &obj) { - // Insert obj right before where - Node *node = allocNode(obj); + private: + // Insert obj right before where. + iterator insert(iterator where, Node *node) { if (!node) return where; @@ -259,6 +250,34 @@ } public: + iterator begin() { + return iterator(head()->next); + } + iterator end() { + return iterator(head()); + } + iterator erase(iterator where) { + Node *pNode = where.this_; + iterator iter(where); + iter++; + + pNode->prev->next = pNode->next; + pNode->next->prev = pNode->prev; + + freeNode(pNode); + length_--; + + return iter; + } + iterator insertBefore(iterator where, const T &obj) { + return insert(where, allocNode(obj)); + } + iterator insertBefore(iterator where, Moveable obj) { + return insert(where, allocNode(obj)); + } + + public: + // Removes one instance of |obj| from the list, if found. void remove(const T &obj) { for (iterator b = begin(); b != end(); b++) { if (*b == obj) { @@ -269,7 +288,7 @@ } template - iterator find(const U &equ) const { + iterator find(const U &equ) { for (iterator iter = begin(); iter != end(); iter++) { if (*iter == equ) return iter; diff -r 0ac292cc1467 -r cfe800908837 include/am-moveable.h --- a/include/am-moveable.h Mon Aug 26 20:41:58 2013 -0700 +++ b/include/am-moveable.h Mon Aug 26 23:18:03 2013 -0700 @@ -61,6 +61,13 @@ T &t_; }; +template +static inline Moveable +Move(T &t) +{ + return Moveable(t); +} + } // namespace ke #endif // _include_amtl_moveable_h_ diff -r 0ac292cc1467 -r cfe800908837 include/am-vector.h --- a/include/am-vector.h Mon Aug 26 20:41:58 2013 -0700 +++ b/include/am-vector.h Mon Aug 26 23:18:03 2013 -0700 @@ -43,25 +43,31 @@ { public: Vector(AllocPolicy = AllocPolicy()) - : data_(NULL), - nitems_(0), - maxsize_(0) + : data_(NULL), + nitems_(0), + maxsize_(0) { } + Vector(Moveable > other) { + data_ = other->data_; + nitems_ = other->nitems_; + maxsize_ = other->maxsize_; + other->reset(); + } + ~Vector() { zap(); } - void steal(Vector &other) { - zap(); - data_ = other.data_; - nitems_ = other.nitems_; - maxsize_ = other.maxsize_; - other.reset(); + bool append(const T &item) { + if (!growIfNeeded(1)) + return false; + new (&data_[nitems_]) T(item); + nitems_++; + return true; } - - bool append(const T &item) { + bool append(Moveable item) { if (!growIfNeeded(1)) return false; new (&data_[nitems_]) T(item); @@ -73,6 +79,12 @@ new (&data_[nitems_]) T(item); nitems_++; } + void infallibleAppend(Moveable item) { + assert(growIfNeeded(1)); + new (&data_[nitems_]) T(item); + nitems_++; + } + T popCopy() { T t = at(length() - 1); pop(); diff -r 0ac292cc1467 -r cfe800908837 tests/AMBuild.tests --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/tests/AMBuild.tests Mon Aug 26 23:18:03 2013 -0700 @@ -0,0 +1,42 @@ +# vim: set ts=2 sw=2 tw=99 noet ft=python: +# +# Copyright (C) 2004-2012 David Anderson +# +# This file is part of SourcePawn. +# +# SourcePawn is free software: you can redistribute it and/or modify it under +# the terms of the GNU General Public License as published by the Free +# Software Foundation, either version 3 of the License, or (at your option) +# any later version. +# +# SourcePawn is distributed in the hope that it will be useful, but WITHOUT ANY +# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS +# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License along with +# SourcePawn. If not, see http://www.gnu.org/licenses/. +# +import os +import os.path +import ambuild.osutil as osutil + +compiler = KE.DefaultCompiler() +compiler.AddToListVar('CXXINCLUDES', os.path.join(AMBuild.sourceFolder, 'include')) + +if compiler.cc.name == 'msvc': + compiler.AddToListVar('POSTLINKFLAGS', '/SUBSYSTEM:CONSOLE') + +job = AMBuild.AddJob('testrun') +binary = Cpp.ExecutableBuilder('testrun', AMBuild, job, compiler) +files = [ + 'main.cpp', + 'test-inlinelist.cpp', + 'test-linkedlist.cpp', + 'test-vector.cpp', + 'test-refcounting.cpp', + 'test-threadutils.cpp', +] + +binary.AddSourceFiles('tests', files) +binary.SendToJob() + diff -r 0ac292cc1467 -r cfe800908837 tests/main.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/tests/main.cpp Mon Aug 26 23:18:03 2013 -0700 @@ -0,0 +1,68 @@ +// vim: set sts=8 ts=2 sw=2 tw=99 et: +// +// Copyright (C) 2013, David Anderson and AlliedModders LLC +// All rights reserved. +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are met: +// +// * Redistributions of source code must retain the above copyright notice, this +// list of conditions and the following disclaimer. +// * Redistributions in binary form must reproduce the above copyright notice, +// this list of conditions and the following disclaimer in the documentation +// and/or other materials provided with the distribution. +// * Neither the name of AlliedModders LLC nor the names of its contributors +// may be used to endorse or promote products derived from this software +// without specific prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" +// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE +// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR +// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF +// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS +// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN +// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) +// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +// POSSIBILITY OF SUCH DAMAGE. + +#include +#include +#include +#include "runner.h" + +using namespace ke; + +Test *Test::head = NULL; + +int main(int argc, char **argv) +{ + Test *test = Test::first(); + while (test) { + fprintf(stdout, "Testing %s... \n", test->name()); + if (!test->Run()) { + fprintf(stdout, "TEST:%s FAIL\n", test->name()); + break; + } + fprintf(stdout, "TEST:%s OK\n", test->name()); + test = test->next(); + } +} + +#if defined(__GNUC__) +extern "C" void __cxa_pure_virtual() +{ + abort(); +} + +void *operator new(size_t amount) +{ + return malloc(amount); +} + +void operator delete(void *p) +{ + free(p); +} +#endif diff -r 0ac292cc1467 -r cfe800908837 tests/runner.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/tests/runner.h Mon Aug 26 23:18:03 2013 -0700 @@ -0,0 +1,131 @@ +// vim: set sts=8 ts=2 sw=2 tw=99 et: +// +// Copyright (C) 2013, David Anderson and AlliedModders LLC +// All rights reserved. +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are met: +// +// * Redistributions of source code must retain the above copyright notice, this +// list of conditions and the following disclaimer. +// * Redistributions in binary form must reproduce the above copyright notice, +// this list of conditions and the following disclaimer in the documentation +// and/or other materials provided with the distribution. +// * Neither the name of AlliedModders LLC nor the names of its contributors +// may be used to endorse or promote products derived from this software +// without specific prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" +// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE +// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR +// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF +// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS +// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN +// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) +// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +// POSSIBILITY OF SUCH DAMAGE. + +#ifndef _include_amtl_runner_h_ +#define _include_amtl_runner_h_ + +#include +#include + +namespace ke { + +class Test +{ + public: + Test(const char *name) + : name_(name), + next_(head) + { + head = this; + } + + virtual bool Run() = 0; + + const char *name() const { + return name_; + } + Test *next() const { + return next_; + } + + static inline Test *first() { + return head; + } + + private: + static Test *head; + + private: + const char *name_; + Test *next_; +}; + +static inline bool +check(bool condition, const char *fmt, ...) +{ + FILE *fp = condition ? stdout : stderr; + if (condition) + fprintf(fp, " -- Ok: "); + else + fprintf(fp, " -- Failure: "); + va_list ap; + va_start(ap, fmt); + vfprintf(fp, fmt, ap); + va_end(ap); + fprintf(fp, "\n"); + return condition; +} + +class FallibleMalloc +{ + public: + FallibleMalloc() + : shouldOutOfMemory_(false), + ooms_(0), + overflows_(0) + { + } + + void *malloc(size_t amount) { + if (shouldOutOfMemory_) { + reportOutOfMemory(); + return NULL; + } + return ::malloc(amount); + } + void free(void *p) { + return ::free(p); + } + void reportOutOfMemory() { + ooms_++; + } + void reportAllocationOverflow() { + overflows_++; + } + + void setOutOfMemory(bool oom) { + shouldOutOfMemory_ = oom; + } + size_t ooms() const { + return ooms_; + } + size_t overflows() const { + return overflows_; + } + + private: + bool shouldOutOfMemory_; + size_t ooms_; + size_t overflows_; +}; + +} + +#endif // _include_amtl_runner_h_ + diff -r 0ac292cc1467 -r cfe800908837 tests/test-inlinelist.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/tests/test-inlinelist.cpp Mon Aug 26 23:18:03 2013 -0700 @@ -0,0 +1,132 @@ +// vim: set sts=8 ts=2 sw=2 tw=99 et: +// +// Copyright (C) 2013, David Anderson and AlliedModders LLC +// All rights reserved. +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are met: +// +// * Redistributions of source code must retain the above copyright notice, this +// list of conditions and the following disclaimer. +// * Redistributions in binary form must reproduce the above copyright notice, +// this list of conditions and the following disclaimer in the documentation +// and/or other materials provided with the distribution. +// * Neither the name of AlliedModders LLC nor the names of its contributors +// may be used to endorse or promote products derived from this software +// without specific prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" +// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE +// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR +// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF +// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS +// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN +// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) +// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +// POSSIBILITY OF SUCH DAMAGE. + +#include +#include +#include "runner.h" + +using namespace ke; + +class IntThing : public InlineListNode +{ + public: + IntThing(int value) + : value_(value) + { + } + ~IntThing() + { + } + int value() const { + return value_; + } + + private: + int value_; +}; + +class TestInlineList : public Test +{ + public: + TestInlineList() + : Test("InlineList") + { + } + + bool testBasic() + { + IntThing thing1(1); + IntThing thing2(2); + IntThing thing3(3); + IntThing thing4(4); + IntThing thing5(5); + + // Do these test twice. InlineList does not take ownership of pointers, + // so we should be guaranteed we can keep moving them in between lists. + for (size_t i = 0; i <= 1; i++) + { + InlineList list; + + InlineList::iterator iter = list.begin(); + if (!check(iter == list.end(), "list should be empty")) + return false; + + list.append(&thing1); + list.append(&thing2); + list.append(&thing3); + list.append(&thing4); + list.append(&thing5); + + iter = list.begin(); + for (int n = 1; n <= 5; n++) { + if (!check(iter->value() == n, "value %d should be %d", iter->value(), n)) + return false; + iter++; + } + if (!check(iter == list.end(), "iterator should have ended")) + return false; + + list.remove(&thing1); + iter = list.begin(); + if (!check(iter->value() == 2, "list should start at 2 after removal")) + return false; + + list.remove(&thing5); + iter = list.begin(); + iter++; + iter++; + if (!check(iter->value() == 4, "list should end at 4 after removal")) + return false; + iter++; + if (!check(iter == list.end(), "iterator should have ended")) + return false; + + list.remove(&thing3); + iter = list.begin(); + if (!check(iter->value() == 2, "first value should be 2 after removal")) + return false; + iter++; + if (!check(iter->value() == 4, "second value should be 4 after removal")) + return false; + iter++; + if (!check(iter == list.end(), "iterator should have ended")) + return false; + } + + return true; + } + + bool Run() KE_OVERRIDE + { + if (!testBasic()) + return false; + return true; + } +} sTestInlineList; + diff -r 0ac292cc1467 -r cfe800908837 tests/test-linkedlist.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/tests/test-linkedlist.cpp Mon Aug 26 23:18:03 2013 -0700 @@ -0,0 +1,281 @@ +// vim: set sts=8 ts=2 sw=2 tw=99 et: +// +// Copyright (C) 2013, David Anderson and AlliedModders LLC +// All rights reserved. +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are met: +// +// * Redistributions of source code must retain the above copyright notice, this +// list of conditions and the following disclaimer. +// * Redistributions in binary form must reproduce the above copyright notice, +// this list of conditions and the following disclaimer in the documentation +// and/or other materials provided with the distribution. +// * Neither the name of AlliedModders LLC nor the names of its contributors +// may be used to endorse or promote products derived from this software +// without specific prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" +// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE +// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR +// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF +// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS +// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN +// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) +// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +// POSSIBILITY OF SUCH DAMAGE. + +#include +#include +#include "runner.h" + +using namespace ke; + +static size_t sCtors = 0; +static size_t sCopyCtors = 0; +static size_t sMovingCtors = 0; +static size_t sMovedDtors = 0; +static size_t sDtors = 0; + +namespace { + +class BasicThing +{ + public: + BasicThing() + { + sCtors++; + } + BasicThing(const BasicThing &other) + { + sCopyCtors++; + } + ~BasicThing() + { + sDtors++; + } +}; + +class MovingThing +{ + public: + MovingThing() + : moved_(false) + { + sCtors++; + } + MovingThing(Moveable other) + { + assert(!other->moved_); + sMovingCtors++; + moved_ = false; + other->moved_ = true; + } + ~MovingThing() + { + sDtors++; + if (moved_) + sMovedDtors++; + } + + private: + MovingThing(const MovingThing &other) KE_DELETE; + bool moved_; +}; + +} // anonymous namespace + +class TestLinkedList : public Test +{ + public: + TestLinkedList() + : Test("LinkedList") + { + } + + bool testInts() + { + LinkedList list; + + if (!check(list.empty(), "list should be empty")) + return false; + if (!check(list.begin() == list.end(), "list iterator should be empty")) + return false; + + list.append(5); + list.append(7); + list.append(9); + if (!check(!list.empty(), "list should not be empty")) + return false; + if (!check(list.length() == 3, "list size should be 3")) + return false; + + LinkedList::iterator iter = list.begin(); + if (!check(iter != list.end(), "list iterator should not have ended")) + return false; + if (!check(*iter == 5, "item 1 should be 5")) + return false; + iter++; + if (!check(iter != list.end(), "list iterator should not have ended")) + return false; + if (!check(*iter == 7, "item 2 should be 7")) + return false; + iter++; + if (!check(iter != list.end(), "list iterator should not have ended")) + return false; + if (!check(*iter == 9, "item 3 should be 9")) + return false; + iter++; + if (!check(iter == list.end(), "list iterator should have ended")) + return false; + + iter = list.begin(); + iter++; + iter++; + iter = list.erase(iter); + if (!check(iter == list.end(), "erasure should have returned ended iterator")) + return false; + list.append(11); + + iter = list.begin(); + iter = list.erase(iter); + if (!check(iter != list.end(), "erasure should not have ended iterator")) + return false; + if (!check(*iter == 7, "erasure should have returned item 2")) + return false; + iter++; + if (!check(*iter == 11, "second item should be 11")) + return false; + if (!check(list.length() == 2, "list should have two items")) + return false; + + list.prepend(1); + iter = list.begin(); + if (!check(*iter == 1, "new item should have been prepended")) + return false; + iter++; + if (!check(*iter == 7, "second item should be 7")) + return false; + if (!check(list.length() == 3, "list should have three items")) + return false; + + list.clear(); + if (!check(list.empty(), "cleared list should be empty")) + return false; + if (!check(list.begin() == list.end(), "cleared list iterator should be empty")) + return false; + + list.append(1); + list.append(2); + list.append(3); + list.append(4); + list.append(5); + list.append(5); + iter = list.find(3); + if (!check(iter != list.end(), "should have found a 3")) + return false; + if (!check(*iter == 3, "should have found 3")) + return false; + list.remove(3); + iter = list.find(3); + if (!check(iter == list.end(), "should not have found a 3")) + return false; + iter = list.find(5); + if (!check(iter != list.end(), "should have found a 5")) + return false; + if (!check(*iter == 5, "should have found 5")) + return false; + list.remove(5); + iter = list.find(5); + if (!check(iter != list.end(), "should have found a second 5")) + return false; + if (!check(*iter == 5, "should have found a second 5")) + return false; + list.remove(5); + list.remove(5); + iter = list.find(5); + if (!check(iter == list.end(), "should not have found a third 5")) + return false; + + return true; + } + + bool testDestructors() + { + { + LinkedList list; + list.append(BasicThing()); + list.append(BasicThing()); + list.append(BasicThing()); + } + if (!check(sCtors == 3, "should get 3 normal constructors")) + return false; + if (!check(sCopyCtors == 3, "should get 3 copy constructors")) + return false; + if (!check(sDtors == 6, "should get 6 destructors")) + return false; + + sCtors = 0; + sCopyCtors = 0; + sDtors = 0; + return true; + } + + bool testMoving() + { + { + LinkedList list; + MovingThing a, b, c; + list.append(Moveable(a)); + list.append(Moveable(b)); + list.append(Moveable(c)); + } + + if (!check(sCtors == 3, "should get 3 normal constructors")) + return false; + if (!check(sMovingCtors == 3, "sould get 3 move constructors")) + return false; + if (!check(sMovedDtors == 3, "sould get 3 moved destructors")) + return false; + if (!check(sDtors == 6, "should get 6 destructors")) + return false; + + sCtors = 0; + sMovingCtors = 0; + sMovedDtors = 0; + sDtors = 0; + return true; + } + + bool testFallibleMalloc() + { + LinkedList list; + list.append(5); + list.append(6); + list.setOutOfMemory(true); + if (!check(!list.append(7), "list handled out-of-memory")) + return false; + list.setOutOfMemory(false); + if (!check(list.append(8), "list recovered out-of-memory")) + return false; + if (!check(list.ooms() == 1, "list received 1 oom")) + return false; + return true; + } + + bool Run() KE_OVERRIDE + { + if (!testInts()) + return false; + if (!testDestructors()) + return false; + if (!testMoving()) + return false; + if (!testFallibleMalloc()) + return false; + return true; + } +} sTestLinkedList; + diff -r 0ac292cc1467 -r cfe800908837 tests/test-refcounting.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/tests/test-refcounting.cpp Mon Aug 26 23:18:03 2013 -0700 @@ -0,0 +1,84 @@ +// vim: set sts=8 ts=2 sw=2 tw=99 et: +// +// Copyright (C) 2013, David Anderson and AlliedModders LLC +// All rights reserved. +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are met: +// +// * Redistributions of source code must retain the above copyright notice, this +// list of conditions and the following disclaimer. +// * Redistributions in binary form must reproduce the above copyright notice, +// this list of conditions and the following disclaimer in the documentation +// and/or other materials provided with the distribution. +// * Neither the name of AlliedModders LLC nor the names of its contributors +// may be used to endorse or promote products derived from this software +// without specific prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" +// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE +// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR +// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF +// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS +// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN +// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) +// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +// POSSIBILITY OF SUCH DAMAGE. + +#include +#include +#include "runner.h" + +using namespace ke; + +static size_t sDtors = 0; + +class Counted : public Refcounted +{ + public: + ~Counted() { + sDtors++; + } +}; + +static inline PassRef +PassThrough(const Ref &obj) +{ + return obj; +} + +class TestRefcounting : public Test +{ + public: + TestRefcounting() + : Test("Refcounting") + { + } + + bool Run() KE_OVERRIDE + { + { + Ref obj(Newborn(new Counted())); + } + if (!check(sDtors == 1, "Ref/Newborn counted properly")) + return false; + + { + Ref obj(Newborn(new Counted())); + Ref obj2 = PassThrough(obj); + Ref obj3 = PassThrough(obj); + if (!check(sDtors == 1, "destructor not called early")) + return false; + Ref obj4 = PassThrough(PassThrough(PassThrough(obj))); + if (!check(sDtors == 1, "destructor not called early")) + return false; + } + if (!check(sDtors == 2, "PassRef/Ref counted properly")) + return false; + + return true; + } +} sTestRefcounting; + diff -r 0ac292cc1467 -r cfe800908837 tests/test-threadutils.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/tests/test-threadutils.cpp Mon Aug 26 23:18:03 2013 -0700 @@ -0,0 +1,147 @@ +// vim: set sts=8 ts=2 sw=2 tw=99 et: +// +// Copyright (C) 2013, David Anderson and AlliedModders LLC +// All rights reserved. +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are met: +// +// * Redistributions of source code must retain the above copyright notice, this +// list of conditions and the following disclaimer. +// * Redistributions in binary form must reproduce the above copyright notice, +// this list of conditions and the following disclaimer in the documentation +// and/or other materials provided with the distribution. +// * Neither the name of AlliedModders LLC nor the names of its contributors +// may be used to endorse or promote products derived from this software +// without specific prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" +// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE +// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR +// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF +// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS +// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN +// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) +// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +// POSSIBILITY OF SUCH DAMAGE. + +#include +#include +#include +#include "runner.h" + +using namespace ke; + +// Simple worker that adds up numbers. +class TestWorkerModel : public IRunnable +{ + public: + TestWorkerModel() + : terminate_(false), + result_(0) + { + } + + void Run() { + { + AutoLock lock(main()); + main()->Notify(); + } + + AutoLock lock(wakeup()); + while (true) { + while (items_.begin() != items_.end()) { + int value = *(items_.begin()); + items_.erase(items_.begin()); + + AutoUnlock unlock(wakeup()); + result_ += value; + } + + // Only terminate if we're guaranteed the queue has no items - after + // we've just finished processing all items in the queue, while the lock + // is held. + if (terminate_) + return; + + wakeup()->Wait(); + } + } + + ConditionVariable *main() { + return &main_; + } + ConditionVariable *wakeup() { + return &wakeup_; + } + + void terminate() { + AutoLock lock(&wakeup_); + terminate_ = true; + wakeup_.Notify(); + } + + int result() { + return result_; + } + + void send(int value) { + AutoLock lock(&wakeup_); + items_.append(value); + wakeup_.Notify(); + } + + private: + ConditionVariable main_; + ConditionVariable wakeup_; + LinkedList items_; + bool terminate_; + int result_; +}; + +class TestThreading : public Test +{ + public: + TestThreading() + : Test("ThreadUtils") + { + } + + bool testWorkerModel() + { + TestWorkerModel test; + ke::AutoPtr thread(new Thread(&test, "TestWorkerModel")); + if (!check(thread->Succeeded(), "thread launched")) + return false; + + { + AutoLock lock(test.main()); + if (!check(test.main()->Wait(5000) == Wait_Signaled, "thread started within five seconds")) + return false; + } + + int total = 0; + for (int i = 0; i < 100000; i++) { + test.send(i); + total += i; + } + + test.terminate(); + thread->Join(); + + if (!check(test.result() == total, "thread returned correct result")) + return false; + + return true; + } + + bool Run() KE_OVERRIDE + { + if (!testWorkerModel()) + return false; + return true; + } +} sTestThreading; + diff -r 0ac292cc1467 -r cfe800908837 tests/test-vector.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/tests/test-vector.cpp Mon Aug 26 23:18:03 2013 -0700 @@ -0,0 +1,233 @@ +// vim: set sts=8 ts=2 sw=2 tw=99 et: +// +// Copyright (C) 2013, David Anderson and AlliedModders LLC +// All rights reserved. +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are met: +// +// * Redistributions of source code must retain the above copyright notice, this +// list of conditions and the following disclaimer. +// * Redistributions in binary form must reproduce the above copyright notice, +// this list of conditions and the following disclaimer in the documentation +// and/or other materials provided with the distribution. +// * Neither the name of AlliedModders LLC nor the names of its contributors +// may be used to endorse or promote products derived from this software +// without specific prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" +// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE +// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR +// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF +// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS +// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN +// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) +// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +// POSSIBILITY OF SUCH DAMAGE. + +#include +#include +#include "runner.h" + +using namespace ke; + +static size_t sCtors = 0; +static size_t sCopyCtors = 0; +static size_t sMovingCtors = 0; +static size_t sMovedDtors = 0; +static size_t sDtors = 0; + +namespace { + +class BasicThing +{ + public: + BasicThing() + { + sCtors++; + } + BasicThing(const BasicThing &other) + { + sCopyCtors++; + } + ~BasicThing() + { + sDtors++; + } +}; + +class MovingThing +{ + public: + MovingThing() + : moved_(false) + { + sCtors++; + } + MovingThing(Moveable other) + { + assert(!other->moved_); + sMovingCtors++; + moved_ = false; + other->moved_ = true; + } + ~MovingThing() + { + sDtors++; + if (moved_) + sMovedDtors++; + } + + private: + MovingThing(const MovingThing &other) KE_DELETE; + bool moved_; +}; + +} // anonymous namespace + +class TestVector : public Test +{ + public: + TestVector() + : Test("Vector") + { + } + + bool testInts() + { + Vector vector; + + if (!check(vector.empty(), "vector should be empty")) + return false; + if (!check(vector.length() == 0, "vector length should be 0")) + return false; + + vector.append(1); + vector.append(2); + vector.append(3); + vector.append(4); + vector.append(5); + for (int i = 0; i < 5; i++) { + if (!check(vector[i] == i + 1, "vector[%d] is %d", i, i)) + return false; + } + if (!check(vector.length() == 5, "vector length is 5")) + return false; + if (!check(!vector.empty(), "vector is not empty")) + return false; + + if (!check(vector.popCopy() == 5, "popped 5")) + return false; + if (!check(vector.popCopy() == 4, "popped 4")) + return false; + if (!check(vector.length() == 3, "vector length is 3")) + return false; + + while (!vector.empty()) + vector.pop(); + + if (!check(vector.empty(), "vector should be empty")) + return false; + if (!check(vector.ensure(128), "vector size ensured")) + return false; + if (!check(vector.empty(), "vector should be empty")) + return false; + + for (int i = 0; i < 128; i++) + vector.append(i); + if (!check(vector.length() == 128, "vector length should be 128")) + return false; + + vector.clear(); + if (!check(vector.empty(), "vector should be empty")) + return false; + + return true; + } + + bool testDestructors() + { + { + Vector vector; + vector.append(BasicThing()); + vector.append(BasicThing()); + vector.append(BasicThing()); + } + if (!check(sCtors == 3, "should get 3 normal constructors")) + return false; + if (!check(sCopyCtors == 3, "should get 3 copy constructors")) + return false; + if (!check(sDtors == 6, "should get 6 destructors")) + return false; + + sCtors = 0; + sCopyCtors = 0; + sDtors = 0; + return true; + } + + bool testMoving() + { + { + Vector vector; + MovingThing a, b, c; + vector.append(Moveable(a)); + vector.append(Moveable(b)); + vector.append(Moveable(c)); + } + + if (!check(sCtors == 3, "should get 3 normal constructors")) + return false; + if (!check(sMovingCtors == 3, "sould get 3 move constructors")) + return false; + if (!check(sMovedDtors == 3, "sould get 3 moved destructors")) + return false; + if (!check(sDtors == 6, "should get 6 destructors")) + return false; + + sCtors = 0; + sMovingCtors = 0; + sMovedDtors = 0; + sDtors = 0; + + Vector v1; + v1.append(10); + Vector v2(Move(v1)); + if (!check(v2.length() == 1, "should have moved vector")) + return false; + if (!check(v1.length() == 0, "should have gutted vector")) + return false; + + return true; + } + + bool testFallibleMalloc() + { + Vector vector; + vector.setOutOfMemory(true); + if (!check(!vector.append(7), "vector handled out-of-memory")) + return false; + vector.setOutOfMemory(false); + if (!check(vector.append(8), "vector recovered out-of-memory")) + return false; + if (!check(vector.ooms() == 1, "vector received 1 oom")) + return false; + return true; + } + + bool Run() KE_OVERRIDE + { + if (!testInts()) + return false; + if (!testDestructors()) + return false; + if (!testMoving()) + return false; + if (!testFallibleMalloc()) + return false; + return true; + } +} sTestVector; +