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