tests/test-inlinelist.cpp
changeset 11 cfe800908837
child 17 563aea5eba06
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/tests/test-inlinelist.cpp	Mon Aug 26 23:18:03 2013 -0700
     1.3 @@ -0,0 +1,132 @@
     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-inlinelist.h>
    1.34 +#include <am-utility.h>
    1.35 +#include "runner.h"
    1.36 +
    1.37 +using namespace ke;
    1.38 +
    1.39 +class IntThing : public InlineListNode<IntThing>
    1.40 +{
    1.41 + public:
    1.42 +  IntThing(int value)
    1.43 +   : value_(value)
    1.44 +  {
    1.45 +  }
    1.46 +  ~IntThing()
    1.47 +  {
    1.48 +  }
    1.49 +  int value() const {
    1.50 +    return value_;
    1.51 +  }
    1.52 +
    1.53 + private:
    1.54 +  int value_;
    1.55 +};
    1.56 +
    1.57 +class TestInlineList : public Test
    1.58 +{
    1.59 + public:
    1.60 +  TestInlineList()
    1.61 +   : Test("InlineList")
    1.62 +  {
    1.63 +  }
    1.64 +
    1.65 +  bool testBasic()
    1.66 +  {
    1.67 +    IntThing thing1(1);
    1.68 +    IntThing thing2(2);
    1.69 +    IntThing thing3(3);
    1.70 +    IntThing thing4(4);
    1.71 +    IntThing thing5(5);
    1.72 +
    1.73 +    // Do these test twice. InlineList does not take ownership of pointers,
    1.74 +    // so we should be guaranteed we can keep moving them in between lists.
    1.75 +    for (size_t i = 0; i <= 1; i++)
    1.76 +    {
    1.77 +      InlineList<IntThing> list;
    1.78 +
    1.79 +      InlineList<IntThing>::iterator iter = list.begin();
    1.80 +      if (!check(iter == list.end(), "list should be empty"))
    1.81 +        return false;
    1.82 +      
    1.83 +      list.append(&thing1);
    1.84 +      list.append(&thing2);
    1.85 +      list.append(&thing3);
    1.86 +      list.append(&thing4);
    1.87 +      list.append(&thing5);
    1.88 +
    1.89 +      iter = list.begin();
    1.90 +      for (int n = 1; n <= 5; n++) {
    1.91 +        if (!check(iter->value() == n, "value %d should be %d", iter->value(), n))
    1.92 +          return false;
    1.93 +        iter++;
    1.94 +      }
    1.95 +      if (!check(iter == list.end(), "iterator should have ended"))
    1.96 +        return false;
    1.97 +
    1.98 +      list.remove(&thing1);
    1.99 +      iter = list.begin();
   1.100 +      if (!check(iter->value() == 2, "list should start at 2 after removal"))
   1.101 +        return false;
   1.102 +
   1.103 +      list.remove(&thing5);
   1.104 +      iter = list.begin();
   1.105 +      iter++;
   1.106 +      iter++;
   1.107 +      if (!check(iter->value() == 4, "list should end at 4 after removal"))
   1.108 +        return false;
   1.109 +      iter++;
   1.110 +      if (!check(iter == list.end(), "iterator should have ended"))
   1.111 +        return false;
   1.112 +
   1.113 +      list.remove(&thing3);
   1.114 +      iter = list.begin();
   1.115 +      if (!check(iter->value() == 2, "first value should be 2 after removal"))
   1.116 +        return false;
   1.117 +      iter++;
   1.118 +      if (!check(iter->value() == 4, "second value should be 4 after removal"))
   1.119 +        return false;
   1.120 +      iter++;
   1.121 +      if (!check(iter == list.end(), "iterator should have ended"))
   1.122 +        return false;
   1.123 +    }
   1.124 +
   1.125 +    return true;
   1.126 +  }
   1.127 +
   1.128 +  bool Run() KE_OVERRIDE
   1.129 +  {
   1.130 +    if (!testBasic())
   1.131 +      return false;
   1.132 +    return true;
   1.133 +  }
   1.134 +} sTestInlineList;
   1.135 +