From dd7a54298e0ba6bfe21c847f65a022e359a667f2 Mon Sep 17 00:00:00 2001 From: Patrick Dinger <121539+paxos@users.noreply.github.com> Date: Sun, 6 Feb 2022 15:19:15 -0800 Subject: [PATCH 1/3] Add out of bounds test --- Differ.xcodeproj/project.pbxproj | 31 +++++++- Tests/DifferTests/ExtendedPatchTests.swift | 85 ++++++++++++++++++++++ 2 files changed, 115 insertions(+), 1 deletion(-) create mode 100644 Tests/DifferTests/ExtendedPatchTests.swift diff --git a/Differ.xcodeproj/project.pbxproj b/Differ.xcodeproj/project.pbxproj index 8dfb19d..0bca6e9 100644 --- a/Differ.xcodeproj/project.pbxproj +++ b/Differ.xcodeproj/project.pbxproj @@ -3,10 +3,11 @@ archiveVersion = 1; classes = { }; - objectVersion = 51; + objectVersion = 52; objects = { /* Begin PBXBuildFile section */ + 6F5641E927B086E5009F0F6F /* ExtendedPatchTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6F5641E827B086E5009F0F6F /* ExtendedPatchTests.swift */; }; 900E03A41DE7C6C60033A799 /* Differ.h in Headers */ = {isa = PBXBuildFile; fileRef = C92178BB1CD0023E004642C7 /* Differ.h */; settings = {ATTRIBUTES = (Public, ); }; }; 900E03A51DE7C6D60033A799 /* Differ.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = C9EE87161CCFCA83006BD90E /* Differ.framework */; }; 90382F122075D23E006EFFBF /* NestedBatchUpdate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 90382F112075D23E006EFFBF /* NestedBatchUpdate.swift */; }; @@ -44,6 +45,7 @@ /* End PBXContainerItemProxy section */ /* Begin PBXFileReference section */ + 6F5641E827B086E5009F0F6F /* ExtendedPatchTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ExtendedPatchTests.swift; sourceTree = ""; }; 900E039F1DE7C3370033A799 /* Universal-Framework-Target.xcconfig */ = {isa = PBXFileReference; lastKnownFileType = text.xcconfig; path = "Universal-Framework-Target.xcconfig"; sourceTree = ""; }; 900E03A01DE7C3370033A799 /* Universal-Target-Base.xcconfig */ = {isa = PBXFileReference; lastKnownFileType = text.xcconfig; path = "Universal-Target-Base.xcconfig"; sourceTree = ""; }; 900E03AF1DE7F1E80033A799 /* Deployment-Targets.xcconfig */ = {isa = PBXFileReference; lastKnownFileType = text.xcconfig; path = "Deployment-Targets.xcconfig"; sourceTree = ""; }; @@ -138,6 +140,7 @@ C921BF701E15448300747566 /* NestedExtendedDiffTests.swift */, C991189D1DDB4F1100067A60 /* PatchApplyTests.swift */, C9C924791DBB97130006ACC4 /* PatchSortTests.swift */, + 6F5641E827B086E5009F0F6F /* ExtendedPatchTests.swift */, ); path = DifferTests; sourceTree = ""; @@ -223,6 +226,9 @@ dependencies = ( ); name = Differ; + packageProductDependencies = ( + 6F04634427B0727E0062425C /* FastDiff */, + ); productName = Diff.swift; productReference = C9EE87161CCFCA83006BD90E /* Differ.framework */; productType = "com.apple.product-type.framework"; @@ -257,6 +263,9 @@ en, ); mainGroup = C9EE870C1CCFCA83006BD90E; + packageReferences = ( + 6F04634327B0727E0062425C /* XCRemoteSwiftPackageReference "FastDiff" */, + ); productRefGroup = C9EE87171CCFCA83006BD90E /* Products */; projectDirPath = ""; projectRoot = ""; @@ -279,6 +288,7 @@ C921BF711E15448300747566 /* NestedExtendedDiffTests.swift in Sources */, C90CD7191DFB43C600BE9114 /* NestedDiffTests.swift in Sources */, E2F1C8561E3A14DF00FBE786 /* BatchUpdateTests.swift in Sources */, + 6F5641E927B086E5009F0F6F /* ExtendedPatchTests.swift in Sources */, ); runOnlyForDeploymentPostprocessing = 0; }; @@ -496,6 +506,25 @@ defaultConfigurationName = Release; }; /* End XCConfigurationList section */ + +/* Begin XCRemoteSwiftPackageReference section */ + 6F04634327B0727E0062425C /* XCRemoteSwiftPackageReference "FastDiff" */ = { + isa = XCRemoteSwiftPackageReference; + repositoryURL = "https://github.com/kandelvijaya/FastDiff.git"; + requirement = { + kind = upToNextMajorVersion; + minimumVersion = 2.0.0; + }; + }; +/* End XCRemoteSwiftPackageReference section */ + +/* Begin XCSwiftPackageProductDependency section */ + 6F04634427B0727E0062425C /* FastDiff */ = { + isa = XCSwiftPackageProductDependency; + package = 6F04634327B0727E0062425C /* XCRemoteSwiftPackageReference "FastDiff" */; + productName = FastDiff; + }; +/* End XCSwiftPackageProductDependency section */ }; rootObject = C9EE870D1CCFCA83006BD90E /* Project object */; } diff --git a/Tests/DifferTests/ExtendedPatchTests.swift b/Tests/DifferTests/ExtendedPatchTests.swift new file mode 100644 index 0000000..03ddbb3 --- /dev/null +++ b/Tests/DifferTests/ExtendedPatchTests.swift @@ -0,0 +1,85 @@ +// +// ExtendedPatchTests.swift +// DifferTests +// +// Created by Patrick Dinger on 2/6/22. +// Copyright © 2022 Differ Project. All rights reserved. +// + +import XCTest +import Differ + +class ExtendedPatchTests: XCTestCase { + func testDifferScenario1() throws { + let input = [1, 3, 4] + let output = [1, 2, 3, 4] + let result = runDiffer(start: input, end: output) + XCTAssertEqual(output, result) + } + + func testDifferScenario2() throws { + let input = [1, 2, 3, 4] + let output = [1, 3, 4] + let result = runDiffer(start: input, end: output) + XCTAssertEqual(output, result) + } + + func testDifferScenario3() throws { + let input = [1, 2, 3, 4] + let output = [4, 3, 2, 1] + let result = runDiffer(start: input, end: output) + XCTAssertEqual(output, result) + } + + /// This fails: Thread 1: Fatal error: Index out of range + /// MOVE number from 8 4 [4, 4, 3, 8, 2, 5, 6, 9] + /// The array is only 8 elements long, not 9 + func testDifferScenarioFailure() throws { + let input = [7, 2, 5, 6, 9] + let output = [4, 4, 3, 8, 9, 5, 2, 5] + let result = runDiffer(start: input, end: output) + XCTAssertEqual(output, result) + } + + /// This is another example of a failed scenario + func testDifferScenarioFailure_alternative() throws { + let input = [9, 0, 10, 5] + let output = [1, 2, 7, 5, 6, 5, 10, 6, 9, 10] + let result = runDiffer(start: input, end: output) + XCTAssertEqual(output, result) + } + + func testDifferScenario5() throws { + let input = [7, 2, 5, 6, 9] + let output = [4, 4, 3, 8, 9, 5, 2, 5] + let result = runDiffer(start: input, end: output) + XCTAssertEqual(output, result) + } +} + +private func runDiffer(start: [T], end: [T]) -> [T] { + let patches = Differ.extendedPatch(from: start, to: end) + var workingSet = start + + for patch in patches { + switch patch { + case .insertion(index: let index, element: let element): + print("INSERT \(element) at", index) + workingSet.insert(element, at: index) + print(workingSet) + case .deletion(index: let index): + print("REMOVE \(workingSet[index]) at", index) + workingSet.remove(at: index) + print(workingSet) + case .move(from: let from, to: let to): + print("MOVE number from", from, to, workingSet) + let val = workingSet[from] + print(val) + workingSet.remove(at: from) + workingSet.insert(val, at: to) + print(workingSet) + } + } + + return workingSet +} From da3495a64e188dfd3de269ad355a41a1ac65ae0f Mon Sep 17 00:00:00 2001 From: Patrick Dinger <121539+paxos@users.noreply.github.com> Date: Sun, 6 Feb 2022 15:38:40 -0800 Subject: [PATCH 2/3] Cleanup project file --- Differ.xcodeproj/project.pbxproj | 35 +++++--------------------------- 1 file changed, 5 insertions(+), 30 deletions(-) diff --git a/Differ.xcodeproj/project.pbxproj b/Differ.xcodeproj/project.pbxproj index 0bca6e9..accdfb1 100644 --- a/Differ.xcodeproj/project.pbxproj +++ b/Differ.xcodeproj/project.pbxproj @@ -3,11 +3,11 @@ archiveVersion = 1; classes = { }; - objectVersion = 52; + objectVersion = 51; objects = { /* Begin PBXBuildFile section */ - 6F5641E927B086E5009F0F6F /* ExtendedPatchTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6F5641E827B086E5009F0F6F /* ExtendedPatchTests.swift */; }; + 6F5641F027B09338009F0F6F /* ExtendedPatchTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6F5641EF27B09338009F0F6F /* ExtendedPatchTests.swift */; }; 900E03A41DE7C6C60033A799 /* Differ.h in Headers */ = {isa = PBXBuildFile; fileRef = C92178BB1CD0023E004642C7 /* Differ.h */; settings = {ATTRIBUTES = (Public, ); }; }; 900E03A51DE7C6D60033A799 /* Differ.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = C9EE87161CCFCA83006BD90E /* Differ.framework */; }; 90382F122075D23E006EFFBF /* NestedBatchUpdate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 90382F112075D23E006EFFBF /* NestedBatchUpdate.swift */; }; @@ -45,7 +45,7 @@ /* End PBXContainerItemProxy section */ /* Begin PBXFileReference section */ - 6F5641E827B086E5009F0F6F /* ExtendedPatchTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ExtendedPatchTests.swift; sourceTree = ""; }; + 6F5641EF27B09338009F0F6F /* ExtendedPatchTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ExtendedPatchTests.swift; sourceTree = ""; }; 900E039F1DE7C3370033A799 /* Universal-Framework-Target.xcconfig */ = {isa = PBXFileReference; lastKnownFileType = text.xcconfig; path = "Universal-Framework-Target.xcconfig"; sourceTree = ""; }; 900E03A01DE7C3370033A799 /* Universal-Target-Base.xcconfig */ = {isa = PBXFileReference; lastKnownFileType = text.xcconfig; path = "Universal-Target-Base.xcconfig"; sourceTree = ""; }; 900E03AF1DE7F1E80033A799 /* Deployment-Targets.xcconfig */ = {isa = PBXFileReference; lastKnownFileType = text.xcconfig; path = "Deployment-Targets.xcconfig"; sourceTree = ""; }; @@ -140,7 +140,7 @@ C921BF701E15448300747566 /* NestedExtendedDiffTests.swift */, C991189D1DDB4F1100067A60 /* PatchApplyTests.swift */, C9C924791DBB97130006ACC4 /* PatchSortTests.swift */, - 6F5641E827B086E5009F0F6F /* ExtendedPatchTests.swift */, + 6F5641EF27B09338009F0F6F /* ExtendedPatchTests.swift */, ); path = DifferTests; sourceTree = ""; @@ -226,9 +226,6 @@ dependencies = ( ); name = Differ; - packageProductDependencies = ( - 6F04634427B0727E0062425C /* FastDiff */, - ); productName = Diff.swift; productReference = C9EE87161CCFCA83006BD90E /* Differ.framework */; productType = "com.apple.product-type.framework"; @@ -263,9 +260,6 @@ en, ); mainGroup = C9EE870C1CCFCA83006BD90E; - packageReferences = ( - 6F04634327B0727E0062425C /* XCRemoteSwiftPackageReference "FastDiff" */, - ); productRefGroup = C9EE87171CCFCA83006BD90E /* Products */; projectDirPath = ""; projectRoot = ""; @@ -288,7 +282,7 @@ C921BF711E15448300747566 /* NestedExtendedDiffTests.swift in Sources */, C90CD7191DFB43C600BE9114 /* NestedDiffTests.swift in Sources */, E2F1C8561E3A14DF00FBE786 /* BatchUpdateTests.swift in Sources */, - 6F5641E927B086E5009F0F6F /* ExtendedPatchTests.swift in Sources */, + 6F5641F027B09338009F0F6F /* ExtendedPatchTests.swift in Sources */, ); runOnlyForDeploymentPostprocessing = 0; }; @@ -506,25 +500,6 @@ defaultConfigurationName = Release; }; /* End XCConfigurationList section */ - -/* Begin XCRemoteSwiftPackageReference section */ - 6F04634327B0727E0062425C /* XCRemoteSwiftPackageReference "FastDiff" */ = { - isa = XCRemoteSwiftPackageReference; - repositoryURL = "https://github.com/kandelvijaya/FastDiff.git"; - requirement = { - kind = upToNextMajorVersion; - minimumVersion = 2.0.0; - }; - }; -/* End XCRemoteSwiftPackageReference section */ - -/* Begin XCSwiftPackageProductDependency section */ - 6F04634427B0727E0062425C /* FastDiff */ = { - isa = XCSwiftPackageProductDependency; - package = 6F04634327B0727E0062425C /* XCRemoteSwiftPackageReference "FastDiff" */; - productName = FastDiff; - }; -/* End XCSwiftPackageProductDependency section */ }; rootObject = C9EE870D1CCFCA83006BD90E /* Project object */; } From 1bbc028a6898855907e569126852864589c75823 Mon Sep 17 00:00:00 2001 From: Patrick Dinger <121539+paxos@users.noreply.github.com> Date: Sun, 6 Feb 2022 15:40:53 -0800 Subject: [PATCH 3/3] Remove header comment --- Tests/DifferTests/ExtendedPatchTests.swift | 8 -------- 1 file changed, 8 deletions(-) diff --git a/Tests/DifferTests/ExtendedPatchTests.swift b/Tests/DifferTests/ExtendedPatchTests.swift index 03ddbb3..eca2362 100644 --- a/Tests/DifferTests/ExtendedPatchTests.swift +++ b/Tests/DifferTests/ExtendedPatchTests.swift @@ -1,11 +1,3 @@ -// -// ExtendedPatchTests.swift -// DifferTests -// -// Created by Patrick Dinger on 2/6/22. -// Copyright © 2022 Differ Project. All rights reserved. -// - import XCTest import Differ