2015-05-13 23:20:16 +00:00
|
|
|
chai = require 'chai'
|
|
|
|
chai.should()
|
2015-06-23 00:03:21 +00:00
|
|
|
expect = chai.expect
|
2015-05-13 23:20:16 +00:00
|
|
|
|
2015-06-23 00:03:21 +00:00
|
|
|
{listToVector, vectorToList,
|
|
|
|
listToString, cons, list, nil} = require '../src/lists'
|
2015-05-13 23:20:16 +00:00
|
|
|
{map, reduce, filter, reverse} = require '../src/reduce'
|
|
|
|
|
|
|
|
id = (item) -> item
|
2015-06-23 00:03:21 +00:00
|
|
|
|
2015-05-13 23:20:16 +00:00
|
|
|
describe 'Map Identity Testing', ->
|
|
|
|
|
|
|
|
samples = [
|
|
|
|
cons(),
|
|
|
|
cons(nil),
|
|
|
|
cons('a'),
|
|
|
|
cons('a', cons('b'))
|
|
|
|
cons('a', cons('b', cons('c'))),
|
|
|
|
cons('a', cons('b', cons('c'), nil))]
|
2015-06-23 00:03:21 +00:00
|
|
|
|
2015-05-13 23:20:16 +00:00
|
|
|
for t in samples
|
|
|
|
do (t) ->
|
|
|
|
it "should produce the same thing as #{t}", ->
|
|
|
|
product = map(t, id)
|
2015-06-23 00:03:21 +00:00
|
|
|
expect(product).to.deep.equal(t)
|
2015-05-13 23:20:16 +00:00
|
|
|
|
|
|
|
describe 'Filter Testing Testing', ->
|
|
|
|
|
|
|
|
samples = [
|
|
|
|
[vectorToList([]), nil],
|
|
|
|
[vectorToList([1]), nil],
|
|
|
|
[vectorToList([1, 2]), cons(2)],
|
|
|
|
[vectorToList([1, 2, 3]), cons(2)],
|
|
|
|
[vectorToList([1, 2, 3 ,4]), cons(2, cons(4))]]
|
|
|
|
|
|
|
|
truth = (item) -> item % 2 == 0
|
2015-06-23 00:03:21 +00:00
|
|
|
|
2015-05-13 23:20:16 +00:00
|
|
|
for [t, v] in samples
|
|
|
|
do (t, v) ->
|
|
|
|
it "should produce the same thing as #{v}", ->
|
|
|
|
product = filter(t, truth)
|
2015-06-23 00:03:21 +00:00
|
|
|
expect(product).to.deep.equal(v)
|
2015-05-13 23:20:16 +00:00
|
|
|
|
|
|
|
describe 'Reverse', ->
|
|
|
|
|
|
|
|
samples = [
|
|
|
|
[cons(), cons()]
|
|
|
|
[cons(nil), cons(nil)]
|
|
|
|
[cons('a'), cons('a')]
|
|
|
|
[cons('a', cons('b')), cons('b', cons('a'))]
|
|
|
|
[cons('a', cons('b', cons('c'))), cons('c', cons('b', cons('a')))]]
|
|
|
|
|
|
|
|
for [t, v] in samples
|
|
|
|
do (t, v) ->
|
|
|
|
it "#{t} should produce a reverse of #{v}", ->
|
|
|
|
product = reverse(t)
|
|
|
|
expect(product).to.deep.equal(v)
|