Skip to content

Commit 3cccc7c

Browse files
authored
merge test work
2 parents c0caba3 + be9886f commit 3cccc7c

File tree

5 files changed

+225
-0
lines changed

5 files changed

+225
-0
lines changed

Test.Tests/Test.Tests.fsproj

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFramework>net7.0</TargetFramework>
5+
6+
<IsPackable>false</IsPackable>
7+
<GenerateProgramFile>true</GenerateProgramFile>
8+
<IsTestProject>true</IsTestProject>
9+
</PropertyGroup>
10+
11+
<ItemGroup>
12+
<Compile Include="TestTests.fs" />
13+
</ItemGroup>
14+
15+
<ItemGroup>
16+
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.12.0" />
17+
<PackageReference Include="NUnit" Version="4.2.2" />
18+
<PackageReference Include="NUnit3TestAdapter" Version="4.6.0" />
19+
<PackageReference Include="NUnit.Analyzers" Version="4.4.0"><IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
20+
<PrivateAssets>all</PrivateAssets>
21+
</PackageReference>
22+
<PackageReference Include="coverlet.collector" Version="6.0.2"><IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
23+
<PrivateAssets>all</PrivateAssets>
24+
</PackageReference>
25+
<PackageReference Include="FsUnit" Version="6.0.1">
26+
<GeneratePathProperty></GeneratePathProperty>
27+
</PackageReference>
28+
<PackageReference Include="FsCheck" Version="2.16.6">
29+
<GeneratePathProperty></GeneratePathProperty>
30+
</PackageReference>
31+
<PackageReference Include="FSharp.Core" Version="9.0.100">
32+
<GeneratePathProperty></GeneratePathProperty>
33+
</PackageReference>
34+
</ItemGroup>
35+
36+
<ItemGroup>
37+
<ProjectReference Include="..\Test\Test.fsproj" />
38+
</ItemGroup>
39+
</Project>

Test.Tests/TestTests.fs

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
module Test.Tests
2+
3+
open NUnit.Framework
4+
open FsUnit
5+
open Test
6+
7+
//find min tests
8+
[<Test>]
9+
let ``If ls is empty returns None test`` () = findMinElement [] |> should equal None
10+
11+
[<Test>]
12+
let ``Works correctly on int`` () =
13+
findMinElement [ 1; 2; 3; -19; 0 ] |> should equal <| Some(-19)
14+
15+
[<Test>]
16+
let ``Works correctly on string`` () =
17+
findMinElement [ "5"; "3"; "" ] |> should equal <| Some("")
18+
19+
//tree tests
20+
[<Test>]
21+
let ``If tree is Empty returns 0 test`` () =
22+
let tree = Empty
23+
findMinDistance tree |> should equal <| None
24+
25+
[<Test>]
26+
let ``Returns correct distance test`` () =
27+
let tree = Node(2, Empty, Empty)
28+
findMinDistance tree |> should equal <| Some(0)
29+
30+
[<Test>]
31+
let ``Returns correct distance in complex tree test`` () =
32+
let tree =
33+
Node(3, Node(3, Node(3, Empty, Empty), Node(3, Empty, Empty)), Node(3, Empty, Empty))
34+
35+
findMinDistance tree |> should equal <| Some(1)
36+
37+
//hash-table tests
38+
let hashTable = HashTable(5, id)
39+
40+
[<Test>]
41+
let ``Adding more than one test`` () =
42+
hashTable.Add(5)
43+
hashTable.Add(10)
44+
hashTable.Contains(5) |> should be True
45+
hashTable.Contains(10) |> should be True
46+
47+
[<Test>]
48+
let ``Remove test`` () =
49+
hashTable.Remove(5) |> should be True
50+
hashTable.Contains(5) |> should be False

Test/Test.fs

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
namespace Test
2+
3+
module Test =
4+
/// finds min element in list if possible
5+
/// <param name="ls">list in which we trying to find min element</param>
6+
/// <returns>min element</returns>
7+
let findMinElement ls =
8+
match ls with
9+
| h :: tail -> Some <| List.fold (fun acc x -> if x < acc then x else acc) h tail
10+
| _ -> None
11+
12+
/// represents BinaryTree
13+
type BinaryTree<'a> =
14+
| Node of 'a * BinaryTree<'a> * BinaryTree<'a>
15+
| Empty
16+
17+
/// represents Continuation steps with tree on currentStep and intermediate int calculation
18+
type ContinuationStep<'a> =
19+
| Finished
20+
| Step of BinaryTree<'a> * int * (unit -> ContinuationStep<'a>)
21+
22+
/// linearizes binary tree traversal using steps of ContinuationStep
23+
/// <param name="binTree">tree to find traversal</param>
24+
/// <param name="currDistance">distance from root to current Node to store in step</param>
25+
/// <param name="currDistance">continuation function</param>
26+
/// <returns>step</returns>
27+
let rec linearize binTree currDistance cont =
28+
match binTree with
29+
| Empty -> cont ()
30+
| Node(_, l, r) ->
31+
Step(
32+
binTree,
33+
currDistance,
34+
(fun () -> linearize l (currDistance + 1) (fun () -> linearize r (currDistance + 1) cont))
35+
)
36+
37+
/// finds if possible min distance from node to root in tree
38+
/// <param name="binTree">tree where we want to find min distance</param>
39+
/// <returns>min distance</returns>
40+
let findMinDistance binTree =
41+
if binTree = Empty then
42+
None
43+
else
44+
let steps = linearize binTree 0 (fun () -> Finished)
45+
46+
let rec processSteps step distanceList =
47+
match step with
48+
| Finished -> distanceList
49+
| Step(x, currDistance, getNext) ->
50+
match x with
51+
| Node(_, Empty, Empty) -> processSteps (getNext ()) (currDistance :: distanceList)
52+
| _ -> processSteps (getNext ()) distanceList
53+
54+
findMinElement (processSteps steps [])
55+
56+
/// represents hash-table
57+
type HashTable<'a when 'a: equality>(size: int, hashFunction: 'a -> int) =
58+
let mutable table = Array.init size (fun _ -> [])
59+
60+
let rec removeElement element list =
61+
match list with
62+
| [] -> []
63+
| head :: tail when head = element -> tail
64+
| head :: tail -> head :: (removeElement element tail)
65+
66+
let getHashIndex element =
67+
let hash = hashFunction element
68+
hash % size
69+
70+
/// adds element to hash-table by hash index
71+
/// <param name="element">element</param>
72+
member this.Add(element) =
73+
let index = getHashIndex element
74+
table.[index] <- element :: table.[index]
75+
76+
/// checks if element is in hash-table
77+
/// <param name="element">element</param>
78+
/// <returns>true if element in hash-table false otherwise</returns>
79+
member this.Contains(element) =
80+
let index = getHashIndex element
81+
List.contains element table.[index]
82+
83+
/// removes element from hash-table
84+
/// <param name="element">element</param>
85+
/// <returns>true if element was in hash-table (was removed) false otherwise</returns>
86+
member this.Remove(element) =
87+
let index = getHashIndex element
88+
89+
match this.Contains element with
90+
| false -> false
91+
| _ ->
92+
table.[index] <- removeElement element table.[index]
93+
true

Test/Test.fsproj

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFramework>net7.0</TargetFramework>
5+
<GenerateDocumentationFile>true</GenerateDocumentationFile>
6+
</PropertyGroup>
7+
8+
<ItemGroup>
9+
<Compile Include="Test.fs" />
10+
</ItemGroup>
11+
12+
</Project>

Test/Test.sln

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio Version 16
4+
VisualStudioVersion = 25.0.1706.14
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{F2A71F9B-5D33-465A-A702-920D77279786}") = "Test", "Test.fsproj", "{B0628F5B-4A62-47EF-91B8-A1A5F57329E4}"
7+
EndProject
8+
Project("{F2A71F9B-5D33-465A-A702-920D77279786}") = "Test.Tests", "..\Test.Tests\Test.Tests.fsproj", "{1337E2E9-1A08-4077-A299-AA650691EBD3}"
9+
EndProject
10+
Global
11+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
12+
Debug|Any CPU = Debug|Any CPU
13+
Release|Any CPU = Release|Any CPU
14+
EndGlobalSection
15+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
16+
{B0628F5B-4A62-47EF-91B8-A1A5F57329E4}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
17+
{B0628F5B-4A62-47EF-91B8-A1A5F57329E4}.Debug|Any CPU.Build.0 = Debug|Any CPU
18+
{B0628F5B-4A62-47EF-91B8-A1A5F57329E4}.Release|Any CPU.ActiveCfg = Release|Any CPU
19+
{B0628F5B-4A62-47EF-91B8-A1A5F57329E4}.Release|Any CPU.Build.0 = Release|Any CPU
20+
{1337E2E9-1A08-4077-A299-AA650691EBD3}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
21+
{1337E2E9-1A08-4077-A299-AA650691EBD3}.Debug|Any CPU.Build.0 = Debug|Any CPU
22+
{1337E2E9-1A08-4077-A299-AA650691EBD3}.Release|Any CPU.ActiveCfg = Release|Any CPU
23+
{1337E2E9-1A08-4077-A299-AA650691EBD3}.Release|Any CPU.Build.0 = Release|Any CPU
24+
EndGlobalSection
25+
GlobalSection(SolutionProperties) = preSolution
26+
HideSolutionNode = FALSE
27+
EndGlobalSection
28+
GlobalSection(ExtensibilityGlobals) = postSolution
29+
SolutionGuid = {3DD940C1-FD18-4A1E-9F67-24E61A9D1722}
30+
EndGlobalSection
31+
EndGlobal

0 commit comments

Comments
 (0)