Skip to content

Commit

Permalink
Use boundary and break instead of return; minor fixes to http server
Browse files Browse the repository at this point in the history
  • Loading branch information
whym committed Jan 27, 2024
1 parent c247217 commit 1aa39e7
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 28 deletions.
2 changes: 1 addition & 1 deletion build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ libraryDependencies ++= Seq(
"com.typesafe" % "config" % "1.3.4",
"com.typesafe.scala-logging" %% "scala-logging" % "3.9.4",
"ch.qos.logback" % "logback-classic" % "1.2.11",
"com.softwaremill.sttp.client3" %% "core" % "3.5.1" % "test",
"com.softwaremill.sttp.client3" %% "core" % "3.9.2" % "test",
"org.scalatest" %% "scalatest" % "3.2.10" % "test",
"org.scalatestplus" %% "mockito-3-4" % "3.2.10.0" % "test",
)
Expand Down
31 changes: 13 additions & 18 deletions src/main/scala/org/whym/growthring/SuffixArrays.scala
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ package org.whym.growthring

import scala.jdk.CollectionConverters._
import scala.collection.{ mutable, immutable }
import scala.util.boundary, boundary.break
import com.typesafe.scalalogging.LazyLogging
import java.{ io => jio }
import java.nio
Expand Down Expand Up @@ -252,15 +253,12 @@ case class SuffixArrays(arr: Array[Int], sa: Array[Int], lcp: Array[Int]) {
}
private def _lowerbound(q: Array[Int]): Int = {
def cmp(i: Int): Int = {
for (j <- Range(0, q.size)) {
if (sa(i) + j >= arr.size) {
return -1
}
if (q(j) != arr(sa(i) + j)) {
return arr(sa(i) + j) - q(j)
}
}
return 0
boundary:
for j <- Range(0, q.size)
do
if (sa(i) + j >= arr.size) then break(-1)
else if (q(j) != arr(sa(i) + j)) then break(arr(sa(i) + j) - q(j))
0
}
var l = -1
var r = arr.size
Expand All @@ -279,15 +277,12 @@ case class SuffixArrays(arr: Array[Int], sa: Array[Int], lcp: Array[Int]) {
}
private def _upperbound(q: Array[Int]): Int = {
def cmp(i: Int): Int = {
for (j <- Range(0, q.size)) {
if (sa(i) + j >= arr.size) {
return +1
}
if (q(j) != arr(sa(i) + j)) {
return arr(sa(i) + j) - q(j)
}
}
return 0
boundary:
for j <- Range(0, q.size)
do
if (sa(i) + j >= arr.size) then break(+1)
else if (q(j) != arr(sa(i) + j)) then break(arr(sa(i) + j) - q(j))
0
}
var l = -1
var r = arr.size
Expand Down
20 changes: 13 additions & 7 deletions src/test/scala/test-servlets.scala
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ class TestFindRepeatsServlet extends AnyFunSuite with MockitoSugar {
}

class TestWikiBlameServlet extends AnyFunSuite with MockitoSugar {
def get(): JValue = {
object MockedResponse1 {
val response = mock[HttpServletResponse]
val request = mock[HttpServletRequest]
val stringWriter = new StringWriter
Expand All @@ -162,7 +162,7 @@ class TestWikiBlameServlet extends AnyFunSuite with MockitoSugar {
when(request.getParameter("title")).thenReturn("Main_page")
when(request.getParameter("n")).thenReturn("3")

SimpleHttpServer.create("localhost", port,
val server = SimpleHttpServer.create("localhost", port,
Map(("/index.php", <mediawiki xmlns="http://www.mediawiki.org/xml/export-0.8/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http:
//www.mediawiki.org/xml/export-0.8/ http://www.mediawiki.org/xml/export-0.8.xsd" version="0.8" xml:lang="ja">
<siteinfo>
Expand Down Expand Up @@ -214,12 +214,18 @@ class TestWikiBlameServlet extends AnyFunSuite with MockitoSugar {
<format>text/x-wiki</format>
</revision>
</page>
</mediawiki>.toString))).start
TestSimpleHttpServer.waitUntilPrepared(addr, 1000L)
new WikiBlameServlet().doGet(request, response)
return parse(stringWriter.toString)
</mediawiki>.toString)))
try {
server.start
TestSimpleHttpServer.waitUntilPrepared(addr, 1000L)
new WikiBlameServlet().doGet(request, response)
} finally {
server.stop(0)
}
val result = parse(stringWriter.toString)
}
val json = get()

val json = MockedResponse1.result

test("wiki blame servlet") {
assertResult(JString("Main_page")) {
Expand Down
1 change: 1 addition & 0 deletions src/test/scala/test-simple-http-server.scala
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ object TestSimpleHttpServer {
sock.close
return true
} else {
sock.close
if (System.currentTimeMillis - start > limit) {
return false
}
Expand Down
4 changes: 2 additions & 2 deletions src/test/scala/util/SimpleHttpServer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import java.io.IOException
import com.sun.net.httpserver.{ HttpServer, HttpHandler, HttpExchange }

object SimpleHttpServer {
class MyHandler(body: String, ctype: String) extends HttpHandler {
class MyHandler(path: String, body: String, ctype: String) extends HttpHandler {
@throws[IOException] override def handle(exchange: HttpExchange): Unit = {
val response = body.getBytes()
exchange.sendResponseHeaders(
Expand All @@ -24,7 +24,7 @@ object SimpleHttpServer {
val ctype = if (body.size > 0 && body(0) == '<') { "text/xml" }
else if (body.size > 0 && body(0) == '{') { "application/json" }
else { "text/plain" }
httpServer.createContext(if (path(0) == '/') { path } else { "/" + path }, new MyHandler(body, ctype))
httpServer.createContext(if (path(0) == '/') { path } else { "/" + path }, new MyHandler(path, body, ctype))
}
httpServer
}
Expand Down

0 comments on commit 1aa39e7

Please sign in to comment.