Add an isEmpty convenience method to BpfMap.
Test: new unit test Change-Id: Ibec09f328e24111aee4760af2f75ea5a80ba84c3
This commit is contained in:
@@ -134,6 +134,11 @@ public class BpfMap<K extends Struct, V extends Struct> implements AutoCloseable
|
|||||||
return deleteMapEntry(mMapFd, key.writeToBytes());
|
return deleteMapEntry(mMapFd, key.writeToBytes());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** Returns {@code true} if this map contains no elements. */
|
||||||
|
public boolean isEmpty() throws ErrnoException {
|
||||||
|
return getFirstKey() == null;
|
||||||
|
}
|
||||||
|
|
||||||
private K getNextKeyInternal(@Nullable K key) throws ErrnoException {
|
private K getNextKeyInternal(@Nullable K key) throws ErrnoException {
|
||||||
final byte[] rawKey = getNextRawKey(
|
final byte[] rawKey = getNextRawKey(
|
||||||
key == null ? null : key.writeToBytes());
|
key == null ? null : key.writeToBytes());
|
||||||
|
|||||||
@@ -91,6 +91,7 @@ public final class BpfMapTest {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
assertNull(mTestMap.getFirstKey());
|
assertNull(mTestMap.getFirstKey());
|
||||||
|
assertTrue(mTestMap.isEmpty());
|
||||||
}
|
}
|
||||||
|
|
||||||
private TetherDownstream6Key createTetherDownstream6Key(long iif, String address)
|
private TetherDownstream6Key createTetherDownstream6Key(long iif, String address)
|
||||||
@@ -135,6 +136,18 @@ public final class BpfMapTest {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testIsEmpty() throws Exception {
|
||||||
|
assertNull(mTestMap.getFirstKey());
|
||||||
|
assertTrue(mTestMap.isEmpty());
|
||||||
|
|
||||||
|
mTestMap.insertEntry(mTestData.keyAt(0), mTestData.valueAt(0));
|
||||||
|
assertFalse(mTestMap.isEmpty());
|
||||||
|
|
||||||
|
mTestMap.deleteEntry((mTestData.keyAt(0)));
|
||||||
|
assertTrue(mTestMap.isEmpty());
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testGetFirstKey() throws Exception {
|
public void testGetFirstKey() throws Exception {
|
||||||
// getFirstKey on an empty map returns null.
|
// getFirstKey on an empty map returns null.
|
||||||
|
|||||||
Reference in New Issue
Block a user