cons-lists/test/lists.coffee

90 lines
2.5 KiB
CoffeeScript
Raw Normal View History

2015-05-13 23:20:16 +00:00
chai = require 'chai'
chai.should()
expect = chai.expect
2015-05-13 23:20:16 +00:00
{listToVector, vectorToList, cons, list, nil,
metacadr, car, cdr, cadr} = require '../src/lists'
2015-05-13 23:20:16 +00:00
describe "Basic list building", ->
for [t, v] in [
[cons(), cons()]
[cons('a'), cons('a')]
[cons('a', cons('b', cons('c'))), cons('a', cons('b', cons('c')))]
[cons('a', cons('b', cons('c'))), cons('a', cons('b', cons('c', nil)))]
[cons('a', cons('b', cons('c', nil))), cons('a', cons('b', cons('c')))]]
2015-05-13 23:20:16 +00:00
do (t, v) ->
it "should match #{t}", ->
expect(t).to.deep.equal(v)
mcsimple = cons('a', cons('b', cons('c')))
describe "Basic list traversing", ->
it "should car", ->
expect(car mcsimple).to.equal("a")
it "should cadr", ->
expect(cadr mcsimple).to.equal("b")
it "should car cdr cdr", ->
expect(car cdr cdr mcsimple).to.equal("c")
2015-05-13 23:20:16 +00:00
describe 'Round trip equivalence', ->
for [t, v] in [
[[], []]
[['a'], ['a']]
[['a', 'b'], ['a', 'b']]
[['a', 'b', 'c'], ['a', 'b', 'c']]]
do (t, v) ->
it "should successfully round-trip #{t}", ->
expect(listToVector vectorToList t).to.deep.equal(v)
2015-05-13 23:20:16 +00:00
describe 'List Building', ->
for [t, v] in [
[cons(), []]
[cons(nil), [nil]]
2015-05-13 23:20:16 +00:00
[cons('a'), ['a']]
[cons('a', cons('b')), ['a', 'b']]
[cons('a', cons('b', cons('c'))), ['a', 'b', 'c']]
[cons('a', cons('b', cons('c'), nil)), ['a', 'b', 'c']]]
do (t, v) ->
it "should cons a list into #{v}", ->
expect(listToVector t).to.deep.equal(v)
2015-05-13 23:20:16 +00:00
describe 'Dynamic list constructor', ->
2015-05-13 23:20:16 +00:00
for [t, v] in [
[list(), []]
[list('a'), ['a']]
[list('a', 'b'), ['a', 'b']]
[list('a', 'b', 'c'), ['a', 'b', 'c']]]
do (t, v) ->
it "should round trip list arguments into #{v}", ->
expect(listToVector t).to.deep.equal(v)
mcsimple = cons('a', cons('b', cons('c')))
describe 'Metacadr Simple', ->
2015-05-13 23:20:16 +00:00
for [t, v, r] in [
['car', 'a']
['cadr', 'b']
['caddr', 'c']]
do (t, v) ->
it "The #{t} should read #{v}", ->
expect(metacadr(t)(mcsimple)).to.equal(v)
mccomplex = vectorToList([
['a', 'b', 'c'],
['1', '2', '3'],
['X', 'Y', 'Z'],
['f', 'g', 'h']])
2015-05-13 23:20:16 +00:00
describe 'Metacadr Complex', ->
2015-05-13 23:20:16 +00:00
for [t, v, r] in [
['cadar', 'b']
['caadddr', 'f']
['caadr', '1']
['caaddr', 'X']]
do (t, v) ->
it "The #{t} should read #{v}", ->
mcadr = metacadr(t)
expect(mcadr mccomplex).to.equal(v)
expect(mcadr mccomplex).to.equal(v)
expect(mcadr mccomplex).to.equal(v)