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 6139157 commit f3207e1
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 13 deletions.
6 changes: 4 additions & 2 deletions src/FPinFSharp.Exercises/Chapter_02/Section_02_12.fs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@ module Section_02_12 =
// Helpers.
let formatNumeric (values: 'T * 'T) : string * string =
System.String.Format("{0}", fst values), System.String.Format("{0}", snd values)

let formatHexadecimal (values: 'T * 'T) : string * string =
System.String.Format("0x{0:X2}", fst values), System.String.Format("0x{0:X2}", snd values)
if fst values < Unchecked.defaultof<'T>
then System.String.Format("-0x{0:X2}", fst values), System.String.Format("0x{0:X2}", snd values)
else System.String.Format("0x{0:X2}", fst values), System.String.Format("0x{0:X2}", snd values)

// Char
let convertIntToChar (x: int) : char =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ module Section_02_12_Tests =
actualResult.ShouldBe(expectedResult)

[<Theory>]
[<InlineData(true, "0x80", "0x7F")>]
[<InlineData(true, "-0x80", "0x7F")>]
[<InlineData(false, "-128", "127")>]
let ``GIVEN isHex WHEN getMinMaxSByteInString THEN result as expected`` (isHex:bool) (expectedResult: string * string) =
let actualResult = getMinMaxSByteInString isHex
Expand All @@ -59,7 +59,7 @@ module Section_02_12_Tests =
actualResult.ShouldBe(expectedResult)

[<Theory>]
[<InlineData(true, "0x8000", "0x7FFF")>]
[<InlineData(true, "-0x8000", "0x7FFF")>]
[<InlineData(false, "-32768", "32767")>]
let ``GIVEN isHex WHEN getMinMaxInt16InString THEN result as expected`` (isHex:bool) (expectedResult: string * string) =
let actualResult = getMinMaxInt16InString isHex
Expand Down Expand Up @@ -87,7 +87,7 @@ module Section_02_12_Tests =
actualResult.ShouldBe(expectedResult)

[<Theory>]
[<InlineData(true, "0x80000000", "0x7FFFFFFF")>]
[<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
Expand All @@ -107,22 +107,50 @@ module Section_02_12_Tests =
let actualResult = getUInt32InString isHex
actualResult.ShouldBe(expectedResult)

[<Theory>]
[<InlineData(true, -0x8000000000000000L, 0x7FFFFFFFFFFFFFFFL)>]
[<InlineData(false, -9223372036854775808L, 9223372036854775807L)>]
let ``GIVEN isHex WHEN getInt64 THEN result as expected`` (isHex:bool) (expectedResult: int64 * int64) =
let actualResult = getInt64 isHex
actualResult.ShouldBe(expectedResult)

[<Theory>]
[<InlineData(true, "-0x8000000000000000", "0x7FFFFFFFFFFFFFFF")>]
[<InlineData(false, "-9223372036854775808", "9223372036854775807")>]
let ``GIVEN isHex WHEN getInt64InString THEN result as expected`` (isHex:bool) (expectedResult: string * string) =
let actualResult = getInt64InString isHex
actualResult.ShouldBe(expectedResult)

[<Theory>]
[<InlineData(true, 0UL, 0xFFFFFFFFFFFFFFFFUL)>]
[<InlineData(false, 0UL, 18446744073709551615UL)>]
let ``GIVEN isHex WHEN getUInt64 THEN result as expected`` (isHex:bool) (expectedResult: uint64 * uint64) =
let actualResult = getUInt64 isHex
actualResult.ShouldBe(expectedResult)

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

[<Fact>]
let ``GIVEN WHEN getMinMaxSingle THEN result as expected`` () =
let actualResult = getMinMaxSingle ()
let ``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 ()
let ``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 ()
let ``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 ()
let ``WHEN getBigInt THEN result as expected`` () =
let actualResult = getBigInt()
actualResult.ShouldBe(18446744073709551615I)

Check warning on line 156 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 156 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 f3207e1

Please sign in to comment.