Skip to content

Commit 7d991be

Browse files
author
Kai Liu
committed
Some small refactorings on table_test
Summary: Just revise some hard-to-read or unnecessarily verbose code. Test Plan: make check
1 parent eda924a commit 7d991be

File tree

1 file changed

+57
-58
lines changed

1 file changed

+57
-58
lines changed

table/table_test.cc

+57-58
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
// Copyright (c) 2011 The LevelDB Authors. All rights reserved.
77
// Use of this source code is governed by a BSD-style license that can be
88
// found in the LICENSE file. See the AUTHORS file for names of contributors.
9+
#include <algorithm>
910
#include <map>
1011
#include <string>
1112
#include <memory>
@@ -39,15 +40,12 @@
3940
namespace rocksdb {
4041

4142
namespace {
43+
4244
// Return reverse of "key".
4345
// Used to test non-lexicographic comparators.
44-
static std::string Reverse(const Slice& key) {
45-
std::string str(key.ToString());
46-
std::string rev("");
47-
for (std::string::reverse_iterator rit = str.rbegin();
48-
rit != str.rend(); ++rit) {
49-
rev.push_back(*rit);
50-
}
46+
std::string Reverse(const Slice& key) {
47+
auto rev = key.ToString();
48+
std::reverse(rev.begin(), rev.end());
5149
return rev;
5250
}
5351

@@ -76,10 +74,10 @@ class ReverseKeyComparator : public Comparator {
7674
*key = Reverse(s);
7775
}
7876
};
79-
} // namespace
80-
static ReverseKeyComparator reverse_key_comparator;
8177

82-
static void Increment(const Comparator* cmp, std::string* key) {
78+
ReverseKeyComparator reverse_key_comparator;
79+
80+
void Increment(const Comparator* cmp, std::string* key) {
8381
if (cmp == BytewiseComparator()) {
8482
key->push_back('\0');
8583
} else {
@@ -91,7 +89,6 @@ static void Increment(const Comparator* cmp, std::string* key) {
9189
}
9290

9391
// An STL comparator that uses a Comparator
94-
namespace anon {
9592
struct STLLessThan {
9693
const Comparator* cmp;
9794

@@ -101,6 +98,7 @@ struct STLLessThan {
10198
return cmp->Compare(Slice(a), Slice(b)) < 0;
10299
}
103100
};
101+
104102
} // namespace
105103

106104
class StringSink: public WritableFile {
@@ -168,13 +166,13 @@ class StringSource: public RandomAccessFile {
168166
bool mmap_;
169167
};
170168

171-
typedef std::map<std::string, std::string, anon::STLLessThan> KVMap;
169+
typedef std::map<std::string, std::string, STLLessThan> KVMap;
172170

173171
// Helper class for tests to unify the interface between
174172
// BlockBuilder/TableBuilder and Block/Table.
175173
class Constructor {
176174
public:
177-
explicit Constructor(const Comparator* cmp) : data_(anon::STLLessThan(cmp)) { }
175+
explicit Constructor(const Comparator* cmp) : data_(STLLessThan(cmp)) {}
178176
virtual ~Constructor() { }
179177

180178
void Add(const std::string& key, const Slice& value) {
@@ -520,61 +518,62 @@ struct TestArgs {
520518
CompressionType compression;
521519
};
522520

523-
524521
static std::vector<TestArgs> GenerateArgList() {
525-
std::vector<TestArgs> ret;
526-
TestType test_type[6] = { BLOCK_BASED_TABLE_TEST,
527-
PLAIN_TABLE_SEMI_FIXED_PREFIX, PLAIN_TABLE_FULL_STR_PREFIX, BLOCK_TEST,
528-
MEMTABLE_TEST, DB_TEST };
529-
int test_type_len = 6;
530-
bool reverse_compare[2] = {false, true};
531-
int reverse_compare_len = 2;
532-
int restart_interval[3] = {16, 1, 1024};
533-
int restart_interval_len = 3;
522+
std::vector<TestArgs> test_args;
523+
std::vector<TestType> test_types = {
524+
BLOCK_BASED_TABLE_TEST, PLAIN_TABLE_SEMI_FIXED_PREFIX,
525+
PLAIN_TABLE_FULL_STR_PREFIX, BLOCK_TEST,
526+
MEMTABLE_TEST, DB_TEST};
527+
std::vector<bool> reverse_compare_types = {false, true};
528+
std::vector<int> restart_intervals = {16, 1, 1024};
534529

535530
// Only add compression if it is supported
536-
std::vector<CompressionType> compression_types;
537-
compression_types.push_back(kNoCompression);
531+
std::vector<CompressionType> compression_types = {kNoCompression};
538532
#ifdef SNAPPY
539-
if (SnappyCompressionSupported())
533+
if (SnappyCompressionSupported()) {
540534
compression_types.push_back(kSnappyCompression);
535+
}
541536
#endif
542537

543538
#ifdef ZLIB
544-
if (ZlibCompressionSupported())
539+
if (ZlibCompressionSupported()) {
545540
compression_types.push_back(kZlibCompression);
541+
}
546542
#endif
547543

548544
#ifdef BZIP2
549-
if (BZip2CompressionSupported())
545+
if (BZip2CompressionSupported()) {
550546
compression_types.push_back(kBZip2Compression);
547+
}
551548
#endif
552549

553-
for(int i =0; i < test_type_len; i++)
554-
for (int j =0; j < reverse_compare_len; j++) {
555-
if (test_type[i] == PLAIN_TABLE_SEMI_FIXED_PREFIX
556-
|| test_type[i] == PLAIN_TABLE_FULL_STR_PREFIX) {
550+
for (auto test_type : test_types) {
551+
for (auto reverse_compare : reverse_compare_types) {
552+
if (test_type == PLAIN_TABLE_SEMI_FIXED_PREFIX ||
553+
test_type == PLAIN_TABLE_FULL_STR_PREFIX) {
557554
// Plain table doesn't use restart index or compression.
558555
TestArgs one_arg;
559-
one_arg.type = test_type[i];
560-
one_arg.reverse_compare = reverse_compare[0];
561-
one_arg.restart_interval = restart_interval[0];
556+
one_arg.type = test_type;
557+
one_arg.reverse_compare = reverse_compare;
558+
one_arg.restart_interval = restart_intervals[0];
562559
one_arg.compression = compression_types[0];
563-
ret.push_back(one_arg);
560+
test_args.push_back(one_arg);
564561
continue;
565562
}
566563

567-
for (int k = 0; k < restart_interval_len; k++)
568-
for (unsigned int n = 0; n < compression_types.size(); n++) {
564+
for (auto restart_interval : restart_intervals) {
565+
for (auto compression_type : compression_types) {
569566
TestArgs one_arg;
570-
one_arg.type = test_type[i];
571-
one_arg.reverse_compare = reverse_compare[j];
572-
one_arg.restart_interval = restart_interval[k];
573-
one_arg.compression = compression_types[n];
574-
ret.push_back(one_arg);
567+
one_arg.type = test_type;
568+
one_arg.reverse_compare = reverse_compare;
569+
one_arg.restart_interval = restart_interval;
570+
one_arg.compression = compression_type;
571+
test_args.push_back(one_arg);
575572
}
573+
}
576574
}
577-
return ret;
575+
}
576+
return test_args;
578577
}
579578

580579
// In order to make all tests run for plain table format, including
@@ -1245,7 +1244,7 @@ TEST(TableTest, ApproximateOffsetOfPlain) {
12451244

12461245
}
12471246

1248-
static void Do_Compression_Test(CompressionType comp) {
1247+
static void DoCompressionTest(CompressionType comp) {
12491248
Random rnd(301);
12501249
TableConstructor c(BytewiseComparator());
12511250
std::string tmp;
@@ -1287,7 +1286,7 @@ TEST(TableTest, ApproximateOffsetOfCompressed) {
12871286

12881287
for(int i =0; i < valid; i++)
12891288
{
1290-
Do_Compression_Test(compression_state[i]);
1289+
DoCompressionTest(compression_state[i]);
12911290
}
12921291

12931292
}
@@ -1407,29 +1406,29 @@ TEST(MemTableTest, Simple) {
14071406

14081407
// Test the empty key
14091408
TEST(Harness, SimpleEmptyKey) {
1410-
std::vector<TestArgs> args = GenerateArgList();
1411-
for (unsigned int i = 0; i < args.size(); i++) {
1412-
Init(args[i]);
1409+
auto args = GenerateArgList();
1410+
for (const auto& arg : args) {
1411+
Init(arg);
14131412
Random rnd(test::RandomSeed() + 1);
14141413
Add("", "v");
14151414
Test(&rnd);
14161415
}
14171416
}
14181417

14191418
TEST(Harness, SimpleSingle) {
1420-
std::vector<TestArgs> args = GenerateArgList();
1421-
for (unsigned int i = 0; i < args.size(); i++) {
1422-
Init(args[i]);
1419+
auto args = GenerateArgList();
1420+
for (const auto& arg : args) {
1421+
Init(arg);
14231422
Random rnd(test::RandomSeed() + 2);
14241423
Add("abc", "v");
14251424
Test(&rnd);
14261425
}
14271426
}
14281427

14291428
TEST(Harness, SimpleMulti) {
1430-
std::vector<TestArgs> args = GenerateArgList();
1431-
for (unsigned int i = 0; i < args.size(); i++) {
1432-
Init(args[i]);
1429+
auto args = GenerateArgList();
1430+
for (const auto& arg : args) {
1431+
Init(arg);
14331432
Random rnd(test::RandomSeed() + 3);
14341433
Add("abc", "v");
14351434
Add("abcd", "v");
@@ -1439,9 +1438,9 @@ TEST(Harness, SimpleMulti) {
14391438
}
14401439

14411440
TEST(Harness, SimpleSpecialKey) {
1442-
std::vector<TestArgs> args = GenerateArgList();
1443-
for (unsigned int i = 0; i < args.size(); i++) {
1444-
Init(args[i]);
1441+
auto args = GenerateArgList();
1442+
for (const auto& arg : args) {
1443+
Init(arg);
14451444
Random rnd(test::RandomSeed() + 4);
14461445
Add("\xff\xff", "v3");
14471446
Test(&rnd);

0 commit comments

Comments
 (0)