Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Release-Candidate committed Apr 24, 2021
1 parent a31cdc3 commit b908b67
Show file tree
Hide file tree
Showing 15 changed files with 454 additions and 122 deletions.
6 changes: 0 additions & 6 deletions src/Tzolkin.Android/Tzolkin.Android.fsproj
Original file line number Diff line number Diff line change
Expand Up @@ -183,15 +183,9 @@
<Reference Include="Fabulous">
<HintPath>..\..\packages\Fabulous.1.0.1\lib\netstandard2.0\Fabulous.dll</HintPath>
</Reference>
<Reference Include="Fabulous.LiveUpdate">
<HintPath>..\..\packages\Fabulous.LiveUpdate.1.0.1\lib\netstandard2.0\Fabulous.LiveUpdate.dll</HintPath>
</Reference>
<Reference Include="Fabulous.XamarinForms">
<HintPath>..\..\packages\Fabulous.XamarinForms.1.0.1\lib\netstandard2.0\Fabulous.XamarinForms.dll</HintPath>
</Reference>
<Reference Include="Fabulous.XamarinForms.LiveUpdate">
<HintPath>..\..\packages\Fabulous.XamarinForms.LiveUpdate.1.0.1\lib\netstandard2.0\Fabulous.XamarinForms.LiveUpdate.dll</HintPath>
</Reference>
<Reference Include="Fabulous.XamarinForms.SkiaSharp">
<HintPath>..\..\packages\Fabulous.XamarinForms.SkiaSharp.1.0.1\lib\netstandard2.0\Fabulous.XamarinForms.SkiaSharp.dll</HintPath>
</Reference>
Expand Down
2 changes: 0 additions & 2 deletions src/Tzolkin.Android/packages.config
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="Fabulous" version="1.0.1" targetFramework="monoandroid11.0" />
<package id="Fabulous.LiveUpdate" version="1.0.1" targetFramework="monoandroid11.0" />
<package id="Fabulous.XamarinForms" version="1.0.1" targetFramework="monoandroid11.0" />
<package id="Fabulous.XamarinForms.LiveUpdate" version="1.0.1" targetFramework="monoandroid11.0" />
<package id="Fabulous.XamarinForms.SkiaSharp" version="1.0.1" targetFramework="monoandroid11.0" />
<package id="Fizzler" version="1.3.0" targetFramework="monoandroid11.0" />
<package id="FSharp.Core" version="5.0.1" targetFramework="monoandroid11.0" />
Expand Down
6 changes: 0 additions & 6 deletions src/Tzolkin.iOS/Tzolkin.iOS.fsproj
Original file line number Diff line number Diff line change
Expand Up @@ -149,15 +149,9 @@
<Reference Include="Fabulous">
<HintPath>..\..\packages\Fabulous.1.0.1\lib\netstandard2.0\Fabulous.dll</HintPath>
</Reference>
<Reference Include="Fabulous.LiveUpdate">
<HintPath>..\..\packages\Fabulous.LiveUpdate.1.0.1\lib\netstandard2.0\Fabulous.LiveUpdate.dll</HintPath>
</Reference>
<Reference Include="Fabulous.XamarinForms">
<HintPath>..\..\packages\Fabulous.XamarinForms.1.0.1\lib\netstandard2.0\Fabulous.XamarinForms.dll</HintPath>
</Reference>
<Reference Include="Fabulous.XamarinForms.LiveUpdate">
<HintPath>..\..\packages\Fabulous.XamarinForms.LiveUpdate.1.0.1\lib\netstandard2.0\Fabulous.XamarinForms.LiveUpdate.dll</HintPath>
</Reference>
<Reference Include="Fabulous.XamarinForms.SkiaSharp">
<HintPath>..\..\packages\Fabulous.XamarinForms.SkiaSharp.1.0.1\lib\netstandard2.0\Fabulous.XamarinForms.SkiaSharp.dll</HintPath>
</Reference>
Expand Down
2 changes: 0 additions & 2 deletions src/Tzolkin.iOS/packages.config
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="Fabulous" version="1.0.1" targetFramework="xamarinios10" />
<package id="Fabulous.LiveUpdate" version="1.0.1" targetFramework="xamarinios10" />
<package id="Fabulous.XamarinForms" version="1.0.1" targetFramework="xamarinios10" />
<package id="Fabulous.XamarinForms.LiveUpdate" version="1.0.1" targetFramework="xamarinios10" />
<package id="Fabulous.XamarinForms.SkiaSharp" version="1.0.1" targetFramework="xamarinios10" />
<package id="Fizzler" version="1.3.0" targetFramework="xamarinios10" />
<package id="FSharp.Core" version="5.0.1" targetFramework="xamarinios10" />
Expand Down
2 changes: 0 additions & 2 deletions src/Tzolkin/TzolkinApp.fsproj
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,7 @@

<ItemGroup>
<PackageReference Include="Fabulous" Version="1.0.1" />
<PackageReference Include="Fabulous.LiveUpdate" Version="1.0.1" />
<PackageReference Include="Fabulous.XamarinForms" Version="1.0.1" />
<PackageReference Include="Fabulous.XamarinForms.LiveUpdate" Version="1.0.1" />
<PackageReference Include="Fabulous.XamarinForms.SkiaSharp" Version="1.0.1" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
<PackageReference Include="Svg.Skia" Version="0.5.0" />
Expand Down
2 changes: 1 addition & 1 deletion src/TzolkinDate/TzolkinDate.fs
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,7 @@ module TzolkinDate =
/// `tzolkinDate1` = 3 Kawak
/// returns 250 for `tzolkinDate2` = 8 Chuwen and
/// `tzolkinDate1` = 11 Imix
static member (-)(tzolkinDate1, tzolkinDate2) =
static member ( - )(tzolkinDate1, tzolkinDate2) =
let day2 = toInt tzolkinDate2
let day1 = toInt tzolkinDate1

Expand Down
6 changes: 3 additions & 3 deletions src/TzolkinDate/TzolkinDate.fsproj
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,15 @@
<NeutralLanguage>es</NeutralLanguage>
<PackageLicenseExpression>MIT</PackageLicenseExpression>
<RepositoryUrl>https://github.com/Release-Candidate/Tzolkin</RepositoryUrl>
<Version>0.9.16</Version>
<Version>0.9.17</Version>
<Authors>Roland Csaszar</Authors>
<Company>Release-Candidate</Company>
<Product>Tzolkin</Product>
<Copyright>(c) 2021 Roland Csaszar</Copyright>
<PackageIcon>nuget_icon.png</PackageIcon>
<PackageTags>Maya Tzolkin calendar calculator converter</PackageTags>
<PackageTags>F# Maya Tzolkin calendar calculator converter</PackageTags>
<PackageReleaseNotes>Initial Release</PackageReleaseNotes>
<Description>A library to convert and calculate with MayaTzolk’in dates and gregorian dates.</Description>
<Description>A F# library to convert and calculate with MayaTzolk’in dates and gregorian dates.</Description>
</PropertyGroup>
<ItemGroup>
<Compile Include="Generics.fs" />
Expand Down
7 changes: 6 additions & 1 deletion src/TzolkinDate/TzolkinGlyph.fs
Original file line number Diff line number Diff line change
Expand Up @@ -365,7 +365,12 @@ module TzolkinGlyph =
type T with
/// Subtract two `TzolkinGlyph`.
static member ( - ) (tz1:T, tz2:T) =
(int tz1) - (int tz2)
let day1 = int tz1
let day2 = int tz2
if day1 > day2 then
day1 - day2
else
20 - day2 + day1

/// Convert the given Gregorian date `gregorian` to a Tzolk’in day glyph.
///
Expand Down
7 changes: 6 additions & 1 deletion src/TzolkinDate/TzolkinNumber.fs
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,12 @@ module TzolkinNumber =
type T with
/// Subtract two `TzolkinNumber`.
static member ( - ) (tz1:T, tz2:T) =
(int tz1) - (int tz2)
let day1 = int tz1
let day2 = int tz2
if day1 > day2 then
day1 - day2
else
13 - day2 + day1

/// Convert the given Gregorian date `gregorian` to a Tzolk’in day number.
///
Expand Down
177 changes: 177 additions & 0 deletions tests/TestTzolkin/Generic.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,177 @@
// SPDX-License-Identifier: MIT
// Copyright (C) 2021 Roland Csaszar
//
// Project: TestTzolkin
// File: Generic.fs
// Date: 4/24/2021 1:11:17 PM
//==============================================================================


namespace TestTzolkin

open Expecto
open Swensen.Unquote
open System
open FsCheck
open Expecto.Logging

[<AutoOpen>]
module Generic=

let logger = Log.create "Tzolkin"

let config = { FsCheckConfig.defaultConfig with
maxTest = 100000
endSize = 1000000
receivedArgs = fun _ name no args ->
logger.infoWithBP (
Message.eventX "For {test} {no}, generated {args}"
>> Message.setField "test" name
>> Message.setField "no" no
>> Message.setField "args" args)}

let configList = { config with
maxTest = 25
endSize = 10000 }

// Rules to calculate with `TzolkinNumber` and `TzolkinGlyph`. =================================

let inline testCommutativity constructor i days =
let tzolkin = constructor i
match tzolkin with
| None -> test <@ i < 1 @>
| Some tz1 -> test <@ tz1 + days = days + tz1 @>

let inline testCommutativityDate constructor i j days =
let tzolkin = constructor i j
match tzolkin with
| None -> test <@ i < 1 || j < 1 @>
| Some tz1 -> test <@ tz1 + days = days + tz1 @>

let inline testNeutralElement constructor i neutral =
let tzolkin = constructor i
match tzolkin with
| None -> test <@ i < 1 @>
| Some tz1 -> test <@ tz1 + neutral = tz1 @>

let inline testNeutralElementDate constructor i j neutral =
let tzolkin = constructor i j
match tzolkin with
| None -> test <@ i < 1 || j < 1 @>
| Some tz1 -> test <@ tz1 + neutral = tz1 @>

let inline testAssociativity constructor i days1 days2 =
let tzolkin = constructor i
match tzolkin with
| None -> test <@ i < 1 @>
| Some tz1 -> test <@ (tz1 + days1) + days2 = tz1 + (days1 + days2) @>

let inline testAssociativityDate constructor i j days1 days2 =
let tzolkin = constructor i j
match tzolkin with
| None -> test <@ i < 1 || j < 1 @>
| Some tz1 -> test <@ (tz1 + days1) + days2 = tz1 + (days1 + days2) @>

let inline testIntTimeSpanSame constructor i daysI daysTS =
let tzolkin = constructor i
match tzolkin with
| None -> test <@ i < 1 @>
| Some tz1 -> test <@ tz1 + daysI = tz1 + daysTS @>

let inline testIntTimeSpanSameDate constructor i j daysI daysTS =
let tzolkin = constructor i j
match tzolkin with
| None -> test <@ i < 1 || j < 1 @>
| Some tz1 -> test <@ tz1 + daysI = tz1 + daysTS @>

let inline testSubtraction constructor i days modulo =
let tzolkin = constructor i
match tzolkin with
| None -> test <@ i < 1 @>
| Some tz1 ->
let modDays = modulo days
let tz2 = tz1 + modDays
test <@ tz2 - tz1 = modDays @>

let inline testSubtractionDate constructor i j days modulo =
let tzolkin = constructor i j
match tzolkin with
| None -> test <@ i < 1 || j < 1 @>
| Some tz1 ->
let modDays = modulo days
let tz2 = tz1 + modDays
test <@ tz2 - tz1 = modDays @>

let inline addTimespan (tzolkin:^T) days =
(^T: (static member ( + ) : ^T * TimeSpan -> ^T) tzolkin, (TimeSpan.FromDays (float days)) )

let inline addToTimespan days (tzolkin: ^T) =
(^T: (static member ( + ) : TimeSpan * ^T -> ^T) (TimeSpan.FromDays (float days)), tzolkin)

// date calculations ===========================================================================

let inline stringToDate refString =
let formatProvider = System.Globalization.DateTimeFormatInfo.InvariantInfo
System.DateTime.ParseExact (refString, "dd.MM.yyyy", formatProvider)

let inline stringList2DateList constructor list =
List.map (fun (date, tzolkin) -> stringToDate date, constructor tzolkin) list

let inline testFromDate fromDate referenceDates =
let testFromDateHelper (fromDate: DateTime -> 'T) referenceDate date =
test <@ fromDate date = referenceDate @>

List.iter (fun elem ->
let date, tzolkin = elem
testFromDateHelper fromDate tzolkin date) referenceDates

let inline testNextDate nextDate cycleLength referenceDates isLast =
let testNextDateHelper2 nextDate cycleLength referenceDate days isLast =
let (date:DateTime), tzolkin = referenceDate
let start = date + TimeSpan.FromDays (float days)
if isLast then
test <@ nextDate tzolkin start = date @>
else
test <@ nextDate tzolkin start = date + TimeSpan.FromDays (float cycleLength) @>

let testNextDateHelper1 nextDate cycleLength referenceDate isLast=
if isLast then
Gen.choose (1, cycleLength) |> Gen.sample 0 100
else
Gen.choose (0, cycleLength - 1) |> Gen.sample 0 100
|> List.iter (fun i -> testNextDateHelper2 nextDate cycleLength referenceDate i isLast)

referenceDates
|> List.iter (fun elem -> testNextDateHelper1 nextDate cycleLength elem isLast)


let inline testNextList nextDateList numDates cycleLength referenceDates isLast =
let rec testList list difference =
match list with
| head :: (head2 :: tail) ->
test <@ head2 - head = TimeSpan.FromDays difference @>
testList (head2 :: tail) difference
| [_] -> ()
| [] -> ()

let testNextListHelper2 numDates nextDateList cycleLength referenceDate days isLast =
let (date:DateTime), tzolkin = referenceDate
let start = date + TimeSpan.FromDays (float days)
let listToTest: DateTime list = nextDateList numDates tzolkin start
if isLast then
test <@ listToTest.Head = date @>
testList listToTest (float -cycleLength)
else
test <@ listToTest.Head = date + TimeSpan.FromDays (float cycleLength) @>
testList listToTest (float cycleLength)


let testNextListHelper1 numDates nextDateList cycleLength referenceDate isLast =
Gen.choose (0, cycleLength - 1) |> Gen.sample 0 10
|> List.iter (fun i -> testNextListHelper2 numDates nextDateList cycleLength referenceDate i isLast)

referenceDates
|> List.iter (fun elem -> testNextListHelper1 numDates nextDateList cycleLength elem isLast)



18 changes: 10 additions & 8 deletions tests/TestTzolkin/TestMain.fs
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,16 @@
//
//==============================================================================

/// Test module containing the main entry point for the `standalone` tests, when
/// called by `dotnet run` instead of `dotnet test`.
module Tests.TestMain
namespace TestTzolkin

open Expecto

/// Main entry point of the tests, if called by `dotnet run` instead of
/// `dotnet test`.
[<EntryPoint>]
let main argv =
Tests.runTestsInAssembly defaultConfig argv
/// Test module containing the main entry point for the `standalone` tests, when
/// called by `dotnet run` instead of `dotnet test`.
module TestMain=

/// Main entry point of the tests, if called by `dotnet run` instead of
/// `dotnet test`.
[<EntryPoint>]
let main argv =
Tests.runTestsInAssembly defaultConfig argv
1 change: 1 addition & 0 deletions tests/TestTzolkin/TestTzolkin.fsproj
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
<WarningLevel>5</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Compile Include="Generic.fs" />
<Compile Include="TestTzolkinNumber.fs" />
<Compile Include="TestTzolkinGlyph.fs" />
<Compile Include="TestTzolkinDate.fs" />
Expand Down

0 comments on commit b908b67

Please sign in to comment.