Skip to content

Commit b826b3c

Browse files
authored
[AST] Fix bug in IndexSubset::findPrevious. (#29675)
Fix incorrect loop decrement in `IndexSubset::findPrevious` Add unit tests.
1 parent 2b85835 commit b826b3c

File tree

2 files changed

+25
-2
lines changed

2 files changed

+25
-2
lines changed

lib/AST/IndexSubset.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ int IndexSubset::findPrevious(int endIndex) const {
125125
offset = (int)indexAndOffset.second - 1;
126126
}
127127
for (; bitWordIndex >= 0; --bitWordIndex, offset = numBitsPerBitWord - 1) {
128-
for (; offset < (int)numBitsPerBitWord; --offset) {
128+
for (; offset >= 0; --offset) {
129129
auto index = bitWordIndex * (int)numBitsPerBitWord + offset;
130130
auto bitWord = getBitWord(bitWordIndex);
131131
if (!bitWord)

unittests/AST/IndexSubsetTests.cpp

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
//
33
// This source file is part of the Swift.org open source project
44
//
5-
// Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors
5+
// Copyright (c) 2019 - 2020 Apple Inc. and the Swift project authors
66
// Licensed under Apache License v2.0 with Runtime Library Exception
77
//
88
// See https://swift.org/LICENSE.txt for license information
@@ -187,3 +187,26 @@ TEST(IndexSubset, Insertion) {
187187
EXPECT_EQ(indices1->adding(3, ctx.Ctx),
188188
IndexSubset::get(ctx.Ctx, 5, {0, 2, 3, 4}));
189189
}
190+
191+
TEST(IndexSubset, FindNext) {
192+
TestContext ctx;
193+
auto *indices1 = IndexSubset::get(ctx.Ctx, 5, {1, 2, 4});
194+
EXPECT_EQ(indices1->findFirst(), 1);
195+
EXPECT_EQ(indices1->findNext(/*startIndex*/ -1), 1);
196+
EXPECT_EQ(indices1->findNext(/*startIndex*/ 0), 1);
197+
EXPECT_EQ(indices1->findNext(/*startIndex*/ 1), 2);
198+
EXPECT_EQ(indices1->findNext(/*startIndex*/ 2), 4);
199+
EXPECT_EQ(indices1->findNext(/*startIndex*/ 3), 4);
200+
}
201+
202+
TEST(IndexSubset, FindPrevious) {
203+
TestContext ctx;
204+
auto *indices1 = IndexSubset::get(ctx.Ctx, 5, {0, 2, 4});
205+
EXPECT_EQ(indices1->findLast(), 4);
206+
EXPECT_EQ(indices1->findPrevious(/*endIndex*/ 5), 4);
207+
EXPECT_EQ(indices1->findPrevious(/*endIndex*/ 4), 2);
208+
EXPECT_EQ(indices1->findPrevious(/*endIndex*/ 3), 2);
209+
EXPECT_EQ(indices1->findPrevious(/*endIndex*/ 2), 0);
210+
EXPECT_EQ(indices1->findPrevious(/*endIndex*/ 1), 0);
211+
EXPECT_EQ(indices1->findPrevious(/*endIndex*/ 0), -1);
212+
}

0 commit comments

Comments
 (0)