<commit_before>//
//  Arbitrary.swift
//  SwiftCheck
//
//  Created by <NAME> on 7/31/14.
//  Copyright (c) 2014 <NAME>. All rights reserved.
//

import Darwin

/// A type that implements random generation and shrinking of values.
///
/// While testing, SwiftCheck will invoke `arbitrary()` a given amount of times (usually 100 if the
/// default settings are used).  During that time, the receiver has an opportunity to call through 
/// to any data or sources of randomness it needs to return what it deems an "Arbitrary" value.
///
/// Shrinking is reduction in the complexity of a tested value to remove noise and present a minimal
/// counterexample when a property fails.  While it may seem counterintuitive, a shrink necessitates
/// returning a list of all possible "smaller" values for SwiftCheck to run through.  As long as
/// each individual value in the returned list is less than or equal to the size of the input value
/// a minimal case should be reached fairly efficiently.
///
/// As an example, take the `ArrayOf` implementation of shrink:
///
/// shrink(ArrayOf([1, 2, 3]))
///	> [[], [2,3], [1,3], [1,2], [0,2,3], [1,0,3], [1,1,3], [1,2,0], [1,2,2]]
///
/// SwiftCheck will search each case 1 by one and continue shrinking until it has reached a case
/// it deems minimal enough to present.
///
/// SwiftCheck implements a number of generators for common STL types for convenience.  If more fine-
/// grained testing is required see `Modifiers.swift` for an example of how to define a "Modifier"
/// type to implement it.
public protocol Arbitrary {
	static func arbitrary() -> Gen<Self>
	static func shrink(Self) -> [Self]
}


extension Bool : Arbitrary {
	public static func arbitrary() -> Gen<Bool> {
		return Gen.pure((arc4random() % 2) == 1)
	}

	public static func shrink(x : Bool) -> [Bool] {
		if x {
			return [false]
		}
		return []
	}
}

extension Int : Arbitrary {
	public static func arbitrary() -> Gen<Int> {
		let sign = ((arc4random() % 2) == 1)
		return sized { n in Gen.pure((sign ? 1 : -1) * Int(arc4random_uniform(UInt32(n)))) }
	}

	public static func shrink(x : Int) -> [Int] {
		return shrinkIntegral(x)
	}
}

extension Int8 : Arbitrary {
	public static func arbitrary() -> Gen<Int8> {
		let sign = ((arc4random() % 2) == 1)
		return sized { n in Gen.pure((sign ? 1 : -1) * Int8(arc4random_uniform(UInt32(n)))) }
	}

	public static func shrink(x : Int8) -> [Int8] {
		return shrinkIntegral(x)
	}
}

extension Int16 : Arbitrary {
	public static func arbitrary() -> Gen<Int16> {
		let sign = ((arc4random() % 2) == 1)
		return sized { n in Gen.pure((sign ? 1 : -1) * Int16(arc4random_uniform(UInt32(n)))) }
	}

	public static func shrink(x : Int16) -> [Int16] {
		return shrinkIntegral(x)
	}
}

extension Int32 : Arbitrary {
	public static func arbitrary() -> Gen<Int32> {
		let sign = ((arc4random() % 2) == 1)
		return sized { n in Gen.pure((sign ? 1 : -1) * Int32(arc4random_uniform(UInt32(n)))) }
	}

	public static func shrink(x : Int32) -> [Int32] {
		return shrinkIntegral(x)
	}
}

extension Int64 : Arbitrary {
	public static func arbitrary() -> Gen<Int64> {
		let sign = ((arc4random() % 2) == 1)
		return sized { n in Gen.pure((sign ? 1 : -1) * Int64(arc4random_uniform(UInt32(n)))) }
	}

	public static func shrink(x : Int64) -> [Int64] {
		return shrinkIntegral(x)
	}
}

extension UInt : Arbitrary {
	public static func arbitrary() -> Gen<UInt> {
		return sized { n in Gen<UInt>.pure(UInt(arc4random_uniform(UInt32(abs(n))))) }
	}

	public static func shrink(x : UInt) -> [UInt] {
		return shrinkIntegral(x)
	}
}

extension UInt8 : Arbitrary {
	public static func arbitrary() -> Gen<UInt8> {
		return sized({ n in
			return sized { n in Gen<UInt8>.pure(UInt8(arc4random_uniform(UInt32(abs(n))))) }
		})
	}

	public static func shrink(x : UInt8) -> [UInt8] {
		return shrinkIntegral(x)
	}
}

extension UInt16 : Arbitrary {
	public static func arbitrary() -> Gen<UInt16> {
		return sized { n in Gen<UInt16>.pure(UInt16(arc4random_uniform(UInt32(abs(n))))) }
	}

	public static func shrink(x : UInt16) -> [UInt16] {
		return shrinkIntegral(x)
	}
}

extension UInt32 : Arbitrary {
	public static func arbitrary() -> Gen<UInt32> {
		return sized { n in Gen<UInt32>.pure(arc4random_uniform(UInt32(abs(n)))) }
	}

	public static func shrink(x : UInt32) -> [UInt32] {
		return shrinkIntegral(x)
	}
}

extension UInt64 : Arbitrary {
	public static func arbitrary() -> Gen<UInt64> {
		return sized { n in Gen<UInt64>.pure(UInt64(arc4random_uniform(UInt32(abs(n))))) }
	}

	public static func shrink(x : UInt64) -> [UInt64] {
		return shrinkIntegral(x)
	}
}

extension Float : Arbitrary {
	public static func arbitrary() -> Gen<Float> {
		return sized({ n in
			return n == 0 ? Gen<Float>.pure(0.0) : Gen<Float>.pure(Float(-n) + Float(arc4random()) / Float(UINT32_MAX / UInt32((n)*2)))
		})
	}

	public static func shrink(x : Float) -> [Float] {
		return shrinkFloat(x)
	}
}

extension Double : Arbitrary {
	public static func arbitrary() -> Gen<Double> {
		return sized({ n in
			return n == 0 ? Gen<Double>.pure(0.0) : Gen<Double>.pure(Double(-n) + Double(arc4random()) / Double(UINT32_MAX / UInt32(n*2)))
		})
	}

	public static func shrink(x : Double) -> [Double] {
		return shrinkDouble(x)
	}<commit_msg>Inline float and double shrinks<commit_after>//
//  Arbitrary.swift
//  SwiftCheck
//
//  Created by <NAME> on 7/31/14.
//  Copyright (c) 2014 <NAME>. All rights reserved.
//

import Darwin

/// A type that implements random generation and shrinking of values.
///
/// While testing, SwiftCheck will invoke `arbitrary()` a given amount of times (usually 100 if the
/// default settings are used).  During that time, the receiver has an opportunity to call through 
/// to any data or sources of randomness it needs to return what it deems an "Arbitrary" value.
///
/// Shrinking is reduction in the complexity of a tested value to remove noise and present a minimal
/// counterexample when a property fails.  While it may seem counterintuitive, a shrink necessitates
/// returning a list of all possible "smaller" values for SwiftCheck to run through.  As long as
/// each individual value in the returned list is less than or equal to the size of the input value
/// a minimal case should be reached fairly efficiently.
///
/// As an example, take the `ArrayOf` implementation of shrink:
///
/// shrink(ArrayOf([1, 2, 3]))
///	> [[], [2,3], [1,3], [1,2], [0,2,3], [1,0,3], [1,1,3], [1,2,0], [1,2,2]]
///
/// SwiftCheck will search each case 1 by one and continue shrinking until it has reached a case
/// it deems minimal enough to present.
///
/// SwiftCheck implements a number of generators for common STL types for convenience.  If more fine-
/// grained testing is required see `Modifiers.swift` for an example of how to define a "Modifier"
/// type to implement it.
public protocol Arbitrary {
	static func arbitrary() -> Gen<Self>
	static func shrink(Self) -> [Self]
}


extension Bool : Arbitrary {
	public static func arbitrary() -> Gen<Bool> {
		return Gen.pure((arc4random() % 2) == 1)
	}

	public static func shrink(x : Bool) -> [Bool] {
		if x {
			return [false]
		}
		return []
	}
}

extension Int : Arbitrary {
	public static func arbitrary() -> Gen<Int> {
		let sign = ((arc4random() % 2) == 1)
		return sized { n in Gen.pure((sign ? 1 : -1) * Int(arc4random_uniform(UInt32(n)))) }
	}

	public static func shrink(x : Int) -> [Int] {
		return shrinkIntegral(x)
	}
}

extension Int8 : Arbitrary {
	public static func arbitrary() -> Gen<Int8> {
		let sign = ((arc4random() % 2) == 1)
		return sized { n in Gen.pure((sign ? 1 : -1) * Int8(arc4random_uniform(UInt32(n)))) }
	}

	public static func shrink(x : Int8) -> [Int8] {
		return shrinkIntegral(x)
	}
}

extension Int16 : Arbitrary {
	public static func arbitrary() -> Gen<Int16> {
		let sign = ((arc4random() % 2) == 1)
		return sized { n in Gen.pure((sign ? 1 : -1) * Int16(arc4random_uniform(UInt32(n)))) }
	}

	public static func shrink(x : Int16) -> [Int16] {
		return shrinkIntegral(x)
	}
}

extension Int32 : Arbitrary {
	public static func arbitrary() -> Gen<Int32> {
		let sign = ((arc4random() % 2) == 1)
		return sized { n in Gen.pure((sign ? 1 : -1) * Int32(arc4random_uniform(UInt32(n)))) }
	}

	public static func shrink(x : Int32) -> [Int32] {
		return shrinkIntegral(x)
	}
}

extension Int64 : Arbitrary {
	public static func arbitrary() -> Gen<Int64> {
		let sign = ((arc4random() % 2) == 1)
		return sized { n in Gen.pure((sign ? 1 : -1) * Int64(arc4random_uniform(UInt32(n)))) }
	}

	public static func shrink(x : Int64) -> [Int64] {
		return shrinkIntegral(x)
	}
}

extension UInt : Arbitrary {
	public static func arbitrary() -> Gen<UInt> {
		return sized { n in Gen<UInt>.pure(UInt(arc4random_uniform(UInt32(abs(n))))) }
	}

	public static func shrink(x : UInt) -> [UInt] {
		return shrinkIntegral(x)
	}
}

extension UInt8 : Arbitrary {
	public static func arbitrary() -> Gen<UInt8> {
		return sized({ n in
			return sized { n in Gen<UInt8>.pure(UInt8(arc4random_uniform(UInt32(abs(n))))) }
		})
	}

	public static func shrink(x : UInt8) -> [UInt8] {
		return shrinkIntegral(x)
	}
}

extension UInt16 : Arbitrary {
	public static func arbitrary() -> Gen<UInt16> {
		return sized { n in Gen<UInt16>.pure(UInt16(arc4random_uniform(UInt32(abs(n))))) }
	}

	public static func shrink(x : UInt16) -> [UInt16] {
		return shrinkIntegral(x)
	}
}

extension UInt32 : Arbitrary {
	public static func arbitrary() -> Gen<UInt32> {
		return sized { n in Gen<UInt32>.pure(arc4random_uniform(UInt32(abs(n)))) }
	}

	public static func shrink(x : UInt32) -> [UInt32] {
		return shrinkIntegral(x)
	}
}

extension UInt64 : Arbitrary {
	public static func arbitrary() -> Gen<UInt64> {
		return sized { n in Gen<UInt64>.pure(UInt64(arc4random_uniform(UInt32(abs(n))))) }
	}

	public static func shrink(x : UInt64) -> [UInt64] {
		return shrinkIntegral(x)
	}
}

extension Float : Arbitrary {
	public static func arbitrary() -> Gen<Float> {
		return sized({ n in
			return n == 0 ? Gen<Float>.pure(0.0) : Gen<Float>.pure(Float(-n) + Float(arc4random()) / Float(UINT32_MAX / UInt32((n)*2)))
		})
	}

	public static func shrink(x : Float) -> [Float] {
		return unfoldr({ i in
			if i == 0.0 {
				return .None
			}
			let n = i / 2.0
			return .Some((n, n))
		}, initial: x)
	}
}

extension Double : Arbitrary {
	public static func arbitrary() -> Gen<Double> {
		return sized({ n in
			return n == 0 ? Gen<Double>.pure(0.0) : Gen<Double>.pure(Double(-n) + Double(arc4random()) / Double(UINT32_MAX / UInt32(n*2)))
		})
	}

	public static func shrink(x : Double) -> [Double] {
		return unfoldr({ i in
			if i == 0.0 {
				return .None
			}
			let n = i / 2.0
			return .Some((n, n))
		}, initial: x)
	}
{'fraction_non_alphanumeric': 0.14814814814814814, 'fraction_numerical': 0.03582423733557235, 'mean_word_length': 3.5404489623041084, 'pattern_counts': {'":': 0, '<': 53, '<?xml version=': 0, '>': 111, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3364430', 'n_tokens_mistral': 4082, 'n_tokens_neox': 3611, 'n_words': 1172}