MarvinJWendt / testza

Full-featured test framework for Go! Assertions, fuzzing, input testing, output capturing, and much more! 🍕
MIT License
418 stars 21 forks source link

Make `FuzzUtilLimit` return random pieces of input set #154

Closed MarvinJWendt closed 1 year ago

MarvinJWendt commented 2 years ago

Make FuzzUtilLimit return random pieces of input set.

KarolosLykos commented 1 year ago

Hi @MarvinJWendt I was thinking of trying this one what do you think of something like that.

func FuzzUtilLimit[setType any](testSet []setType, limit int) []setType {
    seen := map[int]bool{}

    if len(testSet) <= limit {
        return testSet
    }

    if limit <= 0 {
        return []setType{}
    }

    var result []setType

    counter := 0

    for counter < limit {
        index := rand.Intn(len(testSet) - 1)
        if !seen[index] {
            seen[index] = true
            result = append(result, testSet[index])
            counter++
        }

    }

    return result
}
MarvinJWendt commented 1 year ago

Hi @KarolosLykos, this would definitely work, but I am not sure if the performance might be better with rand.Shuffle. We could then return a sub-slice of the shuffled slice (shuffledTestSet[:limit]). If you want to benchmark both approaches, feel free to do so, otherwise I'll do it in the next weeks and get back to you with the result :)

KarolosLykos commented 1 year ago

Hey @MarvinJWendt , I ran some benchmarks and rand.Shuffle is faster. Look at the benchmark tests and tell me what you think? benchmarks

I also included a PR with the proposal.

MarvinJWendt commented 1 year ago

Fixed in #200 and released with v0.5.2. Thanks!