#!/bin/sh
# Compile a program against the installed jar and round-trip values through
# formats from the plain sources and from the expanded FMPP templates
# (tuples, asProduct2).
set -e

WORK="${AUTOPKGTEST_TMP:-$(mktemp -d)}"
cd "$WORK"
CP=/usr/share/java/sbinary-3.jar:/usr/share/java/scala-xml-3.jar

cat > RoundTrip.scala <<'X'
import sbinary.DefaultProtocol._
import sbinary.Operations._
import sbinary.Format

case class Point(x: Int, name: String)

@main def roundtrip(): Unit =
  given Format[Point] = asProduct2(Point.apply)(p => (p.x, p.name))
  val v = (List(1, 2, 3), Map("a" -> 1L), Option("x"), Point(7, "p"))
  val back = fromByteArray[(List[Int], Map[String, Long], Option[String], Point)](toByteArray(v))
  assert(back == v, s"$back != $v")
  println("roundtrip ok")
X

scalac-3 -classpath "$CP" -d . RoundTrip.scala
scala-3 -classpath "$CP:." roundtrip | tee out.txt
grep -qx 'roundtrip ok' out.txt
