summaryrefslogtreecommitdiffstats
path: root/addon-sdk/source/test/test-sequence.js
blob: 90a12edd14868da6ccb5e5f40c2f150c6b12a0aa (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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
"use strict";

var { seq, iterate, filter, map, reductions, reduce, count,
      isEmpty, every, isEvery, some, take, takeWhile, drop,
      dropWhile, concat, first, rest, nth, last, dropLast,
      distinct, remove, mapcat, fromEnumerator, string,
      object, pairs, keys, values, each, names, symbols
    } = require("sdk/util/sequence");

const boom = () => { throw new Error("Boom!"); };
const broken = seq(function*() {
  yield 1;
  throw new Error("Boom!");
});

exports["test seq"] = assert => {
  let xs = seq(function*() {
    yield 1;
    yield 2;
    yield 3;
  });

  assert.deepEqual([...seq(null)], [], "seq of null is empty");
  assert.deepEqual([...seq(void(0))], [], "seq of void is empty");
  assert.deepEqual([...xs], [1, 2, 3], "seq of 1 2 3");
  assert.deepEqual([...seq(xs)], [1, 2, 3], "seq of seq is seq");

  assert.deepEqual([...seq([])], [], "seq of emtpy array is empty");
  assert.deepEqual([...seq([1])], [1], "seq of lonly array is single element");
  assert.deepEqual([...seq([1, 2, 3])], [1, 2, 3], "seq of array is it's elements");

  assert.deepEqual([...seq("")], [], "seq of emtpy string is empty");
  assert.deepEqual([...seq("o")], ["o"], "seq of char is single char seq");
  assert.deepEqual([...seq("hello")], ["h", "e", "l", "l", "o"],
                   "seq of string are chars");

  assert.deepEqual([...seq(new Set())], [], "seq of emtpy set is empty");
  assert.deepEqual([...seq(new Set([1]))], [1], "seq of lonely set is single");
  assert.deepEqual([...seq(new Set([1, 2, 3]))], [1, 2, 3], "seq of lonely set is single");

  assert.deepEqual([...seq(new Map())], [], "seq of emtpy map is empty");
  assert.deepEqual([...seq(new Map([[1, 2]]))], [[1, 2]], "seq single mapping is that mapping");
  assert.deepEqual([...seq(new Map([[1, 2], [3, 4], [5, 6]]))],
                   [[1, 2], [3, 4], [5, 6]],
                   "seq of map is key value mappings");

  [function(){}, 1, /foo/, true].forEach(x => {
    assert.throws(() => seq(x), "Type is not seq-able");
  });

  assert.throws(() => [...broken],
                /Boom/,
                "broken sequence errors propagate");
};

exports["test seq casting"] = assert => {
  const xs = seq(function*() { yield 1; yield 2; yield 3; });
  const ys = seq(function*() { yield 1; });
  const zs = seq(function*() {});
  const kvs = seq(function*() { yield ["a", 1]; yield ["b", 2]; });
  const kv = seq(function*() { yield ["a", 1]; });

  assert.deepEqual([...xs], [1, 2, 3], "cast to array");
  assert.deepEqual([...ys], [1], "cast to of one element");
  assert.deepEqual([...zs], [], "cast empty array");

  assert.deepEqual(string(...xs), "123", "cast to string");
  assert.deepEqual(string(...ys), "1", "cast to char");
  assert.deepEqual(string(...zs), "", "cast to empty string");

  assert.deepEqual(new Set(xs), new Set([1, 2, 3]),
                   "cast to set of items");
  assert.deepEqual(new Set(ys), new Set([1]),
                   "cast to set of one item");
  assert.deepEqual(new Set(zs), new Set(),
                   "cast to set of one item");

  assert.deepEqual(new Map(kvs), new Map([["a", 1], ["b", 2]]),
                   "cast to map");
  assert.deepEqual(new Map(kv), new Map([["a", 1]]),
                   "cast to single mapping");
  assert.deepEqual(new Map(zs), new Map(),
                   "cast to empty map");

  assert.deepEqual(object(...kvs), {a: 1, b: 2},
                  "cast to object");
  assert.deepEqual(object(...kv), {a: 1},
                  "cast to single pair");
  assert.deepEqual(object(...zs), {},
                  "cast to empty object");
};

exports["test pairs"] = assert => {
  assert.deepEqual([...pairs(null)], [], "pairs on null is empty");
  assert.deepEqual([...pairs(void(0))], [], "pairs on void is empty");
  assert.deepEqual([...pairs({})], [], "empty sequence");
  assert.deepEqual([...pairs({a: 1})], [["a", 1]], "single pair");
  assert.deepEqual([...pairs({a: 1, b: 2, c: 3})].sort(),
                   [["a", 1], ["b", 2], ["c", 3]],
                   "creates pairs");
  let items = [];
  for (let [key, value] of pairs({a: 1, b: 2, c: 3}))
    items.push([key, value]);

  assert.deepEqual(items.sort(),
                   [["a", 1], ["b", 2], ["c", 3]],
                   "for of works on pairs");


  assert.deepEqual([...pairs([])], [], "pairs on empty array is empty");
  assert.deepEqual([...pairs([1])], [[0, 1]], "pairs on array is [index, element]");
  assert.deepEqual([...pairs([1, 2, 3])],
                   [[0, 1], [1, 2], [2, 3]],
                   "for arrays it pair of [index, element]");

  assert.deepEqual([...pairs("")], [], "pairs on empty string is empty");
  assert.deepEqual([...pairs("a")], [[0, "a"]], "pairs on char is [0, char]");
  assert.deepEqual([...pairs("hello")],
                   [[0, "h"], [1, "e"], [2, "l"], [3, "l"], [4, "o"]],
                   "for strings it's pair of [index, char]");

  assert.deepEqual([...pairs(new Map())],
                   [],
                   "pairs on empty map is empty");
  assert.deepEqual([...pairs(new Map([[1, 3]]))],
                   [[1, 3]],
                   "pairs on single mapping single mapping");
  assert.deepEqual([...pairs(new Map([[1, 2], [3, 4]]))],
                   [[1, 2], [3, 4]],
                   "pairs on map returs key vaule pairs");

  assert.throws(() => pairs(new Set()),
                "can't pair set");

  assert.throws(() => pairs(4),
                "can't pair number");

  assert.throws(() => pairs(true),
                "can't pair boolean");
};

exports["test keys"] = assert => {
  assert.deepEqual([...keys(null)], [], "keys on null is empty");
  assert.deepEqual([...keys(void(0))], [], "keys on void is empty");
  assert.deepEqual([...keys({})], [], "empty sequence");
  assert.deepEqual([...keys({a: 1})], ["a"], "single key");
  assert.deepEqual([...keys({a: 1, b: 2, c: 3})].sort(),
                   ["a", "b", "c"],
                   "all keys");
  assert.deepEqual([...keys(Object.create({}, {
                              a: {value: 1, enumerable: true},
                              b: {value: 2, enumerable: false},
                              c: {value: 3, enumerable: false}
                            }))].sort(),
                 ["a"],
                 "only enumerable keys");

  let items = [];
  for (let key of keys({a: 1, b: 2, c: 3}))
    items.push(key);

  assert.deepEqual(items.sort(),
                   ["a", "b", "c"],
                   "for of works on keys");


  assert.deepEqual([...keys([])], [], "keys on empty array is empty");
  assert.deepEqual([...keys([1])], [0], "keys on array is indexes");
  assert.deepEqual([...keys([1, 2, 3])],
                   [0, 1, 2],
                   "keys on arrays returns indexes");

  assert.deepEqual([...keys("")], [], "keys on empty string is empty");
  assert.deepEqual([...keys("a")], [0], "keys on char is 0");
  assert.deepEqual([...keys("hello")],
                   [0, 1, 2, 3, 4],
                   "keys on strings is char indexes");

  assert.deepEqual([...keys(new Map())],
                   [],
                   "keys on empty map is empty");
  assert.deepEqual([...keys(new Map([[1, 3]]))],
                   [1],
                   "keys on single mapping single mapping is single key");
  assert.deepEqual([...keys(new Map([[1, 2], [3, 4]]))],
                   [1, 3],
                   "keys on map is keys from map");

  assert.throws(() => keys(new Set()),
                "can't keys set");

  assert.throws(() => keys(4),
                "can't keys number");

  assert.throws(() => keys(true),
                "can't keys boolean");
};

exports["test names"] = assert => {
  assert.deepEqual([...names(null)], [], "names on null is empty");
  assert.deepEqual([...names(void(0))], [], "names on void is empty");
  assert.deepEqual([...names({})], [], "empty sequence");
  assert.deepEqual([...names({a: 1})], ["a"], "single key");
  assert.deepEqual([...names({a: 1, b: 2, c: 3})].sort(),
                   ["a", "b", "c"],
                   "all keys");
  assert.deepEqual([...names(Object.create({}, {
                              a: {value: 1, enumerable: true},
                              b: {value: 2, enumerable: false},
                              c: {value: 3, enumerable: false}
                            }))].sort(),
                   ["a", "b", "c"],
                   "all enumerable & non-enumerable keys");


  assert.throws(() => names([1, 2, 3]),
                "can't names array");

  assert.throws(() => names(""),
                "can't names string");

  assert.throws(() => names(new Map()),
                "can't names map");

  assert.throws(() => names(new Set()),
                "can't names set");

  assert.throws(() => names(4),
                "can't names number");

  assert.throws(() => names(true),
                "can't names boolean");
};

exports["test symbols"] = assert => {
  assert.deepEqual([...symbols(null)], [], "symbols on null is empty");
  assert.deepEqual([...symbols(void(0))], [], "symbols on void is empty");
  assert.deepEqual([...symbols({})], [], "symbols on empty is empty");
  assert.deepEqual([...symbols({a: 1})], [], "symbols is empty if not owned");

  const b = Symbol("b");
  assert.deepEqual([...symbols({a: 1, [b]: 2, c: 3})].sort(),
                   [b],
                   "only symbols");

  assert.deepEqual([...symbols(Object.create({}, {
                                a: {value: 1, enumerable: true},
                                [b]: {value: 2, enumerable: false},
                                c: {value: 3, enumerable: false}
                              }))].sort(),
                   [b],
                   "includes non-enumerable symbols");


  assert.throws(() => symbols([1, 2, 3]),
                "can't symbols array");

  assert.throws(() => symbols(""),
                "can't symbols string");

  assert.throws(() => symbols(new Map()),
                "can't symbols map");

  assert.throws(() => symbols(new Set()),
                "can't symbols set");

  assert.throws(() => symbols(4),
                "can't symbols number");

  assert.throws(() => symbols(true),
                "can't symbols boolean");
};

exports["test values"] = assert => {
  assert.deepEqual([...values({})], [], "empty sequence");
  assert.deepEqual([...values({a: 1})], [1], "single value");
  assert.deepEqual([...values({a: 1, b: 2, c: 3})].sort(),
                   [1, 2, 3],
                   "all values");

  let items = [];
  for (let value of values({a: 1, b: 2, c: 3}))
    items.push(value);

  assert.deepEqual(items.sort(),
                   [1, 2, 3],
                   "for of works on values");

  assert.deepEqual([...values([])], [], "values on empty array is empty");
  assert.deepEqual([...values([1])], [1], "values on array elements");
  assert.deepEqual([...values([1, 2, 3])],
                   [1, 2, 3],
                   "values on arrays returns elements");

  assert.deepEqual([...values("")], [], "values on empty string is empty");
  assert.deepEqual([...values("a")], ["a"], "values on char is char");
  assert.deepEqual([...values("hello")],
                   ["h", "e", "l", "l", "o"],
                   "values on strings is chars");

  assert.deepEqual([...values(new Map())],
                   [],
                   "values on empty map is empty");
  assert.deepEqual([...values(new Map([[1, 3]]))],
                   [3],
                   "keys on single mapping single mapping is single key");
  assert.deepEqual([...values(new Map([[1, 2], [3, 4]]))],
                   [2, 4],
                   "values on map is values from map");

  assert.deepEqual([...values(new Set())], [], "values on empty set is empty");
  assert.deepEqual([...values(new Set([1]))], [1], "values on set is it's items");
  assert.deepEqual([...values(new Set([1, 2, 3]))],
                   [1, 2, 3],
                   "values on set is it's items");


  assert.throws(() => values(4),
                "can't values number");

  assert.throws(() => values(true),
                "can't values boolean");
};

exports["test fromEnumerator"] = assert => {
  const { Cc, Ci } = require("chrome");
  const { enumerateObservers,
          addObserver,
          removeObserver } = Cc["@mozilla.org/observer-service;1"].
                               getService(Ci.nsIObserverService);


  const topic = "sec:" + Math.random().toString(32).substr(2);
  const [a, b, c] = [{wrappedJSObject: {}},
                     {wrappedJSObject: {}},
                     {wrappedJSObject: {}}];
  const unwrap = x => x.wrappedJSObject;

  [a, b, c].forEach(x => addObserver(x, topic, false));

  const xs = fromEnumerator(() => enumerateObservers(topic));
  const ys = map(unwrap, xs);

  assert.deepEqual([...ys], [a, b, c].map(unwrap),
                   "all observers are there");

  removeObserver(b, topic);

  assert.deepEqual([...ys], [a, c].map(unwrap),
                   "b was removed");

  removeObserver(a, topic);

  assert.deepEqual([...ys], [c].map(unwrap),
                   "a was removed");

  removeObserver(c, topic);

  assert.deepEqual([...ys], [],
                   "c was removed, now empty");

  addObserver(a, topic, false);

  assert.deepEqual([...ys], [a].map(unwrap),
                   "a was added");

  removeObserver(a, topic);

  assert.deepEqual([...ys], [].map(unwrap),
                   "a was removed, now empty");

};

exports["test filter"] = assert => {
  const isOdd = x => x % 2;
  const odds = seq(function*() { yield 1; yield 3; yield 5; });
  const evens = seq(function*() { yield 2; yield 4; yield 6; });
  const mixed = seq(function*() {
    yield 1;
    yield 2;
    yield 3;
    yield 4;
  });

  assert.deepEqual([...filter(isOdd, mixed)], [1, 3],
                   "filtered odds");
  assert.deepEqual([...filter(isOdd, odds)], [1, 3, 5],
                   "kept all");
  assert.deepEqual([...filter(isOdd, evens)], [],
                   "kept none");


  let xs = filter(boom, mixed);
  assert.throws(() => [...xs], /Boom/, "errors propagate");

  assert.throws(() => [...filter(isOdd, broken)], /Boom/,
                "sequence errors propagate");
};

exports["test filter array"] = assert => {
  let isOdd = x => x % 2;
  let xs = filter(isOdd, [1, 2, 3, 4]);
  let ys = filter(isOdd, [1, 3, 5]);
  let zs = filter(isOdd, [2, 4, 6]);

  assert.deepEqual([...xs], [1, 3], "filteres odds");
  assert.deepEqual([...ys], [1, 3, 5], "kept all");
  assert.deepEqual([...zs], [], "kept none");
  assert.ok(!Array.isArray(xs));
};

exports["test filter set"] = assert => {
  let isOdd = x => x % 2;
  let xs = filter(isOdd, new Set([1, 2, 3, 4]));
  let ys = filter(isOdd, new Set([1, 3, 5]));
  let zs = filter(isOdd, new Set([2, 4, 6]));

  assert.deepEqual([...xs], [1, 3], "filteres odds");
  assert.deepEqual([...ys], [1, 3, 5], "kept all");
  assert.deepEqual([...zs], [], "kept none");
};

exports["test filter string"] = assert => {
  let isUpperCase = x => x.toUpperCase() === x;
  let xs = filter(isUpperCase, "aBcDe");
  let ys = filter(isUpperCase, "ABC");
  let zs = filter(isUpperCase, "abcd");

  assert.deepEqual([...xs], ["B", "D"], "filteres odds");
  assert.deepEqual([...ys], ["A", "B", "C"], "kept all");
  assert.deepEqual([...zs], [], "kept none");
};

exports["test filter lazy"] = assert => {
  const x = 1;
  let y = 2;

  const xy = seq(function*() { yield x; yield y; });
  const isOdd = x => x % 2;
  const actual = filter(isOdd, xy);

  assert.deepEqual([...actual], [1], "only one odd number");
  y = 3;
  assert.deepEqual([...actual], [1, 3], "filter is lazy");
};

exports["test filter non sequences"] = assert => {
  const False = _ => false;
  assert.throws(() => [...filter(False, 1)],
                "can't iterate number");
  assert.throws(() => [...filter(False, {a: 1, b:2})],
                "can't iterate object");
};

exports["test map"] = assert => {
  let inc = x => x + 1;
  let xs = seq(function*() { yield 1; yield 2; yield 3; });
  let ys = map(inc, xs);

  assert.deepEqual([...ys], [2, 3, 4], "incremented each item");

  assert.deepEqual([...map(inc, null)], [], "mapping null is empty");
  assert.deepEqual([...map(inc, void(0))], [], "mapping void is empty");
  assert.deepEqual([...map(inc, new Set([1, 2, 3]))], [2, 3, 4], "maps set items");
};

exports["test map two inputs"] = assert => {
  let sum = (x, y) => x + y;
  let xs = seq(function*() { yield 1; yield 2; yield 3; });
  let ys = seq(function*() { yield 4; yield 5; yield 6; });

  let zs = map(sum, xs, ys);

  assert.deepEqual([...zs], [5, 7, 9], "summed numbers");
};

exports["test map diff sized inputs"] = assert => {
  let sum = (x, y) => x + y;
  let xs = seq(function*() { yield 1; yield 2; yield 3; });
  let ys = seq(function*() { yield 4; yield 5; yield 6; yield 7; yield 8; });

  let zs = map(sum, xs, ys);

  assert.deepEqual([...zs], [5, 7, 9], "summed numbers");
  assert.deepEqual([...map(sum, ys, xs)], [5, 7, 9],
                   "index of exhasting input is irrelevant");
};

exports["test map multi"] = assert => {
  let sum = (x, y, z, w) => x + y + z + w;
  let xs = seq(function*() { yield 1; yield 2; yield 3; yield 4; });
  let ys = seq(function*() { yield 4; yield 5; yield 6; yield 7; yield 8; });
  let zs = seq(function*() { yield 10; yield 11; yield 12; });
  let ws = seq(function*() { yield 0; yield 20; yield 40; yield 60; });

  let actual = map(sum, xs, ys, zs, ws);

  assert.deepEqual([...actual], [15, 38, 61], "summed numbers");
};

exports["test map errors"] = assert => {
  assert.deepEqual([...map(boom, [])], [],
                   "won't throw if empty");

  const xs = map(boom, [1, 2, 4]);

  assert.throws(() => [...xs], /Boom/, "propagates errors");

  assert.throws(() => [...map(x => x, broken)], /Boom/,
                "sequence errors propagate");
};

exports["test reductions"] = assert => {
  let sum = (...xs) => xs.reduce((x, y) => x + y, 0);

  assert.deepEqual([...reductions(sum, [1, 1, 1, 1])],
                   [1, 2, 3, 4],
                   "works with arrays");
  assert.deepEqual([...reductions(sum, 5, [1, 1, 1, 1])],
                   [5, 6, 7, 8, 9],
                   "array with initial");

  assert.deepEqual([...reductions(sum, seq(function*() {
    yield 1;
    yield 2;
    yield 3;
  }))],
  [1, 3, 6],
  "works with sequences");

  assert.deepEqual([...reductions(sum, 10, seq(function*() {
    yield 1;
    yield 2;
    yield 3;
  }))],
  [10, 11, 13, 16],
  "works with sequences");

  assert.deepEqual([...reductions(sum, [])], [0],
                   "invokes accumulator with no args");

  assert.throws(() => [...reductions(boom, 1, [1])],
                /Boom/,
                "arg errors errors propagate");
  assert.throws(() => [...reductions(sum, 1, broken)],
                /Boom/,
                "sequence errors propagate");
};

exports["test reduce"] = assert => {
 let sum = (...xs) => xs.reduce((x, y) => x + y, 0);

  assert.deepEqual(reduce(sum, [1, 2, 3, 4, 5]),
                   15,
                   "works with arrays");

  assert.deepEqual(reduce(sum, seq(function*() {
                     yield 1;
                     yield 2;
                     yield 3;
                   })),
                   6,
                   "works with sequences");

  assert.deepEqual(reduce(sum, 10, [1, 2, 3, 4, 5]),
                   25,
                   "works with array & initial");

  assert.deepEqual(reduce(sum, 5, seq(function*() {
                     yield 1;
                     yield 2;
                     yield 3;
                   })),
                   11,
                   "works with sequences & initial");

  assert.deepEqual(reduce(sum, []), 0, "reduce with no args");
  assert.deepEqual(reduce(sum, "a", []), "a", "reduce with initial");
  assert.deepEqual(reduce(sum, 1, [1]), 2, "reduce with single & initial");

  assert.throws(() => [...reduce(boom, 1, [1])],
                /Boom/,
                "arg errors errors propagate");
  assert.throws(() => [...reduce(sum, 1, broken)],
                /Boom/,
                "sequence errors propagate");
};

exports["test each"] = assert => {
  const collect = xs => {
    let result = [];
    each((...etc) => result.push(...etc), xs);
    return result;
  };

  assert.deepEqual(collect(null), [], "each ignores null");
  assert.deepEqual(collect(void(0)), [], "each ignores void");

  assert.deepEqual(collect([]), [], "each ignores empty");
  assert.deepEqual(collect([1]), [1], "each works on single item arrays");
  assert.deepEqual(collect([1, 2, 3, 4, 5]),
                   [1, 2, 3, 4, 5],
                   "works with arrays");

  assert.deepEqual(collect(seq(function*() {
                     yield 1;
                     yield 2;
                     yield 3;
                   })),
                   [1, 2, 3],
                   "works with sequences");

  assert.deepEqual(collect(""), [], "ignores empty strings");
  assert.deepEqual(collect("a"), ["a"], "works on chars");
  assert.deepEqual(collect("hello"), ["h", "e", "l", "l", "o"],
                   "works on strings");

  assert.deepEqual(collect(new Set()), [], "ignores empty sets");
  assert.deepEqual(collect(new Set(["a"])), ["a"],
                   "works on single item sets");
  assert.deepEqual(collect(new Set([1, 2, 3])), [1, 2, 3],
                   "works on muti item tests");

  assert.deepEqual(collect(new Map()), [], "ignores empty maps");
  assert.deepEqual(collect(new Map([["a", 1]])), [["a", 1]],
                   "works on single mapping maps");
  assert.deepEqual(collect(new Map([[1, 2], [3, 4], [5, 6]])),
                   [[1, 2], [3, 4], [5, 6]],
                   "works on muti mapping maps");

  assert.throws(() => collect({}), "objects arn't supported");
  assert.throws(() => collect(1), "numbers arn't supported");
  assert.throws(() => collect(true), "booleas arn't supported");
};

exports["test count"] = assert => {
  assert.equal(count(null), 0, "null counts to 0");
  assert.equal(count(), 0, "undefined counts to 0");
  assert.equal(count([]), 0, "empty array");
  assert.equal(count([1, 2, 3]), 3, "non-empty array");
  assert.equal(count(""), 0, "empty string");
  assert.equal(count("hello"), 5, "non-empty string");
  assert.equal(count(new Map()), 0, "empty map");
  assert.equal(count(new Map([[1, 2], [2, 3]])), 2, "non-empty map");
  assert.equal(count(new Set()), 0, "empty set");
  assert.equal(count(new Set([1, 2, 3, 4])), 4, "non-empty set");
  assert.equal(count(seq(function*() {})), 0, "empty sequence");
  assert.equal(count(seq(function*() { yield 1; yield 2; })), 2,
               "non-empty sequence");

  assert.throws(() => count(broken),
                /Boom/,
                "sequence errors propagate");
};

exports["test isEmpty"] = assert => {
  assert.equal(isEmpty(null), true, "null is empty");
  assert.equal(isEmpty(), true, "undefined is empty");
  assert.equal(isEmpty([]), true, "array is array");
  assert.equal(isEmpty([1, 2, 3]), false, "array isn't empty");
  assert.equal(isEmpty(""), true, "string is empty");
  assert.equal(isEmpty("hello"), false, "non-empty string");
  assert.equal(isEmpty(new Map()), true, "empty map");
  assert.equal(isEmpty(new Map([[1, 2], [2, 3]])), false, "non-empty map");
  assert.equal(isEmpty(new Set()), true, "empty set");
  assert.equal(isEmpty(new Set([1, 2, 3, 4])), false , "non-empty set");
  assert.equal(isEmpty(seq(function*() {})), true, "empty sequence");
  assert.equal(isEmpty(seq(function*() { yield 1; yield 2; })), false,
               "non-empty sequence");

  assert.equal(isEmpty(broken), false, "hasn't reached error");
};

exports["test isEvery"] = assert => {
  let isOdd = x => x % 2;
  let isTrue = x => x === true;
  let isFalse = x => x === false;

  assert.equal(isEvery(isOdd, seq(function*() {
    yield 1;
    yield 3;
    yield 5;
  })), true, "all are odds");

  assert.equal(isEvery(isOdd, seq(function*() {
    yield 1;
    yield 2;
    yield 3;
  })), false, "contains even");

  assert.equal(isEvery(isTrue, seq(function*() {})), true, "true if empty");
  assert.equal(isEvery(isFalse, seq(function*() {})), true, "true if empty");

  assert.equal(isEvery(isTrue, null), true, "true for null");
  assert.equal(isEvery(isTrue, undefined), true, "true for undefined");

  assert.throws(() => isEvery(boom, [1, 2]),
                /Boom/,
                "arg errors errors propagate");
  assert.throws(() => isEvery(x => true, broken),
                /Boom/,
                "sequence errors propagate");

  assert.equal(isEvery(x => false, broken), false,
              "hasn't reached error");
};

exports["test some"] = assert => {
  let isOdd = x => x % 2;
  let isTrue = x => x === true;
  let isFalse = x => x === false;

  assert.equal(some(isOdd, seq(function*() {
    yield 2;
    yield 4;
    yield 6;
  })), null, "all are even");

  assert.equal(some(isOdd, seq(function*() {
    yield 2;
    yield 3;
    yield 4;
  })), true, "contains odd");

  assert.equal(some(isTrue, seq(function*() {})), null,
               "null if empty")
  assert.equal(some(isFalse, seq(function*() {})), null,
               "null if empty")

  assert.equal(some(isTrue, null), null, "null for null");
  assert.equal(some(isTrue, undefined), null, "null for undefined");

  assert.throws(() => some(boom, [1, 2]),
                /Boom/,
                "arg errors errors propagate");
  assert.throws(() => some(x => false, broken),
                /Boom/,
                "sequence errors propagate");

  assert.equal(some(x => true, broken), true,
              "hasn't reached error");
};

exports["test take"] = assert => {
  let xs = seq(function*() {
    yield 1;
    yield 2;
    yield 3;
    yield 4;
    yield 5;
    yield 6;
  });

  assert.deepEqual([...take(3, xs)], [1, 2, 3], "took 3 items");
  assert.deepEqual([...take(3, [1, 2, 3, 4, 5])], [1, 2, 3],
                   "took 3 from array");

  let ys = seq(function*() { yield 1; yield 2; });
  assert.deepEqual([...take(3, ys)], [1, 2], "takes at max n");
  assert.deepEqual([...take(3, [1, 2])], [1, 2],
                   "takes at max n from arary");

  let empty = seq(function*() {});
  assert.deepEqual([...take(5, empty)], [], "nothing to take");

  assert.throws(() => [...take(3, broken)],
                /Boom/,
                "sequence errors propagate");

  assert.deepEqual([...take(1, broken)], [1],
                   "hasn't reached error");
};

exports["test iterate"] = assert => {
  let inc = x => x + 1;
  let nums = iterate(inc, 0);

  assert.deepEqual([...take(5, nums)], [0, 1, 2, 3, 4], "took 5");
  assert.deepEqual([...take(10, nums)], [0, 1, 2, 3, 4, 5, 6, 7, 8, 9], "took 10");

  let xs = iterate(x => x * 3, 2);
  assert.deepEqual([...take(4, xs)], [2, 6, 18, 54], "took 4");

  assert.throws(() => [...iterate(boom, 0)],
                /Boom/,
                "function exceptions propagate");
};

exports["test takeWhile"] = assert => {
  let isNegative = x => x < 0;
  let xs = seq(function*() {
    yield -2;
    yield -1;
    yield 0;
    yield 1;
    yield 2;
    yield 3;
  });

  assert.deepEqual([...takeWhile(isNegative, xs)], [-2, -1],
                   "took until 0");

  let ys = seq(function*() {});
  assert.deepEqual([...takeWhile(isNegative, ys)], [],
                   "took none");

  let zs = seq(function*() {
    yield 0;
    yield 1;
    yield 2;
    yield 3;
  });

  assert.deepEqual([...takeWhile(isNegative, zs)], [],
                   "took none");

  assert.throws(() => [...takeWhile(boom, zs)],
                /Boom/,
                "function errors errors propagate");
  assert.throws(() => [...takeWhile(x => true, broken)],
                /Boom/,
                "sequence errors propagate");

  assert.deepEqual([...takeWhile(x => false, broken)],
                   [],
                   "hasn't reached error");
};

exports["test drop"] = assert => {
  let testDrop = xs => {
    assert.deepEqual([...drop(2, xs)],
                     [3, 4],
                     "dropped two elements");

    assert.deepEqual([...drop(1, xs)],
                     [2, 3, 4],
                     "dropped one");

    assert.deepEqual([...drop(0, xs)],
                     [1, 2, 3, 4],
                     "dropped 0");

    assert.deepEqual([...drop(-2, xs)],
                     [1, 2, 3, 4],
                     "dropped 0 on negative `n`");

    assert.deepEqual([...drop(5, xs)],
                     [],
                     "dropped all items");
  };

  testDrop([1, 2, 3, 4]);
  testDrop(seq(function*() {
    yield 1;
    yield 2;
    yield 3;
    yield 4;
  }));

  assert.throws(() => [...drop(1, broken)],
                /Boom/,
                "sequence errors propagate");
};


exports["test dropWhile"] = assert => {
  let isNegative = x => x < 0;
  let True = _ => true;
  let False = _ => false;

  let test = xs => {
    assert.deepEqual([...dropWhile(isNegative, xs)],
                     [0, 1, 2],
                     "dropped negative");

    assert.deepEqual([...dropWhile(True, xs)],
                     [],
                     "drop all");

    assert.deepEqual([...dropWhile(False, xs)],
                     [-2, -1, 0, 1, 2],
                     "keep all");
  };

  test([-2, -1, 0, 1, 2]);
  test(seq(function*() {
    yield -2;
    yield -1;
    yield 0;
    yield 1;
    yield 2;
  }));

  assert.throws(() => [...dropWhile(boom, [1, 2, 3])],
                /Boom/,
                "function errors errors propagate");
  assert.throws(() => [...dropWhile(x => true, broken)],
                /Boom/,
                "sequence errors propagate");
};


exports["test concat"] = assert => {
  let test = (a, b, c, d) => {
    assert.deepEqual([...concat()],
                     [],
                     "nothing to concat");
    assert.deepEqual([...concat(a)],
                     [1, 2, 3],
                     "concat with nothing returns same as first");
    assert.deepEqual([...concat(a, b)],
                     [1, 2, 3, 4, 5],
                     "concat items from both");
    assert.deepEqual([...concat(a, b, a)],
                     [1, 2, 3, 4, 5, 1, 2, 3],
                     "concat itself");
    assert.deepEqual([...concat(c)],
                     [],
                     "concat of empty is empty");
    assert.deepEqual([...concat(a, c)],
                     [1, 2, 3],
                     "concat with empty");
    assert.deepEqual([...concat(c, c, c)],
                     [],
                     "concat of empties is empty");
    assert.deepEqual([...concat(c, b)],
                     [4, 5],
                     "empty can be in front");
    assert.deepEqual([...concat(d)],
                     [7],
                     "concat singular");
    assert.deepEqual([...concat(d, d)],
                     [7, 7],
                     "concat singulars");

    assert.deepEqual([...concat(a, a, b, c, d, c, d, d)],
                     [1, 2, 3, 1, 2, 3, 4, 5, 7, 7, 7],
                     "many concats");

    let ab = concat(a, b);
    let abcd = concat(ab, concat(c, d));
    let cdabcd = concat(c, d, abcd);

    assert.deepEqual([...cdabcd],
                     [7, 1, 2, 3, 4, 5, 7],
                     "nested concats");
  };

  test([1, 2, 3],
       [4, 5],
       [],
       [7]);

  test(seq(function*() { yield 1; yield 2; yield 3; }),
       seq(function*() { yield 4; yield 5; }),
       seq(function*() { }),
       seq(function*() { yield 7; }));

  assert.throws(() => [...concat(broken, [1, 2, 3])],
                /Boom/,
                "function errors errors propagate");
};


exports["test first"] = assert => {
  let test = (xs, empty) => {
    assert.equal(first(xs), 1, "returns first");
    assert.equal(first(empty), null, "returns null empty");
  };

  test("1234", "");
  test([1, 2, 3], []);
  test([1, 2, 3], null);
  test([1, 2, 3], undefined);
  test(seq(function*() { yield 1; yield 2; yield 3; }),
       seq(function*() { }));
  assert.equal(first(broken), 1, "did not reached error");
};

exports["test rest"] = assert => {
  let test = (xs, x, nil) => {
    assert.deepEqual([...rest(xs)], ["b", "c"],
                     "rest items");
    assert.deepEqual([...rest(x)], [],
                     "empty when singular");
    assert.deepEqual([...rest(nil)], [],
                     "empty when empty");
  };

  test("abc", "a", "");
  test(["a", "b", "c"], ["d"], []);
  test(seq(function*() { yield "a"; yield "b"; yield "c"; }),
       seq(function*() { yield "d"; }),
       seq(function*() {}));
  test(["a", "b", "c"], ["d"], null);
  test(["a", "b", "c"], ["d"], undefined);

  assert.throws(() => [...rest(broken)],
                /Boom/,
                "sequence errors propagate");
};


exports["test nth"] = assert => {
  let notFound = {};
  let test = xs => {
    assert.equal(nth(xs, 0), "h", "first");
    assert.equal(nth(xs, 1), "e", "second");
    assert.equal(nth(xs, 5), void(0), "out of bound");
    assert.equal(nth(xs, 5, notFound), notFound, "out of bound");
    assert.equal(nth(xs, -1), void(0), "out of bound");
    assert.equal(nth(xs, -1, notFound), notFound, "out of bound");
    assert.equal(nth(xs, 4), "o", "5th");
  };

  let testEmpty = xs => {
    assert.equal(nth(xs, 0), void(0), "no first in empty");
    assert.equal(nth(xs, 5), void(0), "no 5th in empty");
    assert.equal(nth(xs, 0, notFound), notFound, "notFound on out of bound");
  };

  test("hello");
  test(["h", "e", "l", "l", "o"]);
  test(seq(function*() {
    yield "h";
    yield "e";
    yield "l";
    yield "l";
    yield "o";
  }));
  testEmpty(null);
  testEmpty(undefined);
  testEmpty([]);
  testEmpty("");
  testEmpty(seq(function*() {}));


  assert.throws(() => nth(broken, 1),
                /Boom/,
                "sequence errors propagate");
  assert.equal(nth(broken, 0), 1, "have not reached error");
};


exports["test last"] = assert => {
  assert.equal(last(null), null, "no last in null");
  assert.equal(last(void(0)), null, "no last in undefined");
  assert.equal(last([]), null, "no last in []");
  assert.equal(last(""), null, "no last in ''");
  assert.equal(last(seq(function*() { })), null, "no last in empty");

  assert.equal(last("hello"), "o", "last from string");
  assert.equal(last([1, 2, 3]), 3, "last from array");
  assert.equal(last([1]), 1, "last from singular");
  assert.equal(last(seq(function*() {
    yield 1;
    yield 2;
    yield 3;
  })), 3, "last from sequence");

  assert.throws(() => last(broken),
                /Boom/,
                "sequence errors propagate");
};


exports["test dropLast"] = assert => {
  let test = xs => {
    assert.deepEqual([...dropLast(xs)],
                     [1, 2, 3, 4],
                     "dropped last");
    assert.deepEqual([...dropLast(0, xs)],
                     [1, 2, 3, 4, 5],
                     "dropped none on 0");
    assert.deepEqual([...dropLast(-3, xs)],
                     [1, 2, 3, 4, 5],
                     "drop none on negative");
    assert.deepEqual([...dropLast(3, xs)],
                     [1, 2],
                     "dropped given number");
    assert.deepEqual([...dropLast(5, xs)],
                     [],
                     "dropped all");
  };

  let testEmpty = xs => {
    assert.deepEqual([...dropLast(xs)],
                     [],
                     "nothing to drop");
    assert.deepEqual([...dropLast(0, xs)],
                     [],
                     "dropped none on 0");
    assert.deepEqual([...dropLast(-3, xs)],
                     [],
                     "drop none on negative");
    assert.deepEqual([...dropLast(3, xs)],
                     [],
                     "nothing to drop");
  };

  test([1, 2, 3, 4, 5]);
  test(seq(function*() {
    yield 1;
    yield 2;
    yield 3;
    yield 4;
    yield 5;
  }));
  testEmpty([]);
  testEmpty("");
  testEmpty(seq(function*() {}));

  assert.throws(() => [...dropLast(broken)],
                /Boom/,
                "sequence errors propagate");
};


exports["test distinct"] = assert => {
  let test = (xs, message) => {
    assert.deepEqual([...distinct(xs)],
                     [1, 2, 3, 4, 5],
                     message);
  };

  test([1, 2, 1, 3, 1, 4, 1, 5], "works with arrays");
  test(seq(function*() {
    yield 1;
    yield 2;
    yield 1;
    yield 3;
    yield 1;
    yield 4;
    yield 1;
    yield 5;
  }), "works with sequences");
  test(new Set([1, 2, 1, 3, 1, 4, 1, 5]),
       "works with sets");
  test(seq(function*() {
    yield 1;
    yield 2;
    yield 2;
    yield 2;
    yield 1;
    yield 3;
    yield 1;
    yield 4;
    yield 4;
    yield 4;
    yield 1;
    yield 5;
  }), "works with multiple repeatitions");
  test([1, 2, 3, 4, 5], "work with distinct arrays");
  test(seq(function*() {
    yield 1;
    yield 2;
    yield 3;
    yield 4;
    yield 5;
  }), "works with distinct seqs");
};


exports["test remove"] = assert => {
  let isPositive = x => x > 0;
  let test = xs => {
    assert.deepEqual([...remove(isPositive, xs)],
                     [-2, -1, 0],
                     "removed positives");
  };

  test([1, -2, 2, -1, 3, 7, 0]);
  test(seq(function*() {
    yield 1;
    yield -2;
    yield 2;
    yield -1;
    yield 3;
    yield 7;
    yield 0;
  }));

  assert.throws(() => [...distinct(broken)],
                /Boom/,
                "sequence errors propagate");
};


exports["test mapcat"] = assert => {
  let upto = n => seq(function* () {
    let index = 0;
    while (index < n) {
      yield index;
      index = index + 1;
    }
  });

  assert.deepEqual([...mapcat(upto, [1, 2, 3, 4])],
                   [0, 0, 1, 0, 1, 2, 0, 1, 2, 3],
                   "expands given sequence");

  assert.deepEqual([...mapcat(upto, [0, 1, 2, 0])],
                   [0, 0, 1],
                   "expands given sequence");

  assert.deepEqual([...mapcat(upto, [0, 0, 0])],
                   [],
                   "expands given sequence");

  assert.deepEqual([...mapcat(upto, [])],
                   [],
                   "nothing to expand");

  assert.deepEqual([...mapcat(upto, null)],
                   [],
                   "nothing to expand");

  assert.deepEqual([...mapcat(upto, void(0))],
                   [],
                   "nothing to expand");

  let xs = seq(function*() {
    yield 0;
    yield 1;
    yield 0;
    yield 2;
    yield 0;
  });

  assert.deepEqual([...mapcat(upto, xs)],
                   [0, 0, 1],
                   "expands given sequence");

  assert.throws(() => [...mapcat(boom, xs)],
                /Boom/,
                "function errors errors propagate");
  assert.throws(() => [...mapcat(upto, broken)],
                /Boom/,
                "sequence errors propagate");
};

require("sdk/test").run(exports);