fix
This commit is contained in:
parent
0dd7d295a1
commit
bbeb9e1540
3 changed files with 151 additions and 175 deletions
|
@ -1,176 +1,133 @@
|
||||||
module Pentole.BinaryPrefix
|
module Pentole.BinaryPrefix
|
||||||
|
|
||||||
module Bits =
|
type IECBit =
|
||||||
module IECScale =
|
| Bit
|
||||||
type IECBit =
|
|
||||||
| B
|
|
||||||
| Kb
|
| Kb
|
||||||
| Mb
|
| Mb
|
||||||
| Gb
|
| Gb
|
||||||
| Tb
|
| Tb
|
||||||
| Pb
|
| Pb
|
||||||
|
|
||||||
type Bit =
|
[<CustomEquality>]
|
||||||
| Bit of double
|
[<CustomComparison>]
|
||||||
| Kb of double
|
type Bits (n: double, scale: IECBit) =
|
||||||
| Mb of double
|
struct
|
||||||
| Gb of double
|
member x.bits =
|
||||||
| 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
|
match scale with
|
||||||
| IECScale.IECBit.B -> 0.0, Bit
|
| Bit -> n
|
||||||
| IECScale.IECBit.Kb -> 1.0, Kb
|
| Kb -> n * 1000.0
|
||||||
| IECScale.IECBit.Mb -> 2.0, Mb
|
| Mb -> n * 1000.0 * 1000.0
|
||||||
| IECScale.IECBit.Gb -> 3.0, Gb
|
| Gb -> n * 1000.0 * 1000.0 * 1000.0
|
||||||
| IECScale.IECBit.Tb -> 4.0, Tb
|
| Tb -> n * 1000.0 * 1000.0 * 1000.0 * 1000.0
|
||||||
| IECScale.IECBit.Pb -> 5.0, Pb
|
| Pb -> n * 1000.0 * 1000.0 * 1000.0 * 1000.0 * 1000.0
|
||||||
|
|
||||||
x * (base_ ** (up - down)) |> kst
|
member x.bytes =
|
||||||
|
match scale with
|
||||||
module Bytes =
|
| Bit -> n / 8.0
|
||||||
[<CustomEquality>]
|
| Kb -> n * 1000.0 / 8.0
|
||||||
[<CustomComparison>]
|
| Mb -> n * 1000.0 * 1000.0 / 8.0
|
||||||
type Byte =
|
| Gb -> n * 1000.0 * 1000.0 * 1000.0 / 8.0
|
||||||
| Byte of double
|
| Tb -> n * 1000.0 * 1000.0 * 1000.0 * 1000.0 / 8.0
|
||||||
| KB of double
|
| Pb -> n * 1000.0 * 1000.0 * 1000.0 * 1000.0 * 1000.0/ 8.0
|
||||||
| 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
|
interface System.IComparable with
|
||||||
member a.CompareTo b =
|
member a.CompareTo b =
|
||||||
match b with
|
match b with
|
||||||
| :? Byte as b -> a.bytes.CompareTo b.bytes
|
| :? Bits as b -> a.bytes.CompareTo b.bytes
|
||||||
| _ -> -1
|
| _ -> -1
|
||||||
override this.GetHashCode () = this.bytes.GetHashCode()
|
|
||||||
|
|
||||||
|
override a.Equals b =
|
||||||
|
match b with
|
||||||
|
| :? Bits as b -> a.bits = b.bits
|
||||||
|
| _ -> false
|
||||||
|
|
||||||
|
override this.GetHashCode () = this.bits.GetHashCode()
|
||||||
|
end
|
||||||
|
|
||||||
type System.Double with
|
type IECByte =
|
||||||
member this.byte = Byte.Byte this
|
| Byte
|
||||||
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.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
|
| KB
|
||||||
| KiB
|
|
||||||
| MB
|
| MB
|
||||||
| MiB
|
|
||||||
| GB
|
| GB
|
||||||
| GiB
|
|
||||||
| TB
|
| TB
|
||||||
| TiB
|
|
||||||
| PB
|
| PB
|
||||||
|
|
||||||
|
| KiB
|
||||||
|
| MiB
|
||||||
|
| GiB
|
||||||
|
| TiB
|
||||||
| PiB
|
| PiB
|
||||||
|
|
||||||
let change_scale (scale: IECScale.IECByte) (old_value: Byte) =
|
[<CustomEquality>]
|
||||||
let x = get_i_byte old_value
|
[<CustomComparison>]
|
||||||
let up, base_ =
|
type Bytes (n: double, scale: IECByte) =
|
||||||
match old_value with
|
struct
|
||||||
| KB _ -> 1.0, 1000.0
|
member x.bits =
|
||||||
| 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
|
match scale with
|
||||||
| IECScale.IECByte.B -> 0.0, Byte
|
| Byte -> n * 8.0
|
||||||
| IECScale.IECByte.KB -> 1.0, KB
|
| KB -> n * 1000.0 * 8.0
|
||||||
| IECScale.IECByte.MB -> 2.0, MB
|
| MB -> n * 1000.0 * 1000.0 * 8.0
|
||||||
| IECScale.IECByte.GB -> 3.0, GB
|
| GB -> n * 1000.0 * 1000.0 * 1000.0 * 8.0
|
||||||
| IECScale.IECByte.TB -> 4.0, TB
|
| TB -> n * 1000.0 * 1000.0 * 1000.0 * 1000.0 * 8.0
|
||||||
| IECScale.IECByte.PB -> 5.0, PB
|
| PB -> n * 1000.0 * 1000.0 * 1000.0 * 1000.0 * 1000.0 * 8.0
|
||||||
|
|
||||||
| IECScale.IECByte.KiB -> 1.0, KiB
|
| KiB -> n * 1024.0 * 8.0
|
||||||
| IECScale.IECByte.MiB -> 2.0, MiB
|
| MiB -> n * 1024.0 * 1024.0 * 8.0
|
||||||
| IECScale.IECByte.GiB -> 3.0, GiB
|
| GiB -> n * 1024.0 * 1024.0 * 1024.0 * 8.0
|
||||||
| IECScale.IECByte.TiB -> 4.0, TiB
|
| TiB -> n * 1024.0 * 1024.0 * 1024.0 * 1024.0 * 8.0
|
||||||
| IECScale.IECByte.PiB -> 5.0, PiB
|
| PiB -> n * 1024.0 * 1024.0 * 1024.0 * 1024.0 * 1024.0 * 8.0
|
||||||
|
|
||||||
x * 8.0 * (base_ ** (up - down)) |> kst
|
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 =
|
||||||
|
type System.Double with
|
||||||
|
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 System.Double with
|
||||||
|
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 = 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)
|
|
@ -2,9 +2,9 @@ module Pentole.TestsExtensions
|
||||||
|
|
||||||
open NUnit.Framework
|
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
|
match got with
|
||||||
| Ok got -> Assert.That (got, Is.EqualTo(expected))
|
| Ok got -> Assert.That (got, Is.EqualTo(expected))
|
||||||
| Error e -> Assert.Fail $"Expected 'ok, got: {e}"
|
| Error e -> Assert.Fail $"Expected 'ok, got: {e}"
|
||||||
|
@ -25,7 +25,7 @@ type NUnit.Framework.Assert with
|
||||||
| Ok got -> Assert.That (got, Is.False)
|
| Ok got -> Assert.That (got, Is.False)
|
||||||
| Error e -> Assert.Fail $"Expected truth value, got: {e}"
|
| 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))
|
Assert.That (got, Is.EqualTo(expected))
|
||||||
|
|
||||||
static member are_seq_equal (expected: 'a seq) (got: 'a seq) =
|
static member are_seq_equal (expected: 'a seq) (got: 'a seq) =
|
||||||
|
|
|
@ -3,9 +3,28 @@ module Tests.BinaryPrefix
|
||||||
open NUnit.Framework
|
open NUnit.Framework
|
||||||
|
|
||||||
open Pentole.TestsExtensions
|
open Pentole.TestsExtensions
|
||||||
open Pentole.BinaryPrefix
|
open Pentole.BinaryPrefix.Bits
|
||||||
|
open Pentole.BinaryPrefix.Bytes
|
||||||
|
|
||||||
[<Test>]
|
[<Test>]
|
||||||
let equality_test () =
|
let equality_test () =
|
||||||
Assert.are_equal (Bytes.KB 1) (Bytes.Byte 1000)
|
1.0.KB |> Assert.are_equal (1000.0.bytes)
|
||||||
Assert.are_equal (Bytes.KiB 1) (Bytes.Byte 1024)
|
1.0.KiB.bytes |> Assert.are_equal (1024.0.bytes.bytes)
|
||||||
|
|
||||||
|
|
||||||
|
[<Test>]
|
||||||
|
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
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue