summaryrefslogtreecommitdiff
path: root/testing/range_set.cpp
diff options
context:
space:
mode:
authorArtem Strygin <art-snake@yandex-team.ru>2017-09-28 18:46:03 +0300
committerChromium commit bot <commit-bot@chromium.org>2017-09-28 15:59:08 +0000
commit0e60b9ef2b79de52ef62101abae2af7292e879b7 (patch)
tree9f71519f2a364ee6702219a44de7fdb1def18cc7 /testing/range_set.cpp
parent1ca7173cd85adcd58766fc89f95c3dc163efa17a (diff)
downloadpdfium-0e60b9ef2b79de52ef62101abae2af7292e879b7.tar.xz
Implement FakeFileAccess.
Update embedder tests to simulate unavailable data and download requests. Change-Id: I634fa89d2a0c859243e849752936da87568909f4 Reviewed-on: https://pdfium-review.googlesource.com/11890 Commit-Queue: Art Snake <art-snake@yandex-team.ru> Reviewed-by: dsinclair <dsinclair@chromium.org>
Diffstat (limited to 'testing/range_set.cpp')
-rw-r--r--testing/range_set.cpp74
1 files changed, 74 insertions, 0 deletions
diff --git a/testing/range_set.cpp b/testing/range_set.cpp
new file mode 100644
index 0000000000..2fc540f17c
--- /dev/null
+++ b/testing/range_set.cpp
@@ -0,0 +1,74 @@
+// Copyright 2017 PDFium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "testing/range_set.h"
+
+#include <algorithm>
+
+#include "core/fxcrt/fx_system.h"
+
+RangeSet::RangeSet() {}
+RangeSet::~RangeSet() {}
+
+bool RangeSet::Contains(const Range& range) const {
+ if (IsEmptyRange(range))
+ return false;
+
+ const Range fixed_range = FixDirection(range);
+ auto it = ranges().upper_bound(fixed_range);
+
+ if (it == ranges().begin())
+ return false; // No ranges includes range.first.
+
+ --it; // Now it starts equal or before range.first.
+ return it->second >= fixed_range.second;
+}
+
+void RangeSet::Union(const Range& range) {
+ if (IsEmptyRange(range))
+ return;
+
+ Range fixed_range = FixDirection(range);
+ if (IsEmpty()) {
+ ranges_.insert(fixed_range);
+ return;
+ }
+
+ auto start = ranges_.upper_bound(fixed_range);
+ if (start != ranges_.begin())
+ --start; // start now points to the key equal or lower than offset.
+
+ if (start->second < fixed_range.first)
+ ++start; // start element is entirely before current range, skip it.
+
+ auto end = ranges_.upper_bound(Range(fixed_range.second, fixed_range.second));
+
+ if (start == end) { // No ranges to merge.
+ ranges_.insert(fixed_range);
+ return;
+ }
+
+ --end;
+
+ const int new_start = std::min<size_t>(start->first, fixed_range.first);
+ const int new_end = std::max(end->second, fixed_range.second);
+
+ ranges_.erase(start, ++end);
+ ranges_.insert(Range(new_start, new_end));
+}
+
+void RangeSet::Union(const RangeSet& range_set) {
+ ASSERT(&range_set != this);
+ for (const auto& it : range_set.ranges())
+ Union(it);
+}
+
+RangeSet::Range RangeSet::FixDirection(const Range& range) const {
+ return range.first <= range.second ? range
+ : Range(range.second + 1, range.first + 1);
+}
+
+bool RangeSet::IsEmptyRange(const Range& range) const {
+ return range.first == range.second;
+}