Skip to content

Commit

Permalink
Add Count command
Browse files Browse the repository at this point in the history
  • Loading branch information
hyerinshelly committed Mar 28, 2024
1 parent 8e7373c commit 63a2345
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 1 deletion.
18 changes: 18 additions & 0 deletions src/main/scala/fhetest/Checker/Utils.scala
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,17 @@ implicit val encodeIntOrDouble: Encoder[Int | Double] = Encoder.instance {
case d: Double => Json.fromDoubleOrNull(d)
}

implicit val mapEncoder: Encoder[Map[List[Int], Int]] =
new Encoder[Map[List[Int], Int]] {
override def apply(a: Map[List[Int], Int]): Json = {
val encodedPairs = a.map {
case (key, value) =>
key.toString() -> value.asJson
}
Json.obj(encodedPairs.toSeq: _*)
}
}

object DumpUtil {
def dumpFile(data: String, filename: String): Unit = {
val writer = new PrintWriter(filename)
Expand Down Expand Up @@ -285,6 +296,13 @@ object DumpUtil {
}
}

def dumpCount(dir: String, countMap: Map[List[Int], Int]): Unit = {
val jsonString =
countMap.asJson.spaces2
val outputFileName = s"$dir/count.json"
dumpFile(jsonString, outputFileName)
}

def readResult(filePath: String): ResultInfo = {
val fileContents = readFile(filePath)
val resultInfo =
Expand Down
50 changes: 49 additions & 1 deletion src/main/scala/fhetest/Command.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,14 @@ package fhetest

import fhetest.Utils.*
import fhetest.Generate.*
import fhetest.Generate.Utils.combinations
import fhetest.Phase.{Parse, Interp, Print, Execute, Generate, Check}
import fhetest.Checker.DumpUtil

import java.nio.file.{Files, Paths};
import java.io.File
import scala.jdk.CollectionConverters._

sealed abstract class Command(
/** command name */
val name: String,
Expand Down Expand Up @@ -119,7 +124,7 @@ case object CmdRun extends BackendCommand("run") {

/** `compile` command */
case object CmdCompile extends Command("compile") {
val help = "compiles a T2 file to the given backend."
val help = "Compile a T2 file to the given backend."
val examples = List(
"fhetest compile -file:tmp.t2 -b:SEAL",
"fhetest compile -file:tmp.t2 -b:OpenFHE",
Expand Down Expand Up @@ -299,3 +304,46 @@ case object CmdReplay extends Command("replay") {
print(result)
}
}

case object CmdCount extends Command("count") {
val help =
"Count the number of programs tested for each combination of valid filters"
val examples = List(
"fhetest count -dir:logs/test-invalid",
)
def runJob(config: Config): Unit =
val dirString = config.dirName.getOrElseThrow("No directory given.")
if (dirString contains "invalid") {
val dir = new File(dirString)
if (dir.exists() && dir.isDirectory) {
val numOfValidFilters =
classOf[ValidFilter].getDeclaredClasses.toList.filter { cls =>
classOf[ValidFilter]
.isAssignableFrom(cls) && cls != classOf[ValidFilter]
}.length
val allCombinations = (1 to numOfValidFilters).toList.flatMap(
combinations(_, numOfValidFilters),
)
var countMap: Map[List[Int], Int] =
allCombinations.foldLeft(Map.empty[List[Int], Int]) {
case (acc, comb) =>
acc + (comb -> 0)
}
val files = Files.list(Paths.get(dirString))
val fileList = files.iterator().asScala.toList
for {
filePath <- fileList
fileName = filePath.toString()
} yield {
val resultInfo = DumpUtil.readResult(fileName)
val t2Program = resultInfo.program
val invalidFilterIdxList = t2Program.invalidFilterIdxList
countMap = countMap.updatedWith(invalidFilterIdxList) {
case Some(cnt) => Some(cnt + 1)
case None => Some(1) // unreachable
}
}
DumpUtil.dumpCount(dirString, countMap)
}
} else println("Directrory contains test cases of VALID programs")
}
3 changes: 3 additions & 0 deletions src/main/scala/fhetest/fhetest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ object FHETest {
CmdTest,
// Replay the given json
CmdReplay,
// Make a json report of invalid program testing
// Count the number of programs tested for each combination of valid filters
CmdCount,
)
val cmdMap = commands.foldLeft[Map[String, Command]](Map()) {
case (map, cmd) => map + (cmd.name -> cmd)
Expand Down

0 comments on commit 63a2345

Please sign in to comment.