diff --git a/src/FPinFSharp.Exercises/Chapter_02/Section_02_12.fs b/src/FPinFSharp.Exercises/Chapter_02/Section_02_12.fs index 78fc3b1..8639024 100644 --- a/src/FPinFSharp.Exercises/Chapter_02/Section_02_12.fs +++ b/src/FPinFSharp.Exercises/Chapter_02/Section_02_12.fs @@ -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 diff --git a/tests/FPinFSharp.Exercises.UnitTests/Chapter_02/Section_02_11_Tests.fs b/tests/FPinFSharp.Exercises.UnitTests/Chapter_02/Section_02_11_Tests.fs index 6995110..d66e73d 100644 --- a/tests/FPinFSharp.Exercises.UnitTests/Chapter_02/Section_02_11_Tests.fs +++ b/tests/FPinFSharp.Exercises.UnitTests/Chapter_02/Section_02_11_Tests.fs @@ -8,21 +8,21 @@ open Xunit module Section_02_11_Tests = [] - 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 [] - 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 [] - 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 [] - 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 diff --git a/tests/FPinFSharp.Exercises.UnitTests/Chapter_02/Section_02_12_Tests.fs b/tests/FPinFSharp.Exercises.UnitTests/Chapter_02/Section_02_12_Tests.fs new file mode 100644 index 0000000..df76f35 --- /dev/null +++ b/tests/FPinFSharp.Exercises.UnitTests/Chapter_02/Section_02_12_Tests.fs @@ -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 = + + [] + [] + [] + [] + let ``GIVEN int WHEN convertIntToChar THEN result as expected`` (x:int) (expectedResult:char) = + let actualResult = convertIntToChar x + actualResult.ShouldBe(expectedResult) + + [] + [] + [] + let ``GIVEN invalid int WHEN convertIntToChar THEN OverflowException thrown`` (x:int) = + let action = fun () -> convertIntToChar x |> ignore + action |> ShouldThrowExtensions.ShouldThrow + + [] + [] + [] + let ``GIVEN isHex WHEN getMinMaxByte THEN result as expected`` (isHex:bool) (expectedResult: byte * byte) = + let actualResult = getMinMaxByte isHex + actualResult.ShouldBe(expectedResult) + + [] + [] + [] + let ``GIVEN isHex WHEN getMinMaxByteInString THEN result as expected`` (isHex:bool) (expectedResult: string * string) = + let actualResult = getMinMaxByteInString isHex + actualResult.ShouldBe(expectedResult); + + [] + [] + [] + let ``GIVEN isHex WHEN getMinMaxSByte THEN result as expected`` (isHex:bool) (expectedResult: sbyte * sbyte) = + let actualResult = getMinMaxSByte isHex + actualResult.ShouldBe(expectedResult) + + [] + [] + [] + let ``GIVEN isHex WHEN getMinMaxSByteInString THEN result as expected`` (isHex:bool) (expectedResult: string * string) = + let actualResult = getMinMaxSByteInString isHex + actualResult.ShouldBe(expectedResult) + + [] + [] + [] + let ``GIVEN isHex WHEN getMinMaxInt16 THEN result as expected`` (isHex:bool) (expectedResult: int16 * int16) = + let actualResult = getMinMaxInt16 isHex + actualResult.ShouldBe(expectedResult) + + [] + [] + [] + let ``GIVEN isHex WHEN getMinMaxInt16InString THEN result as expected`` (isHex:bool) (expectedResult: string * string) = + let actualResult = getMinMaxInt16InString isHex + actualResult.ShouldBe(expectedResult) + + [] + [] + [] + let ``GIVEN isHex WHEN getMinMaxUInt16 THEN result as expected`` (isHex:bool) (expectedResult: uint16 * uint16) = + let actualResult = getMinMaxUInt16 isHex + expectedResult.ShouldBe(expectedResult) + + [] + [] + [] + let ``GIVEN isHex WHEN getMinMaxUInt16InString THEN result as expected`` (isHex:bool) (expectedResult: string * string) = + let actualResult = getMinMaxUInt16InString isHex + actualResult.ShouldBe(expectedResult) diff --git a/tests/FPinFSharp.Exercises.UnitTests/FPinFSharp.Exercises.UnitTests.fsproj b/tests/FPinFSharp.Exercises.UnitTests/FPinFSharp.Exercises.UnitTests.fsproj index d637e0a..f56abd6 100644 --- a/tests/FPinFSharp.Exercises.UnitTests/FPinFSharp.Exercises.UnitTests.fsproj +++ b/tests/FPinFSharp.Exercises.UnitTests/FPinFSharp.Exercises.UnitTests.fsproj @@ -30,6 +30,7 @@ +