This commit is contained in:
Francesco Mecca 2024-09-16 18:18:24 +02:00
parent f3eecaef78
commit 0dd7d295a1
2 changed files with 18 additions and 2 deletions

View file

@ -62,6 +62,8 @@ module Bits =
x * (base_ ** (up - down)) |> kst x * (base_ ** (up - down)) |> kst
module Bytes = module Bytes =
[<CustomEquality>]
[<CustomComparison>]
type Byte = type Byte =
| Byte of double | Byte of double
| KB of double | KB of double
@ -75,7 +77,12 @@ module Bytes =
| GiB of double | GiB of double
| TiB of double | TiB of double
| PiB of double | PiB of double
with
override a.Equals b =
match b with
| :? Byte as b -> b.bytes = a.bytes
| _ -> false
member this.bytes = member this.bytes =
match this with match this with
| Byte.Byte i -> i | Byte.Byte i -> i
@ -91,6 +98,15 @@ module Bytes =
| Byte.TiB i -> i * 1024.0 * 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 | 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 type System.Double with
member this.byte = Byte.Byte this member this.byte = Byte.Byte this
member this.KB = KB this member this.KB = KB this

View file

@ -6,6 +6,6 @@ open Pentole.TestsExtensions
open Pentole.BinaryPrefix open Pentole.BinaryPrefix
[<Test>] [<Test>]
let split_test () = let equality_test () =
Assert.are_equal (Bytes.KB 1) (Bytes.Byte 1000) Assert.are_equal (Bytes.KB 1) (Bytes.Byte 1000)
Assert.are_equal (Bytes.KiB 1) (Bytes.Byte 1024) Assert.are_equal (Bytes.KiB 1) (Bytes.Byte 1024)