diff --git a/ProgressCheck1/ProgressCheck1.Tests/Program.fs b/ProgressCheck1/ProgressCheck1.Tests/Program.fs new file mode 100644 index 0000000..5da7e48 --- /dev/null +++ b/ProgressCheck1/ProgressCheck1.Tests/Program.fs @@ -0,0 +1,5 @@ +module Program = + + [] + let main _ = 0 + diff --git a/ProgressCheck1/ProgressCheck1.Tests/ProgressCheck1.Tests.fsproj b/ProgressCheck1/ProgressCheck1.Tests/ProgressCheck1.Tests.fsproj new file mode 100644 index 0000000..350b4d5 --- /dev/null +++ b/ProgressCheck1/ProgressCheck1.Tests/ProgressCheck1.Tests.fsproj @@ -0,0 +1,34 @@ + + + + net7.0 + + false + false + true + + + + + + + + + + + + runtime; build; native; contentfiles; analyzers; buildtransitive +all + + runtime; build; native; contentfiles; analyzers; buildtransitive +all + + + + + + + + + + diff --git a/ProgressCheck1/ProgressCheck1.Tests/UnitTests.fs b/ProgressCheck1/ProgressCheck1.Tests/UnitTests.fs new file mode 100644 index 0000000..5308fba --- /dev/null +++ b/ProgressCheck1/ProgressCheck1.Tests/UnitTests.fs @@ -0,0 +1,53 @@ +module ProgressCheck1.Tests + +open NUnit.Framework +open FsUnit +open ProgressCheck1.Fibonachi +open ProgressCheck1.PrintSquare +open ProgressCheck1.PriorityQueue + +[] +let TestFibonachi () = + countSumFibEven() |> should equal 1089154 + +let squaresCases = + seq{ + TestCaseData(4, "****\n* *\n* *\n****") + TestCaseData(5, "*****\n* *\n* *\n* *\n*****") + } + +[] +let TestSquare (n : int, answer : string) = + PrintSquare n |> should equal answer + +let queue = new PriorityQueue(fun n1 n2 -> compare n1 n2) + +[] +let TestEnqueue () = + queue.Enqueue(1) + assert not queue.IsEmpty + +[] +let ``dequeue from empty queue throws exception`` () = + queue.Clear() + (fun () -> queue.Dequeue() |> ignore) |> should throw typeof + +[] +let ``peek with empty queue throws exception`` () = + queue.Clear() + (fun () -> queue.Peek() |> ignore) |> should throw typeof + +[] +let fillQueue () = + queue.Clear() + queue.Enqueue(1) + queue.Enqueue(-1) + queue.Enqueue(2) + +[] +let TestDequeue () = + queue.Dequeue() |> should equal 2 + +[] +let TestPeek () = + queue.Peek() |> should equal 2 \ No newline at end of file diff --git a/ProgressCheck1/ProgressCheck1.sln b/ProgressCheck1/ProgressCheck1.sln new file mode 100644 index 0000000..4192bde --- /dev/null +++ b/ProgressCheck1/ProgressCheck1.sln @@ -0,0 +1,31 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 16 +VisualStudioVersion = 25.0.1706.10 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{F2A71F9B-5D33-465A-A702-920D77279786}") = "ProgressCheck1", "ProgressCheck1\ProgressCheck1.fsproj", "{47233943-791F-4600-B5F7-872EAF50CB35}" +EndProject +Project("{F2A71F9B-5D33-465A-A702-920D77279786}") = "ProgressCheck1.Tests", "ProgressCheck1.Tests\ProgressCheck1.Tests.fsproj", "{D5C87B10-390D-4421-A91E-19BA38B2439B}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {47233943-791F-4600-B5F7-872EAF50CB35}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {47233943-791F-4600-B5F7-872EAF50CB35}.Debug|Any CPU.Build.0 = Debug|Any CPU + {47233943-791F-4600-B5F7-872EAF50CB35}.Release|Any CPU.ActiveCfg = Release|Any CPU + {47233943-791F-4600-B5F7-872EAF50CB35}.Release|Any CPU.Build.0 = Release|Any CPU + {D5C87B10-390D-4421-A91E-19BA38B2439B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {D5C87B10-390D-4421-A91E-19BA38B2439B}.Debug|Any CPU.Build.0 = Debug|Any CPU + {D5C87B10-390D-4421-A91E-19BA38B2439B}.Release|Any CPU.ActiveCfg = Release|Any CPU + {D5C87B10-390D-4421-A91E-19BA38B2439B}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {94AE1E0B-85E1-46BC-8F1F-D7EA6DDE84D2} + EndGlobalSection +EndGlobal diff --git a/ProgressCheck1/ProgressCheck1/Fibonachi.fs b/ProgressCheck1/ProgressCheck1/Fibonachi.fs new file mode 100644 index 0000000..f4926cc --- /dev/null +++ b/ProgressCheck1/ProgressCheck1/Fibonachi.fs @@ -0,0 +1,11 @@ +namespace ProgressCheck1 + +module Fibonachi = + + let countSumFibEven () = + let rec countSum n1 n2 sum = + match n1 with + | (n : int) when (n >= 1000000) -> sum + | (n : int) when (n % 2 = 0) -> countSum n2 (n2 + n1) sum + | _ -> countSum n2 (n2 + n1) (sum + n1) + countSum 1 1 0 \ No newline at end of file diff --git a/ProgressCheck1/ProgressCheck1/PriorityQueue.fs b/ProgressCheck1/ProgressCheck1/PriorityQueue.fs new file mode 100644 index 0000000..bbb9e15 --- /dev/null +++ b/ProgressCheck1/ProgressCheck1/PriorityQueue.fs @@ -0,0 +1,38 @@ +namespace ProgressCheck1 + +module PriorityQueue = + + type PriorityQueue<'T>(comparer : 'T -> 'T -> int) = + + let mutable items = [] + + let lockObject = obj + + member this.Clear() = + items <- [] + + member this.Enqueue(item : 'T) = + let rec insert items x = + match items with + | [] -> [x] + | y::tail when comparer x y > 0 -> x::y::tail + | y::tail -> y::insert tail x + lock lockObject (fun () -> (items <- insert items item)) + + member this.Dequeue() = + lock lockObject (fun () -> + match items with + | [] -> failwith "Queue is empty" + | head::tail -> + items <- tail + head) + + member this.Peek() = + lock lockObject (fun () -> + match items with + | [] -> failwith "Queue is empty" + | head::_ -> head + ) + + member this.IsEmpty = + lock lockObject (fun () -> (List.length items = 0)) \ No newline at end of file diff --git a/ProgressCheck1/ProgressCheck1/ProgressCheck1.fsproj b/ProgressCheck1/ProgressCheck1/ProgressCheck1.fsproj new file mode 100644 index 0000000..40dcefe --- /dev/null +++ b/ProgressCheck1/ProgressCheck1/ProgressCheck1.fsproj @@ -0,0 +1,14 @@ + + + + net7.0 + true + + + + + + + + + diff --git a/ProgressCheck1/ProgressCheck1/SquarePrinter.fs b/ProgressCheck1/ProgressCheck1/SquarePrinter.fs new file mode 100644 index 0000000..169413a --- /dev/null +++ b/ProgressCheck1/ProgressCheck1/SquarePrinter.fs @@ -0,0 +1,27 @@ +namespace ProgressCheck1 + +module PrintSquare = + + let PrintSquare n = + + let rec makeInnerString str iter n = + match iter with + | (p : int) when (p = n) -> str + | (p : int) when (p = 0 || p = n - 1) -> + makeInnerString (String.concat "" [str; "*"]) (iter + 1) n + | _ -> makeInnerString (String.concat "" [str; " "]) (iter + 1) n + + let rec makeBoundaries str iter n = + match iter with + | (p : int) when (p = n) -> str + | _ -> makeBoundaries (String.concat "" [str; "*"]) (iter + 1) n + + let boundary = makeBoundaries "" 0 n + let inner = makeInnerString "" 0 n + + let rec makeSquare square iter n = + match iter with + | (p : int) when (p = n - 1) -> (String.concat "\n" [square; boundary]) + | _ -> makeSquare (String.concat "\n" [square; inner]) (iter + 1) n + + makeSquare boundary 1 n \ No newline at end of file