public void TestShapeEquals() { Shape s1 = new Shape(); s1.AddSubstroke(newValidSubstroke()); s1.AddSubstroke(newValidSubstroke()); s1.AddSubstroke(newValidSubstroke()); s1.Type = Domain.LogicDomain.AND; Shape s2 = s1.Clone(); Assert.IsTrue(s1.Equals(s2), "Original shape reports it is not equal (.Equals) to the clone"); Assert.IsTrue(s2.Equals(s1), "Cloned shape reports it is not equal (.Equals) to the original"); Assert.IsTrue(s1.GeometricEquals(s2), "Original shape reports it is not equal (.GeometricEquals) to the clone"); Assert.IsTrue(s2.GeometricEquals(s1), "Cloned shape reports it is not equal (.GeometricEquals) to the original"); Assert.IsFalse(s1 == s2, "Original shape reports it is strictly equal (==) to the clone"); Assert.IsFalse(s2 == s1, "Cloned shape reports it is strictly equal (==) to the original"); s2.Type = Domain.LogicDomain.OR; Assert.IsFalse(s1.Equals(s2), "Shape equality should depend on type"); Assert.IsFalse(s2.Equals(s1), "Shape equality should depend on type"); Assert.IsTrue(s1.GeometricEquals(s2), "Geometric shape equality should not depend on type"); Assert.IsTrue(s2.GeometricEquals(s1), "Geometric shape equality should not depend on type"); Assert.IsFalse(s1.Id.Equals(s2.Id), "Two shapes should not have the same ID, even if they are clones"); }
public void TestSmartCacheMutableObjects() { Shape shape1 = Shapes.newValidShape(); Shape shape2 = Shapes.newValidShape(); shape1.AddSubstroke(Shapes.newValidSubstroke()); int shape1Value = numSubstrokes(shape1); int shape2Value = numSubstrokes(shape2); timesNumSubstrokesCalled = 0; SmartCache <Shape, int> cache = new SmartCache <Shape, int>(numSubstrokes); Assert.AreEqual(0, timesNumSubstrokesCalled, "SmartCache called the computation function in its constructor!?"); Assert.AreEqual(shape1Value, cache[shape1], "SmartCache did not compute shape1 value correctly"); Assert.AreEqual(shape2Value, cache[shape2], "SmartCache did not compute shape2 value correctly"); Assert.AreEqual(shape1Value, cache[shape1], "SmartCache did not save shape1 value correctly"); Assert.AreEqual(shape2Value, cache[shape2], "SmartCache did not save shape2 value correctly"); Assert.AreEqual(2, timesNumSubstrokesCalled, "SmartCache didn't call the computation function twice!?"); shape1.AddSubstroke(Shapes.newValidSubstroke()); shape1Value = shape1.SubstrokesL.Count; Assert.AreEqual(shape1Value, cache[shape1], "SmartCache did not recompute shape1 value correctly"); Assert.AreEqual(shape2Value, cache[shape2], "SmartCache did not save shape2 value correctly"); Assert.AreEqual(3, timesNumSubstrokesCalled, "SmartCache didn't recompute shape1 value!?"); }
public void TestShapesInDictionaries() { Shape shape = new Shape(); shape.Type = Domain.LogicDomain.AND; // ======================= GetHashCode / Equals ========================= shape.AddSubstroke(newValidSubstroke()); shape.AddSubstroke(newValidSubstroke()); shape.AddSubstroke(newValidSubstroke()); int hash1 = shape.GetHashCode(); Shape test1 = shape.Clone(); shape.AddSubstroke(newValidSubstroke()); int hash2 = shape.GetHashCode(); Shape test2 = shape.Clone(); Assert.IsTrue(shape == shape, "Shape does not equal itself (==)"); Assert.IsTrue(shape.Equals(shape), "Shape does not equal itself (.Equals)"); Assert.AreEqual(hash1, hash2, "Shape hash code should never change"); Assert.IsFalse(test1 == test2, "Different shapes with different substrokes should not be equal (==)"); Assert.IsFalse(test1.Equals(test2), "Different shapes with different substrokes should not be equal (.Equals)"); // ======================= Dictionary ========================= Dictionary <Shape, int> d = new Dictionary <Shape, int>(); int value = 0; int realValue; bool success; d.Add(shape, value); Assert.IsTrue(d.ContainsKey(shape), "Dictionary reports it does not contain a shape that was just added"); success = d.TryGetValue(shape, out realValue); Assert.IsTrue(success, "Dictionary contains shape but cannot find it"); Assert.AreEqual(value, realValue, "Dictionary has wrong value for shape"); shape.AddSubstroke(newValidSubstroke()); Assert.IsTrue(d.ContainsKey(shape), "After modifying a shape, dictionary no longer contains it"); success = d.TryGetValue(shape, out realValue); Assert.IsTrue(success, "After adding a substroke, dictionary contains shape but cannot find it"); Assert.AreEqual(value, realValue, "After adding a substroke, dictionary has wrong value for shape"); }
public void TestShapeSubstrokeOperations() { Shape shape = new Shape(); shape.Type = Domain.LogicDomain.AND; Assert.AreEqual(0, shape.SubstrokesL.Count, "New shapes should have 0 substrokes"); for (int i = 1; i <= 10; i++) { shape.AddSubstroke(newValidSubstroke()); Assert.AreEqual(i, shape.SubstrokesL.Count, "Added a substroke; count should be " + i); } }
/// <summary> /// Make the substroke trade. /// </summary> protected override void performActual() { foreach (EndPoint endpoint in _gem.Endpoints) { endpoint.ConnectedShape = null; } Shape victim = _gem.ParentShape; if (victim != null) { victim.RemoveSubstroke(_gem); } _thief.AddSubstroke(_gem); if (victim != null && victim.Substrokes.Length == 0) { MySketch.RemoveShape(victim); } MySketch.CheckConsistency(); }
/// <summary> /// Refreshes the underlying data structures corresponding to the inkStroke after the /// inkStroke has been modified (moved or resized) /// </summary> /// <param name="inkStroke">The Ink Stroke to modify</param> public void UpdateInkStroke(System.Windows.Ink.Stroke inkStroke) { // Remove the old substroke, saving the old shape for the new substroke. Shape oldShape = GetSketchSubstrokeByInk(inkStroke).ParentShape; DeleteStroke(inkStroke); AddStroke(inkStroke); Substroke newSubstroke = GetSketchSubstrokeByInk(inkStroke); if (newSubstroke.ParentShape != null) { throw new Exception("jesus dear lord why does this newly" + "created substroke already have a parent Shape"); } if (oldShape != null) { oldShape.AddSubstroke(newSubstroke); if (!Sketch.containsShape(oldShape)) { Sketch.AddShape(oldShape); } } }