tests/test-linkedlist.cpp
author David Anderson <danderson@mozilla.com>
Mon Aug 26 23:18:03 2013 -0700 (2013-08-26)
changeset 11 cfe800908837
permissions -rw-r--r--
Add tests.
     1 // vim: set sts=8 ts=2 sw=2 tw=99 et:
     2 //
     3 // Copyright (C) 2013, David Anderson and AlliedModders LLC
     4 // All rights reserved.
     5 // 
     6 // Redistribution and use in source and binary forms, with or without
     7 // modification, are permitted provided that the following conditions are met:
     8 // 
     9 //  * Redistributions of source code must retain the above copyright notice, this
    10 //    list of conditions and the following disclaimer.
    11 //  * Redistributions in binary form must reproduce the above copyright notice,
    12 //    this list of conditions and the following disclaimer in the documentation
    13 //    and/or other materials provided with the distribution.
    14 //  * Neither the name of AlliedModders LLC nor the names of its contributors
    15 //    may be used to endorse or promote products derived from this software
    16 //    without specific prior written permission.
    17 //
    18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
    19 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
    20 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
    21 // ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
    22 // LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
    23 // CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
    24 // SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
    25 // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
    26 // CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
    27 // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
    28 // POSSIBILITY OF SUCH DAMAGE.
    29 
    30 #include <am-linkedlist.h>
    31 #include <assert.h>
    32 #include "runner.h"
    33 
    34 using namespace ke;
    35 
    36 static size_t sCtors = 0;
    37 static size_t sCopyCtors = 0;
    38 static size_t sMovingCtors = 0;
    39 static size_t sMovedDtors = 0;
    40 static size_t sDtors = 0;
    41 
    42 namespace {
    43 
    44 class BasicThing
    45 {
    46  public:
    47   BasicThing()
    48   {
    49     sCtors++;
    50   }
    51   BasicThing(const BasicThing &other)
    52   {
    53     sCopyCtors++;
    54   }
    55   ~BasicThing()
    56   {
    57     sDtors++;
    58   }
    59 };
    60 
    61 class MovingThing
    62 {
    63  public:
    64   MovingThing()
    65    : moved_(false)
    66   {
    67     sCtors++;
    68   }
    69   MovingThing(Moveable<MovingThing> other)
    70   {
    71     assert(!other->moved_);
    72     sMovingCtors++;
    73     moved_ = false;
    74     other->moved_ = true;
    75   }
    76   ~MovingThing()
    77   {
    78     sDtors++;
    79     if (moved_)
    80       sMovedDtors++;
    81   }
    82 
    83  private:
    84   MovingThing(const MovingThing &other) KE_DELETE;
    85   bool moved_;
    86 };
    87 
    88 } // anonymous namespace
    89 
    90 class TestLinkedList : public Test
    91 {
    92  public:
    93   TestLinkedList()
    94    : Test("LinkedList")
    95   {
    96   }
    97 
    98   bool testInts()
    99   {
   100     LinkedList<int> list;
   101 
   102     if (!check(list.empty(), "list should be empty"))
   103       return false;
   104     if (!check(list.begin() == list.end(), "list iterator should be empty"))
   105       return false;
   106 
   107     list.append(5);
   108     list.append(7);
   109     list.append(9);
   110     if (!check(!list.empty(), "list should not be empty"))
   111       return false;
   112     if (!check(list.length() == 3, "list size should be 3"))
   113       return false;
   114 
   115     LinkedList<int>::iterator iter = list.begin();
   116     if (!check(iter != list.end(), "list iterator should not have ended"))
   117       return false;
   118     if (!check(*iter == 5, "item 1 should be 5"))
   119       return false;
   120     iter++;
   121     if (!check(iter != list.end(), "list iterator should not have ended"))
   122       return false;
   123     if (!check(*iter == 7, "item 2 should be 7"))
   124       return false;
   125     iter++;
   126     if (!check(iter != list.end(), "list iterator should not have ended"))
   127       return false;
   128     if (!check(*iter == 9, "item 3 should be 9"))
   129       return false;
   130     iter++;
   131     if (!check(iter == list.end(), "list iterator should have ended"))
   132       return false;
   133 
   134     iter = list.begin();
   135     iter++;
   136     iter++;
   137     iter = list.erase(iter);
   138     if (!check(iter == list.end(), "erasure should have returned ended iterator"))
   139       return false;
   140     list.append(11);
   141 
   142     iter = list.begin();
   143     iter = list.erase(iter);
   144     if (!check(iter != list.end(), "erasure should not have ended iterator"))
   145       return false;
   146     if (!check(*iter == 7, "erasure should have returned item 2"))
   147       return false;
   148     iter++;
   149     if (!check(*iter == 11, "second item should be 11"))
   150       return false;
   151     if (!check(list.length() == 2, "list should have two items"))
   152       return false;
   153 
   154     list.prepend(1);
   155     iter = list.begin();
   156     if (!check(*iter == 1, "new item should have been prepended"))
   157       return false;
   158     iter++;
   159     if (!check(*iter == 7, "second item should be 7"))
   160       return false;
   161     if (!check(list.length() == 3, "list should have three items"))
   162       return false;
   163 
   164     list.clear();
   165     if (!check(list.empty(), "cleared list should be empty"))
   166       return false;
   167     if (!check(list.begin() == list.end(), "cleared list iterator should be empty"))
   168       return false;
   169 
   170     list.append(1);
   171     list.append(2);
   172     list.append(3);
   173     list.append(4);
   174     list.append(5);
   175     list.append(5);
   176     iter = list.find(3);
   177     if (!check(iter != list.end(), "should have found a 3"))
   178       return false;
   179     if (!check(*iter == 3, "should have found 3"))
   180       return false;
   181     list.remove(3);
   182     iter = list.find(3);
   183     if (!check(iter == list.end(), "should not have found a 3"))
   184       return false;
   185     iter = list.find(5);
   186     if (!check(iter != list.end(), "should have found a 5"))
   187       return false;
   188     if (!check(*iter == 5, "should have found 5"))
   189       return false;
   190     list.remove(5);
   191     iter = list.find(5);
   192     if (!check(iter != list.end(), "should have found a second 5"))
   193       return false;
   194     if (!check(*iter == 5, "should have found a second 5"))
   195       return false;
   196     list.remove(5);
   197     list.remove(5);
   198     iter = list.find(5);
   199     if (!check(iter == list.end(), "should not have found a third 5"))
   200       return false;
   201 
   202     return true;
   203   }
   204 
   205   bool testDestructors()
   206   {
   207     {
   208       LinkedList<BasicThing> list;
   209       list.append(BasicThing());
   210       list.append(BasicThing());
   211       list.append(BasicThing());
   212     }
   213     if (!check(sCtors == 3, "should get 3 normal constructors"))
   214       return false;
   215     if (!check(sCopyCtors == 3, "should get 3 copy constructors"))
   216       return false;
   217     if (!check(sDtors == 6, "should get 6 destructors"))
   218       return false;
   219 
   220     sCtors = 0;
   221     sCopyCtors = 0;
   222     sDtors = 0;
   223     return true;
   224   }
   225 
   226   bool testMoving()
   227   {
   228     {
   229       LinkedList<MovingThing> list;
   230       MovingThing a, b, c;
   231       list.append(Moveable<MovingThing>(a));
   232       list.append(Moveable<MovingThing>(b));
   233       list.append(Moveable<MovingThing>(c));
   234     }
   235 
   236     if (!check(sCtors == 3, "should get 3 normal constructors"))
   237       return false;
   238     if (!check(sMovingCtors == 3, "sould get 3 move constructors"))
   239       return false;
   240     if (!check(sMovedDtors == 3, "sould get 3 moved destructors"))
   241       return false;
   242     if (!check(sDtors == 6, "should get 6 destructors"))
   243       return false;
   244 
   245     sCtors = 0;
   246     sMovingCtors = 0;
   247     sMovedDtors = 0;
   248     sDtors = 0;
   249     return true;
   250   }
   251 
   252   bool testFallibleMalloc()
   253   {
   254     LinkedList<int, FallibleMalloc> list;
   255     list.append(5);
   256     list.append(6);
   257     list.setOutOfMemory(true);
   258     if (!check(!list.append(7), "list handled out-of-memory"))
   259       return false;
   260     list.setOutOfMemory(false);
   261     if (!check(list.append(8), "list recovered out-of-memory"))
   262       return false;
   263     if (!check(list.ooms() == 1, "list received 1 oom"))
   264       return false;
   265     return true;
   266   }
   267 
   268   bool Run() KE_OVERRIDE
   269   {
   270     if (!testInts())
   271       return false;
   272     if (!testDestructors())
   273       return false;
   274     if (!testMoving())
   275       return false;
   276     if (!testFallibleMalloc())
   277       return false;
   278     return true;
   279   }
   280 } sTestLinkedList;
   281