using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using UnboundedLockFreeQueue;
namespace ChessTestUnboundedLockFreeQueue
{
///
/// Summary description for UnitTest1
///
[TestClass]
public class UnitTest1
{
public UnitTest1()
{
}
private TestContext testContextInstance;
///
///Gets or sets the test context which provides
///information about and functionality for the current test run.
///
public TestContext TestContext
{
get
{
return testContextInstance;
}
set
{
testContextInstance = value;
}
}
#region Additional test attributes
//
// You can use the following additional attributes as you write your tests:
//
// Use ClassInitialize to run code before running the first test in the class
// [ClassInitialize()]
// public static void MyClassInitialize(TestContext testContext) { }
//
// Use ClassCleanup to run code after all tests in a class have run
// [ClassCleanup()]
// public static void MyClassCleanup() { }
//
// Use TestInitialize to run code before running each test
// [TestInitialize()]
// public void MyTestInitialize() { }
//
// Use TestCleanup to run code after each test has run
// [TestCleanup()]
// public void MyTestCleanup() { }
//
#endregion
///
/// One producer thread, one consumer thread. This method is mainly to see that basic queue functionality is working.
///
[TestMethod]
[HostType("Chess")]
public void TestMethod1()
{
ParallelTasks tasks = new ParallelTasks();
UnboundedQueue queue = new UnboundedQueue();
// Add these items to the queue
List addItems = new List();
tasks.Add("Producer", () =>
{
for (int i = 0; i < 6; i++)
{
addItems.Add(i);
queue.Enq(i);
}
});
tasks.Execute();
tasks.Clear();
// Remove the items from the queue
List removeItems = new List();
tasks.Add("Consumer", () =>
{
for (int i = 0; i < 6; i++)
{
int val = (int)queue.Deq();
// Make sure this item wasn't already removed
Assert.IsTrue(!removeItems.Contains(val));
removeItems.Add(val);
// Make sure this item was originally added
Assert.IsTrue(addItems.Contains(val));
}
});
tasks.Execute();
Assert.IsTrue(queue.IsEmpty);
}
///
/// One producer thread, two consumer threads.
///
[TestMethod]
[HostType("Chess")]
[TestProperty("ChessDebug", "true")]
[TestProperty("ChessMonitorVolatiles", "true")]
public void TestMethod2()
{
ParallelTasks tasks = new ParallelTasks();
UnboundedQueue queue = new UnboundedQueue();
// Add items to the queue
List addItems = new List();
tasks.Add("Producer", () =>
{
for (int i = 0; i < 6; i++)
{
addItems.Add(i);
queue.Enq(i);
}
});
tasks.Execute();
tasks.Clear();
// Start up two consumer threads
List removeItems = new List();
tasks.Add("Consumer", () =>
{
for (int i = 0; i < 3; i++)
{
int val = (int)queue.Deq();
// See that this item hasn't already been removed
Assert.IsTrue(!removeItems.Contains(val));
removeItems.Add(val);
// Make sure this item was originally there
Assert.IsTrue(addItems.Contains(val));
}
});
tasks.Add("Consumer", () =>
{
for (int i = 0; i < 3; i++)
{
int val = (int)queue.Deq();
// See that this item hasn't already been removed
Assert.IsTrue(!removeItems.Contains(val));
removeItems.Add(val);
// Make sure this item was originally there
Assert.IsTrue(addItems.Contains(val));
}
});
tasks.Execute();
// Make sure all items get removed from the queue
Assert.IsTrue(queue.IsEmpty);
}
///
/// One producer thread, two consumer threads running in parallel.
///
[TestMethod]
[HostType("Chess")]
[TestProperty("ChessDebug", "true")]
[TestProperty("ChessMonitorVolatiles", "true")]
[TestProperty("ChessMode", "Repro")]
[TestProperty("ChessBreak", "BeforePreemption")]
#region ChessScheduleString (not human readable)
[TestProperty("ChessScheduleString", @"bpilaiaaaaaaaaaaaeaaonlnahgabmejjgcfcgcpgnmkhlhpekpfeknhoahekbaiiagabdcenijaeabaommbiimnogjcombngjeh
cdcjklckibmkgffggffnggbgeammonjnlmphnohloplnphnohloplnphlkdljneochphnpppdpfmgggeabgmpgmoeknkmjjocbia
kkmibpdphohmbpdpcchomnfpodnhpidfpappdpfdhpponplpkgpmdelpppbgpkplkppbglpnbklpmggpkflpppjknooplpjgphpl
kponpnpoolhilppplkhohlpmgnnkhllpppdknoopgipdgdfoblpdplkpkdlpppfkpkppfpmppmfepipipnnhphhoahaolpkfpopo
blpmfkoolnfplhpdpjkpkdlpppbknoohdpoglfppdfdljpjlpgdpblpaangnlpeofpigmonnhobdoppdnpnaplpndgdahndilpjo
phpjgpoelfpppfdljplpapmoopalnpjpnpphongliidolplbphpjgpocpnhobljidojmmeplpmdhpfnkppigjnmpnpahohphifop
mpopplpgdfeejpnpmmplpmdhphholpanembpmogpomhnpoflhionhpjdmoohopidpdplmcphohphhnplbkkcmpgpojhnpoflhjlp
npagkgapohdhpddoppknlnpglphjohpdphmbpjhnobplpdpllohnannbohlhpbdoppgnlnnpgpadndihpdflhkjppphgfoplnpkk
pdpjploabmidppglolpphpdnpnmjpgephppkdgnbpmnpdmogphnpmemaegplppahbffnpiacgbahaaaa")]
#endregion
public void TestMethod3()
{
ParallelTasks tasks = new ParallelTasks();
UnboundedQueue queue = new UnboundedQueue();
// Add items to the queue
AtomicList addItems = new AtomicList();
//List addItems = new List();
tasks.Add("Producer", () =>
{
int temp;
for (int i = 0; i < 4; i++)
{
addItems.Add(i);
queue.Enq(i);
}
temp = 0;
});
// Start up two consumer threads
AtomicList removeItems = new AtomicList();
//List removeItems = new List();
tasks.Add("Consumer1", () =>
{
int i = 0;
while (i < 2)
{
int val;
try
{
Assert.IsTrue(!queue.IsEmpty);
val = (int)queue.Deq();
}
catch (EmptyException ex)
{
// If the queue was empty, try again
continue;
}
// Otherwise, an item was found, remove it
i++;
// See that this item hasn't already been removed
Assert.IsTrue(!removeItems.Contains(val));
removeItems.Add(val);
// Make sure this item was originally there
Assert.IsTrue(addItems.Contains(val));
}
});
tasks.Add("Consumer2", () =>
{
int i = 0;
while (i < 2)
{
int val;
try
{
Assert.IsTrue(!queue.IsEmpty); // ASSERT ERROR
val = (int)queue.Deq();
}
catch (EmptyException ex)
{
// If the queue was empty, try again
continue;
}
// Otherwise, an item was found, remove it
i++;
// See that this item hasn't already been removed
Assert.IsTrue(!removeItems.Contains(val));
removeItems.Add(val);
// Make sure this item was originally there
Assert.IsTrue(addItems.Contains(val));
}
});
tasks.Execute();
// Make sure all items get removed from the queue
Assert.IsTrue(queue.IsEmpty);
}
}
}