Project

General

Profile

root / branches / compiler / cSharp / ooasCompiler / src / libs / c5 / nunit / arrays / CircularQueueTest.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.arrays.circularqueue
27
{
28
  using CollectionOfInt = CircularQueue<int>;
29

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

    
41
    [Test]
42
    public void Extensible()
43
    {
44
      //TODO: Test Circular Queue for Clone(?) and Serializable 
45
      //C5UnitTests.Templates.Extensible.Clone.Tester<CollectionOfInt>();
46
      //C5UnitTests.Templates.Extensible.Serialization.Tester<CollectionOfInt>();
47
    }
48
  }
49

    
50
  //[TestFixture]
51
  public class Template
52
  {
53
    private CircularQueue<int> queue;
54

    
55
    [SetUp]
56
    public void Init()
57
    {
58
      queue = new CircularQueue<int>();
59
    }
60

    
61
    [Test]
62
    public void LeTest()
63
    {
64
    }
65

    
66
    [TearDown]
67
    public void Dispose() { queue = null; }
68

    
69
  }
70

    
71
  [TestFixture]
72
  public class Formatting
73
  {
74
    CircularQueue<int> coll;
75
    IFormatProvider rad16;
76
    [SetUp]
77
    public void Init() { coll = new CircularQueue<int>(); rad16 = new RadixFormatProvider(16); }
78
    [TearDown]
79
    public void Dispose() { coll = null; rad16 = null; }
80
    [Test]
81
    public void Format()
82
    {
83
      Assert.AreEqual("{  }", coll.ToString());
84
      foreach (int i in new int[] { -4, 28, 129, 65530 })
85
        coll.Enqueue(i);
86
      Assert.AreEqual("{ -4, 28, 129, 65530 }", coll.ToString());
87
      Assert.AreEqual("{ -4, 1C, 81, FFFA }", coll.ToString(null, rad16));
88
      Assert.AreEqual("{ -4, 28, 129... }", coll.ToString("L14", null));
89
      Assert.AreEqual("{ -4, 1C, 81... }", coll.ToString("L14", rad16));
90
    }
91
  }
92

    
93
  [TestFixture]
94
  public class CircularQueue
95
  {
96
    private CircularQueue<int> queue;
97

    
98
    [SetUp]
99
    public void Init()
100
    {
101
      queue = new CircularQueue<int>();
102
    }
103

    
104
    void loadup1()
105
    {
106
      queue.Enqueue(11);
107
      queue.Enqueue(12);
108
      queue.Enqueue(13);
109
      queue.Dequeue();
110
      queue.Enqueue(103);
111
      queue.Enqueue(14);
112
      queue.Enqueue(15);
113
    }
114

    
115
    void loadup2()
116
    {
117
      loadup1();
118
      for (int i = 0; i < 4; i++)
119
      {
120
        queue.Dequeue();
121
        queue.Enqueue(1000 + i);
122
      }
123
    }
124

    
125
    void loadup3()
126
    {
127
      for (int i = 0; i < 18; i++)
128
      {
129
        queue.Enqueue(i);
130
        Assert.IsTrue(queue.Check());
131
      }
132
      for (int i = 0; i < 14; i++)
133
      {
134
        Assert.IsTrue(queue.Check());
135
        queue.Dequeue();
136
      }
137
    }
138

    
139
    [Test]
140
    public void Expand()
141
    {
142
      Assert.IsTrue(queue.Check());
143
      loadup3();
144
      Assert.IsTrue(IC.eq(queue, 14, 15, 16, 17));
145
    }
146

    
147
    [Test]
148
    public void Simple()
149
    {
150
      loadup1();
151
      Assert.IsTrue(queue.Check());
152
      Assert.AreEqual(5, queue.Count);
153
      Assert.IsTrue(IC.eq(queue, 12, 13, 103, 14, 15));
154
      Assert.AreEqual(12, queue.Choose());
155
    }
156

    
157
    [Test]
158
    public void Stack()
159
    {
160
      queue.Push(1);
161
      Assert.IsTrue(queue.Check());
162
      queue.Push(2);
163
      Assert.IsTrue(queue.Check());
164
      queue.Push(3);
165
      Assert.IsTrue(queue.Check());
166
      Assert.AreEqual(3, queue.Pop());
167
      Assert.IsTrue(queue.Check());
168
      Assert.AreEqual(2, queue.Pop());
169
      Assert.IsTrue(queue.Check());
170
      Assert.AreEqual(1, queue.Pop());
171
      Assert.IsTrue(queue.Check());
172
    }
173

    
174
    [Test]
175
    [ExpectedException(typeof(NoSuchItemException))]
176
    public void BadChoose()
177
    {
178
      queue.Choose();
179
    }
180

    
181
    [Test]
182
    [ExpectedException(typeof(NoSuchItemException))]
183
    public void BadDequeue()
184
    {
185
      queue.Dequeue();
186
    }
187

    
188
    [Test]
189
    public void Simple2()
190
    {
191
      loadup2();
192
      Assert.IsTrue(queue.Check());
193
      Assert.AreEqual(5, queue.Count);
194
      Assert.IsTrue(IC.eq(queue, 15, 1000, 1001, 1002, 1003));
195
      Assert.AreEqual(15, queue.Choose());
196
    }
197

    
198
    [Test]
199
    public void Counting()
200
    {
201
      Assert.IsTrue(queue.IsEmpty);
202
      Assert.AreEqual(0, queue.Count);
203
      Assert.AreEqual(Speed.Constant, queue.CountSpeed);
204
      queue.Enqueue(11);
205
      Assert.IsFalse(queue.IsEmpty);
206
      queue.Enqueue(12);
207
      Assert.AreEqual(2, queue.Count);
208
    }
209

    
210
    //This test by Steve Wallace uncovered a bug in the indexing.
211
    [Test]
212
    public void SW200602()
213
    {
214
      C5.CircularQueue<int> list = new C5.CircularQueue<int>(8);
215
      for (int count = 0; count <= 7; count++)
216
      {
217
        list.Enqueue(count);
218
      }
219
      int end = list.Count;
220
      for (int index = 0; index < end; index++)
221
      {
222
        Assert.AreEqual(index, list[0]);
223
        list.Dequeue();
224
      }
225
    }
226

    
227
    [TearDown]
228
    public void Dispose() { queue = null; }
229

    
230
  }
231
}