module list-misc-test
imports libstratego-lib

strategies

  main =
    test-suite(!"list-misc-test",
	copy-test;
	number-test;
	flatten-test;
	init-last-test;
	take-test;
	drop-test;
  trim-test
    )

  copy-test = 
    apply-test(!"copy-test"
	, copy
	, !(4, "a")
	, !["a", "a", "a", "a"]
	)


  number-test = 
    apply-test(!"number-test"
	, number(id)
	, !["a", "b", "c", "d"]
	, ![("a",0),("b",1),("c",2),("d",3)]
	)

  init-last-test = 
      apply-test(!"init"
      , init
      , ![1, 2, 3, 4]
      , ![1, 2, 3]
      )
    ; apply-test(!"empty init"
      , init
      , ![1]
      , ![]
      )
    ; apply-test(!"split-last"
      , split-last
      , ![1, 2, 3, 4]
      , !([1, 2, 3], 4)
      )
    ; apply-test(!"split-last"
      , split-last
      , ![1]
      , !([], 1)
      )

  flatten-test =
      apply-test(!"flatten-test 1", flatten-list
      , !["a",["b",["c","d"]],"e"]
      , !["a","b","c","d","e"]
      )
    ; apply-test(!"flatten-test 2", flatten-list
      , !["a",["b",[]],"e"]
      , !["a","b", "e"]
      )
    ; apply-test(!"flatten-test 3", flatten-list
      , ![]
      , ![]
      )
    ; apply-test(!"flatten-test 4", flatten-list
      , ![[]]
      , ![]
      )
    ; apply-test(!"flatten-test 5", flatten-list
      , !["a", "b", "c"]
      , !["a", "b", "c"]
      )
    ; apply-test(!"flatten-test 5", flatten-list
      , !["a", [("b", [1, 2]), "d"], "c"]
      , !["a", ("b", [1, 2]), "d", "c"]
      )

  take-test = id
    ; apply-test(!"take 3 of a list of more than 3 elements"
      , take(!3)
      , !["a","b","c","d","e"]
      , !["a","b","c"]
      )
    ; apply-test(!"take 0 returns the empty list"
      , take(!0)
      , !["a","b","c"]
      , ![]
      )
    ; apply-test(!"take 1 returns the first element"
      , take(!1)
      , !["a","b","c"]
      , !["a"]
      )
    ; apply-test(!"take 1 returns the first element"
      , take(!1)
      , !["a"]
      , !["a"]
      )
    ; apply-test(!"take 3 of a list of 3 elements returns the list"
      , take(!3)
      , !["a","b","c"]
      , !["a","b","c"]
      )
    ; apply-and-fail(!"take 3 fails on a list of two elements"
      , take(!3)
      , !["a","b"]
      )
    ; apply-and-fail(!"take 3 fails on the empty list"
      , take(!3)
      , ![]
      )
    ; apply-test(!"take 3 of a list of more than 3 elements"
      , take(|3)
      , !["a","b","c","d","e"]
      , !["a","b","c"]
      )
    ; apply-test(!"take 0 returns the empty list"
      , take(|0)
      , !["a","b","c"]
      , ![]
      )
    ; apply-test(!"take 1 returns the first element"
      , take(|1)
      , !["a","b","c"]
      , !["a"]
      )
    ; apply-test(!"take 1 returns the first element"
      , take(|1)
      , !["a"]
      , !["a"]
      )
    ; apply-test(!"take 3 of a list of 3 elements returns the list"
      , take(|3)
      , !["a","b","c"]
      , !["a","b","c"]
      )
    ; apply-and-fail(!"take 3 fails on a list of two elements"
      , take(|3)
      , !["a","b"]
      )
    ; apply-and-fail(!"take 3 fails on the empty list"
      , take(|3)
      , ![]
      )

  drop-test =
    apply-test(!"drop-test"
        , drop(!3)
	, !["a","b","c","d","e"]
	, !["d","e"]
    )

  trim-test =
      apply-test(!"ltrim-test 1", ltrim(?0)
      , ![0, 0, 0, 1, 2, 3, 0]
      , ![1, 2, 3, 0]
      )
    ; apply-test(!"ltrim-test 2", ltrim(?0)
      , ![1, 2, 3, 0]
      , ![1, 2, 3, 0]
      )
    ; apply-test(!"ltrim-test 3", ltrim(?0)
      , ![]
      , ![]
      )
    ; apply-test(!"rtrim-test 1", rtrim(?0)
      , ![0, 1, 2, 3, 0]
      , ![0, 1, 2, 3]
      )
    ; apply-test(!"rtrim-test 2", rtrim(?0)
      , ![1, 2, 3]
      , ![1, 2, 3]
      )
    ; apply-test(!"rtrim-test 3", rtrim(?0)
      , ![]
      , ![]
      )
    ; apply-test(!"trim-test 1", trim(?0)
      , ![0, 1, 2, 3, 0]
      , ![1, 2, 3]
      )
    ; apply-test(!"trim-test 2", trim(?0)
      , ![1, 2, 3]
      , ![1, 2, 3]
      )
    ; apply-test(!"trim-test 3", trim(?0)
      , ![]
      , ![]
      )
