issue200_test.cc 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. // Copyright (c) 2013 The LevelDB Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file. See the AUTHORS file for names of contributors.
  4. // Test for issue 200: when iterator switches direction from backward
  5. // to forward, the current key can be yielded unexpectedly if a new
  6. // mutation has been added just before the current key.
  7. #include "gtest/gtest.h"
  8. #include "leveldb/db.h"
  9. #include "util/testutil.h"
  10. namespace leveldb {
  11. TEST(Issue200, Test) {
  12. // Get rid of any state from an old run.
  13. std::string dbpath = testing::TempDir() + "leveldb_issue200_test";
  14. DestroyDB(dbpath, Options());
  15. DB* db;
  16. Options options;
  17. options.create_if_missing = true;
  18. ASSERT_LEVELDB_OK(DB::Open(options, dbpath, &db));
  19. WriteOptions write_options;
  20. ASSERT_LEVELDB_OK(db->Put(write_options, "1", "b"));
  21. ASSERT_LEVELDB_OK(db->Put(write_options, "2", "c"));
  22. ASSERT_LEVELDB_OK(db->Put(write_options, "3", "d"));
  23. ASSERT_LEVELDB_OK(db->Put(write_options, "4", "e"));
  24. ASSERT_LEVELDB_OK(db->Put(write_options, "5", "f"));
  25. ReadOptions read_options;
  26. Iterator* iter = db->NewIterator(read_options);
  27. // Add an element that should not be reflected in the iterator.
  28. ASSERT_LEVELDB_OK(db->Put(write_options, "25", "cd"));
  29. iter->Seek("5");
  30. ASSERT_EQ(iter->key().ToString(), "5");
  31. iter->Prev();
  32. ASSERT_EQ(iter->key().ToString(), "4");
  33. iter->Prev();
  34. ASSERT_EQ(iter->key().ToString(), "3");
  35. iter->Next();
  36. ASSERT_EQ(iter->key().ToString(), "4");
  37. iter->Next();
  38. ASSERT_EQ(iter->key().ToString(), "5");
  39. delete iter;
  40. delete db;
  41. DestroyDB(dbpath, options);
  42. }
  43. } // namespace leveldb