From f6069bd3aa94c1368707ed259d19cdb17bd2f63a Mon Sep 17 00:00:00 2001 From: Alex Macocian Date: Mon, 9 Dec 2019 10:44:16 +0100 Subject: [PATCH] Bit field structures. --- .../BitStructures/Int32BitStruct.cs | 177 +++++++++++++++ .../BitStructures/Int64BitStruct.cs | 205 ++++++++++++++++++ SystemExtensions/SystemExtensions.csproj | 8 +- .../Structures/Int32BitStructTests.cs | 68 ++++++ .../Structures/Int64BitStructTests.cs | 103 +++++++++ .../SystemExtensionsTests.csproj | 2 + 6 files changed, 560 insertions(+), 3 deletions(-) create mode 100644 SystemExtensions/Structures/BitStructures/Int32BitStruct.cs create mode 100644 SystemExtensions/Structures/BitStructures/Int64BitStruct.cs create mode 100644 SystemExtensionsTests/Structures/Int32BitStructTests.cs create mode 100644 SystemExtensionsTests/Structures/Int64BitStructTests.cs diff --git a/SystemExtensions/Structures/BitStructures/Int32BitStruct.cs b/SystemExtensions/Structures/BitStructures/Int32BitStruct.cs new file mode 100644 index 0000000..45aca39 --- /dev/null +++ b/SystemExtensions/Structures/BitStructures/Int32BitStruct.cs @@ -0,0 +1,177 @@ +using System; + +namespace SystemExtensions.Structures.BitStructures +{ + public struct Int32BitStruct : IEquatable + { + public Int32BitStruct(uint value) => this.Value = value; + public Int32BitStruct(int value) { unchecked { this.Value = (uint)value; } } + public uint Value { get; set; } + public uint Bit0 { get => this.Value & 0x1; set => this.Value = (this.Value & ~(0x1U) | (0x1 & value)); } + public uint Bit1 { get => this.Value >> 1 & 0x1; set => this.Value = (this.Value & ~(0x1U << 1)) | ((0x1 & value) << 1); } + public uint Bit2 { get => this.Value >> 2 & 0x1; set => this.Value = (this.Value & ~(0x1U << 2)) | ((0x1 & value) << 2); } + public uint Bit3 { get => this.Value >> 3 & 0x1; set => this.Value = (this.Value & ~(0x1U << 3)) | ((0x1 & value) << 3); } + public uint Bit4 { get => this.Value >> 4 & 0x1; set => this.Value = (this.Value & ~(0x1U << 4)) | ((0x1 & value) << 4); } + public uint Bit5 { get => this.Value >> 5 & 0x1; set => this.Value = (this.Value & ~(0x1U << 5)) | ((0x1 & value) << 5); } + public uint Bit6 { get => this.Value >> 6 & 0x1; set => this.Value = (this.Value & ~(0x1U << 6)) | ((0x1 & value) << 6); } + public uint Bit7 { get => this.Value >> 7 & 0x1; set => this.Value = (this.Value & ~(0x1U << 7)) | ((0x1 & value) << 7); } + public uint Bit8 { get => this.Value >> 8 & 0x1; set => this.Value = (this.Value & ~(0x1U << 8)) | ((0x1 & value) << 8); } + public uint Bit9 { get => this.Value >> 9 & 0x1; set => this.Value = (this.Value & ~(0x1U << 9)) | ((0x1 & value) << 9); } + public uint Bit10 { get => this.Value >> 10 & 0x1; set => this.Value = (this.Value & ~(0x1U << 10)) | ((0x1 & value) << 10); } + public uint Bit11 { get => this.Value >> 11 & 0x1; set => this.Value = (this.Value & ~(0x1U << 11)) | ((0x1 & value) << 11); } + public uint Bit12 { get => this.Value >> 12 & 0x1; set => this.Value = (this.Value & ~(0x1U << 12)) | ((0x1 & value) << 12); } + public uint Bit13 { get => this.Value >> 13 & 0x1; set => this.Value = (this.Value & ~(0x1U << 13)) | ((0x1 & value) << 13); } + public uint Bit14 { get => this.Value >> 14 & 0x1; set => this.Value = (this.Value & ~(0x1U << 14)) | ((0x1 & value) << 14); } + public uint Bit15 { get => this.Value >> 15 & 0x1; set => this.Value = (this.Value & ~(0x1U << 15)) | ((0x1 & value) << 15); } + public uint Bit16 { get => this.Value >> 16 & 0x1; set => this.Value = (this.Value & ~(0x1U << 16)) | ((0x1 & value) << 16); } + public uint Bit17 { get => this.Value >> 17 & 0x1; set => this.Value = (this.Value & ~(0x1U << 17)) | ((0x1 & value) << 17); } + public uint Bit18 { get => this.Value >> 18 & 0x1; set => this.Value = (this.Value & ~(0x1U << 18)) | ((0x1 & value) << 18); } + public uint Bit19 { get => this.Value >> 19 & 0x1; set => this.Value = (this.Value & ~(0x1U << 19)) | ((0x1 & value) << 19); } + public uint Bit20 { get => this.Value >> 20 & 0x1; set => this.Value = (this.Value & ~(0x1U << 20)) | ((0x1 & value) << 20); } + public uint Bit21 { get => this.Value >> 21 & 0x1; set => this.Value = (this.Value & ~(0x1U << 21)) | ((0x1 & value) << 21); } + public uint Bit22 { get => this.Value >> 22 & 0x1; set => this.Value = (this.Value & ~(0x1U << 22)) | ((0x1 & value) << 22); } + public uint Bit23 { get => this.Value >> 23 & 0x1; set => this.Value = (this.Value & ~(0x1U << 23)) | ((0x1 & value) << 23); } + public uint Bit24 { get => this.Value >> 24 & 0x1; set => this.Value = (this.Value & ~(0x1U << 24)) | ((0x1 & value) << 24); } + public uint Bit25 { get => this.Value >> 25 & 0x1; set => this.Value = (this.Value & ~(0x1U << 25)) | ((0x1 & value) << 25); } + public uint Bit26 { get => this.Value >> 26 & 0x1; set => this.Value = (this.Value & ~(0x1U << 26)) | ((0x1 & value) << 26); } + public uint Bit27 { get => this.Value >> 27 & 0x1; set => this.Value = (this.Value & ~(0x1U << 27)) | ((0x1 & value) << 27); } + public uint Bit28 { get => this.Value >> 28 & 0x1; set => this.Value = (this.Value & ~(0x1U << 28)) | ((0x1 & value) << 28); } + public uint Bit29 { get => this.Value >> 29 & 0x1; set => this.Value = (this.Value & ~(0x1U << 29)) | ((0x1 & value) << 29); } + public uint Bit30 { get => this.Value >> 30 & 0x1; set => this.Value = (this.Value & ~(0x1U << 30)) | ((0x1 & value) << 30); } + public uint Bit31 { get => this.Value >> 31 & 0x1; set => this.Value = (this.Value & ~(0x1U << 31)) | ((0x1 & value) << 31); } + + public static implicit operator Int32BitStruct(uint value) => new Int32BitStruct(value); + public static implicit operator Int32BitStruct(int value) => new Int32BitStruct(value); + public static implicit operator int(Int32BitStruct value) => (int)value.Value; + public static implicit operator uint(Int32BitStruct value) => value.Value; + public static bool operator ==(Int32BitStruct first, Int32BitStruct second) + { + return first.Value == second.Value; + } + + public static bool operator !=(Int32BitStruct first, Int32BitStruct second) + { + return first.Value != second.Value; + } + + public static bool operator ==(Int32BitStruct first, uint second) + { + return first.Value == second; + } + + public static bool operator !=(Int32BitStruct first, uint second) + { + return first.Value != second; + } + + public static bool operator ==(Int32BitStruct first, int second) + { + unchecked + { + return first.Value == (uint)second; + } + } + + public static bool operator !=(Int32BitStruct first, int second) + { + unchecked + { + return first.Value != (uint)second; + } + } + + public static bool operator >=(Int32BitStruct first, Int32BitStruct second) + { + return first.Value >= second.Value; + } + + public static bool operator <=(Int32BitStruct first, Int32BitStruct second) + { + return first.Value <= second.Value; + } + + public static bool operator >=(Int32BitStruct first, int second) + { + unchecked + { + return first.Value >= (uint)second; + } + } + + public static bool operator <=(Int32BitStruct first, int second) + { + unchecked + { + return first.Value <= (uint)second; + } + } + + public static bool operator >=(Int32BitStruct first, uint second) + { + return first.Value >= second; + } + + public static bool operator <=(Int32BitStruct first, uint second) + { + return first.Value <= second; + } + + public static bool operator >(Int32BitStruct first, Int32BitStruct second) + { + return first.Value > second.Value; + } + + public static bool operator <(Int32BitStruct first, Int32BitStruct second) + { + return first.Value < second.Value; + } + + public static bool operator >(Int32BitStruct first, int second) + { + unchecked + { + return first.Value > (uint)second; + } + } + + public static bool operator <(Int32BitStruct first, int second) + { + unchecked + { + return first.Value < (uint)second; + } + } + + public static bool operator >(Int32BitStruct first, uint second) + { + return first.Value > second; + } + + public static bool operator <(Int32BitStruct first, uint second) + { + return first.Value < second; + } + + public bool Equals(Int32BitStruct other) + { + return this.Value == other.Value; + } + + public override bool Equals(object obj) + { + if (obj is Int32BitStruct) + { + return this.Equals((Int32BitStruct)obj); + } + else + { + return base.Equals(obj); + } + } + + public override int GetHashCode() + { + return (this.Value).GetHashCode(); + } + } +} \ No newline at end of file diff --git a/SystemExtensions/Structures/BitStructures/Int64BitStruct.cs b/SystemExtensions/Structures/BitStructures/Int64BitStruct.cs new file mode 100644 index 0000000..73603a3 --- /dev/null +++ b/SystemExtensions/Structures/BitStructures/Int64BitStruct.cs @@ -0,0 +1,205 @@ +using System; + +namespace SystemExtensions.Structures.BitStructures +{ + public struct Int64BitStruct : IEquatable + { + public Int64BitStruct(uint low, uint high) + { + this.Value = low + ((ulong)high << 32); + } + + public Int64BitStruct(int low, int high) + { + unchecked + { + this.Value = (uint)low + ((ulong)high << 32); + } + } + + public Int64BitStruct(long value) + { + unchecked + { + this.Value = (ulong)value; + } + } + + public Int64BitStruct(ulong value) + { + this.Value = value; + } + + public ulong Value { get; set; } + public uint Low { get => (uint)(this.Value & 0xFFFFFFFF); set => this.Value = (this.Value & ~(0xFFFFFFFF)) | (0xFFFFFFFF & value); } + public uint High { get => (uint)((this.Value >> 32) & 0xFFFFFFFF); set => this.Value = (this.Value & ~(0xFFFFFFFF00000000)) | ((ulong)(0xFFFFFFFF & value) << 32); } + public ulong Bit0 { get => this.Value & 0x1; set => this.Value = (this.Value & ~(0x1UL) | (0x1 & value)); } + public ulong Bit1 { get => this.Value >> 1 & 0x1; set => this.Value = (this.Value & ~(0x1UL << 1)) | ((0x1 & value) << 1); } + public ulong Bit2 { get => this.Value >> 2 & 0x1; set => this.Value = (this.Value & ~(0x1UL << 2)) | ((0x1 & value) << 2); } + public ulong Bit3 { get => this.Value >> 3 & 0x1; set => this.Value = (this.Value & ~(0x1UL << 3)) | ((0x1 & value) << 3); } + public ulong Bit4 { get => this.Value >> 4 & 0x1; set => this.Value = (this.Value & ~(0x1UL << 4)) | ((0x1 & value) << 4); } + public ulong Bit5 { get => this.Value >> 5 & 0x1; set => this.Value = (this.Value & ~(0x1UL << 5)) | ((0x1 & value) << 5); } + public ulong Bit6 { get => this.Value >> 6 & 0x1; set => this.Value = (this.Value & ~(0x1UL << 6)) | ((0x1 & value) << 6); } + public ulong Bit7 { get => this.Value >> 7 & 0x1; set => this.Value = (this.Value & ~(0x1UL << 7)) | ((0x1 & value) << 7); } + public ulong Bit8 { get => this.Value >> 8 & 0x1; set => this.Value = (this.Value & ~(0x1UL << 8)) | ((0x1 & value) << 8); } + public ulong Bit9 { get => this.Value >> 9 & 0x1; set => this.Value = (this.Value & ~(0x1UL << 9)) | ((0x1 & value) << 9); } + public ulong Bit10 { get => this.Value >> 10 & 0x1; set => this.Value = (this.Value & ~(0x1UL << 10)) | ((0x1 & value) << 10); } + public ulong Bit11 { get => this.Value >> 11 & 0x1; set => this.Value = (this.Value & ~(0x1UL << 11)) | ((0x1 & value) << 11); } + public ulong Bit12 { get => this.Value >> 12 & 0x1; set => this.Value = (this.Value & ~(0x1UL << 12)) | ((0x1 & value) << 12); } + public ulong Bit13 { get => this.Value >> 13 & 0x1; set => this.Value = (this.Value & ~(0x1UL << 13)) | ((0x1 & value) << 13); } + public ulong Bit14 { get => this.Value >> 14 & 0x1; set => this.Value = (this.Value & ~(0x1UL << 14)) | ((0x1 & value) << 14); } + public ulong Bit15 { get => this.Value >> 15 & 0x1; set => this.Value = (this.Value & ~(0x1UL << 15)) | ((0x1 & value) << 15); } + public ulong Bit16 { get => this.Value >> 16 & 0x1; set => this.Value = (this.Value & ~(0x1UL << 16)) | ((0x1 & value) << 16); } + public ulong Bit17 { get => this.Value >> 17 & 0x1; set => this.Value = (this.Value & ~(0x1UL << 17)) | ((0x1 & value) << 17); } + public ulong Bit18 { get => this.Value >> 18 & 0x1; set => this.Value = (this.Value & ~(0x1UL << 18)) | ((0x1 & value) << 18); } + public ulong Bit19 { get => this.Value >> 19 & 0x1; set => this.Value = (this.Value & ~(0x1UL << 19)) | ((0x1 & value) << 19); } + public ulong Bit20 { get => this.Value >> 20 & 0x1; set => this.Value = (this.Value & ~(0x1UL << 20)) | ((0x1 & value) << 20); } + public ulong Bit21 { get => this.Value >> 21 & 0x1; set => this.Value = (this.Value & ~(0x1UL << 21)) | ((0x1 & value) << 21); } + public ulong Bit22 { get => this.Value >> 22 & 0x1; set => this.Value = (this.Value & ~(0x1UL << 22)) | ((0x1 & value) << 22); } + public ulong Bit23 { get => this.Value >> 23 & 0x1; set => this.Value = (this.Value & ~(0x1UL << 23)) | ((0x1 & value) << 23); } + public ulong Bit24 { get => this.Value >> 24 & 0x1; set => this.Value = (this.Value & ~(0x1UL << 24)) | ((0x1 & value) << 24); } + public ulong Bit25 { get => this.Value >> 25 & 0x1; set => this.Value = (this.Value & ~(0x1UL << 25)) | ((0x1 & value) << 25); } + public ulong Bit26 { get => this.Value >> 26 & 0x1; set => this.Value = (this.Value & ~(0x1UL << 26)) | ((0x1 & value) << 26); } + public ulong Bit27 { get => this.Value >> 27 & 0x1; set => this.Value = (this.Value & ~(0x1UL << 27)) | ((0x1 & value) << 27); } + public ulong Bit28 { get => this.Value >> 28 & 0x1; set => this.Value = (this.Value & ~(0x1UL << 28)) | ((0x1 & value) << 28); } + public ulong Bit29 { get => this.Value >> 29 & 0x1; set => this.Value = (this.Value & ~(0x1UL << 29)) | ((0x1 & value) << 29); } + public ulong Bit30 { get => this.Value >> 30 & 0x1; set => this.Value = (this.Value & ~(0x1UL << 30)) | ((0x1 & value) << 30); } + public ulong Bit31 { get => this.Value >> 31 & 0x1; set => this.Value = (this.Value & ~(0x1UL << 31)) | ((0x1 & value) << 31); } + public ulong Bit32 { get => this.Value >> 32 & 0x1; set => this.Value = (this.Value & ~(0x1UL << 32)) | ((0x1 & value) << 32); } + public ulong Bit33 { get => this.Value >> 33 & 0x1; set => this.Value = (this.Value & ~(0x1UL << 33)) | ((0x1 & value) << 33); } + public ulong Bit34 { get => this.Value >> 34 & 0x1; set => this.Value = (this.Value & ~(0x1UL << 34)) | ((0x1 & value) << 34); } + public ulong Bit35 { get => this.Value >> 35 & 0x1; set => this.Value = (this.Value & ~(0x1UL << 35)) | ((0x1 & value) << 35); } + public ulong Bit36 { get => this.Value >> 36 & 0x1; set => this.Value = (this.Value & ~(0x1UL << 36)) | ((0x1 & value) << 36); } + public ulong Bit37 { get => this.Value >> 37 & 0x1; set => this.Value = (this.Value & ~(0x1UL << 37)) | ((0x1 & value) << 37); } + public ulong Bit38 { get => this.Value >> 38 & 0x1; set => this.Value = (this.Value & ~(0x1UL << 38)) | ((0x1 & value) << 38); } + public ulong Bit39 { get => this.Value >> 39 & 0x1; set => this.Value = (this.Value & ~(0x1UL << 39)) | ((0x1 & value) << 39); } + public ulong Bit40 { get => this.Value >> 40 & 0x1; set => this.Value = (this.Value & ~(0x1UL << 40)) | ((0x1 & value) << 40); } + public ulong Bit41 { get => this.Value >> 41 & 0x1; set => this.Value = (this.Value & ~(0x1UL << 41)) | ((0x1 & value) << 41); } + public ulong Bit42 { get => this.Value >> 42 & 0x1; set => this.Value = (this.Value & ~(0x1UL << 42)) | ((0x1 & value) << 42); } + public ulong Bit43 { get => this.Value >> 43 & 0x1; set => this.Value = (this.Value & ~(0x1UL << 43)) | ((0x1 & value) << 43); } + public ulong Bit44 { get => this.Value >> 44 & 0x1; set => this.Value = (this.Value & ~(0x1UL << 44)) | ((0x1 & value) << 44); } + public ulong Bit45 { get => this.Value >> 45 & 0x1; set => this.Value = (this.Value & ~(0x1UL << 45)) | ((0x1 & value) << 45); } + public ulong Bit46 { get => this.Value >> 46 & 0x1; set => this.Value = (this.Value & ~(0x1UL << 46)) | ((0x1 & value) << 46); } + public ulong Bit47 { get => this.Value >> 47 & 0x1; set => this.Value = (this.Value & ~(0x1UL << 47)) | ((0x1 & value) << 47); } + public ulong Bit48 { get => this.Value >> 48 & 0x1; set => this.Value = (this.Value & ~(0x1UL << 48)) | ((0x1 & value) << 48); } + public ulong Bit49 { get => this.Value >> 49 & 0x1; set => this.Value = (this.Value & ~(0x1UL << 49)) | ((0x1 & value) << 49); } + public ulong Bit50 { get => this.Value >> 50 & 0x1; set => this.Value = (this.Value & ~(0x1UL << 50)) | ((0x1 & value) << 50); } + public ulong Bit51 { get => this.Value >> 51 & 0x1; set => this.Value = (this.Value & ~(0x1UL << 51)) | ((0x1 & value) << 51); } + public ulong Bit52 { get => this.Value >> 52 & 0x1; set => this.Value = (this.Value & ~(0x1UL << 52)) | ((0x1 & value) << 52); } + public ulong Bit53 { get => this.Value >> 53 & 0x1; set => this.Value = (this.Value & ~(0x1UL << 53)) | ((0x1 & value) << 53); } + public ulong Bit54 { get => this.Value >> 54 & 0x1; set => this.Value = (this.Value & ~(0x1UL << 54)) | ((0x1 & value) << 54); } + public ulong Bit55 { get => this.Value >> 55 & 0x1; set => this.Value = (this.Value & ~(0x1UL << 55)) | ((0x1 & value) << 55); } + public ulong Bit56 { get => this.Value >> 56 & 0x1; set => this.Value = (this.Value & ~(0x1UL << 56)) | ((0x1 & value) << 56); } + public ulong Bit57 { get => this.Value >> 57 & 0x1; set => this.Value = (this.Value & ~(0x1UL << 57)) | ((0x1 & value) << 57); } + public ulong Bit58 { get => this.Value >> 58 & 0x1; set => this.Value = (this.Value & ~(0x1UL << 58)) | ((0x1 & value) << 58); } + public ulong Bit59 { get => this.Value >> 59 & 0x1; set => this.Value = (this.Value & ~(0x1UL << 59)) | ((0x1 & value) << 59); } + public ulong Bit60 { get => this.Value >> 60 & 0x1; set => this.Value = (this.Value & ~(0x1UL << 60)) | ((0x1 & value) << 60); } + public ulong Bit61 { get => this.Value >> 61 & 0x1; set => this.Value = (this.Value & ~(0x1UL << 61)) | ((0x1 & value) << 61); } + public ulong Bit62 { get => this.Value >> 62 & 0x1; set => this.Value = (this.Value & ~(0x1UL << 62)) | ((0x1 & value) << 62); } + public ulong Bit63 { get => this.Value >> 63 & 0x1; set => this.Value = (this.Value & ~(0x1UL << 63)) | ((0x1 & value) << 63); } + + public static implicit operator Int64BitStruct(ulong value) => new Int64BitStruct(value); + public static implicit operator Int64BitStruct(long value) => new Int64BitStruct(value); + public static implicit operator long(Int64BitStruct value) => (long)value.Value; + public static implicit operator ulong(Int64BitStruct value) => value.Value; + public static bool operator ==(Int64BitStruct first, Int64BitStruct second) + { + return first.Value == second.Value; + } + + public static bool operator !=(Int64BitStruct first, Int64BitStruct second) + { + return first.Value != second.Value; + } + + public static bool operator ==(Int64BitStruct first, ulong second) + { + return first.Value == second; + } + + public static bool operator !=(Int64BitStruct first, ulong second) + { + return first.Value != second; + } + + public static bool operator ==(Int64BitStruct first, long second) + { + unchecked + { + return first.Value == (ulong)second; + } + } + + public static bool operator !=(Int64BitStruct first, long second) + { + unchecked + { + return first.Value != (ulong)second; + } + } + + public static bool operator >=(Int64BitStruct first, Int64BitStruct second) + { + return first.Value >= second.Value; + } + + public static bool operator <=(Int64BitStruct first, Int64BitStruct second) + { + return first.Value <= second.Value; + } + + public static bool operator >=(Int64BitStruct first, long second) + { + unchecked + { + return first.Value >= (ulong)second; + } + } + + public static bool operator <=(Int64BitStruct first, long second) + { + unchecked + { + return first.Value <= (ulong)second; + } + } + + public static bool operator >=(Int64BitStruct first, ulong second) + { + unchecked + { + return first.Value >= second; + } + } + + public static bool operator <=(Int64BitStruct first, ulong second) + { + unchecked + { + return first.Value <= second; + } + } + + public bool Equals(Int64BitStruct other) + { + return this.Value == other.Value; + } + + public override bool Equals(object obj) + { + if (obj is Int64BitStruct) + { + return this.Equals((Int64BitStruct)obj); + } + else + { + return base.Equals(obj); + } + } + + public override int GetHashCode() + { + return this.Value.GetHashCode(); + } + } +} \ No newline at end of file diff --git a/SystemExtensions/SystemExtensions.csproj b/SystemExtensions/SystemExtensions.csproj index d28d10a..ed85aca 100644 --- a/SystemExtensions/SystemExtensions.csproj +++ b/SystemExtensions/SystemExtensions.csproj @@ -33,6 +33,8 @@ prompt 4 false + + @@ -55,10 +57,10 @@ + + - - - + \ No newline at end of file diff --git a/SystemExtensionsTests/Structures/Int32BitStructTests.cs b/SystemExtensionsTests/Structures/Int32BitStructTests.cs new file mode 100644 index 0000000..169ab49 --- /dev/null +++ b/SystemExtensionsTests/Structures/Int32BitStructTests.cs @@ -0,0 +1,68 @@ +using Microsoft.VisualStudio.TestTools.UnitTesting; +using SystemExtensions.Structures.BitStructures; + +namespace SystemExtensionsTests.Structures +{ + [TestClass] + public class Int32BitStructTests + { + [DataTestMethod] + [DataRow(-1)] + [DataRow(int.MaxValue)] + public void TestSetValueInt(int value) + { + Int32BitStruct int32 = value; + Assert.IsTrue(int32 == value); + } + + [DataTestMethod] + [DataRow(1U)] + [DataRow(uint.MaxValue)] + public void TestSetValueUint(uint value) + { + Int32BitStruct int32 = value; + Assert.IsTrue(int32 == value); + } + + [DataTestMethod] + [DataRow(0)] + [DataRow(int.MaxValue)] + [DataRow(int.MinValue)] + public void TestGetBit(int value) + { + Int32BitStruct int32 = value; + Assert.IsTrue(value >= 0 ? int32.Bit31 == 0 : int32.Bit31 == 1); + } + + [DataTestMethod] + [DataRow(0U)] + [DataRow(1U)] + public void TestSetBit(uint value) + { + Int32BitStruct int32 = 0; + int32.Bit0 = value; + Assert.IsTrue(int32 == value); + } + + [TestMethod] + public void TestUseCaseExample() + { + // Set value = -1, represented as MSB being 1 and the rest 0. + Int32BitStruct int32 = int.MinValue; + + // Test that MSB is 1. + Assert.IsTrue(int32.Bit31 == 1); + + // Set MSB to 0 and test that everything else is 0. + int32.Bit31 = 0; + Assert.IsTrue(int32 == 0); + + // Set the least 31 significant bits (all besides MSB) to 1. + int32 = int.MaxValue; + + // Set the MSB to 1 and test that everything is 1. + int32.Bit31 = 1; + Assert.IsTrue(int32 == uint.MaxValue); + } + } +} \ No newline at end of file diff --git a/SystemExtensionsTests/Structures/Int64BitStructTests.cs b/SystemExtensionsTests/Structures/Int64BitStructTests.cs new file mode 100644 index 0000000..55fe812 --- /dev/null +++ b/SystemExtensionsTests/Structures/Int64BitStructTests.cs @@ -0,0 +1,103 @@ +using Microsoft.VisualStudio.TestTools.UnitTesting; +using SystemExtensions.Structures.BitStructures; + +namespace SystemExtensionsTests.Structures +{ + [TestClass] + public class Int64BitStructTests + { + [DataTestMethod] + [DataRow(-1L)] + [DataRow(long.MaxValue)] + public void TestSetValueInt(long value) + { + Int64BitStruct int64 = value; + Assert.IsTrue(int64 == value); + } + + [DataTestMethod] + [DataRow(1UL)] + [DataRow(uint.MaxValue)] + public void TestSetValueUint(ulong value) + { + Int64BitStruct int64 = value; + Assert.IsTrue(int64 == value); + } + + [DataTestMethod] + [DataRow(0)] + [DataRow(long.MaxValue)] + [DataRow(long.MinValue)] + public void TestGetBit(long value) + { + Int64BitStruct int64 = value; + Assert.IsTrue(value >= 0L ? int64.Bit63 == 0 : int64.Bit63 == 1); + } + + [DataTestMethod] + [DataRow(0U)] + [DataRow(1U)] + public void TestSetBit(ulong value) + { + Int64BitStruct int64 = 0L; + int64.Bit0 = value; + Assert.IsTrue(int64 == value); + } + + [DataTestMethod] + [DataRow(int.MaxValue, int.MinValue)] + public void TestLowHigh(int low, int high) + { + Int64BitStruct int64 = new Int64BitStruct(low, high); + Int32BitStruct lowStruct = low; + Int32BitStruct highStruct = high; + Assert.IsTrue(int64.Low == lowStruct); + Assert.IsTrue(int64.High == highStruct); + unchecked + { + Assert.IsTrue(int64 == ((ulong)low + ((ulong)high << 32))); + } + } + + [TestMethod] + public void TestUseCaseExample() + { + // Set value = -1, represented as MSB being 1 and the rest 0. + Int64BitStruct int64 = long.MinValue; + + // Test that MSB is 1. + Assert.IsTrue(int64.Bit63 == 1); + + // Set MSB to 0 and test that everything else is 0. + int64.Bit63 = 0; + Assert.IsTrue(int64 == 0UL); + + // Set the least 31 significant bits (all besides MSB) to 1. + int64 = long.MaxValue; + + // Set the MSB to 1 and test that everything is 1. + int64.Bit63 = 1; + Assert.IsTrue(int64 == ulong.MaxValue); + } + + [TestMethod] + public void TestUseCaseExample2() + { + // Set all bits to 1. Check low and high values to be equal to having all 32 bits set. + Int64BitStruct int64 = ulong.MaxValue; + Assert.IsTrue(int64.Low == uint.MaxValue && int64.High == uint.MaxValue); + + // Set all 63 least significant bits to 1 and MSB to 0. + int64 = long.MaxValue; + Assert.IsTrue(int64.Bit63 == 0); + + // Check that high has all bits besides MSB set to 1 and that low has all bits set to 1. + Assert.IsTrue(int64.High == int.MaxValue && int64.Low == uint.MaxValue); + + // Set the MSB back to 1 and check that all bits are set to 1. + int64.Bit63 = 1; + Assert.IsTrue(int64 == ulong.MaxValue); + Assert.IsTrue(int64.High == uint.MaxValue && int64.Low == uint.MaxValue); + } + } +} \ No newline at end of file diff --git a/SystemExtensionsTests/SystemExtensionsTests.csproj b/SystemExtensionsTests/SystemExtensionsTests.csproj index 72d2c9c..75d363c 100644 --- a/SystemExtensionsTests/SystemExtensionsTests.csproj +++ b/SystemExtensionsTests/SystemExtensionsTests.csproj @@ -64,6 +64,8 @@ + +