Coverage Report

Created: 2025-07-09 06:27

/src/cppitertools/fuzz_cppitertools.cpp
Line
Count
Source
1
/* Copyright 2023 Google LLC
2
Licensed under the Apache License, Version 2.0 (the "License");
3
you may not use this file except in compliance with the License.
4
You may obtain a copy of the License at
5
      http://www.apache.org/licenses/LICENSE-2.0
6
Unless required by applicable law or agreed to in writing, software
7
distributed under the License is distributed on an "AS IS" BASIS,
8
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
9
See the License for the specific language governing permissions and
10
limitations under the License.
11
*/
12
#include <chain.hpp>
13
#include <combinations.hpp>
14
#include <compress.hpp>
15
#include <cycle.hpp>
16
#include <groupby.hpp>
17
18
#include <fuzzer/FuzzedDataProvider.h>
19
20
#include <iterator>
21
#include <string>
22
#include <utility>
23
#include <vector>
24
25
using iter::chain;
26
using iter::combinations;
27
using iter::compress;
28
using iter::cycle;
29
using iter::groupby;
30
31
476
void FuzzChained(const uint8_t *data, size_t size) {
32
476
  FuzzedDataProvider fdp(data, size);
33
476
  std::vector<char> v;
34
51.1k
  for (int i = 0; i < fdp.ConsumeIntegralInRange<int>(1, 1024); i++) {
35
50.6k
    v.push_back((char)fdp.ConsumeIntegral<char>());
36
50.6k
  }
37
476
  const auto ch = chain(v, v, v);
38
476
  std::vector<char> v2(std::begin(ch), std::end(ch));
39
476
}
40
41
18.0k
int length(const std::string &s) { return s.size(); }
42
43
476
void FuzzGroupby(const uint8_t *data, size_t size) {
44
476
  FuzzedDataProvider fdp(data, size);
45
476
  std::vector<std::string> v;
46
12.3k
  for (int i = 0; i < fdp.ConsumeIntegralInRange<int>(1, 1024); i++) {
47
11.8k
    v.push_back(fdp.ConsumeRandomLengthString());
48
11.8k
  }
49
3.31k
  for (auto &&gb : groupby(v, length)) {
50
3.31k
  }
51
476
}
52
53
476
void FuzzCycle(const uint8_t *data, size_t size) {
54
476
  FuzzedDataProvider fdp(data, size);
55
476
  std::vector<int> v;
56
44.1k
  for (int i = 0; i < fdp.ConsumeIntegralInRange<int>(1, 1024); i++) {
57
43.6k
    v.push_back(fdp.ConsumeIntegral<int>());
58
43.6k
  }
59
476
  auto ch = cycle(v);
60
61
476
  std::vector<int> o;
62
476
  size_t count = 0;
63
714k
  for (auto val : ch) {
64
714k
    o.push_back(val);
65
714k
    count++;
66
714k
    if (count > 1500) {
67
476
      break;
68
476
    }
69
714k
  }
70
476
}
71
72
476
void FuzzCombinations(const uint8_t *data, size_t size) {
73
476
  FuzzedDataProvider fdp(data, size);
74
476
  std::string s = fdp.ConsumeRandomLengthString();
75
476
  std::vector<std::vector<char>> sc;
76
476
  size_t count = 0;
77
283k
  for (auto &&v : combinations(s, fdp.ConsumeIntegralInRange(1, 16))) {
78
283k
    sc.emplace_back(std::begin(v), std::end(v));
79
283k
    count++;
80
283k
    if (count > 1500) {
81
168
      break;
82
168
    }
83
283k
  }
84
476
}
85
86
476
void FuzzCompress(const uint8_t *data, size_t size) {
87
476
  FuzzedDataProvider fdp(data, size);
88
476
  std::vector<int> ivec;
89
476
  std::vector<bool> bvec;
90
48.0k
  for (int i = 0; i < 100; i++) {
91
47.6k
    ivec.push_back(fdp.ConsumeIntegralInRange(1, 1000000));
92
47.6k
    bvec.push_back(fdp.ConsumeBool());
93
47.6k
  }
94
476
  auto c = compress(ivec, bvec);
95
476
  std::vector<int> v(std::begin(c), std::end(c));
96
476
  const auto &c2 = c;
97
476
  (void)(std::begin(c) == std::end(c2));
98
476
}
99
100
476
extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
101
476
  FuzzChained(data, size);
102
476
  FuzzGroupby(data, size);
103
476
  FuzzCycle(data, size);
104
476
  FuzzCombinations(data, size);
105
476
  FuzzCompress(data, size);
106
476
  return 0;
107
476
}