tests/test-linkedlist.cpp
changeset 11 cfe800908837
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/tests/test-linkedlist.cpp	Mon Aug 26 23:18:03 2013 -0700
     1.3 @@ -0,0 +1,281 @@
     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-linkedlist.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 TestLinkedList : public Test
    1.94 +{
    1.95 + public:
    1.96 +  TestLinkedList()
    1.97 +   : Test("LinkedList")
    1.98 +  {
    1.99 +  }
   1.100 +
   1.101 +  bool testInts()
   1.102 +  {
   1.103 +    LinkedList<int> list;
   1.104 +
   1.105 +    if (!check(list.empty(), "list should be empty"))
   1.106 +      return false;
   1.107 +    if (!check(list.begin() == list.end(), "list iterator should be empty"))
   1.108 +      return false;
   1.109 +
   1.110 +    list.append(5);
   1.111 +    list.append(7);
   1.112 +    list.append(9);
   1.113 +    if (!check(!list.empty(), "list should not be empty"))
   1.114 +      return false;
   1.115 +    if (!check(list.length() == 3, "list size should be 3"))
   1.116 +      return false;
   1.117 +
   1.118 +    LinkedList<int>::iterator iter = list.begin();
   1.119 +    if (!check(iter != list.end(), "list iterator should not have ended"))
   1.120 +      return false;
   1.121 +    if (!check(*iter == 5, "item 1 should be 5"))
   1.122 +      return false;
   1.123 +    iter++;
   1.124 +    if (!check(iter != list.end(), "list iterator should not have ended"))
   1.125 +      return false;
   1.126 +    if (!check(*iter == 7, "item 2 should be 7"))
   1.127 +      return false;
   1.128 +    iter++;
   1.129 +    if (!check(iter != list.end(), "list iterator should not have ended"))
   1.130 +      return false;
   1.131 +    if (!check(*iter == 9, "item 3 should be 9"))
   1.132 +      return false;
   1.133 +    iter++;
   1.134 +    if (!check(iter == list.end(), "list iterator should have ended"))
   1.135 +      return false;
   1.136 +
   1.137 +    iter = list.begin();
   1.138 +    iter++;
   1.139 +    iter++;
   1.140 +    iter = list.erase(iter);
   1.141 +    if (!check(iter == list.end(), "erasure should have returned ended iterator"))
   1.142 +      return false;
   1.143 +    list.append(11);
   1.144 +
   1.145 +    iter = list.begin();
   1.146 +    iter = list.erase(iter);
   1.147 +    if (!check(iter != list.end(), "erasure should not have ended iterator"))
   1.148 +      return false;
   1.149 +    if (!check(*iter == 7, "erasure should have returned item 2"))
   1.150 +      return false;
   1.151 +    iter++;
   1.152 +    if (!check(*iter == 11, "second item should be 11"))
   1.153 +      return false;
   1.154 +    if (!check(list.length() == 2, "list should have two items"))
   1.155 +      return false;
   1.156 +
   1.157 +    list.prepend(1);
   1.158 +    iter = list.begin();
   1.159 +    if (!check(*iter == 1, "new item should have been prepended"))
   1.160 +      return false;
   1.161 +    iter++;
   1.162 +    if (!check(*iter == 7, "second item should be 7"))
   1.163 +      return false;
   1.164 +    if (!check(list.length() == 3, "list should have three items"))
   1.165 +      return false;
   1.166 +
   1.167 +    list.clear();
   1.168 +    if (!check(list.empty(), "cleared list should be empty"))
   1.169 +      return false;
   1.170 +    if (!check(list.begin() == list.end(), "cleared list iterator should be empty"))
   1.171 +      return false;
   1.172 +
   1.173 +    list.append(1);
   1.174 +    list.append(2);
   1.175 +    list.append(3);
   1.176 +    list.append(4);
   1.177 +    list.append(5);
   1.178 +    list.append(5);
   1.179 +    iter = list.find(3);
   1.180 +    if (!check(iter != list.end(), "should have found a 3"))
   1.181 +      return false;
   1.182 +    if (!check(*iter == 3, "should have found 3"))
   1.183 +      return false;
   1.184 +    list.remove(3);
   1.185 +    iter = list.find(3);
   1.186 +    if (!check(iter == list.end(), "should not have found a 3"))
   1.187 +      return false;
   1.188 +    iter = list.find(5);
   1.189 +    if (!check(iter != list.end(), "should have found a 5"))
   1.190 +      return false;
   1.191 +    if (!check(*iter == 5, "should have found 5"))
   1.192 +      return false;
   1.193 +    list.remove(5);
   1.194 +    iter = list.find(5);
   1.195 +    if (!check(iter != list.end(), "should have found a second 5"))
   1.196 +      return false;
   1.197 +    if (!check(*iter == 5, "should have found a second 5"))
   1.198 +      return false;
   1.199 +    list.remove(5);
   1.200 +    list.remove(5);
   1.201 +    iter = list.find(5);
   1.202 +    if (!check(iter == list.end(), "should not have found a third 5"))
   1.203 +      return false;
   1.204 +
   1.205 +    return true;
   1.206 +  }
   1.207 +
   1.208 +  bool testDestructors()
   1.209 +  {
   1.210 +    {
   1.211 +      LinkedList<BasicThing> list;
   1.212 +      list.append(BasicThing());
   1.213 +      list.append(BasicThing());
   1.214 +      list.append(BasicThing());
   1.215 +    }
   1.216 +    if (!check(sCtors == 3, "should get 3 normal constructors"))
   1.217 +      return false;
   1.218 +    if (!check(sCopyCtors == 3, "should get 3 copy constructors"))
   1.219 +      return false;
   1.220 +    if (!check(sDtors == 6, "should get 6 destructors"))
   1.221 +      return false;
   1.222 +
   1.223 +    sCtors = 0;
   1.224 +    sCopyCtors = 0;
   1.225 +    sDtors = 0;
   1.226 +    return true;
   1.227 +  }
   1.228 +
   1.229 +  bool testMoving()
   1.230 +  {
   1.231 +    {
   1.232 +      LinkedList<MovingThing> list;
   1.233 +      MovingThing a, b, c;
   1.234 +      list.append(Moveable<MovingThing>(a));
   1.235 +      list.append(Moveable<MovingThing>(b));
   1.236 +      list.append(Moveable<MovingThing>(c));
   1.237 +    }
   1.238 +
   1.239 +    if (!check(sCtors == 3, "should get 3 normal constructors"))
   1.240 +      return false;
   1.241 +    if (!check(sMovingCtors == 3, "sould get 3 move constructors"))
   1.242 +      return false;
   1.243 +    if (!check(sMovedDtors == 3, "sould get 3 moved destructors"))
   1.244 +      return false;
   1.245 +    if (!check(sDtors == 6, "should get 6 destructors"))
   1.246 +      return false;
   1.247 +
   1.248 +    sCtors = 0;
   1.249 +    sMovingCtors = 0;
   1.250 +    sMovedDtors = 0;
   1.251 +    sDtors = 0;
   1.252 +    return true;
   1.253 +  }
   1.254 +
   1.255 +  bool testFallibleMalloc()
   1.256 +  {
   1.257 +    LinkedList<int, FallibleMalloc> list;
   1.258 +    list.append(5);
   1.259 +    list.append(6);
   1.260 +    list.setOutOfMemory(true);
   1.261 +    if (!check(!list.append(7), "list handled out-of-memory"))
   1.262 +      return false;
   1.263 +    list.setOutOfMemory(false);
   1.264 +    if (!check(list.append(8), "list recovered out-of-memory"))
   1.265 +      return false;
   1.266 +    if (!check(list.ooms() == 1, "list received 1 oom"))
   1.267 +      return false;
   1.268 +    return true;
   1.269 +  }
   1.270 +
   1.271 +  bool Run() KE_OVERRIDE
   1.272 +  {
   1.273 +    if (!testInts())
   1.274 +      return false;
   1.275 +    if (!testDestructors())
   1.276 +      return false;
   1.277 +    if (!testMoving())
   1.278 +      return false;
   1.279 +    if (!testFallibleMalloc())
   1.280 +      return false;
   1.281 +    return true;
   1.282 +  }
   1.283 +} sTestLinkedList;
   1.284 +