Skip to content

Commit

Permalink
Improve unit tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
eminencegrs committed Feb 4, 2024
1 parent dd1b4c5 commit 6139157
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 22 deletions.
7 changes: 4 additions & 3 deletions src/FPinFSharp.Exercises/Chapter_02/Section_02_08.fs
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@ module Section_02_08 =
// A closure gives the means of explaining a value that is a function. A closure is a triple: (x, exp, env).
// Where 'x' is an argument identifier,
// 'exp' is the expression to evaluate to get a function value,
// while 'env' is an environment giving bindings to be used in such an evalua- tion.
// while 'env' is an environment giving bindings to be used in such an evaluation.

// The following simple example illustrates the role of the environment in the closure:
let pi = System.Math.PI
let circleArea r = pi * r * r // float -> float
let getPI = System.Math.PI

let circleArea r = getPI * r * r // float -> float
// These declarations bind the identifier 'pi' to a float value and 'circleArea' to a closure:
// pi --> 3.14159
// circleArea --> (r, pi * r * r, [pi --> 3.14159...])
68 changes: 49 additions & 19 deletions src/FPinFSharp.Exercises/Chapter_02/Section_02_12.fs
Original file line number Diff line number Diff line change
Expand Up @@ -59,48 +59,78 @@ module Section_02_12 =
| false -> getMinMaxUInt16 false |> formatNumeric

// Int32
let getInt32 isHex : int32 =
let getInt32 (isHex: bool) : int32 * int32 =
match isHex with
| true -> -0x80000000
| false -> 2147483647
| true -> (-0x80000000, 0x7FFFFFFF)
| false -> (-2147483648, 2147483647)

let getInt32InString (isHex: bool) : string * string =
match isHex with
| true -> getInt32 true |> formatHexadecimal
| false -> getInt32 false |> formatNumeric

// UInt32
let getUInt32 isHex : uint32 =
let getUInt32 (isHex: bool) : uint32 * uint32 =
match isHex with
| true -> (0u, 0xFFFFFFFFu)
| false -> (0u, 4294967295u)

let getUInt32InString (isHex: bool) : string * string =
match isHex with
| true -> 0xFFFFFFFFu
| false -> 4294967295u
| true -> getUInt32 true |> formatHexadecimal
| false -> getUInt32 false |> formatNumeric

// Int64
let getInt64 isHex : int64 =
let getInt64 (isHex: bool) : int64 * int64 =
match isHex with
| true -> (-0x8000000000000000L, 0x7FFFFFFFFFFFFFFFL)
| false -> (-9223372036854775808L, 9223372036854775807L)

let getInt64InString (isHex: bool) : string * string =
match isHex with
| true -> -0x8000000000000000L
| false -> 9223372036854775807L
| true -> getInt64 true |> formatHexadecimal
| false -> getInt64 false |> formatNumeric

// UInt64
let getUInt64 isHex : uint64 =
let getUInt64 (isHex: bool) : uint64 * uint64 =
match isHex with
| true -> 0xFFFFFFFFFFFFFFFFUL
| false -> 18446744073709551615UL
| true -> (0UL, 0xFFFFFFFFFFFFFFFFUL)
| false -> (0UL, 18446744073709551615UL)

let getUInt64InString (isHex: bool) : string * string =
match isHex with
| true -> getUInt64 true |> formatHexadecimal
| false -> getUInt64 false |> formatNumeric

// nativeint
let getNativeInt isHex : nativeint =
let getNativeInt (isHex: bool) : nativeint * nativeint =
match isHex with
| true -> (-0x80000000n, 0x7FFFFFFFn)
| false -> (-2147483648n, 2147483647n)

let getNativeIntInString (isHex: bool) : string * string =
match isHex with
| true -> -0x80000000n
| false -> 2147483647n
| true -> getNativeInt true |> formatHexadecimal
| false -> getNativeInt false |> formatNumeric

// unativeint
let getUNativeInt isHex : unativeint =
let getUNativeInt (isHex: bool) : unativeint * unativeint =
match isHex with
| true -> (0un, 0xFFFFFFFFun)
| false -> (0un, 4294967295un)

let getUNativeIntInString (isHex: bool) : string * string =
match isHex with
| true -> 0xFFFFFFFFun
| false -> 4294967295un
| true -> getUNativeInt true |> formatHexadecimal
| false -> getUNativeInt false |> formatNumeric

// Single
let getMinMaxSingle () : single * single =
(-3.40282346639e+38f, 3.40282346639e+38f)

// Double
let getMinMaxDouble () : double * double =
(-1.79769313486e+308, 1.79769313486e+308)
(-1.7976931348623157e+308, 1.7976931348623157e+308)

// Decimal
let getMinMaxDecimal () : decimal * decimal =
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
namespace FPinFSharp.Exercises.UnitTests.Chapter_02

open System
open System.Numerics
open FPinFSharp.Exercises.Chapter_02.Section_02_12
open Shouldly
open Xunit
Expand Down Expand Up @@ -77,3 +78,51 @@ module Section_02_12_Tests =
let ``GIVEN isHex WHEN getMinMaxUInt16InString THEN result as expected`` (isHex:bool) (expectedResult: string * string) =
let actualResult = getMinMaxUInt16InString isHex
actualResult.ShouldBe(expectedResult)

[<Theory>]
[<InlineData(true, Int32.MinValue, Int32.MaxValue)>]
[<InlineData(false, Int32.MinValue, Int32.MaxValue)>]
let ``GIVEN isHex WHEN getInt32 THEN result as expected`` (isHex:bool) (expectedResult: int32 * int32) =
let actualResult = getInt32 isHex
actualResult.ShouldBe(expectedResult)

[<Theory>]
[<InlineData(true, "0x80000000", "0x7FFFFFFF")>]
[<InlineData(false, "-2147483648", "2147483647")>]
let ``GIVEN isHex WHEN getInt32InString THEN result as expected`` (isHex:bool) (expectedResult: string * string) =
let actualResult = getInt32InString isHex
actualResult.ShouldBe(expectedResult)

[<Theory>]
[<InlineData(true, UInt32.MinValue, UInt32.MaxValue)>]
[<InlineData(false, UInt32.MinValue, UInt32.MaxValue)>]
let ``GIVEN isHex WHEN getUInt32 THEN result as expected`` (isHex:bool) (expectedResult: uint32 * uint32) =
let actualResult = getUInt32 isHex
actualResult.ShouldBe(expectedResult)

[<Theory>]
[<InlineData(true, "0x00", "0xFFFFFFFF")>]
[<InlineData(false, "0", "4294967295")>]
let ``GIVEN isHex WHEN getUInt32InString THEN result as expected`` (isHex:bool) (expectedResult: string * string) =
let actualResult = getUInt32InString isHex
actualResult.ShouldBe(expectedResult)

[<Fact>]
let ``GIVEN WHEN getMinMaxSingle THEN result as expected`` () =
let actualResult = getMinMaxSingle ()
actualResult.ShouldBe((Single.MinValue, Single.MaxValue))

[<Fact>]
let ``GIVEN WHEN getMinMaxDouble THEN result as expected`` () =
let actualResult = getMinMaxDouble ()
actualResult.ShouldBe((Double.MinValue, Double.MaxValue))

[<Fact>]
let ``GIVEN WHEN getMinMaxDecimal THEN result as expected`` () =
let actualResult = getMinMaxDecimal ()
actualResult.ShouldBe((Decimal.MinValue, Decimal.MaxValue))

[<Fact>]
let ``GIVEN WHEN getBigInt THEN result as expected`` () =
let actualResult = getBigInt ()
actualResult.ShouldBe(18446744073709551615I)

Check warning on line 128 in tests/FPinFSharp.Exercises.UnitTests/Chapter_02/Section_02_12_Tests.fs

View workflow job for this annotation

GitHub Actions / build

Main module of program is empty: nothing will happen when it is run

Check warning on line 128 in tests/FPinFSharp.Exercises.UnitTests/Chapter_02/Section_02_12_Tests.fs

View workflow job for this annotation

GitHub Actions / build

Main module of program is empty: nothing will happen when it is run

0 comments on commit 6139157

Please sign in to comment.