"Fossies" - the Fresh Open Source Software Archive

Member "ponyc-0.33.0/packages/collections/persistent/_test.pony" (1 Nov 2019, 15749 Bytes) of package /linux/misc/ponyc-0.33.0.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Pony source code syntax highlighting (style: standard) with prefixed line numbers. Alternatively you can here view or download the uninterpreted source code file. See also the last Fossies "Diffs" side-by-side code changes report for "_test.pony": 0.30.0_vs_0.31.0.

    1 use "ponytest"
    2 use mut = "collections"
    3 use "random"
    4 use "time"
    5 
    6 actor Main is TestList
    7   new create(env: Env) => PonyTest(env, this)
    8   new make() => None
    9 
   10   fun tag tests(test: PonyTest) =>
   11     run_tests(test)
   12 
   13   fun tag run_tests(test: PonyTest) =>
   14     test(_TestListPrepend)
   15     test(_TestListFrom)
   16     test(_TestListApply)
   17     test(_TestListValues)
   18     test(_TestListConcat)
   19     test(_TestListMap)
   20     test(_TestListFlatMap)
   21     test(_TestListFilter)
   22     test(_TestListFold)
   23     test(_TestListEveryExists)
   24     test(_TestListPartition)
   25     test(_TestListDrop)
   26     test(_TestListDropWhile)
   27     test(_TestListTake)
   28     test(_TestListTakeWhile)
   29     test(_TestMap)
   30     test(_TestMapVsMap)
   31     test(_TestSet)
   32     test(_TestVec)
   33     test(_TestVecIterators)
   34 
   35 class iso _TestListPrepend is UnitTest
   36   fun name(): String => "collections/persistent/List (prepend)"
   37 
   38   fun apply(h: TestHelper) ? =>
   39     let a = Lists[U32].empty()
   40     let b = Lists[U32].cons(1, Lists[U32].empty())
   41     let c = Lists[U32].cons(2, b)
   42     let d = c.prepend(3)
   43     let e = a.prepend(10)
   44 
   45     h.assert_eq[USize](a.size(), 0)
   46     h.assert_eq[USize](b.size(), 1)
   47     h.assert_eq[USize](c.size(), 2)
   48     h.assert_eq[USize](d.size(), 3)
   49     h.assert_eq[USize](e.size(), 1)
   50 
   51     h.assert_eq[U32](b.head()?, 1)
   52     h.assert_eq[USize](b.tail()?.size(), 0)
   53     h.assert_eq[U32](c.head()?, 2)
   54     h.assert_eq[USize](c.tail()?.size(), 1)
   55     h.assert_eq[U32](d.head(), 3)
   56     h.assert_eq[USize](d.tail().size(), 2)
   57     h.assert_eq[U32](e.head(), 10)
   58     h.assert_eq[USize](e.tail().size(), 0)
   59 
   60 class iso _TestListFrom is UnitTest
   61   fun name(): String => "collections/persistent/Lists (from)"
   62 
   63   fun apply(h: TestHelper) ? =>
   64     let l1 = Lists[U32].from([1; 2; 3].values())
   65     h.assert_eq[USize](l1.size(), 3)
   66     h.assert_eq[U32](l1.head()?, 1)
   67 
   68 class iso _TestListApply is UnitTest
   69   fun name(): String => "collections/persistent/List (apply)"
   70 
   71   fun apply(h: TestHelper) ? =>
   72     let l1 = Lists[U32]([1; 2; 3])
   73     h.assert_eq[U32](l1(0)?, 1)
   74     h.assert_eq[U32](l1(1)?, 2)
   75     h.assert_eq[U32](l1(2)?, 3)
   76     h.assert_error({() ? => l1(3)? })
   77     h.assert_error({() ? => l1(4)? })
   78 
   79     let l2 = Lists[U32].empty()
   80     h.assert_error({() ? => l2(0)? })
   81 
   82 class iso _TestListValues is UnitTest
   83   fun name(): String => "collections/persistent/List (values)"
   84 
   85   fun apply(h: TestHelper) ? =>
   86     let iter = Lists[U32]([1; 2; 3]).values()
   87     h.assert_true(iter.has_next())
   88     h.assert_eq[U32](iter.next()?, 1)
   89     h.assert_true(iter.has_next())
   90     h.assert_eq[U32](iter.next()?, 2)
   91     h.assert_true(iter.has_next())
   92     h.assert_eq[U32](iter.next()?, 3)
   93     h.assert_false(iter.has_next())
   94     h.assert_false(try iter.next()?; true else false end)
   95     h.assert_false(iter.has_next())
   96     h.assert_false(try iter.next()?; true else false end)
   97 
   98 class iso _TestListConcat is UnitTest
   99   fun name(): String => "collections/persistent/List (concat)"
  100 
  101   fun apply(h: TestHelper) ? =>
  102     let l1 = Lists[U32]([1; 2; 3])
  103     let l2 = Lists[U32]([4; 5; 6])
  104     let l3 = l1.concat(l2)
  105     let l4 = l3.reverse()
  106     h.assert_eq[USize](l3.size(), 6)
  107     h.assert_true(Lists[U32].eq(l3, Lists[U32]([1; 2; 3; 4; 5; 6]))?)
  108     h.assert_true(Lists[U32].eq(l4, Lists[U32]([6; 5; 4; 3; 2; 1]))?)
  109 
  110     let l5 = Lists[U32].empty()
  111     let l6 = l5.reverse()
  112     let l7 = l6.concat(l1)
  113     h.assert_eq[USize](l6.size(), 0)
  114     h.assert_true(Lists[U32].eq(l7, Lists[U32]([1; 2; 3]))?)
  115 
  116     let l8 = Lists[U32]([1])
  117     let l9 = l8.reverse()
  118     h.assert_true(Lists[U32].eq(l9, Lists[U32]([1]))?)
  119 
  120 class iso _TestListMap is UnitTest
  121   fun name(): String => "collections/persistent/Lists (map)"
  122 
  123   fun apply(h: TestHelper) ? =>
  124     let l5 = Lists[U32]([1; 2; 3]).map[U32]({(x) => x * 2 })
  125     h.assert_true(Lists[U32].eq(l5, Lists[U32]([2; 4; 6]))?)
  126 
  127 class iso _TestListFlatMap is UnitTest
  128   fun name(): String => "collections/persistent/Lists (flat_map)"
  129 
  130   fun apply(h: TestHelper) ? =>
  131     let f = {(x: U32): List[U32] => Lists[U32]([x - 1; x; x + 1]) }
  132     let l6 = Lists[U32]([2; 5; 8]).flat_map[U32](f)
  133     h.assert_true(Lists[U32].eq(l6, Lists[U32]([1; 2; 3; 4; 5; 6; 7; 8; 9]))?)
  134 
  135 class iso _TestListFilter is UnitTest
  136   fun name(): String => "collections/persistent/Lists (filter)"
  137 
  138   fun apply(h: TestHelper) ? =>
  139     let is_even = {(x: U32): Bool => (x % 2) == 0 }
  140     let l7 = Lists[U32]([1; 2; 3; 4; 5; 6; 7; 8]).filter(is_even)
  141     h.assert_true(Lists[U32].eq(l7, Lists[U32]([2; 4; 6; 8]))?)
  142 
  143 class iso _TestListFold is UnitTest
  144   fun name(): String => "collections/persistent/Lists (fold)"
  145 
  146   fun apply(h: TestHelper) ? =>
  147     let add = {(acc: U32, x: U32): U32 => acc + x }
  148     let value = Lists[U32]([1; 2; 3]).fold[U32](add, 0)
  149     h.assert_eq[U32](value, 6)
  150 
  151     let doubleAndPrepend =
  152       {(acc: List[U32], x: U32): List[U32] => acc.prepend(x * 2) }
  153     let l8 =
  154       Lists[U32]([1; 2; 3]).fold[List[U32]](
  155         doubleAndPrepend, Lists[U32].empty())
  156     h.assert_true(Lists[U32].eq(l8, Lists[U32]([6; 4; 2]))?)
  157 
  158 class iso _TestListEveryExists is UnitTest
  159   fun name(): String => "collections/persistent/Lists (every, exists)"
  160 
  161   fun apply(h: TestHelper) =>
  162     let is_even = {(x: U32): Bool => (x % 2) == 0 }
  163     let l9 = Lists[U32]([4; 2; 10])
  164     let l10 = Lists[U32]([1; 1; 3])
  165     let l11 = Lists[U32]([1; 1; 2])
  166     let l12 = Lists[U32]([2; 2; 3])
  167     let l13 = Lists[U32].empty()
  168     h.assert_eq[Bool](l9.every(is_even), true)
  169     h.assert_eq[Bool](l10.every(is_even), false)
  170     h.assert_eq[Bool](l11.every(is_even), false)
  171     h.assert_eq[Bool](l12.every(is_even), false)
  172     h.assert_eq[Bool](l13.every(is_even), true)
  173     h.assert_eq[Bool](l9.exists(is_even), true)
  174     h.assert_eq[Bool](l10.exists(is_even), false)
  175     h.assert_eq[Bool](l11.exists(is_even), true)
  176     h.assert_eq[Bool](l12.exists(is_even), true)
  177     h.assert_eq[Bool](l13.exists(is_even), false)
  178 
  179 class iso _TestListPartition is UnitTest
  180   fun name(): String => "collections/persistent/Lists (partition)"
  181 
  182   fun apply(h: TestHelper) ? =>
  183     let is_even = {(x: U32): Bool => (x % 2) == 0 }
  184     let l = Lists[U32]([1; 2; 3; 4; 5; 6])
  185     (let hits, let misses) = l.partition(is_even)
  186     h.assert_true(Lists[U32].eq(hits, Lists[U32]([2; 4; 6]))?)
  187     h.assert_true(Lists[U32].eq(misses, Lists[U32]([1; 3; 5]))?)
  188 
  189 class iso _TestListDrop is UnitTest
  190   fun name(): String => "collections/persistent/List (drop)"
  191 
  192   fun apply(h: TestHelper) ? =>
  193     let l = Lists[String](["a"; "b"; "c"; "d"; "e"])
  194     let l2 = Lists[U32]([1; 2])
  195     let empty = Lists[String].empty()
  196     h.assert_true(Lists[String].eq(l.drop(3), Lists[String](["d"; "e"]))?)
  197     h.assert_true(Lists[U32].eq(l2.drop(3), Lists[U32].empty())?)
  198     h.assert_true(Lists[String].eq(empty.drop(3), Lists[String].empty())?)
  199 
  200 class iso _TestListDropWhile is UnitTest
  201   fun name(): String => "collections/persistent/List (drop_while)"
  202 
  203   fun apply(h: TestHelper) ? =>
  204     let is_even = {(x: U32): Bool => (x % 2) == 0 }
  205     let l = Lists[U32]([4; 2; 6; 1; 3; 4; 6])
  206     let empty = Lists[U32].empty()
  207     h.assert_true(Lists[U32].eq(l.drop_while(is_even),
  208       Lists[U32]([1; 3; 4; 6]))?)
  209     h.assert_true(Lists[U32].eq(empty.drop_while(is_even), Lists[U32].empty())?)
  210 
  211 class iso _TestListTake is UnitTest
  212   fun name(): String => "collections/persistent/List (take)"
  213 
  214   fun apply(h: TestHelper) ? =>
  215     let l = Lists[String](["a"; "b"; "c"; "d"; "e"])
  216     let l2 = Lists[U32]([1; 2])
  217     let empty = Lists[String].empty()
  218     h.assert_true(Lists[String].eq(l.take(3), Lists[String](["a"; "b"; "c"]))?)
  219     h.assert_true(Lists[U32].eq(l2.take(3), Lists[U32]([1; 2]))?)
  220     h.assert_true(Lists[String].eq(empty.take(3), Lists[String].empty())?)
  221 
  222 class iso _TestListTakeWhile is UnitTest
  223   fun name(): String => "collections/persistent/List (take_while)"
  224 
  225   fun apply(h: TestHelper) ? =>
  226     let is_even = {(x: U32): Bool => (x % 2) == 0 }
  227     let l = Lists[U32]([4; 2; 6; 1; 3; 4; 6])
  228     let empty = Lists[U32].empty()
  229     h.assert_true(Lists[U32].eq(l.take_while(is_even), Lists[U32]([4; 2; 6]))?)
  230     h.assert_true(Lists[U32].eq(empty.take_while(is_even), Lists[U32].empty())?)
  231 
  232 class iso _TestMap is UnitTest
  233   fun name(): String =>
  234     "collections/persistent/Map (update, remove, concat, add, sub)"
  235 
  236   fun apply(h: TestHelper) ? =>
  237     let m1 = Map[String,U32]
  238     h.assert_error({() ? => m1("a")? })
  239     let s1 = m1.size()
  240     h.assert_eq[USize](s1, 0)
  241 
  242     let m2 = m1("a") = 5
  243     let m3 = m2("b") = 10
  244     let m4 = m3("a") = 4
  245     let m5 = m4("c") = 0
  246     h.assert_eq[U32](m2("a")?, 5)
  247     h.assert_eq[U32](m3("b")?, 10)
  248     h.assert_eq[U32](m4("a")?, 4)
  249     h.assert_eq[U32](m5("c")?, 0)
  250 
  251     let vs = [as (String, U32): ("a", 2); ("b", 3); ("d", 4); ("e", 5)]
  252     let m6 = Map[String,U32].concat(vs.values())
  253     let m7 = m6("a") = 10
  254     h.assert_eq[U32](m6("a")?, 2)
  255     h.assert_eq[U32](m6("b")?, 3)
  256     h.assert_eq[U32](m6("d")?, 4)
  257     h.assert_eq[U32](m6("e")?, 5)
  258     h.assert_eq[U32](m7("a")?, 10)
  259     h.assert_eq[U32](m7("b")?, 3)
  260     h.assert_eq[U32](m7("a")?, 10)
  261     let m8 = m7.remove("a")?
  262     h.assert_error({() ? => m8("a")? })
  263     h.assert_eq[U32](m8("b")?, 3)
  264     h.assert_eq[U32](m8("d")?, 4)
  265     h.assert_eq[U32](m8("e")?, 5)
  266     let m9 = m7.remove("e")?
  267     h.assert_error({() ? => m9("e")? })
  268     h.assert_eq[U32](m9("b")?, 3)
  269     h.assert_eq[U32](m9("d")?, 4)
  270     let m10 = m9.remove("b")?.remove("d")?
  271     h.assert_error({() ? => m10("b")? })
  272     h.assert_error({() ? => m10("d")? })
  273 
  274     let m11 = m10 + ("b", 3)
  275     h.assert_eq[U32](m11("b")?, 3)
  276     let m12 = m11 - "b"
  277     h.assert_error({() ? => m12("b")? })
  278 
  279     let seed = Time.millis()
  280     h.log("seed: " + seed.string())
  281     let rand = Rand(seed)
  282 
  283     var map = Map[USize, None]
  284     for n in mut.Range(0, 100) do
  285       try
  286         map(USize.max_value())?
  287       else
  288         h.fail()
  289         return
  290       end
  291       map = map.update(rand.int[USize](USize.max_value() - 1), None)
  292     end
  293 
  294     gen_test(h, rand)?
  295 
  296   fun gen_test(h: TestHelper, rand: Rand) ? =>
  297     var a = _Map
  298     let b = mut.Map[U64, U64]
  299 
  300     let ops = gen_ops(300, rand)?
  301     for op in ops.values() do
  302       h.log(op.str())
  303       let prev = a
  304       a = op(a, b)?
  305 
  306       var n: USize = 0
  307       for (k, v) in a.pairs() do
  308         n = n + 1
  309         h.assert_eq[U64](b(k)?, v)
  310       end
  311       h.assert_eq[USize](n, a.size())
  312     end
  313 
  314   fun gen_ops(n: USize, rand: Rand): Array[_TestOp] ? =>
  315     let ops = Array[_TestOp](n)
  316     let keys = Array[U64](n)
  317     for v in mut.Range[U64](0, n.u64()) do
  318       let op_n = if keys.size() == 0 then 0 else rand.int[U64](4) end
  319       ops.push(
  320         match op_n
  321         | 0 | 1 =>
  322           // insert
  323           let k = rand.u64()
  324           keys.push(k)
  325           _OpMapUpdate(k, v)
  326         | 2 =>
  327           // update
  328           let k = keys(rand.int[USize](keys.size()))?
  329           _OpMapUpdate(k, v)
  330         | 3 =>
  331           // remove
  332           let k = keys.delete(rand.int[USize](keys.size()))?
  333           _OpMapRemove(k)
  334         else error
  335         end)
  336     end
  337     ops
  338 
  339 type _Map is HashMap[U64, U64, CollisionHash]
  340 
  341 primitive CollisionHash is mut.HashFunction[U64]
  342   fun hash(x: U64): USize => x.usize() % 100
  343   fun eq(x: U64, y: U64): Bool => x == y
  344 
  345 interface val _TestOp
  346   fun apply(a: _Map, b: mut.Map[U64, U64]): _Map ?
  347   fun str(): String
  348 
  349 class val _OpMapUpdate
  350   let k: U64
  351   let v: U64
  352 
  353   new val create(k': U64, v': U64) =>
  354     k = k'
  355     v = v'
  356 
  357   fun apply(a: _Map, b: mut.Map[U64, U64]): _Map =>
  358     b.update(k, v)
  359     a(k) = v
  360 
  361   fun str(): String =>
  362     "".join(["Update("; k; "_"; CollisionHash.hash(k); ", "; v; ")"].values())
  363 
  364 class val _OpMapRemove
  365   let k: U64
  366 
  367   new val create(k': U64) =>
  368     k = k'
  369 
  370   fun apply(a: _Map, b: mut.Map[U64, U64]): _Map ? =>
  371     b.remove(k)?
  372     a.remove(k)?
  373 
  374   fun str(): String =>
  375     "".join(["Remove("; k; ")"].values())
  376 
  377 class iso _TestMapVsMap is UnitTest
  378   fun name(): String => "collections/persistent/Map (persistent vs mutable)"
  379 
  380   fun apply(h: TestHelper) ? =>
  381     let keys: USize = 300
  382     var p_map = Map[String, U64]
  383     let m_map = mut.Map[String, U64](keys)
  384     let r = MT
  385 
  386     var count: USize = 0
  387     while count < keys do
  388       let k: String = count.string()
  389       let v = r.next()
  390       p_map = p_map(k) = v
  391       m_map(k) = v
  392       count = count + 1
  393       h.assert_eq[USize](m_map.size(), p_map.size())
  394     end
  395 
  396     h.assert_eq[USize](m_map.size(), keys)
  397     h.assert_eq[USize](p_map.size(), keys)
  398 
  399     for (k, v) in m_map.pairs() do
  400       h.assert_eq[U64](p_map(k)?, v)
  401     end
  402 
  403     var c: USize = 0
  404     for (k, v) in p_map.pairs() do
  405       c = c + 1
  406       m_map.remove(k)?
  407     end
  408     h.assert_eq[USize](p_map.size(), c)
  409     h.assert_eq[USize](m_map.size(), 0)
  410 
  411 class iso _TestSet is UnitTest
  412   fun name(): String => "collections/persistent/Set"
  413 
  414   fun apply(h: TestHelper) =>
  415     let a = Set[USize] + 1 + 2 + 3
  416     let b = Set[USize] + 2 + 3 + 4
  417 
  418     h.assert_false(a == b)
  419     h.assert_true(a != b)
  420 
  421     h.assert_false(a.contains(4))
  422     h.assert_true((a + 4).contains(4))
  423 
  424     h.assert_true(a.contains(3))
  425     h.assert_false((a - 3).contains(3))
  426 
  427     h.assert_true((a or b) == (Set[USize] + 1 + 2 + 3 + 4))
  428     h.assert_true((b or a) == (Set[USize] + 1 + 2 + 3 + 4))
  429     h.assert_true((a and b) == (Set[USize] + 2 + 3))
  430     h.assert_true((b and a) == (Set[USize] + 2 + 3))
  431     h.assert_true((a xor b) == (Set[USize] + 1 + 4))
  432     h.assert_true((b xor a) == (Set[USize] + 1 + 4))
  433     h.assert_true(a.without(b) == (Set[USize] + 1))
  434     h.assert_true(b.without(a) == (Set[USize] + 4))
  435 
  436 class iso _TestVec is UnitTest
  437   fun name(): String => "collections/persistent/Vec"
  438 
  439   fun apply(h: TestHelper) ? =>
  440     var v = Vec[USize]
  441     let n: USize = 33_000 // resize up to 4 levels in depth
  442 
  443     // push
  444     for i in mut.Range(0, n) do
  445       v = v.push(i)
  446       h.assert_eq[USize](v(i)?, i)
  447     end
  448 
  449     // update
  450     for i in mut.Range(0, n) do
  451       v = v.update(i, -i)?
  452       h.assert_eq[USize](v(i)?, -i)
  453     end
  454 
  455     var idx: USize = 0
  456     for num in v.values() do
  457       h.assert_eq[USize](num, -idx)
  458       idx = idx + 1
  459     end
  460     h.assert_eq[USize](v.size(), idx)
  461 
  462     // pop
  463     for i in mut.Range(0, n) do
  464       v = v.pop()?
  465       h.assert_error({() ? => v(n - i)? })
  466       h.assert_eq[USize](v.size(), n - i - 1)
  467     end
  468 
  469     // concat
  470     v = Vec[USize].concat(mut.Range(0, n))
  471     for i in mut.Range(0, n) do
  472       h.assert_eq[USize](v(i)?, i)
  473     end
  474 
  475     // insert
  476     let insert_idx: USize = 14
  477     v = v.insert(insert_idx, 9999)?
  478     h.assert_eq[USize](v(insert_idx - 1)?, insert_idx - 1)
  479     h.assert_eq[USize](v(insert_idx)?, 9999)
  480     h.assert_eq[USize](v(insert_idx + 1)?, insert_idx)
  481     h.assert_eq[USize](v.size(), n + 1)
  482     h.assert_error({() ? => v.insert(v.size(), 0)? })
  483     h.assert_error({() ? => v.insert(-1, 0)? })
  484 
  485     // delete
  486     v = v.delete(insert_idx)?
  487     h.assert_eq[USize](v(insert_idx - 1)?, insert_idx - 1)
  488     h.assert_eq[USize](v(insert_idx)?, insert_idx)
  489     h.assert_eq[USize](v.size(), n)
  490     h.assert_error({() ? => v.delete(v.size())? })
  491     h.assert_error({() ? => v.delete(-1)? })
  492 
  493     // remove
  494     v = v.remove(0, 1)?
  495     h.assert_eq[USize](v(0)?, 1)
  496     h.assert_eq[USize](v(1)?, 2)
  497     h.assert_eq[USize](v.size(), n - 1)
  498 
  499     v = v.remove(10, 10)?
  500     h.assert_eq[USize](v(9)?, 10)
  501     h.assert_eq[USize](v(10)?, 21)
  502     h.assert_eq[USize](v(n - 12)?, n - 1)
  503     h.assert_eq[USize](v.size(), n - 11)
  504 
  505 class iso _TestVecIterators is UnitTest
  506   fun name(): String => "collections/persistent/Vec (iterators)"
  507 
  508   fun apply(h: TestHelper) ? =>
  509     let n: USize = 33_000 // resize up to 4 levels in depth
  510     var vec = Vec[USize]
  511     for i in mut.Range(0, n) do vec = vec.push(i) end
  512     var c = vec.size()
  513     for (i, v) in vec.pairs() do
  514       c = c - 1
  515       h.assert_eq[USize](v, vec(i)?)
  516     end
  517     h.assert_eq[USize](c, 0)