From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from eggs.gnu.org ([2001:4830:134:3::10]:37365) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1X84Rd-0002Oy-95 for qemu-devel@nongnu.org; Fri, 18 Jul 2014 05:27:19 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1X84RX-0002us-48 for qemu-devel@nongnu.org; Fri, 18 Jul 2014 05:27:13 -0400 Received: from mx1.redhat.com ([209.132.183.28]:33813) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1X84RW-0002sy-Ps for qemu-devel@nongnu.org; Fri, 18 Jul 2014 05:27:07 -0400 Date: Fri, 18 Jul 2014 10:26:56 +0100 From: Stefan Hajnoczi Message-ID: <20140718092656.GD2685@stefanha-thinkpad.redhat.com> References: <040a32f2f5ebb18bcdfbcb3e6336ae912467370b.1405538416.git.maria.k@catit.be> MIME-Version: 1.0 Content-Type: multipart/signed; micalg=pgp-sha1; protocol="application/pgp-signature"; boundary="pZs/OQEoSSbxGlYw" Content-Disposition: inline In-Reply-To: <040a32f2f5ebb18bcdfbcb3e6336ae912467370b.1405538416.git.maria.k@catit.be> Subject: Re: [Qemu-devel] [PATCH V3 3/5] fuzz: Fuzzing functions for qcow2 images List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: Maria Kustova Cc: kwolf@redhat.com, famz@redhat.com, qemu-devel@nongnu.org, Maria Kustova --pZs/OQEoSSbxGlYw Content-Type: text/plain; charset=us-ascii Content-Disposition: inline On Wed, Jul 16, 2014 at 11:49:37PM +0400, Maria Kustova wrote: > +def string_validator(current, strings): > + """Return a random string value from the list not equal to the current. > + > + This function is useful for selection from valid values except current one. > + """ > + val = random.choice(strings) > + if val == current: > + return string_validator(current, strings) > + else: > + return val There is a pattern to these validator functions: def validator(current, pick, choices): while True: val = pick(choices) if val != current: return val int_validator is validator(_, random_from_intervals, _) bit_validator is validator(_, random_bits, _) string_validator is validator(_, random.choice, _) The code duplication in int_validator, bit_validator, and string_validator can be eliminated. > + > + > +def selector(current, constraints, fmt=None): > + """Select one value from all defined by constraints > + > + Each constraint produces one random value satisfying to it. The function > + randomly selects one value satisfying at least one constraint (depending on > + constraints overlaps). > + """ > + validate = { > + 'bitmask': bit_validator, > + 'string': string_validator > + }.get(fmt, int_validator) Is the indirection through a string necessary? def selector(current, constraints, fmt=int_validator): Callers can pass bit_validator or string_validator if they want. It makes the code a little simpler and it stays readable since string_validator's name tells you what it does. --pZs/OQEoSSbxGlYw Content-Type: application/pgp-signature -----BEGIN PGP SIGNATURE----- Version: GnuPG v1 iQEcBAEBAgAGBQJTyOhgAAoJEJykq7OBq3PIkjAIAIjjuGpOhUxZX/KMdKirdWcL Ru2AeBErvF+SGy8bx9vnQ2pa2I0EQSYCWrKbFoSV0VzMjrbtzEMvYgd5tHhRpxIN pK1BHtbvncGIjHSvWS7QmXeWSjzZBG5kLA+RB8tUmXsX7U5a7zS9FyRkWQsTkeZD 6OjPzSWxlconbrldY35NIosMWJbV5fCu2hLOh4yB3dKO5/iOBI3JY+cyuxp1JRI9 OKMO5MZr7W5g0HjWB0RPVABBef80/rs0/XDwDXv41oBIN9itNsT8VXN2mz+5O+2q h0kf+wnuWdKfDVR6LU3e0ZY93LJwmt5Uk7vKVemh+ELl7txqdminikvfZUqZq7Y= =Svli -----END PGP SIGNATURE----- --pZs/OQEoSSbxGlYw--