tree_test-inl.h 11.5 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
      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);
69 70 71 72 73
  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
  ::std::random_shuffle(elements_.begin(), elements_.end());
130 131
  ElementIterator elements_middle = elements_.begin() +
      static_cast<ptrdiff_t>(elements_.size() / 2);
132 133 134
  for (ElementIterator it = elements_.begin(); it != elements_middle; ++it) {
    Value old_value;
    Value expected = it->second;
135
    it->second *= 13;
136
    bool success = tree_->TryInsert(it->first, it->second, old_value);
137
    PT_ASSERT_MSG(success, "Failed to insert element into the tree.");
138
    PT_ASSERT_EQ_MSG(old_value, expected, "Wrong value replaced in the tree.");
139 140 141
  }

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

151 152 153
template<typename Tree>
void TreeTest<Tree>::Worker::
DeleteAll() {
154
  // Delete elements from the tree
155 156 157 158 159
  ::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);
160
    PT_ASSERT_MSG(success, "Failed to delete element from the tree.");
161
    PT_ASSERT_EQ_MSG(expected, old_value, "Wrong value deleted from the tree.");
162 163 164 165
  }
}

template<typename Tree>
166 167 168 169
void TreeTest<Tree>::Worker::
DeleteHalf() {
  // Delete half of the elements from the tree
  ::std::random_shuffle(elements_.begin(), elements_.end());
170 171
  ElementIterator elements_middle = elements_.begin() +
      static_cast<ptrdiff_t>(elements_.size() / 2);
172 173 174 175 176 177 178
  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.");
  }
179 180 181 182
}

template<typename Tree>
void TreeTest<Tree>::
183
TreeTestInsertDelete_Pre() {
184
  embb_internal_thread_index_reset();
185 186
  tree_ = new Tree(TREE_CAPACITY, bad_key_, bad_value_);
}
187

188 189 190 191
template<typename Tree>
void TreeTest<Tree>::
TreeTestInsertDeleteSingleThread_ThreadMethod() {
  Worker worker(tree_, 0);
192

193 194 195
  for (int i = 1; i <= 10; ++i) {
    worker.InsertReplaceDelete(i * (TREE_CAPACITY / 10));
  }
196 197 198 199
}

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

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

209 210 211 212 213
  Worker worker(tree_, thread_id);

  for (int i = 1; i <= 10; ++i) {
    worker.InsertReplaceDelete(i * (num_elements / 10));
  }
214 215 216 217
}

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

223 224 225
template<typename Tree>
void TreeTest<Tree>::
TreeTestConcurrentGet_Pre() {
226
  embb_internal_thread_index_reset();
227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248
  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() {
249
  embb_internal_thread_index_reset();
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
  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() {
308
  embb_internal_thread_index_reset();
309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333
  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_;
}

334 335 336 337 338
}  // namespace test
}  // namespace containers
}  // namespace embb

#endif // CONTAINERS_CPP_TEST_TREE_TEST_INL_H_