1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/tests/test-vector.cpp Mon Aug 26 23:18:03 2013 -0700
1.3 @@ -0,0 +1,233 @@
1.4 +// vim: set sts=8 ts=2 sw=2 tw=99 et:
1.5 +//
1.6 +// Copyright (C) 2013, David Anderson and AlliedModders LLC
1.7 +// All rights reserved.
1.8 +//
1.9 +// Redistribution and use in source and binary forms, with or without
1.10 +// modification, are permitted provided that the following conditions are met:
1.11 +//
1.12 +// * Redistributions of source code must retain the above copyright notice, this
1.13 +// list of conditions and the following disclaimer.
1.14 +// * Redistributions in binary form must reproduce the above copyright notice,
1.15 +// this list of conditions and the following disclaimer in the documentation
1.16 +// and/or other materials provided with the distribution.
1.17 +// * Neither the name of AlliedModders LLC nor the names of its contributors
1.18 +// may be used to endorse or promote products derived from this software
1.19 +// without specific prior written permission.
1.20 +//
1.21 +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
1.22 +// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1.23 +// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1.24 +// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
1.25 +// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
1.26 +// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
1.27 +// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
1.28 +// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
1.29 +// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
1.30 +// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
1.31 +// POSSIBILITY OF SUCH DAMAGE.
1.32 +
1.33 +#include <am-vector.h>
1.34 +#include <assert.h>
1.35 +#include "runner.h"
1.36 +
1.37 +using namespace ke;
1.38 +
1.39 +static size_t sCtors = 0;
1.40 +static size_t sCopyCtors = 0;
1.41 +static size_t sMovingCtors = 0;
1.42 +static size_t sMovedDtors = 0;
1.43 +static size_t sDtors = 0;
1.44 +
1.45 +namespace {
1.46 +
1.47 +class BasicThing
1.48 +{
1.49 + public:
1.50 + BasicThing()
1.51 + {
1.52 + sCtors++;
1.53 + }
1.54 + BasicThing(const BasicThing &other)
1.55 + {
1.56 + sCopyCtors++;
1.57 + }
1.58 + ~BasicThing()
1.59 + {
1.60 + sDtors++;
1.61 + }
1.62 +};
1.63 +
1.64 +class MovingThing
1.65 +{
1.66 + public:
1.67 + MovingThing()
1.68 + : moved_(false)
1.69 + {
1.70 + sCtors++;
1.71 + }
1.72 + MovingThing(Moveable<MovingThing> other)
1.73 + {
1.74 + assert(!other->moved_);
1.75 + sMovingCtors++;
1.76 + moved_ = false;
1.77 + other->moved_ = true;
1.78 + }
1.79 + ~MovingThing()
1.80 + {
1.81 + sDtors++;
1.82 + if (moved_)
1.83 + sMovedDtors++;
1.84 + }
1.85 +
1.86 + private:
1.87 + MovingThing(const MovingThing &other) KE_DELETE;
1.88 + bool moved_;
1.89 +};
1.90 +
1.91 +} // anonymous namespace
1.92 +
1.93 +class TestVector : public Test
1.94 +{
1.95 + public:
1.96 + TestVector()
1.97 + : Test("Vector")
1.98 + {
1.99 + }
1.100 +
1.101 + bool testInts()
1.102 + {
1.103 + Vector<int> vector;
1.104 +
1.105 + if (!check(vector.empty(), "vector should be empty"))
1.106 + return false;
1.107 + if (!check(vector.length() == 0, "vector length should be 0"))
1.108 + return false;
1.109 +
1.110 + vector.append(1);
1.111 + vector.append(2);
1.112 + vector.append(3);
1.113 + vector.append(4);
1.114 + vector.append(5);
1.115 + for (int i = 0; i < 5; i++) {
1.116 + if (!check(vector[i] == i + 1, "vector[%d] is %d", i, i))
1.117 + return false;
1.118 + }
1.119 + if (!check(vector.length() == 5, "vector length is 5"))
1.120 + return false;
1.121 + if (!check(!vector.empty(), "vector is not empty"))
1.122 + return false;
1.123 +
1.124 + if (!check(vector.popCopy() == 5, "popped 5"))
1.125 + return false;
1.126 + if (!check(vector.popCopy() == 4, "popped 4"))
1.127 + return false;
1.128 + if (!check(vector.length() == 3, "vector length is 3"))
1.129 + return false;
1.130 +
1.131 + while (!vector.empty())
1.132 + vector.pop();
1.133 +
1.134 + if (!check(vector.empty(), "vector should be empty"))
1.135 + return false;
1.136 + if (!check(vector.ensure(128), "vector size ensured"))
1.137 + return false;
1.138 + if (!check(vector.empty(), "vector should be empty"))
1.139 + return false;
1.140 +
1.141 + for (int i = 0; i < 128; i++)
1.142 + vector.append(i);
1.143 + if (!check(vector.length() == 128, "vector length should be 128"))
1.144 + return false;
1.145 +
1.146 + vector.clear();
1.147 + if (!check(vector.empty(), "vector should be empty"))
1.148 + return false;
1.149 +
1.150 + return true;
1.151 + }
1.152 +
1.153 + bool testDestructors()
1.154 + {
1.155 + {
1.156 + Vector<BasicThing> vector;
1.157 + vector.append(BasicThing());
1.158 + vector.append(BasicThing());
1.159 + vector.append(BasicThing());
1.160 + }
1.161 + if (!check(sCtors == 3, "should get 3 normal constructors"))
1.162 + return false;
1.163 + if (!check(sCopyCtors == 3, "should get 3 copy constructors"))
1.164 + return false;
1.165 + if (!check(sDtors == 6, "should get 6 destructors"))
1.166 + return false;
1.167 +
1.168 + sCtors = 0;
1.169 + sCopyCtors = 0;
1.170 + sDtors = 0;
1.171 + return true;
1.172 + }
1.173 +
1.174 + bool testMoving()
1.175 + {
1.176 + {
1.177 + Vector<MovingThing> vector;
1.178 + MovingThing a, b, c;
1.179 + vector.append(Moveable<MovingThing>(a));
1.180 + vector.append(Moveable<MovingThing>(b));
1.181 + vector.append(Moveable<MovingThing>(c));
1.182 + }
1.183 +
1.184 + if (!check(sCtors == 3, "should get 3 normal constructors"))
1.185 + return false;
1.186 + if (!check(sMovingCtors == 3, "sould get 3 move constructors"))
1.187 + return false;
1.188 + if (!check(sMovedDtors == 3, "sould get 3 moved destructors"))
1.189 + return false;
1.190 + if (!check(sDtors == 6, "should get 6 destructors"))
1.191 + return false;
1.192 +
1.193 + sCtors = 0;
1.194 + sMovingCtors = 0;
1.195 + sMovedDtors = 0;
1.196 + sDtors = 0;
1.197 +
1.198 + Vector<int> v1;
1.199 + v1.append(10);
1.200 + Vector<int> v2(Move(v1));
1.201 + if (!check(v2.length() == 1, "should have moved vector"))
1.202 + return false;
1.203 + if (!check(v1.length() == 0, "should have gutted vector"))
1.204 + return false;
1.205 +
1.206 + return true;
1.207 + }
1.208 +
1.209 + bool testFallibleMalloc()
1.210 + {
1.211 + Vector<int, FallibleMalloc> vector;
1.212 + vector.setOutOfMemory(true);
1.213 + if (!check(!vector.append(7), "vector handled out-of-memory"))
1.214 + return false;
1.215 + vector.setOutOfMemory(false);
1.216 + if (!check(vector.append(8), "vector recovered out-of-memory"))
1.217 + return false;
1.218 + if (!check(vector.ooms() == 1, "vector received 1 oom"))
1.219 + return false;
1.220 + return true;
1.221 + }
1.222 +
1.223 + bool Run() KE_OVERRIDE
1.224 + {
1.225 + if (!testInts())
1.226 + return false;
1.227 + if (!testDestructors())
1.228 + return false;
1.229 + if (!testMoving())
1.230 + return false;
1.231 + if (!testFallibleMalloc())
1.232 + return false;
1.233 + return true;
1.234 + }
1.235 +} sTestVector;
1.236 +