aboutsummaryrefslogtreecommitdiff
path: root/Examples/test-suite/csharp/li_std_list_runme.cs
blob: 1a5b1562d738f55353baa32e2663a8895e4df97c (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
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
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
using System;
using li_std_listNamespace;

public class li_std_list_runme {
  private static readonly int collectionSize = 20;

  public static void Main() {
    // Setup a list of int
    IntList list = new IntList();
    IntList.IntListNode node;

    for (int i = 0; i < 20; i++) {
      int nb = i * 10;
      list.Add(nb);
    }

    // Count property test
    if (list.Count != collectionSize)
      throw new Exception("Count test failed");

    // IsReadOnly property test
    if (list.IsReadOnly)
      throw new Exception("IsReadOnly test failed");

    // Contains method test
    if (!list.Contains(0))
      throw new Exception("Contains method test 1 failed");
    if (!list.Contains(2 * 10))
      throw new Exception("Contains method test 2 failed");
    if (!list.Contains(19 * 10))
      throw new Exception("Contains method test 3 failed");
    if (list.Contains(20 * 10))
      throw new Exception("Contains method test 4 failed");

    // Nodes comparison method overload
    {
      IntList.IntListNode temp = new IntList.IntListNode(3);
      if (list.First == temp)
        throw new Exception("== overload method test (1) failed");
      temp = new IntList.IntListNode(0);
      if (list.First == temp)
        throw new Exception("== overload method test (2) failed");
      IntList.IntListNode temp2 = new IntList.IntListNode(0);
      if (temp == temp2)
        throw new Exception("== overload method test (3) failed");
      if (!(list.First == list.First))
        throw new Exception("== overload method test (4) failed");
      if (list.First != list.First)
        throw new Exception("!= overload method test (1) failed");
      if (!(temp != temp2))
        throw new Exception("!= overload method test (2) failed");
      if (list.First.Equals(temp))
        throw new Exception("Equals method test failed");
      if (list.First.GetHashCode() == temp.GetHashCode())
        throw new Exception("GetHashCode method test (1) failed");
      if (list.First.GetHashCode() == list.First.GetHashCode())
        throw new Exception("GetHashCode method test (2) failed");

    }

    // Getter test
    {
      if (list.First == null)
        throw new Exception("First getter test (1) failed");
      if (list.Last == null)
        throw new Exception("Last getter test (1) failed");
      if (list.Last.Next != null)
        throw new Exception("Next getter test (1) failed");
      if (list.First.Next == null)
        throw new Exception("Next getter test (2) failed");
      if (list.First.Previous != null)
        throw new Exception("Previous getter test (1) failed");
       if (list.Last.Previous == null)
        throw new Exception("Previous getter test (2) failed");
    }

    // AddFirst method test
    node = list.AddFirst(34);
    if (list.First.Value != 34 || node.Value != 34 || node != list.First)
      throw new Exception("AddFirst method test failed");
    try {
      list.AddFirst(null);
    } catch (ArgumentNullException) {
      try {
        list.AddFirst(list.First);
      } catch (InvalidOperationException) {
      }
    }

    // RemoveFirst method test
    int tmp = list.First.Value;
    list.RemoveFirst();
    if (list.First.Value == tmp || list.First.Value != 0 * 10)
      throw new Exception("RemoveFirst method test failed");

    // AddLast method test
    node = list.AddLast(8);
    if (list.Last.Value != 8 || node.Value != 8 || node != list.Last)
      throw new Exception("AddLast method test failed");
    try {
      list.AddLast(null);
    } catch (ArgumentNullException) {
      try {
        list.AddLast(list.First);
      } catch (InvalidOperationException) {
      }
    }

    // RemoveLast method test
    int tmp2 = list.Last.Value;
    list.RemoveLast();
    if (list.Last.Value == tmp2 || list.Last.Value != (list.Count - 1) * 10)
      throw new Exception("RemoveLast method test failed");

    // AddBefore method test
    node = list.AddBefore(list.Last, 17);
    if (list.Last.Previous.Value != 17 || node.Value != 17 || node != list.Last.Previous)
      throw new Exception("AddBefore method test (1) failed");
    try {
      node = null;
      list.AddBefore(list.Last, node);
      throw new Exception("AddBefore method test (2) failed");
    } catch (ArgumentNullException) {
      try {
        node = new IntList.IntListNode(1);
        list.AddBefore(null, node);
        throw new Exception("AddBefore method test (3) failed");
      } catch (ArgumentNullException) {
        try {
          list.AddBefore(list.Last, list.First);
        } catch (InvalidOperationException) {
        }
      }
    }

    // AddAfter method test
    node = list.AddAfter(list.First, 47);
    if (list.First.Next.Value != 47 || node.Value != 47 || node != list.First.Next)
      throw new Exception("AddAfter method test (1) failed");
    try {
      node = null;
      list.AddAfter(list.First.Next, node);
      throw new Exception("AddAfter method test (2) failed");
    } catch (ArgumentNullException) {
      try {
        list.AddAfter(list.First, list.Last);
      } catch (InvalidOperationException) {
      }
    }

    // Find method test
    node = list.Find(0);
    if (node == null || node.Value != 0)
      throw new Exception("Find method test (1) failed");
    node = list.Find(47);
    if (node == null || node.Value != 47)
      throw new Exception("Find method test (2) failed");
    node = list.Find(190);
    if (node == null || node.Value != 190)
      throw new Exception("Find method test (3) failed");
    node = list.Find(-3);
    if (node != null)
      throw new Exception("Find method test (4) failed");

    // Remove method test
    if (!list.Remove(17) || list.Contains(17) || list.Last.Previous.Value == 17)
      throw new Exception("Remove method test (1) failed");
    if (!list.Remove(47) || list.Contains(47) || list.First.Next.Value == 47)
      throw new Exception("Remove method test (2) failed");
    if (!list.Remove(0) || list.Contains(0) || list.First.Value == 0)
      throw new Exception("Remove method test (3) failed");
    if (!list.Remove(190) || list.Contains(190) || list.Last.Value == 190)
      throw new Exception("Remove method test (4) failed");
    try {
      node = null;
      list.Remove(node);
      throw new Exception("Remove method test (5) failed");
    } catch (ArgumentNullException) {
      try {
          node = new IntList.IntListNode(4);
          list.Remove(node);         
          throw new Exception("Remove method test (5) failed");
      } catch (InvalidOperationException) {
      }
    }

    // ICollection constructor test
    {
      int[] intArray = new int[] { 0, 11, 22, 33, 44, 55, 33 };
      IntList il = new IntList(intArray);
      if (intArray.Length != il.Count)
        throw new Exception("ICollection constructor length check failed: " + intArray.Length + "-" + il.Count);
      node = il.First;
      for (int i = 0; i < intArray.Length; i++) {
        if (intArray[i] != node.Value)
          throw new Exception("ICollection constructor failed, index:" + i);
        node = node.Next;
      }
      try {
        new IntList((System.Collections.ICollection)null);
        throw new Exception("ICollection constructor null test failed");
      } catch (ArgumentNullException) {
      }
    }

    // Enumerator test
    {
      node = list.First;
      System.Collections.IEnumerator myEnumerator = list.GetEnumerator();
      while (myEnumerator.MoveNext()) {
        if ((int)myEnumerator.Current != node.Value)
          throw new Exception("Enumerator (1) test failed");
        node = node.Next;
      }
    }
    {
      node = list.First;
      System.Collections.Generic.IEnumerator<int> myEnumerator = list.GetEnumerator();
      while (myEnumerator.MoveNext()) {
        if (myEnumerator.Current != node.Value)
          throw new Exception("Enumerator (2) test failed");
        node = node.Next;
      }
    }
    {
      node = list.First;
      IntList.IntListEnumerator myEnumerator = list.GetEnumerator();
      while (myEnumerator.MoveNext()) {
        if (myEnumerator.Current != node.Value)
          throw new Exception("Enumerator (3) test failed");
        node = node.Next;
      }
    }
    {
      node = list.First;
      foreach (var elem in list) {
        if (elem != node.Value)
          throw new Exception("Enumerator (4) test failed");
        node = node.Next;
      }
    }
    
    // CopyTo method test
    {
      int[] outputarray = new int[collectionSize - 2];
      list.CopyTo(outputarray, 0);
      int index = 0;
      IntList.IntListNode temp = list.First;
      foreach (int val in outputarray) {
        if (temp.Value != val) {
          throw new Exception("CopyTo method test (1) failed, index:" + index);
        }
        index++;
        temp = temp.Next;
      }
    }
    {
      DoubleList inputlist = new DoubleList();
      int arrayLen = 10;
      for (int i = 0; i < arrayLen; i++) {
        double num = i * 10.1;
        inputlist.Add(num);
      }
      double[] outputarray = new double[arrayLen];
      inputlist.CopyTo(outputarray, 0);
      DoubleList.DoubleListNode temp = inputlist.First;
      for (int i = 0; i < arrayLen; i++) {
        if (outputarray[i] != temp.Value)
          throw new Exception("CopyTo method test (2) failed, index:" + i);
        temp = temp.Next;
      }
    }
    {
      StructList inputlist = new StructList();
      int arrayLen = 10;
      for (int i = 0; i < arrayLen; i++)
        inputlist.Add(new Struct(i / 10.0));
      Struct[] outputarray = new Struct[arrayLen];
      inputlist.CopyTo(outputarray, 0);
      StructList.StructListNode temp = inputlist.First;
      for (int i = 0; i < arrayLen; i++) {
        if (outputarray[i].num != temp.Value.num)
          throw new Exception("CopyTo method test (3) failed, index:" + i);
        temp = temp.Next;
      }
      foreach (Struct s in inputlist) {
        s.num += 20.0;
      }
      temp = inputlist.First;
      for (int i = 0; i < arrayLen; i++) {
        if (outputarray[i].num != temp.Value.num)
          throw new Exception("CopyTo method test (4) failed, index:" + i);
        temp = temp.Next;
      }
    }
    try {
      list.CopyTo(null, 0);
      throw new Exception("CopyTo method test (5) failed");
    } catch (ArgumentNullException) {
    }
     
    // Clear() test
    list.Clear();
    if (list.Count != 0)
      throw new Exception("Clear method failed");

    // Finally test the methods being wrapped
    {
      IntList il = new IntList();
      for (int i = 0; i < 4; i++) {
        il.Add(i);
      }

      double x = li_std_list.average(il);
      x += li_std_list.average(new IntList(new int[] { 1, 2, 3, 4 }));

      DoubleList dlist = new DoubleList();
      for (int i = 0; i < 10; i++) {
        dlist.Add(i / 2.0);
      }
      li_std_list.halve_in_place(dlist);
    }

    // Dispose()
    {
      using (StructList ls = new StructList(new Struct[] { new Struct(0.0), new Struct(11.1) }))
      using (DoubleList ld = new DoubleList(new double[] { 0.0, 11.1 })) {  }
    }

    // More wrapped methods
    {
      FloatList l0 = li_std_list.listreal(new FloatList());
      float flo = 123.456f;
      l0.Add(flo);
      flo = l0.First.Value;

      IntList l1 = li_std_list.listint(new IntList());
      IntPtrList l2 = li_std_list.listintptr(new IntPtrList());
      IntConstPtrList l3 = li_std_list.listintconstptr(new IntConstPtrList());

      l1.Add(123);
      l2.Clear();
      l3.Clear();

      StructList l4 = li_std_list.liststruct(new StructList());
      StructPtrList l5 = li_std_list.liststructptr(new StructPtrList());
      StructConstPtrList l6 = li_std_list.liststructconstptr(new StructConstPtrList());

      l4.Add(new Struct(123));
      l5.Add(new Struct(123));
      l6.Add(new Struct(123));
    }

    // Test lists of pointers
    {
      StructPtrList inputlist = new StructPtrList();
      int arrayLen = 10;
      for (int i = 0; i < arrayLen; i++) {
        inputlist.Add(new Struct(i / 10.0));
      }
      Struct[] outputarray = new Struct[arrayLen];
      inputlist.CopyTo(outputarray, 0);
      StructPtrList.StructPtrListNode temp = inputlist.First;
      for (int i = 0; i < arrayLen; i++) {
        if (outputarray[i].num != temp.Value.num)
          throw new Exception("StructPtrList test (1) failed, i:" + i);
        temp = temp.Next;
      }
      foreach (Struct s in inputlist) {
        s.num += 20.0;
      }
      for (int i = 0; i < arrayLen; i++) {
        if (outputarray[i].num != 20.0 + i / 10.0)
          throw new Exception("StructPtrList test (2) failed (a deep copy was incorrectly made), i:" + i);
      }
    }

    // Test lists of const pointers
    {
      StructConstPtrList inputlist = new StructConstPtrList();
      int arrayLen = 10;
      for (int i = 0; i < arrayLen; i++) {
        inputlist.Add(new Struct(i / 10.0));
      }
      Struct[] outputarray = new Struct[arrayLen];
      inputlist.CopyTo(outputarray, 0);
      StructConstPtrList.StructConstPtrListNode temp = inputlist.First;
      for (int i = 0; i < arrayLen; i++) {
        if (outputarray[i].num != temp.Value.num)
          throw new Exception("StructConstPtrList test (1) failed, i:" + i);
        temp = temp.Next;
      }
      foreach (Struct s in inputlist) {
        s.num += 20.0;
      }
      for (int i = 0; i < arrayLen; i++) {
        if (outputarray[i].num != 20.0 + i / 10.0)
          throw new Exception("StructConstPtrList test (2) failed (a deep copy was incorrectly made), i:" + i);
      }
    }
  }
}