Skip to content

Commit

Permalink
Add unit tests for Section 02 12.
Browse files Browse the repository at this point in the history
  • Loading branch information
eminencegrs committed Feb 4, 2024
1 parent 87d6505 commit dd1b4c5
Show file tree
Hide file tree
Showing 4 changed files with 172 additions and 17 deletions.
101 changes: 88 additions & 13 deletions src/FPinFSharp.Exercises/Chapter_02/Section_02_12.fs
Original file line number Diff line number Diff line change
Expand Up @@ -3,34 +3,109 @@ namespace FPinFSharp.Exercises.Chapter_02
// 2.12 Summary of the basic types
module Section_02_12 =

let getChar () : char = 'c'
// 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)

let getByte isHex : byte =
// Char
let convertIntToChar (x: int) : char =
System.Convert.ToChar x

// Byte
let getMinMaxByte (isHex: bool) : byte * byte =
match isHex with
| true -> (0x0uy, 0xFFuy)
| false -> (0uy, 255uy)

let getMinMaxByteInString (isHex: bool) : string * string =
match isHex with
| true -> 0xFFuy
| false -> 255uy
| true -> getMinMaxByte true |> formatHexadecimal
| false -> getMinMaxByte false |> formatNumeric

let getSByte isHex : sbyte =
// SByte
let getMinMaxSByte (isHex: bool) : sbyte * sbyte =
match isHex with
| true -> -0x80y
| false -> 127y
| true -> (0x80y, 0x7Fy)
| false -> (-128y, 127y)

let getInt16 isHex : int16 =
let getMinMaxSByteInString (isHex: bool) : string * string =
match isHex with
| true -> -0x8000s
| false -> 32767s
| true -> getMinMaxSByte true |> formatHexadecimal
| false -> getMinMaxSByte false |> formatNumeric

let getUInt16 isHex : uint16 =
// Int16
let getMinMaxInt16 (isHex: bool) : int16 * int16 =
match isHex with
| true -> 0xFFFFus
| false -> 65535us
| true -> (-0x8000s, 0x7FFFs)
| false -> (-32768s, 32767s)

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

// UInt16
let getMinMaxUInt16 (isHex: bool) : uint16 * uint16 =
match isHex with
| true -> (0x0us, 0xFFFFus)
| false -> (0us, 65535us)

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

// Int32
let getInt32 isHex : int32 =
match isHex with
| true -> -0x80000000
| false -> 2147483647

// UInt32
let getUInt32 isHex : uint32 =
match isHex with
| true -> 0xFFFFFFFFu
| false -> 4294967295u

// Int64
let getInt64 isHex : int64 =
match isHex with
| true -> -0x8000000000000000L
| false -> 9223372036854775807L

// UInt64
let getUInt64 isHex : uint64 =
match isHex with
| true -> 0xFFFFFFFFFFFFFFFFUL
| false -> 18446744073709551615UL

// nativeint
let getNativeInt isHex : nativeint =
match isHex with
| true -> -0x80000000n
| false -> 2147483647n

// unativeint
let getUNativeInt isHex : unativeint =
match isHex with
| true -> 0xFFFFFFFFun
| false -> 4294967295un

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

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

// Decimal
let getMinMaxDecimal () : decimal * decimal =
(System.Decimal.MinValue, System.Decimal.MaxValue)

// BigInt
let getBigInt () : bigint =
18446744073709551615I
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,21 @@ open Xunit
module Section_02_11_Tests =

[<Fact>]
let ``Given true when call raiseExceptionIfTrue then InvalidOperationException is thrown`` () =
let ``GIVEN true WHEN raiseExceptionIfTrue THEN InvalidOperationException is thrown`` () =
let action = fun () -> raiseExceptionIfTrue true
action |> ShouldThrowExtensions.ShouldThrow<InvalidOperationException>

[<Fact>]
let ``Given false when call raiseExceptionIfTrue then no exception is thrown`` () =
let ``GIVEN false WHEN raiseExceptionIfTrue THEN no exception is thrown`` () =
let action = fun () -> raiseExceptionIfTrue false
action |> ShouldThrowExtensions.ShouldNotThrow

[<Fact>]
let ``Given false when call raiseExceptionIfFalse then InvalidOperationException is thrown`` () =
let ``GIVEN false WHEN raiseExceptionIfFalse THEN InvalidOperationException is thrown`` () =
let action = fun () -> raiseExceptionIfFalse false
action |> ShouldThrowExtensions.ShouldThrow<InvalidOperationException>

[<Fact>]
let ``Given true when call raiseExceptionIfFalse then no exception is thrown`` () =
let ``GIVEN true WHEN raiseExceptionIfFalse THEN no exception is thrown`` () =
let action = fun () -> raiseExceptionIfFalse true
action |> ShouldThrowExtensions.ShouldNotThrow
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
namespace FPinFSharp.Exercises.UnitTests.Chapter_02

open System
open FPinFSharp.Exercises.Chapter_02.Section_02_12
open Shouldly
open Xunit

module Section_02_12_Tests =

[<Theory>]
[<InlineData(Char.MinValue, Char.MinValue)>]
[<InlineData(Char.MaxValue, Char.MaxValue)>]
[<InlineData(65, 'A')>]
let ``GIVEN int WHEN convertIntToChar THEN result as expected`` (x:int) (expectedResult:char) =
let actualResult = convertIntToChar x
actualResult.ShouldBe(expectedResult)

[<Theory>]
[<InlineData(Int32.MinValue)>]
[<InlineData(-1)>]
let ``GIVEN invalid int WHEN convertIntToChar THEN OverflowException thrown`` (x:int) =
let action = fun () -> convertIntToChar x |> ignore
action |> ShouldThrowExtensions.ShouldThrow<OverflowException>

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

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

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

[<Theory>]
[<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
actualResult.ShouldBe(expectedResult)

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

[<Theory>]
[<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
actualResult.ShouldBe(expectedResult)

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

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

Check warning on line 79 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 79 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
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
<Compile Include="Chapter_02\Section_02_09_Tests.fs" />
<Compile Include="Chapter_02\Section_02_10_Tests.fs" />
<Compile Include="Chapter_02\Section_02_11_Tests.fs" />
<Compile Include="Chapter_02\Section_02_12_Tests.fs" />
<Folder Include="Chapter_03\" />
</ItemGroup>

Expand Down

0 comments on commit dd1b4c5

Please sign in to comment.