tests/test-vector.cpp
author David Anderson <dvander@alliedmods.net>
Fri Sep 06 13:54:31 2013 -0700 (2013-09-06)
changeset 18 9b30f80f6e49
parent 17 563aea5eba06
permissions -rw-r--r--
Eliminate Newborn/NoAddRef (bug 5907, r=ds).
     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-vector.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 TestVector : public Test
    91 {
    92  public:
    93   TestVector()
    94    : Test("Vector")
    95   {
    96   }
    97 
    98   bool testInts()
    99   {
   100     Vector<int> vector;
   101 
   102     if (!check(vector.empty(), "vector should be empty"))
   103       return false;
   104     if (!check(vector.length() == 0, "vector length should be 0"))
   105       return false;
   106 
   107     vector.append(1);
   108     vector.append(2);
   109     vector.append(3);
   110     vector.append(4);
   111     vector.append(5);
   112     for (int i = 0; i < 5; i++) {
   113       if (!check(vector[i] == i + 1, "vector[%d] is %d", i, i))
   114         return false;
   115     }
   116     if (!check(vector.length() == 5, "vector length is 5"))
   117       return false;
   118     if (!check(!vector.empty(), "vector is not empty"))
   119       return false;
   120 
   121     if (!check(vector.popCopy() == 5, "popped 5"))
   122       return false;
   123     if (!check(vector.popCopy() == 4, "popped 4"))
   124       return false;
   125     if (!check(vector.length() == 3, "vector length is 3"))
   126       return false;
   127 
   128     vector.insert(0, 88);
   129     vector.insert(0, 99);
   130     vector.insert(4, 111);
   131     if (!check(vector[0] == 99, "[0] should be 99"))
   132       return false;
   133     if (!check(vector[1] == 88, "[1] should be 88"))
   134       return false;
   135     if (!check(vector[2] == 1, "[2] should be 1"))
   136       return false;
   137     if (!check(vector[3] == 2, "[3] should be 2"))
   138       return false;
   139     if (!check(vector[4] == 111, "[4] should be 111"))
   140       return false;
   141     if (!check(vector[5] == 3, "[5] should be 3"))
   142       return false;
   143     if (!check(vector.length() == 6, "length should be 6"))
   144       return false;
   145 
   146     vector.remove(5);
   147     if (!check(vector[4] == 111, "[4] should still be 111"))
   148       return false;
   149     vector.remove(0);
   150     if (!check(vector[0] == 88, "[0] should be 88"))
   151       return false;
   152     if (!check(vector[3] == 111, "[3] should be 111"))
   153       return false;
   154     if (!check(vector.length() == 4, "length should be 4"))
   155       return false;
   156 
   157     while (!vector.empty())
   158       vector.pop();
   159 
   160     if (!check(vector.empty(), "vector should be empty"))
   161       return false;
   162     if (!check(vector.ensure(128), "vector size ensured"))
   163       return false;
   164     if (!check(vector.empty(), "vector should be empty"))
   165       return false;
   166 
   167     for (int i = 0; i < 128; i++)
   168       vector.append(i);
   169     if (!check(vector.length() == 128, "vector length should be 128"))
   170       return false;
   171 
   172     vector.clear();
   173     if (!check(vector.empty(), "vector should be empty"))
   174       return false;
   175 
   176     return true;
   177   }
   178 
   179   bool testDestructors()
   180   {
   181     {
   182       Vector<BasicThing> vector;
   183       vector.append(BasicThing());
   184       vector.append(BasicThing());
   185       vector.append(BasicThing());
   186     }
   187     if (!check(sCtors == 3, "should get 3 normal constructors"))
   188       return false;
   189     if (!check(sCopyCtors == 3, "should get 3 copy constructors"))
   190       return false;
   191     if (!check(sDtors == 6, "should get 6 destructors"))
   192       return false;
   193 
   194     sCtors = 0;
   195     sCopyCtors = 0;
   196     sDtors = 0;
   197     return true;
   198   }
   199 
   200   bool testMoving()
   201   {
   202     {
   203       Vector<MovingThing> vector;
   204       MovingThing a, b, c;
   205       vector.append(Moveable<MovingThing>(a));
   206       vector.append(Moveable<MovingThing>(b));
   207       vector.append(Moveable<MovingThing>(c));
   208     }
   209 
   210     if (!check(sCtors == 3, "should get 3 normal constructors"))
   211       return false;
   212     if (!check(sMovingCtors == 3, "sould get 3 move constructors"))
   213       return false;
   214     if (!check(sMovedDtors == 3, "sould get 3 moved destructors"))
   215       return false;
   216     if (!check(sDtors == 6, "should get 6 destructors"))
   217       return false;
   218 
   219     sCtors = 0;
   220     sMovingCtors = 0;
   221     sMovedDtors = 0;
   222     sDtors = 0;
   223 
   224     Vector<int> v1;
   225     v1.append(10);
   226     Vector<int> v2(Move(v1));
   227     if (!check(v2.length() == 1, "should have moved vector"))
   228       return false;
   229     if (!check(v1.length() == 0, "should have gutted vector"))
   230       return false;
   231 
   232     return true;
   233   }
   234 
   235   bool testFallibleMalloc()
   236   {
   237     Vector<int, FallibleMalloc> vector;
   238     vector.setOutOfMemory(true);
   239     if (!check(!vector.append(7), "vector handled out-of-memory"))
   240       return false;
   241     vector.setOutOfMemory(false);
   242     if (!check(vector.append(8), "vector recovered out-of-memory"))
   243       return false;
   244     if (!check(vector.ooms() == 1, "vector received 1 oom"))
   245       return false;
   246     return true;
   247   }
   248 
   249   bool Run() KE_OVERRIDE
   250   {
   251     if (!testInts())
   252       return false;
   253     if (!testDestructors())
   254       return false;
   255     if (!testMoving())
   256       return false;
   257     if (!testFallibleMalloc())
   258       return false;
   259     return true;
   260   }
   261 } sTestVector;
   262