Coverage Report

Created: 2025-08-03 07:06

/src/immer/extra/fuzzer/set-st.cpp
Line
Count
Source (jump to first uncovered line)
1
//
2
// immer: immutable data structures for C++
3
// Copyright (C) 2016, 2017, 2018 Juan Pedro Bolivar Puente
4
//
5
// This software is distributed under the Boost Software License, Version 1.0.
6
// See accompanying file LICENSE or copy at http://boost.org/LICENSE_1_0.txt
7
//
8
9
#include "fuzzer_input.hpp"
10
11
#include <immer/heap/gc_heap.hpp>
12
#include <immer/refcount/no_refcount_policy.hpp>
13
#include <immer/set.hpp>
14
15
#include <immer/algorithm.hpp>
16
17
#include <array>
18
19
using st_memory = immer::memory_policy<immer::heap_policy<immer::cpp_heap>,
20
                                       immer::unsafe_refcount_policy,
21
                                       immer::no_lock_policy,
22
                                       immer::no_transience_policy,
23
                                       false>;
24
25
struct colliding_hash_t
26
{
27
487k
    std::size_t operator()(std::size_t x) const { return x & ~15; }
28
};
29
30
extern "C" int LLVMFuzzerTestOneInput(const std::uint8_t* data,
31
                                      std::size_t size)
32
4.19k
{
33
4.19k
    constexpr auto var_count = 4;
34
35
4.19k
    using set_t =
36
4.19k
        immer::set<size_t, colliding_hash_t, std::equal_to<>, st_memory>;
37
38
4.19k
    auto vars = std::array<set_t, var_count>{};
39
40
409k
    auto is_valid_var = [&](auto idx) { return idx >= 0 && idx < var_count; };
41
42
148k
    return fuzzer_input{data, size}.run([&](auto& in) {
43
148k
        enum ops
44
148k
        {
45
148k
            op_insert,
46
148k
            op_erase,
47
148k
            op_insert_move,
48
148k
            op_erase_move,
49
148k
            op_iterate,
50
148k
            op_diff
51
148k
        };
52
148k
        auto src = read<char>(in, is_valid_var);
53
148k
        auto dst = read<char>(in, is_valid_var);
54
148k
        switch (read<char>(in)) {
55
64.5k
        case op_insert: {
56
64.5k
            auto value = read<size_t>(in);
57
64.5k
            vars[dst]  = vars[src].insert(value);
58
64.5k
            break;
59
0
        }
60
10.4k
        case op_erase: {
61
10.4k
            auto value = read<size_t>(in);
62
10.4k
            vars[dst]  = vars[src].erase(value);
63
10.4k
            break;
64
0
        }
65
23.2k
        case op_insert_move: {
66
23.2k
            auto value = read<size_t>(in);
67
23.2k
            vars[dst]  = std::move(vars[src]).insert(value);
68
23.2k
            break;
69
0
        }
70
9.69k
        case op_erase_move: {
71
9.69k
            auto value = read<size_t>(in);
72
9.69k
            vars[dst]  = std::move(vars[src]).erase(value);
73
9.69k
            break;
74
0
        }
75
7.55k
        case op_iterate: {
76
7.55k
            auto srcv = vars[src];
77
170k
            for (const auto& v : srcv) {
78
170k
                vars[dst] = vars[dst].insert(v);
79
170k
            }
80
7.55k
            break;
81
0
        }
82
11.8k
        case op_diff: {
83
11.8k
            auto&& a = vars[src];
84
11.8k
            auto&& b = vars[dst];
85
11.8k
            diff(
86
11.8k
                a,
87
11.8k
                b,
88
45.2k
                [&](auto&& x) {
89
45.2k
                    assert(!a.count(x));
90
45.2k
                    assert(b.count(x));
91
45.2k
                },
92
43.8k
                [&](auto&& x) {
93
43.8k
                    assert(a.count(x));
94
43.8k
                    assert(!b.count(x));
95
43.8k
                },
96
11.8k
                [&](auto&& x, auto&& y) { assert(false); });
97
11.8k
        }
98
28.9k
        default:
99
28.9k
            break;
100
148k
        };
101
144k
        return true;
102
148k
    });
103
4.19k
}