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.
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.
9 #include "leveldb/db.h"
10 #include "util/testharness.h"
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());
23 options.create_if_missing = true;
24 ASSERT_OK(DB::Open(options, dbpath, &db));
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"));
33 ReadOptions read_options;
34 Iterator *iter = db->NewIterator(read_options);
36 // Add an element that should not be reflected in the iterator.
37 ASSERT_OK(db->Put(write_options, "25", "cd"));
40 ASSERT_EQ(iter->key().ToString(), "5");
42 ASSERT_EQ(iter->key().ToString(), "4");
44 ASSERT_EQ(iter->key().ToString(), "3");
46 ASSERT_EQ(iter->key().ToString(), "4");
48 ASSERT_EQ(iter->key().ToString(), "5");
52 DestroyDB(dbpath, options);
55 } // namespace leveldb
57 int main(int argc, char** argv) {
58 return leveldb::test::RunAllTests();