diff options
author | Dolphin Hawkins <dolphin@exitzer0.com> | 2016-07-09 02:47:45 +0300 |
---|---|---|
committer | Dolphin Hawkins <dolphin@exitzer0.com> | 2016-07-26 06:19:19 +0300 |
commit | 9ab4146a006f8cfbda37f89c040cc3b38d36bf71 (patch) | |
tree | 45a319c0e89f31013bda55e912923c4c0f42a737 /mcs | |
parent | ccc6d60a3373f07ed1e7a0e82ec3dbc1b5aef26b (diff) |
Added Mono.Tasklets test
Diffstat (limited to 'mcs')
-rw-r--r-- | mcs/class/Mono.Tasklets/Test/Mono.Tasklets/ContinuationsTest.cs | 346 |
1 files changed, 180 insertions, 166 deletions
diff --git a/mcs/class/Mono.Tasklets/Test/Mono.Tasklets/ContinuationsTest.cs b/mcs/class/Mono.Tasklets/Test/Mono.Tasklets/ContinuationsTest.cs index d0b2de65429..6fa67a901d5 100644 --- a/mcs/class/Mono.Tasklets/Test/Mono.Tasklets/ContinuationsTest.cs +++ b/mcs/class/Mono.Tasklets/Test/Mono.Tasklets/ContinuationsTest.cs @@ -6,170 +6,184 @@ using Mono.Tasklets; namespace MonoTests.System { [TestFixture] - public class ContinuationsTest { - - private Continuation _contA = new Continuation(); - - private int total = 0; - - [Test] - public void TestContinuationsLoop() { - _contA.Mark(); - int value = 0; - int ret = _contA.Store(0); - for(int i = ret; i < 10; i++) { - value += i; - } - - if(value > 0) { - total += value; - _contA.Restore(ret + 1); - } - - Assert.AreEqual(total,330); - } - - private int yields = 0; - - [Test] - public void Yielding() { - Continuation baseCont = new Continuation(); - Continuation taskCont = new Continuation(); - - baseCont.Mark(); - taskCont.Mark(); - - // Store the base continuation to start the task - if (baseCont.Store(0) == 0) { - bool done = false; - int count = 0; - - while (!done) { - // Do stuff for the task. - ++count; - - // This task is counting to 100. - if (count == 100) { - done = true; - } - - // Yield every 10 loops - else if (count % 10 == 0) { - - // To yield, store the task continuation then restore - // the base continuation. - if (taskCont.Store(0) == 0) { - baseCont.Restore(1); - } - } - } - } - // When restored, 'Store' will return what was passed to Restore, in this case 1 so fall here. - else { - // Count the yields, then go back to the task. - ++yields; - taskCont.Restore(1); - } - - Assert.AreEqual(9, yields); - } - - - public class MicroThread { - - public void Yield() { - if (MyThread.Store(0) == 0) { - MainThread.Restore(1); - } - } - - public void Resume() { - if (MainThread.Store(0) == 0) { - MyThread.Restore(1); - } - } - - public void DoWork(Action action) { - if (MainThread.Store(0) == 0) { - action(); - Done = true; - MainThread.Restore(1); - } - } - - public bool Done = false; - public Continuation MainThread = new Continuation(); - public Continuation MyThread = new Continuation(); - } - - public class MicroBJob { - private int _Count = 0; - public int Count { - get { return _Count; } - set { _Count = value;} - } - - public MicroThread MicroThread; - public void Work() { - while (Count < 100) { - ++Count; - if (Count % 10 == 0) { - MicroThread.Yield(); - } - } - } - } - - [Test] - public void MicroThreadTest() { - MicroThread microA = new MicroThread(); - MicroThread microB = new MicroThread(); - - microA.MainThread.Mark(); - microA.MyThread.Mark(); - microB.MainThread.Mark(); - microB.MyThread.Mark(); - - Assert.AreEqual(false,microA.Done); - Assert.AreEqual(false,microB.Done); - - microA.DoWork( () => { - int count = 0; - while (count < 100) { - ++count; - if (count % 10 == 0) { - microA.Yield(); - } - } - }); - - MicroBJob jobB = new MicroBJob(); - jobB.MicroThread = microB; - - microB.DoWork(jobB.Work); - - Assert.AreEqual(false,microA.Done); - Assert.AreEqual(false,microB.Done); - - int yields = 0; - while (yields < 20) { - if (!microA.Done) microA.Resume(); - if (!microB.Done) microB.Resume(); - if (microA.Done && microB.Done) break; - ++yields; - } - - Assert.AreEqual(true,microA.Done); - Assert.AreEqual(true,microB.Done); - Assert.AreEqual(100,jobB.Count); - Assert.AreEqual(9,yields); - } - } + public class ContinuationsTest + { + [TestFixtureSetUp] + public void FixtureSetUp () + { + try { + var temp = new Continuation (); + } catch (NotImplementedException) { + Assert.Ignore ("This platform doesn't support Tasklets."); + } + } + + int total = 0; + + [Test] + public void TestContinuationsLoop() + { + Continuation _contA = new Continuation(); + + _contA.Mark(); + int value = 0; + int ret = _contA.Store(0); + for (int i = ret; i < 10; i++) { + value += i; + } + + if (value > 0) { + total += value; + _contA.Restore(ret + 1); + } + + Assert.AreEqual(total, 330); + } + + private int yields = 0; + + [Test] + public void Yielding() + { + Continuation baseCont = new Continuation(); + Continuation taskCont = new Continuation(); + + baseCont.Mark(); + taskCont.Mark(); + + // Store the base continuation to start the task + if (baseCont.Store(0) == 0) { + bool done = false; + int count = 0; + + while (!done) { + // Do stuff for the task. + ++count; + + // This task is counting to 100. + if (count == 100) { + done = true; + } + + // Yield every 10 loops + else if (count % 10 == 0) { + + // To yield, store the task continuation then restore + // the base continuation. + if (taskCont.Store(0) == 0) { + baseCont.Restore(1); + } + } + } + } + // When restored, 'Store' will return what was passed to Restore, in this case 1 so fall here. + else { + // Count the yields, then go back to the task. + ++yields; + taskCont.Restore(1); + } + + Assert.AreEqual(9, yields); + } + + + public class MicroThread + { + + public void Yield() + { + if (MyThread.Store(0) == 0) { + MainThread.Restore(1); + } + } + + public void Resume() + { + if (MainThread.Store(0) == 0) { + MyThread.Restore(1); + } + } + + public void DoWork(Action action) + { + if (MainThread.Store(0) == 0) { + action(); + Done = true; + MainThread.Restore(1); + } + } + + public bool Done = false; + public Continuation MainThread = new Continuation(); + public Continuation MyThread = new Continuation(); + } + + public class MicroBJob + { + private int _Count = 0; + public int Count + { + get { return _Count; } + set { _Count = value; } + } + + public MicroThread MicroThread; + public void Work() + { + while (Count < 100) { + ++Count; + if (Count % 10 == 0) { + MicroThread.Yield(); + } + } + } + } + + [Test] + public void MicroThreadTest() + { + MicroThread microA = new MicroThread(); + MicroThread microB = new MicroThread(); + + microA.MainThread.Mark(); + microA.MyThread.Mark(); + microB.MainThread.Mark(); + microB.MyThread.Mark(); + + Assert.AreEqual(false, microA.Done); + Assert.AreEqual(false, microB.Done); + + microA.DoWork(() => + { + int count = 0; + while (count < 100) { + ++count; + if (count % 10 == 0) { + microA.Yield(); + } + } + }); + + MicroBJob jobB = new MicroBJob(); + jobB.MicroThread = microB; + + microB.DoWork(jobB.Work); + + Assert.AreEqual(false, microA.Done); + Assert.AreEqual(false, microB.Done); + + int yields = 0; + while (yields < 20) { + if (!microA.Done) microA.Resume(); + if (!microB.Done) microB.Resume(); + if (microA.Done && microB.Done) break; + ++yields; + } + + Assert.AreEqual(true, microA.Done); + Assert.AreEqual(true, microB.Done); + Assert.AreEqual(100, jobB.Count); + Assert.AreEqual(9, yields); + } + } } - -// vim: noexpandtab -// Local Variables: -// tab-width: 4 -// c-basic-offset: 4 -// indent-tabs-mode: t -// End: |