| 2fd4c7c by Jonathan Pryor at 2010-06-09 |
1 |
// |
|
2 |
// IListContract.cs |
|
3 |
// |
|
4 |
// Author: |
|
5 |
// Jonathan Pryor <jpryor@novell.com> |
|
6 |
// |
|
7 |
// Copyright (c) 2010 Novell, Inc. (http://www.novell.com) |
|
8 |
// |
|
9 |
// Permission is hereby granted, free of charge, to any person obtaining |
|
10 |
// a copy of this software and associated documentation files (the |
|
11 |
// "Software"), to deal in the Software without restriction, including |
|
12 |
// without limitation the rights to use, copy, modify, merge, publish, |
|
13 |
// distribute, sublicense, and/or sell copies of the Software, and to |
|
14 |
// permit persons to whom the Software is furnished to do so, subject to |
|
15 |
// the following conditions: |
|
16 |
// |
|
17 |
// The above copyright notice and this permission notice shall be |
|
18 |
// included in all copies or substantial portions of the Software. |
|
19 |
// |
|
20 |
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
|
21 |
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
|
22 |
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
|
23 |
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE |
|
24 |
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION |
|
25 |
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION |
|
26 |
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
|
27 |
// |
|
28 |
|
|
29 |
using System; |
|
30 |
using System.Collections.Generic; |
|
31 |
using System.Linq; |
|
32 |
|
|
33 |
using NUnit.Framework; |
|
34 |
|
|
35 |
using Cadenza.Collections; |
|
36 |
using Cadenza.Tests; |
|
37 |
|
|
38 |
namespace Cadenza.Collections.Tests { |
|
39 |
|
| ee22786 by Jonathan Pryor at 2010-06-09 |
40 |
public abstract class ListContract<T> : CollectionContract<T> { |
| 2fd4c7c by Jonathan Pryor at 2010-06-09 |
41 |
|
|
42 |
private IList<T> CreateList (IEnumerable<T> values) |
|
43 |
{ |
|
44 |
return (IList<T>) CreateCollection (values); |
|
45 |
} |
|
46 |
|
|
47 |
[Test] |
|
48 |
public void IndexOf () |
|
49 |
{ |
|
50 |
var a = CreateValueA (); |
|
51 |
var b = CreateValueB (); |
|
52 |
|
|
53 |
var list = CreateList (new T[0]); |
|
54 |
|
|
55 |
Assert.AreEqual (-1, list.IndexOf (a)); |
|
56 |
|
|
57 |
try { |
|
58 |
list.Add (a); |
|
59 |
Assert.AreEqual (0, list.IndexOf (a)); |
|
60 |
|
|
61 |
list.Add (b); |
|
62 |
Assert.AreEqual (1, list.IndexOf (b)); |
|
63 |
|
|
64 |
list.Remove (a); |
|
65 |
Assert.AreEqual (-1, list.IndexOf (a)); |
|
66 |
Assert.AreEqual (0, list.IndexOf (b)); |
|
67 |
|
|
68 |
list.Remove (b); |
|
69 |
Assert.AreEqual (-1, list.IndexOf (b)); |
|
70 |
} |
|
71 |
catch (NotSupportedException) { |
|
72 |
Assert.IsTrue (list.IsReadOnly); |
|
73 |
} |
|
74 |
} |
|
75 |
|
|
76 |
[Test] |
|
77 |
public void Insert () |
|
78 |
{ |
|
79 |
var a = CreateValueA (); |
|
80 |
var b = CreateValueB (); |
|
81 |
|
|
82 |
var list = CreateList (new T[0]); |
|
83 |
|
|
84 |
try { |
|
85 |
Assert.Throws<ArgumentOutOfRangeException>(() => list.Insert (-1, a)); |
|
86 |
Assert.Throws<ArgumentOutOfRangeException>(() => list.Insert (1, a)); |
|
87 |
|
|
88 |
list.Insert (0, a); |
|
89 |
Assert.AreEqual (0, list.IndexOf (a)); |
|
90 |
|
|
91 |
list.Insert (0, b); |
|
92 |
Assert.AreEqual (2, list.Count); |
|
93 |
Assert.AreEqual (0, list.IndexOf (b)); |
|
94 |
Assert.AreEqual (1, list.IndexOf (a)); |
|
95 |
} |
|
96 |
catch (NotSupportedException) { |
|
97 |
Assert.IsTrue (list.IsReadOnly); |
|
98 |
} |
|
99 |
} |
|
100 |
|
|
101 |
[Test] |
|
102 |
public void RemoveAt () |
|
103 |
{ |
|
104 |
var a = CreateValueA (); |
|
105 |
var b = CreateValueB (); |
|
106 |
|
|
107 |
var list = CreateList (new T [0]); |
|
108 |
|
|
109 |
try { |
|
110 |
Assert.Throws<ArgumentOutOfRangeException>(() => list.RemoveAt (-1)); |
|
111 |
Assert.Throws<ArgumentOutOfRangeException>(() => list.RemoveAt (0)); |
|
112 |
|
|
113 |
list.Add (a); |
|
114 |
Assert.AreEqual (1, list.Count); |
|
115 |
|
|
116 |
list.RemoveAt (0); |
|
117 |
Assert.AreEqual (0, list.Count); |
|
118 |
|
|
119 |
list.Add (a); |
|
120 |
list.Add (b); |
|
121 |
list.RemoveAt (0); |
|
122 |
Assert.AreEqual (1, list.Count); |
|
123 |
Assert.AreEqual (0, list.IndexOf (b)); |
|
124 |
} |
|
125 |
catch (NotSupportedException) { |
|
126 |
Assert.IsTrue (list.IsReadOnly); |
|
127 |
} |
|
128 |
} |
|
129 |
|
|
130 |
[Test] |
|
131 |
public void Item () |
|
132 |
{ |
|
133 |
var a = CreateValueA (); |
|
134 |
var b = CreateValueB (); |
|
135 |
|
|
136 |
var list = CreateList (new []{a}); |
|
137 |
|
|
138 |
Assert.AreEqual (a, list [0]); |
|
139 |
Assert.Throws<ArgumentOutOfRangeException>(() => Ignore (list [-1])); |
|
140 |
|
|
141 |
try { |
|
142 |
Assert.Throws<ArgumentOutOfRangeException>(() => list [-1] = a); |
|
143 |
Assert.Throws<ArgumentOutOfRangeException>(() => list [1] = a); |
|
144 |
|
|
145 |
list [0] = b; |
|
146 |
Assert.AreEqual (-1, list.IndexOf (a)); |
|
147 |
Assert.AreEqual (0, list.IndexOf (b)); |
|
148 |
} |
|
149 |
catch (NotSupportedException) { |
|
150 |
Assert.IsTrue (list.IsReadOnly); |
|
151 |
} |
|
152 |
} |
|
153 |
} |
|
154 |
} |
|
155 |
|