📄 nunittest.cs
字号:
/* Copyright 2006 Christian Gross http://www.devspace.com From the book Ajax Patterns and Best Practices Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.*/using System;using NUnit.Framework;using System.Collections.Generic;using TaskManager;using PrimeNumberCalculator;using System.Threading;using System.IO;using System.Text;namespace Tests { [TestFixture] public class TestCalculatorImplementation { private class TaskManagerStub : ITaskManager { public Queue< IResult> Results = new Queue< IResult>(); public void AddResult(IResult result) { Results.Enqueue( result); } } [SetUp] public void SetUp() { // TODO add your initialization code } [Test] [ExpectedException( typeof( IndexOutOfRangeException))] public void TestIfCorrectNumber() { Calculator calculator = new Calculator( 0, 100); } [Test] public void TestIsIfPrime() { Calculator calculator = new Calculator( 1, 100); Assert.IsTrue( calculator.IsPrime( 7)); Assert.IsTrue( calculator.IsPrime( 17)); Assert.IsTrue( calculator.IsPrime( 19)); Assert.IsFalse( calculator.IsPrime( 21)); Assert.IsFalse( calculator.IsPrime( 27)); } [Test] public void TestMethod() { TaskManagerStub stub = new TaskManagerStub(); Calculator calculator = new Calculator( 20, 100); calculator.Execute( stub); foreach( PrimeNumberData number in stub.Results) { Console.WriteLine( "Found value (" + number.Number + ")"); } Assert.AreEqual( 8, stub.Results.Count); } [Test] public void TestThreaded() { TaskManagerImpl mgr = new TaskManagerImpl(); mgr.AddTask( new Calculator( 10000, 100)); mgr.RunThreadedTasks(); while( mgr.GetCompletedTask() == null) { Console.WriteLine( "Sleeping"); Thread.Sleep( 1000); } IResult result = null; while((result = mgr.GetResult()) != null) { PrimeNumberData number = (PrimeNumberData)result; Console.WriteLine( "Found value (" + number.Number + ")"); } } [Test] public void TestReadResults() { TaskManagerImpl taskManager = new TaskManagerImpl(); IResult result = taskManager.GetResultWait( 10); Assert.IsNull( result); } } [TestFixture] public class TestSerialization { [Test] public void TestRead() { string buffer = @"<Action> <TransactionIdentifier>1</TransactionIdentifier> <Number>21</Number> </Action>"; MemoryStream stream = new MemoryStream( Encoding.ASCII.GetBytes( buffer) ); ActionData posted = Serializer.Parse( stream); Assert.AreEqual( 1, posted.TransactionIdentifier); Assert.AreEqual( 21, posted.Number); } [Test] public void TestWrite() { PrimeNumberData data = new PrimeNumberData( 9, 1); byte[] buffer = Serializer.Generate( data); string testbuffer = @"<?xml version=""1.0"" encoding=""us-ascii""?><PrimeNumber xmlns:xsd=""http://www.w3.org/2001/XMLSchema"" xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance""> <Result>success</Result> <TransactionIdentifier>1</TransactionIdentifier> <Number>9</Number></PrimeNumber>"; Console.WriteLine( "(" + Encoding.ASCII.GetString( buffer) + ")"); Assert.AreEqual( Encoding.ASCII.GetString( buffer), testbuffer); } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -