commit 2af14442ee7e9406446056f0ad2e8cf3cb752c89 Author: Timothée Floure Date: Tue Sep 24 11:56:48 2019 +0200 Import funsets handout diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..a35362b --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,8 @@ +{ + "dotty": { + "trace": { + "remoteTracingUrl": "wss://lamppc36.epfl.ch/dotty-remote-tracer/upload/lsp.log", + "server": { "format": "JSON", "verbosity": "verbose" } + } + } +} diff --git a/build.sbt b/build.sbt new file mode 100644 index 0000000..324e60b --- /dev/null +++ b/build.sbt @@ -0,0 +1,12 @@ +course := "progfun1" +assignment := "funsets" +name := course.value + "-" + assignment.value +testSuite := "funsets.FunSetSuite" + +scalaVersion := "0.19.0-bin-20190918-dd68eb8-NIGHTLY" + +scalacOptions ++= Seq("-language:implicitConversions", "-deprecation") + +libraryDependencies += "com.novocode" % "junit-interface" % "0.11" % Test + +testOptions in Test += Tests.Argument(TestFrameworks.JUnit, "-a", "-v", "-s") diff --git a/grading-tests.jar b/grading-tests.jar new file mode 100644 index 0000000..26d869e Binary files /dev/null and b/grading-tests.jar differ diff --git a/project/MOOCSettings.scala b/project/MOOCSettings.scala new file mode 100644 index 0000000..80e34b8 --- /dev/null +++ b/project/MOOCSettings.scala @@ -0,0 +1,23 @@ +package ch.epfl.lamp + +import sbt._ +import sbt.Keys._ + +/** + * Settings shared by all assignments, reused in various tasks. + */ +object MOOCSettings extends AutoPlugin { + + object autoImport { + val course = SettingKey[String]("course") + val assignment = SettingKey[String]("assignment") + val testSuite = SettingKey[String]("testSuite") + val options = SettingKey[Map[String, Map[String, String]]]("options") + } + + override def trigger = allRequirements + + override val projectSettings: Seq[Def.Setting[_]] = Seq( + parallelExecution in Test := false + ) +} diff --git a/project/StudentTasks.scala b/project/StudentTasks.scala new file mode 100644 index 0000000..587ba85 --- /dev/null +++ b/project/StudentTasks.scala @@ -0,0 +1,323 @@ +package ch.epfl.lamp + +import sbt._ +import Keys._ + +// import scalaj.http._ +import java.io.{File, FileInputStream, IOException} +import java.nio.file.FileSystems +import org.apache.commons.codec.binary.Base64 +// import play.api.libs.json.{Json, JsObject, JsPath} +import scala.util.{Failure, Success, Try} + +import MOOCSettings.autoImport._ + +case class AssignmentInfo( + key: String, + itemId: String, + premiumItemId: Option[String], + partId: String +) + +/** + * Provides tasks for submitting the assignment + */ +object StudentTasks extends AutoPlugin { + + object autoImport { + val assignmentInfo = SettingKey[AssignmentInfo]("assignmentInfo") + + val packageSourcesOnly = TaskKey[File]("packageSourcesOnly", "Package the sources of the project") + val packageBinWithoutResources = TaskKey[File]("packageBinWithoutResources", "Like packageBin, but without the resources") + val packageSubmissionZip = TaskKey[File]("packageSubmissionZip") + val packageSubmission = inputKey[Unit]("package solution as an archive file") + val runGradingTests = taskKey[Unit]("run black-box tests used for final grading") + } + + + import autoImport._ + + override lazy val projectSettings = Seq( + packageSubmissionSetting, + // submitSetting, // FIXME: restore assignmentInfo setting on assignments + runGradingTestsSettings, + + fork := true, + connectInput in run := true, + outputStrategy := Some(StdoutOutput) + ) ++ packageSubmissionZipSettings + + lazy val runGradingTestsSettings = runGradingTests := { + val testSuiteJar = "grading-tests.jar" + if (!new File(testSuiteJar).exists) { + throw new MessageOnlyException(s"Could not find tests JarFile: $testSuiteJar") + } + + val classPath = s"${(Test / dependencyClasspath).value.map(_.data).mkString(File.pathSeparator)}${File.pathSeparator}$testSuiteJar" + val junitProcess = + Fork.java.fork( + ForkOptions(), + "-cp" :: classPath :: + "org.junit.runner.JUnitCore" :: + (Test / testSuite).value :: + Nil + ) + + // Wait for tests to complete. + junitProcess.exitValue() + } + + + /** ********************************************************** + * SUBMITTING A SOLUTION TO COURSERA + */ + + val packageSubmissionZipSettings = Seq( + packageSubmissionZip := { + val submission = crossTarget.value / "submission.zip" + val sources = (packageSourcesOnly in Compile).value + val binaries = (packageBinWithoutResources in Compile).value + IO.zip(Seq(sources -> "sources.zip", binaries -> "binaries.jar"), submission) + submission + }, + artifactClassifier in packageSourcesOnly := Some("sources"), + artifact in (Compile, packageBinWithoutResources) ~= (art => art.withName(art.name + "-without-resources")) + ) ++ + inConfig(Compile)( + Defaults.packageTaskSettings(packageSourcesOnly, Defaults.sourceMappings) ++ + Defaults.packageTaskSettings(packageBinWithoutResources, Def.task { + val relativePaths = + (unmanagedResources in Compile).value.flatMap(Path.relativeTo((unmanagedResourceDirectories in Compile).value)(_)) + (mappings in (Compile, packageBin)).value.filterNot { case (_, path) => relativePaths.contains(path) } + }) + ) + + val maxSubmitFileSize = { + val mb = 1024 * 1024 + 10 * mb + } + + /** Check that the jar exists, isn't empty, isn't crazy big, and can be read + * If so, encode jar as base64 so we can send it to Coursera + */ + def prepareJar(jar: File, s: TaskStreams): String = { + val errPrefix = "Error submitting assignment jar: " + val fileLength = jar.length() + if (!jar.exists()) { + s.log.error(errPrefix + "jar archive does not exist\n" + jar.getAbsolutePath) + failSubmit() + } else if (fileLength == 0L) { + s.log.error(errPrefix + "jar archive is empty\n" + jar.getAbsolutePath) + failSubmit() + } else if (fileLength > maxSubmitFileSize) { + s.log.error(errPrefix + "jar archive is too big. Allowed size: " + + maxSubmitFileSize + " bytes, found " + fileLength + " bytes.\n" + + jar.getAbsolutePath) + failSubmit() + } else { + val bytes = new Array[Byte](fileLength.toInt) + val sizeRead = try { + val is = new FileInputStream(jar) + val read = is.read(bytes) + is.close() + read + } catch { + case ex: IOException => + s.log.error(errPrefix + "failed to read sources jar archive\n" + ex.toString) + failSubmit() + } + if (sizeRead != bytes.length) { + s.log.error(errPrefix + "failed to read the sources jar archive, size read: " + sizeRead) + failSubmit() + } else encodeBase64(bytes) + } + } + + /** Task to package solution to a given file path */ + lazy val packageSubmissionSetting = packageSubmission := { + val args: Seq[String] = Def.spaceDelimited("[path]").parsed + val s: TaskStreams = streams.value // for logging + val jar = (packageSubmissionZip in Compile).value + + val base64Jar = prepareJar(jar, s) + + val path = args.headOption.getOrElse((baseDirectory.value / "submission.jar").absolutePath) + scala.tools.nsc.io.File(path).writeAll(base64Jar) + } + +/* + /** Task to submit a solution to coursera */ + val submit = inputKey[Unit]("submit solution to Coursera") + lazy val submitSetting = submit := { + val args: Seq[String] = Def.spaceDelimited("").parsed + val s: TaskStreams = streams.value // for logging + val jar = (packageSubmissionZip in Compile).value + + val assignmentDetails = assignmentInfo.value + val assignmentKey = assignmentDetails.key + val courseName = + course.value match { + case "capstone" => "scala-capstone" + case "bigdata" => "scala-spark-big-data" + case other => other + } + + val partId = assignmentDetails.partId + val itemId = assignmentDetails.itemId + val premiumItemId = assignmentDetails.premiumItemId + + val (email, secret) = args match { + case email :: secret :: Nil => + (email, secret) + case _ => + val inputErr = + s"""|Invalid input to `submit`. The required syntax for `submit` is: + |submit + | + |The submit token is NOT YOUR LOGIN PASSWORD. + |It can be obtained from the assignment page: + |https://www.coursera.org/learn/$courseName/programming/$itemId + |${ + premiumItemId.fold("") { id => + s"""or (for premium learners): + |https://www.coursera.org/learn/$courseName/programming/$id + """.stripMargin + } + } + """.stripMargin + s.log.error(inputErr) + failSubmit() + } + + val base64Jar = prepareJar(jar, s) + val json = + s"""|{ + | "assignmentKey":"$assignmentKey", + | "submitterEmail":"$email", + | "secret":"$secret", + | "parts":{ + | "$partId":{ + | "output":"$base64Jar" + | } + | } + |}""".stripMargin + + def postSubmission[T](data: String): Try[HttpResponse[String]] = { + val http = Http("https://www.coursera.org/api/onDemandProgrammingScriptSubmissions.v1") + val hs = List( + ("Cache-Control", "no-cache"), + ("Content-Type", "application/json") + ) + s.log.info("Connecting to Coursera...") + val response = Try(http.postData(data) + .headers(hs) + .option(HttpOptions.connTimeout(10000)) // scalaj default timeout is only 100ms, changing that to 10s + .asString) // kick off HTTP POST + response + } + + val connectMsg = + s"""|Attempting to submit "${assignment.value}" assignment in "$courseName" course + |Using: + |- email: $email + |- submit token: $secret""".stripMargin + s.log.info(connectMsg) + + def reportCourseraResponse(response: HttpResponse[String]): Unit = { + val code = response.code + val respBody = response.body + + /* Sample JSON response from Coursera + { + "message": "Invalid email or token.", + "details": { + "learnerMessage": "Invalid email or token." + } + } + */ + + // Success, Coursera responds with 2xx HTTP status code + if (response.is2xx) { + val successfulSubmitMsg = + s"""|Successfully connected to Coursera. (Status $code) + | + |Assignment submitted successfully! + | + |You can see how you scored by going to: + |https://www.coursera.org/learn/$courseName/programming/$itemId/ + |${ + premiumItemId.fold("") { id => + s"""or (for premium learners): + |https://www.coursera.org/learn/$courseName/programming/$id + """.stripMargin + } + } + |and clicking on "My Submission".""".stripMargin + s.log.info(successfulSubmitMsg) + } + + // Failure, Coursera responds with 4xx HTTP status code (client-side failure) + else if (response.is4xx) { + val result = Try(Json.parse(respBody)).toOption + val learnerMsg = result match { + case Some(resp: JsObject) => + (JsPath \ "details" \ "learnerMessage").read[String].reads(resp).get + case Some(x) => // shouldn't happen + "Could not parse Coursera's response:\n" + x + case None => + "Could not parse Coursera's response:\n" + respBody + } + val failedSubmitMsg = + s"""|Submission failed. + |There was something wrong while attempting to submit. + |Coursera says: + |$learnerMsg (Status $code)""".stripMargin + s.log.error(failedSubmitMsg) + } + + // Failure, Coursera responds with 5xx HTTP status code (server-side failure) + else if (response.is5xx) { + val failedSubmitMsg = + s"""|Submission failed. + |Coursera seems to be unavailable at the moment (Status $code) + |Check https://status.coursera.org/ and try again in a few minutes. + """.stripMargin + s.log.error(failedSubmitMsg) + } + + // Failure, Coursera repsonds with an unexpected status code + else { + val failedSubmitMsg = + s"""|Submission failed. + |Coursera replied with an unexpected code (Status $code) + """.stripMargin + s.log.error(failedSubmitMsg) + } + } + + // kick it all off, actually make request + postSubmission(json) match { + case Success(resp) => reportCourseraResponse(resp) + case Failure(e) => + val failedConnectMsg = + s"""|Connection to Coursera failed. + |There was something wrong while attempting to connect to Coursera. + |Check your internet connection. + |${e.toString}""".stripMargin + s.log.error(failedConnectMsg) + } + + } +*/ + + def failSubmit(): Nothing = { + sys.error("Submission failed") + } + + /** + * ***************** + * DEALING WITH JARS + */ + def encodeBase64(bytes: Array[Byte]): String = + new String(Base64.encodeBase64(bytes)) +} diff --git a/project/build.properties b/project/build.properties new file mode 100644 index 0000000..c0bab04 --- /dev/null +++ b/project/build.properties @@ -0,0 +1 @@ +sbt.version=1.2.8 diff --git a/project/buildSettings.sbt b/project/buildSettings.sbt new file mode 100644 index 0000000..a309025 --- /dev/null +++ b/project/buildSettings.sbt @@ -0,0 +1,8 @@ +libraryDependencies += "com.novocode" % "junit-interface" % "0.11" % Test +// Used for base64 encoding +libraryDependencies += "commons-codec" % "commons-codec" % "1.10" + +// Used for Coursera submussion +// libraryDependencies += "org.scalaj" %% "scalaj-http" % "2.3.0" +// libraryDependencies += "com.typesafe.play" %% "play-json" % "2.6.9" + diff --git a/project/plugins.sbt b/project/plugins.sbt new file mode 100644 index 0000000..64a2492 --- /dev/null +++ b/project/plugins.sbt @@ -0,0 +1,2 @@ +addSbtPlugin("io.get-coursier" % "sbt-coursier" % "2.0.0-RC3-5") +addSbtPlugin("ch.epfl.lamp" % "sbt-dotty" % "0.3.4") diff --git a/src/main/scala/funsets/FunSets.scala b/src/main/scala/funsets/FunSets.scala new file mode 100644 index 0000000..3ea7c4f --- /dev/null +++ b/src/main/scala/funsets/FunSets.scala @@ -0,0 +1,91 @@ +package funsets + +/** + * 2. Purely Functional Sets. + */ +trait FunSets extends FunSetsInterface { + /** + * We represent a set by its characteristic function, i.e. + * its `contains` predicate. + */ + override type FunSet = Int => Boolean + + /** + * Indicates whether a set contains a given element. + */ + def contains(s: FunSet, elem: Int): Boolean = s(elem) + + /** + * Returns the set of the one given element. + */ + def singletonSet(elem: Int): FunSet = ??? + + + /** + * Returns the union of the two given sets, + * the sets of all elements that are in either `s` or `t`. + */ + def union(s: FunSet, t: FunSet): FunSet = ??? + + /** + * Returns the intersection of the two given sets, + * the set of all elements that are both in `s` and `t`. + */ + def intersect(s: FunSet, t: FunSet): FunSet = ??? + + /** + * Returns the difference of the two given sets, + * the set of all elements of `s` that are not in `t`. + */ + def diff(s: FunSet, t: FunSet): FunSet = ??? + + /** + * Returns the subset of `s` for which `p` holds. + */ + def filter(s: FunSet, p: Int => Boolean): FunSet = ??? + + + /** + * The bounds for `forall` and `exists` are +/- 1000. + */ + val bound = 1000 + + /** + * Returns whether all bounded integers within `s` satisfy `p`. + */ + def forall(s: FunSet, p: Int => Boolean): Boolean = + def iter(a: Int): Boolean = + if ??? then + ??? + else if ??? then + ??? + else + iter(???) + iter(???) + + /** + * Returns whether there exists a bounded integer within `s` + * that satisfies `p`. + */ + def exists(s: FunSet, p: Int => Boolean): Boolean = ??? + + /** + * Returns a set transformed by applying `f` to each element of `s`. + */ + def map(s: FunSet, f: Int => Int): FunSet = ??? + + /** + * Displays the contents of a set + */ + def toString(s: FunSet): String = + val xs = for i <- (-bound to bound) if contains(s, i) yield i + xs.mkString("{", ",", "}") + + /** + * Prints the contents of a set on the console. + */ + def printSet(s: FunSet): Unit = + println(toString(s)) +} + +object FunSets extends FunSets diff --git a/src/main/scala/funsets/FunSetsInterface.scala b/src/main/scala/funsets/FunSetsInterface.scala new file mode 100644 index 0000000..5e5ca94 --- /dev/null +++ b/src/main/scala/funsets/FunSetsInterface.scala @@ -0,0 +1,20 @@ +package funsets + +/** + * The interface used by the grading infrastructure. You should not edit any + * code here, or your submission may fail with a NoSuchMethodError. + */ +trait FunSetsInterface { + type FunSet = Int => Boolean + + def contains(s: FunSet, elem: Int): Boolean + def singletonSet(elem: Int): FunSet + def union(s: FunSet, t: FunSet): FunSet + def intersect(s: FunSet, t: Int => Boolean): FunSet + def diff(s: FunSet, t: FunSet): FunSet + def filter(s: FunSet, p: Int => Boolean): FunSet + def forall(s: FunSet, p: Int => Boolean): Boolean + def exists(s: FunSet, p: Int => Boolean): Boolean + def map(s: FunSet, f: Int => Int): FunSet + def toString(s: FunSet): String +} diff --git a/src/main/scala/funsets/Main.scala b/src/main/scala/funsets/Main.scala new file mode 100644 index 0000000..6126909 --- /dev/null +++ b/src/main/scala/funsets/Main.scala @@ -0,0 +1,6 @@ +package funsets + +object Main extends App { + import FunSets._ + println(contains(singletonSet(1), 1)) +} diff --git a/src/test/scala/funsets/FunSetSuite.scala b/src/test/scala/funsets/FunSetSuite.scala new file mode 100644 index 0000000..2837ad4 --- /dev/null +++ b/src/test/scala/funsets/FunSetSuite.scala @@ -0,0 +1,73 @@ +package funsets + +import org.junit._ + +/** + * This class is a test suite for the methods in object FunSets. + * + * To run this test suite, start "sbt" then run the "test" command. + */ +class FunSetSuite { + + import FunSets._ + + @Test def `contains is implemented`: Unit = + assert(contains(x => true, 100)) + + /** + * When writing tests, one would often like to re-use certain values for multiple + * tests. For instance, we would like to create an Int-set and have multiple test + * about it. + * + * Instead of copy-pasting the code for creating the set into every test, we can + * store it in the test class using a val: + * + * val s1 = singletonSet(1) + * + * However, what happens if the method "singletonSet" has a bug and crashes? Then + * the test methods are not even executed, because creating an instance of the + * test class fails! + * + * Therefore, we put the shared values into a separate trait (traits are like + * abstract classes), and create an instance inside each test method. + * + */ + + trait TestSets { + val s1 = singletonSet(1) + val s2 = singletonSet(2) + val s3 = singletonSet(3) + } + + /** + * This test is currently disabled (by using @Ignore) because the method + * "singletonSet" is not yet implemented and the test would fail. + * + * Once you finish your implementation of "singletonSet", remvoe the + * @Ignore annotation. + */ + @Ignore("not ready yet") @Test def `singleton set one contains one`: Unit = + /** + * We create a new instance of the "TestSets" trait, this gives us access + * to the values "s1" to "s3". + */ + new TestSets { + /** + * The string argument of "assert" is a message that is printed in case + * the test fails. This helps identifying which assertion failed. + */ + assert(contains(s1, 1), "Singleton") + } + + @Test def `union contains all elements of each set`: Unit = + new TestSets { + val s = union(s1, s2) + assert(contains(s, 1), "Union 1") + assert(contains(s, 2), "Union 2") + assert(!contains(s, 3), "Union 3") + } + + + + @Rule def individualTestTimeout = new org.junit.rules.Timeout(10 * 1000) +} diff --git a/student.sbt b/student.sbt new file mode 100644 index 0000000..855fa0c --- /dev/null +++ b/student.sbt @@ -0,0 +1,9 @@ +// Used for base64 encoding +libraryDependencies += "commons-codec" % "commons-codec" % "1.10" + +// Used for Coursera submussion +// libraryDependencies += "org.scalaj" %% "scalaj-http" % "2.4.2" +// libraryDependencies += "com.typesafe.play" %% "play-json" % "2.7.4" + +// Student tasks (i.e. packageSubmission) +enablePlugins(StudentTasks)