-
Notifications
You must be signed in to change notification settings - Fork 708
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Type independent gens for TypedPipe/Execution #1918
Open
dieu
wants to merge
1
commit into
twitter:develop
Choose a base branch
from
dieu:apanasenko/erik-exp
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
61 changes: 61 additions & 0 deletions
61
scalding-core/src/test/scala/com/twitter/scalding/typed/gen/ExecutionGen.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package com.twitter.scalding.typed.gen | ||
|
||
import com.twitter.scalding.Execution | ||
import com.twitter.scalding.typed.TypedPipe | ||
import org.scalacheck.{Cogen, Gen} | ||
|
||
object ExecutionGen { | ||
import TypedPipeGen._ | ||
|
||
private[this] def cogen(t: TypeWith[TypeGen]): Cogen[TypedPipe[t.Type]] = | ||
Cogen[TypedPipe[t.Type]] { pipe: TypedPipe[t.Type] => | ||
pipe.hashCode.toLong | ||
} | ||
|
||
def executionOf(implicit tg: Gen[TypeWith[TypeGen]]): Gen[(Execution[TypedPipe[_]], TypeWith[TypeGen])] = | ||
tg.flatMap { t => | ||
executionOf(t).map(_ -> t) | ||
} | ||
|
||
def executionOf(a: TypeWith[TypeGen])(implicit tg: Gen[TypeWith[TypeGen]]): Gen[Execution[TypedPipe[a.Type]]] = | ||
Gen.delay( | ||
Gen.frequency( | ||
5 -> genFrom(a), | ||
1 -> genMap(a), | ||
1 -> genFlatMap(a), | ||
1 -> tg.flatMap { t => | ||
Gen.oneOf( | ||
genZipped(a, t).map(_.map(_._1)), | ||
genZipped(t, a).map(_.map(_._2)) | ||
) | ||
} | ||
) | ||
) | ||
|
||
def genFrom(a: TypeWith[TypeGen])(implicit tg: Gen[TypeWith[TypeGen]]): Gen[Execution[TypedPipe[a.Type]]] = | ||
pipeOf(a).map(Execution.from(_)) | ||
|
||
def genMap(a: TypeWith[TypeGen])(implicit tg: Gen[TypeWith[TypeGen]]): Gen[Execution[TypedPipe[a.Type]]] = | ||
tg.flatMap { t => | ||
executionOf(t).flatMap { exec => | ||
Gen.function1(pipeOf(a))(cogen(t)).map { f => | ||
exec.map(f) | ||
} | ||
} | ||
} | ||
|
||
def genFlatMap(a: TypeWith[TypeGen])(implicit tg: Gen[TypeWith[TypeGen]]): Gen[Execution[TypedPipe[a.Type]]] = | ||
tg.flatMap { t => | ||
executionOf(t).flatMap { exec => | ||
Gen.function1(executionOf(a))(cogen(t)).map { f => | ||
exec.flatMap(f) | ||
} | ||
} | ||
} | ||
|
||
def genZipped(l: TypeWith[TypeGen], r: TypeWith[TypeGen])(implicit tg: Gen[TypeWith[TypeGen]]): Gen[Execution[(TypedPipe[l.Type], TypedPipe[r.Type])]] = | ||
for { | ||
le <- executionOf(l) | ||
re <- executionOf(r) | ||
} yield le.zip(re) | ||
} |
25 changes: 25 additions & 0 deletions
25
scalding-core/src/test/scala/com/twitter/scalding/typed/gen/StdGen.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package com.twitter.scalding.typed.gen | ||
|
||
import org.scalacheck.Gen | ||
|
||
object StdGen { | ||
implicit val stringGen: Gen[String] = Gen.alphaStr | ||
|
||
implicit val charGen: Gen[Char] = Gen.alphaChar | ||
|
||
implicit val booleanGen: Gen[Boolean] = Gen.oneOf(true, false) | ||
|
||
implicit val unitGen: Gen[Unit] = Gen.const(()) | ||
|
||
implicit val byteGen: Gen[Byte] = Gen.chooseNum(Byte.MinValue, Byte.MaxValue) | ||
|
||
implicit val shortGen: Gen[Short] = Gen.chooseNum(Short.MinValue, Short.MaxValue) | ||
|
||
implicit val intGen: Gen[Int] = Gen.chooseNum(Int.MinValue, Int.MaxValue) | ||
|
||
implicit val longGen: Gen[Long] = Gen.chooseNum(Long.MinValue, Long.MaxValue) | ||
|
||
implicit val floatGen: Gen[Float] = Gen.chooseNum(Float.MinValue, Float.MaxValue) | ||
|
||
implicit val doubleGen: Gen[Double] = Gen.chooseNum(Double.MinValue, Double.MaxValue) | ||
} |
17 changes: 17 additions & 0 deletions
17
scalding-core/src/test/scala/com/twitter/scalding/typed/gen/StdSemigroup.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package com.twitter.scalding.typed.gen | ||
|
||
import com.twitter.algebird.Semigroup | ||
|
||
object StdSemigroup { | ||
implicit val byteGroup: Semigroup[Byte] = Semigroup.from { case (l, r) => | ||
implicitly[Numeric[Byte]].plus(l, r) | ||
} | ||
|
||
implicit val charGroup: Semigroup[Char] = Semigroup.from { case (l, r) => | ||
implicitly[Numeric[Char]].plus(l, r) | ||
} | ||
|
||
implicit val stringGroup: Semigroup[String] = Semigroup.from { case (l, r) => | ||
l + r | ||
} | ||
} |
59 changes: 59 additions & 0 deletions
59
scalding-core/src/test/scala/com/twitter/scalding/typed/gen/TypeGen.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package com.twitter.scalding.typed.gen | ||
|
||
|
||
import com.twitter.algebird.Semigroup | ||
import com.twitter.scalding.TupleConverter | ||
import org.scalacheck.{Cogen, Gen} | ||
|
||
trait TypeGen[A] { | ||
val gen: Gen[A] | ||
val cogen: Cogen[A] | ||
val ordering: Ordering[A] | ||
val semigroup: Semigroup[A] | ||
val tupleConverter: TupleConverter[A] | ||
} | ||
|
||
object TypeGen { | ||
import StdGen._ | ||
import StdSemigroup._ | ||
|
||
def apply[A](g: Gen[A], c: Cogen[A], o: Ordering[A], s: Semigroup[A], t: TupleConverter[A]): TypeGen[A] = | ||
new TypeGen[A] { | ||
val gen: Gen[A] = g | ||
val cogen: Cogen[A] = c | ||
val ordering: Ordering[A] = o | ||
val semigroup: Semigroup[A] = s | ||
val tupleConverter: TupleConverter[A] = t | ||
} | ||
|
||
def apply[A, B](a: TypeGen[A], b: TypeGen[B]): TypeGen[(A, B)] = | ||
new TypeGen[(A, B)] { | ||
val gen: Gen[(A, B)] = Gen.zip(a.gen, b.gen) | ||
val cogen: Cogen[(A, B)] = Cogen.tuple2(a.cogen, b.cogen) | ||
val ordering: Ordering[(A, B)] = Ordering.Tuple2(a.ordering, b.ordering) | ||
val semigroup: Semigroup[(A, B)] = Semigroup.semigroup2(a.semigroup, b.semigroup) | ||
val tupleConverter: TupleConverter[(A, B)] = | ||
TupleConverter.build(a.tupleConverter.arity + b.tupleConverter.arity) { te => | ||
val ta = a.tupleConverter.apply(te) | ||
val tb = b.tupleConverter.apply(te) | ||
(ta, tb) | ||
} | ||
} | ||
|
||
implicit def typeGen[A: Gen: Cogen: Ordering: Semigroup: TupleConverter]: TypeGen[A] = | ||
TypeGen(implicitly, implicitly, implicitly, implicitly, implicitly) | ||
|
||
implicit val std: Gen[TypeWith[TypeGen]] = | ||
Gen.oneOf( | ||
TypeWith[Unit, TypeGen], | ||
TypeWith[Boolean, TypeGen], | ||
TypeWith[Byte, TypeGen], | ||
TypeWith[Char, TypeGen], | ||
TypeWith[Short, TypeGen], | ||
TypeWith[Int, TypeGen], | ||
TypeWith[Long, TypeGen], | ||
TypeWith[Float, TypeGen], | ||
TypeWith[Double, TypeGen], | ||
TypeWith[String, TypeGen] | ||
) | ||
} |
16 changes: 16 additions & 0 deletions
16
scalding-core/src/test/scala/com/twitter/scalding/typed/gen/TypeWith.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package com.twitter.scalding.typed.gen | ||
|
||
sealed abstract class TypeWith[+Ev[_]] { | ||
type Type | ||
def evidence: Ev[Type] | ||
} | ||
|
||
object TypeWith { | ||
type Aux[A, Ev[_]] = TypeWith[Ev] { type Type = A } | ||
|
||
def apply[A, Ev[_]](implicit eva: Ev[A]): Aux[A, Ev] = | ||
new TypeWith[Ev] { | ||
type Type = A | ||
def evidence: Ev[Type] = eva | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is really nice!