mirror of
https://github.com/KingDuckZ/dindexer.git
synced 2025-02-17 11:45:50 +00:00
New string_to_tiger function.
Comes with unit test that indirectly also tests the previously added lexical_cast.
This commit is contained in:
parent
7db82f83d9
commit
4b30fabbe4
4 changed files with 84 additions and 0 deletions
|
@ -21,6 +21,7 @@
|
|||
#include <cstdint>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include <ciso646>
|
||||
|
||||
namespace mchlib {
|
||||
struct TigerHash {
|
||||
|
@ -35,6 +36,8 @@ namespace mchlib {
|
|||
uint64_t data[3];
|
||||
uint8_t byte_data[sizeof(uint64_t) * 3];
|
||||
};
|
||||
|
||||
bool operator== (const TigerHash& parOther) const { return part_a == parOther.part_a and part_b == parOther.part_b and part_c == parOther.part_c; }
|
||||
};
|
||||
|
||||
static_assert(sizeof(TigerHash) == 24, "Wrong struct size");
|
||||
|
@ -42,6 +45,7 @@ namespace mchlib {
|
|||
void tiger_file ( const std::string& parPath, TigerHash& parHashFile, TigerHash& parHashDir, uint64_t& parSizeOut );
|
||||
void tiger_init_hash ( TigerHash& parHash );
|
||||
std::string tiger_to_string ( const TigerHash& parHash, bool parUpcase=false );
|
||||
TigerHash string_to_tiger ( const std::string& parString );
|
||||
void tiger_data ( const std::string& parData, TigerHash& parHash );
|
||||
void tiger_data ( const std::vector<char>& parData, TigerHash& parHash );
|
||||
} //namespace mchlib
|
||||
|
|
|
@ -16,6 +16,7 @@
|
|||
*/
|
||||
|
||||
#include "dindexer-machinery/tiger.hpp"
|
||||
#include "helpers/casts.hpp"
|
||||
#include <fstream>
|
||||
#include <cstdint>
|
||||
#include <memory>
|
||||
|
@ -24,6 +25,7 @@
|
|||
#include <utility>
|
||||
#include <sstream>
|
||||
#include <iomanip>
|
||||
#include <boost/utility/string_ref.hpp>
|
||||
|
||||
extern "C" void tiger ( const char* parStr, uint64_t parLength, uint64_t parHash[3], char parPadding );
|
||||
|
||||
|
@ -153,6 +155,20 @@ namespace mchlib {
|
|||
return oss.str();
|
||||
}
|
||||
|
||||
TigerHash string_to_tiger (const std::string& parString) {
|
||||
using boost::string_ref;
|
||||
using TigerPartType = decltype(TigerHash::part_a);
|
||||
|
||||
assert(parString.size() == sizeof(TigerHash) * 2);
|
||||
|
||||
TigerHash retval;
|
||||
const string_ref inp(parString);
|
||||
retval.part_a = swap_long(dinhelp::lexical_cast<TigerPartType, dinhelp::tags::hex>(inp.substr(0, sizeof(TigerPartType) * 2)));
|
||||
retval.part_b = swap_long(dinhelp::lexical_cast<TigerPartType, dinhelp::tags::hex>(inp.substr(sizeof(TigerPartType) * 2, sizeof(TigerPartType) * 2)));
|
||||
retval.part_c = swap_long(dinhelp::lexical_cast<TigerPartType, dinhelp::tags::hex>(inp.substr(sizeof(TigerPartType) * 4, sizeof(TigerPartType) * 2)));
|
||||
return retval;
|
||||
}
|
||||
|
||||
void tiger_data (const std::string& parData, TigerHash& parHash) {
|
||||
tiger (parData.data(), parData.size(), parHash.data, g_tiger_padding);
|
||||
}
|
||||
|
|
|
@ -4,6 +4,7 @@ add_executable(${PROJECT_NAME}
|
|||
test_diriterator.cpp
|
||||
test_guess_content_type.cpp
|
||||
test_glob2regex.cpp
|
||||
test_tiger_string_conv.cpp
|
||||
)
|
||||
|
||||
target_include_directories(${PROJECT_NAME} SYSTEM
|
||||
|
|
63
test/unit/test_tiger_string_conv.cpp
Normal file
63
test/unit/test_tiger_string_conv.cpp
Normal file
|
@ -0,0 +1,63 @@
|
|||
/* Copyright 2015, 2016, Michele Santullo
|
||||
* This file is part of "dindexer".
|
||||
*
|
||||
* "dindexer" is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* "dindexer" is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with "dindexer". If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include "dindexer-machinery/tiger.hpp"
|
||||
#include <gtest/gtest.h>
|
||||
#include <string>
|
||||
|
||||
TEST(machinery, TigerStringConv) {
|
||||
using mchlib::TigerHash;
|
||||
|
||||
const std::size_t TigerStringCharCount = sizeof(TigerHash) * 2;
|
||||
{
|
||||
TigerHash tiger_zero;
|
||||
tiger_zero.part_a = tiger_zero.part_b = tiger_zero.part_c = 0;
|
||||
|
||||
const std::string expected(TigerStringCharCount, '0');
|
||||
const std::string got = mchlib::tiger_to_string(tiger_zero);
|
||||
EXPECT_EQ(expected, got);
|
||||
|
||||
auto hash2 = mchlib::string_to_tiger(got);
|
||||
EXPECT_EQ(tiger_zero, hash2);
|
||||
}
|
||||
|
||||
{
|
||||
TigerHash t;
|
||||
mchlib::tiger_init_hash(t);
|
||||
|
||||
const std::string expected = "efcdab89674523011032547698badcfe87e1b2c3b4a596f0";
|
||||
EXPECT_EQ(TigerStringCharCount, expected.size());
|
||||
const std::string got = mchlib::tiger_to_string(t);
|
||||
EXPECT_EQ(expected, got);
|
||||
|
||||
auto hash2 = mchlib::string_to_tiger(got);
|
||||
EXPECT_EQ(t, hash2);
|
||||
}
|
||||
|
||||
{
|
||||
TigerHash t;
|
||||
t.part_a = t.part_b = t.part_c = 0xababababababababULL;
|
||||
|
||||
const std::string expected = "abababababababababababababababababababababababab";
|
||||
EXPECT_EQ(TigerStringCharCount, expected.size());
|
||||
const std::string got = mchlib::tiger_to_string(t);
|
||||
EXPECT_EQ(expected, got);
|
||||
|
||||
auto hash2 = mchlib::string_to_tiger(got);
|
||||
EXPECT_EQ(t, hash2);
|
||||
}
|
||||
}
|
Loading…
Add table
Reference in a new issue