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

/// 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,
/// and is not a duplicate of the input value, a minimal case should be reached fairly efficiently.
/// Shrinking is an optional extension of normal testing.  If no implementation of `shrink` is
/// provided, SwiftCheck will default to an empty one.
///
/// As an example, take the `ArrayOf` implementation of shrink:
///
/// Arbitrary.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 one-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 {
	/// The generator for this particular type.
	///
	/// This function should call out to any sources of randomness or state necessary to generate
	/// values.  It should not, however, be written as a deterministic function.  If such a
	/// generator is needed, combinators are provided in `Gen.swift`.
	static var arbitrary : Gen<Self> { get }

	/// An optional shrinking function.  If this function goes unimplemented, it is the same as
	/// returning the empty list.
	///
	/// Shrunken values must be less than or equal to the "size" of the original type but never the
	/// same as the value provided to this function (or a loop will form in the shrinker).  It is
	/// recommended that they be presented smallest to largest to speed up the overall shrinking
	/// process.
	static func shrink(_ : Self) -> [Self]
}

extension Arbitrary {
	/// The implementation of a shrink that returns no alternatives.
	public static func shrink(_ : Self) -> [Self] {
		return []
	}
}

extension IntegerType {
	/// Shrinks any IntegerType.
	public var shrinkIntegral : [Self] {
		return unfoldr({ i in
			if i <= 0 {
				return .None
			}
			let n = i / 2
			return .Some((n, n))
		}, initial: self < 0 ? (self * -1) : self)
	}
}

extension Bool : Arbitrary {
	public static var arbitrary : Gen<Bool> {
		return Gen<Bool>.choose((false, true))
	}

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

extension Int : Arbitrary {
	public static var arbitrary : Gen<Int> {
		return Gen.sized { n in
			return Gen<Int>.choose((-n, n))
		}
	}

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

//private func inBounds<A : IntegerType>(fi : (Int -> A)) -> Gen<A.IntegerLiteralType> -> Gen<A> {
//	return { g in
//		return fi <^> g.suchThat { x in
//			return (fi(x) as! A.IntegerLiteralType) == x
//		}
//	}
//}

extension Int8 : Arbitrary {
	public static var arbitrary : Gen<Int8> {
		return Gen.sized { n in
			return Gen<Int8>.choose((Int8(truncatingBitPattern: -n), Int8(truncatingBitPattern: n)))
		}
	}

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

extension Int16 : Arbitrary {
	public static var arbitrary : Gen<Int16> {
		return Gen.sized { n in
			return Gen<Int16>.choose((Int16(truncatingBitPattern: -n), Int16(truncatingBitPattern: n)))
		}
	}

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

extension Int32 : Arbitrary {
	public static var arbitrary : Gen<Int32> {
		return Gen.sized { n in
			return Gen<Int32>.choose((Int32(truncatingBitPattern: -n), Int32(truncatingBitPattern: n)))
		}<commit_msg>Remove extra copy of unneeded function<commit_after>//
//  Arbitrary.swift
//  SwiftCheck
//
//  Created by <NAME> on 7/31/14.
//  Copyright (c) 2015 TypeLift. All rights reserved.
//

/// 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,
/// and is not a duplicate of the input value, a minimal case should be reached fairly efficiently.
/// Shrinking is an optional extension of normal testing.  If no implementation of `shrink` is
/// provided, SwiftCheck will default to an empty one.
///
/// As an example, take the `ArrayOf` implementation of shrink:
///
/// Arbitrary.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 one-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 {
	/// The generator for this particular type.
	///
	/// This function should call out to any sources of randomness or state necessary to generate
	/// values.  It should not, however, be written as a deterministic function.  If such a
	/// generator is needed, combinators are provided in `Gen.swift`.
	static var arbitrary : Gen<Self> { get }

	/// An optional shrinking function.  If this function goes unimplemented, it is the same as
	/// returning the empty list.
	///
	/// Shrunken values must be less than or equal to the "size" of the original type but never the
	/// same as the value provided to this function (or a loop will form in the shrinker).  It is
	/// recommended that they be presented smallest to largest to speed up the overall shrinking
	/// process.
	static func shrink(_ : Self) -> [Self]
}

extension Arbitrary {
	/// The implementation of a shrink that returns no alternatives.
	public static func shrink(_ : Self) -> [Self] {
		return []
	}
}

extension IntegerType {
	/// Shrinks any IntegerType.
	public var shrinkIntegral : [Self] {
		return unfoldr({ i in
			if i <= 0 {
				return .None
			}
			let n = i / 2
			return .Some((n, n))
		}, initial: self < 0 ? (self * -1) : self)
	}
}

extension Bool : Arbitrary {
	public static var arbitrary : Gen<Bool> {
		return Gen<Bool>.choose((false, true))
	}

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

extension Int : Arbitrary {
	public static var arbitrary : Gen<Int> {
		return Gen.sized { n in
			return Gen<Int>.choose((-n, n))
		}
	}

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

extension Int8 : Arbitrary {
	public static var arbitrary : Gen<Int8> {
		return Gen.sized { n in
			return Gen<Int8>.choose((Int8(truncatingBitPattern: -n), Int8(truncatingBitPattern: n)))
		}
	}

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

extension Int16 : Arbitrary {
	public static var arbitrary : Gen<Int16> {
		return Gen.sized { n in
			return Gen<Int16>.choose((Int16(truncatingBitPattern: -n), Int16(truncatingBitPattern: n)))
		}
	}

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

extension Int32 : Arbitrary {
	public static var arbitrary : Gen<Int32> {
		return Gen.sized { n in
			return Gen<Int32>.choose((Int32(truncatingBitPattern: -n), Int32(truncatingBitPattern: n)))
		}
{'fraction_non_alphanumeric': 0.11593361958918418, 'fraction_numerical': 0.016479053034698852, 'mean_word_length': 3.216242661448141, 'pattern_counts': {'":': 0, '<': 35, '<?xml version=': 0, '>': 48, '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': '5417906', 'n_tokens_mistral': 2837, 'n_tokens_neox': 2577, 'n_words': 1100}