From 5bba9b0470291643393f67a4178a321a82e93eec Mon Sep 17 00:00:00 2001 From: Arseny Smirnov Date: Fri, 18 Jan 2019 19:39:19 +0300 Subject: SetWithPosition: fixes and tests GitOrigin-RevId: 7b74a05c72261222a4f43b86eecad668ffd40403 --- test/set_with_position.cpp | 167 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 167 insertions(+) create mode 100644 test/set_with_position.cpp (limited to 'test/set_with_position.cpp') diff --git a/test/set_with_position.cpp b/test/set_with_position.cpp new file mode 100644 index 000000000..211cc5b2f --- /dev/null +++ b/test/set_with_position.cpp @@ -0,0 +1,167 @@ +// +// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2019 +// +// Distributed under the Boost Software License, Version 1.0. (See accompanying +// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) +// +#include "td/utils/tests.h" +#include "td/utils/Random.h" + +#include "td/telegram/SetWithPosition.h" + +#include + +using namespace td; + +template class Set = SetWithPosition> +class CheckedSetWithPosition { + public: + void add(int x) { + s_.add(x); + if (checked_.count(x) != 0) { + return; + } + not_checked_.insert(x); + } + void remove(int x) { + s_.remove(x); + checked_.erase(x); + not_checked_.erase(x); + } + bool has_next() { + auto res = !not_checked_.empty(); + //LOG(ERROR) << res; + ASSERT_EQ(res, s_.has_next()); + return res; + } + void reset_position() { + s_.reset_position(); + not_checked_.insert(checked_.begin(), checked_.end()); + checked_ = {}; + } + + T next() { + CHECK(has_next()); + auto next = s_.next(); + //LOG(ERROR) << next; + ASSERT_TRUE(not_checked_.count(next) != 0); + not_checked_.erase(next); + checked_.insert(next); + return next; + } + + void merge(CheckedSetWithPosition &&other) { + for (auto x : other.checked_) { + not_checked_.erase(x); + checked_.insert(x); + } + for (auto x : other.not_checked_) { + if (checked_.count(x) != 0) { + continue; + } + not_checked_.insert(x); + } + s_.merge(std::move(other.s_)); + } + + private: + std::set checked_; + std::set not_checked_; + Set s_; +}; + +template