Some small refactorings on table_test

Summary:

Just revise some hard-to-read or unnecessarily verbose code.

Test Plan:

make check
main
Kai Liu 11 years ago
parent eda924a03a
commit 7d991be400
  1. 115
      table/table_test.cc

@ -6,6 +6,7 @@
// Copyright (c) 2011 The LevelDB Authors. All rights reserved. // Copyright (c) 2011 The LevelDB Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be // Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file. See the AUTHORS file for names of contributors. // found in the LICENSE file. See the AUTHORS file for names of contributors.
#include <algorithm>
#include <map> #include <map>
#include <string> #include <string>
#include <memory> #include <memory>
@ -39,15 +40,12 @@
namespace rocksdb { namespace rocksdb {
namespace { namespace {
// Return reverse of "key". // Return reverse of "key".
// Used to test non-lexicographic comparators. // Used to test non-lexicographic comparators.
static std::string Reverse(const Slice& key) { std::string Reverse(const Slice& key) {
std::string str(key.ToString()); auto rev = key.ToString();
std::string rev(""); std::reverse(rev.begin(), rev.end());
for (std::string::reverse_iterator rit = str.rbegin();
rit != str.rend(); ++rit) {
rev.push_back(*rit);
}
return rev; return rev;
} }
@ -76,10 +74,10 @@ class ReverseKeyComparator : public Comparator {
*key = Reverse(s); *key = Reverse(s);
} }
}; };
} // namespace
static ReverseKeyComparator reverse_key_comparator;
static void Increment(const Comparator* cmp, std::string* key) { ReverseKeyComparator reverse_key_comparator;
void Increment(const Comparator* cmp, std::string* key) {
if (cmp == BytewiseComparator()) { if (cmp == BytewiseComparator()) {
key->push_back('\0'); key->push_back('\0');
} else { } else {
@ -91,7 +89,6 @@ static void Increment(const Comparator* cmp, std::string* key) {
} }
// An STL comparator that uses a Comparator // An STL comparator that uses a Comparator
namespace anon {
struct STLLessThan { struct STLLessThan {
const Comparator* cmp; const Comparator* cmp;
@ -101,6 +98,7 @@ struct STLLessThan {
return cmp->Compare(Slice(a), Slice(b)) < 0; return cmp->Compare(Slice(a), Slice(b)) < 0;
} }
}; };
} // namespace } // namespace
class StringSink: public WritableFile { class StringSink: public WritableFile {
@ -168,13 +166,13 @@ class StringSource: public RandomAccessFile {
bool mmap_; bool mmap_;
}; };
typedef std::map<std::string, std::string, anon::STLLessThan> KVMap; typedef std::map<std::string, std::string, STLLessThan> KVMap;
// Helper class for tests to unify the interface between // Helper class for tests to unify the interface between
// BlockBuilder/TableBuilder and Block/Table. // BlockBuilder/TableBuilder and Block/Table.
class Constructor { class Constructor {
public: public:
explicit Constructor(const Comparator* cmp) : data_(anon::STLLessThan(cmp)) { } explicit Constructor(const Comparator* cmp) : data_(STLLessThan(cmp)) {}
virtual ~Constructor() { } virtual ~Constructor() { }
void Add(const std::string& key, const Slice& value) { void Add(const std::string& key, const Slice& value) {
@ -520,61 +518,62 @@ struct TestArgs {
CompressionType compression; CompressionType compression;
}; };
static std::vector<TestArgs> GenerateArgList() { static std::vector<TestArgs> GenerateArgList() {
std::vector<TestArgs> ret; std::vector<TestArgs> test_args;
TestType test_type[6] = { BLOCK_BASED_TABLE_TEST, std::vector<TestType> test_types = {
PLAIN_TABLE_SEMI_FIXED_PREFIX, PLAIN_TABLE_FULL_STR_PREFIX, BLOCK_TEST, BLOCK_BASED_TABLE_TEST, PLAIN_TABLE_SEMI_FIXED_PREFIX,
MEMTABLE_TEST, DB_TEST }; PLAIN_TABLE_FULL_STR_PREFIX, BLOCK_TEST,
int test_type_len = 6; MEMTABLE_TEST, DB_TEST};
bool reverse_compare[2] = {false, true}; std::vector<bool> reverse_compare_types = {false, true};
int reverse_compare_len = 2; std::vector<int> restart_intervals = {16, 1, 1024};
int restart_interval[3] = {16, 1, 1024};
int restart_interval_len = 3;
// Only add compression if it is supported // Only add compression if it is supported
std::vector<CompressionType> compression_types; std::vector<CompressionType> compression_types = {kNoCompression};
compression_types.push_back(kNoCompression);
#ifdef SNAPPY #ifdef SNAPPY
if (SnappyCompressionSupported()) if (SnappyCompressionSupported()) {
compression_types.push_back(kSnappyCompression); compression_types.push_back(kSnappyCompression);
}
#endif #endif
#ifdef ZLIB #ifdef ZLIB
if (ZlibCompressionSupported()) if (ZlibCompressionSupported()) {
compression_types.push_back(kZlibCompression); compression_types.push_back(kZlibCompression);
}
#endif #endif
#ifdef BZIP2 #ifdef BZIP2
if (BZip2CompressionSupported()) if (BZip2CompressionSupported()) {
compression_types.push_back(kBZip2Compression); compression_types.push_back(kBZip2Compression);
}
#endif #endif
for(int i =0; i < test_type_len; i++) for (auto test_type : test_types) {
for (int j =0; j < reverse_compare_len; j++) { for (auto reverse_compare : reverse_compare_types) {
if (test_type[i] == PLAIN_TABLE_SEMI_FIXED_PREFIX if (test_type == PLAIN_TABLE_SEMI_FIXED_PREFIX ||
|| test_type[i] == PLAIN_TABLE_FULL_STR_PREFIX) { test_type == PLAIN_TABLE_FULL_STR_PREFIX) {
// Plain table doesn't use restart index or compression. // Plain table doesn't use restart index or compression.
TestArgs one_arg; TestArgs one_arg;
one_arg.type = test_type[i]; one_arg.type = test_type;
one_arg.reverse_compare = reverse_compare[0]; one_arg.reverse_compare = reverse_compare;
one_arg.restart_interval = restart_interval[0]; one_arg.restart_interval = restart_intervals[0];
one_arg.compression = compression_types[0]; one_arg.compression = compression_types[0];
ret.push_back(one_arg); test_args.push_back(one_arg);
continue; continue;
} }
for (int k = 0; k < restart_interval_len; k++) for (auto restart_interval : restart_intervals) {
for (unsigned int n = 0; n < compression_types.size(); n++) { for (auto compression_type : compression_types) {
TestArgs one_arg; TestArgs one_arg;
one_arg.type = test_type[i]; one_arg.type = test_type;
one_arg.reverse_compare = reverse_compare[j]; one_arg.reverse_compare = reverse_compare;
one_arg.restart_interval = restart_interval[k]; one_arg.restart_interval = restart_interval;
one_arg.compression = compression_types[n]; one_arg.compression = compression_type;
ret.push_back(one_arg); test_args.push_back(one_arg);
} }
}
} }
return ret; }
return test_args;
} }
// In order to make all tests run for plain table format, including // In order to make all tests run for plain table format, including
@ -1245,7 +1244,7 @@ TEST(TableTest, ApproximateOffsetOfPlain) {
} }
static void Do_Compression_Test(CompressionType comp) { static void DoCompressionTest(CompressionType comp) {
Random rnd(301); Random rnd(301);
TableConstructor c(BytewiseComparator()); TableConstructor c(BytewiseComparator());
std::string tmp; std::string tmp;
@ -1287,7 +1286,7 @@ TEST(TableTest, ApproximateOffsetOfCompressed) {
for(int i =0; i < valid; i++) for(int i =0; i < valid; i++)
{ {
Do_Compression_Test(compression_state[i]); DoCompressionTest(compression_state[i]);
} }
} }
@ -1407,9 +1406,9 @@ TEST(MemTableTest, Simple) {
// Test the empty key // Test the empty key
TEST(Harness, SimpleEmptyKey) { TEST(Harness, SimpleEmptyKey) {
std::vector<TestArgs> args = GenerateArgList(); auto args = GenerateArgList();
for (unsigned int i = 0; i < args.size(); i++) { for (const auto& arg : args) {
Init(args[i]); Init(arg);
Random rnd(test::RandomSeed() + 1); Random rnd(test::RandomSeed() + 1);
Add("", "v"); Add("", "v");
Test(&rnd); Test(&rnd);
@ -1417,9 +1416,9 @@ TEST(Harness, SimpleEmptyKey) {
} }
TEST(Harness, SimpleSingle) { TEST(Harness, SimpleSingle) {
std::vector<TestArgs> args = GenerateArgList(); auto args = GenerateArgList();
for (unsigned int i = 0; i < args.size(); i++) { for (const auto& arg : args) {
Init(args[i]); Init(arg);
Random rnd(test::RandomSeed() + 2); Random rnd(test::RandomSeed() + 2);
Add("abc", "v"); Add("abc", "v");
Test(&rnd); Test(&rnd);
@ -1427,9 +1426,9 @@ TEST(Harness, SimpleSingle) {
} }
TEST(Harness, SimpleMulti) { TEST(Harness, SimpleMulti) {
std::vector<TestArgs> args = GenerateArgList(); auto args = GenerateArgList();
for (unsigned int i = 0; i < args.size(); i++) { for (const auto& arg : args) {
Init(args[i]); Init(arg);
Random rnd(test::RandomSeed() + 3); Random rnd(test::RandomSeed() + 3);
Add("abc", "v"); Add("abc", "v");
Add("abcd", "v"); Add("abcd", "v");
@ -1439,9 +1438,9 @@ TEST(Harness, SimpleMulti) {
} }
TEST(Harness, SimpleSpecialKey) { TEST(Harness, SimpleSpecialKey) {
std::vector<TestArgs> args = GenerateArgList(); auto args = GenerateArgList();
for (unsigned int i = 0; i < args.size(); i++) { for (const auto& arg : args) {
Init(args[i]); Init(arg);
Random rnd(test::RandomSeed() + 4); Random rnd(test::RandomSeed() + 4);
Add("\xff\xff", "v3"); Add("\xff\xff", "v3");
Test(&rnd); Test(&rnd);

Loading…
Cancel
Save