summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorErik Kline <ek@google.com>2018-05-08 20:48:41 +0900
committerErik Kline <ek@google.com>2018-05-08 20:51:10 +0900
commitce4842e4186fdcdc0bed8221c216c26780acf2c1 (patch)
treef060702526f343fb3230a25f64852ef71c19daa6
parent0198ef5cd67b74a4e67a485a03c1c12c815aa32b (diff)
downloadnetd-ce4842e4186fdcdc0bed8221c216c26780acf2c1.tar.gz
Fix iteration through a slice to account for -Wsign-compare
It seems that pi-dev and AOSP compile flags might be different, as no -Wsign-compare failures happened on AOSP. Switching from for (int i = 0; ...) style to for (; !slice.empty(); ...) in several places also provides a better example of how to use Slice. Test: netdutils_test passes Bug: 78250686 Change-Id: Ic41d3e77810e41f7ae8573ea1cadbdf4808fa461
-rw-r--r--libnetdutils/MemBlockTest.cpp21
1 files changed, 10 insertions, 11 deletions
diff --git a/libnetdutils/MemBlockTest.cpp b/libnetdutils/MemBlockTest.cpp
index 81ea352b..6455a7e0 100644
--- a/libnetdutils/MemBlockTest.cpp
+++ b/libnetdutils/MemBlockTest.cpp
@@ -28,22 +28,21 @@ namespace netdutils {
namespace {
-constexpr int DNS_PACKET_SIZE = 512;
+constexpr unsigned DNS_PACKET_SIZE = 512;
constexpr int ARBITRARY_VALUE = 0x55;
MemBlock makeArbitraryMemBlock(size_t len) {
MemBlock result(len);
- Slice slice = result.get();
// Do some fictional work before returning.
- for (int i = 0; i < slice.size(); i++) {
- slice.base()[i] = ARBITRARY_VALUE;
+ for (Slice slice = result.get(); !slice.empty(); slice = drop(slice, 1)) {
+ slice.base()[0] = ARBITRARY_VALUE;
}
return result;
}
void checkAllZeros(Slice slice) {
- for (int i = 0; i < slice.size(); i++) {
- EXPECT_EQ(0U, slice.base()[i]);
+ for (; !slice.empty(); slice = drop(slice, 1)) {
+ EXPECT_EQ(0U, slice.base()[0]);
}
}
@@ -51,8 +50,8 @@ void checkArbitraryMemBlock(const MemBlock& block, size_t expectedSize) {
Slice slice = block.get();
EXPECT_EQ(expectedSize, slice.size());
EXPECT_NE(nullptr, slice.base());
- for (int i = 0; i < slice.size(); i++) {
- EXPECT_EQ(ARBITRARY_VALUE, slice.base()[i]);
+ for (; !slice.empty(); slice = drop(slice, 1)) {
+ EXPECT_EQ(ARBITRARY_VALUE, slice.base()[0]);
}
}
@@ -101,7 +100,7 @@ TEST(MemBlockTest, MoveAssignmentOrConstruction) {
}
TEST(MemBlockTest, StdMoveAssignment) {
- constexpr int SIZE = 10;
+ constexpr unsigned SIZE = 10;
MemBlock block;
EXPECT_TRUE(block.get().empty());
@@ -111,14 +110,14 @@ TEST(MemBlockTest, StdMoveAssignment) {
MemBlock block2 = makeArbitraryMemBlock(SIZE);
EXPECT_EQ(SIZE, block2.get().size());
// More fictional work.
- for (int i = 0; i < SIZE; i++) {
+ for (unsigned i = 0; i < SIZE; i++) {
block2.get().base()[i] = i;
}
block = std::move(block2);
}
EXPECT_EQ(SIZE, block.get().size());
- for (int i = 0; i < SIZE; i++) {
+ for (unsigned i = 0; i < SIZE; i++) {
EXPECT_EQ(i, block.get().base()[i]);
}
}