<commit_before>use "ponytest"
use "random"
use "time"

actor Main is TestList
  new create(env: Env) => PonyTest(env, this)
  new make() => None

  fun tag tests(test: PonyTest) =>
    test(_TestList)
    test(_TestMap)
    test(_TestMapRemove)
    test(_TestMapUpsert)
    test(_TestRing)
    test(_TestListsFrom)
    test(_TestListsMap)
    test(_TestListsFlatMap)
    test(_TestListsFilter)
    test(_TestListsFold)
    test(_TestListsEvery)
    test(_TestListsExists)
    test(_TestListsPartition)
    test(_TestListsDrop)
    test(_TestListsTake)
    test(_TestListsTakeWhile)
    test(_TestListsContains)
    test(_TestListsReverse)
    test(_TestHashSetContains)
    test(_TestHashSetIntersect)
    test(_TestSort)
    test(_TestRange)
    test(_TestHeap)

class iso _TestList is UnitTest
  fun name(): String => "collections/List"

  fun apply(h: TestHelper) ? =>
    let a = List[U32]
    a .> push(0) .> push(1) .> push(2)

    let b = a.clone()
    h.assert_eq[USize](b.size(), 3)
    h.assert_eq[U32](b(0)?, 0)
    h.assert_eq[U32](b(1)?, 1)
    h.assert_eq[U32](b(2)?, 2)

    b.remove(1)?
    h.assert_eq[USize](b.size(), 2)
    h.assert_eq[U32](b(0)?, 0)
    h.assert_eq[U32](b(1)?, 2)

    b.append_list(a)
    h.assert_eq[USize](b.size(), 5)
    h.assert_eq[U32](b(0)?, 0)
    h.assert_eq[U32](b(1)?, 2)
    h.assert_eq[U32](b(2)?, 0)
    h.assert_eq[U32](b(3)?, 1)
    h.assert_eq[U32](b(4)?, 2)

class iso _TestListsFrom is UnitTest
  fun name(): String => "collections/Lists/from()"

  fun apply(h: TestHelper) ? =>
    let a = List[U32].from([1; 3; 5; 7; 9])

    h.assert_eq[USize](a.size(), 5)
    h.assert_eq[U32](a(0)?, 1)
    h.assert_eq[U32](a(1)?, 3)
    h.assert_eq[U32](a(2)?, 5)
    h.assert_eq[U32](a(3)?, 7)
    h.assert_eq[U32](a(4)?, 9)

    let b = List[U32].from(Array[U32])
    h.assert_eq[USize](b.size(), 0)

class iso _TestMap is UnitTest
  fun name(): String => "collections/Map"

  fun apply(h: TestHelper) ? =>
    let a = Map[String, U32]
    h.assert_eq[USize](0, a.size())
    h.assert_eq[USize](8, a.space())
    (a("0") = 0) as None
    (a("1") = 1) as None
    (a("2") = 2) as None
    (a("3") = 3) as None
    (a("4") = 4) as None
    (a("5") = 5) as None
    (a("6") = 6) as None

    h.assert_true(a.contains("0"),
      "contains did not find expected element in HashMap")
    h.assert_false(a.contains("7"),
      "contains found unexpected element in HashMap")

    h.assert_eq[U32](1, (a("1") = 11) as U32)
    h.assert_eq[U32](11, (a("1") = 1) as U32)

    let b = Map[String, U32]
    h.assert_eq[U32](0, b.insert("0", 0)?)
    h.assert_eq[U32](1, b.insert("1", 1)?)
    h.assert_eq[U32](2, b.insert("2", 2)?)
    h.assert_eq[U32](3, b.insert("3", 3)?)
    h.assert_eq[U32](4, b.insert("4", 4)?)
    h.assert_eq[U32](5, b.insert("5", 5)?)
    h.assert_eq[U32](6, b.insert("6", 6)?)

    h.assert_eq[USize](7, a.size())
    h.assert_eq[USize](16, a.space())
    h.assert_eq[USize](7, b.size())
    h.assert_eq[USize](16, b.space())

    var pair_count: USize = 0
    for (k, v) in a.pairs() do
      h.assert_eq[U32](b(k)?, v)
      pair_count = pair_count + 1
    end
    h.assert_eq[USize](7, pair_count)

    let a_removed_pair = a.remove("1")?
    h.assert_eq[String]("1", a_removed_pair._1)
    h.assert_eq[U32](1, a_removed_pair._2)
    h.assert_eq[USize](6, a.size())
    h.assert_eq[USize](16, a.space())

    a.compact()
    h.assert_eq[USize](6, a.size())
    h.assert_eq[USize](8, a.space())

    b.clear()
    h.assert_eq[USize](0, b.size())
    h.assert_eq[USize](8, b.space())

    let c = Map[String, U32]
    c.insert_if_absent("a", 1)?
    h.assert_eq[U32](1, c("a")?)
    c.insert_if_absent("a", 2)?
    h.assert_eq[U32](1, c("a")?)
    h.assert_eq[U32](0, c.insert_if_absent("0", 0)?)
    h.assert_eq[U32](0, c.insert_if_absent("0", 1)?)

class iso _TestMapRemove is UnitTest
  fun name(): String => "collections/Map.remove"

  fun apply(h: TestHelper) ? =>
    let x: Map[U32, String] = Map[U32,String]
    x(1) = "one"
    h.assert_eq[USize](x.size(), 1)
    x.remove(1)?
    h.assert_eq[USize](x.size(), 0)

    x(2) = "two"
    h.assert_eq[USize](x.size(), 1)
    x.remove(2)?
    h.assert_eq[USize](x.size(), 0)

    x(11) = "here"
    h.assert_eq[String](x(11)?, "here")

class iso _TestMapUpsert is UnitTest
  fun name(): String => "collections/Map.upsert"

  fun apply(h: TestHelper) ? =>
    let x: Map[U32, U64] = Map[U32,U64]
    let f = {(x: U64, y: U64): U64 => x + y }
    x.upsert(1, 5, f)?
    h.assert_eq[U64](5, x(1)?)
    x.upsert(1, 3, f)?
    h.assert_eq[U64](8, x(1)?)
    x.upsert(1, 4, f)?
    h.assert_eq[U64](12, x(1)?)
    x.upsert(1, 2, f)?
    h.assert_eq[U64](14, x(1)?)
    x.upsert(1, 1, f)?
    h.assert_eq[U64](15, x(1)?)

    let x2: Map[U32, String] = Map[U32,String]
    let g = {(x: String, y: String): String => x + ", " + y }
    x2.upsert(1, "1", g)?
    h.assert_eq[String]("1", x2(1)?)
    x2.upsert(1, "2", g)?
    h.assert_eq[String]("1, 2", x2(1)?)
    x2.upsert(1, "3", g)?
    h.assert_eq[String]("1, 2, 3", x2(1)?)
    x2.upsert(1, "abc", g)?
    h.assert_eq[String]("1, 2, 3, abc", x2(1)?)
    x2.upsert(1, "def", g)?
    h.assert_eq[String]("1, 2, 3, abc, def", x2(1)?)

    // verify correct results when we trigger a resize
    let prealloc: USize = 6
    let expected_initial_size: USize = (prealloc * 4) / 3
    let x3: Map[U32, U64] = Map[U32,U64](prealloc)
    let f' = {(x: U64, y: U64): U64 => x + y }
    h.assert_eq[USize](expected_initial_size, x3.space())
    h.assert_eq[U64](1, x3.upsert(1, 1, f')?)
    h.assert_eq[U64](1, x3.upsert(2, 1, f')?)
    h.assert_eq[U64](1, x3.upsert(3, 1, f')?)
    h.assert_eq[U64](1, x3.upsert(4, 1, f')?)
    h.assert_eq[U64](1, x3.upsert(5, 1, f')?)
    h.assert_eq[U64](1, x3.upsert(6, 1, f')?)
    h.assert_eq[U64](1, x3.upsert(7, 1, f')?)
    h.assert_eq[U64](2, x3.upsert(5, 1, f')?)
    h.assert_ne[USize](expected_initial_size, x3.space())

    let x4: Map[I32, I64] = Map[I32,I64]
    let i = {(x: I64, y: I64): I64 => x - y }
    x4.upsert(2, 4, i)?
    h.assert_eq[I64](4, x4(2)?)
    x4.upsert(2, 4, i)?
    h.assert_eq[I64](0, x4(2)?)
    x4.upsert(2, 4, i)?
    h.assert_eq[I64](-4, x4(2)?)

class iso _TestRing is UnitTest
  fun name(): String => "collections/RingBuffer"

  fun apply(h: TestHelper) ? =>
    let a = RingBuffer[U64](4)
    a .> push(0) .> push(1) .> push(2) .> push(3)

    h.assert_eq[U64](a(0)?, 0)
    h.assert_eq[U64](a(1)?, 1)
    h.assert_eq[U64](a(2)?, 2)
    h.assert_eq[U64](a(3)?, 3)

    a .> push(4) .> push(5)<commit_msg>Add failing test case for siphash error<commit_after>use "ponytest"
use "random"
use "time"

actor Main is TestList
  new create(env: Env) => PonyTest(env, this)
  new make() => None

  fun tag tests(test: PonyTest) =>
    test(_TestList)
    test(_TestMap)
    test(_TestMapRemove)
    test(_TestMapUpsert)
    test(_TestMapHashFunc)
    test(_TestRing)
    test(_TestListsFrom)
    test(_TestListsMap)
    test(_TestListsFlatMap)
    test(_TestListsFilter)
    test(_TestListsFold)
    test(_TestListsEvery)
    test(_TestListsExists)
    test(_TestListsPartition)
    test(_TestListsDrop)
    test(_TestListsTake)
    test(_TestListsTakeWhile)
    test(_TestListsContains)
    test(_TestListsReverse)
    test(_TestHashSetContains)
    test(_TestHashSetIntersect)
    test(_TestSort)
    test(_TestRange)
    test(_TestHeap)

class iso _TestList is UnitTest
  fun name(): String => "collections/List"

  fun apply(h: TestHelper) ? =>
    let a = List[U32]
    a .> push(0) .> push(1) .> push(2)

    let b = a.clone()
    h.assert_eq[USize](b.size(), 3)
    h.assert_eq[U32](b(0)?, 0)
    h.assert_eq[U32](b(1)?, 1)
    h.assert_eq[U32](b(2)?, 2)

    b.remove(1)?
    h.assert_eq[USize](b.size(), 2)
    h.assert_eq[U32](b(0)?, 0)
    h.assert_eq[U32](b(1)?, 2)

    b.append_list(a)
    h.assert_eq[USize](b.size(), 5)
    h.assert_eq[U32](b(0)?, 0)
    h.assert_eq[U32](b(1)?, 2)
    h.assert_eq[U32](b(2)?, 0)
    h.assert_eq[U32](b(3)?, 1)
    h.assert_eq[U32](b(4)?, 2)

class iso _TestListsFrom is UnitTest
  fun name(): String => "collections/Lists/from()"

  fun apply(h: TestHelper) ? =>
    let a = List[U32].from([1; 3; 5; 7; 9])

    h.assert_eq[USize](a.size(), 5)
    h.assert_eq[U32](a(0)?, 1)
    h.assert_eq[U32](a(1)?, 3)
    h.assert_eq[U32](a(2)?, 5)
    h.assert_eq[U32](a(3)?, 7)
    h.assert_eq[U32](a(4)?, 9)

    let b = List[U32].from(Array[U32])
    h.assert_eq[USize](b.size(), 0)

class iso _TestMap is UnitTest
  fun name(): String => "collections/Map"

  fun apply(h: TestHelper) ? =>
    let a = Map[String, U32]
    h.assert_eq[USize](0, a.size())
    h.assert_eq[USize](8, a.space())
    (a("0") = 0) as None
    (a("1") = 1) as None
    (a("2") = 2) as None
    (a("3") = 3) as None
    (a("4") = 4) as None
    (a("5") = 5) as None
    (a("6") = 6) as None

    h.assert_true(a.contains("0"),
      "contains did not find expected element in HashMap")
    h.assert_false(a.contains("7"),
      "contains found unexpected element in HashMap")

    h.assert_eq[U32](1, (a("1") = 11) as U32)
    h.assert_eq[U32](11, (a("1") = 1) as U32)

    let b = Map[String, U32]
    h.assert_eq[U32](0, b.insert("0", 0)?)
    h.assert_eq[U32](1, b.insert("1", 1)?)
    h.assert_eq[U32](2, b.insert("2", 2)?)
    h.assert_eq[U32](3, b.insert("3", 3)?)
    h.assert_eq[U32](4, b.insert("4", 4)?)
    h.assert_eq[U32](5, b.insert("5", 5)?)
    h.assert_eq[U32](6, b.insert("6", 6)?)

    h.assert_eq[USize](7, a.size())
    h.assert_eq[USize](16, a.space())
    h.assert_eq[USize](7, b.size())
    h.assert_eq[USize](16, b.space())

    var pair_count: USize = 0
    for (k, v) in a.pairs() do
      h.assert_eq[U32](b(k)?, v)
      pair_count = pair_count + 1
    end
    h.assert_eq[USize](7, pair_count)

    let a_removed_pair = a.remove("1")?
    h.assert_eq[String]("1", a_removed_pair._1)
    h.assert_eq[U32](1, a_removed_pair._2)
    h.assert_eq[USize](6, a.size())
    h.assert_eq[USize](16, a.space())

    a.compact()
    h.assert_eq[USize](6, a.size())
    h.assert_eq[USize](8, a.space())

    b.clear()
    h.assert_eq[USize](0, b.size())
    h.assert_eq[USize](8, b.space())

    let c = Map[String, U32]
    c.insert_if_absent("a", 1)?
    h.assert_eq[U32](1, c("a")?)
    c.insert_if_absent("a", 2)?
    h.assert_eq[U32](1, c("a")?)
    h.assert_eq[U32](0, c.insert_if_absent("0", 0)?)
    h.assert_eq[U32](0, c.insert_if_absent("0", 1)?)

class iso _TestMapRemove is UnitTest
  fun name(): String => "collections/Map.remove"

  fun apply(h: TestHelper) ? =>
    let x: Map[U32, String] = Map[U32,String]
    x(1) = "one"
    h.assert_eq[USize](x.size(), 1)
    x.remove(1)?
    h.assert_eq[USize](x.size(), 0)

    x(2) = "two"
    h.assert_eq[USize](x.size(), 1)
    x.remove(2)?
    h.assert_eq[USize](x.size(), 0)

    x(11) = "here"
    h.assert_eq[String](x(11)?, "here")

class iso _TestMapUpsert is UnitTest
  fun name(): String => "collections/Map.upsert"

  fun apply(h: TestHelper) ? =>
    let x: Map[U32, U64] = Map[U32,U64]
    let f = {(x: U64, y: U64): U64 => x + y }
    x.upsert(1, 5, f)?
    h.assert_eq[U64](5, x(1)?)
    x.upsert(1, 3, f)?
    h.assert_eq[U64](8, x(1)?)
    x.upsert(1, 4, f)?
    h.assert_eq[U64](12, x(1)?)
    x.upsert(1, 2, f)?
    h.assert_eq[U64](14, x(1)?)
    x.upsert(1, 1, f)?
    h.assert_eq[U64](15, x(1)?)

    let x2: Map[U32, String] = Map[U32,String]
    let g = {(x: String, y: String): String => x + ", " + y }
    x2.upsert(1, "1", g)?
    h.assert_eq[String]("1", x2(1)?)
    x2.upsert(1, "2", g)?
    h.assert_eq[String]("1, 2", x2(1)?)
    x2.upsert(1, "3", g)?
    h.assert_eq[String]("1, 2, 3", x2(1)?)
    x2.upsert(1, "abc", g)?
    h.assert_eq[String]("1, 2, 3, abc", x2(1)?)
    x2.upsert(1, "def", g)?
    h.assert_eq[String]("1, 2, 3, abc, def", x2(1)?)

    // verify correct results when we trigger a resize
    let prealloc: USize = 6
    let expected_initial_size: USize = (prealloc * 4) / 3
    let x3: Map[U32, U64] = Map[U32,U64](prealloc)
    let f' = {(x: U64, y: U64): U64 => x + y }
    h.assert_eq[USize](expected_initial_size, x3.space())
    h.assert_eq[U64](1, x3.upsert(1, 1, f')?)
    h.assert_eq[U64](1, x3.upsert(2, 1, f')?)
    h.assert_eq[U64](1, x3.upsert(3, 1, f')?)
    h.assert_eq[U64](1, x3.upsert(4, 1, f')?)
    h.assert_eq[U64](1, x3.upsert(5, 1, f')?)
    h.assert_eq[U64](1, x3.upsert(6, 1, f')?)
    h.assert_eq[U64](1, x3.upsert(7, 1, f')?)
    h.assert_eq[U64](2, x3.upsert(5, 1, f')?)
    h.assert_ne[USize](expected_initial_size, x3.space())

    let x4: Map[I32, I64] = Map[I32,I64]
    let i = {(x: I64, y: I64): I64 => x - y }
    x4.upsert(2, 4, i)?
    h.assert_eq[I64](4, x4(2)?)
    x4.upsert(2, 4, i)?
    h.assert_eq[I64](0, x4(2)?)
    x4.upsert(2, 4, i)?
    h.assert_eq[I64](-4, x4(2)?)

class iso _TestMapHashFunc is UnitTest
  fun name(): String => "collections/Map.hashfunc"

  fun apply(h: TestHelper) =>
    let a1: Array[U8] = [65; 77; 83; 0; 0; 39; 15; 0; 6; 200; 28; 0; 0; 0; 107]
    let a2: Array[U8] = [65; 77; 83; 0; 0; 39; 15; 0; 6; 200; 28; 0; 0; 0; 173]
    let h1: USize = @ponyint_hash_block[USize](a1.cpointer(), a1.size())
    let h2: USize = @ponyint_hash_block[USize](a2.cpointer(), a2.size())

    // The expected values on the right are generated by two reference
    // implementations:
    // C via NetBSD's src/external/mit/expat/dist/lib/siphash.h
    // C++ via https://github.com/bloomberg/bde/blob/master/groups/bsl/bslh/bslh_siphashalgorithm.cpp
    h.assert_eq[USize](h1, 13698363685738975395)
    h.assert_eq[USize](h2, 17028932028986796913)

class iso _TestRing is UnitTest
  fun name(): String => "collections/RingBuffer"

  fun apply(h: TestHelper) ? =>
    let a = RingBuffer[U64](4)
    a .> push(0) .> push(1) .> push(2) .> push(3)

    h.assert_eq[U64](a(0)?, 0)
    h.assert_eq[U64](a(1)?, 1)
    h.assert_eq[U64](a(2)?, 2)
    h.assert_eq[U64](a(3)?, 3)

    a .> push(4) .> push(5)
{'fraction_non_alphanumeric': 0.21018604985274048, 'fraction_numerical': 0.06982257021765678, 'mean_word_length': 3.2200666868748105, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 61, 'https://': 1, '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': '4376711', 'n_tokens_mistral': 6839, 'n_tokens_neox': 6306, 'n_words': 1203}