|
2 | 2 |
|
3 | 3 | open Microsoft.VisualStudio.TestTools.UnitTesting
|
4 | 4 | open Algorithms.Strings
|
| 5 | +open MinCostStringConversion |
| 6 | + |
| 7 | +[<TestClass>] |
| 8 | +type MinCostStringConversionTests () = |
| 9 | + |
| 10 | + let validateAndApply (source: string ) (operations: Operation array) : string = |
| 11 | + operations |
| 12 | + |> Array.mapFold (fun sourcePosition op -> |
| 13 | + match op with |
| 14 | + | Operation.Copy s -> |
| 15 | + Assert.AreEqual(source.[sourcePosition], s) |
| 16 | + Some s, sourcePosition + 1 |
| 17 | + | Operation.Replace (s, d) -> |
| 18 | + Assert.AreEqual(source.[sourcePosition], s) |
| 19 | + Some d, sourcePosition + 1 |
| 20 | + | Operation.Delete s -> |
| 21 | + Assert.AreEqual(source.[sourcePosition], s) |
| 22 | + None, sourcePosition + 1 |
| 23 | + | Operation.Insert c -> |
| 24 | + Some c, sourcePosition |
| 25 | + ) 0 |
| 26 | + |> fst |
| 27 | + |> Array.choose id |
| 28 | + |> Array.map string |
| 29 | + |> String.concat "" |
| 30 | + |
| 31 | + let calculateCost (operations: Operation array, copyCost:int, replaceCost:int, deleteCost:int, insertCost:int) = |
| 32 | + operations |
| 33 | + |> Array.sumBy (function |
| 34 | + | Operation.Copy _ -> copyCost |
| 35 | + | Operation.Replace _ -> replaceCost |
| 36 | + | Operation.Delete _ -> deleteCost |
| 37 | + | Operation.Insert _ -> insertCost |
| 38 | + ) |
| 39 | + |
| 40 | + |
| 41 | + [<TestMethod>] |
| 42 | + [<DataRow("", "", 1, 2, 3, 4)>] |
| 43 | + [<DataRow("github", "", 1, 2, 3, 4)>] |
| 44 | + [<DataRow("", "github", 1, 2, 3, 4)>] |
| 45 | + [<DataRow("github", "github", 1, 2, 3, 4)>] |
| 46 | + [<DataRow("banana", "apple", 1, 2, 3, 4)>] |
| 47 | + [<DataRow("banana", "apple", 3, 1, 2, 4)>] |
| 48 | + [<DataRow("banana", "apple", 3, 1, 2, 4)>] |
| 49 | + member this.validateResult (source: string, destination: string, copyCost:int, replaceCost:int, deleteCost:int, insertCost:int) = |
| 50 | + let costs, ops = computeTransformTables (source, destination, copyCost, replaceCost, deleteCost, insertCost) |
| 51 | + |
| 52 | + for i = 0 to source.Length do |
| 53 | + for j = 0 to destination.Length do |
| 54 | + let sourceSubstring = source.Substring(0, i) |
| 55 | + let destinationSubstring = destination.Substring(0, j) |
| 56 | + let operations = assembleTransformation (ops, i, j) |
| 57 | + let actualDestinationSubstring = validateAndApply sourceSubstring operations |
| 58 | + let calculatedCost = calculateCost (operations, copyCost, replaceCost, deleteCost, insertCost) |
| 59 | + Assert.AreEqual (destinationSubstring, actualDestinationSubstring) |
| 60 | + Assert.AreEqual (costs.[i].[j], calculatedCost) |
| 61 | + |
| 62 | + static member inputForComputeTransformTables = |
| 63 | + seq { |
| 64 | + yield [| |
| 65 | + "abbbaba" :> obj |
| 66 | + "ababa" :> obj |
| 67 | + 1 :> obj |
| 68 | + 2 :> obj |
| 69 | + 3 :> obj |
| 70 | + 3 :> obj |
| 71 | + ([| |
| 72 | + [|0; 3; 6; 9; 12; 15|] |
| 73 | + [|3; 1; 4; 7; 10; 13|] |
| 74 | + [|6; 4; 2; 5; 8; 11|] |
| 75 | + [|9; 7; 5; 4; 6; 9|] |
| 76 | + [|12; 10; 8; 7; 5; 8|] |
| 77 | + [|15; 13; 11; 9; 8; 6|] |
| 78 | + [|18; 16; 14; 12; 10; 9|] |
| 79 | + [|21; 19; 17; 15; 13; 11|] |
| 80 | + |], |
| 81 | + [| |
| 82 | + [|Operation.Copy 'a'; Operation.Insert 'a'; Operation.Insert 'b'; Operation.Insert 'a'; Operation.Insert 'b'; Operation.Insert 'a'|] |
| 83 | + [|Operation.Delete 'a'; Operation.Copy 'a'; Operation.Insert 'b'; Operation.Copy 'a'; Operation.Insert 'b'; Operation.Copy 'a'|] |
| 84 | + [|Operation.Delete 'b'; Operation.Delete 'b'; Operation.Copy 'b'; Operation.Insert 'a'; Operation.Copy 'b'; Operation.Insert 'a'|] |
| 85 | + [|Operation.Delete 'b'; Operation.Delete 'b'; Operation.Copy 'b'; Operation.Replace ('b', 'a'); Operation.Copy 'b'; Operation.Insert 'a'|] |
| 86 | + [|Operation.Delete 'b'; Operation.Delete 'b'; Operation.Copy 'b'; Operation.Replace ('b', 'a'); Operation.Copy 'b'; Operation.Replace ('b', 'a')|] |
| 87 | + [|Operation.Delete 'a'; Operation.Copy 'a'; Operation.Delete 'a'; Operation.Copy 'a'; Operation.Delete 'a'; Operation.Copy 'a'|] |
| 88 | + [|Operation.Delete 'b'; Operation.Delete 'b'; Operation.Copy 'b'; Operation.Delete 'b'; Operation.Copy 'b'; Operation.Delete 'b'|] |
| 89 | + [|Operation.Delete 'a'; Operation.Copy 'a'; Operation.Delete 'a'; Operation.Copy 'a'; Operation.Delete 'a'; Operation.Copy 'a'|] |
| 90 | + |]) :> obj |
| 91 | + |] |
| 92 | + } |
| 93 | + |
| 94 | + [<TestMethod>] |
| 95 | + [<DynamicData(nameof(MinCostStringConversionTests.inputForComputeTransformTables))>] |
| 96 | + member this.computeTransformTables (sourceString:string, destinationString:string, copyCost:int, replaceCost:int, deleteCost:int, insertCost:int, expected:int array array * Operation array array) = |
| 97 | + let actual = MinCostStringConversion.computeTransformTables(sourceString,destinationString,copyCost,replaceCost,deleteCost,insertCost) |
| 98 | + Assert.IsTrue((expected = actual)) |
| 99 | + |
5 | 100 |
|
6 |
| -// FIXME |
7 |
| -// [<TestClass>] |
8 |
| -// type MinCostStringConversionTests () = |
9 |
| - |
10 |
| -// [<TestMethod>] |
11 |
| -// [<DataRow("abbbaba", "abbba")>] |
12 |
| -// [<DataRow("ababa", "ababa")>] |
13 |
| -// member this.assembleTransformation (ops:string list, i:int, j:int, expected:string list) = |
14 |
| -// let actual = MinCostStringConversion.assembleTransformation(ops, i, j) |
15 |
| -// Assert.AreEqual(expected, actual) |
16 |
| - |
17 |
| -// [<TestMethod>] |
18 |
| -// [<DataRow("abbbaba", "abbba")>] |
19 |
| -// [<DataRow("ababa", "ababa")>] |
20 |
| -// member this.assembleTransformation (sourceString:string, destinationString:string, copyCost:int, replaceCost:int, deleteCost:int, insertCost:int, expected:int list * string list) = |
21 |
| -// let actual = MinCostStringConversion.computeTransformTables(sourceString,destinationString,copyCost,replaceCost,deleteCost,insertCost) |
22 |
| -// Assert.AreEqual(expected, actual) |
23 | 101 |
|
0 commit comments