<commit_before>package org.fusesource.lmdbjni;

import org.junit.After;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;

import java.io.IOException;
import java.util.LinkedList;

import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertTrue;

public class EntryIteratorTest {
  static {
    Setup.setLmdbLibraryPath();
  }

  @Rule
  public TemporaryFolder tmp = new TemporaryFolder();

  Env env;
  Database db;
  LinkedList<byte[]> keys;

  @Before
  public void before() throws IOException {
    String path = tmp.newFolder().getCanonicalPath();
    env = new Env(path);
    db = env.openDatabase();
    keys = new LinkedList<>();
    for (int i = 0; i < 10; i++) {
      byte[] bytes = new byte[]{(byte) i};
      keys.add(bytes);
      db.put(bytes, bytes);
    }
  }

  @After
  public void after() {
    db.close();
    env.close();
  }

  @Test
  public void testIterateForward() {
    try (Transaction tx = env.createReadTransaction(); EntryIterator it = db.iterate(tx)) {
      while (it.hasNext()) {
        Entry next = it.next();
        assertArrayEquals(keys.pollFirst(), next.getKey());
      }
      assertTrue(keys.isEmpty());
    }
  }

  @Test
  public void testIterateBackward() {
    try (Transaction tx = env.createReadTransaction(); EntryIterator it = db.iterateBackward(tx)) {
      while (it.hasNext()) {
        Entry next = it.next();
        assertArrayEquals(keys.pollLast(), next.getKey());
      }
      assertTrue(keys.isEmpty());
    }
  }

  @Test
  public void testSeekForward() {
    keys.pollFirst();
    keys.pollFirst();
    keys.pollFirst();
    keys.pollFirst();
    keys.pollFirst();
    try (Transaction tx = env.createReadTransaction(); EntryIterator it = db.seek(tx, new byte[]{5})) {
      while (it.hasNext()) {
        Entry next = it.next();
        assertArrayEquals(keys.pollFirst(), next.getKey());
      }
      assertTrue(keys.isEmpty());
    }
  }

  @Test
  public void testSeekBackward() {
    keys.pollLast();
    keys.pollLast();
    keys.pollLast();
    keys.pollLast();
    try (Transaction tx = env.createReadTransaction(); EntryIterator it = db.seekBackward(tx, new byte[]{5})) {
      while (it.hasNext()) {
        Entry next = it.next();
        assertArrayEquals(keys.pollLast(), next.getKey());
      }
      assertTrue(keys.isEmpty());
    }
  }

  @Test
  public void testIterable() {
    try (Transaction tx = env.createReadTransaction(); EntryIterator it = db.iterate(tx)) {
      for (Entry next : it.iterable()) {
        assertArrayEquals(keys.pollFirst(), next.getKey());
      }
    }
    assertTrue(keys.isEmpty());
  }
}<commit_msg>Test Iterator contract for EntryIterator #52<commit_after>package org.fusesource.lmdbjni;

import org.junit.After;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;

import java.io.IOException;
import java.util.Iterator;
import java.util.LinkedList;

import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

public class EntryIteratorTest {
  static {
    Setup.setLmdbLibraryPath();
  }

  @Rule
  public TemporaryFolder tmp = new TemporaryFolder();

  Env env;
  Database db;
  LinkedList<byte[]> keys;

  @Before
  public void before() throws IOException {
    String path = tmp.newFolder().getCanonicalPath();
    env = new Env(path);
    db = env.openDatabase();
    keys = new LinkedList<>();
    for (int i = 0; i < 10; i++) {
      byte[] bytes = new byte[]{(byte) i};
      keys.add(bytes);
      db.put(bytes, bytes);
    }
  }

  @After
  public void after() {
    db.close();
    env.close();
  }

  @Test
  public void testIterateForward() {
    try (Transaction tx = env.createReadTransaction(); EntryIterator it = db.iterate(tx)) {
      while (it.hasNext()) {
        Entry next = it.next();
        assertArrayEquals(keys.pollFirst(), next.getKey());
      }
      assertFalse(it.hasNext());
      assertTrue(keys.isEmpty());
    }
  }

  @Test
  public void testIterateBackward() {
    try (Transaction tx = env.createReadTransaction(); EntryIterator it = db.iterateBackward(tx)) {
      while (it.hasNext()) {
        Entry next = it.next();
        assertArrayEquals(keys.pollLast(), next.getKey());
      }
      assertFalse(it.hasNext());
      assertTrue(keys.isEmpty());
    }
  }

  @Test
  public void testSeekForward() {
    keys.pollFirst();
    keys.pollFirst();
    keys.pollFirst();
    keys.pollFirst();
    keys.pollFirst();
    try (Transaction tx = env.createReadTransaction(); EntryIterator it = db.seek(tx, new byte[]{5})) {
      while (it.hasNext()) {
        Entry next = it.next();
        assertArrayEquals(keys.pollFirst(), next.getKey());
      }
      assertFalse(it.hasNext());
      assertTrue(keys.isEmpty());
    }
  }

  @Test
  public void testSeekBackward() {
    keys.pollLast();
    keys.pollLast();
    keys.pollLast();
    keys.pollLast();
    try (Transaction tx = env.createReadTransaction(); EntryIterator it = db.seekBackward(tx, new byte[]{5})) {
      while (it.hasNext()) {
        Entry next = it.next();
        assertArrayEquals(keys.pollLast(), next.getKey());
      }
      assertFalse(it.hasNext());
      assertTrue(keys.isEmpty());
    }
  }

  @Test
  public void testIterable() {
    try (Transaction tx = env.createReadTransaction(); EntryIterator it = db.iterate(tx)) {
      Iterator<Entry> iterator = it.iterable().iterator();
      while (iterator.hasNext()) {
        assertArrayEquals(keys.pollFirst(), iterator.next().getKey());
      }
      assertFalse(iterator.hasNext());
    }
    assertTrue(keys.isEmpty());
  }
}
{'fraction_non_alphanumeric': 0.1472072385592483, 'fraction_numerical': 0.0020880459370106142, 'mean_word_length': 3.0365168539325844, 'pattern_counts': {'":': 0, '<': 10, '<?xml version=': 0, '>': 8, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4362809', 'n_tokens_mistral': 1775, 'n_tokens_neox': 1761, 'n_words': 384}