aboutsummaryrefslogtreecommitdiff
path: root/ClojureCLR/Clojure/Clojure.Tests/LibTests/PersistentListTests.cs
blob: 7ae7b78b7891074823fa6c8b56bb9bd8a12d65f3 (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
/**
 *   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
{
    [TestFixture]
    public class PersistentListTests : AssertionHelper
    {
        #region C-tor tests

        [Test]
        public void OneArgCtorConstructsListOfOneElement()
        {
            PersistentList p = new PersistentList("abc");

            Expect(p.first(), EqualTo("abc"));
            Expect(p.next(), Null);
            Expect(p.count(), EqualTo(1));
        }

        [Test]
        public void ListCtorConstructsListOfSeveralElements()
        {
            object[] items = new object[] { 1, "abc", 2, "def" };
            IPersistentList p = PersistentList.create(items);

            Expect(p.count(), EqualTo(4));

            ISeq s = p.seq();
            Expect(s.first(), EqualTo(1));
            Expect(s.next().first(), EqualTo("abc"));
            Expect(s.next().next().first(), EqualTo(2));
            Expect(s.next().next().next().first(), EqualTo("def"));
            Expect(s.next().next().next().next(), Null);
        }


        #endregion

        #region IPersistentStack tests

        [Test]
        public void PeekYieldsFirstElementAndListUnchanged()
        {
            PersistentList p = (PersistentList)PersistentList.create(new object[] { "abc", 1, "def" });

            Expect(p.peek(), EqualTo("abc"));
            Expect(p.count(), EqualTo(3));
        }

        [Test]
        public void PopLosesfirstElement()
        {
            PersistentList p = (PersistentList)PersistentList.create(new object[]{"abc", 1, "def"});
            PersistentList p2 = (PersistentList)p.pop();
            Expect(p2.count(), EqualTo(2));
            Expect(p2.peek(), EqualTo(1));
        }

        [Test]
        public void PopOnSingletonListYieldsEmptyList()
        {
            PersistentList p = new PersistentList("abc");
            IPersistentStack s = p.pop();
            Expect(s.count(), EqualTo(0));
        }

        [Test]
        [ExpectedException(typeof(InvalidOperationException))]
        public void DoublePopOnSingletonListYieldsException()
        {
            PersistentList p = new PersistentList("abc");
            IPersistentStack s = p.pop().pop();
        }

        #endregion

        #region IPersistentCollection tests

        [Test]
        public void EmptyHasNoElements()
        {
            PersistentList p = new PersistentList("abc");
            IPersistentCollection c = p.empty();

            Expect(c.count(), EqualTo(0));
        }

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

            IPersistentCollection p = (IPersistentCollection)new PersistentList("abc").withMeta(meta);
            IObj obj = (IObj) p.empty();

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

        #endregion

        #region IReduce  tests

        [Test]
        public void ReduceWithNoStartIterates()
        {
            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();

            PersistentList p = (PersistentList)PersistentList.create(new object[] { 1, 2, 3 });
            object ret = p.reduce(fn);

            Expect(ret, EqualTo(7));

            mocks.VerifyAll();
        }

        [Test]
        public void ReduceWithStartIterates()
        {
            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();

            PersistentList p = (PersistentList)PersistentList.create(new object[] { 1, 2, 3 });
            object ret = p.reduce(fn, 20);

            Expect(ret, EqualTo(7));

            mocks.VerifyAll();
        }


        #endregion

    }

    [TestFixture]
    public class PersistentList_ISeq_Tests : ISeqTestHelper
    {
        PersistentList _pl;
        PersistentList _plWithMeta;
        object[] _values;


        [SetUp]
        public void Setup()
        {
            PersistentList p1 = new PersistentList("abc");
            PersistentList p2 = (PersistentList)p1.cons("def");
            _pl = (PersistentList)p2.cons(7);
            _values = new object[] { 7, "def", "abc" };
            _plWithMeta = (PersistentList)_pl.withMeta(PersistentHashMap.create("a", 1));
        }

        [Test]
        public void ISeq_has_correct_valuess()
        {
            VerifyISeqContents(_pl, _values);
        }

        [Test]
        public void ISeq_with_meta_has_correct_valuess()
        {
            VerifyISeqContents(_plWithMeta, _values);
        }

        [Test]
        public void Rest_has_correct_type()
        {
            VerifyISeqRestTypes(_pl, typeof(PersistentList));
        }

        [Test]
        public void Cons_works()
        {
            VerifyISeqCons(_pl, "pqr", _values);
        }

        [Test]
        public void ConsPreservesMeta()
        {
            PersistentList p2 = (PersistentList)_plWithMeta.cons("def");
            Expect(p2.meta(), SameAs(_plWithMeta.meta()));
        }
    }


    [TestFixture]
    public class PersistentList_IObj_Tests : IObjTests
    {
        MockRepository _mocks;

        [SetUp]
        public void Setup()
        {
            _mocks = new MockRepository();
            IPersistentMap meta = _mocks.StrictMock<IPersistentMap>();
            _mocks.ReplayAll();

            PersistentList p1 = (PersistentList)PersistentList.create(new object[] { "abc", "def" });


            _objWithNullMeta = (IObj)p1;
            _obj = _objWithNullMeta.withMeta(meta);
            _expectedType = typeof(PersistentList);
        }

        [TearDown]
        public void Teardown()
        {
            _mocks.VerifyAll();
        }

    }
}