[clangd] Allow consuming limited number of items

This patch modifies `consume` function to allow retrieval of limited
number of symbols. This is the "cheap" implementation of top-level
limiting iterator. In the future we would like to have a complete limit
iterator implementation to insert it into the query subtrees, but in the
meantime this version would be enough for a fully-functional
proof-of-concept Dex implementation.

Reviewers: ioeric, ilya-biryukov

Reviewed by: ioeric

Differential Revision: https://reviews.llvm.org/D50500

llvm-svn: 339426
diff --git a/clang-tools-extra/unittests/clangd/DexIndexTests.cpp b/clang-tools-extra/unittests/clangd/DexIndexTests.cpp
index 906d62a..d5db97c 100644
--- a/clang-tools-extra/unittests/clangd/DexIndexTests.cpp
+++ b/clang-tools-extra/unittests/clangd/DexIndexTests.cpp
@@ -240,6 +240,27 @@
             "(& (& [1, 3, 5, 8, 9] [1, 5, 7, 9]) (| [0, 5] [0, 1, 5] []))");
 }
 
+TEST(DexIndexIterators, Limit) {
+  const PostingList L0 = {4, 7, 8, 20, 42, 100};
+  const PostingList L1 = {1, 3, 5, 8, 9};
+  const PostingList L2 = {1, 5, 7, 9};
+  const PostingList L3 = {0, 5};
+  const PostingList L4 = {0, 1, 5};
+  const PostingList L5;
+
+  auto DocIterator = create(L0);
+  EXPECT_THAT(consume(*DocIterator, 42), ElementsAre(4, 7, 8, 20, 42, 100));
+
+  DocIterator = create(L0);
+  EXPECT_THAT(consume(*DocIterator), ElementsAre(4, 7, 8, 20, 42, 100));
+
+  DocIterator = create(L0);
+  EXPECT_THAT(consume(*DocIterator, 3), ElementsAre(4, 7, 8));
+
+  DocIterator = create(L0);
+  EXPECT_THAT(consume(*DocIterator, 0), ElementsAre());
+}
+
 testing::Matcher<std::vector<Token>>
 trigramsAre(std::initializer_list<std::string> Trigrams) {
   std::vector<Token> Tokens;