issue200_test.cc raw

   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  
   5  // Test for issue 200: when iterator switches direction from backward
   6  // to forward, the current key can be yielded unexpectedly if a new
   7  // mutation has been added just before the current key.
   8  
   9  #include "leveldb/db.h"
  10  #include "util/testharness.h"
  11  
  12  namespace leveldb {
  13  
  14  class Issue200 {};
  15  
  16  TEST(Issue200, Test) {
  17    // Get rid of any state from an old run.
  18    std::string dbpath = test::TmpDir() + "/leveldb_issue200_test";
  19    DestroyDB(dbpath, Options());
  20  
  21    DB* db;
  22    Options options;
  23    options.create_if_missing = true;
  24    ASSERT_OK(DB::Open(options, dbpath, &db));
  25  
  26    WriteOptions write_options;
  27    ASSERT_OK(db->Put(write_options, "1", "b"));
  28    ASSERT_OK(db->Put(write_options, "2", "c"));
  29    ASSERT_OK(db->Put(write_options, "3", "d"));
  30    ASSERT_OK(db->Put(write_options, "4", "e"));
  31    ASSERT_OK(db->Put(write_options, "5", "f"));
  32  
  33    ReadOptions read_options;
  34    Iterator* iter = db->NewIterator(read_options);
  35  
  36    // Add an element that should not be reflected in the iterator.
  37    ASSERT_OK(db->Put(write_options, "25", "cd"));
  38  
  39    iter->Seek("5");
  40    ASSERT_EQ(iter->key().ToString(), "5");
  41    iter->Prev();
  42    ASSERT_EQ(iter->key().ToString(), "4");
  43    iter->Prev();
  44    ASSERT_EQ(iter->key().ToString(), "3");
  45    iter->Next();
  46    ASSERT_EQ(iter->key().ToString(), "4");
  47    iter->Next();
  48    ASSERT_EQ(iter->key().ToString(), "5");
  49  
  50    delete iter;
  51    delete db;
  52    DestroyDB(dbpath, options);
  53  }
  54  
  55  }  // namespace leveldb
  56  
  57  int main(int argc, char** argv) { return leveldb::test::RunAllTests(); }
  58