6
6
// Copyright (c) 2011 The LevelDB Authors. All rights reserved.
7
7
// Use of this source code is governed by a BSD-style license that can be
8
8
// found in the LICENSE file. See the AUTHORS file for names of contributors.
9
+ #include < algorithm>
9
10
#include < map>
10
11
#include < string>
11
12
#include < memory>
39
40
namespace rocksdb {
40
41
41
42
namespace {
43
+
42
44
// Return reverse of "key".
43
45
// 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 ());
51
49
return rev;
52
50
}
53
51
@@ -76,10 +74,10 @@ class ReverseKeyComparator : public Comparator {
76
74
*key = Reverse (s);
77
75
}
78
76
};
79
- } // namespace
80
- static ReverseKeyComparator reverse_key_comparator;
81
77
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) {
83
81
if (cmp == BytewiseComparator ()) {
84
82
key->push_back (' \0 ' );
85
83
} else {
@@ -91,7 +89,6 @@ static void Increment(const Comparator* cmp, std::string* key) {
91
89
}
92
90
93
91
// An STL comparator that uses a Comparator
94
- namespace anon {
95
92
struct STLLessThan {
96
93
const Comparator* cmp;
97
94
@@ -101,6 +98,7 @@ struct STLLessThan {
101
98
return cmp->Compare (Slice (a), Slice (b)) < 0 ;
102
99
}
103
100
};
101
+
104
102
} // namespace
105
103
106
104
class StringSink : public WritableFile {
@@ -168,13 +166,13 @@ class StringSource: public RandomAccessFile {
168
166
bool mmap_;
169
167
};
170
168
171
- typedef std::map<std::string, std::string, anon:: STLLessThan> KVMap;
169
+ typedef std::map<std::string, std::string, STLLessThan> KVMap;
172
170
173
171
// Helper class for tests to unify the interface between
174
172
// BlockBuilder/TableBuilder and Block/Table.
175
173
class Constructor {
176
174
public:
177
- explicit Constructor (const Comparator* cmp) : data_(anon:: STLLessThan(cmp)) { }
175
+ explicit Constructor (const Comparator* cmp) : data_(STLLessThan(cmp)) {}
178
176
virtual ~Constructor () { }
179
177
180
178
void Add (const std::string& key, const Slice& value) {
@@ -520,61 +518,62 @@ struct TestArgs {
520
518
CompressionType compression;
521
519
};
522
520
523
-
524
521
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 };
534
529
535
530
// 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 };
538
532
#ifdef SNAPPY
539
- if (SnappyCompressionSupported ())
533
+ if (SnappyCompressionSupported ()) {
540
534
compression_types.push_back (kSnappyCompression );
535
+ }
541
536
#endif
542
537
543
538
#ifdef ZLIB
544
- if (ZlibCompressionSupported ())
539
+ if (ZlibCompressionSupported ()) {
545
540
compression_types.push_back (kZlibCompression );
541
+ }
546
542
#endif
547
543
548
544
#ifdef BZIP2
549
- if (BZip2CompressionSupported ())
545
+ if (BZip2CompressionSupported ()) {
550
546
compression_types.push_back (kBZip2Compression );
547
+ }
551
548
#endif
552
549
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) {
557
554
// Plain table doesn't use restart index or compression.
558
555
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 ];
562
559
one_arg.compression = compression_types[0 ];
563
- ret .push_back (one_arg);
560
+ test_args .push_back (one_arg);
564
561
continue ;
565
562
}
566
563
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) {
569
566
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);
575
572
}
573
+ }
576
574
}
577
- return ret;
575
+ }
576
+ return test_args;
578
577
}
579
578
580
579
// In order to make all tests run for plain table format, including
@@ -1245,7 +1244,7 @@ TEST(TableTest, ApproximateOffsetOfPlain) {
1245
1244
1246
1245
}
1247
1246
1248
- static void Do_Compression_Test (CompressionType comp) {
1247
+ static void DoCompressionTest (CompressionType comp) {
1249
1248
Random rnd (301 );
1250
1249
TableConstructor c (BytewiseComparator ());
1251
1250
std::string tmp;
@@ -1287,7 +1286,7 @@ TEST(TableTest, ApproximateOffsetOfCompressed) {
1287
1286
1288
1287
for (int i =0 ; i < valid; i++)
1289
1288
{
1290
- Do_Compression_Test (compression_state[i]);
1289
+ DoCompressionTest (compression_state[i]);
1291
1290
}
1292
1291
1293
1292
}
@@ -1407,29 +1406,29 @@ TEST(MemTableTest, Simple) {
1407
1406
1408
1407
// Test the empty key
1409
1408
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 );
1413
1412
Random rnd (test::RandomSeed () + 1 );
1414
1413
Add (" " , " v" );
1415
1414
Test (&rnd);
1416
1415
}
1417
1416
}
1418
1417
1419
1418
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 );
1423
1422
Random rnd (test::RandomSeed () + 2 );
1424
1423
Add (" abc" , " v" );
1425
1424
Test (&rnd);
1426
1425
}
1427
1426
}
1428
1427
1429
1428
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 );
1433
1432
Random rnd (test::RandomSeed () + 3 );
1434
1433
Add (" abc" , " v" );
1435
1434
Add (" abcd" , " v" );
@@ -1439,9 +1438,9 @@ TEST(Harness, SimpleMulti) {
1439
1438
}
1440
1439
1441
1440
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 );
1445
1444
Random rnd (test::RandomSeed () + 4 );
1446
1445
Add (" \xff\xff " , " v3" );
1447
1446
Test (&rnd);
0 commit comments