Project

General

Profile

root / branches / compiler / cSharp / ooasCompiler / src / libs / c5 / nunit / hashing / HashDictionaryTests.cs @ 3

1
/*
2
 Copyright (c) 2003-2006 Niels Kokholm and Peter Sestoft
3
 Permission is hereby granted, free of charge, to any person obtaining a copy
4
 of this software and associated documentation files (the "Software"), to deal
5
 in the Software without restriction, including without limitation the rights
6
 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7
 copies of the Software, and to permit persons to whom the Software is
8
 furnished to do so, subject to the following conditions:
9
 
10
 The above copyright notice and this permission notice shall be included in
11
 all copies or substantial portions of the Software.
12
 
13
 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
19
 SOFTWARE.
20
*/
21

    
22
using System;
23
using C5;
24
using NUnit.Framework;
25
using SCG = System.Collections.Generic;
26
namespace C5UnitTests.hashtable.dictionary
27
{
28
  using DictionaryIntToInt = HashDictionary<int, int>;
29

    
30
  [TestFixture]
31
  public class GenericTesters
32
  {
33
    [Test]
34
    public void TestEvents()
35
    {
36
      Fun<DictionaryIntToInt> factory = delegate() { return new DictionaryIntToInt(TenEqualityComparer.Default); };
37
      new C5UnitTests.Templates.Events.DictionaryTester<DictionaryIntToInt>().Test(factory);
38
    }
39

    
40
    [Test]
41
    public void TestSerialize()
42
    {
43
      C5UnitTests.Templates.Extensible.Serialization.DTester<DictionaryIntToInt>();
44
    }
45
  }
46

    
47
  static class Factory
48
  {
49
    public static IDictionary<K, V> New<K, V>() { return new HashDictionary<K, V>(); }
50
  }
51

    
52
  [TestFixture]
53
  public class Formatting
54
  {
55
    IDictionary<int, int> coll;
56
    IFormatProvider rad16;
57
    [SetUp]
58
    public void Init() { coll = Factory.New<int, int>(); rad16 = new RadixFormatProvider(16); }
59
    [TearDown]
60
    public void Dispose() { coll = null; rad16 = null; }
61
    [Test]
62
    public void Format()
63
    {
64
      Assert.AreEqual("{  }", coll.ToString());
65
      coll.Add(23, 67); coll.Add(45, 89);
66
      Assert.AreEqual("{ 45 => 89, 23 => 67 }", coll.ToString());
67
      Assert.AreEqual("{ 2D => 59, 17 => 43 }", coll.ToString(null, rad16));
68
      Assert.AreEqual("{ 45 => 89, ... }", coll.ToString("L14", null));
69
      Assert.AreEqual("{ 2D => 59, ... }", coll.ToString("L14", rad16));
70
    }
71
  }
72

    
73
  [TestFixture]
74
  public class HashDict
75
  {
76
    private HashDictionary<string, string> dict;
77

    
78

    
79
    [SetUp]
80
    public void Init()
81
    {
82
      dict = new HashDictionary<string, string>();
83
      //dict = TreeDictionary<string,string>.MakeNaturalO<string,string>();
84
    }
85

    
86
    [Test]
87
    [ExpectedException(typeof(NullReferenceException))]
88
    public void NullEqualityComparerinConstructor1()
89
    {
90
      new HashDictionary<int, int>(null);
91
    }
92

    
93
    [Test]
94
    [ExpectedException(typeof(NullReferenceException))]
95
    public void NullEqualityComparerinConstructor2()
96
    {
97
      new HashDictionary<int, int>(5, 0.5, null);
98
    }
99

    
100
    [Test]
101
    public void Choose()
102
    {
103
      dict.Add("ER", "FOO");
104
      Assert.AreEqual(new KeyValuePair<string, string>("ER", "FOO"), dict.Choose());
105
    }
106

    
107
    [Test]
108
    [ExpectedException(typeof(NoSuchItemException))]
109
    public void BadChoose()
110
    {
111
      dict.Choose();
112
    }
113

    
114

    
115

    
116
    [TearDown]
117
    public void Dispose()
118
    {
119
      dict = null;
120
    }
121

    
122

    
123
    [Test]
124
    public void Initial()
125
    {
126
      bool res;
127

    
128
      Assert.IsFalse(dict.IsReadOnly);
129
      Assert.AreEqual(0, dict.Count, "new dict should be empty");
130
      dict.Add("A", "B");
131
      Assert.AreEqual(1, dict.Count, "bad count");
132
      Assert.AreEqual("B", dict["A"], "Wrong value for dict[A]");
133
      dict.Add("C", "D");
134
      Assert.AreEqual(2, dict.Count, "bad count");
135
      Assert.AreEqual("B", dict["A"], "Wrong value");
136
      Assert.AreEqual("D", dict["C"], "Wrong value");
137
      res = dict.Remove("A");
138
      Assert.IsTrue(res, "bad return value from Remove(A)");
139
      Assert.AreEqual(1, dict.Count, "bad count");
140
      Assert.AreEqual("D", dict["C"], "Wrong value of dict[C]");
141
      res = dict.Remove("Z");
142
      Assert.IsFalse(res, "bad return value from Remove(Z)");
143
      Assert.AreEqual(1, dict.Count, "bad count");
144
      Assert.AreEqual("D", dict["C"], "Wrong value of dict[C] (2)");
145
    }
146

    
147

    
148
    [Test]
149
    public void Contains()
150
    {
151
      dict.Add("C", "D");
152
      Assert.IsTrue(dict.Contains("C"));
153
      Assert.IsFalse(dict.Contains("D"));
154
    }
155

    
156

    
157
    [Test]
158
    [ExpectedException(typeof(DuplicateNotAllowedException), "Key being added: 'A'")]
159
    public void IllegalAdd()
160
    {
161
      dict.Add("A", "B");
162
      dict.Add("A", "B");
163
    }
164

    
165

    
166
    [Test]
167
    [ExpectedException(typeof(NoSuchItemException))]
168
    public void GettingNonExisting()
169
    {
170
      Console.WriteLine(dict["R"]);
171
    }
172

    
173

    
174
    [Test]
175
    public void Setter()
176
    {
177
      dict["R"] = "UYGUY";
178
      Assert.AreEqual("UYGUY", dict["R"]);
179
      dict["R"] = "UIII";
180
      Assert.AreEqual("UIII", dict["R"]);
181
      dict["S"] = "VVV";
182
      Assert.AreEqual("UIII", dict["R"]);
183
      Assert.AreEqual("VVV", dict["S"]);
184
      //dict.dump();
185
    }
186

    
187
    [Test]
188
    public void CombinedOps()
189
    {
190
      dict["R"] = "UIII";
191
      dict["S"] = "VVV";
192
      dict["T"] = "XYZ";
193

    
194
      string s;
195

    
196
      Assert.IsTrue(dict.Remove("S", out s));
197
      Assert.AreEqual("VVV", s);
198
      Assert.IsFalse(dict.Contains("S"));
199
      Assert.IsFalse(dict.Remove("A", out s));
200

    
201
      //
202
      Assert.IsTrue(dict.Find("T", out s));
203
      Assert.AreEqual("XYZ", s);
204
      Assert.IsFalse(dict.Find("A", out s));
205

    
206
      //
207
      Assert.IsTrue(dict.Update("R", "UHU"));
208
      Assert.AreEqual("UHU", dict["R"]);
209
      Assert.IsFalse(dict.Update("A", "W"));
210
      Assert.IsFalse(dict.Contains("A"));
211

    
212
      //
213
      s = "KKK";
214
      Assert.IsFalse(dict.FindOrAdd("B", ref s));
215
      Assert.AreEqual("KKK", dict["B"]);
216
      Assert.IsTrue(dict.FindOrAdd("T", ref s));
217
      Assert.AreEqual("XYZ", s);
218

    
219
      //
220
      s = "LLL";
221
      Assert.IsTrue(dict.UpdateOrAdd("R", s));
222
      Assert.AreEqual("LLL", dict["R"]);
223
      s = "MMM";
224
      Assert.IsFalse(dict.UpdateOrAdd("C", s));
225
      Assert.AreEqual("MMM", dict["C"]);
226

    
227
      // bug20071112 fixed 2008-02-03
228
      s = "NNN";
229
      String old;
230
      Assert.IsTrue(dict.UpdateOrAdd("R", s, out old));
231
      Assert.AreEqual("NNN", dict["R"]);
232
      Assert.AreEqual("LLL", old);
233
      s = "OOO";
234
      Assert.IsFalse(dict.UpdateOrAdd("D", s, out old));
235
      Assert.AreEqual("OOO", dict["D"]);
236
      // Unclear which of these is correct:
237
      // Assert.AreEqual(null, old);
238
      // Assert.AreEqual("OOO", old);
239
    }
240

    
241
    [Test]
242
    public void DeepBucket()
243
    {
244
      HashDictionary<int, int> dict2 = new HashDictionary<int, int>();
245

    
246
      for (int i = 0; i < 5; i++)
247
        dict2[16 * i] = 5 * i;
248

    
249
      for (int i = 0; i < 5; i++)
250
        Assert.AreEqual(5 * i, dict2[16 * i]);
251

    
252
      for (int i = 0; i < 5; i++)
253
        dict2[16 * i] = 7 * i + 1;
254

    
255
      for (int i = 0; i < 5; i++)
256
        Assert.AreEqual(7 * i + 1, dict2[16 * i]);
257
      Assert.IsTrue(dict.Check());
258
    }
259
  }
260

    
261

    
262

    
263
  [TestFixture]
264
  public class Enumerators
265
  {
266
    private HashDictionary<string, string> dict;
267

    
268
    private SCG.IEnumerator<KeyValuePair<string, string>> dictenum;
269

    
270

    
271
    [SetUp]
272
    public void Init()
273
    {
274
      dict = new HashDictionary<string, string>();
275
      dict["S"] = "A";
276
      dict["T"] = "B";
277
      dict["R"] = "C";
278
      dictenum = dict.GetEnumerator();
279
    }
280

    
281

    
282
    [TearDown]
283
    public void Dispose()
284
    {
285
      dictenum = null;
286
      dict = null;
287
    }
288

    
289

    
290
    [Test]
291
    public void Keys()
292
    {
293
      SCG.IEnumerator<string> keys = dict.Keys.GetEnumerator();
294

    
295
      Assert.IsTrue(keys.MoveNext());
296
      Assert.AreEqual("R", keys.Current);
297
      Assert.IsTrue(keys.MoveNext());
298
      Assert.AreEqual("T", keys.Current);
299
      Assert.IsTrue(keys.MoveNext());
300
      Assert.AreEqual("S", keys.Current);
301
      Assert.IsFalse(keys.MoveNext());
302
    }
303

    
304

    
305
    [Test]
306
    public void Values()
307
    {
308
      SCG.IEnumerator<string> values = dict.Values.GetEnumerator();
309

    
310
      Assert.IsTrue(values.MoveNext());
311
      Assert.AreEqual("C", values.Current);
312
      Assert.IsTrue(values.MoveNext());
313
      Assert.AreEqual("B", values.Current);
314
      Assert.IsTrue(values.MoveNext());
315
      Assert.AreEqual("A", values.Current);
316
      Assert.IsFalse(values.MoveNext());
317
    }
318

    
319
    [Test]
320
    public void Fun()
321
    {
322
      Assert.AreEqual("B", dict.Fun("T"));
323
    }
324

    
325

    
326
    [Test]
327
    public void NormalUse()
328
    {
329
      Assert.IsTrue(dictenum.MoveNext());
330
      Assert.AreEqual(dictenum.Current, new KeyValuePair<string, string>("R", "C"));
331
      Assert.IsTrue(dictenum.MoveNext());
332
      Assert.AreEqual(dictenum.Current, new KeyValuePair<string, string>("T", "B"));
333
      Assert.IsTrue(dictenum.MoveNext());
334
      Assert.AreEqual(dictenum.Current, new KeyValuePair<string, string>("S", "A"));
335
      Assert.IsFalse(dictenum.MoveNext());
336
    }
337
  }
338
}
339

    
340

    
341

    
342

    
343