Tanoda
CircularObjectBufferTests.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 NUnit.Framework;
10using LeapInternal;
11
14 public int id = 0;
15 public TestObjectType() { id = -1; }
16 public TestObjectType(int t) { id = t; }
17 }
18
19 [TestFixture]
21 [Test]
22 public void CreateTest() {
24 Assert.AreEqual(100, ciq.Capacity, "Capacity is the same as initialized value");
25 Assert.AreEqual(0, ciq.Count, "Buffer starts with no items");
26 Assert.IsTrue(ciq.IsEmpty, "Buffer starts out empty");
27 }
28
29 [Test]
30 public void PutGetTest() {
32 TestObjectType bar = new TestObjectType(1);
33 ciq.Put(ref bar);
34 Assert.IsFalse(ciq.IsEmpty, "Not empty.");
35
36 for (int t = 0; t <= 12345; ++t) {
37 TestObjectType foo = new TestObjectType(t);
39 ciq.Put(ref foo);
40 ciq.Get(out mu);
41 Assert.AreEqual(t, mu.id, "Got the same value that we put.");
42 }
43 ciq.Get(out bar);
44 int currentId = bar.id;
45 for (int t = 0; t < ciq.Capacity; t++) {
46 //Console.WriteLine(t + ", " + ciq.Get (t).id + ", " + currentId);
47 TestObjectType chew = new TestObjectType(t);
48 ciq.Get(out chew, t);
49 Assert.AreEqual(chew.id, currentId, "Older objects are in order: " + chew.id + ", " + currentId);
50 currentId--;
51 }
52 }
53
54 [Test]
55 public void OutOfBoundsTests() {
57 TestObjectType foo = new TestObjectType(1);
58 for (int t = 0; t <= 12345; ++t) {
59 ciq.Get(out foo, t);
60 Assert.AreEqual(-1, foo.id, "Get default object from empty buffer");
61 }
62 TestObjectType bar = new TestObjectType(0);
63 ciq.Put(ref bar);
64 for (int t = 1; t <= 12345; ++t) {
65 ciq.Get(out foo, t);
66 Assert.AreEqual(-1, foo.id, "Get default object past last item in mostly empty buffer");
67 }
68 for (int t = 0; t <= 122; ++t) {
70 ciq.Put(ref mu);
71 }
72 for (int t = ciq.Capacity; t <= 12345; ++t) {
73 ciq.Get(out foo, t);
74 Assert.AreEqual(-1, foo.id, "Get default object past last item in full buffer");
75 }
76 }
77 [Test()]
78 public void OrderTests() {
80 Assert.AreEqual(10, ciq.Capacity, "Capacity is the same as initialized value");
81 Assert.AreEqual(0, ciq.Count, "Buffer starts with no items");
82 for (int t = 0; t < 5; ++t) {
83 TestObjectType foo = new TestObjectType(t);
84 ciq.Put(ref foo);
85 }
86
87 TestObjectType bar = new TestObjectType(0);
88 Assert.AreEqual(5, ciq.Count, "Buffer has 5 items");
89 ciq.Get(out bar, 0);
90 Assert.AreEqual(bar.id, 4, "Objects are still in order: " + bar.id + ", " + 4);
91 ciq.Get(out bar, 1);
92 Assert.AreEqual(bar.id, 3, "Objects are still in order: " + bar.id + ", " + 3);
93 ciq.Get(out bar, 2);
94 Assert.AreEqual(bar.id, 2, "Objects are still in order: " + bar.id + ", " + 2);
95 ciq.Get(out bar, 3);
96 Assert.AreEqual(bar.id, 1, "Objects are still in order: " + bar.id + ", " + 1);
97 ciq.Get(out bar, 4);
98 Assert.AreEqual(bar.id, 0, "Objects are still in order: " + bar.id + ", " + 0);
99 }
100
101 [Test()]
102 public void ResizeTests() {
104 Assert.AreEqual(10, ciq.Capacity, "Capacity is the same as initialized value");
105 Assert.AreEqual(0, ciq.Count, "Buffer starts with no items");
106 for (int t = 0; t < 5; ++t) {
107 TestObjectType foo = new TestObjectType(t);
108 ciq.Put(ref foo);
109 }
110 Assert.AreEqual(5, ciq.Count, "Buffer has 5 items");
111 ciq.Resize(15);
112 Assert.AreEqual(15, ciq.Capacity, "Capacity now is 15");
113 Assert.AreEqual(5, ciq.Count, "Buffer still has 5 items");
114 TestObjectType bar = new TestObjectType(0);
115 ciq.Get(out bar, 0);
116 Assert.AreEqual(4, bar.id, "Objects are still in order: " + bar.id + ", " + 4);
117 ciq.Get(out bar, 1);
118 Assert.AreEqual(3, bar.id, "Objects are still in order: " + bar.id + ", " + 3);
119 ciq.Get(out bar, 2);
120 Assert.AreEqual(2, bar.id, "Objects are still in order: " + bar.id + ", " + 2);
121 ciq.Get(out bar, 3);
122 Assert.AreEqual(1, bar.id, "Objects are still in order: " + bar.id + ", " + 1);
123 ciq.Get(out bar, 4);
124 Assert.AreEqual(0, bar.id, "Objects are still in order: " + bar.id + ", " + 0);
125
126 for (int t = 0; t <= 12345; ++t) {
127 TestObjectType foo = new TestObjectType(t);
128 ciq.Put(ref foo);
129 ciq.Get(out foo);
130 Assert.AreEqual(t, foo.id, "Got the same value that we put.");
131 }
133 ciq.Get(out mu);
134 int currentId = mu.id;
135 for (int t = 0; t < ciq.Capacity; t++) {
136 //Console.WriteLine(t + ", " + ciq.Get (t).id + ", " + currentId);
137 ciq.Get(out mu, t);
138 Assert.AreEqual(mu.id, currentId, "Older objects are in order: " + mu.id + ", " + currentId);
139 currentId--;
140 }
141 }
142 }
143}
void Get(out T t, int index=0)