diff --git a/Pentole/binaryprefix.fs b/Pentole/binaryprefix.fs index 091ff17..5e6e6a1 100644 --- a/Pentole/binaryprefix.fs +++ b/Pentole/binaryprefix.fs @@ -1,176 +1,133 @@ module Pentole.BinaryPrefix +type IECBit = + | Bit + | Kb + | Mb + | Gb + | Tb + | Pb + +[] +[] +type Bits (n: double, scale: IECBit) = + struct + member x.bits = + match scale with + | Bit -> n + | Kb -> n * 1000.0 + | Mb -> n * 1000.0 * 1000.0 + | Gb -> n * 1000.0 * 1000.0 * 1000.0 + | Tb -> n * 1000.0 * 1000.0 * 1000.0 * 1000.0 + | Pb -> n * 1000.0 * 1000.0 * 1000.0 * 1000.0 * 1000.0 + + member x.bytes = + match scale with + | Bit -> n / 8.0 + | Kb -> n * 1000.0 / 8.0 + | Mb -> n * 1000.0 * 1000.0 / 8.0 + | Gb -> n * 1000.0 * 1000.0 * 1000.0 / 8.0 + | Tb -> n * 1000.0 * 1000.0 * 1000.0 * 1000.0 / 8.0 + | Pb -> n * 1000.0 * 1000.0 * 1000.0 * 1000.0 * 1000.0/ 8.0 + + interface System.IComparable with + member a.CompareTo b = + match b with + | :? Bits as b -> a.bytes.CompareTo b.bytes + | _ -> -1 + + override a.Equals b = + match b with + | :? Bits as b -> a.bits = b.bits + | _ -> false + + override this.GetHashCode () = this.bits.GetHashCode() +end + +type IECByte = + | Byte + | KB + | MB + | GB + | TB + | PB + + | KiB + | MiB + | GiB + | TiB + | PiB + +[] +[] +type Bytes (n: double, scale: IECByte) = + struct + member x.bits = + match scale with + | Byte -> n * 8.0 + | KB -> n * 1000.0 * 8.0 + | MB -> n * 1000.0 * 1000.0 * 8.0 + | GB -> n * 1000.0 * 1000.0 * 1000.0 * 8.0 + | TB -> n * 1000.0 * 1000.0 * 1000.0 * 1000.0 * 8.0 + | PB -> n * 1000.0 * 1000.0 * 1000.0 * 1000.0 * 1000.0 * 8.0 + + | KiB -> n * 1024.0 * 8.0 + | MiB -> n * 1024.0 * 1024.0 * 8.0 + | GiB -> n * 1024.0 * 1024.0 * 1024.0 * 8.0 + | TiB -> n * 1024.0 * 1024.0 * 1024.0 * 1024.0 * 8.0 + | PiB -> n * 1024.0 * 1024.0 * 1024.0 * 1024.0 * 1024.0 * 8.0 + + member x.bytes = + match scale with + | Byte -> n + | KB -> n * 1000.0 + | MB -> n * 1000.0 * 1000.0 + | GB -> n * 1000.0 * 1000.0 * 1000.0 + | TB -> n * 1000.0 * 1000.0 * 1000.0 * 1000.0 + | PB -> n * 1000.0 * 1000.0 * 1000.0 * 1000.0 * 1000.0 + + | KiB -> n * 1024.0 + | MiB -> n * 1024.0 * 1024.0 + | GiB -> n * 1024.0 * 1024.0 * 1024.0 + | TiB -> n * 1024.0 * 1024.0 * 1024.0 * 1024.0 + | PiB -> n * 1024.0 * 1024.0 * 1024.0 * 1024.0 * 1024.0 + + interface System.IComparable with + member a.CompareTo b = + match b with + | :? Bytes as b -> a.bytes.CompareTo b.bytes + | _ -> -1 + + override a.Equals b = + match b with + | :? Bytes as b -> a.bytes = b.bytes + | _ -> false + + override this.ToString () = $"Bytes({n} {scale})" + override this.GetHashCode () = this.bits.GetHashCode() +end + module Bits = - module IECScale = - type IECBit = - | B - | Kb - | Mb - | Gb - | Tb - | Pb - - type Bit = - | Bit of double - | Kb of double - | Mb of double - | Gb of double - | Tb of double - | Pb of double - - member this.bytes = - match this with - | Bit.Bit i -> i - | Bit.Kb i -> i * 1000.0 / 8.0 - | Bit.Mb i -> i * 1000.0 * 1000.0 / 8.0 - | Bit.Gb i -> i * 1000.0 * 1000.0 * 1000.0 / 8.0 - | Bit.Tb i -> i * 1000.0 * 1000.0 * 1000.0 * 1000.0 / 8.0 - | Bit.Pb i -> i * 1000.0 * 1000.0 * 1000.0 * 1000.0 * 1000.0 / 8.0 - type System.Double with - member this.bit = Bit this - member this.Kb = Bit this - member this.Mb = Mb this - member this.Gb = Gb this - member this.Tb = Tb this - member this.Pb = Pb this - - let private get_i_bit = function - | Kb i | Mb i | Gb i | Tb i | Pb i - | Bit.Bit i -> i - - let change_scale (scale: IECScale.IECBit) (old_value: Bit) = - let x = get_i_bit old_value - let up, base_ = - match old_value with - | Bit _ -> 0.0, 1000.0 - | Kb _ -> 1.0, 1000.0 - | Mb _ -> 2.0, 1000.0 - | Gb _ -> 3.0, 1000.0 - | Tb _ -> 4.0, 1000.0 - | Pb _ -> 5.0, 1000.0 - - let down, kst = - match scale with - | IECScale.IECBit.B -> 0.0, Bit - | IECScale.IECBit.Kb -> 1.0, Kb - | IECScale.IECBit.Mb -> 2.0, Mb - | IECScale.IECBit.Gb -> 3.0, Gb - | IECScale.IECBit.Tb -> 4.0, Tb - | IECScale.IECBit.Pb -> 5.0, Pb - - x * (base_ ** (up - down)) |> kst - + member this.bits = Bits (this, Bit) + member this.Kb = Bits (this, Kb) + member this.Mb = Bits (this, Mb) + member this.Gb = Bits (this, Gb) + member this.Tb = Bits (this, Tb) + member this.Pb = Bits (this, Pb) + + module Bytes = - [] - [] - type Byte = - | Byte of double - | KB of double - | MB of double - | GB of double - | TB of double - | PB of double - - | KiB of double - | MiB of double - | GiB of double - | TiB of double - | PiB of double - with - override a.Equals b = - match b with - | :? Byte as b -> b.bytes = a.bytes - | _ -> false - - member this.bytes = - match this with - | Byte.Byte i -> i - | Byte.KB i -> i * 1000.0 - | Byte.MB i -> i * 1000.0 * 1000.0 - | Byte.GB i -> i * 1000.0 * 1000.0 * 1000.0 - | Byte.TB i -> i * 1000.0 * 1000.0 * 1000.0 * 1000.0 - | Byte.PB i -> i * 1000.0 * 1000.0 * 1000.0 * 1000.0 * 1000.0 - - | Byte.KiB i -> i * 1024.0 - | Byte.MiB i -> i * 1024.0 * 1024.0 - | Byte.GiB i -> i * 1024.0 * 1024.0 * 1024.0 - | Byte.TiB i -> i * 1024.0 * 1024.0 * 1024.0 * 1024.0 - | Byte.PiB i -> i * 1024.0 * 1024.0 * 1024.0 * 1024.0 * 1024.0 - - interface System.IComparable with - member a.CompareTo b = - match b with - | :? Byte as b -> a.bytes.CompareTo b.bytes - | _ -> -1 - override this.GetHashCode () = this.bytes.GetHashCode() - - - type System.Double with - member this.byte = Byte.Byte this - member this.KB = KB this - member this.MB = MB this - member this.GB = GB this - member this.TB = TB this - member this.PB = PB this + member this.bytes = Bytes (this, Byte) + member this.KB = Bytes (this, KB) + member this.MB = Bytes (this, MB) + member this.GB = Bytes (this, GB) + member this.TB = Bytes (this, TB) + member this.PB = Bytes (this, PB) - member this.KiB = KiB this - member this.MiB = MiB this - member this.GiB = GiB this - member this.TiB = TiB this - member this.PiB = PiB this - - let private get_i_byte = function - | KB i | MB i | GB i | TB i | PB i - | KiB i | MiB i | GiB i | TiB i | PiB i - | Byte.Byte i -> i - - module IECScale = - type IECByte = - | B - | KB - | KiB - | MB - | MiB - | GB - | GiB - | TB - | TiB - | PB - | PiB - - let change_scale (scale: IECScale.IECByte) (old_value: Byte) = - let x = get_i_byte old_value - let up, base_ = - match old_value with - | KB _ -> 1.0, 1000.0 - | MB _ -> 2.0, 1000.0 - | GB _ -> 3.0, 1000.0 - | TB _ -> 4.0, 1000.0 - | PB _ -> 5.0, 1000.0 - - | Byte.KiB _ -> 1.0, 1024.0 - | Byte.MiB _ -> 2.0, 1024.0 - | Byte.GiB _ -> 3.0, 1024.0 - | Byte.TiB _ -> 4.0, 1024.0 - | Byte.PiB _ -> 5.0, 1024.0 - - | Byte.Byte _ -> 0.0, 1000.0 - - let down, kst = - match scale with - | IECScale.IECByte.B -> 0.0, Byte - | IECScale.IECByte.KB -> 1.0, KB - | IECScale.IECByte.MB -> 2.0, MB - | IECScale.IECByte.GB -> 3.0, GB - | IECScale.IECByte.TB -> 4.0, TB - | IECScale.IECByte.PB -> 5.0, PB - - | IECScale.IECByte.KiB -> 1.0, KiB - | IECScale.IECByte.MiB -> 2.0, MiB - | IECScale.IECByte.GiB -> 3.0, GiB - | IECScale.IECByte.TiB -> 4.0, TiB - | IECScale.IECByte.PiB -> 5.0, PiB - - x * 8.0 * (base_ ** (up - down)) |> kst + member this.KiB = Bytes (this, KiB) + member this.MiB = Bytes (this, MiB) + member this.GiB = Bytes (this, GiB) + member this.TiB = Bytes (this, TiB) + member this.PiB = Bytes (this, PiB) \ No newline at end of file diff --git a/Pentole/test_extensions.fs b/Pentole/test_extensions.fs index f475f0d..2622775 100644 --- a/Pentole/test_extensions.fs +++ b/Pentole/test_extensions.fs @@ -2,9 +2,9 @@ module Pentole.TestsExtensions open NUnit.Framework -type NUnit.Framework.Assert with +type Assert with - static member ok_is_equal<'ok, 'err> (expected: 'ok) (got: Result<'ok, 'err>) = + static member inline ok_is_equal<'ok, 'err> (expected: 'ok) (got: Result<'ok, 'err>) = match got with | Ok got -> Assert.That (got, Is.EqualTo(expected)) | Error e -> Assert.Fail $"Expected 'ok, got: {e}" @@ -25,7 +25,7 @@ type NUnit.Framework.Assert with | Ok got -> Assert.That (got, Is.False) | Error e -> Assert.Fail $"Expected truth value, got: {e}" - static member are_equal expected (got: 'a) = + static member inline are_equal expected (got: 'a) = Assert.That (got, Is.EqualTo(expected)) static member are_seq_equal (expected: 'a seq) (got: 'a seq) = diff --git a/tests/bytes_tests.fs b/tests/bytes_tests.fs index ccdc70d..aeeca3b 100644 --- a/tests/bytes_tests.fs +++ b/tests/bytes_tests.fs @@ -3,9 +3,28 @@ module Tests.BinaryPrefix open NUnit.Framework open Pentole.TestsExtensions -open Pentole.BinaryPrefix +open Pentole.BinaryPrefix.Bits +open Pentole.BinaryPrefix.Bytes [] let equality_test () = - Assert.are_equal (Bytes.KB 1) (Bytes.Byte 1000) - Assert.are_equal (Bytes.KiB 1) (Bytes.Byte 1024) + 1.0.KB |> Assert.are_equal (1000.0.bytes) + 1.0.KiB.bytes |> Assert.are_equal (1024.0.bytes.bytes) + + +[] +let ``Bit.bytes returns correct values`` () = + Assert.are_equal 0.125 1.0.bits.bytes + Assert.are_equal 125.0 1.0.Kb.bytes + Assert.are_equal 125000.0 1.0.Mb.bytes + Assert.are_equal 125000000.0 1.0.Gb.bytes + Assert.are_equal 125000000000.0 1.0.Tb.bytes + Assert.are_equal 125000000000000.0 1.0.Pb.bytes + + Assert.are_equal 0.250 2.0.bits.bytes + Assert.are_equal 250.0 2.0.Kb.bytes + Assert.are_equal 250000.0 2.0.Mb.bytes + Assert.are_equal 500000000.0 4.0.Gb.bytes + Assert.are_equal 500000000000.0 4.0.Tb.bytes + Assert.are_equal 500000000000000.0 4.0.Pb.bytes +