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
|
/**
* 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 PersistentVectorTests : AssertionHelper
{
#region C-tor tests
[Test]
public void CreateOnISeqReturnsCorrectCount()
{
Range r = new Range(2,5);
PersistentVector v = PersistentVector.create(r);
Expect(v.count(),EqualTo(r.count()));
}
[Test]
public void CreateOnISeqHasItems()
{
Range r = new Range(2, 5);
PersistentVector v = PersistentVector.create(r);
Expect(v.nth(0), EqualTo(2));
Expect(v.nth(1), EqualTo(3));
Expect(v.nth(2), EqualTo(4));
}
[Test]
public void CreateOnISeqWithManyItemsWorks()
{
// Want to bust out of the first tail, so need to insert more than 32 elements.
Range r = new Range(2, 1000);
PersistentVector v = PersistentVector.create(r);
Expect(v.count(), EqualTo(r.count()));
for (int i = 0; i < v.count(); ++i)
Expect(v.nth(i), EqualTo(i + 2));
}
[Test]
public void CreateOnISeqWithManyManyItemsWorks()
{
// Want to bust out of the first tail, so need to insert more than 32 elements.
// Let's get out of the second level, too.
Range r = new Range(2, 100000);
PersistentVector v = PersistentVector.create(r);
Expect(v.count(), EqualTo(r.count()));
for (int i = 0; i < v.count(); ++i)
Expect(v.nth(i), EqualTo(i + 2));
}
[Test]
public void CreateOnMultipleItemsWorks()
{
PersistentVector v = PersistentVector.create(2,3,4);
Expect(v.count(),EqualTo(3));
Expect(v.nth(0), EqualTo(2));
Expect(v.nth(1), EqualTo(3));
Expect(v.nth(2), EqualTo(4));
}
#endregion
#region IPersistentVector tests
// nth - tested in c-tor tests
[Test]
public void CountYieldsLength()
{
PersistentVector v = PersistentVector.create(1, 2, 3);
Expect(v.length(), EqualTo(3));
}
[Test]
[ExpectedException(typeof(IndexOutOfRangeException))]
public void NthOutOfRangeLowFails()
{
PersistentVector v = PersistentVector.create(1, 2, 3);
object obj = v.nth(-4);
}
[Test]
[ExpectedException(typeof(IndexOutOfRangeException))]
public void NthOutOfRangeHighFails()
{
PersistentVector v = PersistentVector.create(1, 2, 3);
object obj = v.nth(4);
}
[Test]
public void AssocNReplacesInRangeForSmall()
{
Range r = new Range(2, 5);
PersistentVector v1 = PersistentVector.create(r);
IPersistentVector v2 = v1.assocN(1,10);
Expect(v1.nth(0), EqualTo(2));
Expect(v1.nth(1), EqualTo(3));
Expect(v1.nth(2), EqualTo(4));
Expect(v1.count(), EqualTo(3));
Expect(v2.nth(0), EqualTo(2));
Expect(v2.nth(1), EqualTo(10));
Expect(v2.nth(2), EqualTo(4));
Expect(v2.count(), EqualTo(3));
}
[Test]
public void AssocNAddsAtEndForSmall()
{
Range r = new Range(2, 5);
PersistentVector v1 = PersistentVector.create(r);
IPersistentVector v2 = v1.assocN(3, 10);
Expect(v1.nth(0), EqualTo(2));
Expect(v1.nth(1), EqualTo(3));
Expect(v1.nth(2), EqualTo(4));
Expect(v1.count(), EqualTo(3));
Expect(v2.nth(0), EqualTo(2));
Expect(v2.nth(1), EqualTo(3));
Expect(v2.nth(2), EqualTo(4));
Expect(v2.nth(3), EqualTo(10));
Expect(v2.count(), EqualTo(4));
}
[Test]
[ExpectedException(typeof(IndexOutOfRangeException))]
public void AssocNOutOfRangeLowThrowsException()
{
Range r = new Range(2, 5);
PersistentVector v1 = PersistentVector.create(r);
IPersistentVector v2 = v1.assocN(-4, 10);
}
[Test]
[ExpectedException(typeof(IndexOutOfRangeException))]
public void AssocNOutOfRangeHighThrowsException()
{
Range r = new Range(2, 5);
PersistentVector v1 = PersistentVector.create(r);
IPersistentVector v2 = v1.assocN(4, 10);
}
[Test]
public void AssocNAddsAtEndForEmpty()
{
PersistentVector v1 = PersistentVector.create();
IPersistentVector v2 = v1.assocN(0, "abc");
Expect(v1.count(), EqualTo(0));
Expect(v2.count(), EqualTo(1));
Expect(v2.nth(0), EqualTo("abc"));
}
[Test]
public void AssocNChangesForBig()
{
Range r = new Range(2, 100000);
PersistentVector v1 = PersistentVector.create(r);
IPersistentVector v2 = v1;
for (int i = 0; i < 110000; i++)
v2 = v2.assocN(i, i + 20);
for ( int i=0; i<v1.count(); ++i )
Expect(v1.nth(i),EqualTo(i+2));
for (int i = 0; i < v2.count(); ++i)
Expect(v2.nth(i), EqualTo(i + 20));
}
[Test]
public void ConsWorks()
{
PersistentVector v1 = PersistentVector.create(2,3,4);
IPersistentVector v2 = v1;
for (int i = 3; i < 100000; i++)
v2 = v2.cons(i+2);
Expect(v1.count(), EqualTo(3));
Expect(v2.count(), EqualTo(100000));
for (int i = 0; i < v2.count(); ++i)
Expect(v2.nth(i), EqualTo(i + 2));
}
#endregion
#region IPersistentCollection tests
[Test]
public void EmptyReturnsEmptyCollection()
{
PersistentVector v = PersistentVector.create(1, 2, 3);
IPersistentCollection e = v.empty();
Expect(e.count(), EqualTo(0));
}
[Test]
public void EmptyCopiesMeta()
{
MockRepository mocks = new MockRepository();
IPersistentMap meta = mocks.StrictMock<IPersistentMap>();
mocks.ReplayAll();
PersistentVector v1 = PersistentVector.create(1, 2, 3);
IPersistentCollection e1 = v1.empty();
PersistentVector v2 = (PersistentVector) v1.withMeta(meta);
IPersistentCollection e2 = v2.empty();
Expect(((IObj)e1).meta(), Null);
Expect(((IObj)e2).meta(), SameAs(meta));
mocks.VerifyAll();
}
#endregion
#region IPersistentStack tests
[Test]
[ExpectedException(typeof(InvalidOperationException))]
public void PopOnEmptyThrowsException()
{
PersistentVector v = PersistentVector.create();
IPersistentStack s = v.pop();
}
[Test]
public void PopOnSizeOneReturnsEmpty()
{
PersistentVector v = PersistentVector.create(1);
IPersistentStack s = v.pop();
Expect(s.count(), EqualTo(0));
}
[Test]
public void PopOnSmallReturnsOneLess()
{
Range r = new Range(2, 20);
PersistentVector v = PersistentVector.create(r);
IPersistentStack s = v.pop();
Expect(v.count(),EqualTo(r.count()));
Expect(s.count(),EqualTo(v.count()-1));
}
[Test]
public void PopOnBigWorks()
{
Range r = new Range(0, 100000);
PersistentVector v = PersistentVector.create(r);
IPersistentStack s = v;
for (int i = 16; i < 100000; i++)
s = s.pop();
Expect(v.count(), EqualTo(r.count()));
Expect(s.count(), EqualTo(16));
}
#endregion
#region IFn tests
#endregion
}
[TestFixture]
public class PersistentVector_IObj_Tests : IObjTests
{
MockRepository _mocks;
[SetUp]
public void Setup()
{
_mocks = new MockRepository();
IPersistentMap meta = _mocks.StrictMock<IPersistentMap>();
_mocks.ReplayAll();
PersistentVector v = PersistentVector.create(2, 3, 4);
_objWithNullMeta = (IObj)v;
_obj = _objWithNullMeta.withMeta(meta);
_expectedType = typeof(PersistentVector);
}
[TearDown]
public void Teardown()
{
_mocks.VerifyAll();
}
}
}
|