Skip to content
This repository has been archived by the owner on Jul 8, 2023. It is now read-only.

Commit

Permalink
Added support for unsigned longs and integers
Browse files Browse the repository at this point in the history
Also added support for Short and its unsigned variant
  • Loading branch information
AptiviCEO committed Nov 27, 2020
1 parent 70c1aa2 commit 1c04e43
Show file tree
Hide file tree
Showing 7 changed files with 372 additions and 0 deletions.
22 changes: 22 additions & 0 deletions Extensification.Tests/Integers.vb
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,26 @@ Public Class IntegerTests
Assert.AreEqual(ExpectedInteger, TargetInteger)
End Sub

''' <summary>
''' Tests unsigned integer incrementation
''' </summary>
<TestMethod>
Public Sub TestIncrementUnsigned()
Dim ExpectedUInteger As UInteger = 5
Dim TargetUInteger As UInteger = 3
TargetUInteger = TargetUInteger.Increment(2)
Assert.AreEqual(ExpectedUInteger, TargetUInteger)
End Sub

''' <summary>
''' Tests unsigned integer decrementation
''' </summary>
<TestMethod>
Public Sub TestDecrementUnsigned()
Dim ExpectedUInteger As UInteger = 3
Dim TargetUInteger As UInteger = 5
TargetUInteger = TargetUInteger.Decrement(2)
Assert.AreEqual(ExpectedUInteger, TargetUInteger)
End Sub

End Class
82 changes: 82 additions & 0 deletions Extensification.Tests/Long.vb
Original file line number Diff line number Diff line change
Expand Up @@ -86,4 +86,86 @@ Public Class LongTests
Assert.AreEqual(Expected, Number.ToHumanReadable(HumanFormats.VolumeImperial))
End Sub

''' <summary>
''' Tests unsigned long integer incrementation
''' </summary>
<TestMethod>
Public Sub TestIncrementUnsigned()
Dim ExpectedULong As ULong = 5
Dim TargetULong As ULong = 3
TargetULong = TargetULong.Increment(2)
Assert.AreEqual(ExpectedULong, TargetULong)
End Sub

''' <summary>
''' Tests unsigned long integer decrementation
''' </summary>
<TestMethod>
Public Sub TestDecrementUnsigned()
Dim ExpectedULong As ULong = 3
Dim TargetULong As ULong = 5
TargetULong = TargetULong.Decrement(2)
Assert.AreEqual(ExpectedULong, TargetULong)
End Sub

''' <summary>
''' Tests converting data size to human readable (Unsigned long)
''' </summary>
<TestMethod>
Public Sub TestDataToHumanReadableUnsigned()
Dim Expected As String = "4.2 MB"
Dim Number As ULong = 4200000
Assert.AreEqual(Expected, Number.ToHumanReadable(HumanFormats.DataSize))
End Sub

''' <summary>
''' Tests converting metric measurement without unusual measurements to human readable (Unsigned long)
''' </summary>
<TestMethod>
Public Sub TestMetricMeasurementToHumanReadableUnsigned()
Dim Expected As String = "12.5 centimeters"
Dim Number As ULong = 125
Assert.AreEqual(Expected, Number.ToHumanReadable(HumanFormats.MeasurementsMetric))
End Sub

''' <summary>
''' Tests converting metric measurement with unusual measurements to human readable (Unsigned long)
''' </summary>
<TestMethod>
Public Sub TestMetricMeasurementUnusualToHumanReadableUnsigned()
Dim Expected As String = "1.25 decimeters"
Dim Number As ULong = 125
Assert.AreEqual(Expected, Number.ToHumanReadable(HumanFormats.MeasurementsMetricUnusual))
End Sub

''' <summary>
''' Tests converting imperial measurement to human readable (Unsigned long)
''' </summary>
<TestMethod>
Public Sub TestImperialMeasurementToHumanReadableUnsigned()
Dim Expected As String = "1.10479797979798 miles"
Dim Number As ULong = 70000
Assert.AreEqual(Expected, Number.ToHumanReadable(HumanFormats.MeasurementsImperial))
End Sub

''' <summary>
''' Tests converting metric volume measurement to human readable (Unsigned long)
''' </summary>
<TestMethod>
Public Sub TestMetricVolumeToHumanReadableUnsigned()
Dim Expected As String = "2.5 liters"
Dim Number As ULong = 2500
Assert.AreEqual(Expected, Number.ToHumanReadable(HumanFormats.VolumeMetric))
End Sub

''' <summary>
''' Tests converting metric volume measurement to human readable (Unsigned long)
''' </summary>
<TestMethod>
Public Sub TestImperialVolumeToHumanReadableUnsigned()
Dim Expected As String = "2 quarts"
Dim Number As ULong = 4
Assert.AreEqual(Expected, Number.ToHumanReadable(HumanFormats.VolumeImperial))
End Sub

End Class
51 changes: 51 additions & 0 deletions Extensification.Tests/Short.vb
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
Imports Microsoft.VisualStudio.TestTools.UnitTesting
Imports Extensification.ShortExts

<TestClass>
Public Class ShortTests

''' <summary>
''' Tests short integer incrementation
''' </summary>
<TestMethod>
Public Sub TestIncrement()
Dim ExpectedShort As Short = 5
Dim TargetShort As Short = 3
TargetShort = TargetShort.Increment(2)
Assert.AreEqual(ExpectedShort, TargetShort)
End Sub

''' <summary>
''' Tests short integer decrementation
''' </summary>
<TestMethod>
Public Sub TestDecrement()
Dim ExpectedShort As Short = 3
Dim TargetShort As Short = 5
TargetShort = TargetShort.Decrement(2)
Assert.AreEqual(ExpectedShort, TargetShort)
End Sub

''' <summary>
''' Tests unsigned short integer incrementation
''' </summary>
<TestMethod>
Public Sub TestIncrementUnsigned()
Dim ExpectedUShort As UShort = 5
Dim TargetUShort As UShort = 3
TargetUShort = TargetUShort.Increment(2)
Assert.AreEqual(ExpectedUShort, TargetUShort)
End Sub

''' <summary>
''' Tests unsigned short integer decrementation
''' </summary>
<TestMethod>
Public Sub TestDecrementUnsigned()
Dim ExpectedUShort As UShort = 3
Dim TargetUShort As UShort = 5
TargetUShort = TargetUShort.Decrement(2)
Assert.AreEqual(ExpectedUShort, TargetUShort)
End Sub

End Class
1 change: 1 addition & 0 deletions Extensification/Extensification.vbproj
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
<PackageReleaseNotes>2020.2.0:
- Fixed GetIndexOfEntry taking only string entries
- Added new extensions
- Added support for UInteger, ULong, Short, and UShort
- Added some extensions from .NET Core

2020.1.0:
Expand Down
26 changes: 26 additions & 0 deletions Extensification/Integers.vb
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,31 @@ Namespace IntegerExts
Return Number
End Function

''' <summary>
''' Increments the number
''' </summary>
''' <param name="Number">Number</param>
''' <param name="IncrementThreshold">How many times to increment</param>
''' <returns>Incremented number</returns>
<Extension>
Public Function Increment(ByVal Number As UInteger, ByVal IncrementThreshold As Integer) As Integer
If IncrementThreshold < 0 Then Throw New InvalidOperationException("Threshold is negative. Use Decrement().")
Number += IncrementThreshold
Return Number
End Function

''' <summary>
''' Decrements the number
''' </summary>
''' <param name="Number">Number</param>
''' <param name="DecrementThreshold">How many times to decrement</param>
''' <returns>Decremented number</returns>
<Extension>
Public Function Decrement(ByVal Number As UInteger, ByVal DecrementThreshold As Integer) As Integer
If DecrementThreshold < 0 Then Throw New InvalidOperationException("Threshold is negative. Use Increment().")
Number -= DecrementThreshold
Return Number
End Function

End Module
End Namespace
131 changes: 131 additions & 0 deletions Extensification/Long.vb
Original file line number Diff line number Diff line change
Expand Up @@ -168,5 +168,136 @@ Namespace LongExts
Return Number
End Function

''' <summary>
''' Converts number to a human readable format with units used
''' </summary>
''' <param name="Num">Number</param>
''' <param name="Type">Measurement types</param>
''' <returns>A string containing the processed number and unit</returns>
<Extension>
Public Function ToHumanReadable(ByVal Num As ULong, ByVal Type As HumanFormats) As String
Dim HumanString As String = ""
Dim HumanNum As Double
Select Case Type
Case HumanFormats.DataSize
If Num >= 1000000000000000000 Then 'EB
HumanNum = Num / 1000000000000000000
HumanString = HumanNum & " EB"
ElseIf Num >= 1000000000000000 Then 'PB
HumanNum = Num / 1000000000000000
HumanString = HumanNum & " PB"
ElseIf Num >= 1000000000000 Then 'TB
HumanNum = Num / 1000000000000
HumanString = HumanNum & " TB"
ElseIf Num >= 1000000000 Then 'GB
HumanNum = Num / 1000000000
HumanString = HumanNum & " GB"
ElseIf Num >= 1000000 Then 'MB
HumanNum = Num / 1000000
HumanString = HumanNum & " MB"
ElseIf Num >= 1000 Then 'KB
HumanNum = Num / 1000
HumanString = HumanNum & " KB"
Else 'bytes
HumanString = HumanNum & " bytes"
End If
Case HumanFormats.MeasurementsImperial
If Num >= 63360 Then 'miles
HumanNum = Num / 63360
HumanString = HumanNum & " miles"
ElseIf Num >= 36 Then 'yards
HumanNum = Num / 36
HumanString = HumanNum & " yards"
ElseIf Num >= 12 Then 'feet
HumanNum = Num / 12
HumanString = HumanNum & " feet"
Else 'inches
HumanString = Num & " inches"
End If
Case HumanFormats.MeasurementsMetric
If Num >= 1000000 Then 'km
HumanNum = Num / 1000000
HumanString = HumanNum & " kilometers"
ElseIf Num >= 1000 Then 'm
HumanNum = Num / 1000
HumanString = HumanNum & " meters"
ElseIf Num >= 10 Then 'cm
HumanNum = Num / 10
HumanString = HumanNum & " centimeters"
Else 'mm
HumanString = Num & " millimeters"
End If
Case HumanFormats.MeasurementsMetricUnusual
If Num >= 1000000 Then 'km
HumanNum = Num / 1000000
HumanString = HumanNum & " kilometers"
ElseIf Num >= 100000 Then 'hm
HumanNum = Num / 100000
HumanString = HumanNum & " hectameters"
ElseIf Num >= 10000 Then 'dcm
HumanNum = Num / 10000
HumanString = HumanNum & " decameters"
ElseIf Num >= 1000 Then 'm
HumanNum = Num / 1000
HumanString = HumanNum & " meters"
ElseIf Num >= 100 Then 'dm
HumanNum = Num / 100
HumanString = HumanNum & " decimeters"
ElseIf Num >= 10 Then 'cm
HumanNum = Num / 10
HumanString = HumanNum & " centimeters"
Else 'mm
HumanString = Num & " millimeters"
End If
Case HumanFormats.VolumeMetric
If Num >= 1000000 Then 'kL
HumanNum = Num / 1000000
HumanString = HumanNum & " kiloliters"
ElseIf Num >= 1000 Then 'L
HumanNum = Num / 1000
HumanString = HumanNum & " liters"
Else 'mL
HumanString = Num & " milliliters"
End If
Case HumanFormats.VolumeImperial
If Num >= 8 Then 'gallons
HumanNum = Num / 8
HumanString = HumanNum & " gallons"
ElseIf Num >= 2 Then 'quarts
HumanNum = Num / 2
HumanString = HumanNum & " quarts"
Else 'pints
HumanString = Num & " pints"
End If
End Select
Return HumanString
End Function

''' <summary>
''' Increments the number
''' </summary>
''' <param name="Number">Number</param>
''' <param name="IncrementThreshold">How many times to increment</param>
''' <returns>Incremented number</returns>
<Extension>
Public Function Increment(ByVal Number As ULong, ByVal IncrementThreshold As Long) As Long
If IncrementThreshold < 0 Then Throw New InvalidOperationException("Threshold is negative. Use Decrement().")
Number += IncrementThreshold
Return Number
End Function

''' <summary>
''' Decrements the number
''' </summary>
''' <param name="Number">Number</param>
''' <param name="DecrementThreshold">How many times to decrement</param>
''' <returns>Decremented number</returns>
<Extension>
Public Function Decrement(ByVal Number As ULong, ByVal DecrementThreshold As Long) As Long
If DecrementThreshold < 0 Then Throw New InvalidOperationException("Threshold is negative. Use Increment().")
Number -= DecrementThreshold
Return Number
End Function

End Module
End Namespace
Loading

0 comments on commit 1c04e43

Please sign in to comment.