コード例 #1
0
 private void MultiThreadedSyncedCommandifiedListAccessTest_ThreadA(CommandifiedList <string> list, WaitHandle waitHandle, Action <Exception> handler)
 {
     Console.WriteLine("Thread A started");
     try
     {
         var random = new Random((int)DateTime.Now.Ticks);
         for (int i = 0; i < 500; i++)
         {
             //Console.WriteLine("\tThread A: iteration: {0}", i);
             var countBefore = list.Count;
             var index       = random.Next(0, countBefore);
             list.RemoveAt(index);
             Assert.AreEqual(countBefore - 1, list.Count);
             CQManager.UndoLastCommand();
             Assert.AreEqual(countBefore, list.Count);
             Thread.Sleep(1);
             CQManager.RedoLastCommand();
             Assert.AreEqual(countBefore - 1, list.Count);
             Thread.Sleep(2);
             CQManager.UndoLastCommand();
             Assert.AreEqual(countBefore, list.Count);
         }
     }
     catch (Exception e)
     {
         handler(e);
     }
     finally
     {
         ((AutoResetEvent)waitHandle).Set();
     }
     Console.WriteLine("Thread A ended");
 }
コード例 #2
0
        public void MultipleUndoRedoOfSingleCommandLevelTest()
        {
            // set up our session.
            Guid sessionId = Guid.NewGuid();

            CQManager.ActivateCommandQueueStack(sessionId);

            HelperClass h = new HelperClass();

            h.Name = "Foo";
            Assert.AreEqual("Foo", h.Name);
            h.Name = "Bar";
            Assert.AreEqual("Bar", h.Name);

            CQManager.UndoLastCommand();
            Assert.AreEqual("Foo", h.Name);
            CQManager.UndoLastCommand();
            Assert.AreEqual(string.Empty, h.Name);

            // we're now back at the beginstate, with the exception that the queue is currently filled with undone commands.
            // we'll now re-do these commands
            CQManager.RedoLastCommand();
            Assert.AreEqual("Foo", h.Name);
            CQManager.RedoLastCommand();
            Assert.AreEqual("Bar", h.Name);

            // now back to the start.
            CQManager.UndoLastCommand();
            Assert.AreEqual("Foo", h.Name);
            CQManager.UndoLastCommand();
            Assert.AreEqual(string.Empty, h.Name);

            CQManager.ActivateCommandQueueStack(Guid.Empty);
        }
コード例 #3
0
 private void MultiThreadedSyncedGraphAccessTest_ThreadA(DirectedGraph <int, DirectedEdge <int> > graph, WaitHandle waitHandle, Action <Exception> handler)
 {
     Console.WriteLine("Thread A started");
     try
     {
         for (int i = 0; i < 100; i++)
         {
             var vertexCountBefore = graph.VertexCount;
             var index             = _random.Next(0, vertexCountBefore);
             graph.Remove(index);
             Assert.AreEqual(vertexCountBefore - 1, graph.VertexCount);
             CQManager.UndoLastCommand();
             Assert.AreEqual(vertexCountBefore, graph.VertexCount);
             Thread.Sleep(1);
             CQManager.RedoLastCommand();
             Assert.AreEqual(vertexCountBefore - 1, graph.VertexCount);
             Thread.Sleep(2);
             CQManager.UndoLastCommand();
             Assert.AreEqual(vertexCountBefore, graph.VertexCount);
         }
     }
     catch (Exception e)
     {
         handler(e);
     }
     finally
     {
         ((AutoResetEvent)waitHandle).Set();
     }
     Console.WriteLine("Thread A ended");
 }
コード例 #4
0
        public void MultiUndoRedoOfMultiCommandLevelWithBeforeAfterActionCallsTest()
        {
            // set up our session.
            Guid sessionId = Guid.NewGuid();

            CQManager.ActivateCommandQueueStack(sessionId);

            HelperClass h = new HelperClass();

            int beforeDoCounter   = 0;
            int afterDoCounter    = 0;
            int beforeUndoCounter = 0;
            int afterUndoCounter  = 0;

            // set the property through a command. As the property name change also spawns a command, it will create a 2-level command set.
            // it doesn't use an undo function, as it doesn't change state itself, that's delegated to another command. Typically one wouldn't do it this way,
            // as this would assume knowledge of how e.Name's setter works, however for the test it's ok.
            Command <string> nameSetter = new Command <string>(() => h.Name = "Foo")
            {
                AfterDoAction    = () => afterDoCounter++,
                AfterUndoAction  = () => afterUndoCounter++,
                BeforeDoAction   = () => beforeDoCounter++,
                BeforeUndoAction = () => beforeUndoCounter++
            };

            CQManager.EnqueueAndRunCommand(nameSetter);
            Assert.AreEqual("Foo", h.Name);
            CQManager.UndoLastCommand();
            Assert.AreEqual(string.Empty, h.Name);

            CQManager.RedoLastCommand();
            CQManager.UndoLastCommand();
            CQManager.RedoLastCommand();
            CQManager.UndoLastCommand();
            // we called do 3 times, and undo also 3 times
            Assert.AreEqual(3, beforeDoCounter);
            Assert.AreEqual(3, beforeUndoCounter);
            Assert.AreEqual(3, afterDoCounter);
            Assert.AreEqual(3, afterUndoCounter);

            CQManager.ActivateCommandQueueStack(Guid.Empty);
        }
コード例 #5
0
        public void CommandifiedGraphAddRemoveGraphAndEdgesWithUndoTest()
        {
            Guid sessionId = Guid.NewGuid();

            CQManager.ActivateCommandQueueStack(sessionId);

            DirectedGraph <int, DirectedEdge <int> > graph = new DirectedGraph <int, DirectedEdge <int> >(true);

            graph.Add(42);
            graph.Add(13);
            graph.Add(10);
            graph.Add(new DirectedEdge <int>(42, 13));                  // 42 -> 13
            graph.Add(new DirectedEdge <int>(42, 10));                  // 42 -> 10
            Assert.IsTrue(graph.Contains(42));
            Assert.IsTrue(graph.Contains(13));
            Assert.IsTrue(graph.Contains(10));
            Assert.AreEqual(2, graph.EdgeCount);
            Assert.IsTrue(graph.ContainsEdge(42, 13));
            Assert.IsTrue(graph.ContainsEdge(42, 10));

            // create graph to add to this graph. Doesn't have to be a commandified graph, as we're not rolling back the actions on that graph.
            DirectedGraph <int, DirectedEdge <int> > graphToAdd = new DirectedGraph <int, DirectedEdge <int> >();

            graphToAdd.Add(1);
            graphToAdd.Add(2);
            graphToAdd.Add(3);
            graphToAdd.Add(new DirectedEdge <int>(1, 2));               // 1 -> 2
            graphToAdd.Add(new DirectedEdge <int>(1, 3));               // 1 -> 3
            graphToAdd.Add(new DirectedEdge <int>(2, 3));               // 2 -> 3
            Assert.AreEqual(3, graphToAdd.VertexCount);
            Assert.AreEqual(3, graphToAdd.EdgeCount);

            // add this graph to the main graph. This is an undoable action.
            graph.Add(graphToAdd);
            Assert.AreEqual(6, graph.VertexCount);
            Assert.AreEqual(5, graph.EdgeCount);
            Assert.IsTrue(graph.Contains(1));
            Assert.IsTrue(graph.Contains(2));
            Assert.IsTrue(graph.Contains(3));
            Assert.IsTrue(graph.ContainsEdge(1, 2));
            Assert.IsTrue(graph.ContainsEdge(1, 3));
            Assert.IsTrue(graph.ContainsEdge(2, 3));

            // undo add
            CQManager.UndoLastCommand();
            Assert.AreEqual(3, graph.VertexCount);
            Assert.AreEqual(2, graph.EdgeCount);
            Assert.IsFalse(graph.Contains(1));
            Assert.IsFalse(graph.Contains(2));
            Assert.IsFalse(graph.Contains(3));
            Assert.IsFalse(graph.ContainsEdge(1, 2));
            Assert.IsFalse(graph.ContainsEdge(1, 3));
            Assert.IsFalse(graph.ContainsEdge(2, 3));

            // redo
            CQManager.RedoLastCommand();
            Assert.AreEqual(6, graph.VertexCount);
            Assert.AreEqual(5, graph.EdgeCount);
            Assert.IsTrue(graph.Contains(1));
            Assert.IsTrue(graph.Contains(2));
            Assert.IsTrue(graph.Contains(3));
            Assert.IsTrue(graph.ContainsEdge(1, 2));
            Assert.IsTrue(graph.ContainsEdge(1, 3));
            Assert.IsTrue(graph.ContainsEdge(2, 3));

            // remove the graph we added
            graph.Remove(graphToAdd);
            Assert.AreEqual(3, graph.VertexCount);
            Assert.AreEqual(2, graph.EdgeCount);
            Assert.IsFalse(graph.Contains(1));
            Assert.IsFalse(graph.Contains(2));
            Assert.IsFalse(graph.Contains(3));
            Assert.IsFalse(graph.ContainsEdge(1, 2));
            Assert.IsFalse(graph.ContainsEdge(1, 3));
            Assert.IsFalse(graph.ContainsEdge(2, 3));

            CQManager.UndoLastCommand();
            Assert.AreEqual(6, graph.VertexCount);
            Assert.AreEqual(5, graph.EdgeCount);
            Assert.IsTrue(graph.Contains(1));
            Assert.IsTrue(graph.Contains(2));
            Assert.IsTrue(graph.Contains(3));
            Assert.IsTrue(graph.ContainsEdge(1, 2));
            Assert.IsTrue(graph.ContainsEdge(1, 3));
            Assert.IsTrue(graph.ContainsEdge(2, 3));

            DirectedEdge <int> newEdge = new DirectedEdge <int>(42, 1);                         // 42 -> 1

            graph.Add(newEdge);
            Assert.AreEqual(6, graph.EdgeCount);
            Assert.IsTrue(graph.ContainsEdge(42, 1));
            Assert.IsFalse(graph.ContainsEdge(1, 42));

            CQManager.UndoLastCommand();
            Assert.AreEqual(5, graph.EdgeCount);
            Assert.IsFalse(graph.ContainsEdge(42, 1));

            CQManager.RedoLastCommand();
            Assert.AreEqual(6, graph.EdgeCount);
            Assert.IsTrue(graph.ContainsEdge(42, 1));

            graph.Remove(newEdge);
            Assert.AreEqual(5, graph.EdgeCount);
            Assert.IsFalse(graph.ContainsEdge(42, 1));

            CQManager.UndoLastCommand();
            Assert.AreEqual(6, graph.EdgeCount);
            Assert.IsTrue(graph.ContainsEdge(42, 1));

            graph.Disconnect(42, 1, false);
            Assert.AreEqual(5, graph.EdgeCount);
            Assert.IsFalse(graph.ContainsEdge(42, 1));

            CQManager.UndoLastCommand();
            Assert.AreEqual(6, graph.EdgeCount);
            Assert.IsTrue(graph.ContainsEdge(42, 1));

            CQManager.ActivateCommandQueueStack(Guid.Empty);
        }