// Copyright 2014, VIXL authors
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
//
//   * Redistributions of source code must retain the above copyright notice,
//     this list of conditions and the following disclaimer.
//   * 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.
//   * Neither the name of ARM Limited nor the names of its contributors may be
//     used to endorse or promote products derived from this software without
//     specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS 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 OWNER 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.

#include "invalset-vixl.h"
#include "test-runner.h"

namespace vixl {

// This file contains tests for the `InvalSet` and `InvalSetIterator` classes.

#define TEST(name) TEST_(INVALSET_##name)

typedef ptrdiff_t KeyType;
typedef ptrdiff_t ValType;

// We test with an object for which the key and the value are distinct.
class Obj {
 public:
  Obj() {}
  Obj(KeyType key, ValType val) : key_(key), val_(val) {}
  KeyType key_;
  ValType val_;

  bool operator==(const Obj& other) const {
    return (key_ == other.key_) && (val_ == other.val_);
  }
  bool operator<(const Obj& other) const {
    return (key_ < other.key_) || ((key_ == other.key_) && (val_ < other.val_));
  }
  bool operator<=(const Obj& other) const {
    return (key_ <= other.key_) ||
           ((key_ == other.key_) && (val_ <= other.val_));
  }
  bool operator>(const Obj& other) const {
    return (key_ > other.key_) || ((key_ == other.key_) && (val_ > other.val_));
  }
};

static const unsigned kNPreallocatedElements = 8;
static const KeyType kInvalidKey = PTRDIFF_MAX;
static const size_t kReclaimFrom = 1000;
static const unsigned kReclaimFactor = 10;

typedef InvalSet<Obj,
                 kNPreallocatedElements,
                 KeyType,
                 kInvalidKey,
                 kReclaimFrom,
                 kReclaimFactor>
    TestSet;

template <>
inline KeyType InvalSet<Obj,
                        kNPreallocatedElements,
                        KeyType,
                        kInvalidKey,
                        kReclaimFrom,
                        kReclaimFactor>::GetKey(const Obj& obj) {
  return obj.key_;
}
template <>
inline void InvalSet<Obj,
                     kNPreallocatedElements,
                     KeyType,
                     kInvalidKey,
                     kReclaimFrom,
                     kReclaimFactor>::SetKey(Obj* obj, KeyType key) {
  obj->key_ = key;
}


TEST(basic_test) {
  TestSet set;
  VIXL_CHECK(set.empty() && (set.size() == 0));

  for (unsigned i = 0; i < kNPreallocatedElements; i++) {
    set.insert(Obj(i, i));
  }
  VIXL_CHECK(set.size() == kNPreallocatedElements);

  set.insert(Obj(-123, 456));
  set.insert(Obj(2718, 2871828));
  VIXL_CHECK(set.size() == kNPreallocatedElements + 2);
  VIXL_CHECK(set.GetMinElement() == Obj(-123, 456));

  set.erase(Obj(-123, 456));
  VIXL_CHECK(set.GetMinElementKey() == 0);

  set.clear();
  VIXL_CHECK(set.empty() && (set.size() == 0));
}


TEST(valid_element) {
  VIXL_CHECK(TestSet::IsValid(Obj(0, 0)));
  VIXL_CHECK(TestSet::IsValid(Obj(-1, 0)));
  VIXL_CHECK(TestSet::IsValid(Obj(kInvalidKey - 1, 0)));
  VIXL_CHECK(!TestSet::IsValid(Obj(kInvalidKey, 0)));
}


TEST(insert) {
  TestSet set;
  VIXL_CHECK(set.empty() && (set.size() == 0));

  for (unsigned i = 0; i < kNPreallocatedElements; i++) {
    set.insert(Obj(i, i));
  }
  VIXL_CHECK(set.size() == kNPreallocatedElements);
  set.insert(Obj(-123, 1));
  VIXL_CHECK(set.size() == kNPreallocatedElements + 1);
  set.insert(Obj(-123, 2));
  set.insert(Obj(-123, 3));
  VIXL_CHECK(set.size() == kNPreallocatedElements + 3);

  set.clear();
  VIXL_CHECK(set.empty() && (set.size() == 0));
}


TEST(erase) {
  TestSet set;
  VIXL_CHECK(set.empty() && (set.size() == 0));

  // Test with only preallocated elements in the set.
  VIXL_STATIC_ASSERT(kNPreallocatedElements >= 2);
  set.insert(Obj(2718, 0));
  set.erase(Obj(2718, 0));
  VIXL_CHECK(set.empty() && (set.size() == 0));
  set.insert(Obj(2718, 0));
  VIXL_CHECK(set.size() == 1);
  set.insert(Obj(2718, 1));
  VIXL_CHECK(set.size() == 2);
  set.erase(Obj(2718, 0));
  VIXL_CHECK(set.size() == 1);

  // Test with more elements.
  for (unsigned i = 0; i < 100 * kNPreallocatedElements; i++) {
    set.insert(Obj(i * i, i % 30));
    set.insert(Obj(i, -1));
  }
  size_t max_size = set.size();
  set.erase(Obj(100, -1));
  VIXL_CHECK(set.size() == max_size - 1);
  for (size_t i = 2; i <= max_size; i++) {
    set.erase(set.GetMinElement());
    VIXL_CHECK(set.size() == max_size - i);
  }

  VIXL_CHECK(set.empty() && (set.size() == 0));
}


TEST(min) {
  TestSet set;
  VIXL_CHECK(set.empty() && (set.size() == 0));

  // Test with only preallocated elements in the set.
  VIXL_STATIC_ASSERT(kNPreallocatedElements >= 4);
  set.insert(Obj(-1, -1));
  set.insert(Obj(-1, 0));
  set.insert(Obj(0, 0));
  set.insert(Obj(1, 0));
  VIXL_CHECK(set.GetMinElement() == Obj(-1, -1));
  VIXL_CHECK(set.GetMinElementKey() == -1);
  VIXL_CHECK(set.GetMinElement().key_ == set.GetMinElementKey());

  // Test with more elements.
  set.clear();
  int max_index = 100 * kNPreallocatedElements;
  for (int i = 0; i <= max_index; i++) {
    // Insert elements out of order.
    int sign = ((i % 2) == 0) ? -1 : 1;
    set.insert(Obj(sign * i, i));
  }
  VIXL_CHECK(set.GetMinElement() == Obj(-max_index, max_index));
  VIXL_CHECK(set.GetMinElement().key_ == set.GetMinElementKey());

  set.erase(Obj(0, 0));
  VIXL_CHECK(set.GetMinElement() == Obj(-max_index, max_index));
  set.erase(set.GetMinElement());
  VIXL_CHECK(set.GetMinElement() == Obj(-(max_index - 2), max_index - 2));

  set.clear();
  VIXL_CHECK(set.empty() && (set.size() == 0));
}


TEST(iterator) {
  TestSet set;
  VIXL_CHECK(set.empty() && (set.size() == 0));

  // Ensure that set.begin() == set.end() for the empty set.
  VIXL_CHECK(set.begin() == set.end());

  // Test with only preallocated elements in the set.
  size_t expected_total = 0;
  for (unsigned i = 0; i < kNPreallocatedElements; i++) {
    set.insert(Obj(i, i));
    expected_total += i;
  }

  size_t size = 0;
  size_t total = 0;
  for (InvalSetIterator<TestSet> it = set.begin(); it != set.end(); ++it) {
    total += it->val_;
    size++;
  }
  VIXL_CHECK(size == set.size());
  VIXL_CHECK(total == expected_total);

  // Test with more elements.
  for (unsigned i = kNPreallocatedElements; i < 4 * kNPreallocatedElements;
       i++) {
    set.insert(Obj(i, i));
    expected_total += i;
  }

  size = 0;
  total = 0;
  for (InvalSetIterator<TestSet> it = set.begin(); it != set.end(); ++it) {
    total += it->val_;
    size++;
  }
  VIXL_CHECK(size == set.size());
  VIXL_CHECK(total == expected_total);

  // Test after elements have been deleted.
  // - Select an interesting list of elements to erase.
  std::vector<Obj> to_erase;
  unsigned step = 0;
  for (unsigned i = 0; i < set.size(); i += step, step++) {
    to_erase.push_back(Obj(i, i));
  }
  to_erase.push_back(Obj(set.size() - 1, set.size() - 1));  // The last element.
  to_erase.push_back(Obj(set.size(), set.size()));          // Not in the set.

  // - Erase one at a time, retesting after each one.
  while (!to_erase.empty()) {
    size_t erased = set.erase(to_erase.back());
    if (erased > 0) {
      VIXL_CHECK(erased == 1);
      expected_total -= to_erase.back().val_;
    } else {
    }
    to_erase.pop_back();

    size = 0;
    total = 0;
    for (InvalSetIterator<TestSet> it = set.begin(); it != set.end(); ++it) {
      total += it->val_;
      size++;
    }
    VIXL_CHECK(size == set.size());
    VIXL_CHECK(total == expected_total);
  }
}


#if __cplusplus >= 201103L
TEST(iterator_cxx11) {
  TestSet set;
  VIXL_CHECK(set.empty() && (set.size() == 0));

  // Test with only preallocated elements in the set.
  size_t expected_total = 0;
  for (unsigned i = 0; i < kNPreallocatedElements; i++) {
    set.insert(Obj(i, i));
    expected_total += i;
  }

  size_t size = 0;
  size_t total = 0;
  for (auto object : set) {
    total += object.val_;
    size++;
  }
  VIXL_CHECK(size == set.size());
  VIXL_CHECK(total == expected_total);

  // Test with more elements.
  for (unsigned i = kNPreallocatedElements; i < 4 * kNPreallocatedElements;
       i++) {
    set.insert(Obj(i, i));
    expected_total += i;
  }

  size = 0;
  total = 0;
  for (auto object : set) {
    total += object.val_;
    size++;
  }
  VIXL_CHECK(size == set.size());
  VIXL_CHECK(total == expected_total);

  // Test after elements have been deleted.
  // - Select an interesting list of elements to erase.
  std::vector<Obj> to_erase;
  unsigned step = 0;
  for (unsigned i = 0; i < set.size(); i += step, step++) {
    to_erase.push_back(Obj(i, i));
  }
  to_erase.push_back(Obj(set.size() - 1, set.size() - 1));  // The last element.
  to_erase.push_back(Obj(set.size(), set.size()));          // Not in the set.

  // - Erase one at a time, retesting after each one.
  while (!to_erase.empty()) {
    size_t erased = set.erase(to_erase.back());
    if (erased > 0) {
      VIXL_CHECK(erased == 1);
      expected_total -= to_erase.back().val_;
    } else {
    }
    to_erase.pop_back();

    size = 0;
    total = 0;
    for (auto object : set) {
      total += object.val_;
      size++;
    }
    VIXL_CHECK(size == set.size());
    VIXL_CHECK(total == expected_total);
  }
}
#endif


TEST(stl_forward_iterator) {
  {
    TestSet::iterator default_it;           // Default-constructible.
    TestSet::iterator copy_it(default_it);  // Copy-constructible.
    copy_it = default_it;                   // Copy-assignable.
  }                                         // Destructible.

  TestSet set1;
  VIXL_CHECK(set1.empty() && (set1.size() == 0));

  TestSet set2;
  VIXL_CHECK(set2.empty() && (set2.size() == 0));

  // Test with only preallocated elements in the set.
  for (unsigned i = 0; i < kNPreallocatedElements; i++) {
    set1.insert(Obj(i, 1));
    set2.insert(Obj(i, 2));
  }

  TestSet::iterator it1_a = set1.begin();
  TestSet::iterator it1_b = set1.begin();

  // Incrementable (whilst valid).
  it1_a++;
  ++it1_b;

  // Testable for equivalence.
  VIXL_CHECK(it1_a == it1_b);
  VIXL_CHECK(set1.begin() != set1.end());
  VIXL_CHECK(set2.begin() != set2.end());
  VIXL_CHECK(set1.begin() != set2.begin());
  VIXL_CHECK(set1.end() != set2.end());

  // Dereferencable.
  VIXL_CHECK((*it1_a++).key_ == 1);
  VIXL_CHECK(((*it1_a).key_ == 2) && ((*it1_a).val_ == 1));
  *it1_b = Obj(42, 1);
  VIXL_CHECK((it1_b->key_ == 42) && (it1_b->val_ == 1));

#if __cplusplus >= 201103L
  // Swappable.
  std::swap(it1_a, it1_b);
  VIXL_CHECK(it1_a->key_ == 42);
  VIXL_CHECK(it1_b->key_ == 2);
#endif
}

TEST(move) {
  TestSet set1;

  set1.insert(Obj(-123, 456));
  set1.insert(Obj(2718, 2871828));

  TestSet set2(std::move(set1));
  VIXL_CHECK(set1.empty());
  VIXL_CHECK(set2.size() == 2);
  VIXL_CHECK(set2.GetMinElement() == Obj(-123, 456));

  // Test with more elements.
  for (unsigned i = 0; i < 4 * kNPreallocatedElements; i++) {
    set2.insert(Obj(i, -1));
  }

  TestSet set3(std::move(set2));
  VIXL_CHECK(set2.empty());
  VIXL_CHECK(set3.size() == 2 + 4 * kNPreallocatedElements);
  VIXL_CHECK(set3.GetMinElement() == Obj(-123, 456));
}


}  // namespace vixl
