public void OperatorTest() { Random random = new Random(7777777); int length1 = 50, length2 = 50; int times = 4; Alg nullAlg = null; // + Alg alg1 = Alg.FromRandomMoves(length1, random); Alg alg2 = Alg.FromRandomMoves(length2, random); Alg addTest = alg1 + alg2; Assert.AreEqual(alg1, Alg.FromEnumerable(addTest, 0, alg1.Length)); Assert.AreEqual(alg2, Alg.FromEnumerable(addTest, alg1.Length, alg1.Length + alg2.Length)); Assert.ThrowsException <ArgumentNullException>(() => nullAlg + alg1); Assert.ThrowsException <ArgumentNullException>(() => alg1 + nullAlg); // * Alg multiplyTest = alg1 * times; for (int i = 0; i < times; i++) { Assert.AreEqual(alg1, Alg.FromEnumerable(multiplyTest, i * alg1.Length, (i + 1) * alg1.Length)); } Assert.ThrowsException <ArgumentNullException>(() => nullAlg * times); Assert.ThrowsException <ArgumentOutOfRangeException>(() => alg1 * -1); }
public void FromEnumerableTest() { Move[] moves = { Move.R1, Move.D2, Move.B3 }; Alg alg1 = Alg.FromEnumerable(moves); Assert.AreEqual("R D2 B'", alg1.ToString()); Assert.ThrowsException <ArgumentNullException>(() => Alg.FromEnumerable(null)); Alg alg2 = Alg.FromEnumerable(moves, 0, moves.Length); Assert.AreEqual(alg2, alg1); for (int i = 0; i < moves.Length; i++) { Alg alg3 = Alg.FromEnumerable(moves, i, i); Assert.AreEqual(0, alg3.Length); } Alg alg4a = Alg.FromEnumerable(moves, 0, 2); Alg alg4b = Alg.FromEnumerable(moves.Take(2)); Assert.AreEqual(alg4b, alg4a); Alg alg5a = Alg.FromEnumerable(moves, 1, 3); Alg alg5b = Alg.FromEnumerable(moves.Skip(1)); Assert.AreEqual(alg5b, alg5a); Assert.ThrowsException <ArgumentNullException>(() => Alg.FromEnumerable(null, 0, 0)); Assert.ThrowsException <ArgumentOutOfRangeException>(() => Alg.FromEnumerable(moves, -1, 0)); Assert.ThrowsException <ArgumentOutOfRangeException>(() => Alg.FromEnumerable(moves, 0, moves.Length + 1)); Assert.ThrowsException <ArgumentException>(() => Alg.FromEnumerable(moves, 1, 0)); }
public void EqualityTest() { Alg alg1 = Alg.FromEnumerable(new[] { Move.L2, Move.F1, Move.R3 }); Alg alg2 = Alg.FromEnumerable(new[] { Move.L2, Move.F1, Move.R3 }); Alg nullAlg = null; Assert.IsTrue(alg1.Equals(alg1)); Assert.IsTrue(alg1.Equals(alg2)); Assert.IsFalse(alg1.Equals(nullAlg)); Assert.IsTrue(Alg.AreEqual(alg1, alg1)); Assert.IsTrue(Alg.AreEqual(alg1, alg2)); Assert.IsFalse(Alg.AreEqual(null, alg1)); #pragma warning disable CS1718 // Comparison made to same variable Assert.IsTrue(alg1 == alg1); Assert.IsTrue(alg1 == alg2); Assert.IsFalse(alg1 == nullAlg); Assert.IsFalse(nullAlg == alg1); Assert.IsFalse(alg1 != alg1); Assert.IsFalse(alg1 != alg2); Assert.IsTrue(alg1 != nullAlg); Assert.IsTrue(nullAlg != alg1); #pragma warning restore CS1718 // Comparison made to same variable }
public void ToStringTest() { Alg alg = Alg.FromEnumerable(new[] { Move.B1, Move.R3, Move.F2 }); string expected = "B R' F2"; string result1 = Alg.ToString(alg); string result2 = alg.ToString(); Assert.AreEqual(expected, result1); Assert.AreEqual(expected, result2); Assert.ThrowsException <ArgumentNullException>(() => Alg.ToString(null)); }
public void GetHashCodeTest() { Random random = new Random(7777777); int length = 50; int repetitions = 50; for (int rep = 0; rep < repetitions; rep++) { Alg alg = Alg.FromRandomMoves(length, random); Alg clone = Alg.FromEnumerable(alg); Assert.AreEqual(alg.GetHashCode(), clone.GetHashCode()); } }
private void SearchPhase2(int cp, int equatorPermutation, int udEdgeOrder, int depth, int remainingMoves, int phase1Length) { if (IsTerminated.Value) { return; } if (remainingMoves == 0 && equatorPermutation == 0) //check if solved { Alg solution = Alg.FromEnumerable(_currentPhase1Solution.Take(phase1Length).Concat(_currentPhase2Solution.Take(depth)).Cast <Move>()); for (int i = 0; i < _rotation; i++) { solution = solution.Rotate(Rotation.y3).Rotate(Rotation.x3); } if (_inversed) { solution = solution.Inverse(); } lock (_lockObject) { if (solution.Length < _shortestSolutionLength.Value) { _shortestSolutionIndex.Value = _solutions.Count; _shortestSolutionLength.Value = solution.Length; } _solutions.Add(solution); } if (solution.Length <= _returnLength) { IsTerminated.Value = true; } return; } //increase depth foreach (int move in TwoPhaseConstants.Phase2Moves) { //prevent two consecutive moves on the same face or two //consecutive moves on the same axis in the wrong order if (depth == 0) { if (phase1Length > 0) { int relation = move / 3 - _currentPhase1Solution[phase1Length - 1] / 3; if (relation == SameFace || relation == SameAxisInWrongOrder) { continue; } } } else { int relation = move / 3 - _currentPhase2Solution[depth - 1] / 3; if (relation == SameFace || relation == SameAxisInWrongOrder) { continue; } } int newCp = TableController.CornerPermutationMoveTable[cp, move]; int newEquatorPermutation = TableController.EquatorPermutationMoveTable[equatorPermutation, move]; int newUdEdgePermutation = TableController.UdEdgeOrderMoveTable[udEdgeOrder, MoveTables.Phase1IndexToPhase2Index[move]]; //prune int cornerUdPruningIndex = PruningTables.GetPhase2CornerUdPruningIndex(newUdEdgePermutation, newCp); int cornerUdPruningValue = TableController.Phase2CornerUdPruningTable[cornerUdPruningIndex]; int cornerEquatorPruningIndex = Coordinates.NumEquatorOrders * newCp + newEquatorPermutation; int cornerEquatorPruningValue = TableController.Phase2CornerEquatorPruningTable[cornerEquatorPruningIndex]; if (Math.Max(cornerUdPruningValue, cornerEquatorPruningValue) > remainingMoves - 1) { continue; } _currentPhase2Solution[depth] = move; SearchPhase2(newCp, newEquatorPermutation, newUdEdgePermutation, depth + 1, remainingMoves - 1, phase1Length); } }