Tanoda
MultiTypedListTests.cs
Go to the documentation of this file.
1/******************************************************************************
2 * Copyright (C) Ultraleap, Inc. 2011-2020. *
3 * *
4 * Use subject to the terms of the Apache License 2.0 available at *
5 * http://www.apache.org/licenses/LICENSE-2.0, or another agreement *
6 * between Ultraleap and you, your company or other organization. *
7 ******************************************************************************/
8
9using System.Collections.Generic;
10using NUnit.Framework;
11
12namespace Leap.Unity.Tests {
13
14 public class MultiTypedListTests {
15
16 public class BaseClass { }
17 public class A : BaseClass { }
18 public class B : BaseClass { }
19 public class C : BaseClass { }
20 public class D : BaseClass { }
21 public class E : BaseClass { }
22 public class F : BaseClass { }
23 public class G : BaseClass { }
24 public class H : BaseClass { }
25
26 public class InvalidClass : BaseClass { }
27
28 private class ListClass : MultiTypedList<BaseClass, A, B, C, D, E, F, G, H> { }
29
30 private ListClass _list;
31
32 [SetUp]
33 public void Setup() {
34 _list = new ListClass();
35 }
36
37 [TearDown]
38 public void Teardown() {
39 _list = null;
40 }
41
42 [Test]
43 public void InsertTests() {
44 addOneOfEach();
45
46 int countBefore = _list.Count;
47
48 _list.Insert(0, new A());
49
50 int countAfter = _list.Count;
51
52 Assert.That(countAfter, Is.EqualTo(countBefore + 1));
53
54 Assert.That(_list[0], Is.TypeOf<A>());
55 Assert.That(_list[1], Is.TypeOf<A>());
56 Assert.That(_list[2], Is.TypeOf<B>());
57 }
58
59 [Test]
60 public void EnumerableTest() {
61 addOneOfEach();
62
63 List<BaseClass> objs = new List<BaseClass>();
64 foreach (var obj in _list) {
65 objs.Add(obj);
66 }
67
68 Assert.That(objs[0], Is.TypeOf<A>());
69 Assert.That(objs[1], Is.TypeOf<B>());
70 Assert.That(objs[2], Is.TypeOf<C>());
71 Assert.That(objs[3], Is.TypeOf<D>());
72 Assert.That(objs[4], Is.TypeOf<E>());
73 Assert.That(objs[5], Is.TypeOf<F>());
74 Assert.That(objs[6], Is.TypeOf<G>());
75 Assert.That(objs[7], Is.TypeOf<H>());
76 }
77
78 [Test]
79 public void RemoveAtTest1() {
80 addOneOfEach();
81 Assert.That(_list.Count, Is.EqualTo(8));
82
83 for (int i = 0; i < 8; i++) {
84 _list.RemoveAt(0);
85 }
86
87 Assert.That(_list.Count, Is.EqualTo(0));
88 }
89
90 [Test]
91 public void RemoveAtTest2() {
92 addOneOfEach();
93 Assert.That(_list.Count, Is.EqualTo(8));
94
95 for (int i = 8; i-- != 0;) {
96 _list.RemoveAt(i);
97 }
98
99 Assert.That(_list.Count, Is.EqualTo(0));
100 }
101
102 [Test]
103 public void IndexTest1() {
104 addOneOfEach();
105
106 Assert.That(_list[0], Is.TypeOf<A>());
107 Assert.That(_list[1], Is.TypeOf<B>());
108 Assert.That(_list[2], Is.TypeOf<C>());
109 Assert.That(_list[3], Is.TypeOf<D>());
110 Assert.That(_list[4], Is.TypeOf<E>());
111 Assert.That(_list[5], Is.TypeOf<F>());
112 Assert.That(_list[6], Is.TypeOf<G>());
113 Assert.That(_list[7], Is.TypeOf<H>());
114 }
115
116 [Test]
117 public void IndexTest2() {
118 addOneOfEach();
119
120 _list.RemoveAt(0);
121 _list.RemoveAt(0);
122 _list.RemoveAt(0);
123 _list.RemoveAt(0);
124
125 Assert.That(_list[0], Is.TypeOf<E>());
126 Assert.That(_list[1], Is.TypeOf<F>());
127 Assert.That(_list[2], Is.TypeOf<G>());
128 Assert.That(_list[3], Is.TypeOf<H>());
129 }
130
131 [Test]
132 public void AssignTest() {
133 addOneOfEach();
134
135 A a = new A();
136 _list[5] = a;
137
138 Assert.That(_list[0], Is.TypeOf<A>());
139 Assert.That(_list[1], Is.TypeOf<B>());
140 Assert.That(_list[2], Is.TypeOf<C>());
141 Assert.That(_list[3], Is.TypeOf<D>());
142 Assert.That(_list[4], Is.TypeOf<E>());
143 Assert.That(_list[5], Is.EqualTo(a));
144 Assert.That(_list[6], Is.TypeOf<G>());
145 Assert.That(_list[7], Is.TypeOf<H>());
146 }
147
148 [Test]
149 public void AddInvalidObjectTest() {
150 Assert.That(() => {
151 _list.Add(new InvalidClass());
152 }, Throws.ArgumentException);
153 }
154
155 private void addOneOfEach() {
156 _list.Add(new A());
157 _list.Add(new B());
158 _list.Add(new C());
159 _list.Add(new D());
160 _list.Add(new E());
161 _list.Add(new F());
162 _list.Add(new G());
163 _list.Add(new H());
164 }
165
166 }
167}
Represents an ordered collection of objects of type BaseType.