PageRenderTime 173ms CodeModel.GetById 68ms app.highlight 23ms RepoModel.GetById 60ms app.codeStats 0ms

/tests.coffee

http://github.com/MichaelBlume/coffeestream
CoffeeScript | 163 lines | 124 code | 36 blank | 3 comment | 7 complexity | c52da07d935946c273807168c192629a MD5 | raw file
  1#! /usr/bin/env coffee
  2{Stream, primeStream} = require "./stream"
  3
  4assert = (b) ->
  5  if not b
  6    console.log 'FUCK'
  7    throw 'fuck'
  8
  9eq = (a,b) ->
 10    if a==b
 11        return
 12    console.log a
 13    console.log b
 14    throw 'fuck'
 15
 16eqList = (s,l) ->
 17  if s.equal Stream.make l...
 18      return
 19  console.log s.list()
 20  console.log l
 21  throw 'fuck'
 22
 23
 24#before we start, make sure this equal thing works
 25one_to_five = Stream.range(1,5)
 26one_to_ten = Stream.range(1,10)
 27six_to_ten = Stream.range(6,10)
 28empty = new Stream()
 29assert not one_to_five.equal one_to_ten
 30assert not empty.equal one_to_five
 31assert not one_to_five.equal empty
 32assert not one_to_five.equal six_to_ten
 33
 34assert one_to_five.member(1)
 35assert one_to_five.member(5)
 36assert not one_to_five.member(6)
 37
 38s = Stream.make(10, 20, 30)
 39eqList s, [10,20,30]
 40eq s.length(), 3
 41eq s.head(), 10
 42eq s.item(0), 10
 43eq s.item(1), 20
 44eq s.item(2), 30
 45eq s.sum(), 60
 46
 47t = s.tail()
 48eq t.head(), 20
 49u = t.tail()
 50eq u.head(), 30
 51v = u.tail()
 52assert v.empty()
 53
 54
 55s = Stream.range(10, 20)
 56eqList s, [10..20]
 57
 58
 59doubleNumber = (x) ->
 60  2 * x
 61
 62numbers = Stream.range(10, 15)
 63doubles = numbers.map(doubleNumber)
 64eqList doubles, [20,22,24,26,28,30]
 65
 66checkIfOdd = (x) ->
 67  if x % 2 == 0
 68    false
 69  else
 70    true
 71numbers = Stream.range(10, 15)
 72onlyOdds = numbers.filter(checkIfOdd)
 73eqList onlyOdds, [11,13,15]
 74
 75isFive = (x) -> x==5
 76
 77eqList Stream.range(1).until(isFive), [1..4]
 78eqList Stream.range(1).up_until(isFive), [1..5]
 79
 80printItem = (x) ->
 81  console.log "The element is: " + x
 82numbers = Stream.range(10, 12)
 83#test this better
 84numbers.walk printItem
 85
 86numbers = Stream.range(10, 100)
 87fewerNumbers = numbers.take(10)
 88eqList fewerNumbers, [10..19]
 89numbers = Stream.range(1, 3)
 90multiplesOfTen = numbers.scale(10)
 91eqList multiplesOfTen, [10,20,30]
 92eqList numbers.add(multiplesOfTen), [11,22,33]
 93
 94naturalNumbers = Stream.range()
 95oneToTen = naturalNumbers.take(10)
 96eqList oneToTen, [1..10]
 97
 98evenNumbers = naturalNumbers.map((x) ->
 99  2 * x
100)
101oddNumbers = naturalNumbers.filter((x) ->
102  x % 2 != 0
103)
104eqList evenNumbers.take(3), [2,4,6]
105eqList oddNumbers.take(3), [1,3,5]
106
107s = new Stream(10, ->
108  new Stream()
109)
110eqList s, [10]
111t = new Stream(10, ->
112  new Stream(20, ->
113    new Stream(30, ->
114      new Stream()
115    )
116  )
117)
118eqList t, [10,20,30]
119
120ones = ->
121  new Stream(1, ones)
122
123s = ones()
124eqList s.take(3), [1,1,1]
125
126a = Stream.make 1,2,3
127b = Stream.make 4,5,6
128
129eqList a.append(b), [1,2,3,4,5,6]
130eqList a.cycle().take(8), [1,2,3,1,2,3,1,2]
131eqList Stream.repeat(1).take(5), [1,1,1,1,1]
132
133naturalNumbers = ->
134  new Stream(1, ->
135    ones().add naturalNumbers()
136  )
137
138eqList naturalNumbers().take(5), [1..5]
139
140
141
142sieve = (s) ->
143  h = s.head()
144  new Stream(h, ->
145    sieve s.tail().filter((x) ->
146      x % h != 0
147    )
148  )
149tenPrimes = [2,3,5,7,11,13,17,19,23,29]
150eqList sieve(Stream.range(2)).take(10), tenPrimes
151
152
153eqList primeStream.take(10), tenPrimes
154
155fib = Stream.recursive ((a,b) -> a+b), 1, 1
156
157eqList fib.take(10), [1, 1, 2, 3, 5, 8, 13, 21, 34, 55]
158
159
160assert Stream.make(false, false, true).any()
161assert not Stream.make(false, false, false).any()
162assert Stream.make(true, true, true).all()
163assert not Stream.make(true, true, false).all()