Skip to content

Commit

Permalink
Make map and except apply transformer/filter to ALL value providers
Browse files Browse the repository at this point in the history
  • Loading branch information
IlyaMuravjov committed Sep 11, 2023
1 parent 4e73137 commit 9a7d2ef
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 20 deletions.
25 changes: 10 additions & 15 deletions utbot-fuzzing/src/main/kotlin/org/utbot/fuzzing/Providers.kt
Original file line number Diff line number Diff line change
Expand Up @@ -92,26 +92,16 @@ fun interface ValueProvider<T, R, D : Description<T>> {
}

/**
* Removes `anotherValueProviders<T, R` from current one.
* Removes providers matching [filter] from the current one.
*/
fun except(filter: (ValueProvider<T, R, D>) -> Boolean): ValueProvider<T, R, D> {
return if (this is Combined) {
Combined(providers.map { it.unwrapIfFallback() }.filterNot(filter))
} else {
Combined(if (filter(unwrapIfFallback())) emptyList() else listOf(this))
}
}
fun except(filter: (ValueProvider<T, R, D>) -> Boolean): ValueProvider<T, R, D> =
map { if (filter(it)) Combined(emptyList()) else it }

/**
* Applies [transform] for current provider
*/
fun map(transform: (ValueProvider<T, R, D>) -> ValueProvider<T, R, D>): ValueProvider<T, R, D> {
return if (this is Combined) {
Combined(providers.map(transform))
} else {
transform(this)
}
}
fun map(transform: (ValueProvider<T, R, D>) -> ValueProvider<T, R, D>): ValueProvider<T, R, D> =
transform(this)

/**
* Uses fallback value provider in case when 'this' one failed to generate any value.
Expand Down Expand Up @@ -167,6 +157,8 @@ fun interface ValueProvider<T, R, D : Description<T>> {
}
}

override fun map(transform: (ValueProvider<T, R, D>) -> ValueProvider<T, R, D>): ValueProvider<T, R, D> =
transform(Fallback(provider.map(transform), fallback.map(transform)))
}

/**
Expand Down Expand Up @@ -200,6 +192,9 @@ fun interface ValueProvider<T, R, D : Description<T>> {
}
}
}

override fun map(transform: (ValueProvider<T, R, D>) -> ValueProvider<T, R, D>): ValueProvider<T, R, D> =
transform(Combined(providers.map { it.map(transform) }))
}

companion object {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,6 @@ abstract class ValueProviderDecorator<T, R, D : Description<T>>(
override fun generate(description: D, type: T): Sequence<Seed<T, R>> =
delegate.generate(description, type)

override fun except(filter: (ValueProvider<T, R, D>) -> Boolean): ValueProvider<T, R, D> {
val res = wrap(delegate.except(filter))
return if (filter(res)) ValueProvider.of(emptyList()) else res
}

override fun map(transform: (ValueProvider<T, R, D>) -> ValueProvider<T, R, D>): ValueProvider<T, R, D> =
transform(wrap(delegate.map(transform)))
}

0 comments on commit 9a7d2ef

Please sign in to comment.