Add tests.
Add tests.
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/AMBuildScript Mon Aug 26 23:18:03 2013 -0700
1.3 @@ -0,0 +1,159 @@
1.4 +# vim: set ts=2 sw=2 tw=99 noet ft=python:
1.5 +import os
1.6 +import sys
1.7 +from ambuild.command import SymlinkCommand
1.8 +
1.9 +class KE:
1.10 + def __init__(self):
1.11 + self.compiler = Cpp.Compiler()
1.12 +
1.13 + if AMBuild.mode == 'config':
1.14 + #Detect compilers
1.15 + self.compiler.DetectAll(AMBuild)
1.16 +
1.17 + #Set up defines
1.18 + cxx = self.compiler.cxx
1.19 + if isinstance(cxx, Cpp.CompatGCC):
1.20 + if isinstance(cxx, Cpp.GCC):
1.21 + self.vendor = 'gcc'
1.22 + elif isinstance(cxx, Cpp.Clang):
1.23 + self.vendor = 'clang'
1.24 + self.compiler.AddToListVar('CFLAGS', '-pipe')
1.25 + self.compiler.AddToListVar('CFLAGS', '-Wall')
1.26 + self.compiler.AddToListVar('CFLAGS', '-Werror')
1.27 +
1.28 + if AMBuild.options.arch == 'x86':
1.29 + self.compiler.AddToListVar('CFLAGS', '-m32')
1.30 + self.compiler.AddToListVar('POSTLINKFLAGS', '-m32')
1.31 + elif AMBuild.options.arch == 'x64':
1.32 + self.compiler.AddToListVar('CFLAGS', '-m64')
1.33 + self.compiler.AddToListVar('POSTLINKFLAGS', '-m64')
1.34 + elif AMBuild.options.arch:
1.35 + raise Exception('Unknown architecture: {0}'.format(AMBuild.options.arch))
1.36 +
1.37 + # Disable some stuff we don't use, that gives us better binary
1.38 + # compatibility on Linux.
1.39 + self.compiler.AddToListVar('CXXFLAGS', '-fno-exceptions')
1.40 + self.compiler.AddToListVar('CXXFLAGS', '-fno-rtti')
1.41 + self.compiler.AddToListVar('CXXFLAGS', '-fno-threadsafe-statics')
1.42 +
1.43 + # We don't really care about these.
1.44 + self.compiler.AddToListVar('CXXFLAGS', '-Wno-non-virtual-dtor')
1.45 + self.compiler.AddToListVar('CXXFLAGS', '-Wno-overloaded-virtual')
1.46 +
1.47 + # The compiler is too aggressive about what what should be a valid offsetof.
1.48 + self.compiler.AddToListVar('CXXFLAGS', '-Wno-invalid-offsetof')
1.49 +
1.50 + # This would otherwise forbid ((x = y) == NULL) in clang apparently.
1.51 + if self.vendor == 'clang' and cxx.majorVersion > 1:
1.52 + self.compiler.AddToListVar('CXXFLAGS', '-Wno-null-arithmetic')
1.53 + if self.vendor == 'clang' and \
1.54 + cxx.majorVersion >= 3 and \
1.55 + cxx.minorVersion >= 3:
1.56 + self.compiler.AddToListVar('CXXFLAGS', '-Wno-unused-private-field')
1.57 +
1.58 + if (self.vendor == 'gcc' and cxx.majorVersion >= 4 and cxx.minorVersion >= 7) or \
1.59 + (self.vendor == 'clang' and cxx.majorVersion >= 3):
1.60 + self.compiler.AddToListVar('CXXFLAGS', '-Wno-delete-non-virtual-dtor')
1.61 +
1.62 + self.compiler.AddToListVar('POSTLINKFLAGS', '-lpthread')
1.63 + self.compiler.AddToListVar('POSTLINKFLAGS', '-lrt')
1.64 + elif isinstance(cxx, Cpp.MSVC):
1.65 + self.vendor = 'msvc'
1.66 + if AMBuild.options.debug == '1':
1.67 + self.compiler.AddToListVar('CFLAGS', '/MTd')
1.68 + self.compiler.AddToListVar('POSTLINKFLAGS', '/NODEFAULTLIB:libcmt')
1.69 + else:
1.70 + self.compiler.AddToListVar('CFLAGS', '/MT')
1.71 + self.compiler.AddToListVar('CDEFINES', 'WIN32')
1.72 + self.compiler.AddToListVar('CDEFINES', '_CRT_SECURE_NO_DEPRECATE')
1.73 + self.compiler.AddToListVar('CDEFINES', '_CRT_SECURE_NO_WARNINGS')
1.74 + self.compiler.AddToListVar('CDEFINES', '_CRT_NONSTDC_NO_DEPRECATE')
1.75 + self.compiler.AddToListVar('CXXFLAGS', '/EHsc')
1.76 + self.compiler.AddToListVar('CXXFLAGS', '/GR-')
1.77 + self.compiler.AddToListVar('CFLAGS', '/W3')
1.78 + self.compiler.AddToListVar('CFLAGS', '/nologo')
1.79 + self.compiler.AddToListVar('CFLAGS', '/Zi')
1.80 + self.compiler.AddToListVar('CXXFLAGS', '/TP')
1.81 + self.compiler.AddToListVar('POSTLINKFLAGS', '/DEBUG')
1.82 +
1.83 + self.compiler.AddToListVar('POSTLINKFLAGS', 'kernel32.lib')
1.84 + self.compiler.AddToListVar('POSTLINKFLAGS', 'user32.lib')
1.85 + self.compiler.AddToListVar('POSTLINKFLAGS', 'gdi32.lib')
1.86 + self.compiler.AddToListVar('POSTLINKFLAGS', 'winspool.lib')
1.87 + self.compiler.AddToListVar('POSTLINKFLAGS', 'comdlg32.lib')
1.88 + self.compiler.AddToListVar('POSTLINKFLAGS', 'advapi32.lib')
1.89 + self.compiler.AddToListVar('POSTLINKFLAGS', 'shell32.lib')
1.90 + self.compiler.AddToListVar('POSTLINKFLAGS', 'ole32.lib')
1.91 + self.compiler.AddToListVar('POSTLINKFLAGS', 'oleaut32.lib')
1.92 + self.compiler.AddToListVar('POSTLINKFLAGS', 'uuid.lib')
1.93 + self.compiler.AddToListVar('POSTLINKFLAGS', 'odbc32.lib')
1.94 + self.compiler.AddToListVar('POSTLINKFLAGS', 'odbccp32.lib')
1.95 +
1.96 + #Optimization
1.97 + if AMBuild.options.opt == '1':
1.98 + self.compiler.AddToListVar('CDEFINES', 'NDEBUG')
1.99 + if self.vendor == 'gcc' or self.vendor == 'clang':
1.100 + self.compiler.AddToListVar('CFLAGS', '-O3')
1.101 + elif self.vendor == 'msvc':
1.102 + self.compiler.AddToListVar('CFLAGS', '/Ox')
1.103 + self.compiler.AddToListVar('POSTLINKFLAGS', '/OPT:ICF')
1.104 + self.compiler.AddToListVar('POSTLINKFLAGS', '/OPT:REF')
1.105 +
1.106 + #Debugging
1.107 + if AMBuild.options.debug == '1':
1.108 + if self.vendor == 'gcc' or self.vendor == 'clang':
1.109 + self.compiler.AddToListVar('CFLAGS', '-g3')
1.110 + self.compiler.AddToListVar('CFLAGS', '-O0')
1.111 + elif self.vendor == 'msvc':
1.112 + self.compiler.AddToListVar('CFLAGS', '/Od')
1.113 + self.compiler.AddToListVar('CFLAGS', '/RTC1')
1.114 +
1.115 + #Platform-specifics
1.116 + if AMBuild.target['platform'] == 'linux':
1.117 + if self.vendor == 'gcc':
1.118 + self.compiler.AddToListVar('POSTLINKFLAGS', '-static-libgcc')
1.119 + if self.vendor == 'clang':
1.120 + self.compiler.AddToListVar('POSTLINKFLAGS', '-lgcc_eh')
1.121 + elif AMBuild.target['platform'] == 'darwin':
1.122 + self.compiler.AddToListVar('POSTLINKFLAGS', '-mmacosx-version-min=10.5')
1.123 + if AMBuild.options.arch == 'x86':
1.124 + self.compiler.AddToListVar('POSTLINKFLAGS', ['-arch', 'i386'])
1.125 + elif AMBuild.options.arch == 'x64':
1.126 + self.compiler.AddToListVar('POSTLINKFLAGS', ['-arch', 'x86_64'])
1.127 + self.compiler.AddToListVar('POSTLINKFLAGS', '-lstdc++')
1.128 +
1.129 + #Finish up
1.130 + self.compiler.ToConfig(AMBuild, 'compiler')
1.131 + AMBuild.cache.CacheVariable('vendor', self.vendor)
1.132 + self.targetMap = { }
1.133 + AMBuild.cache.CacheVariable('targetMap', self.targetMap)
1.134 + else:
1.135 + self.compiler.FromConfig(AMBuild, 'compiler')
1.136 + self.targetMap = AMBuild.cache['targetMap']
1.137 +
1.138 + def DefaultCompiler(self):
1.139 + return self.compiler.Clone()
1.140 +
1.141 + def JobMatters(self, jobname):
1.142 + file = sys._getframe().f_code.co_filename
1.143 + if AMBuild.mode == 'config':
1.144 + self.targetMap[jobname] = file
1.145 + return True
1.146 + if len(AMBuild.args) == 0:
1.147 + return True
1.148 + if not jobname in AMBuild.args:
1.149 + return False
1.150 +
1.151 +ke = KE()
1.152 +globals = {
1.153 + 'KE': ke
1.154 +}
1.155 +
1.156 +FileList = [
1.157 + ['tests', 'AMBuild.tests'],
1.158 + ]
1.159 +
1.160 +for parts in FileList:
1.161 + AMBuild.Include(os.path.join(*parts), globals)
1.162 +
2.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
2.2 +++ b/configure.py Mon Aug 26 23:18:03 2013 -0700
2.3 @@ -0,0 +1,28 @@
2.4 +# vim: set ts=2 sw=2 tw=99 noet ft=python:
2.5 +#
2.6 +# Copyright (C) 2004-2012 David Anderson
2.7 +#
2.8 +# This file is part of SourcePawn.
2.9 +#
2.10 +# SourcePawn is free software: you can redistribute it and/or modify it under
2.11 +# the terms of the GNU General Public License as published by the Free
2.12 +# Software Foundation, either version 3 of the License, or (at your option)
2.13 +# any later version.
2.14 +#
2.15 +# SourcePawn is distributed in the hope that it will be useful, but WITHOUT ANY
2.16 +# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
2.17 +# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
2.18 +#
2.19 +# You should have received a copy of the GNU General Public License along with
2.20 +# SourcePawn. If not, see http://www.gnu.org/licenses/.
2.21 +#
2.22 +import sys
2.23 +import ambuild.runner as runner
2.24 +
2.25 +run = runner.Runner()
2.26 +run.options.add_option('--enable-debug', action='store_const', const='1', dest='debug',
2.27 + help='Enable debugging symbols')
2.28 +run.options.add_option('--enable-optimize', action='store_const', const='1', dest='opt',
2.29 + help='Enable optimization')
2.30 +run.options.add_option('--arch', dest='arch', help='Architecture (x86, x64)')
2.31 +run.Configure(sys.path[0])
3.1 --- a/include/am-inlinelist.h Mon Aug 26 20:41:58 2013 -0700
3.2 +++ b/include/am-inlinelist.h Mon Aug 26 23:18:03 2013 -0700
3.3 @@ -31,11 +31,14 @@
3.4 #define _include_amtl_inline_list_h_
3.5
3.6 #include <stddef.h>
3.7 +#include <assert.h>
3.8
3.9 namespace ke {
3.10
3.11 template <typename T> class InlineList;
3.12
3.13 +// Objects can recursively inherit from InlineListNode in order to have
3.14 +// membership in an InlineList<T>.
3.15 template <typename T>
3.16 class InlineListNode
3.17 {
3.18 @@ -43,14 +46,14 @@
3.19
3.20 public:
3.21 InlineListNode()
3.22 - : next_(NULL),
3.23 - prev_(NULL)
3.24 + : next_(NULL),
3.25 + prev_(NULL)
3.26 {
3.27 }
3.28
3.29 InlineListNode(InlineListNode *next, InlineListNode *prev)
3.30 - : next_(next),
3.31 - prev_(prev)
3.32 + : next_(next),
3.33 + prev_(prev)
3.34 {
3.35 }
3.36
3.37 @@ -59,6 +62,14 @@
3.38 InlineListNode *prev_;
3.39 };
3.40
3.41 +// An InlineList is a linked list that threads link pointers through objects,
3.42 +// rather than allocating node memory. A node can be in at most one list at
3.43 +// any time.
3.44 +//
3.45 +// Since InlineLists are designed to be very cheap, there is no requirement
3.46 +// that elements be removed from a list once the list is destructed. However,
3.47 +// for as long as the list is alive, all of its contained nodes must also
3.48 +// be alive.
3.49 template <typename T>
3.50 class InlineList
3.51 {
3.52 @@ -66,10 +77,24 @@
3.53
3.54 Node head_;
3.55
3.56 + // Work around a clang bug where we can't initialize with &head_ in the ctor.
3.57 + inline Node *head() {
3.58 + return &head_;
3.59 + }
3.60 +
3.61 public:
3.62 InlineList()
3.63 - : head_(&head_, &head_)
3.64 + : head_(head(), head())
3.65 + {
3.66 + }
3.67 +
3.68 + ~InlineList()
3.69 {
3.70 +#if !defined(NDEBUG)
3.71 + // Remove all items to clear their next/prev fields.
3.72 + while (begin() != end())
3.73 + remove(*begin());
3.74 +#endif
3.75 }
3.76
3.77 public:
3.78 @@ -86,7 +111,7 @@
3.79
3.80 iterator & operator ++() {
3.81 iter_ = iter_->next;
3.82 - return *iter_;
3.83 + return *this;
3.84 }
3.85 iterator operator ++(int) {
3.86 iterator old(*this);
3.87 @@ -105,14 +130,6 @@
3.88 bool operator ==(const iterator &where) const {
3.89 return iter_ == where.iter_;
3.90 }
3.91 - iterator prev() const {
3.92 - iterator p(iter_->prev_);
3.93 - return p;
3.94 - }
3.95 - iterator next() const {
3.96 - iterator p(iter_->next_);
3.97 - return p;
3.98 - }
3.99 };
3.100
3.101 iterator begin() {
3.102 @@ -123,12 +140,20 @@
3.103 return iterator(&head_);
3.104 }
3.105
3.106 - void erase(Node *t) {
3.107 + void remove(Node *t) {
3.108 t->prev_->next_ = t->next_;
3.109 t->next_->prev_ = t->prev_;
3.110 +
3.111 +#if !defined(NDEBUG)
3.112 + t->next_ = NULL;
3.113 + t->prev_ = NULL;
3.114 +#endif
3.115 }
3.116
3.117 - void insert(Node *t) {
3.118 + void append(Node *t) {
3.119 + assert(!t->next_);
3.120 + assert(!t->prev_);
3.121 +
3.122 t->prev_ = head_.prev_;
3.123 t->next_ = &head_;
3.124 head_.prev_->next_ = t;
4.1 --- a/include/am-linkedlist.h Mon Aug 26 20:41:58 2013 -0700
4.2 +++ b/include/am-linkedlist.h Mon Aug 26 23:18:03 2013 -0700
4.3 @@ -34,6 +34,7 @@
4.4 #include <stdlib.h>
4.5 #include <am-allocator-policies.h>
4.6 #include <am-utility.h>
4.7 +#include <am-moveable.h>
4.8
4.9 namespace ke {
4.10
4.11 @@ -58,6 +59,10 @@
4.12 : obj(o)
4.13 {
4.14 }
4.15 + Node(Moveable<T> o)
4.16 + : obj(o)
4.17 + {
4.18 + }
4.19
4.20 T obj;
4.21 Node *next;
4.22 @@ -68,29 +73,28 @@
4.23 LinkedList(AllocPolicy = AllocPolicy())
4.24 : length_(0)
4.25 {
4.26 + head()->prev = head();
4.27 + head()->next = head();
4.28 }
4.29 ~LinkedList() {
4.30 clear();
4.31 }
4.32
4.33 bool append(const T &obj) {
4.34 - Node *node = allocNode(obj);
4.35 - if (!node)
4.36 - return false;
4.37 -
4.38 - node->prev = head()->prev;
4.39 - node->next = head();
4.40 - head()->prev->next = node;
4.41 - head()->prev = node;
4.42 -
4.43 - length_++;
4.44 + return insertBefore(end(), obj) != end();
4.45 + }
4.46 + bool append(Moveable<T> obj) {
4.47 + return insertBefore(end(), obj) != end();
4.48 }
4.49
4.50 bool prepend(const T &obj) {
4.51 - return insert(begin(), obj);
4.52 + return insertBefore(begin(), obj) != begin();
4.53 + }
4.54 + bool prepend(Moveable<T> obj) {
4.55 + return insertBefore(begin(), obj) != begin();
4.56 }
4.57
4.58 - size_t size() const {
4.59 + size_t length() const {
4.60 return length_;
4.61 }
4.62
4.63 @@ -137,6 +141,13 @@
4.64 new (node) Node(obj);
4.65 return node;
4.66 }
4.67 + Node *allocNode(Moveable<T> obj) {
4.68 + Node *node = (Node *)this->malloc(sizeof(Node));
4.69 + if (!node)
4.70 + return NULL;
4.71 + new (node) Node(obj);
4.72 + return node;
4.73 + }
4.74
4.75 void freeNode(Node *node) {
4.76 node->obj.~T();
4.77 @@ -221,29 +232,9 @@
4.78 Node *this_;
4.79 };
4.80
4.81 - public:
4.82 - iterator begin() const {
4.83 - return iterator(head()->next);
4.84 - }
4.85 - iterator end() const {
4.86 - return iterator(head());
4.87 - }
4.88 - iterator erase(iterator &where) {
4.89 - Node *pNode = where.this_;
4.90 - iterator iter(where);
4.91 - iter++;
4.92 -
4.93 - pNode->prev->next = pNode->next;
4.94 - pNode->next->prev = pNode->prev;
4.95 -
4.96 - freeNode(pNode);
4.97 - length_--;
4.98 -
4.99 - return iter;
4.100 - }
4.101 - iterator insert(iterator where, const T &obj) {
4.102 - // Insert obj right before where
4.103 - Node *node = allocNode(obj);
4.104 + private:
4.105 + // Insert obj right before where.
4.106 + iterator insert(iterator where, Node *node) {
4.107 if (!node)
4.108 return where;
4.109
4.110 @@ -259,6 +250,34 @@
4.111 }
4.112
4.113 public:
4.114 + iterator begin() {
4.115 + return iterator(head()->next);
4.116 + }
4.117 + iterator end() {
4.118 + return iterator(head());
4.119 + }
4.120 + iterator erase(iterator where) {
4.121 + Node *pNode = where.this_;
4.122 + iterator iter(where);
4.123 + iter++;
4.124 +
4.125 + pNode->prev->next = pNode->next;
4.126 + pNode->next->prev = pNode->prev;
4.127 +
4.128 + freeNode(pNode);
4.129 + length_--;
4.130 +
4.131 + return iter;
4.132 + }
4.133 + iterator insertBefore(iterator where, const T &obj) {
4.134 + return insert(where, allocNode(obj));
4.135 + }
4.136 + iterator insertBefore(iterator where, Moveable<T> obj) {
4.137 + return insert(where, allocNode(obj));
4.138 + }
4.139 +
4.140 + public:
4.141 + // Removes one instance of |obj| from the list, if found.
4.142 void remove(const T &obj) {
4.143 for (iterator b = begin(); b != end(); b++) {
4.144 if (*b == obj) {
4.145 @@ -269,7 +288,7 @@
4.146 }
4.147
4.148 template <typename U>
4.149 - iterator find(const U &equ) const {
4.150 + iterator find(const U &equ) {
4.151 for (iterator iter = begin(); iter != end(); iter++) {
4.152 if (*iter == equ)
4.153 return iter;
5.1 --- a/include/am-moveable.h Mon Aug 26 20:41:58 2013 -0700
5.2 +++ b/include/am-moveable.h Mon Aug 26 23:18:03 2013 -0700
5.3 @@ -61,6 +61,13 @@
5.4 T &t_;
5.5 };
5.6
5.7 +template <typename T>
5.8 +static inline Moveable<T>
5.9 +Move(T &t)
5.10 +{
5.11 + return Moveable<T>(t);
5.12 +}
5.13 +
5.14 } // namespace ke
5.15
5.16 #endif // _include_amtl_moveable_h_
6.1 --- a/include/am-vector.h Mon Aug 26 20:41:58 2013 -0700
6.2 +++ b/include/am-vector.h Mon Aug 26 23:18:03 2013 -0700
6.3 @@ -43,25 +43,31 @@
6.4 {
6.5 public:
6.6 Vector(AllocPolicy = AllocPolicy())
6.7 - : data_(NULL),
6.8 - nitems_(0),
6.9 - maxsize_(0)
6.10 + : data_(NULL),
6.11 + nitems_(0),
6.12 + maxsize_(0)
6.13 {
6.14 }
6.15
6.16 + Vector(Moveable<Vector<T, AllocPolicy> > other) {
6.17 + data_ = other->data_;
6.18 + nitems_ = other->nitems_;
6.19 + maxsize_ = other->maxsize_;
6.20 + other->reset();
6.21 + }
6.22 +
6.23 ~Vector() {
6.24 zap();
6.25 }
6.26
6.27 - void steal(Vector &other) {
6.28 - zap();
6.29 - data_ = other.data_;
6.30 - nitems_ = other.nitems_;
6.31 - maxsize_ = other.maxsize_;
6.32 - other.reset();
6.33 + bool append(const T &item) {
6.34 + if (!growIfNeeded(1))
6.35 + return false;
6.36 + new (&data_[nitems_]) T(item);
6.37 + nitems_++;
6.38 + return true;
6.39 }
6.40 -
6.41 - bool append(const T &item) {
6.42 + bool append(Moveable<T> item) {
6.43 if (!growIfNeeded(1))
6.44 return false;
6.45 new (&data_[nitems_]) T(item);
6.46 @@ -73,6 +79,12 @@
6.47 new (&data_[nitems_]) T(item);
6.48 nitems_++;
6.49 }
6.50 + void infallibleAppend(Moveable<T> item) {
6.51 + assert(growIfNeeded(1));
6.52 + new (&data_[nitems_]) T(item);
6.53 + nitems_++;
6.54 + }
6.55 +
6.56 T popCopy() {
6.57 T t = at(length() - 1);
6.58 pop();
7.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
7.2 +++ b/tests/AMBuild.tests Mon Aug 26 23:18:03 2013 -0700
7.3 @@ -0,0 +1,42 @@
7.4 +# vim: set ts=2 sw=2 tw=99 noet ft=python:
7.5 +#
7.6 +# Copyright (C) 2004-2012 David Anderson
7.7 +#
7.8 +# This file is part of SourcePawn.
7.9 +#
7.10 +# SourcePawn is free software: you can redistribute it and/or modify it under
7.11 +# the terms of the GNU General Public License as published by the Free
7.12 +# Software Foundation, either version 3 of the License, or (at your option)
7.13 +# any later version.
7.14 +#
7.15 +# SourcePawn is distributed in the hope that it will be useful, but WITHOUT ANY
7.16 +# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
7.17 +# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
7.18 +#
7.19 +# You should have received a copy of the GNU General Public License along with
7.20 +# SourcePawn. If not, see http://www.gnu.org/licenses/.
7.21 +#
7.22 +import os
7.23 +import os.path
7.24 +import ambuild.osutil as osutil
7.25 +
7.26 +compiler = KE.DefaultCompiler()
7.27 +compiler.AddToListVar('CXXINCLUDES', os.path.join(AMBuild.sourceFolder, 'include'))
7.28 +
7.29 +if compiler.cc.name == 'msvc':
7.30 + compiler.AddToListVar('POSTLINKFLAGS', '/SUBSYSTEM:CONSOLE')
7.31 +
7.32 +job = AMBuild.AddJob('testrun')
7.33 +binary = Cpp.ExecutableBuilder('testrun', AMBuild, job, compiler)
7.34 +files = [
7.35 + 'main.cpp',
7.36 + 'test-inlinelist.cpp',
7.37 + 'test-linkedlist.cpp',
7.38 + 'test-vector.cpp',
7.39 + 'test-refcounting.cpp',
7.40 + 'test-threadutils.cpp',
7.41 +]
7.42 +
7.43 +binary.AddSourceFiles('tests', files)
7.44 +binary.SendToJob()
7.45 +
8.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
8.2 +++ b/tests/main.cpp Mon Aug 26 23:18:03 2013 -0700
8.3 @@ -0,0 +1,68 @@
8.4 +// vim: set sts=8 ts=2 sw=2 tw=99 et:
8.5 +//
8.6 +// Copyright (C) 2013, David Anderson and AlliedModders LLC
8.7 +// All rights reserved.
8.8 +//
8.9 +// Redistribution and use in source and binary forms, with or without
8.10 +// modification, are permitted provided that the following conditions are met:
8.11 +//
8.12 +// * Redistributions of source code must retain the above copyright notice, this
8.13 +// list of conditions and the following disclaimer.
8.14 +// * Redistributions in binary form must reproduce the above copyright notice,
8.15 +// this list of conditions and the following disclaimer in the documentation
8.16 +// and/or other materials provided with the distribution.
8.17 +// * Neither the name of AlliedModders LLC nor the names of its contributors
8.18 +// may be used to endorse or promote products derived from this software
8.19 +// without specific prior written permission.
8.20 +//
8.21 +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
8.22 +// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
8.23 +// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
8.24 +// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
8.25 +// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
8.26 +// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
8.27 +// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
8.28 +// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
8.29 +// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
8.30 +// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
8.31 +// POSSIBILITY OF SUCH DAMAGE.
8.32 +
8.33 +#include <stddef.h>
8.34 +#include <stdio.h>
8.35 +#include <stdlib.h>
8.36 +#include "runner.h"
8.37 +
8.38 +using namespace ke;
8.39 +
8.40 +Test *Test::head = NULL;
8.41 +
8.42 +int main(int argc, char **argv)
8.43 +{
8.44 + Test *test = Test::first();
8.45 + while (test) {
8.46 + fprintf(stdout, "Testing %s... \n", test->name());
8.47 + if (!test->Run()) {
8.48 + fprintf(stdout, "TEST:%s FAIL\n", test->name());
8.49 + break;
8.50 + }
8.51 + fprintf(stdout, "TEST:%s OK\n", test->name());
8.52 + test = test->next();
8.53 + }
8.54 +}
8.55 +
8.56 +#if defined(__GNUC__)
8.57 +extern "C" void __cxa_pure_virtual()
8.58 +{
8.59 + abort();
8.60 +}
8.61 +
8.62 +void *operator new(size_t amount)
8.63 +{
8.64 + return malloc(amount);
8.65 +}
8.66 +
8.67 +void operator delete(void *p)
8.68 +{
8.69 + free(p);
8.70 +}
8.71 +#endif
9.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
9.2 +++ b/tests/runner.h Mon Aug 26 23:18:03 2013 -0700
9.3 @@ -0,0 +1,131 @@
9.4 +// vim: set sts=8 ts=2 sw=2 tw=99 et:
9.5 +//
9.6 +// Copyright (C) 2013, David Anderson and AlliedModders LLC
9.7 +// All rights reserved.
9.8 +//
9.9 +// Redistribution and use in source and binary forms, with or without
9.10 +// modification, are permitted provided that the following conditions are met:
9.11 +//
9.12 +// * Redistributions of source code must retain the above copyright notice, this
9.13 +// list of conditions and the following disclaimer.
9.14 +// * Redistributions in binary form must reproduce the above copyright notice,
9.15 +// this list of conditions and the following disclaimer in the documentation
9.16 +// and/or other materials provided with the distribution.
9.17 +// * Neither the name of AlliedModders LLC nor the names of its contributors
9.18 +// may be used to endorse or promote products derived from this software
9.19 +// without specific prior written permission.
9.20 +//
9.21 +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
9.22 +// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
9.23 +// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
9.24 +// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
9.25 +// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
9.26 +// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
9.27 +// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
9.28 +// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
9.29 +// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
9.30 +// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
9.31 +// POSSIBILITY OF SUCH DAMAGE.
9.32 +
9.33 +#ifndef _include_amtl_runner_h_
9.34 +#define _include_amtl_runner_h_
9.35 +
9.36 +#include <stdio.h>
9.37 +#include <stdarg.h>
9.38 +
9.39 +namespace ke {
9.40 +
9.41 +class Test
9.42 +{
9.43 + public:
9.44 + Test(const char *name)
9.45 + : name_(name),
9.46 + next_(head)
9.47 + {
9.48 + head = this;
9.49 + }
9.50 +
9.51 + virtual bool Run() = 0;
9.52 +
9.53 + const char *name() const {
9.54 + return name_;
9.55 + }
9.56 + Test *next() const {
9.57 + return next_;
9.58 + }
9.59 +
9.60 + static inline Test *first() {
9.61 + return head;
9.62 + }
9.63 +
9.64 + private:
9.65 + static Test *head;
9.66 +
9.67 + private:
9.68 + const char *name_;
9.69 + Test *next_;
9.70 +};
9.71 +
9.72 +static inline bool
9.73 +check(bool condition, const char *fmt, ...)
9.74 +{
9.75 + FILE *fp = condition ? stdout : stderr;
9.76 + if (condition)
9.77 + fprintf(fp, " -- Ok: ");
9.78 + else
9.79 + fprintf(fp, " -- Failure: ");
9.80 + va_list ap;
9.81 + va_start(ap, fmt);
9.82 + vfprintf(fp, fmt, ap);
9.83 + va_end(ap);
9.84 + fprintf(fp, "\n");
9.85 + return condition;
9.86 +}
9.87 +
9.88 +class FallibleMalloc
9.89 +{
9.90 + public:
9.91 + FallibleMalloc()
9.92 + : shouldOutOfMemory_(false),
9.93 + ooms_(0),
9.94 + overflows_(0)
9.95 + {
9.96 + }
9.97 +
9.98 + void *malloc(size_t amount) {
9.99 + if (shouldOutOfMemory_) {
9.100 + reportOutOfMemory();
9.101 + return NULL;
9.102 + }
9.103 + return ::malloc(amount);
9.104 + }
9.105 + void free(void *p) {
9.106 + return ::free(p);
9.107 + }
9.108 + void reportOutOfMemory() {
9.109 + ooms_++;
9.110 + }
9.111 + void reportAllocationOverflow() {
9.112 + overflows_++;
9.113 + }
9.114 +
9.115 + void setOutOfMemory(bool oom) {
9.116 + shouldOutOfMemory_ = oom;
9.117 + }
9.118 + size_t ooms() const {
9.119 + return ooms_;
9.120 + }
9.121 + size_t overflows() const {
9.122 + return overflows_;
9.123 + }
9.124 +
9.125 + private:
9.126 + bool shouldOutOfMemory_;
9.127 + size_t ooms_;
9.128 + size_t overflows_;
9.129 +};
9.130 +
9.131 +}
9.132 +
9.133 +#endif // _include_amtl_runner_h_
9.134 +
10.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
10.2 +++ b/tests/test-inlinelist.cpp Mon Aug 26 23:18:03 2013 -0700
10.3 @@ -0,0 +1,132 @@
10.4 +// vim: set sts=8 ts=2 sw=2 tw=99 et:
10.5 +//
10.6 +// Copyright (C) 2013, David Anderson and AlliedModders LLC
10.7 +// All rights reserved.
10.8 +//
10.9 +// Redistribution and use in source and binary forms, with or without
10.10 +// modification, are permitted provided that the following conditions are met:
10.11 +//
10.12 +// * Redistributions of source code must retain the above copyright notice, this
10.13 +// list of conditions and the following disclaimer.
10.14 +// * Redistributions in binary form must reproduce the above copyright notice,
10.15 +// this list of conditions and the following disclaimer in the documentation
10.16 +// and/or other materials provided with the distribution.
10.17 +// * Neither the name of AlliedModders LLC nor the names of its contributors
10.18 +// may be used to endorse or promote products derived from this software
10.19 +// without specific prior written permission.
10.20 +//
10.21 +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
10.22 +// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
10.23 +// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
10.24 +// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
10.25 +// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
10.26 +// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
10.27 +// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
10.28 +// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
10.29 +// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
10.30 +// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
10.31 +// POSSIBILITY OF SUCH DAMAGE.
10.32 +
10.33 +#include <am-inlinelist.h>
10.34 +#include <am-utility.h>
10.35 +#include "runner.h"
10.36 +
10.37 +using namespace ke;
10.38 +
10.39 +class IntThing : public InlineListNode<IntThing>
10.40 +{
10.41 + public:
10.42 + IntThing(int value)
10.43 + : value_(value)
10.44 + {
10.45 + }
10.46 + ~IntThing()
10.47 + {
10.48 + }
10.49 + int value() const {
10.50 + return value_;
10.51 + }
10.52 +
10.53 + private:
10.54 + int value_;
10.55 +};
10.56 +
10.57 +class TestInlineList : public Test
10.58 +{
10.59 + public:
10.60 + TestInlineList()
10.61 + : Test("InlineList")
10.62 + {
10.63 + }
10.64 +
10.65 + bool testBasic()
10.66 + {
10.67 + IntThing thing1(1);
10.68 + IntThing thing2(2);
10.69 + IntThing thing3(3);
10.70 + IntThing thing4(4);
10.71 + IntThing thing5(5);
10.72 +
10.73 + // Do these test twice. InlineList does not take ownership of pointers,
10.74 + // so we should be guaranteed we can keep moving them in between lists.
10.75 + for (size_t i = 0; i <= 1; i++)
10.76 + {
10.77 + InlineList<IntThing> list;
10.78 +
10.79 + InlineList<IntThing>::iterator iter = list.begin();
10.80 + if (!check(iter == list.end(), "list should be empty"))
10.81 + return false;
10.82 +
10.83 + list.append(&thing1);
10.84 + list.append(&thing2);
10.85 + list.append(&thing3);
10.86 + list.append(&thing4);
10.87 + list.append(&thing5);
10.88 +
10.89 + iter = list.begin();
10.90 + for (int n = 1; n <= 5; n++) {
10.91 + if (!check(iter->value() == n, "value %d should be %d", iter->value(), n))
10.92 + return false;
10.93 + iter++;
10.94 + }
10.95 + if (!check(iter == list.end(), "iterator should have ended"))
10.96 + return false;
10.97 +
10.98 + list.remove(&thing1);
10.99 + iter = list.begin();
10.100 + if (!check(iter->value() == 2, "list should start at 2 after removal"))
10.101 + return false;
10.102 +
10.103 + list.remove(&thing5);
10.104 + iter = list.begin();
10.105 + iter++;
10.106 + iter++;
10.107 + if (!check(iter->value() == 4, "list should end at 4 after removal"))
10.108 + return false;
10.109 + iter++;
10.110 + if (!check(iter == list.end(), "iterator should have ended"))
10.111 + return false;
10.112 +
10.113 + list.remove(&thing3);
10.114 + iter = list.begin();
10.115 + if (!check(iter->value() == 2, "first value should be 2 after removal"))
10.116 + return false;
10.117 + iter++;
10.118 + if (!check(iter->value() == 4, "second value should be 4 after removal"))
10.119 + return false;
10.120 + iter++;
10.121 + if (!check(iter == list.end(), "iterator should have ended"))
10.122 + return false;
10.123 + }
10.124 +
10.125 + return true;
10.126 + }
10.127 +
10.128 + bool Run() KE_OVERRIDE
10.129 + {
10.130 + if (!testBasic())
10.131 + return false;
10.132 + return true;
10.133 + }
10.134 +} sTestInlineList;
10.135 +
11.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
11.2 +++ b/tests/test-linkedlist.cpp Mon Aug 26 23:18:03 2013 -0700
11.3 @@ -0,0 +1,281 @@
11.4 +// vim: set sts=8 ts=2 sw=2 tw=99 et:
11.5 +//
11.6 +// Copyright (C) 2013, David Anderson and AlliedModders LLC
11.7 +// All rights reserved.
11.8 +//
11.9 +// Redistribution and use in source and binary forms, with or without
11.10 +// modification, are permitted provided that the following conditions are met:
11.11 +//
11.12 +// * Redistributions of source code must retain the above copyright notice, this
11.13 +// list of conditions and the following disclaimer.
11.14 +// * Redistributions in binary form must reproduce the above copyright notice,
11.15 +// this list of conditions and the following disclaimer in the documentation
11.16 +// and/or other materials provided with the distribution.
11.17 +// * Neither the name of AlliedModders LLC nor the names of its contributors
11.18 +// may be used to endorse or promote products derived from this software
11.19 +// without specific prior written permission.
11.20 +//
11.21 +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
11.22 +// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
11.23 +// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
11.24 +// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
11.25 +// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
11.26 +// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
11.27 +// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
11.28 +// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
11.29 +// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
11.30 +// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
11.31 +// POSSIBILITY OF SUCH DAMAGE.
11.32 +
11.33 +#include <am-linkedlist.h>
11.34 +#include <assert.h>
11.35 +#include "runner.h"
11.36 +
11.37 +using namespace ke;
11.38 +
11.39 +static size_t sCtors = 0;
11.40 +static size_t sCopyCtors = 0;
11.41 +static size_t sMovingCtors = 0;
11.42 +static size_t sMovedDtors = 0;
11.43 +static size_t sDtors = 0;
11.44 +
11.45 +namespace {
11.46 +
11.47 +class BasicThing
11.48 +{
11.49 + public:
11.50 + BasicThing()
11.51 + {
11.52 + sCtors++;
11.53 + }
11.54 + BasicThing(const BasicThing &other)
11.55 + {
11.56 + sCopyCtors++;
11.57 + }
11.58 + ~BasicThing()
11.59 + {
11.60 + sDtors++;
11.61 + }
11.62 +};
11.63 +
11.64 +class MovingThing
11.65 +{
11.66 + public:
11.67 + MovingThing()
11.68 + : moved_(false)
11.69 + {
11.70 + sCtors++;
11.71 + }
11.72 + MovingThing(Moveable<MovingThing> other)
11.73 + {
11.74 + assert(!other->moved_);
11.75 + sMovingCtors++;
11.76 + moved_ = false;
11.77 + other->moved_ = true;
11.78 + }
11.79 + ~MovingThing()
11.80 + {
11.81 + sDtors++;
11.82 + if (moved_)
11.83 + sMovedDtors++;
11.84 + }
11.85 +
11.86 + private:
11.87 + MovingThing(const MovingThing &other) KE_DELETE;
11.88 + bool moved_;
11.89 +};
11.90 +
11.91 +} // anonymous namespace
11.92 +
11.93 +class TestLinkedList : public Test
11.94 +{
11.95 + public:
11.96 + TestLinkedList()
11.97 + : Test("LinkedList")
11.98 + {
11.99 + }
11.100 +
11.101 + bool testInts()
11.102 + {
11.103 + LinkedList<int> list;
11.104 +
11.105 + if (!check(list.empty(), "list should be empty"))
11.106 + return false;
11.107 + if (!check(list.begin() == list.end(), "list iterator should be empty"))
11.108 + return false;
11.109 +
11.110 + list.append(5);
11.111 + list.append(7);
11.112 + list.append(9);
11.113 + if (!check(!list.empty(), "list should not be empty"))
11.114 + return false;
11.115 + if (!check(list.length() == 3, "list size should be 3"))
11.116 + return false;
11.117 +
11.118 + LinkedList<int>::iterator iter = list.begin();
11.119 + if (!check(iter != list.end(), "list iterator should not have ended"))
11.120 + return false;
11.121 + if (!check(*iter == 5, "item 1 should be 5"))
11.122 + return false;
11.123 + iter++;
11.124 + if (!check(iter != list.end(), "list iterator should not have ended"))
11.125 + return false;
11.126 + if (!check(*iter == 7, "item 2 should be 7"))
11.127 + return false;
11.128 + iter++;
11.129 + if (!check(iter != list.end(), "list iterator should not have ended"))
11.130 + return false;
11.131 + if (!check(*iter == 9, "item 3 should be 9"))
11.132 + return false;
11.133 + iter++;
11.134 + if (!check(iter == list.end(), "list iterator should have ended"))
11.135 + return false;
11.136 +
11.137 + iter = list.begin();
11.138 + iter++;
11.139 + iter++;
11.140 + iter = list.erase(iter);
11.141 + if (!check(iter == list.end(), "erasure should have returned ended iterator"))
11.142 + return false;
11.143 + list.append(11);
11.144 +
11.145 + iter = list.begin();
11.146 + iter = list.erase(iter);
11.147 + if (!check(iter != list.end(), "erasure should not have ended iterator"))
11.148 + return false;
11.149 + if (!check(*iter == 7, "erasure should have returned item 2"))
11.150 + return false;
11.151 + iter++;
11.152 + if (!check(*iter == 11, "second item should be 11"))
11.153 + return false;
11.154 + if (!check(list.length() == 2, "list should have two items"))
11.155 + return false;
11.156 +
11.157 + list.prepend(1);
11.158 + iter = list.begin();
11.159 + if (!check(*iter == 1, "new item should have been prepended"))
11.160 + return false;
11.161 + iter++;
11.162 + if (!check(*iter == 7, "second item should be 7"))
11.163 + return false;
11.164 + if (!check(list.length() == 3, "list should have three items"))
11.165 + return false;
11.166 +
11.167 + list.clear();
11.168 + if (!check(list.empty(), "cleared list should be empty"))
11.169 + return false;
11.170 + if (!check(list.begin() == list.end(), "cleared list iterator should be empty"))
11.171 + return false;
11.172 +
11.173 + list.append(1);
11.174 + list.append(2);
11.175 + list.append(3);
11.176 + list.append(4);
11.177 + list.append(5);
11.178 + list.append(5);
11.179 + iter = list.find(3);
11.180 + if (!check(iter != list.end(), "should have found a 3"))
11.181 + return false;
11.182 + if (!check(*iter == 3, "should have found 3"))
11.183 + return false;
11.184 + list.remove(3);
11.185 + iter = list.find(3);
11.186 + if (!check(iter == list.end(), "should not have found a 3"))
11.187 + return false;
11.188 + iter = list.find(5);
11.189 + if (!check(iter != list.end(), "should have found a 5"))
11.190 + return false;
11.191 + if (!check(*iter == 5, "should have found 5"))
11.192 + return false;
11.193 + list.remove(5);
11.194 + iter = list.find(5);
11.195 + if (!check(iter != list.end(), "should have found a second 5"))
11.196 + return false;
11.197 + if (!check(*iter == 5, "should have found a second 5"))
11.198 + return false;
11.199 + list.remove(5);
11.200 + list.remove(5);
11.201 + iter = list.find(5);
11.202 + if (!check(iter == list.end(), "should not have found a third 5"))
11.203 + return false;
11.204 +
11.205 + return true;
11.206 + }
11.207 +
11.208 + bool testDestructors()
11.209 + {
11.210 + {
11.211 + LinkedList<BasicThing> list;
11.212 + list.append(BasicThing());
11.213 + list.append(BasicThing());
11.214 + list.append(BasicThing());
11.215 + }
11.216 + if (!check(sCtors == 3, "should get 3 normal constructors"))
11.217 + return false;
11.218 + if (!check(sCopyCtors == 3, "should get 3 copy constructors"))
11.219 + return false;
11.220 + if (!check(sDtors == 6, "should get 6 destructors"))
11.221 + return false;
11.222 +
11.223 + sCtors = 0;
11.224 + sCopyCtors = 0;
11.225 + sDtors = 0;
11.226 + return true;
11.227 + }
11.228 +
11.229 + bool testMoving()
11.230 + {
11.231 + {
11.232 + LinkedList<MovingThing> list;
11.233 + MovingThing a, b, c;
11.234 + list.append(Moveable<MovingThing>(a));
11.235 + list.append(Moveable<MovingThing>(b));
11.236 + list.append(Moveable<MovingThing>(c));
11.237 + }
11.238 +
11.239 + if (!check(sCtors == 3, "should get 3 normal constructors"))
11.240 + return false;
11.241 + if (!check(sMovingCtors == 3, "sould get 3 move constructors"))
11.242 + return false;
11.243 + if (!check(sMovedDtors == 3, "sould get 3 moved destructors"))
11.244 + return false;
11.245 + if (!check(sDtors == 6, "should get 6 destructors"))
11.246 + return false;
11.247 +
11.248 + sCtors = 0;
11.249 + sMovingCtors = 0;
11.250 + sMovedDtors = 0;
11.251 + sDtors = 0;
11.252 + return true;
11.253 + }
11.254 +
11.255 + bool testFallibleMalloc()
11.256 + {
11.257 + LinkedList<int, FallibleMalloc> list;
11.258 + list.append(5);
11.259 + list.append(6);
11.260 + list.setOutOfMemory(true);
11.261 + if (!check(!list.append(7), "list handled out-of-memory"))
11.262 + return false;
11.263 + list.setOutOfMemory(false);
11.264 + if (!check(list.append(8), "list recovered out-of-memory"))
11.265 + return false;
11.266 + if (!check(list.ooms() == 1, "list received 1 oom"))
11.267 + return false;
11.268 + return true;
11.269 + }
11.270 +
11.271 + bool Run() KE_OVERRIDE
11.272 + {
11.273 + if (!testInts())
11.274 + return false;
11.275 + if (!testDestructors())
11.276 + return false;
11.277 + if (!testMoving())
11.278 + return false;
11.279 + if (!testFallibleMalloc())
11.280 + return false;
11.281 + return true;
11.282 + }
11.283 +} sTestLinkedList;
11.284 +
12.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
12.2 +++ b/tests/test-refcounting.cpp Mon Aug 26 23:18:03 2013 -0700
12.3 @@ -0,0 +1,84 @@
12.4 +// vim: set sts=8 ts=2 sw=2 tw=99 et:
12.5 +//
12.6 +// Copyright (C) 2013, David Anderson and AlliedModders LLC
12.7 +// All rights reserved.
12.8 +//
12.9 +// Redistribution and use in source and binary forms, with or without
12.10 +// modification, are permitted provided that the following conditions are met:
12.11 +//
12.12 +// * Redistributions of source code must retain the above copyright notice, this
12.13 +// list of conditions and the following disclaimer.
12.14 +// * Redistributions in binary form must reproduce the above copyright notice,
12.15 +// this list of conditions and the following disclaimer in the documentation
12.16 +// and/or other materials provided with the distribution.
12.17 +// * Neither the name of AlliedModders LLC nor the names of its contributors
12.18 +// may be used to endorse or promote products derived from this software
12.19 +// without specific prior written permission.
12.20 +//
12.21 +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
12.22 +// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
12.23 +// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
12.24 +// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
12.25 +// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
12.26 +// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
12.27 +// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
12.28 +// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
12.29 +// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
12.30 +// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
12.31 +// POSSIBILITY OF SUCH DAMAGE.
12.32 +
12.33 +#include <am-utility.h>
12.34 +#include <am-refcounting.h>
12.35 +#include "runner.h"
12.36 +
12.37 +using namespace ke;
12.38 +
12.39 +static size_t sDtors = 0;
12.40 +
12.41 +class Counted : public Refcounted<Counted>
12.42 +{
12.43 + public:
12.44 + ~Counted() {
12.45 + sDtors++;
12.46 + }
12.47 +};
12.48 +
12.49 +static inline PassRef<Counted>
12.50 +PassThrough(const Ref<Counted> &obj)
12.51 +{
12.52 + return obj;
12.53 +}
12.54 +
12.55 +class TestRefcounting : public Test
12.56 +{
12.57 + public:
12.58 + TestRefcounting()
12.59 + : Test("Refcounting")
12.60 + {
12.61 + }
12.62 +
12.63 + bool Run() KE_OVERRIDE
12.64 + {
12.65 + {
12.66 + Ref<Counted> obj(Newborn<Counted>(new Counted()));
12.67 + }
12.68 + if (!check(sDtors == 1, "Ref/Newborn counted properly"))
12.69 + return false;
12.70 +
12.71 + {
12.72 + Ref<Counted> obj(Newborn<Counted>(new Counted()));
12.73 + Ref<Counted> obj2 = PassThrough(obj);
12.74 + Ref<Counted> obj3 = PassThrough(obj);
12.75 + if (!check(sDtors == 1, "destructor not called early"))
12.76 + return false;
12.77 + Ref<Counted> obj4 = PassThrough(PassThrough(PassThrough(obj)));
12.78 + if (!check(sDtors == 1, "destructor not called early"))
12.79 + return false;
12.80 + }
12.81 + if (!check(sDtors == 2, "PassRef/Ref counted properly"))
12.82 + return false;
12.83 +
12.84 + return true;
12.85 + }
12.86 +} sTestRefcounting;
12.87 +
13.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
13.2 +++ b/tests/test-threadutils.cpp Mon Aug 26 23:18:03 2013 -0700
13.3 @@ -0,0 +1,147 @@
13.4 +// vim: set sts=8 ts=2 sw=2 tw=99 et:
13.5 +//
13.6 +// Copyright (C) 2013, David Anderson and AlliedModders LLC
13.7 +// All rights reserved.
13.8 +//
13.9 +// Redistribution and use in source and binary forms, with or without
13.10 +// modification, are permitted provided that the following conditions are met:
13.11 +//
13.12 +// * Redistributions of source code must retain the above copyright notice, this
13.13 +// list of conditions and the following disclaimer.
13.14 +// * Redistributions in binary form must reproduce the above copyright notice,
13.15 +// this list of conditions and the following disclaimer in the documentation
13.16 +// and/or other materials provided with the distribution.
13.17 +// * Neither the name of AlliedModders LLC nor the names of its contributors
13.18 +// may be used to endorse or promote products derived from this software
13.19 +// without specific prior written permission.
13.20 +//
13.21 +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
13.22 +// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
13.23 +// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
13.24 +// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
13.25 +// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
13.26 +// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
13.27 +// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
13.28 +// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
13.29 +// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
13.30 +// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
13.31 +// POSSIBILITY OF SUCH DAMAGE.
13.32 +
13.33 +#include <am-thread-utils.h>
13.34 +#include <am-utility.h>
13.35 +#include <am-linkedlist.h>
13.36 +#include "runner.h"
13.37 +
13.38 +using namespace ke;
13.39 +
13.40 +// Simple worker that adds up numbers.
13.41 +class TestWorkerModel : public IRunnable
13.42 +{
13.43 + public:
13.44 + TestWorkerModel()
13.45 + : terminate_(false),
13.46 + result_(0)
13.47 + {
13.48 + }
13.49 +
13.50 + void Run() {
13.51 + {
13.52 + AutoLock lock(main());
13.53 + main()->Notify();
13.54 + }
13.55 +
13.56 + AutoLock lock(wakeup());
13.57 + while (true) {
13.58 + while (items_.begin() != items_.end()) {
13.59 + int value = *(items_.begin());
13.60 + items_.erase(items_.begin());
13.61 +
13.62 + AutoUnlock unlock(wakeup());
13.63 + result_ += value;
13.64 + }
13.65 +
13.66 + // Only terminate if we're guaranteed the queue has no items - after
13.67 + // we've just finished processing all items in the queue, while the lock
13.68 + // is held.
13.69 + if (terminate_)
13.70 + return;
13.71 +
13.72 + wakeup()->Wait();
13.73 + }
13.74 + }
13.75 +
13.76 + ConditionVariable *main() {
13.77 + return &main_;
13.78 + }
13.79 + ConditionVariable *wakeup() {
13.80 + return &wakeup_;
13.81 + }
13.82 +
13.83 + void terminate() {
13.84 + AutoLock lock(&wakeup_);
13.85 + terminate_ = true;
13.86 + wakeup_.Notify();
13.87 + }
13.88 +
13.89 + int result() {
13.90 + return result_;
13.91 + }
13.92 +
13.93 + void send(int value) {
13.94 + AutoLock lock(&wakeup_);
13.95 + items_.append(value);
13.96 + wakeup_.Notify();
13.97 + }
13.98 +
13.99 + private:
13.100 + ConditionVariable main_;
13.101 + ConditionVariable wakeup_;
13.102 + LinkedList<int> items_;
13.103 + bool terminate_;
13.104 + int result_;
13.105 +};
13.106 +
13.107 +class TestThreading : public Test
13.108 +{
13.109 + public:
13.110 + TestThreading()
13.111 + : Test("ThreadUtils")
13.112 + {
13.113 + }
13.114 +
13.115 + bool testWorkerModel()
13.116 + {
13.117 + TestWorkerModel test;
13.118 + ke::AutoPtr<Thread> thread(new Thread(&test, "TestWorkerModel"));
13.119 + if (!check(thread->Succeeded(), "thread launched"))
13.120 + return false;
13.121 +
13.122 + {
13.123 + AutoLock lock(test.main());
13.124 + if (!check(test.main()->Wait(5000) == Wait_Signaled, "thread started within five seconds"))
13.125 + return false;
13.126 + }
13.127 +
13.128 + int total = 0;
13.129 + for (int i = 0; i < 100000; i++) {
13.130 + test.send(i);
13.131 + total += i;
13.132 + }
13.133 +
13.134 + test.terminate();
13.135 + thread->Join();
13.136 +
13.137 + if (!check(test.result() == total, "thread returned correct result"))
13.138 + return false;
13.139 +
13.140 + return true;
13.141 + }
13.142 +
13.143 + bool Run() KE_OVERRIDE
13.144 + {
13.145 + if (!testWorkerModel())
13.146 + return false;
13.147 + return true;
13.148 + }
13.149 +} sTestThreading;
13.150 +
14.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
14.2 +++ b/tests/test-vector.cpp Mon Aug 26 23:18:03 2013 -0700
14.3 @@ -0,0 +1,233 @@
14.4 +// vim: set sts=8 ts=2 sw=2 tw=99 et:
14.5 +//
14.6 +// Copyright (C) 2013, David Anderson and AlliedModders LLC
14.7 +// All rights reserved.
14.8 +//
14.9 +// Redistribution and use in source and binary forms, with or without
14.10 +// modification, are permitted provided that the following conditions are met:
14.11 +//
14.12 +// * Redistributions of source code must retain the above copyright notice, this
14.13 +// list of conditions and the following disclaimer.
14.14 +// * Redistributions in binary form must reproduce the above copyright notice,
14.15 +// this list of conditions and the following disclaimer in the documentation
14.16 +// and/or other materials provided with the distribution.
14.17 +// * Neither the name of AlliedModders LLC nor the names of its contributors
14.18 +// may be used to endorse or promote products derived from this software
14.19 +// without specific prior written permission.
14.20 +//
14.21 +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
14.22 +// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
14.23 +// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
14.24 +// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
14.25 +// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
14.26 +// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
14.27 +// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
14.28 +// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
14.29 +// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
14.30 +// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
14.31 +// POSSIBILITY OF SUCH DAMAGE.
14.32 +
14.33 +#include <am-vector.h>
14.34 +#include <assert.h>
14.35 +#include "runner.h"
14.36 +
14.37 +using namespace ke;
14.38 +
14.39 +static size_t sCtors = 0;
14.40 +static size_t sCopyCtors = 0;
14.41 +static size_t sMovingCtors = 0;
14.42 +static size_t sMovedDtors = 0;
14.43 +static size_t sDtors = 0;
14.44 +
14.45 +namespace {
14.46 +
14.47 +class BasicThing
14.48 +{
14.49 + public:
14.50 + BasicThing()
14.51 + {
14.52 + sCtors++;
14.53 + }
14.54 + BasicThing(const BasicThing &other)
14.55 + {
14.56 + sCopyCtors++;
14.57 + }
14.58 + ~BasicThing()
14.59 + {
14.60 + sDtors++;
14.61 + }
14.62 +};
14.63 +
14.64 +class MovingThing
14.65 +{
14.66 + public:
14.67 + MovingThing()
14.68 + : moved_(false)
14.69 + {
14.70 + sCtors++;
14.71 + }
14.72 + MovingThing(Moveable<MovingThing> other)
14.73 + {
14.74 + assert(!other->moved_);
14.75 + sMovingCtors++;
14.76 + moved_ = false;
14.77 + other->moved_ = true;
14.78 + }
14.79 + ~MovingThing()
14.80 + {
14.81 + sDtors++;
14.82 + if (moved_)
14.83 + sMovedDtors++;
14.84 + }
14.85 +
14.86 + private:
14.87 + MovingThing(const MovingThing &other) KE_DELETE;
14.88 + bool moved_;
14.89 +};
14.90 +
14.91 +} // anonymous namespace
14.92 +
14.93 +class TestVector : public Test
14.94 +{
14.95 + public:
14.96 + TestVector()
14.97 + : Test("Vector")
14.98 + {
14.99 + }
14.100 +
14.101 + bool testInts()
14.102 + {
14.103 + Vector<int> vector;
14.104 +
14.105 + if (!check(vector.empty(), "vector should be empty"))
14.106 + return false;
14.107 + if (!check(vector.length() == 0, "vector length should be 0"))
14.108 + return false;
14.109 +
14.110 + vector.append(1);
14.111 + vector.append(2);
14.112 + vector.append(3);
14.113 + vector.append(4);
14.114 + vector.append(5);
14.115 + for (int i = 0; i < 5; i++) {
14.116 + if (!check(vector[i] == i + 1, "vector[%d] is %d", i, i))
14.117 + return false;
14.118 + }
14.119 + if (!check(vector.length() == 5, "vector length is 5"))
14.120 + return false;
14.121 + if (!check(!vector.empty(), "vector is not empty"))
14.122 + return false;
14.123 +
14.124 + if (!check(vector.popCopy() == 5, "popped 5"))
14.125 + return false;
14.126 + if (!check(vector.popCopy() == 4, "popped 4"))
14.127 + return false;
14.128 + if (!check(vector.length() == 3, "vector length is 3"))
14.129 + return false;
14.130 +
14.131 + while (!vector.empty())
14.132 + vector.pop();
14.133 +
14.134 + if (!check(vector.empty(), "vector should be empty"))
14.135 + return false;
14.136 + if (!check(vector.ensure(128), "vector size ensured"))
14.137 + return false;
14.138 + if (!check(vector.empty(), "vector should be empty"))
14.139 + return false;
14.140 +
14.141 + for (int i = 0; i < 128; i++)
14.142 + vector.append(i);
14.143 + if (!check(vector.length() == 128, "vector length should be 128"))
14.144 + return false;
14.145 +
14.146 + vector.clear();
14.147 + if (!check(vector.empty(), "vector should be empty"))
14.148 + return false;
14.149 +
14.150 + return true;
14.151 + }
14.152 +
14.153 + bool testDestructors()
14.154 + {
14.155 + {
14.156 + Vector<BasicThing> vector;
14.157 + vector.append(BasicThing());
14.158 + vector.append(BasicThing());
14.159 + vector.append(BasicThing());
14.160 + }
14.161 + if (!check(sCtors == 3, "should get 3 normal constructors"))
14.162 + return false;
14.163 + if (!check(sCopyCtors == 3, "should get 3 copy constructors"))
14.164 + return false;
14.165 + if (!check(sDtors == 6, "should get 6 destructors"))
14.166 + return false;
14.167 +
14.168 + sCtors = 0;
14.169 + sCopyCtors = 0;
14.170 + sDtors = 0;
14.171 + return true;
14.172 + }
14.173 +
14.174 + bool testMoving()
14.175 + {
14.176 + {
14.177 + Vector<MovingThing> vector;
14.178 + MovingThing a, b, c;
14.179 + vector.append(Moveable<MovingThing>(a));
14.180 + vector.append(Moveable<MovingThing>(b));
14.181 + vector.append(Moveable<MovingThing>(c));
14.182 + }
14.183 +
14.184 + if (!check(sCtors == 3, "should get 3 normal constructors"))
14.185 + return false;
14.186 + if (!check(sMovingCtors == 3, "sould get 3 move constructors"))
14.187 + return false;
14.188 + if (!check(sMovedDtors == 3, "sould get 3 moved destructors"))
14.189 + return false;
14.190 + if (!check(sDtors == 6, "should get 6 destructors"))
14.191 + return false;
14.192 +
14.193 + sCtors = 0;
14.194 + sMovingCtors = 0;
14.195 + sMovedDtors = 0;
14.196 + sDtors = 0;
14.197 +
14.198 + Vector<int> v1;
14.199 + v1.append(10);
14.200 + Vector<int> v2(Move(v1));
14.201 + if (!check(v2.length() == 1, "should have moved vector"))
14.202 + return false;
14.203 + if (!check(v1.length() == 0, "should have gutted vector"))
14.204 + return false;
14.205 +
14.206 + return true;
14.207 + }
14.208 +
14.209 + bool testFallibleMalloc()
14.210 + {
14.211 + Vector<int, FallibleMalloc> vector;
14.212 + vector.setOutOfMemory(true);
14.213 + if (!check(!vector.append(7), "vector handled out-of-memory"))
14.214 + return false;
14.215 + vector.setOutOfMemory(false);
14.216 + if (!check(vector.append(8), "vector recovered out-of-memory"))
14.217 + return false;
14.218 + if (!check(vector.ooms() == 1, "vector received 1 oom"))
14.219 + return false;
14.220 + return true;
14.221 + }
14.222 +
14.223 + bool Run() KE_OVERRIDE
14.224 + {
14.225 + if (!testInts())
14.226 + return false;
14.227 + if (!testDestructors())
14.228 + return false;
14.229 + if (!testMoving())
14.230 + return false;
14.231 + if (!testFallibleMalloc())
14.232 + return false;
14.233 + return true;
14.234 + }
14.235 +} sTestVector;
14.236 +