Implemented Treap and Treap tests.

This commit is contained in:
Alex Macocian
2019-04-22 14:56:03 +02:00
parent afdfeda826
commit 95f2ab709f
12 changed files with 706 additions and 106 deletions
@@ -35,17 +35,6 @@ namespace SystemExtensions.Collections.Tests
{
fibonacciHeap.Insert(i);
}
if (fibonacciHeap.RemoveMinimum() != 0)
{
Assert.Fail();
}
for (int i = 1; i < 1000; i++)
{
if (!fibonacciHeap.Contains(i))
{
Assert.Fail();
}
}
}
[TestMethod()]
@@ -0,0 +1,106 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using SystemExtensions.Collections;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace SystemExtensions.Collections.Tests
{
[TestClass()]
public class TreapTests
{
private static int IntegerComparison(int x, int y)
{
if (x == y)
{
return 0;
}
else if (x > y)
{
return 1;
}
else
{
return -1;
}
}
Treap<int> treap = new Treap<int>(new Comparison<int>(IntegerComparison));
[TestMethod()]
public void TreapTest()
{
treap = new Treap<int>(new Comparison<int>(IntegerComparison));
}
[TestMethod()]
public void InsertTest()
{
Random random = new Random();
for(int i = 0; i < 1000; i++)
{
treap.Insert(random.Next(0, 5000));
}
}
[TestMethod()]
public void RemoveTest()
{
treap.Insert(60);
treap.Insert(6);
treap.Insert(5);
treap.Remove(60);
if(treap.Contains(60) || treap.Count > 2)
{
Assert.Fail();
}
}
[TestMethod()]
public void ClearTest()
{
Random random = new Random();
for (int i = 0; i < 100; i++)
{
treap.Insert(random.Next(0, 5000));
}
treap.Clear();
if(treap.Count > 0)
{
Assert.Fail();
}
}
[TestMethod()]
public void ContainsTest()
{
treap.Insert(50);
treap.Insert(25);
treap.Insert(991142);
treap.Insert(12313);
treap.Insert(24);
treap.Insert(23);
if (!treap.Contains(24))
{
Assert.Fail();
}
}
[TestMethod()]
public void ToArrayTest()
{
for(int i = 0; i < 1000; i++)
{
treap.Insert(i);
}
int[] arr = treap.ToArray();
for(int i = 0; i < 1000; i++)
{
if(arr[i] != i)
{
Assert.Fail();
}
}
}
}
}
@@ -59,6 +59,7 @@
<Compile Include="Collections\BinaryHeapTests.cs" />
<Compile Include="Collections\FibonacciHeapTests.cs" />
<Compile Include="Collections\PriorityQueueTests.cs" />
<Compile Include="Collections\TreapTests.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Threading\PriorityThreadPoolTests.cs" />
</ItemGroup>
@@ -22,7 +22,7 @@ namespace SystemExtensions.Threading.Tests
{
threadPool.Dispose();
threadPool = new PriorityThreadPool();
if (threadPool.NumberOfThreads != System.Environment.ProcessorCount / 4)
if (threadPool.NumberOfThreads != System.Environment.ProcessorCount)
{
Assert.Fail();
}
@@ -33,7 +33,7 @@ namespace SystemExtensions.Threading.Tests
{
threadPool.Dispose();
threadPool = new PriorityThreadPool(4);
if (threadPool.NumberOfThreads != 4 / 4)
if (threadPool.NumberOfThreads != 4)
{
Assert.Fail();
}
@@ -102,7 +102,7 @@ namespace SystemExtensions.Threading.Tests
{
threadPool.Dispose();
threadPool = new PriorityThreadPool(4);
if (threadPool.NumberOfThreads != 4 / 4)
if (threadPool.NumberOfThreads != 4)
{
Assert.Fail();
}
@@ -116,13 +116,6 @@ namespace SystemExtensions.Threading.Tests
}
}, null);
}
for (int i = 0; i < 10; i++)
{
System.Diagnostics.Debug.WriteLine("=====================================");
System.Diagnostics.Debug.WriteLine("Current threads in threadpool: " + threadPool.NumberOfThreads);
System.Diagnostics.Debug.WriteLine("=====================================");
Thread.Sleep(100);
}
while (threadPool.NumberOfThreads != 1)
{
System.Diagnostics.Debug.WriteLine("=====================================");
@@ -203,13 +196,6 @@ namespace SystemExtensions.Threading.Tests
System.Diagnostics.Debug.WriteLine(Thread.CurrentThread.ManagedThreadId + " - " + taskPriority);
}, null, taskPriority);
}
for (int i = 0; i < 10; i++)
{
System.Diagnostics.Debug.WriteLine("=====================================");
System.Diagnostics.Debug.WriteLine("Current threads in threadpool: " + threadPool.NumberOfThreads);
System.Diagnostics.Debug.WriteLine("=====================================");
Thread.Sleep(100);
}
while (threadPool.NumberOfThreads != System.Environment.ProcessorCount / 4)
{
System.Diagnostics.Debug.WriteLine("=====================================");