Skip to content
This repository was archived by the owner on Dec 22, 2019. It is now read-only.

Commit 51195d2

Browse files
committed
Added More Unit tests
1 parent 3293b1e commit 51195d2

5 files changed

Lines changed: 172 additions & 1 deletion

File tree

UpdateLib/UpdateLib.Tests/Tasks/AsyncTaskTest.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public void FaultyTaskReturnsException()
4242
{
4343
Assert.False(e.Cancelled, "The task got cancelled");
4444
Assert.NotNull(e.Error, "The error object is null");
45-
Assert.IsInstanceOf<ErrorTask>(e.Error, $"{e.Error} is not an instance of {nameof(AsyncTaskTestException)}");
45+
Assert.IsInstanceOf<AsyncTaskTestException>(e.Error, $"{e.Error} is not an instance of {nameof(AsyncTaskTestException)}");
4646
// wait.Set();
4747
};
4848
task.Start();
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
using MatthiWare.UpdateLib.Threading;
2+
using NUnit.Framework;
3+
using System;
4+
using System.Collections.Generic;
5+
using System.Linq;
6+
using System.Text;
7+
using System.Threading;
8+
using System.Threading.Tasks;
9+
10+
namespace UpdateLib.Tests.Threading
11+
{
12+
[TestFixture]
13+
public class AtomicIntegerTest
14+
{
15+
16+
17+
[Test, Parallelizable]
18+
public void AtomicIntegerShouldGetCorrectValueFromConstructor()
19+
{
20+
AtomicInteger myInt = new AtomicInteger(50);
21+
Assert.AreEqual(50, myInt.Value);
22+
}
23+
24+
[Test, Parallelizable]
25+
public void AtomicIntegerShouldIncrementCorrectly()
26+
{
27+
AtomicInteger myInt = new AtomicInteger();
28+
Assert.AreEqual(1, myInt.Increment());
29+
}
30+
31+
[Test, Parallelizable]
32+
public void AtomicIntegerShouldDecrementCorrectly()
33+
{
34+
AtomicInteger myInt = new AtomicInteger();
35+
Assert.AreEqual(-1, myInt.Decrement());
36+
}
37+
38+
[Test, Parallelizable]
39+
public void AtomicIntegerShouldHaveCorrectValueAfterSetting()
40+
{
41+
AtomicInteger myInt = new AtomicInteger(50);
42+
Assert.AreEqual(50, myInt.Value);
43+
myInt.Value = 30;
44+
Assert.AreEqual(30, myInt.Value);
45+
}
46+
47+
[Test, Parallelizable]
48+
public void MultithreadedTest()
49+
{
50+
AtomicInteger myInt = new AtomicInteger(50);
51+
52+
ThreadStart IncrementAction = new ThreadStart(() => myInt.Increment());
53+
54+
ThreadStart DecrementAction = new ThreadStart(() => myInt.Decrement());
55+
56+
Thread[] threads = new Thread[10];
57+
58+
for (int i = 0; i < 10; i++)
59+
{
60+
threads[i] = new Thread((i % 2 == 0) ? IncrementAction : DecrementAction);
61+
threads[i].Start();
62+
}
63+
64+
for (int i = 0; i < 10; i++)
65+
{
66+
threads[i].Join();
67+
}
68+
69+
Assert.AreEqual(50, myInt.Value);
70+
}
71+
72+
[Test, Parallelizable]
73+
public void ToStringShouldDisplayValue()
74+
{
75+
AtomicInteger myInt = new AtomicInteger(30);
76+
Assert.AreEqual(myInt.Value.ToString(), myInt.ToString());
77+
}
78+
}
79+
}
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
using MatthiWare.UpdateLib.Threading;
2+
using NUnit.Framework;
3+
using System;
4+
using System.Collections.Generic;
5+
using System.Linq;
6+
using System.Text;
7+
8+
namespace UpdateLib.Tests.Threading
9+
{
10+
[TestFixture]
11+
public class ConcurrentQueueTests
12+
{
13+
private ConcurrentQueue<object> m_queue;
14+
15+
[SetUp]
16+
public void Setup()
17+
{
18+
m_queue = new ConcurrentQueue<object>();
19+
}
20+
21+
[Test, Parallelizable]
22+
public void CountShouldBeZero()
23+
{
24+
Assert.AreEqual(0, m_queue.Count);
25+
}
26+
27+
[Test, Parallelizable]
28+
public void AddingItemsShouldIncrementCount()
29+
{
30+
m_queue.Enqueue(new object());
31+
Assert.AreEqual(1, m_queue.Count);
32+
m_queue.Enqueue(new object());
33+
Assert.AreEqual(2, m_queue.Count);
34+
m_queue.Enqueue(new object());
35+
Assert.AreEqual(3, m_queue.Count);
36+
}
37+
38+
[Test, Parallelizable]
39+
public void DequeueShouldDecrementCount()
40+
{
41+
object data = null;
42+
43+
m_queue.Enqueue(new object());
44+
Assert.AreEqual(1, m_queue.Count);
45+
m_queue.Enqueue(new object());
46+
Assert.AreEqual(2, m_queue.Count);
47+
m_queue.Enqueue(new object());
48+
Assert.AreEqual(3, m_queue.Count);
49+
50+
Assert.IsTrue(m_queue.TryDequeue(out data));
51+
Assert.AreEqual(2, m_queue.Count);
52+
Assert.IsTrue(m_queue.TryDequeue(out data));
53+
Assert.AreEqual(1, m_queue.Count);
54+
Assert.IsTrue(m_queue.TryDequeue(out data));
55+
Assert.AreEqual(0, m_queue.Count);
56+
}
57+
58+
[Test, Parallelizable]
59+
public void ClearShouldEmptyTheQueue()
60+
{
61+
m_queue.Enqueue(new object());
62+
Assert.AreEqual(1, m_queue.Count);
63+
m_queue.Enqueue(new object());
64+
Assert.AreEqual(2, m_queue.Count);
65+
m_queue.Enqueue(new object());
66+
Assert.AreEqual(3, m_queue.Count);
67+
68+
m_queue.Clear();
69+
70+
Assert.AreEqual(0, m_queue.Count);
71+
}
72+
73+
[TearDown]
74+
public void CleanUp()
75+
{
76+
m_queue.Clear();
77+
m_queue = null;
78+
}
79+
80+
}
81+
}

UpdateLib/UpdateLib.Tests/UpdateLib.Tests.csproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,8 @@
6363
<Compile Include="Properties\AssemblyInfo.cs" />
6464
<Compile Include="Security\HashUtilTest.cs" />
6565
<Compile Include="Tasks\AsyncTaskTest.cs" />
66+
<Compile Include="Threading\AtomicIntegerTest.cs" />
67+
<Compile Include="Threading\ConcurrentQueueTests.cs" />
6668
<Compile Include="UI\WizardPageCollectionTest.cs" />
6769
</ItemGroup>
6870
<ItemGroup>

UpdateLib/UpdateLib/Threading/ConcurrentQueue.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,15 @@ public void Enqueue(T item)
2121
m_queue.Enqueue(item);
2222
}
2323

24+
public int Count
25+
{
26+
get
27+
{
28+
lock (sync)
29+
return m_queue.Count;
30+
}
31+
}
32+
2433
public bool TryDequeue(out T data)
2534
{
2635
data = default(T);

0 commit comments

Comments
 (0)