作者: 谢瑞阳 10225101483 徐翔宇 10225101535
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

54 rindas
1.6 KiB

  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