aboutsummaryrefslogtreecommitdiff
path: root/test/gleam/iterator_test.gleam
blob: ce3cdeab697ba4483c2de98a0def08945fc460b6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
import gleam/should
import gleam/iterator
import gleam/list

// a |> from_list |> to_list == a
pub fn to_from_list_test() {
  let test = fn(subject) {
    subject
    |> iterator.from_list
    |> iterator.to_list
    |> should.equal(subject)
  }

  test([])
  test([1])
  test([1, 2])
  test([1, 2, 4, 8])
}

// a |> from_list |> take(n) == a |> list.take(_, n)
pub fn take_test() {
  let test = fn(n, subject) {
    subject
    |> iterator.from_list
    |> iterator.take(n)
    |> should.equal(list.take(subject, n))
  }

  test(0, [])
  test(1, [])
  test(-1, [])
  test(0, [0])
  test(1, [0])
  test(-1, [0])
  test(0, [0, 1, 2, 3, 4])
  test(1, [0, 1, 2, 3, 4])
  test(2, [0, 1, 2, 3, 4])
  test(22, [0, 1, 2, 3, 4])
}

// a |> from_list |> fold(a, f) == a |> list.fold(_, a, f)
pub fn fold_test() {
  let test = fn(subject, acc, f) {
    subject
    |> iterator.from_list
    |> iterator.fold(acc, f)
    |> should.equal(list.fold(subject, acc, f))
  }

  let f = fn(e, acc) { [e, ..acc] }
  test([], [], f)
  test([1], [], f)
  test([1, 2, 3], [], f)
  test([1, 2, 3, 4, 5, 6, 7, 8], [], f)
}

// a |> from_list |> map(f) |> to_list == a |> list.map(_, f)
pub fn map_test() {
  let test = fn(subject, f) {
    subject
    |> iterator.from_list
    |> iterator.map(f)
    |> iterator.to_list
    |> should.equal(list.map(subject, f))
  }

  let f = fn(e) { e * 2 }
  test([], f)
  test([1], f)
  test([1, 2, 3], f)
  test([1, 2, 3, 4, 5, 6, 7, 8], f)
}

// a |> from_list |> filter(f) |> to_list == a |> list.filter(_, f)
pub fn filter_test() {
  let test = fn(subject, f) {
    subject
    |> iterator.from_list
    |> iterator.filter(f)
    |> iterator.to_list
    |> should.equal(list.filter(subject, f))
  }

  let even = fn(x) { x % 2 == 0 }
  test([], even)
  test([1], even)
  test([1, 2], even)
  test([1, 2, 3], even)
  test([1, 2, 3, 4], even)
  test([1, 2, 3, 4, 5], even)
  test([1, 2, 3, 4, 5, 6], even)
}

pub fn repeat_test() {
  1
  |> iterator.repeat
  |> iterator.take(5)
  |> should.equal([1, 1, 1, 1, 1])
}

pub fn cycle_test() {
  [1, 2, 3]
  |> iterator.from_list
  |> iterator.cycle
  |> iterator.take(9)
  |> should.equal([1, 2, 3, 1, 2, 3, 1, 2, 3])
}

pub fn unfold_test() {
  iterator.unfold(2, fn(acc) { iterator.Next(acc, acc * 2) })
  |> iterator.take(5)
  |> should.equal([2, 4, 8, 16, 32])

  iterator.unfold(2, fn(_) { iterator.Done })
  |> iterator.take(5)
  |> should.equal([])

  fn(n) {
    case n {
      0 -> iterator.Done
      n -> iterator.Next(element: n, accumulator: n - 1)
    }
  }
  |> iterator.unfold(from: 5)
  |> iterator.to_list
  |> should.equal([5, 4, 3, 2, 1])
}

pub fn range_test() {
  let test = fn(a, b, expected) {
    iterator.range(a, b)
    |> iterator.to_list
    |> should.equal(expected)
  }

  test(0, 0, [])
  test(1, 1, [])
  test(-1, -1, [])
  test(0, 1, [0])
  test(0, 5, [0, 1, 2, 3, 4])
  test(1, -5, [1, 0, -1, -2, -3, -4])
}

pub fn drop_test() {
  iterator.range(0, 10)
  |> iterator.drop(5)
  |> iterator.to_list
  |> should.equal([5, 6, 7, 8, 9])
}