Skip to content

Commit

Permalink
TESTS: Add unit tests for our generic string hash functions
Browse files Browse the repository at this point in the history
  • Loading branch information
DrMcCoy committed Feb 25, 2017
1 parent cdb6772 commit cceb7f7
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 0 deletions.
65 changes: 65 additions & 0 deletions tests/common/hash.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/* xoreos-tools - Tools to help with xoreos development
*
* xoreos-tools is the legal property of its developers, whose names
* can be found in the AUTHORS file distributed with this source
* distribution.
*
* xoreos-tools 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.
*
* xoreos-tools 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 xoreos-tools. If not, see <http://www.gnu.org/licenses/>.
*/

/** @file
* Unit tests for our generic string hash functions.
*/

#include "gtest/gtest.h"

#include "src/common/hash.h"

static const char *kString = "Foobar";

GTEST_TEST(Hash, DJB2) {
EXPECT_EQ(Common::hashString(kString, Common::kHashDJB2), 0xB33F4C9E);
}

GTEST_TEST(Hash, FNV32) {
EXPECT_EQ(Common::hashString(kString, Common::kHashFNV32), 0xED18E8C2);
}

GTEST_TEST(Hash, FNV64) {
EXPECT_EQ(Common::hashString(kString, Common::kHashFNV64), UINT64_C(0x744E9FFF32CA0A22));
}

GTEST_TEST(Hash, CRC32) {
EXPECT_EQ(Common::hashString(kString, Common::kHashCRC32), 0x995A1AA3);
}

GTEST_TEST(Hash, DJB2Encoding) {
EXPECT_EQ(Common::hashString(kString, Common::kHashDJB2, Common::kEncodingUTF16LE), 0xD11D54FE);
}

GTEST_TEST(Hash, FNV32Encoding) {
EXPECT_EQ(Common::hashString(kString, Common::kHashFNV32, Common::kEncodingUTF16LE), 0xCE5005F0);
}

GTEST_TEST(Hash, FNV64Encoding) {
EXPECT_EQ(Common::hashString(kString, Common::kHashFNV64, Common::kEncodingUTF16LE), UINT64_C(0xA73456F669A95770));
}

GTEST_TEST(Hash, CRC32Encoding) {
EXPECT_EQ(Common::hashString(kString, Common::kHashCRC32, Common::kEncodingUTF16LE), 0x56031CD6);
}

GTEST_TEST(Hash, formatHash) {
EXPECT_STREQ(Common::formatHash(UINT64_C(0x1234567890ABCDEF)).c_str(), "0x1234567890ABCDEF");
}
5 changes: 5 additions & 0 deletions tests/common/rules.mk
Original file line number Diff line number Diff line change
Expand Up @@ -159,3 +159,8 @@ check_PROGRAMS += tests/common/test_filepath
tests_common_test_filepath_SOURCES = tests/common/filepath.cpp
tests_common_test_filepath_LDADD = $(common_LIBS)
tests_common_test_filepath_CXXFLAGS = $(test_CXXFLAGS)

check_PROGRAMS += tests/common/test_hash
tests_common_test_hash_SOURCES = tests/common/hash.cpp
tests_common_test_hash_LDADD = $(common_LIBS)
tests_common_test_hash_CXXFLAGS = $(test_CXXFLAGS)

0 comments on commit cceb7f7

Please sign in to comment.