tree_test-inl.h 11.3 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
/*
 * Copyright (c) 2014-2015, Siemens AG. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * 1. Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 *
 * 2. Redistributions in binary form must reproduce the above copyright notice,
 * this list of conditions and the following disclaimer in the documentation
 * and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

#ifndef CONTAINERS_CPP_TEST_TREE_TEST_INL_H_
#define CONTAINERS_CPP_TEST_TREE_TEST_INL_H_

#include <utility>
#include <vector>
#include <algorithm>

#include "tree_test.h"

namespace embb {
namespace containers {
namespace test {

template<typename Tree>
TreeTest<Tree>::TreeTest()
42 43 44
    : tree_(NULL),
      bad_key_(static_cast<Key>(-1)),
      bad_value_(static_cast<Value>(-1)) {
45
  CreateUnit("TreeTestSingleThreadInsertDelete").
46 47 48 49
      Pre(&TreeTest::TreeTestInsertDelete_Pre, this).
      Add(&TreeTest::TreeTestInsertDeleteSingleThread_ThreadMethod,
          this, 1, NUM_ITERATIONS).
      Post(&TreeTest::TreeTestInsertDelete_Post, this);
50
  CreateUnit("TreeTestMultiThreadInsertDelete").
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
      Pre(&TreeTest::TreeTestInsertDelete_Pre, this).
      Add(&TreeTest::TreeTestInsertDeleteMultiThread_ThreadMethod, this,
          NUM_TEST_THREADS, NUM_ITERATIONS).
      Post(&TreeTest::TreeTestInsertDelete_Post, this);
  CreateUnit("TreeTestConcurrentGet").
      Pre(&TreeTest::TreeTestConcurrentGet_Pre, this).
      Add(&TreeTest::TreeTestConcurrentGet_WriterMethod, this,
          NUM_TEST_THREADS / 2, NUM_ITERATIONS).
      Add(&TreeTest::TreeTestConcurrentGet_ReaderMethod, this,
          NUM_TEST_THREADS / 2, NUM_ITERATIONS).
      Post(&TreeTest::TreeTestConcurrentGet_Post, this);
  CreateUnit("TreeTestConcurrentGetMinimal").
      Pre(&TreeTest::TreeTestConcurrentGetMinimal_Pre, this).
      Add(&TreeTest::TreeTestConcurrentGet_WriterMethod, this,
          NUM_TEST_THREADS / 2, NUM_ITERATIONS).
      Add(&TreeTest::TreeTestConcurrentGet_ReaderMethod, this,
          NUM_TEST_THREADS / 2, NUM_ITERATIONS).
      Post(&TreeTest::TreeTestConcurrentGet_Post, this);
  CreateUnit("TreeTestBalance").
      Pre(&TreeTest::TreeTestBalance_Pre, this).
      Add(&TreeTest::TreeTestBalance_ThreadMethod, this,
          NUM_TEST_THREADS, 1).
      Post(&TreeTest::TreeTestBalance_Post, this);
74 75 76 77
}

template<typename Tree>
TreeTest<Tree>::Worker::
78 79
Worker(TreePtr tree, int thread_id)
    : tree_(tree), thread_id_(thread_id) {}
80 81 82

template<typename Tree>
void TreeTest<Tree>::Worker::
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
InsertReplaceDelete(int num_elements) {
  PrepareElements(num_elements);
  InsertAll();
  ReplaceHalf();
  DeleteAll();
}

template<typename Tree>
void TreeTest<Tree>::Worker::
PrepareElements(int num_elements) {
  // Fill the "elements_" vector
  elements_.clear();
  for (int i = 0; i < num_elements; ++i) {
    Key   key   = static_cast<Key  >(i * 100 + thread_id_);
    Value value = static_cast<Value>(i * 100 + thread_id_);
    elements_.push_back(std::make_pair(key, value));
99
  }
100
}
101

102 103 104
template<typename Tree>
void TreeTest<Tree>::Worker::
InsertAll() {
105
  // Insert elements into the tree
106 107 108 109 110
  ::std::random_shuffle(elements_.begin(), elements_.end());
  for (ElementIterator it = elements_.begin(); it != elements_.end(); ++it) {
    Value old_value;
    Value bad_value = tree_->GetUndefinedValue();
    bool success = tree_->TryInsert(it->first, it->second, old_value);
111
    PT_ASSERT_MSG(success, "Failed to insert element into the tree.");
112
    PT_ASSERT_EQ_MSG(old_value, bad_value, "A key was already in the tree.");
113 114 115
  }

  // Verify that all inserted elements are available in the tree
116 117
  ::std::random_shuffle(elements_.begin(), elements_.end());
  for (ElementIterator it = elements_.begin(); it != elements_.end(); ++it) {
118
    Value value;
119
    bool success = tree_->Get(it->first, value);
120
    PT_ASSERT_MSG(success, "Failed to get an element from the tree.");
121
    PT_ASSERT_EQ_MSG(it->second, value, "Wrong value retrieved from the tree.");
122
  }
123
}
124

125 126 127
template<typename Tree>
void TreeTest<Tree>::Worker::
ReplaceHalf() {
128
  // Replace some of the elements that were inserted earlier
129 130 131 132 133
  ::std::random_shuffle(elements_.begin(), elements_.end());
  ElementIterator elements_middle = elements_.begin() + elements_.size() / 2;
  for (ElementIterator it = elements_.begin(); it != elements_middle; ++it) {
    Value old_value;
    Value expected = it->second;
134
    it->second *= 13;
135
    bool success = tree_->TryInsert(it->first, it->second, old_value);
136
    PT_ASSERT_MSG(success, "Failed to insert element into the tree.");
137
    PT_ASSERT_EQ_MSG(old_value, expected, "Wrong value replaced in the tree.");
138 139 140
  }

  // Verify again that all elements are in the tree and have correct values
141 142
  ::std::random_shuffle(elements_.begin(), elements_.end());
  for (ElementIterator it = elements_.begin(); it != elements_.end(); ++it) {
143
    Value value;
144
    bool success = tree_->Get(it->first, value);
145
    PT_ASSERT_MSG(success, "Failed to get an element from the tree.");
146
    PT_ASSERT_EQ_MSG(it->second, value, "Wrong value retrieved from the tree.");
147
  }
148
}
149

150 151 152
template<typename Tree>
void TreeTest<Tree>::Worker::
DeleteAll() {
153
  // Delete elements from the tree
154 155 156 157 158
  ::std::random_shuffle(elements_.begin(), elements_.end());
  for (ElementIterator it = elements_.begin(); it != elements_.end(); ++it) {
    Value old_value;
    Value expected = it->second;
    bool success = tree_->TryDelete(it->first, old_value);
159
    PT_ASSERT_MSG(success, "Failed to delete element from the tree.");
160
    PT_ASSERT_EQ_MSG(expected, old_value, "Wrong value deleted from the tree.");
161 162 163 164
  }
}

template<typename Tree>
165 166 167 168 169 170 171 172 173 174 175 176
void TreeTest<Tree>::Worker::
DeleteHalf() {
  // Delete half of the elements from the tree
  ::std::random_shuffle(elements_.begin(), elements_.end());
  ElementIterator elements_middle = elements_.begin() + elements_.size() / 2;
  for (ElementIterator it = elements_.begin(); it != elements_middle; ++it) {
    Value old_value;
    Value expected = it->second;
    bool success = tree_->TryDelete(it->first, old_value);
    PT_ASSERT_MSG(success, "Failed to delete element from the tree.");
    PT_ASSERT_EQ_MSG(expected, old_value, "Wrong value deleted from the tree.");
  }
177 178 179 180
}

template<typename Tree>
void TreeTest<Tree>::
181 182 183
TreeTestInsertDelete_Pre() {
  tree_ = new Tree(TREE_CAPACITY, bad_key_, bad_value_);
}
184

185 186 187 188
template<typename Tree>
void TreeTest<Tree>::
TreeTestInsertDeleteSingleThread_ThreadMethod() {
  Worker worker(tree_, 0);
189

190 191 192
  for (int i = 1; i <= 10; ++i) {
    worker.InsertReplaceDelete(i * (TREE_CAPACITY / 10));
  }
193 194 195 196
}

template<typename Tree>
void TreeTest<Tree>::
197 198 199
TreeTestInsertDeleteMultiThread_ThreadMethod() {
  int thread_id = static_cast<int>(partest::TestSuite::GetCurrentThreadID());
  thread_id %= NUM_TEST_THREADS;
200 201 202 203 204 205

  int num_elements = TREE_CAPACITY / (NUM_TEST_THREADS + 1);
  if (thread_id == 0) {
    num_elements *= 2;
  }

206 207 208 209 210
  Worker worker(tree_, thread_id);

  for (int i = 1; i <= 10; ++i) {
    worker.InsertReplaceDelete(i * (num_elements / 10));
  }
211 212 213 214
}

template<typename Tree>
void TreeTest<Tree>::
215
TreeTestInsertDelete_Post() {
216 217 218 219
  PT_ASSERT_MSG((tree_->IsEmpty()), "The tree must be empty at this point.");
  delete tree_;
}

220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327
template<typename Tree>
void TreeTest<Tree>::
TreeTestConcurrentGet_Pre() {
  tree_ = new Tree(TREE_CAPACITY / 2, bad_key_, bad_value_);

  ElementVector elements;
  for (int i = 0; i < TREE_CAPACITY / 2; ++i) {
    Key   key   = static_cast<Key  >(i);
    Value value = static_cast<Value>(i);
    elements.push_back(std::make_pair(key, value));
  }

  ::std::random_shuffle(elements.begin(), elements.end());
  for (ElementIterator it = elements.begin(); it != elements.end(); ++it) {
    Value old_value;
    Value bad_value = tree_->GetUndefinedValue();
    bool success = tree_->TryInsert(it->first, it->second, old_value);
    PT_ASSERT_MSG(success, "Failed to insert element into the tree.");
    PT_ASSERT_EQ_MSG(old_value, bad_value, "A key was already in the tree.");
  }
}

template<typename Tree>
void TreeTest<Tree>::
TreeTestConcurrentGetMinimal_Pre() {
  tree_ = new Tree(NUM_TEST_THREADS / 2, bad_key_, bad_value_);

  for (int i = 0; i < NUM_TEST_THREADS / 2; ++i) {
    Key   key   = static_cast<Key  >(TREE_CAPACITY / 4 + i);
    Value value = static_cast<Value>(TREE_CAPACITY / 4 + i);
    Value old_value;
    Value bad_value = tree_->GetUndefinedValue();
    bool success = tree_->TryInsert(key, value, old_value);
    PT_ASSERT_MSG(success, "Failed to insert element into the tree.");
    PT_ASSERT_EQ_MSG(old_value, bad_value, "A key was already in the tree.");
  }
}

template<typename Tree>
void TreeTest<Tree>::
TreeTestConcurrentGet_WriterMethod() {
  int idx = static_cast<int>(partest::TestSuite::GetCurrentThreadID());
  idx %= (NUM_TEST_THREADS / 2);

  Key   key   = static_cast<Key  >(TREE_CAPACITY / 4 + idx);
  Value value = static_cast<Value>(TREE_CAPACITY / 4 + idx);

  for (int i = 0; i < 1000; ++i) {
    Value old_value;
    bool success = tree_->TryDelete(key, old_value);
    PT_ASSERT_MSG(success, "Failed to delete element from the tree.");
    PT_ASSERT_EQ_MSG(old_value, value, "Wrong value deleted from the tree.");

    Value bad_value = tree_->GetUndefinedValue();
    success = tree_->TryInsert(key, value, old_value);
    PT_ASSERT_MSG(success, "Failed to insert element into the tree.");
    PT_ASSERT_EQ_MSG(old_value, bad_value, "A key was already in the tree.");
  }
}

template<typename Tree>
void TreeTest<Tree>::
TreeTestConcurrentGet_ReaderMethod() {
  int idx = static_cast<int>(partest::TestSuite::GetCurrentThreadID());
  idx %= (NUM_TEST_THREADS / 2);

  Key key = static_cast<Key>(TREE_CAPACITY / 4 + idx);

  for (int i = 0; i < 1000; ++i) {
    Value value;
    tree_->Get(key, value);
  }
}

template<typename Tree>
void TreeTest<Tree>::
TreeTestConcurrentGet_Post() {
  delete tree_;
}

template<typename Tree>
void TreeTest<Tree>::
TreeTestBalance_Pre() {
  tree_ = new Tree(TREE_CAPACITY, bad_key_, bad_value_);
}

template<typename Tree>
void TreeTest<Tree>::
TreeTestBalance_ThreadMethod() {
  int thread_id = static_cast<int>(partest::TestSuite::GetCurrentThreadID());
  thread_id %= NUM_TEST_THREADS;

  int num_elements = TREE_CAPACITY / NUM_TEST_THREADS;

  Worker worker(tree_, thread_id);

  worker.PrepareElements(num_elements);
  worker.InsertAll();
  worker.DeleteHalf();
}

template<typename Tree>
void TreeTest<Tree>::
TreeTestBalance_Post() {
  PT_ASSERT_MSG((tree_->IsBalanced()), "The tree must balanced at this point.");
  delete tree_;
}

328 329 330 331 332
}  // namespace test
}  // namespace containers
}  // namespace embb

#endif // CONTAINERS_CPP_TEST_TREE_TEST_INL_H_