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