Fix postincrement in truncating and counting iterators

This commit is contained in:
Victor Zverovich 2018-06-07 20:20:36 +02:00
parent 4bc26f0a7b
commit 1b8a7f8fa0
3 changed files with 28 additions and 13 deletions

View File

@ -840,7 +840,11 @@ class counting_iterator {
return *this; return *this;
} }
counting_iterator operator++(int) { return ++*this; } counting_iterator operator++(int) {
auto it = *this;
++*this;
return it;
}
T &operator*() const { return blackhole_; } T &operator*() const { return blackhole_; }
}; };
@ -877,7 +881,11 @@ class truncating_iterator {
return *this; return *this;
} }
truncating_iterator operator++(int) { return ++*this; } truncating_iterator operator++(int) {
auto it = *this;
++*this;
return it;
}
reference operator*() const { return count_ < limit_ ? *out_ : blackhole_; } reference operator*() const { return count_ < limit_ ? *out_ : blackhole_; }
}; };

View File

@ -14,16 +14,11 @@
#include <memory> #include <memory>
#include <stdint.h> #include <stdint.h>
#include "gmock.h"
// Test that the library compiles if None is defined to 0 as done by xlib.h.
#define None 0
#include "fmt/format.h" #include "fmt/format.h"
#include "gmock.h"
#include "util.h"
#include "mock-allocator.h"
#include "gtest-extra.h" #include "gtest-extra.h"
#include "mock-allocator.h"
#include "util.h"
#undef min #undef min
#undef max #undef max
@ -1528,9 +1523,6 @@ TEST(FormatTest, FormatToN) {
EXPECT_EQ(6u, result.size); EXPECT_EQ(6u, result.size);
EXPECT_EQ(buffer + 3, result.out); EXPECT_EQ(buffer + 3, result.out);
EXPECT_EQ("foox", fmt::string_view(buffer, 4)); EXPECT_EQ("foox", fmt::string_view(buffer, 4));
// Workaround for potentially unused macro
static_cast<void>(None);
} }
#if FMT_USE_CONSTEXPR #if FMT_USE_CONSTEXPR

View File

@ -931,3 +931,18 @@ TEST(FPTest, GetCachedPower) {
EXPECT_DOUBLE_EQ(pow(10, dec_exp), ldexp(fp.f, fp.e)); EXPECT_DOUBLE_EQ(pow(10, dec_exp), ldexp(fp.f, fp.e));
} }
} }
TEST(IteratorTest, CountingIterator) {
fmt::internal::counting_iterator<char> it;
auto prev = it++;
EXPECT_EQ(prev.count(), 0);
EXPECT_EQ(it.count(), 1);
}
TEST(IteratorTest, TruncatingIterator) {
char *p = FMT_NULL;
fmt::internal::truncating_iterator<char*> it(p, 3);
auto prev = it++;
EXPECT_EQ(prev.base(), p);
EXPECT_EQ(it.base(), p + 1);
}