1 // Copyright (c) 2011 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 #include "leveldb/db.h"
7 #include "db/memtable.h"
8 #include "db/write_batch_internal.h"
9 #include "leveldb/env.h"
10 #include "util/logging.h"
11 #include "util/testharness.h"
15 static std::string PrintContents(WriteBatch* b) {
16 InternalKeyComparator cmp(BytewiseComparator());
17 MemTable* mem = new MemTable(cmp);
20 Status s = WriteBatchInternal::InsertInto(b, mem);
21 Iterator* iter = mem->NewIterator();
22 for (iter->SeekToFirst(); iter->Valid(); iter->Next()) {
23 ParsedInternalKey ikey;
24 ASSERT_TRUE(ParseInternalKey(iter->key(), &ikey));
28 state.append(ikey.user_key.ToString());
30 state.append(iter->value().ToString());
34 state.append("Delete(");
35 state.append(ikey.user_key.ToString());
40 state.append(NumberToString(ikey.sequence));
44 state.append("ParseError()");
50 class WriteBatchTest { };
52 TEST(WriteBatchTest, Empty) {
54 ASSERT_EQ("", PrintContents(&batch));
55 ASSERT_EQ(0, WriteBatchInternal::Count(&batch));
58 TEST(WriteBatchTest, Multiple) {
60 batch.Put(Slice("foo"), Slice("bar"));
61 batch.Delete(Slice("box"));
62 batch.Put(Slice("baz"), Slice("boo"));
63 WriteBatchInternal::SetSequence(&batch, 100);
64 ASSERT_EQ(100, WriteBatchInternal::Sequence(&batch));
65 ASSERT_EQ(3, WriteBatchInternal::Count(&batch));
66 ASSERT_EQ("Put(baz, boo)@102"
69 PrintContents(&batch));
72 TEST(WriteBatchTest, Corruption) {
74 batch.Put(Slice("foo"), Slice("bar"));
75 batch.Delete(Slice("box"));
76 WriteBatchInternal::SetSequence(&batch, 200);
77 Slice contents = WriteBatchInternal::Contents(&batch);
78 WriteBatchInternal::SetContents(&batch,
79 Slice(contents.data(),contents.size()-1));
80 ASSERT_EQ("Put(foo, bar)@200"
82 PrintContents(&batch));
85 } // namespace leveldb
87 int main(int argc, char** argv) {
88 return leveldb::test::RunAllTests();