<commit_before>package se.chimps.bitziness.core.generic

import akka.actor.ActorSystem
import org.json4s.{ShortTypeHints, DefaultFormats}
import org.json4s.native.Serialization
import org.scalatest.{BeforeAndAfterAll,FunSuite}
import se.chimps.bitziness.core.generic.Serializers.{JSONSerializer, ObjectSerializer}

/**
 * Tests of serializers.
 */
class SerializersTest extends FunSuite with BeforeAndAfterAll {

  val serializer = new Serializer

  test("object can go into bytes and back") {
    val name = "åö?!*≥" // funny name huh?
    val subject = Simple(name)

    val bytes = serializer.serialize(subject)
    assert(bytes.length > 0, "Bytes were not more than 0 in length.")

    val reborn = serializer.deserialize[Simple](bytes)
    assert(reborn.name.equals(name), "the reborns name did not match the original name.")
    assert(reborn.equals(subject), "the reborn does not equal the subject.")
  }

  test("object can go into json and back") {
    val name = "åäö?!*≥"
    val subject = Simple(name)

    val json = serializer.toJSON(subject)
    assert(json != null, "the json string was null...")
    assert(!json.isEmpty, "the json string was empty.")
    assert(json.contains(name), "the json string does not contain name.")

    val reborn = serializer.fromJSON[Simple](json)
    assert(reborn.name.equals(name), "the reborns name did not match the original name.")
    assert(reborn.equals(subject), "the reborn did not equal the subject.")
  }

  test("list of objects can go into json and back") {
    val subject = List(Simple("1"), Simple("2"))

    val json = serializer.toJSON(subject)
    assert(json != null, "the json string was null")
    assert(!json.isEmpty, "the json string was empty")

    val reborn = serializer.fromJSON[List[Simple]](json)
    assert(reborn.size == 2, "the size of the reborn are incorrect.")
    assert(reborn.equals(subject), "the reborn does not equal the subject")
  }

  override protected def afterAll():Unit = {
    serializer.shutdown()
  }
}

class Serializer extends ObjectSerializer with JSONSerializer {
  protected val system = ActorSystem()

  def shutdown() = system.shutdown()
}

case class Simple(name:String)<commit_msg>Improves serialization test a tiny bit.<commit_after>package se.chimps.bitziness.core.generic

import akka.actor.ActorSystem
import org.json4s.{ShortTypeHints, DefaultFormats}
import org.json4s.native.Serialization
import org.scalatest.{BeforeAndAfterAll,FunSuite}
import se.chimps.bitziness.core.generic.Serializers.{JSONSerializer, ObjectSerializer}

/**
 * Tests of serializers.
 */
class SerializersTest extends FunSuite with BeforeAndAfterAll {

  val serializer = new Serializer

  test("object can go into bytes and back") {
    val name = "åö?!*≥" // funny name huh?
    val subject = Simple(name)

    val bytes = serializer.serialize(subject)
    assert(bytes.length > 0, "Bytes were not more than 0 in length.")

    val reborn = serializer.deserialize[Simple](bytes)
    assert(reborn.name.equals(name), "the reborns name did not match the original name.")
    assert(reborn.equals(subject), "the reborn does not equal the subject.")
  }

  test("object can go into json and back") {
    val name = "åäö?!*≥"
    val subject = Simple(name)

    val json = serializer.toJSON(subject)
    assert(json != null, "the json string was null...")
    assert(!json.isEmpty, "the json string was empty.")
    assert(json.contains(name), "the json string does not contain name.")

    val reborn = serializer.fromJSON[Simple](json)
    assert(reborn.name.equals(name), "the reborns name did not match the original name.")
    assert(reborn.equals(subject), "the reborn did not equal the subject.")
  }

  test("list of objects can go into json and back") {
    val subject = List(Simple("1"), Simple("2"))

    val json = serializer.toJSON(subject)
    assert(json != null, "the json string was null")
    assert(!json.isEmpty, "the json string was empty")

    val reborn = serializer.fromJSON[List[Simple]](json)
    assert(reborn.size == 2, "the size of the reborn are incorrect.")
    assert(reborn.equals(subject), "the reborn does not equal the subject")
  }

  test("list of objects can go into bytes and back") {
    val subject = List(Simple("1"), Simple("2"))

    val bytes = serializer.serialize(subject)
    assert(bytes.length > 0, "bytes were not more than 0 in length.")
    println(bytes.length)
    bytes.foreach(b => print(b.toChar))
    println()

    val reborn = serializer.deserialize[List[Simple]](bytes)
    assert(reborn.size == subject.size, "reborn had different size than subject.")
    assert(reborn.equals(subject), "reborn was not equal to the subject.")
  }

  override protected def afterAll():Unit = {
    serializer.shutdown()
  }
}

class Serializer extends ObjectSerializer with JSONSerializer {
  protected val system = ActorSystem()

  def shutdown() = system.shutdown()
}

case class Simple(name:String)
{'fraction_non_alphanumeric': 0.116848926690968, 'fraction_numerical': 0.0036452004860267314, 'mean_word_length': 4.044943820224719, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2654478', 'n_tokens_mistral': 1493, 'n_tokens_neox': 1445, 'n_words': 463}