Skip to content

Commit

Permalink
Misc work
Browse files Browse the repository at this point in the history
  • Loading branch information
breki committed Jul 24, 2024
1 parent 4c0e83b commit 29b03d7
Show file tree
Hide file tree
Showing 11 changed files with 228 additions and 104 deletions.
2 changes: 2 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[*.{fs,fsx}]
max_line_length = 80
2 changes: 1 addition & 1 deletion .idea/.idea.Demeton/.idea/watcherTasks.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Demeton.Tests/Demeton.Tests.fsproj
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

<ItemGroup>
<RuntimeHostConfigurationOption Include="System.Globalization.Invariant" Value="true"/>
<Compile Include="Should.fs" />
<Compile Include="TestLog.fs"/>
<Compile Include="TestHelp.fs"/>
<Compile Include="BetterFsCheckRunner.fs"/>
Expand Down
14 changes: 14 additions & 0 deletions Demeton.Tests/Should.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
[<RequireQualifiedAccess>]
module Should

open System

let notBeCalled _ = invalidOp "bug: should not be called"
let notBeCalled2 _ _ = invalidOp "bug: should not be called"
let notBeCalled3 _ _ _ = invalidOp "bug: should not be called"
let notBeCalled4 _ _ _ _ = invalidOp "bug: should not be called"

let fail message = invalidOp message

let reportNotImplemented message =
NotImplementedException message |> raise
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,11 @@ let ``Calling processNextCommand on an empty command stack returns the same stat
|> processNextCommand
localCacheDir
srtmDir
_noCall
_noCall
_noCall2
_noCall2
_noCall2
Should.notBeCalled
Should.notBeCalled
Should.notBeCalled2
Should.notBeCalled2
Should.notBeCalled2

test <@ resultingState = initialState @>

Expand All @@ -56,10 +56,10 @@ let ``When a tile does not exist, puts None in the tiles stack`` () =
localCacheDir
srtmDir
(fun _ -> NotExists)
_noCall
_noCall2
_noCall2
_noCall2
Should.notBeCalled
Should.notBeCalled2
Should.notBeCalled2
Should.notBeCalled2

test <@ resultingState = (initialCommands, None :: initialStackedTiles) @>

Expand All @@ -81,9 +81,9 @@ let ``When a tile is cached, reads it and puts it into the tiles stack`` () =
srtmDir
(fun _ -> Cached)
(expectReadingOfTile tile)
_noCall
_noCall2
_noCall2
Should.notBeCalled
Should.notBeCalled2
Should.notBeCalled2

test
<@
Expand All @@ -107,9 +107,9 @@ let ``If reading of a cache tile fails, put error indicator into the stack``
srtmDir
(fun _ -> Cached)
readingOfTileFails
_noCall
_noCall2
_noCall2
Should.notBeCalled
Should.notBeCalled2
Should.notBeCalled2

test
<@
Expand All @@ -128,10 +128,10 @@ let ``When a level 0 tile is not cached, puts ConvertTileFromHgt command`` () =
localCacheDir
srtmDir
(fun _ -> NotCached)
_noCall
_noCall2
_noCall2
_noCall2
Should.notBeCalled
Should.notBeCalled2
Should.notBeCalled2
Should.notBeCalled2

test
<@
Expand All @@ -152,10 +152,10 @@ let ``When a level > 0 tile is not cached, fills the command stack with children
localCacheDir
srtmDir
(fun _ -> NotCached)
_noCall
_noCall2
_noCall2
_noCall2
Should.notBeCalled
Should.notBeCalled2
Should.notBeCalled2
Should.notBeCalled2

let childTiles =
[| (8, 16); (9, 16); (8, 17); (9, 17) |]
Expand Down Expand Up @@ -192,11 +192,11 @@ let ``When convert from HGT command is received`` () =
|> processNextCommand
localCacheDir
srtmDir
_noCall
_noCall2
Should.notBeCalled
Should.notBeCalled2
convertProducesSomeTile
_noCall2
_noCall2
Should.notBeCalled2
Should.notBeCalled2

test
<@
Expand All @@ -221,11 +221,11 @@ let ``Convert to PNG can fail`` () =
|> processNextCommand
localCacheDir
srtmDir
_noCall
_noCall2
Should.notBeCalled
Should.notBeCalled2
convertThatFails
_noCall2
_noCall2
Should.notBeCalled2
Should.notBeCalled2

let expectedResultingState =
(Failure errorMessage :: initialCommands, initialStackedTiles)
Expand Down Expand Up @@ -264,9 +264,9 @@ let ``When create from lower tiles command is received and create returns tile``
|> processNextCommand
localCacheDir
srtmDir
_noCall
_noCall2
_noCall
Should.notBeCalled
Should.notBeCalled2
Should.notBeCalled
constructParentTileReturnsSomeTile
writeTileToCache

Expand Down Expand Up @@ -311,9 +311,9 @@ let ``When create from lower tiles command is received and create returns None``
|> processNextCommand
localCacheDir
srtmDir
_noCall
_noCall2
_noCall
Should.notBeCalled
Should.notBeCalled2
Should.notBeCalled
constructParentTileReturnsNone
writeTileToCache

Expand Down Expand Up @@ -345,8 +345,8 @@ let ``Testing the tail recursion`` () =
(fun _ -> Cached)
(fun _ _ -> Ok someTileHeights)
(fun _ _ _ -> Ok someTileHeights)
_noCall2
_noCall2
Should.notBeCalled2
Should.notBeCalled2
initialState

// there should be no more commands in the stack
Expand Down Expand Up @@ -390,10 +390,10 @@ let ``Command stack processor should stop on failure and return the error`` () =
localCacheDir
srtmDir
(fun _ -> NotCached)
_noCall2
Should.notBeCalled2
(convertFailsOnSomeCall 4)
_noCall2
_noCall2
Should.notBeCalled2
Should.notBeCalled2
initialState

// there should be an error indicator next in the command stack
Expand Down
2 changes: 1 addition & 1 deletion Demeton.Tests/Srtm/Saving SRTM tiles to cache.fs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ let ``Saves the tile into the cache directory`` () =
cacheDir
ensureCacheDirectoryExists
writeAsPngFile
_noCall
Should.notBeCalled
tile
(Some heights)

Expand Down
36 changes: 18 additions & 18 deletions Demeton.Tests/TestHelp.fs
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,12 @@ open System
open System.Reflection
open System.Runtime.InteropServices

let isLinux() = RuntimeInformation.IsOSPlatform(OSPlatform.Linux)
let isLinux () =
RuntimeInformation.IsOSPlatform(OSPlatform.Linux)

let fail errorMessage =
raise (Xunit.Sdk.XunitException errorMessage)

let _noCall _ = invalidOp "bug: should not be called"
let _noCall2 _ _ = invalidOp "bug: should not be called"
let _noCall3 _ _ _ = invalidOp "bug: should not be called"
let _noCall4 _ _ _ _ = invalidOp "bug: should not be called"

let isOk result =
match result with
| Ok _ -> true
Expand All @@ -31,7 +27,7 @@ let isOkValue expectedOkValue result =
let resultValue result =
match result with
| Ok x -> x
| Error msg ->
| Error msg ->
invalidOp (sprintf "The result indicates an error: '%s'." msg)

let isError (result: Result<'T, 'TError>) =
Expand All @@ -44,26 +40,30 @@ let isErrorData (errorData: 'TError) (result: Result<'T, 'TError>) =
| Ok _ -> false
| Error actualErrorData -> actualErrorData = errorData

let inline (=~=) (x: float) (y: float) = abs (x-y) < 1.E-10
let inline (=~=) (x: float) (y: float) = abs (x - y) < 1.E-10

type ApproxMeasure =
Decimals of int
| Decimals of int
| Percentage of float

let isApproxEqualTo
(controlValue: float) (measure: ApproxMeasure) (actualValue: float) =
let isApproxEqualTo
(controlValue: float)
(measure: ApproxMeasure)
(actualValue: float)
=
match measure with
| Decimals decimals ->
Math.Round(controlValue, decimals)
= Math.Round(actualValue, decimals)
| Decimals decimals ->
Math.Round(controlValue, decimals) = Math.Round(actualValue, decimals)
| Percentage percentage ->
let percentageValue = controlValue * percentage / 100.
Math.Abs(actualValue - controlValue) < percentageValue

/// <summary>
/// Opens a stream for a specified sample resource file.
/// Opens a stream for a specified sample resource file.
/// </summary>
let sampleFileStream sampleFileName =
let assembly = Assembly.GetExecutingAssembly()
assembly.GetManifestResourceStream
("Demeton.Tests.samples." + sampleFileName)

assembly.GetManifestResourceStream(
"Demeton.Tests.samples." + sampleFileName
)
Loading

0 comments on commit 29b03d7

Please sign in to comment.