aboutsummaryrefslogtreecommitdiff
path: root/ClojureCLR/Clojure/Clojure.Tests/LibTests/APersistentVectorTests.cs
blob: a8be1ab21138e8a38edcfd270ef3036f922c09b1 (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
/**
 *   Copyright (c) David Miller. All rights reserved.
 *   The use and distribution terms for this software are covered by the
 *   Eclipse Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php)
 *   which can be found in the file epl-v10.html at the root of this distribution.
 *   By using this software in any fashion, you are agreeing to be bound by
 * 	 the terms of this license.
 *   You must not remove this notice, or any other, from this software.
 **/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using NUnit.Framework;
using Rhino.Mocks;

using clojure.lang;

using RMExpect = Rhino.Mocks.Expect;


namespace Clojure.Tests.LibTests
{

    // TODO: Add tests for APersistentVector.SubVector
    // TODO: Add tests for APersistentVector.stream

    [TestFixture]
    public class APersistentVectorTests : AssertionHelper
    {

        // Usually, we test the abstract classes via the simplest concrete class that derives from it.
        // For APersistentVector, all the concrete classes are fairly complicated.
        // Hence we create a test concrete implementation class.
        // This class has no guarantees of persistence/immutability, thread-safety,
        //   or much of anything else, certainly not efficiency.
        // We determined the methods to override by trying to compile the class with no methods.
        // Thus, we have implemented only the absolute minimum.
        // We will write tests for these methods, too.
        // This class just has an underlying  List<int> to hold values.
        class CPV : APersistentVector
        {
            object[] _values;

            public CPV(object[] values)
                : base(null)
            {
                _values = values;
            }


            public CPV(IPersistentMap meta, object[] values)
                : base(meta)
            {
                _values = values;
            }

            public override IObj withMeta(IPersistentMap meta)
            {
                return meta == _meta
                    ? this
                    : new CPV(meta, _values);
            }

            //public override object applyTo(ISeq arglist)
            //{
            //    throw new NotImplementedException();
            //}

            public override IPersistentStack pop()
            {
                if (_values.Length == 0)
                    throw new InvalidOperationException("Can't pop a null stack.");

                object[] newArr = new object[_values.Length - 1];
                Array.Copy(_values, newArr, _values.Length - 1);

                return new CPV(_meta, newArr);
            }

            public override IPersistentVector cons(object o)
            {
                object[] newArr = new object[_values.Length + 1];
                _values.CopyTo(newArr, 0);
                newArr[_values.Length] = o;
                return new CPV(_meta, newArr);
            }

            public override IPersistentVector assocN(int i, object val)
            {
                if ( 0 <= i && i < _values.Length )
                {
                    object[] newArr = new object[_values.Length];
                    _values.CopyTo(newArr, 0);
                    newArr[i] = val;
                    return new CPV(_meta, newArr);
                }
                if ( i == _values.Length )
                    return cons(val);
                throw new IndexOutOfRangeException();
            }

            public override object nth(int i)
            {
                return _values[i];
            }

            public override int length()
            {
                return _values.Length;
            }

            private static CPV EMPTY = new CPV(new object[0]);
            public override IPersistentCollection empty()
            {
                return EMPTY;
            }

            public override int count()
            {
                return _values.Length;
            }
        }

        #region C-tor tests

        [Test]
        public void NoMetaCtorHasNoMeta()
        {
            CPV v = new CPV(new object[] { 1, 2, 3 });

            Expect(v.meta(),Null);
        }

        [Test]
        public void MetaCtorHasMeta()
        {
            MockRepository mocks = new MockRepository();
            IPersistentMap meta = mocks.StrictMock<IPersistentMap>();
            mocks.ReplayAll();

            CPV v = new CPV(meta,new object[] { 1, 2, 3 });

            Expect(v.meta(), SameAs(meta));
            mocks.VerifyAll();
        }

        #endregion

        #region Object tests

        [Test]
        public void ToStringMentionsTheCount()
        {
            CPV v = new CPV(new object[] { 1, 2, 3 });
            string str = v.ToString();

            Expect(str.Contains("3"));
        }

        [Test]
        public void HashCodeRepeats()
        {
            CPV v = new CPV(new object[] { 1, 2, 3 });

            Expect(v.GetHashCode(), EqualTo(v.GetHashCode()));
        }

        [Test]
        public void HashCodeDependsOnItems()
        {
            CPV v1 = new CPV(new object[] { 1, 2, 3 });
            CPV v2 = new CPV(new object[] { 1, 2, 4 });

            Expect(v1.GetHashCode(), Not.EqualTo(v2.GetHashCode()));
        }

        [Test]
        public void EqualsOnNonPersistentVectorIsFalse()
        {
            CPV v1 = new CPV(new object[] { 1, 2, 3 });

            Expect(v1.equiv(7), False);
        }

        [Test]
        public void EqualsOnPersistentVectorWithDifferentItemsIsFalse()
        {
            CPV v1 = new CPV(new object[] { 1, 2, 3 });
            CPV v2 = new CPV(new object[] { 1, 2, 4 });
            CPV v3 = new CPV(new object[] { 1, 2 });
            CPV v4 = new CPV(new object[] { 1, 2, 3, 4 });

            Expect(v1.equiv(v2), False);
            Expect(v1.equiv(v3), False);
            Expect(v1.equiv(v4), False);
        }

        [Test]
        public void EqualsOnPersistentVectorWithSameItemsIsTrue()
        {
            CPV v1 = new CPV(new object[] { 1, 2, 3 });
            CPV v2 = new CPV(new object[] { 1, 2, 3 });
            CPV v3 = new CPV(new object[] { 1 });
            CPV v4 = new CPV(new object[] { 1 });
            CPV v5 = new CPV(new object[] { });
            CPV v6 = new CPV(new object[] { });

            Expect(v1.equiv(v2));
            Expect(v3.equiv(v4));
            Expect(v5.equiv(v6));
        }

        [Test]
        public void EqualsOnSimilarISeqWorks()
        {
            CPV v1 = new CPV(new object[] { 'a', 'b', 'c' });
            StringSeq s1 = StringSeq.create("abc");

            Expect(v1.equiv(s1));
        }

        [Test]
        public void EqualsOnDissimilarISeqFails()
        {
            CPV v1 = new CPV(new object[] { 'a', 'b', 'c' });
            StringSeq s1 = StringSeq.create("ab");
            StringSeq s2 = StringSeq.create("abd");
            StringSeq s3 = StringSeq.create("abcd");

            Expect(v1.equiv(s1), False);
            Expect(v1.equiv(s2), False);
            Expect(v1.equiv(s3), False);
        }


        #endregion

        #region IFn tests

        [Test]
        public void InvokeCallsNth()
        {
            CPV v = new CPV(new object[] { 5, 6, 7 });

            Expect(v.invoke(0),EqualTo(5));
            Expect(v.invoke(1),EqualTo(6));
            Expect(v.invoke(2),EqualTo(7));
            Expect(v.invoke("1"), EqualTo(6));
            Expect(v.invoke(1.0), EqualTo(6));
            Expect(v.invoke(1.2), EqualTo(6));
            Expect(v.invoke(1.8), EqualTo(6)); // Rounds or not-- should it?
            Expect(v.invoke(1.4M), EqualTo(6));
        }


        #endregion

        #region IPersistentCollection tests

        [Test]
        public void SeqOnCount0YieldsNull()
        {
            CPV v = new CPV(new object[0]);

            Expect(v.seq(), Null);
        }

        [Test]
        public void SeqOnPositiveCountYieldsNotNull()
        {
            CPV v = new CPV(new object[]{ 1,2,3});

            Expect(v.seq(), Not.Null);
        }

        [Test]
        public void SeqOnPositiveCountYieldsValidSequence()
        {
            CPV v = new CPV(new object[] { 1, 2, 3 });
            ISeq s = v.seq();

            Expect(s.first(), EqualTo(1));
            Expect(s.next().first(), EqualTo(2));
            Expect(s.next().next().first(), EqualTo(3));
            Expect(s.next().next().next(), Null);
        }

        [Test]
        public void Explicit_IPersistentCollection_cons_works()
        {
            CPV v = new CPV(new object[] { 1, 2 });
            IPersistentCollection c = v as IPersistentCollection;

            Expect(c, Not.Null);

            IPersistentCollection c2 = c.cons(3);
            Expect(c2.count(), EqualTo(3));

            ISeq s2 = c2.seq();

            Expect(s2.first(), EqualTo(1));
            Expect(s2.next().first(), EqualTo(2));
            Expect(s2.next().next().first(), EqualTo(3));
            Expect(s2.next().next().next(), Null);
        }

        #endregion

        #region Reversible tests

        [Test]
        public void RseqOnCount0YieldsNull()
        {
            CPV v = new CPV(new object[0]);

            Expect(v.rseq(), Null);
        }

        [Test]
        public void RSeqOnPositiveCountYieldsNotNull()
        {
            CPV v = new CPV(new object[] { 1, 2, 3 });

            Expect(v.rseq(), Not.Null);
        }

        [Test]
        public void RseqOnPositiveCountYieldsValidSequence()
        {
            CPV v = new CPV(new object[] { 1, 2, 3 });
            ISeq s = v.rseq();

            Expect(s.first(), EqualTo(3));
            Expect(s.next().first(), EqualTo(2));
            Expect(s.next().next().first(), EqualTo(1));
            Expect(s.next().next().next(), Null);
        }


        #endregion

        #region Associative tests

        [Test]
        public void ContainsKeyOnNonNumericIsFalse()
        {
            CPV v = new CPV(new object[] { 4, 5, 6 });

            Expect(v.containsKey("a"), False);
        }

        [Test]
        public void ContainsKeyOnIndexInRangeIsTrue()
        {
            CPV v = new CPV(new object[] { 4, 5, 6 });

            Expect(v.containsKey(1.2));
        }


        [Test]
        public void ContainsKeyOnIndexOutOfRangeIsFalse()
        {
            CPV v = new CPV(new object[] { 4, 5, 6 });

            Expect(v.containsKey(5),False);
        }

        [Test]
        public void EntryAtOnNonNumericReturnsNull()
        {
            CPV v = new CPV(new object[] { 4, 5, 6 });

            IMapEntry me = v.entryAt("a");

            Expect(me, Null);
        }

        [Test]
        public void EntryAtOnIndexInRangeReturnsEntry()
        {
            CPV v = new CPV(new object[] { 4, 5, 6 });

            IMapEntry me = v.entryAt(1);

            Expect(me.key(), EqualTo(1));
            Expect(me.val(),EqualTo(5));
        }


        [Test]
        public void EntryAtOnIndexOutOfRangeReturnsNull()
        {
            CPV v = new CPV(new object[] { 4, 5, 6 });

            IMapEntry me = v.entryAt(5);

            Expect(me, Null);
        }

        [Test]
        [ExpectedException(typeof(ArgumentException))]
        public void AssocWithNonNumericKeyThrowsException()
        {
            CPV v = new CPV(new object[] { 4, 5, 6 });
            Associative a = v.assoc("a", 7);
        }

        [Test]
        public void AssocWithNumericKeyInRangeChangesValue()
        {
            //This just checks that APersistentVector.assoc calls CPV.assocN
            CPV v = new CPV(new object[] { 4, 5, 6 });
            Associative a = v.assoc(1, 10);

            Expect(a.valAt(0), EqualTo(4));
            Expect(a.valAt(1), EqualTo(10));
            Expect(a.valAt(2), EqualTo(6));
            Expect(a.count(), EqualTo(3));
        }

        [Test]
        public void AssocWithNumericKeyOnePastEndAddValue()
        {
            //This just checks that APersistentVector.assoc calls CPV.assocN
            CPV v = new CPV(new object[] { 4, 5, 6 });
            Associative a = v.assoc(3, 10);

            Expect(a.valAt(0), EqualTo(4));
            Expect(a.valAt(1), EqualTo(5));
            Expect(a.valAt(2), EqualTo(6));
            Expect(a.valAt(3), EqualTo(10));
            Expect(a.count(), EqualTo(4));
        }

        [Test]
        [ExpectedException(typeof(IndexOutOfRangeException))]
        public void AssocWithNumericKeyOutOfRangeHighThrowsException()
        {
            //This just checks that APersistentVector.assoc calls CPV.assocN
            CPV v = new CPV(new object[] { 4, 5, 6 });
            Associative a = v.assoc(4, 10);
        }

        [Test]
        [ExpectedException(typeof(IndexOutOfRangeException))]
        public void AssocWithNumericKeyOutOfRangeLowThrowsException()
        {
            //This just checks that APersistentVector.assoc calls CPV.assocN
            CPV v = new CPV(new object[] { 4, 5, 6 });
            Associative a = v.assoc(-1, 10);
        }

        [Test]
        public void ValAtOnNonNumericReturnsDefault()
        {
            CPV v = new CPV(new object[] { 4, 5, 6 });

            object val1 = v.valAt("a");
            object val2 = v.valAt("a", "abc");

            Expect(val1, Null);
            Expect(val2, EqualTo("abc"));
        }

        [Test]
        public void ValAtOnIndexInRangeReturnsEntry()
        {
            CPV v = new CPV(new object[] { 4, 5, 6 });

            object val1 = v.valAt(1);
            object val2 = v.valAt(1, "abc");

            Expect(val1, EqualTo(5));
            Expect(val2, EqualTo(5));
        }


        [Test]
        public void ValAtOnIndexOutOfRangeReturnsDefault()
        {
            CPV v = new CPV(new object[] { 4, 5, 6 });

            IMapEntry me = v.entryAt(5);

            object val1 = v.valAt(4);
            object val2 = v.valAt(4, "abc");

            Expect(val1, Null);
            Expect(val2, EqualTo("abc"));
        }



        #endregion

        #region IPersistentStack tests

        [Test]
        public void PeekOnCount0ReturnsNull()
        {
            CPV v = new CPV(new object[] {});

            Expect(v.peek(), Null);
        }

        [Test]
        public void PeekOnPositiveCountReturnsLastItem()
        {
            CPV v = new CPV(new object[] { 1, 2, 3 });

            Expect(v.peek(), EqualTo(3));
        }

        #endregion

        #region APersistentVector.Seq tests

        // We'll do all the tests indirectly.

        [Test]
        public void SeqFirstAndRestWork()
        {
            CPV v = new CPV(new object[] { 4, 5, 6 }); 
            ISeq s = v.seq();

            Expect(s.first(), EqualTo(4));
            Expect(s.next().first(), EqualTo(5));
            Expect(s.next().next().first(), EqualTo(6));
            Expect(s.next().next().next(), Null);
        }

        [Test]
        public void SeqIndexedWorks()
        {
            CPV v = new CPV(new object[] { 4, 5, 6 });
            ISeq s0 = v.seq();
            IndexedSeq i0 = s0 as IndexedSeq;

            ISeq s1 = s0.next();
            IndexedSeq i1 = s1 as IndexedSeq;

            Expect(i0.index(), EqualTo(0));
            Expect(i1.index(), EqualTo(1));
        }

        [Test]
        public void SeqCountWorks()
        {
            CPV v = new CPV(new object[] { 4, 5, 6 });
            ISeq s = v.seq();

            Expect(s.count(), EqualTo(3));
            Expect(s.next().count(), EqualTo(2));
            Expect(s.next().next().count(), EqualTo(1));
        }

        [Test]
        public void SeqWithMetaHasMeta()
        {
            MockRepository mocks = new MockRepository();
            IPersistentMap meta = mocks.StrictMock<IPersistentMap>();
            mocks.ReplayAll();

            CPV v = new CPV(new object[] { 4, 5, 6 });
            IObj s = (IObj)v.seq();
            IObj obj = s.withMeta(meta);

            Expect(obj.meta(), SameAs(meta));
            mocks.VerifyAll();
        }

        [Test]
        public void SeqReduceWithNoStartIterates()
        {
            MockRepository mocks = new MockRepository();
            IFn fn = mocks.StrictMock<IFn>();
            RMExpect.Call(fn.invoke(1, 2)).Return(5);
            RMExpect.Call(fn.invoke(5, 3)).Return(7);
            mocks.ReplayAll();

            CPV v = new CPV(new object[] { 1, 2, 3 });
            IReduce r = (IReduce)v.seq();
            object ret = r.reduce(fn);

            Expect(ret, EqualTo(7));
            mocks.VerifyAll();
        }

        [Test]
        public void SeqReduceWithStartIterates()
        {
            MockRepository mocks = new MockRepository();
            IFn fn = mocks.StrictMock<IFn>();
            RMExpect.Call(fn.invoke(20, 1)).Return(10);
            RMExpect.Call(fn.invoke(10, 2)).Return(5);
            RMExpect.Call(fn.invoke(5, 3)).Return(7);
            mocks.ReplayAll();

            CPV v = new CPV(new object[] { 1, 2, 3 });
            IReduce r = (IReduce)v.seq();
            object ret = r.reduce(fn, 20);

            Expect(ret, EqualTo(7));
            mocks.VerifyAll();
        }

        #endregion

        #region APersistentVector.RSeq tests

        // We'll do all the tests indirectly.

        [Test]
        public void RSeqFirstAndRestWork()
        {
            CPV v = new CPV(new object[] { 4, 5, 6 });
            ISeq s = v.rseq();

            Expect(s.first(), EqualTo(6));
            Expect(s.next().first(), EqualTo(5));
            Expect(s.next().next().first(), EqualTo(4));
            Expect(s.next().next().next(), Null);
        }

        [Test]
        public void RSeqIndexedWorks()
        {
            CPV v = new CPV(new object[] { 4, 5, 6 });
            ISeq s0 = v.rseq();
            IndexedSeq i0 = s0 as IndexedSeq;

            ISeq s1 = s0.next();
            IndexedSeq i1 = s1 as IndexedSeq;

            Expect(i0.index(), EqualTo(0));
            Expect(i1.index(), EqualTo(1));
        }

        [Test]
        public void RSeqCountWorks()
        {
            CPV v = new CPV(new object[] { 4, 5, 6 });
            ISeq s = v.rseq();

            Expect(s.count(), EqualTo(3));
            Expect(s.next().count(), EqualTo(2));
            Expect(s.next().next().count(), EqualTo(1));
        }

        [Test]
        public void RSeqWithMetaHasMeta()
        {
            MockRepository mocks = new MockRepository();
            IPersistentMap meta = mocks.StrictMock<IPersistentMap>();
            mocks.ReplayAll();

            CPV v = new CPV(new object[] { 4, 5, 6 });
            IObj s = (IObj)v.rseq();
            IObj obj = s.withMeta(meta);

            Expect(obj.meta(), SameAs(meta));
            mocks.VerifyAll();
        }

        [Test]
        public void RSeqReduceWithNoStartIterates()
        {
            MockRepository mocks = new MockRepository();
            IFn fn = mocks.StrictMock<IFn>();
            RMExpect.Call(fn.invoke(3, 2)).Return(5);
            RMExpect.Call(fn.invoke(5, 1)).Return(7);
            mocks.ReplayAll();

            CPV v = new CPV(new object[] { 1, 2, 3 });
            IReduce r = (IReduce)v.rseq();
            object ret = r.reduce(fn);

            Expect(ret, EqualTo(7));
            mocks.VerifyAll();
        }

        [Test]
        public void RSeqReduceWithStartIterates()
        {
            MockRepository mocks = new MockRepository();
            IFn fn = mocks.StrictMock<IFn>();
            RMExpect.Call(fn.invoke(20, 3)).Return(10);
            RMExpect.Call(fn.invoke(10, 2)).Return(5);
            RMExpect.Call(fn.invoke(5, 1)).Return(7);
            mocks.ReplayAll();

            CPV v = new CPV(new object[] { 1, 2, 3 });
            IReduce r = (IReduce)v.rseq();
            object ret = r.reduce(fn, 20);

            Expect(ret, EqualTo(7));
            mocks.VerifyAll();
        }

        #endregion

    }
}