Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion include/hash23/fnv_1.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@
#pragma once

#include <cstddef>
#include <cstdint>
#include <ranges>
#include <span>
#include <type_traits>

namespace hash23 {
class fnv_1 {
Expand Down Expand Up @@ -37,8 +39,15 @@ namespace hash23 {
requires std::ranges::contiguous_range<T>
constexpr void update(T const &data) {
for (auto const &byte : data) {
using value_type = std::remove_cvref_t<decltype(byte)>;
std::uint8_t b;
if constexpr (std::is_same_v<value_type, std::byte>) {
b = std::to_integer<std::uint8_t>(byte);
} else {
b = static_cast<std::uint8_t>(byte);
}
hash_ *= prime();
hash_ ^= static_cast<std::uint8_t>(byte);
hash_ ^= b;
}
}

Expand Down
11 changes: 10 additions & 1 deletion include/hash23/fnv_1a.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@
#pragma once

#include <cstddef>
#include <cstdint>
#include <ranges>
#include <span>
#include <type_traits>

namespace hash23 {
class fnv_1a {
Expand Down Expand Up @@ -37,7 +39,14 @@ namespace hash23 {
requires std::ranges::contiguous_range<T>
constexpr void update(T const &data) {
for (auto const &byte: data) {
hash_ ^= static_cast<std::uint8_t>(byte);
using value_type = std::remove_cvref_t<decltype(byte)>;
std::uint8_t b;
if constexpr (std::is_same_v<value_type, std::byte>) {
b = std::to_integer<std::uint8_t>(byte);
} else {
b = static_cast<std::uint8_t>(byte);
}
hash_ ^= b;
hash_ *= prime();
}
}
Expand Down
31 changes: 31 additions & 0 deletions tests/fnv_1_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@

#include <gtest/gtest.h>
#include <hash23/hash23.h>
#include <cstddef>
#include <string>
#include <vector>

namespace {
TEST(fnv_1, single_block_test) {
Expand Down Expand Up @@ -39,4 +41,33 @@ namespace {
EXPECT_EQ(expected, actual);
}
}

TEST(fnv_1, high_byte_values_test) {
std::vector<signed char> const data = {
static_cast<signed char>(0x80), static_cast<signed char>(0xAB),
static_cast<signed char>(0xFF), 0x00, 0x7F
};
auto const actual = hash23::fnv_1::calculate(data);
if constexpr (sizeof(std::size_t) == 4) {
constexpr std::size_t expected = 0x4430384auz;
EXPECT_EQ(expected, actual);
} else {
constexpr std::size_t expected = 0xeaadd0afbf431feauz;
EXPECT_EQ(expected, actual);
}
}

TEST(fnv_1, std_byte_test) {
std::vector<std::byte> const data = {
std::byte{0x80}, std::byte{0xAB}, std::byte{0xFF}, std::byte{0x00}, std::byte{0x7F}
};
auto const actual = hash23::fnv_1::calculate(data);
if constexpr (sizeof(std::size_t) == 4) {
constexpr std::size_t expected = 0x4430384auz;
EXPECT_EQ(expected, actual);
} else {
constexpr std::size_t expected = 0xeaadd0afbf431feauz;
EXPECT_EQ(expected, actual);
}
}
}
31 changes: 31 additions & 0 deletions tests/fnv_1a_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@

#include <gtest/gtest.h>
#include <hash23/hash23.h>
#include <cstddef>
#include <string>
#include <vector>

namespace {
TEST(fnv_1a, single_block_test) {
Expand Down Expand Up @@ -39,4 +41,33 @@ namespace {
EXPECT_EQ(expected, actual);
}
}

TEST(fnv_1a, high_byte_values_test) {
std::vector<signed char> const data = {
static_cast<signed char>(0x80), static_cast<signed char>(0xAB),
static_cast<signed char>(0xFF), 0x00, 0x7F
};
auto const actual = hash23::fnv_1a::calculate(data);
if constexpr (sizeof(std::size_t) == 4) {
constexpr std::size_t expected = 0x9036aaacuz;
EXPECT_EQ(expected, actual);
} else {
constexpr std::size_t expected = 0x6b576bf0e27fc9acuz;
EXPECT_EQ(expected, actual);
}
}

TEST(fnv_1a, std_byte_test) {
std::vector<std::byte> const data = {
std::byte{0x80}, std::byte{0xAB}, std::byte{0xFF}, std::byte{0x00}, std::byte{0x7F}
};
auto const actual = hash23::fnv_1a::calculate(data);
if constexpr (sizeof(std::size_t) == 4) {
constexpr std::size_t expected = 0x9036aaacuz;
EXPECT_EQ(expected, actual);
} else {
constexpr std::size_t expected = 0x6b576bf0e27fc9acuz;
EXPECT_EQ(expected, actual);
}
}
}