Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions ComputerNet/ComputerNet.Tests/ComputerNet.Tests.fsproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>

<IsPackable>false</IsPackable>
<GenerateProgramFile>false</GenerateProgramFile>
<IsTestProject>true</IsTestProject>
</PropertyGroup>

<ItemGroup>
<Compile Include="UnitTest.fs" />
<Compile Include="Program.fs" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.9.0" />
<PackageReference Include="NUnit" Version="4.1.0" />
<PackageReference Include="NUnit3TestAdapter" Version="4.5.0" />
<PackageReference Include="NUnit.Analyzers" Version="4.2.0"><IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="coverlet.collector" Version="6.0.2"><IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="FsCheck" Version="2.16.6">
<GeneratePathProperty></GeneratePathProperty>
</PackageReference>
<PackageReference Include="FsUnit" Version="6.0.0">
<GeneratePathProperty></GeneratePathProperty>
</PackageReference>
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\ComputerNet\ComputerNet.fsproj" />
</ItemGroup>
</Project>
5 changes: 5 additions & 0 deletions ComputerNet/ComputerNet.Tests/Program.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module Program =

[<EntryPoint>]
let main _ = 0

68 changes: 68 additions & 0 deletions ComputerNet/ComputerNet.Tests/UnitTest.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
module ComputerNet.Tests

open NUnit.Framework
open FsUnit
open ComputerNet.Computer
open ComputerNet.Net

let ``adjacency matrix`` =
seq {
TestCaseData([|
[| 0; 0; 0; 1; 1 |]
[| 1; 0; 0; 0; 0|]
[| 1; 0; 0; 0; 0|]
[| 0; 0; 1; 0; 0|]
[| 0; 1; 0; 0; 0|]
|]
)
}

let ``adjacency matrix detached case`` =
seq {
TestCaseData([|
[| 0; 0; 0; 1; 0 |]
[| 0; 0; 0; 0; 0|]
[| 1; 0; 0; 0; 0|]
[| 0; 0; 1; 0; 0|]
[| 0; 1; 0; 0; 0|]
|]
)
}
let ``check mac adresses`` correct result = List.map (fun (c : Computer) -> c.macAdress) result |> should equal correct

[<TestCaseSource(nameof(``adjacency matrix detached case``))>]
let ``detached computers should't be infected`` (matrix : array<array<int>>) =
let computers = [ Computer(1, "Linux", 1.0, true);
Computer(2, "Linux", 1.0, false);
Computer(3, "Windows", 1.0, false);
Computer(4, "Linux", 1.0, false);
Computer(5, "macOS", 1.0, false)]
let net = Net(computers, matrix)
net.SpreadVirus() |> ignore
net.SpreadVirus() |> ignore
computers |> List.filter (fun (c : Computer) -> not c.isDeseased) |>
``check mac adresses`` [ 2; 5; ]

[<TestCaseSource(nameof(``adjacency matrix``))>]
let ``all net with probability 1 should spread virus in bfs way`` (matrix : array<array<int>>) =
let computers = [ Computer(1, "Linux", 1.0, true);
Computer(2, "Linux", 1.0, false);
Computer(3, "Windows", 1.0, false);
Computer(4, "Linux", 1.0, false);
Computer(5, "macOS", 1.0, false)]
let net = Net(computers, matrix)
let infectedComputer = net.GetDeseasedComputers().[0]
infectedComputer.SpreadVirus()
net.GetDeseasedComputers() |> ``check mac adresses`` [ 1; 4; 5;]
net.SpreadVirus() |> should equal true

[<TestCaseSource(nameof(``adjacency matrix``))>]
let ``net with probabilities only 0 shouldn't have any deseased computers``(matrix : array<array<int>>) =
let computers = [ Computer(1, "Linux", 0.0, true);
Computer(2, "Linux", 0.0, false);
Computer(3, "Windows", 0.0, false);
Computer(4, "Linux", 0.0, false);
Computer(5, "macOS", 0.0, false)]
let net = Net(computers, matrix)
net.SpreadVirus() |> should equal true
net.GetDeseasedComputers() |> ``check mac adresses`` [ 1; ]
31 changes: 31 additions & 0 deletions ComputerNet/ComputerNet.sln
Original file line number Diff line number Diff line change
@@ -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}") = "ComputerNet", "ComputerNet\ComputerNet.fsproj", "{C81B6B32-DFBF-4F73-8457-2247AB4081EC}"
EndProject
Project("{F2A71F9B-5D33-465A-A702-920D77279786}") = "ComputerNet.Tests", "ComputerNet.Tests\ComputerNet.Tests.fsproj", "{C4BE4761-4777-496C-8440-29E18A942A22}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{C81B6B32-DFBF-4F73-8457-2247AB4081EC}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{C81B6B32-DFBF-4F73-8457-2247AB4081EC}.Debug|Any CPU.Build.0 = Debug|Any CPU
{C81B6B32-DFBF-4F73-8457-2247AB4081EC}.Release|Any CPU.ActiveCfg = Release|Any CPU
{C81B6B32-DFBF-4F73-8457-2247AB4081EC}.Release|Any CPU.Build.0 = Release|Any CPU
{C4BE4761-4777-496C-8440-29E18A942A22}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{C4BE4761-4777-496C-8440-29E18A942A22}.Debug|Any CPU.Build.0 = Debug|Any CPU
{C4BE4761-4777-496C-8440-29E18A942A22}.Release|Any CPU.ActiveCfg = Release|Any CPU
{C4BE4761-4777-496C-8440-29E18A942A22}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {B87D30DF-6E22-48A9-B465-1D46905AFF02}
EndGlobalSection
EndGlobal
40 changes: 40 additions & 0 deletions ComputerNet/ComputerNet/Computer.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
namespace ComputerNet

open System

module Computer =

type Computer (macAdress : int, OS : string,
infectionProbability : float, isDeseasedArg : bool) =

let mutable neighbours : Computer list = []

member val macAdress = macAdress with get

member val OS = OS with get

member val infectionProbability = infectionProbability with get

member val isDeseased = isDeseasedArg with get, set

static member random = new System.Random()

member computer.addNeighbour (neighbour : Computer) =
neighbours <- neighbour :: neighbours

member computer.getDeseased () =

let number = Computer.random.Next(0, 100)

let isDeseased = infectionProbability |> (*) 100.0 |> int |> (-) number |> (>) 0

computer.isDeseased <- isDeseased

member computer.SpreadVirus () =

List.iter (fun (neighbour : Computer) -> (neighbour.getDeseased())) neighbours

member computer.checkIfAllNeighboursAreDeseased () =

List.forall (fun (neigbour : Computer) ->
(neigbour.isDeseased || neigbour.infectionProbability = 0.0)) neighbours
14 changes: 14 additions & 0 deletions ComputerNet/ComputerNet/ComputerNet.fsproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net7.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
<Compile Include="Program.fs" />
<Compile Include="Computer.fs" />
<Compile Include="Net.fs" />
</ItemGroup>

</Project>
36 changes: 36 additions & 0 deletions ComputerNet/ComputerNet/Net.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
namespace ComputerNet

open System
open ComputerNet.Computer

module Net =
type Net (computers : Computer list, adjacencyMatrix : int[][]) =

let computers = computers

let adjacencyMatrix = adjacencyMatrix

do
for i = 0 to computers.Length - 1 do
for j = 0 to computers.Length - 1 do
if adjacencyMatrix.[i].[j] = 1 then
computers.[i].addNeighbour(computers.[j]) |> ignore


member val IsAllComputersDeseased = false

member this.GetDeseasedComputers () =

List.filter (fun (computer : Computer) -> computer.isDeseased) computers

member this.SpreadVirus () =

if this.IsAllComputersDeseased then true
else
if (List.length (this.GetDeseasedComputers()) = List.length computers) then
true
else
let isAllNeigboursDeseased = true
let deseased = this.GetDeseasedComputers()
List.iter (fun (computer : Computer) -> computer.SpreadVirus()) deseased
List.forall (fun (computer : Computer) -> computer.checkIfAllNeighboursAreDeseased()) deseased
1 change: 1 addition & 0 deletions ComputerNet/ComputerNet/Program.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
namespace ComputerNet