/* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil -*- */
/*
 * Copyright (c) 2012-2017 University of California, Los Angeles
 *
 * This file is part of ChronoSync, synchronization library for distributed realtime
 * applications for NDN.
 *
 * ChronoSync is free software: you can redistribute it and/or modify it under the terms
 * of the GNU General Public License as published by the Free Software Foundation, either
 * version 3 of the License, or (at your option) any later version.
 *
 * ChronoSync is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
 * PURPOSE.  See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along with
 * ChronoSync, e.g., in COPYING.md file.  If not, see <http://www.gnu.org/licenses/>.
 */

#include "interest-table.hpp"
#include "boost-test.hpp"
#include "../unit-test-time-fixture.hpp"

#include <unistd.h>

namespace chronosync {
namespace test {

class InterestTableFixture : public ndn::tests::UnitTestTimeFixture
{
public:
  InterestTableFixture()
  {
    uint8_t origin[4] = {0x01, 0x02, 0x03, 0x04};
    Name prefix("/test/prefix");

    Name interestName1;
    digest1 = ndn::util::Sha256::computeDigest(origin, 1);
    interestName1.append(prefix).append(name::Component(digest1));
    interest1 = make_shared<Interest>(interestName1);
    interest1->setInterestLifetime(time::milliseconds(100));

    Name interestName2;
    digest2 = ndn::util::Sha256::computeDigest(origin, 2);
    interestName2.append(prefix).append(name::Component(digest2));
    interest2 = make_shared<Interest>(interestName2);
    interest2->setInterestLifetime(time::milliseconds(100));

    Name interestName3;
    digest3 = ndn::util::Sha256::computeDigest(origin, 3);
    interestName3.append(prefix).append(name::Component(digest3));
    interest3 = make_shared<Interest>(interestName3);
    interest3->setInterestLifetime(time::milliseconds(100));
  }

  void
  insert(InterestTable& table,
         shared_ptr<Interest> interest,
         ndn::ConstBufferPtr digest)
  {
    table.insert(interest, digest);
  }

  shared_ptr<Interest> interest1;
  ndn::ConstBufferPtr digest1;

  shared_ptr<Interest> interest2;
  ndn::ConstBufferPtr digest2;

  shared_ptr<Interest> interest3;
  ndn::ConstBufferPtr digest3;
};

BOOST_FIXTURE_TEST_SUITE(InterestTableTest, InterestTableFixture)

BOOST_AUTO_TEST_CASE(Container)
{
  InterestContainer container;

  container.insert(make_shared<UnsatisfiedInterest>(interest1, digest1));
  container.insert(make_shared<UnsatisfiedInterest>(interest2, digest2));
  container.insert(make_shared<UnsatisfiedInterest>(interest3, digest3));

  BOOST_CHECK_EQUAL(container.size(), 3);
  BOOST_CHECK(container.find(digest3) != container.end());
  BOOST_CHECK(container.find(digest2) != container.end());
  BOOST_CHECK(container.find(digest1) != container.end());
}

BOOST_AUTO_TEST_CASE(Basic)
{
  InterestTable table(io);

  table.insert(interest1, digest1);
  table.insert(interest2, digest2);
  table.insert(interest3, digest3);

  BOOST_CHECK_EQUAL(table.size(), 3);
  InterestTable::const_iterator it = table.begin();
  BOOST_CHECK(it != table.end());
  it++;
  BOOST_CHECK(it != table.end());
  it++;
  BOOST_CHECK(it != table.end());
  it++;
  BOOST_CHECK(it == table.end());

  BOOST_CHECK_EQUAL(table.size(), 3);
  table.erase(digest1);
  BOOST_CHECK_EQUAL(table.size(), 2);
  table.erase(digest2);
  BOOST_CHECK_EQUAL(table.size(), 1);
  ConstUnsatisfiedInterestPtr pendingInterest = *table.begin();
  table.clear();
  BOOST_CHECK_EQUAL(table.size(), 0);
  BOOST_CHECK(*pendingInterest->digest == *digest3);
}

BOOST_AUTO_TEST_CASE(Expire)
{
  InterestTable table(io);

  insert(table, interest1, digest1);

  advanceClocks(ndn::time::milliseconds(10), 10);

  insert(table, interest2, digest2);
  insert(table, interest3, digest3);

  advanceClocks(ndn::time::milliseconds(10), 5);

  insert(table, interest2, digest2);

  advanceClocks(ndn::time::milliseconds(10), 2);
  BOOST_CHECK_EQUAL(table.size(), 2);

  advanceClocks(ndn::time::milliseconds(10), 5);
  BOOST_CHECK_EQUAL(table.size(), 1);

  advanceClocks(ndn::time::milliseconds(10), 15);
  BOOST_CHECK_EQUAL(table.size(), 0);
}

BOOST_AUTO_TEST_SUITE_END()

} // namespace test
} // namespace chronosync
