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
16 changes: 13 additions & 3 deletions appveyor.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,17 @@
image: Visual Studio 2017
image: Visual Studio 2019

init:
- git config --global core.autocrlf true

environment:
matrix:
- solution: Semester4/HomeworkTwo/HomeworkTwo.sln

before_build:
- nuget restore semester2/6.1/HW6T2.sln
- nuget restore %solution%

build:
project: semester2/2.3/2.3.sln
project: $(solution)

test_script:
- dotnet test %solution%
46 changes: 46 additions & 0 deletions semester4/HomeworkTwo/HomeworkTwo.Tests/CountsTests.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
module HomeworkTwo.Tests.Counts

open FsUnit
open FsCheck
open Counts
open NUnit.Framework

let inputs =
[
[1..10], 5
[3], 0
[2], 1
[2; 4; 6; 8; 10], 5
[1; 3; 5; 7; 9; 11; 13], 0
[-1; 0; 1], 1
[-2], 1
[-4; -10; 241212], 3
[], 0
] |> List.map (fun (list, result) -> TestCaseData(list, result))

[<TestCaseSource("inputs")>]
[<Test>]
let filterTest list count = filterCount list |> should equal count

[<TestCaseSource("inputs")>]
[<Test>]
let foldTest list count = foldCount list |> should equal count

[<TestCaseSource("inputs")>]
[<Test>]
let mapTest list count = mapCount list |> should equal count

let filterEqualsMap list = filterCount list = mapCount list

let foldEqualsMap list = foldCount list = mapCount list

let filterEqualsFold list = filterCount list = foldCount list

[<Test>]
let ``filter equals map``() = Check.QuickThrowOnFailure filterEqualsMap

[<Test>]
let ``fold equals map``() = Check.QuickThrowOnFailure foldEqualsMap

[<Test>]
let ``filter equals fold``() = Check.QuickThrowOnFailure filterEqualsFold
30 changes: 30 additions & 0 deletions semester4/HomeworkTwo/HomeworkTwo.Tests/HomeworkTwo.Tests.fsproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>

<IsPackable>false</IsPackable>
<GenerateProgramFile>false</GenerateProgramFile>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="FsCheck" Version="3.0.0-alpha4" />
<PackageReference Include="FsUnit" Version="4.0.0" />
<PackageReference Include="nunit" Version="3.12.0" />
<PackageReference Include="NUnit3TestAdapter" Version="3.15.1" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.4.0" />
</ItemGroup>

<ItemGroup>
<Compile Include="CountsTests.fs" />
<Compile Include="TreeMapTests.fs" />
<Compile Include="ParseTreeTests.fs" />
<Compile Include="InfinitePrimeNumbersTests.fs" />
<Compile Include="Program.fs" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\HomeworkTwo\HomeworkTwo.fsproj" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
module HomeworkTwo.Tests.InfinitePrimeNumbersTests

open NUnit.Framework
open PrimeSequence
open FsUnit

let cases =
[
2, 0
3, 1
5, 2
7, 3
11, 4
13, 5
17, 6
19, 7
23, 8
] |> List.map (fun (number, pos) -> TestCaseData(number, pos))

[<TestCaseSource("cases")>]
[<Test>]
let infinitePrimesTest number pos =
Seq.item(pos) primeSeq |> should equal number
24 changes: 24 additions & 0 deletions semester4/HomeworkTwo/HomeworkTwo.Tests/ParseTreeTests.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
module HomeworkTwo.Tests.ParseTree_Tests

open NUnit.Framework
open FsUnit
open ParseTree

let cases =
[
Sum(Digit(100), Digit(32)), 132
Multiplication(Subtraction(Digit(50), Digit(3)), Digit(2)), 94
Multiplication(Digit(5000), Digit(0)), 0
Multiplication(Digit(5000), Digit(-1)), -5000
Division(Digit(10), Digit(2)), 5
Subtraction(Multiplication(Digit(13), Digit(5)), Division(Digit(25), Digit(5))), 60
] |> List.map (fun (tree, result) -> TestCaseData(tree, result))


[<TestCaseSource("cases")>]
[<Test>]
let ``simple parse tree testes`` tree result = calculateTree tree |> should equal result

[<Test>]
let ``division by zero test`` () =
(fun () -> calculateTree (Division(Digit(10), Digit(0))) |> ignore) |> should throw typeof<System.DivideByZeroException>
2 changes: 2 additions & 0 deletions semester4/HomeworkTwo/HomeworkTwo.Tests/Program.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
module Program
let [<EntryPoint>] main _ = 0

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Тут тоже, пустой main вполлне можно не писать, если собирать библиотеку

17 changes: 17 additions & 0 deletions semester4/HomeworkTwo/HomeworkTwo.Tests/TreeMapTests.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
module HomeworkTwo.Tests.TreeMap

open NUnit.Framework
open FsUnit
open TreeMap

let intTree = (Node(5, Node(3, None, None), Node(0, Node(313, Tree.None, Tree.None), Tree.None)))
let intAnswer = (Node(10, Node(6, None, None), Node(0, Node(626, Tree.None, Tree.None), Tree.None)))

[<Test>]
let intTest () = mapTree (fun (x: int) -> (x * 2)) intTree |> should equal intAnswer

let stringTree = (Node("Hello", Node("World", Node("Another", Node("Word", Tree.None, Tree.None), Tree.None), Tree.None), Tree.None))
let stringAnswer = (Node("Helloa", Node("Worlda", Node("Anothera", Node("Worda", Tree.None, Tree.None), Tree.None), Tree.None), Tree.None))

[<Test>]
let stringTest () = mapTree (fun (x: string) -> x + "a") stringTree |> should equal stringAnswer
22 changes: 22 additions & 0 deletions semester4/HomeworkTwo/HomeworkTwo.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@

Microsoft Visual Studio Solution File, Format Version 12.00
Project("{F2A71F9B-5D33-465A-A702-920D77279786}") = "HomeworkTwo", "HomeworkTwo\HomeworkTwo.fsproj", "{0C6E1509-6286-49C0-8923-48C031618E87}"
EndProject
Project("{F2A71F9B-5D33-465A-A702-920D77279786}") = "HomeworkTwo.Tests", "HomeworkTwo.Tests\HomeworkTwo.Tests.fsproj", "{6AA3EB61-29BD-4819-BB4B-0DA0027156A6}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{0C6E1509-6286-49C0-8923-48C031618E87}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{0C6E1509-6286-49C0-8923-48C031618E87}.Debug|Any CPU.Build.0 = Debug|Any CPU
{0C6E1509-6286-49C0-8923-48C031618E87}.Release|Any CPU.ActiveCfg = Release|Any CPU
{0C6E1509-6286-49C0-8923-48C031618E87}.Release|Any CPU.Build.0 = Release|Any CPU
{6AA3EB61-29BD-4819-BB4B-0DA0027156A6}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{6AA3EB61-29BD-4819-BB4B-0DA0027156A6}.Debug|Any CPU.Build.0 = Debug|Any CPU
{6AA3EB61-29BD-4819-BB4B-0DA0027156A6}.Release|Any CPU.ActiveCfg = Release|Any CPU
{6AA3EB61-29BD-4819-BB4B-0DA0027156A6}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
EndGlobal
13 changes: 13 additions & 0 deletions semester4/HomeworkTwo/HomeworkTwo/Count.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
module Counts

/// Count all even numbers with filter function
let filterCount list =
List.length <| List.filter (fun x -> x % 2 = 0) list

/// Count all even numbers with map function
let mapCount list =
List.sum <| List.map (fun x -> abs(x + 1) % 2) list

/// Count all even numbers with fold functions
let foldCount list =
List.fold (fun acc x -> (abs(x + 1) % 2) + acc) 0 list
14 changes: 14 additions & 0 deletions semester4/HomeworkTwo/HomeworkTwo/HomeworkTwo.fsproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
</PropertyGroup>

<ItemGroup>
<Compile Include="Count.fs" />
<Compile Include="TreeMap.fs" />
<Compile Include="ParseTree.fs" />
<Compile Include="InfinitePrimeNumbers.fs" />
</ItemGroup>

</Project>
11 changes: 11 additions & 0 deletions semester4/HomeworkTwo/HomeworkTwo/InfinitePrimeNumbers.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
module PrimeSequence

/// Checks if number is prime
let isPrime n =
if n < 2 then false
else
let searchEnd = (float >> sqrt >> int) n
List.forall (fun x -> n % x <> 0)([2..searchEnd])

/// Creates infinity sequence of prime numbers
let primeSeq = (Seq.initInfinite id |> Seq.filter (isPrime))
20 changes: 20 additions & 0 deletions semester4/HomeworkTwo/HomeworkTwo/ParseTree.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
module ParseTree

/// Simple parse tree
type ParseTree =
| Digit of int
| Sum of ParseTree * ParseTree
| Subtraction of ParseTree * ParseTree
| Multiplication of ParseTree * ParseTree
| Division of ParseTree * ParseTree

/// Function that calculates the result of parse tree
let rec calculateTree tree =
match tree with
| Digit x -> x
| Sum(l, r) -> calculateTree l + calculateTree r
| Subtraction(l, r) -> calculateTree l - calculateTree r
| Multiplication(l, r) -> calculateTree l * calculateTree r
| Division(l, r) -> let right = calculateTree r
if right = 0 then raise (System.DivideByZeroException())
else (calculateTree l) / right
12 changes: 12 additions & 0 deletions semester4/HomeworkTwo/HomeworkTwo/TreeMap.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
module TreeMap

/// Simple binary tree
type Tree<'a> =
| Node of 'a * Tree<'a> * Tree<'a>
| None

/// Function that transforms tree with given function
let rec mapTree mapFunc tree =
match tree with
| None -> None
| Node(c, l, r) -> Node(mapFunc c, mapTree mapFunc l, mapTree mapFunc r)