Coverage Report

Created: 2025-10-10 06:54

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/simdutf/fuzz/helpers/common.h
Line
Count
Source
1
// fuzzer helper functions
2
//
3
// by Paul Dreik 2024
4
5
#pragma once
6
7
#include <algorithm>
8
#include <charconv>
9
#include <iostream>
10
#include <span>
11
#include <string>
12
#include <tuple>
13
#include <type_traits>
14
#include <vector>
15
16
#include "simdutf.h"
17
18
#include "nameof.hpp"
19
20
/// checks that the given type is a member function pointer
21
template <typename T>
22
concept member_function_pointer = std::is_member_function_pointer_v<T>;
23
24
/// gets a list of implementations to fuzz
25
inline std::span<const simdutf::implementation* const>
26
2.03k
get_supported_implementations() {
27
2.03k
  static const auto impl = []() -> auto {
28
1
    std::vector<const simdutf::implementation*> ret;
29
4
    for (auto e : simdutf::get_available_implementations()) {
30
4
      std::cerr << "implementation " << e->name() << " is available? "
31
4
                << e->supported_by_runtime_system() << '\n';
32
4
      if (e->supported_by_runtime_system()) {
33
3
        ret.push_back(e);
34
3
      }
35
4
    }
36
1
    return ret;
37
1
  }();
38
2.03k
  return {impl.data(), impl.size()};
39
2.03k
}
40
41
/// this should go into the library instead
42
530
inline bool operator!=(const simdutf::result& a, const simdutf::result& b) {
43
530
  return a.count != b.count || a.error != b.error;
44
530
}
45
0
inline bool operator==(const simdutf::result& a, const simdutf::result& b) {
46
0
  return a.count == b.count && a.error == b.error;
47
0
}
48
0
auto operator<=>(const simdutf::result& a, const simdutf::result& b) {
49
0
  return std::tie(a.error, a.count) <=> std::tie(b.error, a.count);
50
0
}
51
52
0
inline std::ostream& operator<<(std::ostream& os, const simdutf::result& a) {
53
0
  os << "[count=" << a.count << ", error=" << NAMEOF_ENUM(a.error) << "]";
54
0
  return os;
55
0
}
56
57
template <typename Data>
58
constexpr bool is_hashable = std::is_arithmetic_v<Data>;
59
60
struct FNV1A_hash {
61
  static constexpr std::uint64_t prime = 0x00000100000001B3;
62
  static constexpr std::uint64_t offset = 0xcbf29ce484222325;
63
64
  static constexpr std::uint64_t
65
399
  fnv1ahash_impl(std::span<const unsigned char> bytes) {
66
399
    auto hash = offset;
67
68
16.1M
    for (std::uint64_t byte : bytes) {
69
16.1M
      hash ^= byte;
70
16.1M
      hash *= prime;
71
16.1M
    }
72
73
399
    return hash;
74
399
  }
75
0
  static constexpr std::uint64_t fnv1ahash_impl(std::span<const char> bytes) {
76
0
    auto hash = offset;
77
0
78
0
    for (auto byte : bytes) {
79
0
      hash ^= static_cast<unsigned char>(byte);
80
0
      hash *= prime;
81
0
    }
82
0
83
0
    return hash;
84
0
  }
85
86
  template <typename Basic, std::size_t N>
87
    requires(is_hashable<Basic> && !std::is_same_v<Basic, char> &&
88
             !std::is_same_v<Basic, unsigned char>)
89
399
  static constexpr std::uint64_t fnv1ahash_impl(std::span<Basic, N> data) {
90
399
    return fnv1ahash_impl({reinterpret_cast<const unsigned char*>(data.data()),
91
399
                           data.size_bytes()});
92
399
  }
93
94
  template <typename Data>
95
    requires is_hashable<Data>
96
399
  static constexpr std::uint64_t fnv1ahash_impl(const std::vector<Data>& data) {
97
399
    return fnv1ahash_impl(std::span(data));
98
399
  }
99
100
399
  template <typename... Data> static std::string as_str(const Data&... data) {
101
399
    static_assert(sizeof...(Data) > 0, "must hash with at least one argument");
102
399
    std::uint64_t h;
103
    if constexpr (sizeof...(Data) > 1) {
104
      const std::array hashes{fnv1ahash_impl(data)...};
105
      const auto s = std::span(hashes);
106
      h = fnv1ahash_impl(s);
107
399
    } else {
108
399
      h = fnv1ahash_impl(data...);
109
399
    }
110
399
    constexpr std::size_t expected_chars = 16;
111
399
    std::string ret(expected_chars, '0');
112
399
    auto c = std::to_chars(ret.data(), ret.data() + ret.size(), h, 16);
113
399
    assert(c.ec == std::errc{});
114
399
    auto nwritten = c.ptr - ret.data();
115
399
    assert(nwritten <= expected_chars);
116
399
    std::rotate(ret.data(), c.ptr, ret.data() + expected_chars);
117
399
    return ret;
118
399
  }
119
};
120
121
static_assert(FNV1A_hash::fnv1ahash_impl(std::string_view{""}) ==
122
              0xcbf29ce484222325);
123
static_assert(FNV1A_hash::fnv1ahash_impl(std::string_view{"xyz"}) ==
124
              0xbff4aa198026f420);
125
#if !defined(_GLIBCXX_RELEASE) || _GLIBCXX_RELEASE > 12
126
// work around https://gcc.gnu.org/bugzilla/show_bug.cgi?id=113294
127
static_assert(FNV1A_hash::fnv1ahash_impl(std::string{"xyz"}) ==
128
              0xbff4aa198026f420);
129
#endif
130
static_assert(FNV1A_hash::fnv1ahash_impl(std::string_view{"\xFF"}) ==
131
              0xaf64724c8602eb6e);
132
static_assert(FNV1A_hash::fnv1ahash_impl(std::string_view{
133
                  "\x01\x01\x01\x01"}) == 0xb5d0e0774c7d7499);
134
static_assert(FNV1A_hash::fnv1ahash_impl(std::array<unsigned char, 4>{
135
                  0x01, 0x01, 0x01, 0x01}) == 0xb5d0e0774c7d7499);