Fix Range when starting at zero (#1073)

The existing behavior results in the `0` value being added twice. Since
`lo` is always added to `dst`, we never want to explicitly add `0` if
`lo` is equal to `0`.
This commit is contained in:
Scott K Logan 2020-11-26 03:12:45 -08:00 committed by GitHub
parent 7fa6f1f91a
commit 17a6b21ee1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 7 additions and 1 deletions

View File

@ -87,7 +87,7 @@ void AddRange(std::vector<T>* dst, T lo, T hi, int mult) {
}
// Treat 0 as a special case (see discussion on #762).
if (lo <= 0 && hi >= 0) {
if (lo < 0 && hi >= 0) {
dst->push_back(0);
}

View File

@ -90,6 +90,12 @@ TEST(AddRangeTest, ZeroOnlyRange) {
EXPECT_THAT(dst, testing::ElementsAre(0));
}
TEST(AddRangeTest, ZeroStartingRange) {
std::vector<int> dst;
AddRange(&dst, 0, 2, 2);
EXPECT_THAT(dst, testing::ElementsAre(0, 1, 2));
}
TEST(AddRangeTest, NegativeRange64) {
std::vector<int64_t> dst;
AddRange<int64_t>(&dst, -4, 4, 2);