Skip to content

Commit 733a035

Browse files
committed
Starting to fill in code generators. Have basic correctness test several.
1 parent 021490b commit 733a035

File tree

2 files changed

+41
-3
lines changed

2 files changed

+41
-3
lines changed

benchmark/src/main/scala/bench/CodeGen.scala

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@ import scala.util._
44
import scala.util.control.NonFatal
55

66
object Generator {
7-
val names = "arr ish lst ils que stm trs vec arb ars ast mhs lhs prq muq wra jix jln".split(' ')
7+
val annotated = "arr ish lst* ils* que* stm* trs* vec arb ars ast* mhs lhs* prq* muq* wra jix jln".split(' ')
8+
val names = annotated.map(_.takeWhile(_.isLetter))
9+
val nojnames = names.filterNot(_ startsWith "j")
810

911
def writeTo(f: java.io.File)(pr: (String => Unit) => Unit): Either[Throwable, Unit] = {
1012
try {
@@ -20,15 +22,39 @@ object Generator {
2022
def sayArrayI(oa: Option[Array[Int]]) = oa match { case Some(a) => a.mkString("Array(", ",", ")"); case _ => "" }
2123

2224
def agreement(target: java.io.File, sizes: Option[Array[Int]] = None) {
25+
val q = "\""
2326
if (target.exists) throw new java.io.IOException("Generator will not write to existing file: " + target.getPath)
2427
writeTo(target){ pr =>
2528
pr("""package bench.test""")
2629
pr("""""")
30+
pr("""import bench.generate._, bench.operate._, bench.generate.EnableIterators._""")
31+
pr("""import scala.compat.java8.StreamConverters._""")
32+
pr("""""")
2733
pr("""object Agreement {""")
2834
pr(""" def run() {""")
2935
pr(""" val wrong = new collection.mutable.ArrayBuffer[String]""")
30-
pr(""" def check[A](a1: A, a2: A, msg: String) = if (a1 != a2) wrong += msg""")
36+
pr(""" def check[A](a1: A, a2: => A, msg: String) {""")
37+
pr(""" var t = System.nanoTime""")
38+
pr(""" if (!CloseEnough(a1, { val ans = a2; t = System.nanoTime - t; ans})) wrong += msg""")
39+
pr(""" if (t > 2000000000) wrong += "Slow " + msg""")
40+
pr(""" }""")
3141
pr( s" val x = new bench.generate.Things(${sayArrayI(sizes)})" )
42+
pr(""" for (i <- 0 until x.N) {""")
43+
pr(""" val si = OnInt.sum(x.arr.cI(i))""")
44+
nojnames.tail.foreach{ n =>
45+
pr( s" check(si, OnInt.sum(x.$n.cI(i)), ${q}cI sum $n ${q}+i.toString)")
46+
}
47+
nojnames.foreach{ n =>
48+
pr( s" check(si, OnInt.sum(x.$n.iI(i)), ${q}iI sum $n ${q}+i.toString)")
49+
}
50+
annotated.foreach{ m =>
51+
val n = m.takeWhile(_.isLetter)
52+
val c = if (m contains "*") "ssI" else "spI"
53+
pr( s" check(si, OnInt.sum(x.$n.$c(i)), ${q}$c sum $n ${q}+i.toString)")
54+
}
55+
pr( s" }")
56+
pr(""" wrong.foreach(println)""")
57+
pr(""" if (wrong.nonEmpty) sys.exit(1) """)
3258
pr(""" }""")
3359
pr("""}""")
3460
} match {

benchmark/src/main/scala/bench/CollectionSource.scala

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,17 @@ package object generate {
214214
}
215215
}
216216

217+
object EnableIterators {
218+
implicit val iterableIntToIterator: (Iterable[Int] => Iterator[Int]) = _.iterator
219+
implicit val iterableStringToIterator: (Iterable[String] => Iterator[String]) = _.iterator
220+
implicit val arrayIntToIterator: (Array[Int] => Iterator[Int]) = (a: Array[Int]) => new Iterator[Int] {
221+
private[this] var i = 0
222+
def hasNext = i < a.length
223+
def next = if (hasNext) { var ans = a(i); i += 1; ans } else throw new NoSuchElementException(i.toString)
224+
}
225+
implicit val arrayStringToIterator: (Array[String] => Iterator[String]) = _.iterator
226+
}
227+
217228
class ArrThings(val sizes: Array[Int]) extends AbstractThings[Array]("Array") {}
218229

219230
class IshThings(val sizes: Array[Int]) extends AbstractThings[collection.immutable.HashSet]("immutable.HashSet") {}
@@ -250,7 +261,8 @@ package object generate {
250261

251262
class JlnThings(val sizes: Array[Int]) extends AbstractThings[java.util.LinkedList]("java.util.LinkedList") {}
252263

253-
class Things(sizes: Array[Int] = Array(0, 1, 2, 5, 7, 15, 16, 32, 33, 64, 129, 256, 1023, 2914, 7151, 50000, 200000, 1000000)) {
264+
class Things(sizes: Array[Int] = Array(0, 1, 2, 5, 7, 15, 16, 32, 33, 64, 129, 256, 1023, 2914, 7151, 20000, 50000, 200000)) {
265+
def N = sizes.length
254266
lazy val arr = new ArrThings(sizes)
255267
lazy val ish = new IshThings(sizes)
256268
lazy val lst = new LstThings(sizes)

0 commit comments

Comments
 (0)