### Question:
BinaryHeaderMap implements Recyclable, Iterable<BinaryHeaderMap.Entry> { public boolean add(String key, byte[] value) { int valuesPos = valueBuffer.position(); CoderResult coderResult = IOUtils.decodeUtf8Bytes(value, valueBuffer); while (coderResult.isOverflow()) { ((Buffer) valueBuffer).limit(valuesPos); if (!enlargeBuffer()) { return false; } coderResult = IOUtils.decodeUtf8Bytes(value, valueBuffer); } boolean result; if (coderResult.isError()) { ((Buffer) valueBuffer).limit(valuesPos); result = false; } else { int size = keys.size(); keys.add(key); if (size == valueLengths.length) { enlargeValueLengths(); } valueLengths[size] = valueBuffer.position() - valuesPos; result = true; } return result; } BinaryHeaderMap(); int size(); boolean isEmpty(); boolean add(String key, byte[] value); @Override void resetState(); @Override Iterator<Entry> iterator(); void copyFrom(BinaryHeaderMap other); static final int MAXIMUM_HEADER_BUFFER_SIZE; }### Answer:
@Test void testOneEntry() { headerMap.add("foo", "bar".getBytes()); int numElements = 0; for (BinaryHeaderMap.Entry entry : headerMap) { numElements++; assertThat(entry.getKey()).isEqualTo("foo"); assertThat(entry.getValue().toString()).isEqualTo("bar"); } assertThat(numElements).isEqualTo(1); }
@Test void testTwoEntries() { headerMap.add(String.valueOf(0), "value0".getBytes()); headerMap.add(String.valueOf(1), "value1".getBytes()); int index = 0; int numElements = 0; for (BinaryHeaderMap.Entry entry : headerMap) { numElements++; String indexS = String.valueOf(index++); assertThat(entry.getKey()).isEqualTo(indexS); assertThat(entry.getValue().toString()).isEqualTo("value" + indexS); } assertThat(numElements).isEqualTo(2); }
@Test void testTwoIterations() { headerMap.add(String.valueOf(0), "value".getBytes()); headerMap.add(String.valueOf(1), "value".getBytes()); int numElements = 0; for (BinaryHeaderMap.Entry entry : headerMap) { numElements++; break; } for (BinaryHeaderMap.Entry entry : headerMap) { numElements++; } assertThat(numElements).isEqualTo(3); }