Coverage for /pythoncovmergedfiles/medio/medio/src/fuzz_itertoolz.py: 80%

100 statements  

« prev     ^ index     » next       coverage.py v7.2.2, created at 2023-03-26 06:47 +0000

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. 

26import sys 

27import atheris 

28import toolz 

29from toolz import dicttoolz, functoolz, itertoolz 

30from operator import add 

31 

32 

33def fuzz_curry(x, y): 

34 return x + y 

35 

36 

37def ConsumeDictionary(fdp, dict_size): 

38 dictionary = {} 

39 for _ in range(dict_size): 

40 dictionary[fdp.ConsumeUnicodeNoSurrogates(20)] = fdp.ConsumeIntInRange(1, 100) 

41 return dictionary 

42 

43 

44def ConsumeDictionaryReversed(fdp, dict_size): 

45 dictionary = {} 

46 for _ in range(dict_size): 

47 dictionary[fdp.ConsumeIntInRange(1, 100)] = fdp.ConsumeUnicodeNoSurrogates(20) 

48 return dictionary 

49 

50 

51def ConsumeDictionaryWithList(fdp, dict_size): 

52 dictionary = {} 

53 for _ in range(dict_size): 

54 dictionary[fdp.ConsumeUnicodeNoSurrogates(20)] = fdp.ConsumeIntListInRange(4, 1, 100) 

55 return dictionary 

56 

57 

58def TestOneInput(data): 

59 fdp = atheris.FuzzedDataProvider(data) 

60 val_1 = fdp.ConsumeUnicodeNoSurrogates(24) 

61 

62 fuzz_int_list_1 = fdp.ConsumeIntList(fdp.ConsumeIntInRange(1, 100), 4) 

63 fuzz_int_list_2 = fdp.ConsumeIntList(fdp.ConsumeIntInRange(1, 100), 4) 

64 fuzz_int_list_3 = fdp.ConsumeIntList(fdp.ConsumeIntInRange(1, 100), 4) 

65 

66 fuzz_int_list_4 = [] 

67 fuzz_int_list_5 = [] 

68 for i in range(fdp.ConsumeIntInRange(10, 50)): 

69 fuzz_int_list_4.append((i, fdp.ConsumeUnicodeNoSurrogates(12))) 

70 fuzz_int_list_5.append((i, fdp.ConsumeUnicodeNoSurrogates(12))) 

71 

72 str_list3 = [] 

73 str_list4 = [] 

74 for i in range(fdp.ConsumeIntInRange(10, 50)): 

75 str_list3.append((fdp.ConsumeUnicodeNoSurrogates(12), 

76 fdp.ConsumeUnicodeNoSurrogates(12))) 

77 str_list4.append((fdp.ConsumeUnicodeNoSurrogates(12), 

78 fdp.ConsumeUnicodeNoSurrogates(12))) 

79 

80 list( 

81 toolz.merge_sorted(fuzz_int_list_1, fuzz_int_list_2, 

82 fuzz_int_list_3)) 

83 list( 

84 toolz.merge_sorted(fuzz_int_list_1, 

85 fuzz_int_list_2, 

86 fuzz_int_list_3, 

87 key=lambda x: x + 2)) 

88 list( 

89 toolz.join(toolz.first, fuzz_int_list_4, 

90 toolz.second, fuzz_int_list_5)) 

91 

92 list( 

93 toolz.join(toolz.second, str_list3, 

94 toolz.first, str_list4)) 

95 list( 

96 toolz.join(toolz.second, 

97 str_list3, 

98 toolz.first, 

99 str_list4, 

100 left_default=None, 

101 right_default=None)) 

102 list( 

103 toolz.join(lambda x: x, 

104 str_list3, 

105 lambda x: x, 

106 str_list4, 

107 left_default=None)) 

108 list( 

109 toolz.join(lambda x: x, 

110 str_list3, 

111 lambda x: x, 

112 str_list4, 

113 right_default=None)) 

114 list(toolz.join(lambda x: x, str_list3, lambda x: x, str_list4)) 

115 list(toolz.diff(fuzz_int_list_2, fuzz_int_list_3)) 

116 list(toolz.partition_all(3, fuzz_int_list_3)) 

117 

118 try: 

119 toolz.get(fdp.ConsumeIntInRange(1, 1000000), fuzz_int_list_3) 

120 toolz.get([1, 2], fuzz_int_list_3) 

121 except (KeyError, IndexError, TypeError): 

122 pass 

123 

124 toolz.isdistinct(fuzz_int_list_3) 

125 toolz.isdistinct(fdp.ConsumeUnicodeNoSurrogates(256)) 

126 toolz.isiterable(fuzz_int_list_3) 

127 toolz.peekn(fdp.ConsumeIntInRange(1, 10), fuzz_int_list_3) 

128 toolz.peek(fuzz_int_list_3) 

129 list(toolz.tail(fdp.ConsumeIntInRange(1, 1000), fuzz_int_list_2)) 

130 tuple(toolz.unique(fuzz_int_list_3)) 

131 tuple(toolz.unique(fuzz_int_list_3, key=lambda x: x + 3)) 

132 list( 

133 toolz.interleave( 

134 [fuzz_int_list_1, fuzz_int_list_2, fuzz_int_list_3])) 

135 list(toolz.accumulate(add, fuzz_int_list_3)) 

136 

137 toolz.reduceby(lambda x: x + 8 == 2, add, fuzz_int_list_2) 

138 

139 # fuzz functoolz 

140 fuzz_curry_int = toolz.curry(fuzz_curry) 

141 first_number = fuzz_curry_int(fdp.ConsumeIntInRange(0, 1000)) 

142 first_number(fdp.ConsumeIntInRange(0, 1000)) 

143 

144 toolz.flip(fuzz_curry, fdp.ConsumeIntInRange(0, 1000), 

145 fdp.ConsumeIntInRange(0, 1000)) 

146 

147 # functions to use 

148 double = lambda i: 2 * i 

149 inc = lambda i: i + 1 

150 

151 toolz.pipe(fdp.ConsumeIntInRange(1, 100), double, str) 

152 toolz.compose(inc, double)(fdp.ConsumeIntInRange(1, 100)) 

153 toolz.compose_left(inc, double)(fdp.ConsumeIntInRange(1, 100)) 

154 

155 # fuzz dicttoolz 

156 toolz.dissoc(ConsumeDictionary(fdp, fdp.ConsumeIntInRange(1, 100)), 

157 fdp.ConsumeUnicodeNoSurrogates( 

158 fdp.ConsumeIntInRange(0, 1000))) 

159 toolz.assoc(ConsumeDictionary(fdp, fdp.ConsumeIntInRange(1, 100)), 

160 fdp.ConsumeUnicodeNoSurrogates( 

161 fdp.ConsumeIntInRange(0, 1000)), 

162 fdp.ConsumeIntInRange(0, 1000)) 

163 toolz.merge(ConsumeDictionary(fdp, fdp.ConsumeIntInRange(1, 100)), 

164 ConsumeDictionary(fdp, fdp.ConsumeIntInRange(1, 100))) 

165 toolz.merge_with(sum, ConsumeDictionary(fdp, fdp.ConsumeIntInRange(1, 100)), 

166 ConsumeDictionary(fdp, fdp.ConsumeIntInRange(1, 100))) 

167 toolz.valmap(sum, 

168 ConsumeDictionaryWithList(fdp, fdp.ConsumeIntInRange(1, 100))) 

169 toolz.keymap(str.lower, 

170 ConsumeDictionaryWithList(fdp, fdp.ConsumeIntInRange(1, 100))) 

171 toolz.itemmap(reversed, 

172 ConsumeDictionary(fdp, fdp.ConsumeIntInRange(1, 100))) 

173 toolz.valfilter(lambda x: x + 8 == 2, 

174 ConsumeDictionary(fdp, fdp.ConsumeIntInRange(1, 100))) 

175 toolz.keyfilter(lambda x: x + 8 == 2, 

176 ConsumeDictionaryReversed(fdp, fdp.ConsumeIntInRange(1, 100))) 

177 

178 # fuzz sandbox 

179 toolz.sandbox.core.unzip(ConsumeDictionary(fdp, 

180 fdp.ConsumeIntInRange(1, 100))) 

181 toolz.sandbox.core.EqualityHashKey(fdp.ConsumeUnicodeNoSurrogates( 

182 fdp.ConsumeIntInRange(0, 1000)), 

183 fdp.ConsumeUnicodeNoSurrogates( 

184 fdp.ConsumeIntInRange(0, 1000))) 

185 fold_list = fdp.ConsumeIntList(fdp.ConsumeIntInRange(1, 100), 4) 

186 toolz.sandbox.parallel.fold(add, 

187 fold_list, 

188 chunksize=fdp.ConsumeIntInRange(2, 100), 

189 map=map) 

190 

191 

192def main(): 

193 atheris.instrument_all() 

194 atheris.Setup(sys.argv, TestOneInput) 

195 atheris.Fuzz() 

196 

197 

198if __name__ == "__main__": 

199 main()