示例#1
0
		// --  Jimi Hendrix

		static void Main(string[] args)
		{
			IUnityContainer container = new UnityContainer();
			container.RegisterServices(AppSettings.MutationCountPerGeneration);


			EvolutionController controller = container.Resolve<EvolutionController>();
			controller.StartEvolution(INPUT);
		}
示例#2
0
		static async Task MainAsync(string[] args)
		{
			IUnityContainer container = new UnityContainer();
			container.RegisterServices();

			MainController controller = container.Resolve<MainController>();
			ProblemSolution problemResult = await controller.FindSolution("10 33 23", 0);

			System.Console.WriteLine("Solution found: " + (problemResult.SolutionFound ? "yes" : "no"));
			System.Console.WriteLine("Number of combinations tried: " + problemResult.NumberOfTries);
			System.Console.WriteLine("Total processing time: " + problemResult.ElapsedTime.ToString(@"dd\.hh\:mm\:ss"));
		}
		public void Test_GetTokenType_PositiveIntegerValue()
		{
			// arrange
			var container = new UnityContainer();
			container.RegisterServices();
			var tokenInfoProvider = container.Resolve<ITokenInfoProvider>();

			// act
			TokenType tokenType = tokenInfoProvider.GetTokenType("4");

			// assert
			Assert.AreEqual(tokenType, TokenType.Value);
		}
		public void Test_GetTokenType_NegativeRealValue()
		{
			// arrange
			var container = new UnityContainer();
			container.RegisterServices();
			var tokenInfoProvider = container.Resolve<ITokenInfoProvider>();

			// act
			TokenType tokenType = tokenInfoProvider.GetTokenType("-74539.27405");

			// assert
			Assert.AreEqual(tokenType, TokenType.Value);
		}
		public void Test_GetTokenType_Operator1()
		{
			// arrange
			var container = new UnityContainer();
			container.RegisterServices();
			var tokenInfoProvider = container.Resolve<ITokenInfoProvider>();

			// act
			TokenType tokenType = tokenInfoProvider.GetTokenType("+");

			// assert
			Assert.AreEqual(tokenType, TokenType.Operator);
		}
		public void Test_TryGetParenthesisNotation_Simple_NoOp_Fails()
		{
			var container = new UnityContainer();
			container.RegisterServices();
			var converter = container.Resolve<IRpnToParenthesisNotationConverter>();

			// act
			string result;
			bool convertSucceeded = converter.TryGetParenthesisNotation("5 7", out result);

			// assert
			Assert.IsFalse(convertSucceeded);
			Assert.IsNull(result);
		}
		public void Test_TryGetParenthesisNotation_Simple_Succeeds()
		{
			var container = new UnityContainer();
			container.RegisterServices();
			var converter = container.Resolve<IRpnToParenthesisNotationConverter>();

			// act
			string result;
			bool convertSucceeded = converter.TryGetParenthesisNotation("6 5 *", out result);

			// assert
			Assert.IsTrue(convertSucceeded);
			Assert.AreEqual(result, "6*5");
		}
		public void Test_GetResult_TwoIntegers_Subtract_Negative()
		{
			var container = new UnityContainer();
			container.RegisterServices();
			var calculator = container.Resolve<IRpnCalculator>();

			// act
			RpnResult result = calculator.GetResult("1 2 -");

			// assert
			Assert.IsNotNull(result);
			Assert.IsTrue(result.CalculationResult.HasValue);
			Assert.IsTrue(!result.ErrorMessages.Any());
			Assert.AreEqual(result.CalculationResult.Value, -1);
		}
		public void Test_GetResult_OneNumber_NoOp()
		{
			var container = new UnityContainer();
			container.RegisterServices();
			var calculator = container.Resolve<IRpnCalculator>();

			// act
			RpnResult result = calculator.GetResult("1");

			// assert
			Assert.IsNotNull(result);
			Assert.IsTrue(result.CalculationResult.HasValue);
			Assert.IsTrue(!result.ErrorMessages.Any());
			Assert.AreEqual(result.CalculationResult.Value, 1);
		}
		public void Test_Validate_MixedNumbersValid()
		{
			// arrange
			var container = new UnityContainer();
			container.RegisterServices();
			var validator = container.Resolve<IRpnInputValidator>();

			// act
			RpnInputValidationResult validationResult = validator.Validate(new[] { "-15.111", "5.56", "-999", "-35", "1024" });

			// assert
			Assert.IsNotNull(validationResult);
			Assert.IsTrue(validationResult.IsValid);
			Assert.IsTrue(!validationResult.ErrorMessages.Any());
		}
		public void Test_Validate_PositiveRealsValid()
		{
			// arrange
			var container = new UnityContainer();
			container.RegisterServices();
			var validator = container.Resolve<IRpnInputValidator>();

			// act
			RpnInputValidationResult validationResult = validator.Validate(new[] { "0.44", "5.11", "87.02" });

			// assert
			Assert.IsNotNull(validationResult);
			Assert.IsTrue(validationResult.IsValid);
			Assert.IsTrue(!validationResult.ErrorMessages.Any());
		}
		public void Test_Validate_NegativeIntegersValid()
		{
			// arrange
			var container = new UnityContainer();
			container.RegisterServices();
			var validator = container.Resolve<IRpnInputValidator>();

			// act
			RpnInputValidationResult validationResult = validator.Validate(new[] { "-15", "-556", "-9997", "-966375" });

			// assert
			Assert.IsNotNull(validationResult);
			Assert.IsTrue(validationResult.IsValid);
			Assert.IsTrue(!validationResult.ErrorMessages.Any());
		}
		public void Test_GetResult_TwoReals_Add_Positive()
		{
			var container = new UnityContainer();
			container.RegisterServices();
			var calculator = container.Resolve<IRpnCalculator>();

			// act
			RpnResult result = calculator.GetResult("-100 105 +");

			// assert
			Assert.IsNotNull(result);
			Assert.IsTrue(result.CalculationResult.HasValue);
			Assert.IsTrue(!result.ErrorMessages.Any());
			Assert.AreEqual(result.CalculationResult.Value, 5);
		}
		public void Test_TryGetParenthesisNotation_Complex_Succeeds2()
		{
			// arrange
			var container = new UnityContainer();
			container.RegisterServices();
			var converter = container.Resolve<IRpnToParenthesisNotationConverter>();

			// act
			string result;
			bool convertSucceeded = converter.TryGetParenthesisNotation("1 5 100 5 - * 9 - 10 + +", out result);

			// assert
			Assert.IsTrue(convertSucceeded);
			Assert.AreEqual(result, "1+((9-((100-5)*5))+10)");
		}
		public void Test_TryGetParenthesisNotation_Complex_Succeeds1()
		{
			// arrange
			var container = new UnityContainer();
			container.RegisterServices();
			var converter = container.Resolve<IRpnToParenthesisNotationConverter>();

			// act
			string result;
			bool convertSucceeded = converter.TryGetParenthesisNotation("2 1 100 10 10 * / + 2 -", out result);

			// assert
			Assert.IsTrue(convertSucceeded);
			Assert.AreEqual(result, "(1+((10*10)/100))-2");
		}
		public void GetDistance_Reverse_Expected4()
		{
			// arrange
			var container = new UnityContainer();
			container.RegisterServices(AppSettings.MutationCountPerGeneration);
			var charDistanceCalculator = container.Resolve<IStringDistanceCalculator>();

			string s1 = "ABC";
			string s2 = "CBA";

			// act
			int distance = charDistanceCalculator.GetDistance(s1, s2);

			// assert
			Assert.AreEqual(distance, 4);
		}
		public void GetNewOffspring_TwoParentsEvenLength_ExpectedOffspringWithTwoMutations()
		{
			// arrange
			const int mutationCount = 2;
			var container = new UnityContainer();
			container.RegisterServices(mutationCount);
			var offspringFactory = container.Resolve<IChildFactory>();
			var individFactory = container.Resolve<IndividFactory>();

			var parents = new List<Person>();
			parents.Add(individFactory.CreateIndividual("qwer", "qwer"));
			parents.Add(individFactory.CreateIndividual("asdf", "asdf"));

			// act
			string offspring = offspringFactory.GetNewChild(parents);

			// assert
			Assert.IsNotNull(offspring);
			Assert.IsTrue(offspring.Length == parents[0].Value.Length);
			Assert.IsTrue(AreParents(offspring, parents, mutationCount), offspring);
		}
		public void GetNewOffspring_ThreeParentsOddLength_ExpectedOffspringWithOneMutation()
		{
			// arrange
			const int mutationCount = 1;
			var container = new UnityContainer();
			container.RegisterServices(mutationCount);
			var offspringFactory = container.Resolve<IChildFactory>();
			var individFactory = container.Resolve<IndividFactory>();

			var parents = new List<Person>();
			parents.Add(individFactory.CreateIndividual("abcde", "abcde"));
			parents.Add(individFactory.CreateIndividual("ghijk", "ghijk"));
			parents.Add(individFactory.CreateIndividual("mnopq", "mnopq"));


			// act
			string offspring = offspringFactory.GetNewChild(parents);

			// assert
			Assert.IsNotNull(offspring);
			Assert.IsTrue(offspring.Length == parents[0].Value.Length);
			Assert.IsTrue(AreParents(offspring, parents, mutationCount), offspring);
		}
		public void Test_Validate_MixedNumbersAndOperationsInvalid2()
		{
			// arrange
			var container = new UnityContainer();
			container.RegisterServices();
			var validator = container.Resolve<IRpnInputValidator>();

			// act
			RpnInputValidationResult validationResult = validator.Validate(new[] { "1", "=", "-9.333", "*" });

			// assert
			Assert.IsNotNull(validationResult);
			Assert.IsFalse(validationResult.IsValid);
			foreach (string errorMessage in validationResult.ErrorMessages)
				Assert.IsTrue(!string.IsNullOrWhiteSpace(errorMessage));
		}
		public void Test_Validate_OperationsValid()
		{
			// arrange
			var container = new UnityContainer();
			container.RegisterServices();
			var validator = container.Resolve<IRpnInputValidator>();

			// act
			RpnInputValidationResult validationResult = validator.Validate(new[] { "1", "1", "+", "1", "-", "1", "*", "1", "/" });

			// assert
			Assert.IsNotNull(validationResult);
			Assert.IsTrue(validationResult.IsValid);
			Assert.IsTrue(!validationResult.ErrorMessages.Any());
		}
		public void Test_GetOperationType_Unkown2()
		{
			// arrange
			var container = new UnityContainer();
			container.RegisterServices();
			var tokenInfoProvider = container.Resolve<ITokenInfoProvider>();

			// act
			OperationType operationType = tokenInfoProvider.GetOperationType('8');

			// assert
			Assert.AreEqual(operationType, OperationType.Unkown);
		}
		public void Test_GetResult_TwoNumbers_Divide_Negative()
		{
			var container = new UnityContainer();
			container.RegisterServices();
			var calculator = container.Resolve<IRpnCalculator>();

			// act
			RpnResult result = calculator.GetResult("-120.2 2 /");

			// assert
			Assert.IsNotNull(result);
			Assert.IsTrue(result.CalculationResult.HasValue);
			Assert.IsTrue(!result.ErrorMessages.Any());
			Assert.AreEqual(result.CalculationResult.Value, (decimal)-60.1);
		}
		public void Test_GetResult_IncorrectTokenFail()
		{
			var container = new UnityContainer();
			container.RegisterServices();
			var calculator = container.Resolve<IRpnCalculator>();

			// act
			RpnResult result = calculator.GetResult("5 1 x");

			// assert
			Assert.IsNotNull(result);
			Assert.IsFalse(result.CalculationResult.HasValue);
			Assert.IsTrue(result.ErrorMessages.Any());
			foreach (string message in result.ErrorMessages)
			{
				Assert.IsTrue(!string.IsNullOrWhiteSpace(message));
			}
		}
		public void GetDistance_IncorrectInput_ExpectedException()
		{
			// arrange
			var container = new UnityContainer();
			container.RegisterServices(AppSettings.MutationCountPerGeneration);
			var charDistanceCalculator = container.Resolve<IStringDistanceCalculator>();

			string s1 = "ABC";
			string s2 = "ABCDEF";

			// act
			charDistanceCalculator.GetDistance(s1, s2);

			// assert
			// (exception expected)
		}