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 "gtest/gtest.h"
10 #include "leveldb/db.h"
11 #include "util/testutil.h"
12
13 namespace leveldb {
14
TEST(Issue200,Test)15 TEST(Issue200, Test) {
16 // Get rid of any state from an old run.
17 std::string dbpath = testing::TempDir() + "leveldb_issue200_test";
18 DestroyDB(dbpath, Options());
19
20 DB* db;
21 Options options;
22 options.create_if_missing = true;
23 ASSERT_LEVELDB_OK(DB::Open(options, dbpath, &db));
24
25 WriteOptions write_options;
26 ASSERT_LEVELDB_OK(db->Put(write_options, "1", "b"));
27 ASSERT_LEVELDB_OK(db->Put(write_options, "2", "c"));
28 ASSERT_LEVELDB_OK(db->Put(write_options, "3", "d"));
29 ASSERT_LEVELDB_OK(db->Put(write_options, "4", "e"));
30 ASSERT_LEVELDB_OK(db->Put(write_options, "5", "f"));
31
32 ReadOptions read_options;
33 Iterator* iter = db->NewIterator(read_options);
34
35 // Add an element that should not be reflected in the iterator.
36 ASSERT_LEVELDB_OK(db->Put(write_options, "25", "cd"));
37
38 iter->Seek("5");
39 ASSERT_EQ(iter->key().ToString(), "5");
40 iter->Prev();
41 ASSERT_EQ(iter->key().ToString(), "4");
42 iter->Prev();
43 ASSERT_EQ(iter->key().ToString(), "3");
44 iter->Next();
45 ASSERT_EQ(iter->key().ToString(), "4");
46 iter->Next();
47 ASSERT_EQ(iter->key().ToString(), "5");
48
49 delete iter;
50 delete db;
51 DestroyDB(dbpath, options);
52 }
53
54 } // namespace leveldb
55
main(int argc,char ** argv)56 int main(int argc, char** argv) {
57 testing::InitGoogleTest(&argc, argv);
58 return RUN_ALL_TESTS();
59 }
60