コード例 #1
0
ファイル: TestDomainTests.cs プロジェクト: JeroenBos/ASDE
			private static void Initialize()
			{
				TestDomain.UseOtherNumberTypes = true;
				tests = new List<CompositeTestCase>();
				args = new[]
				{
					new AbstractSyntaxTreeBuilderConstructorArguments<TestDomain>(CachingMethod.TrackChanges, TestDomain.ImplicitBinaryOperator.ComparePrecedenceTo, AbstractSyntaxTreeBuilder<TestDomain.Expression, TestDomain>.GetDefaultImplicitOperatorInsertionImplementation(TestDomain.ImplicitBinaryOperator)),
					new AbstractSyntaxTreeBuilderConstructorArguments<TestDomain>(CachingMethod.HashedLookups, TestDomain.ImplicitBinaryOperator.ComparePrecedenceTo, AbstractSyntaxTreeBuilder<TestDomain.Expression, TestDomain>.GetDefaultImplicitOperatorInsertionImplementation(TestDomain.ImplicitBinaryOperator))
				};

				#region Case {a}
				tests.Add(new CompositeTestCase(new[] {new TestDomain.Expression('a')}.ToReadOnlyList(), _ => 1, new[] {new int[] {0}.ToReadOnlyList()}.ToReadOnlyList()));
				#endregion
				#region Case {a, b, c}
				{
					//three expressions that each constitute their own constituent. They have to have the same domain in order to be accepted
					var leaves = new[] { new TestDomain.Expression('a'), new TestDomain.Expression('b'), new TestDomain.Expression('c') }.ToReadOnlyList();
					var indices = new[] { new[] { 0 }.ToReadOnlyList(), new[] { 1 }.ToReadOnlyList(), new[] { 2 }.ToReadOnlyList() }.ToReadOnlyList();
					Func<ReadOnlyCollection<TestDomain>, int> steer = domains =>
					{
						Contract.Assert(domains.Count != 0);
						Contract.Assert(domains.Count <= 3);
						if (domains.Last().IsImpossible)
							return domains.Count - 1;
						int i = domains.IndexOf(td => !domains[0].Equals(td));
						if (i == -1)
						{
							if (domains.Count == 3)
								return (domains.Count + 2);//correct domains, and this indicates the last expression should progress to its next possible domain
							else
								return domains.Count; //the next expression is to be fetched
						}
						else
							return i;
					};
					tests.Add(new CompositeTestCase(leaves, steer, indices));
				}
				#endregion
				#region Case {c, b, a}
				{
					//three expressions that each constitute their own constituent in a different order. They have to have the same domain in order to be accepted
					var leaves = new[] { new TestDomain.Expression('a'), new TestDomain.Expression('b'), new TestDomain.Expression('c') }.ToReadOnlyList();
					var indices = new[] { new[] { 2 }.ToReadOnlyList(), new[] { 1 }.ToReadOnlyList(), new[] { 0 }.ToReadOnlyList() }.ToReadOnlyList();
					Func<ReadOnlyCollection<TestDomain>, int> steer = domains =>
					{
						Contract.Assert(domains.Count != 0);
						Contract.Assert(domains.Count <= 3);
						if (domains.Last().IsImpossible)
							return domains.Count - 1;
						int i = domains.IndexOf(td => !domains[0].Equals(td));
						if (i == -1)
						{
							if (domains.Count == 3)
								return (domains.Count + 2);//correct domains, and this indicates the last expression should progress to its next possible domain
							else
								return domains.Count; //the next expression is to be fetched
						}
						else
							return i;
					};
					tests.Add(new CompositeTestCase(leaves, steer, indices));
				}
				#endregion Case 2
				#region Case 3 {a, b, c, a, b, a }
				{
					//three expressions a, b, c that constitute the constituents a, b, c, a, b, a. They have to have the same domain in order to be accepted, but for the 4th, 5th and 6th position the domains aren't checked
					var leaves = new[] { new TestDomain.Expression('a'), new TestDomain.Expression('b'), new TestDomain.Expression('c') }.ToReadOnlyList();
					var indices = new int[] { 0, 1, 2, 0, 1, 0 }.Select(i => new[] { i }.ToReadOnlyList()).ToReadOnlyList();
					Func<ReadOnlyCollection<TestDomain>, int> steer = domains =>
					{
						Contract.Assert(domains.Count != 0);
						Contract.Assert(domains.Count <= 6);
						if (domains.Any(d => d.IsImpossible))
							return domains.Count - 1;

						int i = domains.Take(3).IndexOf(td => !domains[0].Equals(td));
						if (i == -1)
						{
							if (domains.Count == 6)
								return (domains.Count + 5);//correct domains, and this indicates the last expression should progress to its next possible domain
							else
								return domains.Count; //the next expression is to be fetched
						}
						else
							return i;
					};
					tests.Add(new CompositeTestCase(leaves, steer, indices));
				}
				#endregion
				#region Case { a+b, c, a }
				{
					var leaves = new[] { new TestDomain.Expression('a'), new TestDomain.Expression('b'), new TestDomain.Expression('c'), new TestDomain.Expression('+') }.ToReadOnlyList();
					var indices = new int[][] { new[] { 0, 3, 1 }, new int[] { 2 }, new int[] { 0 } }.Select(i => i.ToReadOnlyList()).ToReadOnlyList();
					Func<ReadOnlyCollection<TestDomain>, int> steer = domains =>
					{
						Contract.Assert(domains.Count != 0);
						Contract.Assert(domains.Count <= 6);
						if (domains.Any(d => d.IsImpossible))
							return domains.Count - 1;

						int i = domains.Take(3).IndexOf(td => !domains[0].Equals(td));
						if (i == -1)
						{
							if (domains.Count == 6)
								return (domains.Count + 5);//correct domains, and this indicates the last expression should progress to its next possible domain
							else
								return domains.Count; //the next expression is to be fetched
						}
						else
							return i;
					};
					tests.Add(new CompositeTestCase(leaves, steer, indices));
				}
				#endregion
				#region Case {{a}, {b}}
				{
					var indices = new[] { new int[] { 0 }.ToReadOnlyList() }.ToReadOnlyList();
					var leaf1 = new[] { new TestDomain.Expression('a') }.ToReadOnlyList();
					var leaf2 = new[] { new TestDomain.Expression('b') }.ToReadOnlyList();
					var constituent1 = new CompositeAbstractSyntaxTreeBuilder<TestDomain.Expression, TestDomain>(leaf1, indices, _ => TestDomain.CompositionResult, domains => domains.Count, new LeavesRange(0, 1), args[0], leaf1[0]).Expression.Value;
					var constituent2 = new CompositeAbstractSyntaxTreeBuilder<TestDomain.Expression, TestDomain>(leaf2, indices, _ => TestDomain.CompositionResult, domains => domains.Count, new LeavesRange(0, 1), args[0], leaf2[0]).Expression.Value;
					

					tests.Add(new CompositeTestCase(new [] { constituent1, constituent2 }.ToReadOnlyList(), domains => domains.Count, indices));
				}
				#endregion 
			}
コード例 #2
0
ファイル: TestDomainTests.cs プロジェクト: JeroenBos/ASDE
			public static void Test()
			{
				Initialize();
				foreach (var arg in args)
					foreach (var test in tests.Skip(4))
					{
						TestDomain.UseOtherNumberTypes = test.UseOtherNumberTypes;
						var cast = new CompositeAbstractSyntaxTreeBuilder<TestDomain.Expression, TestDomain>(test.Leaves, test.IndicesPerConstituent, _ => TestDomain.CompositionResult, test.Steer, new LeavesRange(0, 1), arg);
						var result = cast.GetConstituentDomains().ToArray();
					}
			}