halu 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,442 @@
1
+ # -*- compile-command: "rake test" -*-
2
+
3
+ require File.dirname(__FILE__) + '/test_helper.rb'
4
+
5
+ class TestHalu < Test::Unit::TestCase
6
+ include Halu
7
+
8
+ def setup
9
+ end
10
+
11
+ def test_inspect
12
+ assert_equal('Halu::List[1, 2, 3]', List[1, 2, 3].inspect)
13
+ assert_equal('Halu::EmptyList[]', List[].inspect)
14
+ end
15
+
16
+ def test_add
17
+ list = List.new(1)
18
+ assert_equal(2, (list << 2).head)
19
+ assert_equal(1, list.head)
20
+ assert_equal(2, list.tail.head)
21
+ assert_equal(EMPTY_LIST, list.tail.tail)
22
+
23
+ list = EMPTY_LIST << 3
24
+ assert_equal(3, list.head)
25
+ assert_equal(EMPTY_LIST, list.tail)
26
+ end
27
+
28
+ def test_unshift
29
+ list = List.new(1)
30
+ list = list >> 2
31
+ assert_equal(2, list.head)
32
+ assert_equal(1, list.tail.head)
33
+ assert_equal(EMPTY_LIST, list.tail.tail)
34
+
35
+ list = EMPTY_LIST >> 3
36
+ assert_equal(3, list.head)
37
+ assert_equal(EMPTY_LIST, list.tail)
38
+ end
39
+
40
+ def test_map2
41
+ assert_equal(
42
+ 'Halu::List[2, 4, 6]',
43
+ List[1, 2, 3].map2 {|h| h * 2 }.inspect)
44
+ assert_equal(
45
+ 'Halu::EmptyList[]',
46
+ List[].map2 {|h| h * 2 }.inspect)
47
+ end
48
+
49
+ def test_last
50
+ assert_equal(3, List[1, 2, 3].last)
51
+ assert_equal(nil, List[].last)
52
+ end
53
+
54
+ def test_append
55
+ assert_equal(
56
+ 'Halu::List[1, 2, 3, 4, 5, 6]',
57
+ List[1, 2, 3].append(List[4, 5, 6]).inspect)
58
+ assert_equal(
59
+ 'Halu::List[1, 2, 3]',
60
+ List[1, 2, 3].append(List[]).inspect)
61
+ assert_equal(
62
+ 'Halu::List[1, 2, 3]',
63
+ List[].append(List[1, 2, 3]).inspect)
64
+ assert_equal(
65
+ 'Halu::EmptyList[]',
66
+ List[].append(List[]).inspect)
67
+ end
68
+
69
+ def test_filter
70
+ assert_equal(
71
+ 'Halu::List[1, 3]',
72
+ List[1, 2, 3].filter {|h| h % 2 == 1 }.inspect)
73
+ assert_equal(
74
+ 'Halu::EmptyList[]',
75
+ List[].filter {|h| h % 2 == 1 }.inspect)
76
+ end
77
+
78
+ def test_init
79
+ assert_equal(
80
+ 'Halu::List[1, 2]',
81
+ List[1, 2, 3].init.inspect)
82
+ assert_equal(
83
+ 'Halu::EmptyList[]',
84
+ List[].init.inspect)
85
+ end
86
+
87
+ def test_length
88
+ assert_equal(3, List[1, 2, 3].length)
89
+ assert_equal(0, List[].length)
90
+ end
91
+
92
+ def test_at
93
+ assert_equal(2, List[1, 2, 3].at(1))
94
+ assert_equal(nil, List[1, 2, 3].at(4))
95
+ assert_equal(nil, List[1, 2, 3].at(-1))
96
+
97
+ assert_equal(nil, List[].at(0))
98
+ end
99
+
100
+ def test_reverse
101
+ assert_equal(
102
+ 'Halu::List[3, 2, 1]',
103
+ List[1, 2, 3].reverse.inspect)
104
+ assert_equal(
105
+ 'Halu::EmptyList[]',
106
+ List[].reverse.inspect)
107
+ end
108
+
109
+ def test_fold
110
+ assert_equal(16, List[1, 2, 3].fold(10) {|r, h| r + h })
111
+ assert_equal(10, List[].fold(10) {|r, h| r + h })
112
+ end
113
+
114
+ def test_fold1
115
+ assert_equal(6, List[1, 2, 3].fold1 {|r, h| r + h })
116
+ assert_equal(nil, List[].fold1 {|r, h| r + h })
117
+ end
118
+
119
+ def test_and?
120
+ assert_equal(false, List[true, false, true].and?)
121
+ assert_equal(true, List[true, true, true].and?)
122
+
123
+ assert_equal(true, List[].and?)
124
+ end
125
+
126
+ def test_or?
127
+ assert_equal(true, List[false, true, false].or?)
128
+ assert_equal(false, List[false, false, false].or?)
129
+
130
+ assert_equal(false, List[].or?)
131
+ end
132
+
133
+ def test_any?
134
+ assert_equal(true, List[1, 2, 3].any? {|h| h < 3})
135
+ assert_equal(false, List[1, 2, 3].any? {|h| h > 3})
136
+
137
+ assert_equal(false, List[].any? {|h| h < 3})
138
+ end
139
+
140
+ def test_all?
141
+ assert_equal(true, List[1, 2, 3].all? {|h| h < 4})
142
+ assert_equal(false, List[1, 2, 3].all? {|h| h > 4})
143
+
144
+ assert_equal(true, List[].all? {|h| h < 4})
145
+ end
146
+
147
+ def test_sum
148
+ assert_equal(6, List[1, 2, 3].sum)
149
+ assert_equal(0, List[].sum)
150
+ end
151
+
152
+ def test_product
153
+ assert_equal(24, List[1, 2, 3, 4].product)
154
+ assert_equal(1, List[].product)
155
+ end
156
+
157
+ def test_concat
158
+ assert_equal(
159
+ 'Halu::List[1, 2, 3, 4, 5, 6]',
160
+ List[List[1, 2], List[3, 4], List[5, 6]].concat.inspect)
161
+ assert_equal(
162
+ 'Halu::EmptyList[]',
163
+ List[].concat.inspect)
164
+ end
165
+
166
+ def test_concat_map
167
+ assert_equal(
168
+ 'Halu::List[2, 4, 6]',
169
+ List[1, 2, 3].concat_map {|h| List[h * 2]}.inspect)
170
+ assert_equal(
171
+ 'Halu::EmptyList[]',
172
+ List[].concat_map.inspect)
173
+ end
174
+
175
+ def test_maximum
176
+ assert_equal(3, List[1, 2, 3].maximum)
177
+ assert_equal(nil, List[].maximum)
178
+ end
179
+
180
+ def test_minimum
181
+ assert_equal(1, List[1, 2, 3].minimum)
182
+ assert_equal(nil, List[].minimum)
183
+ end
184
+
185
+ def test_scan
186
+ assert_equal(
187
+ 'Halu::List[10, 11, 13, 16]',
188
+ List[1, 2, 3].scan(10) {|r, h| r + h }.inspect)
189
+ assert_equal(
190
+ 'Halu::List[10]',
191
+ List[].scan(10) {|r, h| r + h }.inspect)
192
+ end
193
+
194
+ def test_scan1
195
+ assert_equal(
196
+ 'Halu::List[1, 3, 6]',
197
+ List[1, 2, 3].scan1 {|r, h| r + h }.inspect)
198
+ assert_equal(
199
+ 'Halu::EmptyList[]',
200
+ List[].scan1 {|r, h| r + h }.inspect)
201
+ end
202
+
203
+ def test_replicate
204
+ assert_equal(
205
+ 'Halu::List[1, 1, 1]',
206
+ List::replicate(3, 1).inspect)
207
+ assert_equal(
208
+ 'Halu::EmptyList[]',
209
+ List::replicate(0, 1).inspect)
210
+ assert_equal(
211
+ 'Halu::EmptyList[]',
212
+ List::replicate(-1, 1).inspect)
213
+ end
214
+
215
+ def test_take
216
+ assert_equal(
217
+ 'Halu::List[1, 2]',
218
+ List[1, 2, 3].take(2).inspect)
219
+ assert_equal(
220
+ 'Halu::List[1, 2, 3]',
221
+ List[1, 2, 3].take(4).inspect)
222
+ assert_equal(
223
+ 'Halu::EmptyList[]',
224
+ List[1, 2, 3].take(0).inspect)
225
+ assert_equal(
226
+ 'Halu::EmptyList[]',
227
+ List[1, 2, 3].take(-1).inspect)
228
+ assert_equal(
229
+ 'Halu::EmptyList[]',
230
+ List[].take(1).inspect)
231
+ end
232
+
233
+ def test_drop
234
+ assert_equal(
235
+ 'Halu::List[3]',
236
+ List[1, 2, 3].drop(2).inspect)
237
+ assert_equal(
238
+ 'Halu::EmptyList[]',
239
+ List[1, 2, 3].drop(4).inspect)
240
+ assert_equal(
241
+ 'Halu::List[1, 2, 3]',
242
+ List[1, 2, 3].drop(0).inspect)
243
+ assert_equal(
244
+ 'Halu::List[1, 2, 3]',
245
+ List[1, 2, 3].drop(-1).inspect)
246
+ assert_equal(
247
+ 'Halu::EmptyList[]',
248
+ List[].drop(1).inspect)
249
+ end
250
+
251
+ def test_split_at
252
+ assert_equal(
253
+ '[Halu::List[1, 2], Halu::List[3]]',
254
+ List[1, 2, 3].split_at(2).inspect)
255
+ assert_equal(
256
+ '[Halu::EmptyList[], Halu::List[1, 2, 3]]',
257
+ List[1, 2, 3].split_at(0).inspect)
258
+ assert_equal(
259
+ '[Halu::List[1, 2, 3], Halu::EmptyList[]]',
260
+ List[1, 2, 3].split_at(3).inspect)
261
+ assert_equal(
262
+ '[Halu::EmptyList[], Halu::List[1, 2, 3]]',
263
+ List[1, 2, 3].split_at(-1).inspect)
264
+ assert_equal(
265
+ '[Halu::List[1, 2, 3], Halu::EmptyList[]]',
266
+ List[1, 2, 3].split_at(4).inspect)
267
+ end
268
+
269
+ def test_take_while
270
+ assert_equal(
271
+ 'Halu::List[1, 2]',
272
+ List[1, 2, 3].take_while {|h| h < 3}.inspect)
273
+ assert_equal(
274
+ 'Halu::List[1, 2, 3]',
275
+ List[1, 2, 3].take_while {|h| h < 4}.inspect)
276
+ assert_equal(
277
+ 'Halu::EmptyList[]',
278
+ List[1, 2, 3].take_while {|h| h < 1}.inspect)
279
+ assert_equal(
280
+ 'Halu::EmptyList[]',
281
+ List[].take_while {|h| h < 1}.inspect)
282
+ end
283
+
284
+ def test_drop_while
285
+ assert_equal(
286
+ 'Halu::List[3]',
287
+ List[1, 2, 3].drop_while {|h| h < 3}.inspect)
288
+ assert_equal(
289
+ 'Halu::EmptyList[]',
290
+ List[1, 2, 3].drop_while {|h| h < 4}.inspect)
291
+ assert_equal(
292
+ 'Halu::List[1, 2, 3]',
293
+ List[1, 2, 3].drop_while {|h| h < 1}.inspect)
294
+ assert_equal(
295
+ 'Halu::EmptyList[]',
296
+ List[].drop_while {|h| h < 1}.inspect)
297
+ end
298
+
299
+ def test_span
300
+ assert_equal(
301
+ '[Halu::List[1, 2], Halu::List[3]]',
302
+ List[1, 2, 3].span {|h| h < 3}.inspect)
303
+ assert_equal(
304
+ '[Halu::List[1, 2, 3], Halu::EmptyList[]]',
305
+ List[1, 2, 3].span {|h| h < 4}.inspect)
306
+ assert_equal(
307
+ '[Halu::EmptyList[], Halu::List[1, 2, 3]]',
308
+ List[1, 2, 3].span {|h| h < 1}.inspect)
309
+ assert_equal(
310
+ '[Halu::EmptyList[], Halu::EmptyList[]]',
311
+ List[].span {|h| h < 1}.inspect)
312
+ end
313
+
314
+ def test_break
315
+ assert_equal(
316
+ '[Halu::List[1, 2], Halu::List[3]]',
317
+ List[1, 2, 3].break {|h| h >= 3}.inspect)
318
+ assert_equal(
319
+ '[Halu::List[1, 2, 3], Halu::EmptyList[]]',
320
+ List[1, 2, 3].break {|h| h >= 4}.inspect)
321
+ assert_equal(
322
+ '[Halu::EmptyList[], Halu::List[1, 2, 3]]',
323
+ List[1, 2, 3].break {|h| h >= 1}.inspect)
324
+ assert_equal(
325
+ '[Halu::EmptyList[], Halu::EmptyList[]]',
326
+ List[].span {|h| h >= 1}.inspect)
327
+ end
328
+
329
+ def test_elem
330
+ assert_equal(true, List[1, 2, 3].elem(2))
331
+ assert_equal(false, List[1, 2, 3].elem(0))
332
+ assert_equal(false, List[].elem(0))
333
+ end
334
+
335
+ def test_not_elem
336
+ assert_equal(false, List[1, 2, 3].not_elem(2))
337
+ assert_equal(true, List[1, 2, 3].not_elem(0))
338
+ assert_equal(true, List[].not_elem(0))
339
+ end
340
+
341
+ def test_lookup
342
+ assert_equal(:bbb,
343
+ List[[1, :aaa], [2, :bbb], [3, :ccc]].lookup(2))
344
+ assert_equal(nil,
345
+ List[[1, :aaa], [2, :bbb], [3, :ccc]].lookup(0))
346
+ assert_equal(nil,
347
+ List[].lookup(0))
348
+ end
349
+
350
+ def test_zip2
351
+ assert_equal('Halu::List[[1, 4], [2, 5], [3, 6]]',
352
+ List[1, 2, 3].zip2(List[4, 5, 6]).inspect)
353
+ assert_equal('Halu::List[[1, 4], [2, 5]]',
354
+ List[1, 2, 3].zip2(List[4, 5]).inspect)
355
+ assert_equal('Halu::List[[1, 4], [2, 5]]',
356
+ List[1, 2].zip2(List[4, 5, 6]).inspect)
357
+ assert_equal('Halu::EmptyList[]',
358
+ List[].zip2(List[4, 5, 6]).inspect)
359
+ assert_equal('Halu::EmptyList[]',
360
+ List[1, 2, 3].zip2(List[]).inspect)
361
+ end
362
+
363
+ def test_zip3
364
+ assert_equal('Halu::List[[1, 4, 7], [2, 5, 8], [3, 6, 9]]',
365
+ List[1, 2, 3].zip3(List[4, 5, 6], List[7, 8, 9]).inspect)
366
+ assert_equal('Halu::List[[1, 4, 7], [2, 5, 8]]',
367
+ List[1, 2, 3].zip3(List[4, 5, 6], List[7, 8]).inspect)
368
+ assert_equal('Halu::List[[1, 4, 7], [2, 5, 8]]',
369
+ List[1, 2, 3].zip3(List[4, 5], List[7, 8, 9]).inspect)
370
+ assert_equal('Halu::List[[1, 4, 7], [2, 5, 8]]',
371
+ List[1, 2].zip3(List[4, 5, 6], List[7, 8, 9]).inspect)
372
+ assert_equal('Halu::EmptyList[]',
373
+ List[].zip3(List[4, 5, 6], List[7, 8, 9]).inspect)
374
+ assert_equal('Halu::EmptyList[]',
375
+ List[1, 2, 3].zip3(List[], List[7, 8, 9]).inspect)
376
+ assert_equal('Halu::EmptyList[]',
377
+ List[1, 2, 3].zip3(List[4, 5, 6], List[]).inspect)
378
+ end
379
+
380
+ def test_zip_with
381
+ assert_equal('Halu::List[5, 7, 9]',
382
+ List[1, 2, 3].zip_with(List[4, 5, 6]) {|a, b| a + b}.inspect)
383
+ assert_equal('Halu::List[5, 7]',
384
+ List[1, 2, 3].zip_with(List[4, 5]) {|a, b| a + b}.inspect)
385
+ assert_equal('Halu::List[5, 7]',
386
+ List[1, 2].zip_with(List[4, 5, 6]) {|a, b| a + b}.inspect)
387
+ assert_equal('Halu::EmptyList[]',
388
+ List[].zip_with(List[4, 5, 6]) {|a, b| a + b}.inspect)
389
+ assert_equal('Halu::EmptyList[]',
390
+ List[1, 2, 3].zip_with(List[]) {|a, b| a + b}.inspect)
391
+ end
392
+
393
+ def test_zip_with3
394
+ assert_equal('Halu::List[12, 15, 18]',
395
+ List[1, 2, 3].zip_with3(
396
+ List[4, 5, 6], List[7, 8, 9]) {|a, b, c| a + b + c}.inspect)
397
+ assert_equal('Halu::List[12, 15]',
398
+ List[1, 2, 3].zip_with3(
399
+ List[4, 5, 6], List[7, 8]) {|a, b, c| a + b + c}.inspect)
400
+ assert_equal('Halu::List[12, 15]',
401
+ List[1, 2, 3].zip_with3(
402
+ List[4, 5], List[7, 8, 9]) {|a, b, c| a + b + c}.inspect)
403
+ assert_equal('Halu::List[12, 15]',
404
+ List[1, 2].zip_with3(
405
+ List[4, 5, 6], List[7, 8, 9]) {|a, b, c| a + b + c}.inspect)
406
+ assert_equal('Halu::EmptyList[]',
407
+ List[].zip_with3(List[
408
+ 4, 5, 6], List[7, 8, 9]) {|a, b, c| a + b + c}.inspect)
409
+ assert_equal('Halu::EmptyList[]',
410
+ List[1, 2, 3].zip_with3(
411
+ List[], List[7, 8, 9]) {|a, b, c| a + b + c}.inspect)
412
+ assert_equal('Halu::EmptyList[]',
413
+ List[1, 2, 3].zip_with3(
414
+ List[4, 5, 6], List[]) {|a, b, c| a + b + c}.inspect)
415
+ end
416
+
417
+ def test_unzip
418
+ assert_equal('[Halu::List[1, 3, 5], Halu::List[2, 4, 6]]',
419
+ List[[1, 2], [3, 4], [5, 6]].unzip.inspect)
420
+ assert_equal('[Halu::EmptyList[], Halu::EmptyList[]]',
421
+ List[].unzip.inspect)
422
+ end
423
+
424
+ def test_unzip3
425
+ assert_equal(
426
+ '[Halu::List[1, 4, 7], Halu::List[2, 5, 8], Halu::List[3, 6, 9]]',
427
+ List[[1, 2, 3], [4, 5, 6], [7, 8, 9]].unzip3.inspect)
428
+ assert_equal(
429
+ '[Halu::EmptyList[], Halu::EmptyList[], Halu::EmptyList[]]',
430
+ List[].unzip3.inspect)
431
+ end
432
+
433
+ def test_unlines
434
+ assert_equal("aaa\nbbb\nccc\n", List['aaa', 'bbb', 'ccc'].unlines)
435
+ assert_equal('', List[].unlines)
436
+ end
437
+
438
+ def test_unwords
439
+ assert_equal('aaa bbb ccc', List['aaa', 'bbb', 'ccc'].unwords)
440
+ assert_equal('', List[].unwords)
441
+ end
442
+ end
@@ -0,0 +1,2 @@
1
+ require 'test/unit'
2
+ require File.dirname(__FILE__) + '/../lib/halu'
metadata ADDED
@@ -0,0 +1,63 @@
1
+ --- !ruby/object:Gem::Specification
2
+ rubygems_version: 0.9.2
3
+ specification_version: 1
4
+ name: halu
5
+ version: !ruby/object:Gem::Version
6
+ version: 0.0.1
7
+ date: 2007-08-11 00:00:00 +09:00
8
+ summary: Halu ... HAskell-like List Utility
9
+ require_paths:
10
+ - lib
11
+ email: tanaka.shinya@gmail.com
12
+ homepage: http://halu.rubyforge.org
13
+ rubyforge_project: halu
14
+ description: Halu ... HAskell-like List Utility
15
+ autorequire:
16
+ default_executable:
17
+ bindir: bin
18
+ has_rdoc: true
19
+ required_ruby_version: !ruby/object:Gem::Version::Requirement
20
+ requirements:
21
+ - - ">"
22
+ - !ruby/object:Gem::Version
23
+ version: 0.0.0
24
+ version:
25
+ platform: ruby
26
+ signing_key:
27
+ cert_chain:
28
+ post_install_message:
29
+ authors:
30
+ - TANAKA Shin-ya(id:ha-tan)
31
+ files:
32
+ - History.txt
33
+ - License.txt
34
+ - Manifest.txt
35
+ - README.txt
36
+ - Rakefile
37
+ - example/fib.rb
38
+ - example/insert_sort.rb
39
+ - example/select_sort.rb
40
+ - lib/halu.rb
41
+ - lib/halu/version.rb
42
+ - setup.rb
43
+ - test/test_halu.rb
44
+ - test/test_helper.rb
45
+ test_files:
46
+ - test/test_halu.rb
47
+ - test/test_helper.rb
48
+ rdoc_options:
49
+ - --main
50
+ - README.txt
51
+ extra_rdoc_files:
52
+ - History.txt
53
+ - License.txt
54
+ - Manifest.txt
55
+ - README.txt
56
+ executables: []
57
+
58
+ extensions: []
59
+
60
+ requirements: []
61
+
62
+ dependencies: []
63
+