<commit_before>//
//  SwiftRandomTests.swift
//  SwiftRandomTests
//
//  Created by <NAME> on 10/3/16.
//
//

import XCTest

@testable import SwiftRandom

/// Tests above `SwiftRandom` module.
class SwiftRandomTests: XCTestCase {
    /// Tests if all static methods are working.
    func testStaticMethods() {
        XCTAssertNotNil(Randoms.randomInt(10, 20))
        XCTAssertNotNil(Randoms.randomCGFloat())
        XCTAssertNotNil(Randoms.randomPercentageisOver(70))
        XCTAssertNotNil(Randoms.randomBool())
        XCTAssertNotNil(Randoms.randomDateWithinDaysBeforeToday(5))
        XCTAssertNotNil(Randoms.randomDate())
        XCTAssertNotNil(Randoms.randomColor())
        XCTAssertNotNil(Randoms.randomFakeName())
        XCTAssertNotNil(Randoms.randomFakeGender())
        XCTAssertNotNil(Randoms.randomFakeConversation())
        XCTAssertNotNil(Randoms.randomFakeTitle())
        XCTAssertNotNil(Randoms.randomFakeTag())
        XCTAssertNotNil(Randoms.randomFakeNameAndEnglishHonorific())
        XCTAssertNotNil(Randoms.randomNSURL())
    }

    /// Tests for extensions applied directly above Swift types.
    func testTypeExtensions() {
        XCTAssertNotNil(Int.random())
        XCTAssertNotNil(Date.random())
        XCTAssertNotNil(Date.randomWithinDaysBeforeToday(7))
        XCTAssertNotNil(CGFloat.random())
        XCTAssertNotNil(Float.random())
        XCTAssertNotNil(Double.random())
        XCTAssertNotNil(UIColor.random())
        XCTAssertNotNil(URL.random())
    }

    /// Tests for SwiftRandom when applied above a list.
    func testListItemRandom() {
        let entry = ["hello", "world"]

        for _ in 0...10 {
            XCTAssertNotNil(entry[0..<entry.count].randomItem())
            XCTAssertNotNil(entry.randomItem())
        }
    }

    /// Tests using SwiftRandom above a `Range` of Int.
    func testRandomIntRange() {
        for _ in 0...10 {
            let randomUntilTen = Int.random(0...10)

            XCTAssertGreaterThanOrEqual(randomUntilTen, 0)
            XCTAssertLessThanOrEqual(randomUntilTen, 10)

            let randomLessTen = Int.random(0..<10)

            XCTAssertGreaterThanOrEqual(randomLessTen, 0)
            XCTAssertLessThan(randomLessTen, 10)
        }
    }
	
    /// Tests generating random a `String` of a specified length.
    func testRandomStringOfLength() {
        let precision = 100
        let length = 128
        
        var all: [String] = []
        
        (1...precision).forEach { _ in
            let random: String = .random(ofLength: length)
            XCTAssertEqual(random.characters.count, length)
            
            if all.contains(random) {
                // this is very unlikely to happen with precisions of 100 or over and lengths of 128 or over.
                // if this happens, it is likely a bug in the random string generator.
                XCTFail("Random string collision")
            }
            all.append(random)
        }
        
        XCTAssertEqual(String.random(ofLength: -1), "")
        XCTAssertEqual(String.random(ofLength: 0), "")
    }
    
    /// Tests generating a random `String` of variable length.
    func testRandomStringOfVariableLength() {
        let precision = 100
        let minimumLength = 128
        let maximumLength = 256
        
        var all: [String] = []
        
        (1...precision).forEach { _ in
            let random: String = .random(minimumLength: minimumLength, maximumLength: maximumLength)
            XCTAssertLessThanOrEqual(random.characters.count, maximumLength)
            XCTAssertGreaterThanOrEqual(random.characters.count, minimumLength)
            
            if all.contains(random) {
                // this is very unlikely to happen with precisions of 100 or over and lengths of 128 or over.
                // if this happens, it is likely a bug in the random string generator.
                XCTFail("Random string collision")
            }
            all.append(random)
        }
        
        XCTAssertEqual(String.random(minimumLength: -1, maximumLength: -2), "")
        XCTAssertEqual(String.random(minimumLength: 1, maximumLength: -2), "")
        XCTAssertEqual(String.random(minimumLength: -1, maximumLength: 2), "")
        XCTAssertEqual(String.random(minimumLength: 0, maximumLength: 0), "")
        XCTAssertEqual(String.random(minimumLength: -1, maximumLength: 0), "")
        XCTAssertEqual(String.random(minimumLength: 0, maximumLength: -2), "")
        XCTAssertEqual(String.random(minimumLength: 10, maximumLength: 5), "")
        
		XCTAssertEqual(String.random(minimumLength: 5, maximumLength: 5).characters.count, 5)
    }
    
    /// Tests generating a string with a character set specified
    func testRandomStringWithPossibleCharacters() {
        let precision = 100
        let length = 3
        let allowedCharacters = "ab"
        
        (1...precision).forEach { _ in
            let random: String = .random(withCharactersInString: "ab", ofLength: length)
            for c in random.characters {
                XCTAssertTrue(allowedCharacters.characters.contains(c))
            }
        }
    }

    /// Tests using async way to get a random gravatar.
    func testRandomGravatar() {
        let async = expectation(description: "Randoms.randomGravatar")

        Randoms.randomGravatar { (image, error) in
            XCTAssertNotNil(image)
            XCTAssertNil(error)

            async.fulfill()<commit_msg>Replace tabs with spaces<commit_after>//
//  SwiftRandomTests.swift
//  SwiftRandomTests
//
//  Created by <NAME> on 10/3/16.
//
//

import XCTest

@testable import SwiftRandom

/// Tests above `SwiftRandom` module.
class SwiftRandomTests: XCTestCase {
    /// Tests if all static methods are working.
    func testStaticMethods() {
        XCTAssertNotNil(Randoms.randomInt(10, 20))
        XCTAssertNotNil(Randoms.randomCGFloat())
        XCTAssertNotNil(Randoms.randomPercentageisOver(70))
        XCTAssertNotNil(Randoms.randomBool())
        XCTAssertNotNil(Randoms.randomDateWithinDaysBeforeToday(5))
        XCTAssertNotNil(Randoms.randomDate())
        XCTAssertNotNil(Randoms.randomColor())
        XCTAssertNotNil(Randoms.randomFakeName())
        XCTAssertNotNil(Randoms.randomFakeGender())
        XCTAssertNotNil(Randoms.randomFakeConversation())
        XCTAssertNotNil(Randoms.randomFakeTitle())
        XCTAssertNotNil(Randoms.randomFakeTag())
        XCTAssertNotNil(Randoms.randomFakeNameAndEnglishHonorific())
        XCTAssertNotNil(Randoms.randomNSURL())
    }

    /// Tests for extensions applied directly above Swift types.
    func testTypeExtensions() {
        XCTAssertNotNil(Int.random())
        XCTAssertNotNil(Date.random())
        XCTAssertNotNil(Date.randomWithinDaysBeforeToday(7))
        XCTAssertNotNil(CGFloat.random())
        XCTAssertNotNil(Float.random())
        XCTAssertNotNil(Double.random())
        XCTAssertNotNil(UIColor.random())
        XCTAssertNotNil(URL.random())
    }

    /// Tests for SwiftRandom when applied above a list.
    func testListItemRandom() {
        let entry = ["hello", "world"]

        for _ in 0...10 {
            XCTAssertNotNil(entry[0..<entry.count].randomItem())
            XCTAssertNotNil(entry.randomItem())
        }
    }

    /// Tests using SwiftRandom above a `Range` of Int.
    func testRandomIntRange() {
        for _ in 0...10 {
            let randomUntilTen = Int.random(0...10)

            XCTAssertGreaterThanOrEqual(randomUntilTen, 0)
            XCTAssertLessThanOrEqual(randomUntilTen, 10)

            let randomLessTen = Int.random(0..<10)

            XCTAssertGreaterThanOrEqual(randomLessTen, 0)
            XCTAssertLessThan(randomLessTen, 10)
        }
    }
	
    /// Tests generating random a `String` of a specified length.
    func testRandomStringOfLength() {
        let precision = 100
        let length = 128
        
        var all: [String] = []
        
        (1...precision).forEach { _ in
            let random: String = .random(ofLength: length)
            XCTAssertEqual(random.characters.count, length)
            
            if all.contains(random) {
                // this is very unlikely to happen with precisions of 100 or over and lengths of 128 or over.
                // if this happens, it is likely a bug in the random string generator.
                XCTFail("Random string collision")
            }
            all.append(random)
        }
        
        XCTAssertEqual(String.random(ofLength: -1), "")
        XCTAssertEqual(String.random(ofLength: 0), "")
    }
    
    /// Tests generating a random `String` of variable length.
    func testRandomStringOfVariableLength() {
        let precision = 100
        let minimumLength = 128
        let maximumLength = 256
        
        var all: [String] = []
        
        (1...precision).forEach { _ in
            let random: String = .random(minimumLength: minimumLength, maximumLength: maximumLength)
            XCTAssertLessThanOrEqual(random.characters.count, maximumLength)
            XCTAssertGreaterThanOrEqual(random.characters.count, minimumLength)
            
            if all.contains(random) {
                // this is very unlikely to happen with precisions of 100 or over and lengths of 128 or over.
                // if this happens, it is likely a bug in the random string generator.
                XCTFail("Random string collision")
            }
            all.append(random)
        }
        
        XCTAssertEqual(String.random(minimumLength: -1, maximumLength: -2), "")
        XCTAssertEqual(String.random(minimumLength: 1, maximumLength: -2), "")
        XCTAssertEqual(String.random(minimumLength: -1, maximumLength: 2), "")
        XCTAssertEqual(String.random(minimumLength: 0, maximumLength: 0), "")
        XCTAssertEqual(String.random(minimumLength: -1, maximumLength: 0), "")
        XCTAssertEqual(String.random(minimumLength: 0, maximumLength: -2), "")
        XCTAssertEqual(String.random(minimumLength: 10, maximumLength: 5), "")
        
        XCTAssertEqual(String.random(minimumLength: 5, maximumLength: 5).characters.count, 5)
    }
    
    /// Tests generating a string with a character set specified
    func testRandomStringWithPossibleCharacters() {
        let precision = 100
        let length = 3
        let allowedCharacters = "ab"
        
        (1...precision).forEach { _ in
            let random: String = .random(withCharactersInString: "ab", ofLength: length)
            for c in random.characters {
                XCTAssertTrue(allowedCharacters.characters.contains(c))
            }
        }
    }

    /// Tests using async way to get a random gravatar.
    func testRandomGravatar() {
        let async = expectation(description: "Randoms.randomGravatar")

        Randoms.randomGravatar { (image, error) in
            XCTAssertNotNil(image)
            XCTAssertNil(error)

            async.fulfill()
{'fraction_non_alphanumeric': 0.09756542354335734, 'fraction_numerical': 0.01568341387799763, 'mean_word_length': 2.3296903460837886, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 5, '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': '7645202', 'n_tokens_mistral': 3216, 'n_tokens_neox': 2987, 'n_words': 680}