]>
Commit | Line | Data |
---|---|---|
7c673cae | 1 | // Copyright (c) 2011-present, Facebook, Inc. All rights reserved. |
11fdf7f2 TL |
2 | // This source code is licensed under both the GPLv2 (found in the |
3 | // COPYING file in the root directory) and Apache 2.0 License | |
4 | // (found in the LICENSE.Apache file in the root directory). | |
7c673cae FG |
5 | // |
6 | #ifndef ROCKSDB_LITE | |
7 | ||
8 | #include "rocksdb/utilities/ldb_cmd.h" | |
9 | #include "util/testharness.h" | |
10 | ||
11 | using std::string; | |
12 | using std::vector; | |
13 | using std::map; | |
14 | ||
15 | class LdbCmdTest : public testing::Test {}; | |
16 | ||
17 | TEST_F(LdbCmdTest, HexToString) { | |
18 | // map input to expected outputs. | |
19 | // odd number of "hex" half bytes doesn't make sense | |
20 | map<string, vector<int>> inputMap = { | |
21 | {"0x07", {7}}, {"0x5050", {80, 80}}, {"0xFF", {-1}}, | |
22 | {"0x1234", {18, 52}}, {"0xaaAbAC", {-86, -85, -84}}, {"0x1203", {18, 3}}, | |
23 | }; | |
24 | ||
25 | for (const auto& inPair : inputMap) { | |
26 | auto actual = rocksdb::LDBCommand::HexToString(inPair.first); | |
27 | auto expected = inPair.second; | |
28 | for (unsigned int i = 0; i < actual.length(); i++) { | |
29 | EXPECT_EQ(expected[i], static_cast<int>((signed char) actual[i])); | |
30 | } | |
31 | auto reverse = rocksdb::LDBCommand::StringToHex(actual); | |
32 | EXPECT_STRCASEEQ(inPair.first.c_str(), reverse.c_str()); | |
33 | } | |
34 | } | |
35 | ||
36 | TEST_F(LdbCmdTest, HexToStringBadInputs) { | |
37 | const vector<string> badInputs = { | |
38 | "0xZZ", "123", "0xx5", "0x111G", "0x123", "Ox12", "0xT", "0x1Q1", | |
39 | }; | |
40 | for (const auto badInput : badInputs) { | |
41 | try { | |
42 | rocksdb::LDBCommand::HexToString(badInput); | |
43 | std::cerr << "Should fail on bad hex value: " << badInput << "\n"; | |
44 | FAIL(); | |
45 | } catch (...) { | |
46 | } | |
47 | } | |
48 | } | |
49 | ||
50 | int main(int argc, char** argv) { | |
51 | ::testing::InitGoogleTest(&argc, argv); | |
52 | return RUN_ALL_TESTS(); | |
53 | } | |
54 | #else | |
55 | #include <stdio.h> | |
56 | ||
11fdf7f2 | 57 | int main(int /*argc*/, char** /*argv*/) { |
7c673cae FG |
58 | fprintf(stderr, "SKIPPED as LDBCommand is not supported in ROCKSDB_LITE\n"); |
59 | return 0; | |
60 | } | |
61 | ||
62 | #endif // ROCKSDB_LITE |