Coverage for /pythoncovmergedfiles/medio/medio/src/fuzz_melt_map.py: 67%
Shortcuts on this page
r m x toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
Shortcuts on this page
r m x toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
1###### Coverage stub
2import atexit
3import coverage
4cov = coverage.coverage(data_file='.coverage', cover_pylib=True)
5cov.start()
6# Register an exist handler that will print coverage
7def exit_handler():
8 cov.stop()
9 cov.save()
10atexit.register(exit_handler)
11####### End of coverage stub
12#!/usr/bin/python3
13# Copyright 2023 Google LLC
14#
15# Licensed under the Apache License, Version 2.0 (the "License");
16# you may not use this file except in compliance with the License.
17# You may obtain a copy of the License at
18#
19# http://www.apache.org/licenses/LICENSE-2.0
20#
21# Unless required by applicable law or agreed to in writing, software
22# distributed under the License is distributed on an "AS IS" BASIS,
23# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
24# See the License for the specific language governing permissions and
25# limitations under the License.
27"""This fuzzer script targets the pandas melt and map functions, creating a variety of DataFrames with mixed data
28types and then transforming them using melting and mapping operations."""
30import sys
31import atheris
32import pandas as pd
35def TestOneInput(data):
36 fdp = atheris.FuzzedDataProvider(data)
38 try:
39 groups = {
40 fdp.ConsumeString(10): fdp.ConsumeUnicodeNoSurrogates(10),
41 fdp.ConsumeString(10): fdp.ConsumeUnicodeNoSurrogates(10)
42 }
44 num_rows = fdp.ConsumeIntInRange(3, 30)
45 num_columns = fdp.ConsumeIntInRange(3, 30)
46 col_names = [fdp.ConsumeString(fdp.ConsumeIntInRange(1, 15)) for _ in range(num_columns)]
48 df_content = {}
49 for col_name in col_names:
50 if fdp.ConsumeBool():
51 df_content[col_name] = [fdp.ConsumeInt(10) for _ in range(num_rows)]
52 elif fdp.ConsumeBool():
53 df_content[col_name] = [fdp.ConsumeString(10) for _ in range(num_rows)]
54 elif fdp.ConsumeBool():
55 df_content[col_name] = [fdp.ConsumeIntInRange(0, 2100) for _ in range(num_rows)]
56 elif fdp.ConsumeBool():
57 df_content[col_name] = [fdp.ConsumeFloat() for _ in range(num_rows)]
58 else:
59 df_content[col_name] = [fdp.ConsumeBool() for _ in range(num_rows)]
61 id_vars_data = [
62 col_names[fdp.ConsumeIntInRange(0, len(col_names) - 1)],
63 col_names[fdp.ConsumeIntInRange(0, len(col_names) - 1)],
64 col_names[fdp.ConsumeIntInRange(0, len(col_names) - 1)]
65 ]
66 if fdp.ConsumeBool():
67 id_vars_data.append(fdp.ConsumeString(fdp.ConsumeIntInRange(1, 15)))
69 df = pd.DataFrame(df_content)
70 name = fdp.ConsumeString(20)
71 df = pd.melt(
72 df,
73 id_vars=id_vars_data,
74 value_vars=[
75 col_names[fdp.ConsumeIntInRange(0, len(col_names) - 1)],
76 col_names[fdp.ConsumeIntInRange(0, len(col_names) - 1)]
77 ],
78 var_name='gf', value_name=fdp.ConsumeString(10)
79 )
81 local_var_name = 'gf' if fdp.ConsumeBool() else fdp.ConsumeString(10)
82 df[name] = df[local_var_name].map(groups)
83 except (
84 KeyError, # if `id_vars` are not in the frame.
85 ValueError # if initial data from seed is empty and fdp produces empty data.
86 ):
87 pass
90def main():
91 atheris.Setup(sys.argv, TestOneInput)
92 atheris.instrument_all()
93 atheris.Fuzz()
96if __name__ == "__main__":
97 main()