tests/test-vector.cpp
author David Anderson <danderson@mozilla.com>
Mon Aug 26 23:18:03 2013 -0700 (2013-08-26)
changeset 11 cfe800908837
child 17 563aea5eba06
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-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     while (!vector.empty())
   129       vector.pop();
   130 
   131     if (!check(vector.empty(), "vector should be empty"))
   132       return false;
   133     if (!check(vector.ensure(128), "vector size ensured"))
   134       return false;
   135     if (!check(vector.empty(), "vector should be empty"))
   136       return false;
   137 
   138     for (int i = 0; i < 128; i++)
   139       vector.append(i);
   140     if (!check(vector.length() == 128, "vector length should be 128"))
   141       return false;
   142 
   143     vector.clear();
   144     if (!check(vector.empty(), "vector should be empty"))
   145       return false;
   146 
   147     return true;
   148   }
   149 
   150   bool testDestructors()
   151   {
   152     {
   153       Vector<BasicThing> vector;
   154       vector.append(BasicThing());
   155       vector.append(BasicThing());
   156       vector.append(BasicThing());
   157     }
   158     if (!check(sCtors == 3, "should get 3 normal constructors"))
   159       return false;
   160     if (!check(sCopyCtors == 3, "should get 3 copy constructors"))
   161       return false;
   162     if (!check(sDtors == 6, "should get 6 destructors"))
   163       return false;
   164 
   165     sCtors = 0;
   166     sCopyCtors = 0;
   167     sDtors = 0;
   168     return true;
   169   }
   170 
   171   bool testMoving()
   172   {
   173     {
   174       Vector<MovingThing> vector;
   175       MovingThing a, b, c;
   176       vector.append(Moveable<MovingThing>(a));
   177       vector.append(Moveable<MovingThing>(b));
   178       vector.append(Moveable<MovingThing>(c));
   179     }
   180 
   181     if (!check(sCtors == 3, "should get 3 normal constructors"))
   182       return false;
   183     if (!check(sMovingCtors == 3, "sould get 3 move constructors"))
   184       return false;
   185     if (!check(sMovedDtors == 3, "sould get 3 moved destructors"))
   186       return false;
   187     if (!check(sDtors == 6, "should get 6 destructors"))
   188       return false;
   189 
   190     sCtors = 0;
   191     sMovingCtors = 0;
   192     sMovedDtors = 0;
   193     sDtors = 0;
   194 
   195     Vector<int> v1;
   196     v1.append(10);
   197     Vector<int> v2(Move(v1));
   198     if (!check(v2.length() == 1, "should have moved vector"))
   199       return false;
   200     if (!check(v1.length() == 0, "should have gutted vector"))
   201       return false;
   202 
   203     return true;
   204   }
   205 
   206   bool testFallibleMalloc()
   207   {
   208     Vector<int, FallibleMalloc> vector;
   209     vector.setOutOfMemory(true);
   210     if (!check(!vector.append(7), "vector handled out-of-memory"))
   211       return false;
   212     vector.setOutOfMemory(false);
   213     if (!check(vector.append(8), "vector recovered out-of-memory"))
   214       return false;
   215     if (!check(vector.ooms() == 1, "vector received 1 oom"))
   216       return false;
   217     return true;
   218   }
   219 
   220   bool Run() KE_OVERRIDE
   221   {
   222     if (!testInts())
   223       return false;
   224     if (!testDestructors())
   225       return false;
   226     if (!testMoving())
   227       return false;
   228     if (!testFallibleMalloc())
   229       return false;
   230     return true;
   231   }
   232 } sTestVector;
   233