├── restore.ps1 ├── .gitattributes ├── src ├── DotNetCross.NativeInts │ ├── NativeIntsRef-Debug.res │ ├── NativeIntsRef-Release.res │ └── DotNetCross.NativeInts.csproj ├── DotNetCross.NativeInts.Tests │ ├── packages.config │ ├── Properties │ │ └── AssemblyInfo.cs │ ├── UIntPtrTest.cs │ ├── DotNetCross.NativeInts.Tests.csproj │ ├── IntPtrTest.cs │ ├── nuintTest.cs │ └── nintTest.cs ├── Relative_Output_Path.props ├── Common.props ├── Executable_Project_Output.props ├── Library_Project_Output.props ├── Test_Project_Output.props └── DotNetCross.NativeInts.Ref │ ├── DotNetCross.NativeInts.Ref.csproj │ ├── nint.cs │ └── nuint.cs ├── all.ps1 ├── nuget.config ├── ildasm.bat ├── test.ps1 ├── pack.ps1 ├── .vscode └── launch.json ├── LICENSE ├── ilasm.bat ├── src-pack └── DotNetCross.NativeInts.nuspec ├── NativeInts.sln ├── .gitignore └── README.md /restore.ps1: -------------------------------------------------------------------------------- 1 | dotnet restore -v m -------------------------------------------------------------------------------- /.gitattributes: -------------------------------------------------------------------------------- 1 | * text=auto 2 | *.cs diff=csharp -------------------------------------------------------------------------------- /src/DotNetCross.NativeInts/NativeIntsRef-Debug.res: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DotNetCross/NativeInts/HEAD/src/DotNetCross.NativeInts/NativeIntsRef-Debug.res -------------------------------------------------------------------------------- /all.ps1: -------------------------------------------------------------------------------- 1 | .\restore.ps1 2 | .\build.ps1 3 | .\test.ps1 4 | Write-Host "Check output for errors, since scripts do not stop if errors occur." -foregroundcolor "yellow" -------------------------------------------------------------------------------- /src/DotNetCross.NativeInts/NativeIntsRef-Release.res: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DotNetCross/NativeInts/HEAD/src/DotNetCross.NativeInts/NativeIntsRef-Release.res -------------------------------------------------------------------------------- /nuget.config: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /src/DotNetCross.NativeInts.Tests/packages.config: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | -------------------------------------------------------------------------------- /src/Relative_Output_Path.props: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | ../../build/ 5 | 6 | 7 | -------------------------------------------------------------------------------- /ildasm.bat: -------------------------------------------------------------------------------- 1 | "C:\Program Files (x86)\Microsoft SDKs\Windows\v10.0A\bin\NETFX 4.6.1 Tools\ildasm.exe" .\src\DotNetCross.NativeInts.Ref\bin\Debug\netstandard1.0\DotNetCross.NativeInts.dll /OUT=.\src\DotNetCross.NativeInts\NativeIntsRef-Debug.il 2 | "C:\Program Files (x86)\Microsoft SDKs\Windows\v10.0A\bin\NETFX 4.6.1 Tools\ildasm.exe" .\src\DotNetCross.NativeInts.Ref\bin\Release\netstandard1.0\DotNetCross.NativeInts.dll /OUT=.\src\DotNetCross.NativeInts\NativeIntsRef-Release.il 3 | -------------------------------------------------------------------------------- /test.ps1: -------------------------------------------------------------------------------- 1 | dotnet test src/DotNetCross.NativeInts.Tests/DotNetCross.NativeInts.Tests.csproj -c Debug -v q /nologo /property:Platform=X64 2 | dotnet test src/DotNetCross.NativeInts.Tests/DotNetCross.NativeInts.Tests.csproj -c Release -v q /nologo /property:Platform=X64 3 | dotnet test src/DotNetCross.NativeInts.Tests/DotNetCross.NativeInts.Tests.csproj -c Debug -v q /nologo /property:Platform=X86 4 | dotnet test src/DotNetCross.NativeInts.Tests/DotNetCross.NativeInts.Tests.csproj -c Release -v q /nologo /property:Platform=X86 -------------------------------------------------------------------------------- /src/Common.props: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | $(ProjectName) 5 | $(AssemblyName) 6 | AnyCPU 7 | 8 | 9 | -------------------------------------------------------------------------------- /src/Executable_Project_Output.props: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | $(OutputRelativePath)/$(AssemblyName)_$(Platform)_$(Configuration) 8 | $(OutputRelativePath)/Obj_Exe/$(AssemblyName)_$(Platform) 9 | $(BaseIntermediateOutputPath)_$(Configuration) 10 | 11 | 12 | -------------------------------------------------------------------------------- /pack.ps1: -------------------------------------------------------------------------------- 1 | $sourceNugetExe = "https://dist.nuget.org/win-x86-commandline/latest/nuget.exe" 2 | # We download into packages so it is not checked-in since this is ignored in git 3 | $packagesPath = ".\packages\" 4 | $targetNugetExe = $packagesPath + "nuget.exe" 5 | # Download if it does not exist 6 | If (!(Test-Path $targetNugetExe)) 7 | { 8 | If (!(Test-Path $packagesPath)) 9 | { 10 | mkdir $packagesPath 11 | } 12 | "Downloading nuget to: " + $targetNugetExe 13 | Invoke-WebRequest $sourceNugetExe -OutFile $targetNugetExe 14 | } 15 | xcopy /Y .\build\Libs_AnyCPU_Release\DotNetCross.NativeInts.dll .\src-pack\ 16 | iex ($targetNugetExe + " pack .\src-pack\DotNetCross.NativeInts.nuspec") -------------------------------------------------------------------------------- /src/Library_Project_Output.props: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | $(OutputRelativePath)/Libs_$(Platform)_$(Configuration)/ 8 | $(OutputRelativePath)/Obj_Libs/$(ProjectOrAssemblyName)_$(Platform) 9 | $(BaseIntermediateOutputPath)_$(Configuration)/ 10 | $(IntermediateOutputPath) 11 | $(OutputPath) 12 | 13 | 14 | -------------------------------------------------------------------------------- /src/Test_Project_Output.props: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | $(OutputRelativePath)/Tests/$(ProjectOrAssemblyName)_$(Platform)_$(Configuration)/ 8 | $(OutputRelativePath)/Obj_Tests/$(ProjectOrAssemblyName)_$(Platform) 9 | $(BaseIntermediateOutputPath)_$(Configuration)/ 10 | $(IntermediateOutputPath) 11 | $(OutputPath) 12 | 13 | 14 | -------------------------------------------------------------------------------- /src/DotNetCross.NativeInts.Tests/Properties/AssemblyInfo.cs: -------------------------------------------------------------------------------- 1 | using System.Reflection; 2 | using System.Runtime.CompilerServices; 3 | using System.Runtime.InteropServices; 4 | 5 | [assembly: AssemblyTitle("DotNetCross.NativeInts.TestsFramework")] 6 | [assembly: AssemblyDescription("")] 7 | [assembly: AssemblyConfiguration("")] 8 | [assembly: AssemblyCompany("")] 9 | [assembly: AssemblyProduct("DotNetCross.NativeInts.TestsFramework")] 10 | [assembly: AssemblyCopyright("Copyright © 2017")] 11 | [assembly: AssemblyTrademark("")] 12 | [assembly: AssemblyCulture("")] 13 | 14 | [assembly: ComVisible(false)] 15 | 16 | [assembly: Guid("dc21d6ba-da0e-4c4a-9be0-ab8075eea68d")] 17 | 18 | // [assembly: AssemblyVersion("1.0.*")] 19 | [assembly: AssemblyVersion("1.0.0.0")] 20 | [assembly: AssemblyFileVersion("1.0.0.0")] 21 | -------------------------------------------------------------------------------- /.vscode/launch.json: -------------------------------------------------------------------------------- 1 | { 2 | "version": "0.2.0", 3 | "configurations": [ 4 | { 5 | "name": ".NET Core Launch (console)", 6 | "type": "coreclr", 7 | "request": "launch", 8 | "preLaunchTask": "build", 9 | "program": "${workspaceRoot}\\src\\DotNetCross.NativeInts.Tests\\bin\\Debug\\netcoreapp1.0\\DotNetCross.NativeInts.Tests.dll", 10 | "args": [], 11 | "cwd": "${workspaceRoot}", 12 | "externalConsole": false, 13 | "stopAtEntry": false, 14 | "internalConsoleOptions": "openOnSessionStart" 15 | }, 16 | { 17 | "name": ".NET Core Attach", 18 | "type": "coreclr", 19 | "request": "attach", 20 | "processId": "${command.pickProcess}" 21 | } 22 | ] 23 | } -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2016 DotNetCross 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /src/DotNetCross.NativeInts.Ref/DotNetCross.NativeInts.Ref.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | netstandard1.0 5 | DotNetCross.NativeInts 6 | DotNetCross.NativeInts 7 | 0.1.0 8 | 10 | DotNetCross.NativeInts.nietras 11 | 12 | nietras 13 | Straightforward 'nint' and 'nuint' native integers, written in IL, probably second best to proper compiler support. 14 | false 15 | First release 16 | Copyright 2017 (c) nietras. All rights reserved. 17 | 18 | 19 | 20 | 21 | True 22 | 23 | 24 | -------------------------------------------------------------------------------- /ilasm.bat: -------------------------------------------------------------------------------- 1 | if not exist ".\build\Libs_AnyCPU_Debug\" mkdir .\build\Libs_AnyCPU_Debug\ 2 | if not exist ".\build\Libs_AnyCPU_Release\" mkdir .\build\Libs_AnyCPU_Release\ 3 | "C:\Windows\Microsoft.NET\Framework\v4.0.30319\Ilasm.exe" /nologo /dll /debug .\src\DotNetCross.NativeInts\NativeInts.il /OUTPUT=.\build\Libs_AnyCPU_Debug\NativeInts.dll 4 | "C:\Windows\Microsoft.NET\Framework\v4.0.30319\Ilasm.exe" /nologo /dll /optimize .\src\DotNetCross.NativeInts\NativeInts.il /OUTPUT=.\build\Libs_AnyCPU_Release\NativeInts.dll 5 | REM .\tools\ilasm.exe /dll /optimize .\src\DotNetCross.NativeInts\NativeInts.il /OUTPUT=.\build\Libs_AnyCPU_Debug\NativeInts.dll 6 | REM "C:\Windows\Microsoft.NET\Framework\v4.0.30319\Ilasm.exe" /nologo /dll /debug .\src\DotNetCross.NativeInts\NativeInts.il /OUTPUT=.\build\Libs_AnyCPU_Debug\NativeInts.dll 7 | REM "C:\Windows\Microsoft.NET\Framework\v4.0.30319\Ilasm.exe" /NOLOGO /DLL /DEBUG /OUTPUT:"./build/Libs_AnyCPU_Debug/DotNetCross.NativeInts.dll" ".\src\DotNetCross.NativeInts\NativeInts.il" 8 | REM "C:\Windows\Microsoft.NET\Framework\v4.0.30319\Ilasm.exe" /NOLOGO /DLL /DEBUG /OUTPUT:"obj\Debug\DotNetCross.NativeInts.dll" "E:\oss\NativeInts\src\DotNetCross.NativeInts\NativeInts.il" 9 | -------------------------------------------------------------------------------- /src-pack/DotNetCross.NativeInts.nuspec: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | DotNetCross.NativeInts 5 | 0.1.0 6 | nietras 7 | nietras 8 | https://github.com/DotNetCross/NativeInts/blob/master/LICENSE 9 | https://github.com/DotNetCross/NativeInts 10 | false 11 | Straightforward `nint` and `nuint` native integers, written in IL, probably second best to proper compiler support. 12 | Fix nuspec for netstandard2.0. 13 | © nietras. All rights reserved. 14 | native integers 32-bit 64-bit nint nuint msil dotnet-standard performance 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | -------------------------------------------------------------------------------- /src/DotNetCross.NativeInts.Tests/UIntPtrTest.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using Microsoft.VisualStudio.TestTools.UnitTesting; 3 | 4 | namespace DotNetCross.NativeInts.Tests 5 | { 6 | [TestClass] 7 | public class UIntPtrTest 8 | { 9 | private const string NotAvailable = "This feature not available for UIntPtr"; 10 | 11 | [TestMethod] 12 | public void operator_Addition_IntPtr() 13 | { 14 | Assert.Inconclusive(NotAvailable); 15 | //var ip = new UIntPtr(1); 16 | //Assert.AreEqual(ip + new UIntPtr(1), new UIntPtr(2)); 17 | //Assert.AreEqual(new UIntPtr(1) + ip, new UIntPtr(2)); 18 | } 19 | [TestMethod] 20 | public void operator_Addition_int() 21 | { 22 | var ip = new UIntPtr(1); 23 | Assert.AreEqual(ip + 1, new UIntPtr(2)); 24 | // Below not available 25 | //Assert.AreEqual(1 + ip, new UIntPtr(2)); 26 | } 27 | [TestMethod] 28 | public void operator_Addition_uint() 29 | { 30 | Assert.Inconclusive(NotAvailable); 31 | //var ip = new UIntPtr(1); 32 | //Assert.AreEqual(ip + 1u, new UIntPtr(2)); 33 | //Assert.AreEqual(1u + ip, new UIntPtr(2)); 34 | } 35 | 36 | [TestMethod] 37 | public void operator_Subtraction_UIntPtr() 38 | { 39 | Assert.Inconclusive(NotAvailable); 40 | //var ip = new UIntPtr(1); 41 | //Assert.AreEqual(ip - new UIntPtr(1), new UIntPtr(0)); 42 | //Assert.AreEqual(new UIntPtr(1) - ip, new UIntPtr(0)); 43 | } 44 | [TestMethod] 45 | public void operator_Subtraction_int() 46 | { 47 | var ip = new UIntPtr(1); 48 | Assert.AreEqual(ip - 1, new UIntPtr(0)); 49 | // Below not available 50 | //Assert.AreEqual(1 - ip, new UIntPtr(0)); 51 | } 52 | [TestMethod] 53 | public void operator_Subtraction_uint() 54 | { 55 | Assert.Inconclusive(NotAvailable); 56 | //var ip = new UIntPtr(1); 57 | //Assert.AreEqual(ip - 1u, new UIntPtr(0)); 58 | //Assert.AreEqual(1u - ip, new UIntPtr(0)); 59 | } 60 | 61 | [TestMethod] 62 | public void operator_AdditionAssignment() 63 | { 64 | var ip = new UIntPtr(7); 65 | ip += 1; 66 | Assert.AreEqual(ip, new UIntPtr(8)); 67 | } 68 | [TestMethod] 69 | public void operator_SubtractionAssignment() 70 | { 71 | var ip = new UIntPtr(7); 72 | ip -= 1; 73 | Assert.AreEqual(ip, new UIntPtr(6)); 74 | } 75 | } 76 | } 77 | -------------------------------------------------------------------------------- /src/DotNetCross.NativeInts/DotNetCross.NativeInts.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | Debug 6 | AnyCPU 7 | {4AE5D1B8-DCA8-4943-BFEA-3596F093E61C} 8 | Library 9 | Properties 10 | DotNetCross.NativeInts 11 | DotNetCross.NativeInts 12 | v4.0 13 | 512 14 | 15 | 16 | 17 | 18 | 19 | true 20 | full 21 | false 22 | DEBUG;TRACE 23 | prompt 24 | 4 25 | 26 | 27 | pdbonly 28 | true 29 | TRACE 30 | prompt 31 | 4 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | C:\Windows\Microsoft.NET\Framework\v4.0.30319\ 48 | 49 | 50 | "$(FrameworkPath)\Ilasm.exe" /NOLOGO /DLL /OUTPUT:"@(IntermediateAssembly)" 51 | 52 | 53 | 54 | 55 | 56 | $(IlAsmCommand) /OPTIMIZE 57 | 58 | 59 | $(IlAsmCommand) /KEY:"$(AssemblyOriginatorKeyFile)" 60 | 61 | 62 | 63 | 64 | -------------------------------------------------------------------------------- /NativeInts.sln: -------------------------------------------------------------------------------- 1 | 2 | Microsoft Visual Studio Solution File, Format Version 12.00 3 | # Visual Studio 15 4 | VisualStudioVersion = 15.0.26228.10 5 | MinimumVisualStudioVersion = 10.0.40219.1 6 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DotNetCross.NativeInts", "src\DotNetCross.NativeInts\DotNetCross.NativeInts.csproj", "{4AE5D1B8-DCA8-4943-BFEA-3596F093E61C}" 7 | EndProject 8 | Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "root", "root", "{18128D4D-3526-4A0A-8F78-DB65BE75C063}" 9 | ProjectSection(SolutionItems) = preProject 10 | all.ps1 = all.ps1 11 | build.ps1 = build.ps1 12 | ilasm.bat = ilasm.bat 13 | ildasm.bat = ildasm.bat 14 | LICENSE = LICENSE 15 | pack.ps1 = pack.ps1 16 | README.md = README.md 17 | restore.ps1 = restore.ps1 18 | test.ps1 = test.ps1 19 | EndProjectSection 20 | EndProject 21 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DotNetCross.NativeInts.Ref", "src\DotNetCross.NativeInts.Ref\DotNetCross.NativeInts.Ref.csproj", "{7DB8DB1D-C894-4C23-9379-5DCA9D9BCF27}" 22 | EndProject 23 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DotNetCross.NativeInts.Tests", "src\DotNetCross.NativeInts.Tests\DotNetCross.NativeInts.Tests.csproj", "{DC21D6BA-DA0E-4C4A-9BE0-AB8075EEA68D}" 24 | EndProject 25 | Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src-pack", "src-pack", "{909A5D54-EEDA-4925-A7DE-1B839EFC0FDD}" 26 | ProjectSection(SolutionItems) = preProject 27 | src-pack\DotNetCross.NativeInts.nuspec = src-pack\DotNetCross.NativeInts.nuspec 28 | EndProjectSection 29 | EndProject 30 | Global 31 | GlobalSection(SolutionConfigurationPlatforms) = preSolution 32 | Debug|Any CPU = Debug|Any CPU 33 | Debug|x64 = Debug|x64 34 | Debug|x86 = Debug|x86 35 | Release|Any CPU = Release|Any CPU 36 | Release|x64 = Release|x64 37 | Release|x86 = Release|x86 38 | EndGlobalSection 39 | GlobalSection(ProjectConfigurationPlatforms) = postSolution 40 | {4AE5D1B8-DCA8-4943-BFEA-3596F093E61C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 41 | {4AE5D1B8-DCA8-4943-BFEA-3596F093E61C}.Debug|Any CPU.Build.0 = Debug|Any CPU 42 | {4AE5D1B8-DCA8-4943-BFEA-3596F093E61C}.Debug|x64.ActiveCfg = Debug|Any CPU 43 | {4AE5D1B8-DCA8-4943-BFEA-3596F093E61C}.Debug|x64.Build.0 = Debug|Any CPU 44 | {4AE5D1B8-DCA8-4943-BFEA-3596F093E61C}.Debug|x86.ActiveCfg = Debug|Any CPU 45 | {4AE5D1B8-DCA8-4943-BFEA-3596F093E61C}.Debug|x86.Build.0 = Debug|Any CPU 46 | {4AE5D1B8-DCA8-4943-BFEA-3596F093E61C}.Release|Any CPU.ActiveCfg = Release|Any CPU 47 | {4AE5D1B8-DCA8-4943-BFEA-3596F093E61C}.Release|Any CPU.Build.0 = Release|Any CPU 48 | {4AE5D1B8-DCA8-4943-BFEA-3596F093E61C}.Release|x64.ActiveCfg = Release|Any CPU 49 | {4AE5D1B8-DCA8-4943-BFEA-3596F093E61C}.Release|x64.Build.0 = Release|Any CPU 50 | {4AE5D1B8-DCA8-4943-BFEA-3596F093E61C}.Release|x86.ActiveCfg = Release|Any CPU 51 | {4AE5D1B8-DCA8-4943-BFEA-3596F093E61C}.Release|x86.Build.0 = Release|Any CPU 52 | {7DB8DB1D-C894-4C23-9379-5DCA9D9BCF27}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 53 | {7DB8DB1D-C894-4C23-9379-5DCA9D9BCF27}.Debug|Any CPU.Build.0 = Debug|Any CPU 54 | {7DB8DB1D-C894-4C23-9379-5DCA9D9BCF27}.Debug|x64.ActiveCfg = Debug|Any CPU 55 | {7DB8DB1D-C894-4C23-9379-5DCA9D9BCF27}.Debug|x64.Build.0 = Debug|Any CPU 56 | {7DB8DB1D-C894-4C23-9379-5DCA9D9BCF27}.Debug|x86.ActiveCfg = Debug|Any CPU 57 | {7DB8DB1D-C894-4C23-9379-5DCA9D9BCF27}.Debug|x86.Build.0 = Debug|Any CPU 58 | {7DB8DB1D-C894-4C23-9379-5DCA9D9BCF27}.Release|Any CPU.ActiveCfg = Release|Any CPU 59 | {7DB8DB1D-C894-4C23-9379-5DCA9D9BCF27}.Release|Any CPU.Build.0 = Release|Any CPU 60 | {7DB8DB1D-C894-4C23-9379-5DCA9D9BCF27}.Release|x64.ActiveCfg = Release|Any CPU 61 | {7DB8DB1D-C894-4C23-9379-5DCA9D9BCF27}.Release|x64.Build.0 = Release|Any CPU 62 | {7DB8DB1D-C894-4C23-9379-5DCA9D9BCF27}.Release|x86.ActiveCfg = Release|Any CPU 63 | {7DB8DB1D-C894-4C23-9379-5DCA9D9BCF27}.Release|x86.Build.0 = Release|Any CPU 64 | {DC21D6BA-DA0E-4C4A-9BE0-AB8075EEA68D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 65 | {DC21D6BA-DA0E-4C4A-9BE0-AB8075EEA68D}.Debug|Any CPU.Build.0 = Debug|Any CPU 66 | {DC21D6BA-DA0E-4C4A-9BE0-AB8075EEA68D}.Debug|x64.ActiveCfg = Debug|Any CPU 67 | {DC21D6BA-DA0E-4C4A-9BE0-AB8075EEA68D}.Debug|x64.Build.0 = Debug|Any CPU 68 | {DC21D6BA-DA0E-4C4A-9BE0-AB8075EEA68D}.Debug|x86.ActiveCfg = Debug|Any CPU 69 | {DC21D6BA-DA0E-4C4A-9BE0-AB8075EEA68D}.Debug|x86.Build.0 = Debug|Any CPU 70 | {DC21D6BA-DA0E-4C4A-9BE0-AB8075EEA68D}.Release|Any CPU.ActiveCfg = Release|Any CPU 71 | {DC21D6BA-DA0E-4C4A-9BE0-AB8075EEA68D}.Release|Any CPU.Build.0 = Release|Any CPU 72 | {DC21D6BA-DA0E-4C4A-9BE0-AB8075EEA68D}.Release|x64.ActiveCfg = Release|Any CPU 73 | {DC21D6BA-DA0E-4C4A-9BE0-AB8075EEA68D}.Release|x64.Build.0 = Release|Any CPU 74 | {DC21D6BA-DA0E-4C4A-9BE0-AB8075EEA68D}.Release|x86.ActiveCfg = Release|Any CPU 75 | {DC21D6BA-DA0E-4C4A-9BE0-AB8075EEA68D}.Release|x86.Build.0 = Release|Any CPU 76 | EndGlobalSection 77 | GlobalSection(SolutionProperties) = preSolution 78 | HideSolutionNode = FALSE 79 | EndGlobalSection 80 | GlobalSection(ExtensibilityGlobals) = postSolution 81 | SolutionGuid = {6327F85B-1D9F-4872-B2A7-874C51043233} 82 | EndGlobalSection 83 | EndGlobal 84 | -------------------------------------------------------------------------------- /src/DotNetCross.NativeInts.Tests/DotNetCross.NativeInts.Tests.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | Debug 6 | AnyCPU 7 | {DC21D6BA-DA0E-4C4A-9BE0-AB8075EEA68D} 8 | Library 9 | Properties 10 | DotNetCross.NativeInts.TestsFramework 11 | DotNetCross.NativeInts.TestsFramework 12 | v4.6.2 13 | 512 14 | {3AC096D0-A1C2-E12C-1390-A8335801FDAB};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC} 15 | 15.0 16 | $(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion) 17 | $(ProgramFiles)\Common Files\microsoft shared\VSTT\$(VisualStudioVersion)\UITestExtensionPackages 18 | False 19 | UnitTest 20 | 21 | 22 | 23 | 24 | 25 | 26 | true 27 | full 28 | false 29 | DEBUG;TRACE 30 | prompt 31 | 4 32 | true 33 | 34 | 35 | pdbonly 36 | true 37 | TRACE 38 | prompt 39 | 4 40 | true 41 | 42 | 43 | 44 | ..\..\packages\MSTest.TestFramework.1.3.2\lib\net45\Microsoft.VisualStudio.TestPlatform.TestFramework.dll 45 | 46 | 47 | ..\..\packages\MSTest.TestFramework.1.3.2\lib\net45\Microsoft.VisualStudio.TestPlatform.TestFramework.Extensions.dll 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | {4ae5d1b8-dca8-4943-bfea-3596f093e61c} 62 | DotNetCross.NativeInts 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | This project references NuGet package(s) that are missing on this computer. Use NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is {0}. 73 | 74 | 75 | 76 | 77 | 78 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | ## Ignore Visual Studio temporary files, build results, and 2 | ## files generated by popular Visual Studio add-ons. 3 | 4 | # User-specific files 5 | *.suo 6 | *.user 7 | *.userosscache 8 | *.sln.docstates 9 | 10 | # User-specific files (MonoDevelop/Xamarin Studio) 11 | *.userprefs 12 | 13 | # Build results 14 | build/ 15 | /src-pack/DotNetCross.NativeInts.dll 16 | [Dd]ebug/ 17 | [Dd]ebugPublic/ 18 | [Rr]elease/ 19 | [Rr]eleases/ 20 | x64/ 21 | x86/ 22 | bld/ 23 | [Bb]in/ 24 | [Oo]bj/ 25 | [Ll]og/ 26 | 27 | # Visual Studio 2015 cache/options directory 28 | .vs/ 29 | # Uncomment if you have tasks that create the project's static files in wwwroot 30 | #wwwroot/ 31 | 32 | # MSTest test Results 33 | [Tt]est[Rr]esult*/ 34 | [Bb]uild[Ll]og.* 35 | 36 | # NUNIT 37 | *.VisualState.xml 38 | TestResult.xml 39 | 40 | # Build Results of an ATL Project 41 | [Dd]ebugPS/ 42 | [Rr]eleasePS/ 43 | dlldata.c 44 | 45 | # DNX 46 | project.lock.json 47 | artifacts/ 48 | 49 | *_i.c 50 | *_p.c 51 | *_i.h 52 | *.ilk 53 | *.meta 54 | *.obj 55 | *.pch 56 | *.pdb 57 | *.pgc 58 | *.pgd 59 | *.rsp 60 | *.sbr 61 | *.tlb 62 | *.tli 63 | *.tlh 64 | *.tmp 65 | *.tmp_proj 66 | *.log 67 | *.vspscc 68 | *.vssscc 69 | .builds 70 | *.pidb 71 | *.svclog 72 | *.scc 73 | 74 | # Chutzpah Test files 75 | _Chutzpah* 76 | 77 | # Visual C++ cache files 78 | ipch/ 79 | *.aps 80 | *.ncb 81 | *.opendb 82 | *.opensdf 83 | *.sdf 84 | *.cachefile 85 | *.VC.db 86 | *.VC.VC.opendb 87 | 88 | # Visual Studio profiler 89 | *.psess 90 | *.vsp 91 | *.vspx 92 | *.sap 93 | 94 | # TFS 2012 Local Workspace 95 | $tf/ 96 | 97 | # Guidance Automation Toolkit 98 | *.gpState 99 | 100 | # ReSharper is a .NET coding add-in 101 | _ReSharper*/ 102 | *.[Rr]e[Ss]harper 103 | *.DotSettings.user 104 | 105 | # JustCode is a .NET coding add-in 106 | .JustCode 107 | 108 | # TeamCity is a build add-in 109 | _TeamCity* 110 | 111 | # DotCover is a Code Coverage Tool 112 | *.dotCover 113 | 114 | # NCrunch 115 | _NCrunch_* 116 | .*crunch*.local.xml 117 | nCrunchTemp_* 118 | 119 | # MightyMoose 120 | *.mm.* 121 | AutoTest.Net/ 122 | 123 | # Web workbench (sass) 124 | .sass-cache/ 125 | 126 | # Installshield output folder 127 | [Ee]xpress/ 128 | 129 | # DocProject is a documentation generator add-in 130 | DocProject/buildhelp/ 131 | DocProject/Help/*.HxT 132 | DocProject/Help/*.HxC 133 | DocProject/Help/*.hhc 134 | DocProject/Help/*.hhk 135 | DocProject/Help/*.hhp 136 | DocProject/Help/Html2 137 | DocProject/Help/html 138 | 139 | # Click-Once directory 140 | publish/ 141 | 142 | # Publish Web Output 143 | *.[Pp]ublish.xml 144 | *.azurePubxml 145 | # TODO: Comment the next line if you want to checkin your web deploy settings 146 | # but database connection strings (with potential passwords) will be unencrypted 147 | *.pubxml 148 | *.publishproj 149 | 150 | # Microsoft Azure Web App publish settings. Comment the next line if you want to 151 | # checkin your Azure Web App publish settings, but sensitive information contained 152 | # in these scripts will be unencrypted 153 | PublishScripts/ 154 | 155 | # NuGet Packages 156 | *.nupkg 157 | # The packages folder can be ignored because of Package Restore 158 | **/packages/* 159 | # except build/, which is used as an MSBuild target. 160 | !**/packages/build/ 161 | # Uncomment if necessary however generally it will be regenerated when needed 162 | #!**/packages/repositories.config 163 | # NuGet v3's project.json files produces more ignoreable files 164 | *.nuget.props 165 | *.nuget.targets 166 | 167 | # Microsoft Azure Build Output 168 | csx/ 169 | *.build.csdef 170 | 171 | # Microsoft Azure Emulator 172 | ecf/ 173 | rcf/ 174 | 175 | # Windows Store app package directories and files 176 | AppPackages/ 177 | BundleArtifacts/ 178 | Package.StoreAssociation.xml 179 | _pkginfo.txt 180 | 181 | # Visual Studio cache files 182 | # files ending in .cache can be ignored 183 | *.[Cc]ache 184 | # but keep track of directories ending in .cache 185 | !*.[Cc]ache/ 186 | 187 | # Others 188 | ClientBin/ 189 | ~$* 190 | *~ 191 | *.dbmdl 192 | *.dbproj.schemaview 193 | *.pfx 194 | *.publishsettings 195 | node_modules/ 196 | orleans.codegen.cs 197 | 198 | # Since there are multiple workflows, uncomment next line to ignore bower_components 199 | # (https://github.com/github/gitignore/pull/1529#issuecomment-104372622) 200 | #bower_components/ 201 | 202 | # RIA/Silverlight projects 203 | Generated_Code/ 204 | 205 | # Backup & report files from converting an old project file 206 | # to a newer Visual Studio version. Backup files are not needed, 207 | # because we have git ;-) 208 | _UpgradeReport_Files/ 209 | Backup*/ 210 | UpgradeLog*.XML 211 | UpgradeLog*.htm 212 | 213 | # SQL Server files 214 | *.mdf 215 | *.ldf 216 | 217 | # Business Intelligence projects 218 | *.rdl.data 219 | *.bim.layout 220 | *.bim_*.settings 221 | 222 | # Microsoft Fakes 223 | FakesAssemblies/ 224 | 225 | # GhostDoc plugin setting file 226 | *.GhostDoc.xml 227 | 228 | # Node.js Tools for Visual Studio 229 | .ntvs_analysis.dat 230 | 231 | # Visual Studio 6 build log 232 | *.plg 233 | 234 | # Visual Studio 6 workspace options file 235 | *.opt 236 | 237 | # Visual Studio LightSwitch build output 238 | **/*.HTMLClient/GeneratedArtifacts 239 | **/*.DesktopClient/GeneratedArtifacts 240 | **/*.DesktopClient/ModelManifest.xml 241 | **/*.Server/GeneratedArtifacts 242 | **/*.Server/ModelManifest.xml 243 | _Pvt_Extensions 244 | 245 | # Paket dependency manager 246 | .paket/paket.exe 247 | paket-files/ 248 | 249 | # FAKE - F# Make 250 | .fake/ 251 | 252 | # JetBrains Rider 253 | .idea/ 254 | *.sln.iml 255 | -------------------------------------------------------------------------------- /src/DotNetCross.NativeInts.Tests/IntPtrTest.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using Microsoft.VisualStudio.TestTools.UnitTesting; 3 | 4 | namespace DotNetCross.NativeInts.Tests 5 | { 6 | [TestClass] 7 | public class IntPtrTest 8 | { 9 | private const string NotAvailable = "This feature not available for IntPtr"; 10 | 11 | [TestMethod] 12 | public void Zero() 13 | { 14 | Assert.AreEqual(IntPtr.Zero, IntPtr.Zero); 15 | } 16 | 17 | [TestMethod] 18 | public void ctor_IntPtr_0() 19 | { 20 | IntPtr ip = new IntPtr(0); 21 | Assert.AreEqual(ip, IntPtr.Zero); 22 | } 23 | [TestMethod] 24 | public void ctor_IntPtr_1() 25 | { 26 | IntPtr ip = new IntPtr(1); 27 | Assert.AreEqual(ip, new IntPtr(1)); 28 | } 29 | 30 | [TestMethod] 31 | public void ctor_int_0() 32 | { 33 | IntPtr ip = new IntPtr(0); 34 | Assert.AreEqual(ip, new IntPtr(0)); 35 | } 36 | [TestMethod] 37 | public void ctor_int_1() 38 | { 39 | IntPtr ip = new IntPtr(1); 40 | Assert.AreEqual(ip, new IntPtr(1)); 41 | } 42 | 43 | [TestMethod] 44 | public void implicit_conversion_from_int() 45 | { 46 | Assert.Inconclusive(NotAvailable); 47 | //IntPtr ip = 42; 48 | //Assert.AreEqual(ip, new IntPtr(42)); 49 | } 50 | [TestMethod] 51 | public void explicit_conversion_from_long() 52 | { 53 | IntPtr ip = (IntPtr)42L; 54 | Assert.AreEqual(ip, new IntPtr(42L)); 55 | } 56 | 57 | [TestMethod] 58 | public void operator_Increment() 59 | { 60 | Assert.Inconclusive(NotAvailable); 61 | //var ip = IntPtr.Zero; 62 | //++ip; 63 | //Assert.AreEqual(ip, new IntPtr(1)); 64 | } 65 | [TestMethod] 66 | public void operator_Decrement() 67 | { 68 | Assert.Inconclusive(NotAvailable); 69 | //var ip = IntPtr.Zero; 70 | //--ip; 71 | //Assert.AreEqual(ip, new IntPtr(1)); 72 | } 73 | [TestMethod] 74 | public void operator_UnaryPlus() 75 | { 76 | Assert.Inconclusive(NotAvailable); 77 | //var ip = new IntPtr(1); 78 | //Assert.AreEqual(+ip, new IntPtr(1)); 79 | } 80 | [TestMethod] 81 | public void operator_UnaryNegate() 82 | { 83 | Assert.Inconclusive(NotAvailable); 84 | //var ip = new IntPtr(1); 85 | //Assert.AreEqual(-ip, new IntPtr(-1)); 86 | } 87 | 88 | [TestMethod] 89 | public void operator_OnesComplement() 90 | { 91 | Assert.Inconclusive(NotAvailable); 92 | //Assert.AreEqual(~new IntPtr(1), new IntPtr(-2)); 93 | //Assert.AreEqual(~new IntPtr(0), new IntPtr(-1)); 94 | //Assert.AreEqual(~new IntPtr(-1), new IntPtr(0)); 95 | } 96 | 97 | [TestMethod] 98 | public void operator_Addition_IntPtr() 99 | { 100 | Assert.Inconclusive(NotAvailable); 101 | //var ip = new IntPtr(1); 102 | //Assert.AreEqual(ip + new IntPtr(1), new IntPtr(2)); 103 | //Assert.AreEqual(new IntPtr(1) + ip, new IntPtr(2)); 104 | } 105 | [TestMethod] 106 | public void operator_Addition_int() 107 | { 108 | var ip = new IntPtr(1); 109 | Assert.AreEqual(ip + 1, new IntPtr(2)); 110 | // Below not available 111 | //Assert.AreEqual(1 + ip, new IntPtr(2)); 112 | } 113 | [TestMethod] 114 | public void operator_Addition_uint() 115 | { 116 | Assert.Inconclusive(NotAvailable); 117 | //var ip = new IntPtr(1); 118 | //Assert.AreEqual(ip + 1u, new IntPtr(2)); 119 | //Assert.AreEqual(1u + ip, new IntPtr(2)); 120 | } 121 | 122 | [TestMethod] 123 | public void operator_Subtraction_IntPtr() 124 | { 125 | Assert.Inconclusive(NotAvailable); 126 | //var ip = new IntPtr(1); 127 | //Assert.AreEqual(ip - new IntPtr(1), new IntPtr(0)); 128 | //Assert.AreEqual(new IntPtr(1) - ip, new IntPtr(0)); 129 | } 130 | [TestMethod] 131 | public void operator_Subtraction_int() 132 | { 133 | var ip = new IntPtr(1); 134 | Assert.AreEqual(ip - 1, new IntPtr(0)); 135 | // Below not available 136 | //Assert.AreEqual(1 - ip, new IntPtr(0)); 137 | } 138 | [TestMethod] 139 | public void operator_Subtraction_uint() 140 | { 141 | Assert.Inconclusive(NotAvailable); 142 | //var ip = new IntPtr(1); 143 | //Assert.AreEqual(ip - 1u, new IntPtr(0)); 144 | //Assert.AreEqual(1u - ip, new IntPtr(0)); 145 | } 146 | 147 | [TestMethod] 148 | public void operator_Multiply_IntPtr() 149 | { 150 | Assert.Inconclusive(NotAvailable); 151 | //var ip = new IntPtr(7); 152 | //Assert.AreEqual(ip * new IntPtr(2), new IntPtr(14)); 153 | } 154 | [TestMethod] 155 | public void operator_Multiply_int() 156 | { 157 | Assert.Inconclusive(NotAvailable); 158 | //var ip = new IntPtr(7); 159 | //Assert.AreEqual(ip * 2, new IntPtr(14)); 160 | } 161 | 162 | [TestMethod] 163 | public void operator_Division_IntPtr() 164 | { 165 | Assert.Inconclusive(NotAvailable); 166 | //var ip = new IntPtr(7); 167 | //Assert.AreEqual(ip / new IntPtr(2), new IntPtr(3)); 168 | } 169 | [TestMethod] 170 | public void operator_Division_int() 171 | { 172 | Assert.Inconclusive(NotAvailable); 173 | //var ip = new IntPtr(7); 174 | //Assert.AreEqual(ip / 2, new IntPtr(3)); 175 | } 176 | 177 | [TestMethod] 178 | public void operator_AdditionAssignment() 179 | { 180 | var ip = new IntPtr(7); 181 | ip += 1; 182 | Assert.AreEqual(ip, new IntPtr(8)); 183 | } 184 | [TestMethod] 185 | public void operator_SubtractionAssignment() 186 | { 187 | var ip = new IntPtr(7); 188 | ip -= 1; 189 | Assert.AreEqual(ip, new IntPtr(6)); 190 | } 191 | } 192 | } 193 | -------------------------------------------------------------------------------- /src/DotNetCross.NativeInts.Ref/nint.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace DotNetCross.NativeInts 4 | { 5 | public struct nint : IEquatable, IComparable 6 | { 7 | public IntPtr Value; 8 | 9 | public nint(IntPtr value) 10 | { 11 | Value = value; 12 | } 13 | public nint(int value) 14 | { 15 | Value = new IntPtr(value); 16 | } 17 | // Add or not? Rather be sure this is only done via explicit cast 18 | private nint(long value) 19 | { 20 | Value = new IntPtr(value); 21 | } 22 | 23 | // To nint 24 | public static implicit operator nint(IntPtr value) => new nint(value); 25 | public static implicit operator nint(int value) => new nint(value); 26 | public static explicit operator nint(long value) => new nint(value); 27 | // From nint 28 | public static implicit operator IntPtr(nint value) => value.Value; 29 | public static implicit operator long(nint value) => (long)value.Value; 30 | public static explicit operator int(nint value) => (int)value.Value; 31 | 32 | // 33 | // Unary 34 | // 35 | public static nint operator ++(nint value) => new nint(value.Value + 1); 36 | public static nint operator --(nint value) => new nint(value.Value - 1); 37 | 38 | public static nint operator +(nint value) => value; 39 | public static nint operator -(nint value) 40 | { 41 | return Is32Bit ? new nint(-(int)value.Value) 42 | : new nint(-(long)value.Value); 43 | } 44 | 45 | public unsafe static nint operator ~(nint value) 46 | { 47 | return Is32Bit ? new nint(~(int)value.Value) 48 | : new nint(~(long)value.Value); 49 | } 50 | 51 | // 52 | // Binary 53 | // 54 | public unsafe static nint operator +(nint l, nint r) 55 | { 56 | return Is32Bit ? new nint((int)l.Value + (int)r.Value) 57 | : new nint((long)l.Value + (long)r.Value); 58 | } 59 | public unsafe static nint operator +(nint l, int r) 60 | { 61 | return l.Value + r; 62 | } 63 | public unsafe static nint operator +(int l, nint r) 64 | { 65 | return r.Value + l; 66 | } 67 | 68 | public unsafe static nint operator -(nint l, nint r) 69 | { 70 | return Is32Bit ? new nint((int)l.Value - (int)r.Value) 71 | : new nint((long)l.Value - (long)r.Value); 72 | } 73 | public unsafe static nint operator -(nint l, int r) 74 | { 75 | return l.Value - r; 76 | } 77 | public unsafe static nint operator -(int l, nint r) 78 | { 79 | return r.Value - l; 80 | } 81 | 82 | public unsafe static nint operator *(nint l, nint r) 83 | { 84 | return Is32Bit ? new nint((int)l.Value * (int)r.Value) 85 | : new nint((long)l.Value * (long)r.Value); 86 | } 87 | public unsafe static nint operator *(nint l, int r) 88 | { 89 | return Is32Bit ? new nint((int)l.Value * r) 90 | : new nint((long)l.Value * r); 91 | } 92 | 93 | public unsafe static nint operator /(nint l, nint r) 94 | { 95 | return Is32Bit ? new nint((int)l.Value / (int)r.Value) 96 | : new nint((long)l.Value / (long)r.Value); 97 | } 98 | public unsafe static nint operator /(nint l, int r) 99 | { 100 | return Is32Bit ? new nint((int)l.Value / r) 101 | : new nint((long)l.Value / r); 102 | } 103 | 104 | public unsafe static nint operator %(nint l, nint r) 105 | { 106 | return Is32Bit ? new nint((int)l.Value % (int)r.Value) 107 | : new nint((long)l.Value % (long)r.Value); 108 | } 109 | public unsafe static nint operator %(nint l, int r) 110 | { 111 | return Is32Bit ? new nint((int)l.Value % r) 112 | : new nint((long)l.Value % r); 113 | } 114 | 115 | public unsafe static nint operator ^(nint l, nint r) 116 | { 117 | return Is32Bit ? new nint((int)l.Value ^ (int)r.Value) 118 | : new nint((long)l.Value ^ (long)r.Value); 119 | } 120 | public unsafe static nint operator ^(nint l, int r) 121 | { 122 | return Is32Bit ? new nint((int)l.Value ^ r) 123 | : new nint((long)l.Value ^ (long)r); 124 | } 125 | 126 | public unsafe static nint operator &(nint l, nint r) 127 | { 128 | return Is32Bit ? new nint((int)l.Value & (int)r.Value) 129 | : new nint((long)l.Value & (long)r.Value); 130 | } 131 | public unsafe static nint operator &(nint l, int r) 132 | { 133 | return Is32Bit ? new nint((int)l.Value & r) 134 | : new nint((long)l.Value & (long)r); 135 | } 136 | 137 | public unsafe static nint operator |(nint l, nint r) 138 | { 139 | return Is32Bit ? new nint((int)l.Value | (int)r.Value) 140 | : new nint((long)l.Value | (long)r.Value); 141 | } 142 | public unsafe static nint operator |(nint l, int r) 143 | { 144 | return Is32Bit ? new nint((int)l.Value | r) 145 | : new nint((long)l.Value | (long)r); 146 | } 147 | 148 | // <<(nint l, nint r) overload not allowed in C# 149 | 150 | public unsafe static nint operator <<(nint l, int r) 151 | { 152 | return Is32Bit ? new nint((int)l.Value << r) 153 | : new nint((long)l.Value << r); 154 | } 155 | 156 | // >>(nint l, nint r) overload not allowed in C# 157 | 158 | public unsafe static nint operator >>(nint l, int r) 159 | { 160 | return Is32Bit ? new nint((int)l.Value >> r) 161 | : new nint((long)l.Value >> r); 162 | } 163 | 164 | public unsafe static bool operator ==(nint l, nint r) 165 | { 166 | return l.Value == r.Value; 167 | } 168 | public unsafe static bool operator ==(nint l, int r) 169 | { 170 | return Is32Bit ? (int)l.Value == r 171 | : (long)l.Value == r; 172 | } 173 | public unsafe static bool operator ==(int l, nint r) 174 | { 175 | return Is32Bit ? l == (int)r.Value 176 | : l == (long)r.Value; 177 | } 178 | public unsafe static bool operator ==(nint l, long r) 179 | { 180 | return Is32Bit ? (int)l.Value == r 181 | : (long)l.Value == r; 182 | } 183 | public unsafe static bool operator ==(long l, nint r) 184 | { 185 | return Is32Bit ? l == (int)r.Value 186 | : l == (long)r.Value; 187 | } 188 | public unsafe static bool operator ==(nint l, IntPtr r) 189 | { 190 | return l.Value == r; 191 | } 192 | public unsafe static bool operator ==(IntPtr l, nint r) 193 | { 194 | return l == r.Value; 195 | } 196 | 197 | public unsafe static bool operator !=(nint l, nint r) 198 | { 199 | return l.Value != r.Value; 200 | } 201 | public unsafe static bool operator !=(nint l, int r) 202 | { 203 | return Is32Bit ? (int)l.Value != r 204 | : (long)l.Value != r; 205 | } 206 | public unsafe static bool operator !=(int l, nint r) 207 | { 208 | return Is32Bit ? l != (int)r.Value 209 | : l != (long)r.Value; 210 | } 211 | public unsafe static bool operator !=(nint l, long r) 212 | { 213 | return Is32Bit ? (int)l.Value != r 214 | : (long)l.Value != r; 215 | } 216 | public unsafe static bool operator !=(long l, nint r) 217 | { 218 | return Is32Bit ? l != (int)r.Value 219 | : l != (long)r.Value; 220 | } 221 | public unsafe static bool operator !=(nint l, IntPtr r) 222 | { 223 | return l.Value != r; 224 | } 225 | public unsafe static bool operator !=(IntPtr l, nint r) 226 | { 227 | return l != r.Value; 228 | } 229 | 230 | public unsafe static bool operator >(nint l, nint r) 231 | { 232 | return Is32Bit ? (int)l.Value > (int)r.Value 233 | : (long)l.Value > (long)r.Value; 234 | } 235 | public unsafe static bool operator >(nint l, int r) 236 | { 237 | return Is32Bit ? (int)l.Value > r 238 | : (long)l.Value > r; 239 | } 240 | public unsafe static bool operator >(int l, nint r) 241 | { 242 | return Is32Bit ? l > (int)r.Value 243 | : l > (long)r.Value; 244 | } 245 | public unsafe static bool operator >(nint l, long r) 246 | { 247 | return Is32Bit ? (int)l.Value > r 248 | : (long)l.Value > r; 249 | } 250 | public unsafe static bool operator >(long l, nint r) 251 | { 252 | return Is32Bit ? l > (int)r.Value 253 | : l > (long)r.Value; 254 | } 255 | public unsafe static bool operator >(nint l, IntPtr r) 256 | { 257 | return Is32Bit ? (int)l.Value > (int)r 258 | : (long)l.Value > (long)r; 259 | } 260 | public unsafe static bool operator >(IntPtr l, nint r) 261 | { 262 | return Is32Bit ? (int)l > (int)r.Value 263 | : (long)l > (long)r.Value; 264 | } 265 | 266 | public unsafe static bool operator <(nint l, nint r) 267 | { 268 | return Is32Bit ? (int)l.Value < (int)r.Value 269 | : (long)l.Value < (long)r.Value; 270 | } 271 | public unsafe static bool operator <(nint l, int r) 272 | { 273 | return Is32Bit ? (int)l.Value < r 274 | : (long)l.Value < r; 275 | } 276 | public unsafe static bool operator <(int l, nint r) 277 | { 278 | return Is32Bit ? l < (int)r.Value 279 | : l < (long)r.Value; 280 | } 281 | public unsafe static bool operator <(nint l, long r) 282 | { 283 | return Is32Bit ? (int)l.Value < r 284 | : (long)l.Value < r; 285 | } 286 | public unsafe static bool operator <(long l, nint r) 287 | { 288 | return Is32Bit ? l < (int)r.Value 289 | : l < (long)r.Value; 290 | } 291 | public unsafe static bool operator <(nint l, IntPtr r) 292 | { 293 | return Is32Bit ? (int)l.Value < (int)r 294 | : (long)l.Value < (long)r; 295 | } 296 | public unsafe static bool operator <(IntPtr l, nint r) 297 | { 298 | return Is32Bit ? (int)l < (int)r.Value 299 | : (long)l < (long)r.Value; 300 | } 301 | 302 | public unsafe static bool operator >=(nint l, nint r) 303 | { 304 | return Is32Bit ? (int)l.Value >= (int)r.Value 305 | : (long)l.Value >= (long)r.Value; 306 | } 307 | public unsafe static bool operator >=(nint l, int r) 308 | { 309 | return Is32Bit ? (int)l.Value >= r 310 | : (long)l.Value >= r; 311 | } 312 | public unsafe static bool operator >=(int l, nint r) 313 | { 314 | return Is32Bit ? l >= (int)r.Value 315 | : l >= (long)r.Value; 316 | } 317 | public unsafe static bool operator >=(nint l, long r) 318 | { 319 | return Is32Bit ? (int)l.Value >= r 320 | : (long)l.Value >= r; 321 | } 322 | public unsafe static bool operator >=(long l, nint r) 323 | { 324 | return Is32Bit ? l >= (int)r.Value 325 | : l >= (long)r.Value; 326 | } 327 | public unsafe static bool operator >=(nint l, IntPtr r) 328 | { 329 | return Is32Bit ? (int)l.Value >= (int)r 330 | : (long)l.Value >= (long)r; 331 | } 332 | public unsafe static bool operator >=(IntPtr l, nint r) 333 | { 334 | return Is32Bit ? (int)l >= (int)r.Value 335 | : (long)l >= (long)r.Value; 336 | } 337 | 338 | public unsafe static bool operator <=(nint l, nint r) 339 | { 340 | return Is32Bit ? (int)l.Value <= (int)r.Value 341 | : (long)l.Value <= (long)r.Value; 342 | } 343 | public unsafe static bool operator <=(nint l, int r) 344 | { 345 | return Is32Bit ? (int)l.Value <= r 346 | : (long)l.Value <= r; 347 | } 348 | public unsafe static bool operator <=(int l, nint r) 349 | { 350 | return Is32Bit ? l <= (int)r.Value 351 | : l <= (long)r.Value; 352 | } 353 | public unsafe static bool operator <=(nint l, long r) 354 | { 355 | return Is32Bit ? (int)l.Value <= r 356 | : (long)l.Value <= r; 357 | } 358 | public unsafe static bool operator <=(long l, nint r) 359 | { 360 | return Is32Bit ? l <= (int)r.Value 361 | : l <= (long)r.Value; 362 | } 363 | public unsafe static bool operator <=(nint l, IntPtr r) 364 | { 365 | return Is32Bit ? (int)l.Value <= (int)r 366 | : (long)l.Value <= (long)r; 367 | } 368 | public unsafe static bool operator <=(IntPtr l, nint r) 369 | { 370 | return Is32Bit ? (int)l <= (int)r.Value 371 | : (long)l <= (long)r.Value; 372 | } 373 | 374 | // <<= cannot be overloaded directly in C# 375 | 376 | // >>= cannot be overloaded directly in C# 377 | 378 | public bool Equals(nint other) => this == other; 379 | 380 | public int CompareTo(nint other) 381 | { 382 | return Is32Bit ? ((int)this.Value).CompareTo((int)other.Value) 383 | : ((long)this.Value).CompareTo((long)other.Value); 384 | } 385 | 386 | public override bool Equals(object obj) 387 | { 388 | if (obj is nint) 389 | { 390 | return (Value == ((nint)obj).Value); 391 | } 392 | return false; 393 | } 394 | 395 | public override int GetHashCode() 396 | { 397 | return Value.GetHashCode(); 398 | } 399 | 400 | public override string ToString() 401 | { 402 | return Value.ToString(); 403 | } 404 | 405 | private static readonly bool Is32Bit = CheckIs32Bit(); 406 | private static unsafe bool CheckIs32Bit() 407 | { 408 | return sizeof(IntPtr) == sizeof(int); 409 | } 410 | } 411 | } 412 | -------------------------------------------------------------------------------- /src/DotNetCross.NativeInts.Ref/nuint.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace DotNetCross.NativeInts 4 | { 5 | public struct nuint : IEquatable, IComparable 6 | { 7 | public UIntPtr Value; 8 | 9 | public nuint(UIntPtr value) 10 | { 11 | Value = value; 12 | } 13 | public nuint(uint value) 14 | { 15 | Value = new UIntPtr(value); 16 | } 17 | // Add or not? Rather be sure this is only done via explicit cast 18 | private nuint(ulong value) 19 | { 20 | Value = new UIntPtr(value); 21 | } 22 | 23 | // To nuint 24 | public static implicit operator nuint(UIntPtr value) => new nuint(value); 25 | public static implicit operator nuint(uint value) => new nuint(value); 26 | public static explicit operator nuint(ulong value) => new nuint(value); 27 | // From nuint 28 | public static implicit operator UIntPtr(nuint value) => value.Value; 29 | public static implicit operator ulong(nuint value) => (ulong)value.Value; 30 | public static explicit operator uint(nuint value) => (uint)value.Value; 31 | 32 | // 33 | // Unary 34 | // 35 | public static nuint operator ++(nuint value) => new nuint(value.Value + 1); 36 | public static nuint operator --(nuint value) => new nuint(value.Value - 1); 37 | 38 | public static nuint operator +(nuint value) => value; 39 | //public static nuint operator -(nuint value) 40 | //{ 41 | // return Is32Bit ? new nuint(-(uint)value.Value) 42 | // : new nuint(-(ulong)value.Value); 43 | //} 44 | 45 | public unsafe static nuint operator ~(nuint value) 46 | { 47 | return Is32Bit ? new nuint(~(uint)value.Value) 48 | : new nuint(~(ulong)value.Value); 49 | } 50 | 51 | // 52 | // Binary 53 | // 54 | public unsafe static nuint operator +(nuint l, nuint r) 55 | { 56 | return Is32Bit ? new nuint((uint)l.Value + (uint)r.Value) 57 | : new nuint((ulong)l.Value + (ulong)r.Value); 58 | } 59 | // UIntPtr does not support add/subtract uint 60 | public unsafe static nuint operator +(nuint l, uint r) 61 | { 62 | return Is32Bit ? new nuint((uint)l.Value + r) 63 | : new nuint((ulong)l.Value + r); 64 | } 65 | public unsafe static nuint operator +(uint l, nuint r) 66 | { 67 | return Is32Bit ? new nuint(l + (uint)r.Value) 68 | : new nuint(l + (ulong)r.Value); 69 | } 70 | // UIntPtr supports add/subtract int 71 | public unsafe static nuint operator +(nuint l, int r) 72 | { 73 | return l.Value + r; 74 | } 75 | public unsafe static nuint operator +(int l, nuint r) 76 | { 77 | return r.Value + l; 78 | } 79 | 80 | public unsafe static nuint operator -(nuint l, nuint r) 81 | { 82 | return Is32Bit ? new nuint((uint)l.Value - (uint)r.Value) 83 | : new nuint((ulong)l.Value - (ulong)r.Value); 84 | } 85 | // UIntPtr does not support add/subtract uint 86 | public unsafe static nuint operator -(nuint l, uint r) 87 | { 88 | return Is32Bit ? new nuint((uint)l.Value - r) 89 | : new nuint((ulong)l.Value - r); 90 | } 91 | public unsafe static nuint operator -(uint l, nuint r) 92 | { 93 | return Is32Bit ? new nuint(l - (uint)r.Value) 94 | : new nuint(l - (ulong)r.Value); 95 | } 96 | // UIntPtr supports add/subtract int 97 | public unsafe static nuint operator -(nuint l, int r) 98 | { 99 | return l.Value - r; 100 | } 101 | public unsafe static nuint operator -(int l, nuint r) 102 | { 103 | return r.Value - l; 104 | } 105 | 106 | public unsafe static nuint operator *(nuint l, nuint r) 107 | { 108 | return Is32Bit ? new nuint((uint)l.Value * (uint)r.Value) 109 | : new nuint((ulong)l.Value * (ulong)r.Value); 110 | } 111 | public unsafe static nuint operator *(nuint l, uint r) 112 | { 113 | return Is32Bit ? new nuint((uint)l.Value * r) 114 | : new nuint((ulong)l.Value * r); 115 | } 116 | 117 | public unsafe static nuint operator /(nuint l, nuint r) 118 | { 119 | return Is32Bit ? new nuint((uint)l.Value / (uint)r.Value) 120 | : new nuint((ulong)l.Value / (ulong)r.Value); 121 | } 122 | public unsafe static nuint operator /(nuint l, uint r) 123 | { 124 | return Is32Bit ? new nuint((uint)l.Value / r) 125 | : new nuint((ulong)l.Value / r); 126 | } 127 | 128 | public unsafe static nuint operator %(nuint l, nuint r) 129 | { 130 | return Is32Bit ? new nuint((uint)l.Value % (uint)r.Value) 131 | : new nuint((ulong)l.Value % (ulong)r.Value); 132 | } 133 | public unsafe static nuint operator %(nuint l, uint r) 134 | { 135 | return Is32Bit ? new nuint((uint)l.Value % r) 136 | : new nuint((ulong)l.Value % r); 137 | } 138 | 139 | public unsafe static nuint operator ^(nuint l, nuint r) 140 | { 141 | return Is32Bit ? new nuint((uint)l.Value ^ (uint)r.Value) 142 | : new nuint((ulong)l.Value ^ (ulong)r.Value); 143 | } 144 | public unsafe static nuint operator ^(nuint l, uint r) 145 | { 146 | return Is32Bit ? new nuint((uint)l.Value ^ r) 147 | : new nuint((ulong)l.Value ^ (ulong)r); 148 | } 149 | 150 | public unsafe static nuint operator &(nuint l, nuint r) 151 | { 152 | return Is32Bit ? new nuint((uint)l.Value & (uint)r.Value) 153 | : new nuint((ulong)l.Value & (ulong)r.Value); 154 | } 155 | public unsafe static nuint operator &(nuint l, uint r) 156 | { 157 | return Is32Bit ? new nuint((uint)l.Value & r) 158 | : new nuint((ulong)l.Value & (ulong)r); 159 | } 160 | 161 | public unsafe static nuint operator |(nuint l, nuint r) 162 | { 163 | return Is32Bit ? new nuint((uint)l.Value | (uint)r.Value) 164 | : new nuint((ulong)l.Value | (ulong)r.Value); 165 | } 166 | public unsafe static nuint operator |(nuint l, uint r) 167 | { 168 | return Is32Bit ? new nuint((uint)l.Value | r) 169 | : new nuint((ulong)l.Value | (ulong)r); 170 | } 171 | 172 | // <<(nuint l, nuint r) overload not allowed in C# 173 | 174 | public unsafe static nuint operator <<(nuint l, int r) 175 | { 176 | return Is32Bit ? new nuint((uint)l.Value << r) 177 | : new nuint((ulong)l.Value << r); 178 | } 179 | 180 | // >>(nuint l, nuint r) overload not allowed in C# 181 | 182 | public unsafe static nuint operator >>(nuint l, int r) 183 | { 184 | return Is32Bit ? new nuint((uint)l.Value >> r) 185 | : new nuint((ulong)l.Value >> r); 186 | } 187 | 188 | public unsafe static bool operator ==(nuint l, nuint r) 189 | { 190 | return l.Value == r.Value; 191 | } 192 | public unsafe static bool operator ==(nuint l, uint r) 193 | { 194 | return Is32Bit ? (uint)l.Value == r 195 | : (ulong)l.Value == r; 196 | } 197 | public unsafe static bool operator ==(uint l, nuint r) 198 | { 199 | return Is32Bit ? l == (uint)r.Value 200 | : l == (ulong)r.Value; 201 | } 202 | public unsafe static bool operator ==(nuint l, ulong r) 203 | { 204 | return Is32Bit ? (uint)l.Value == r 205 | : (ulong)l.Value == r; 206 | } 207 | public unsafe static bool operator ==(ulong l, nuint r) 208 | { 209 | return Is32Bit ? l == (uint)r.Value 210 | : l == (ulong)r.Value; 211 | } 212 | public unsafe static bool operator ==(nuint l, UIntPtr r) 213 | { 214 | return l.Value == r; 215 | } 216 | public unsafe static bool operator ==(UIntPtr l, nuint r) 217 | { 218 | return l == r.Value; 219 | } 220 | 221 | public unsafe static bool operator !=(nuint l, nuint r) 222 | { 223 | return l.Value != r.Value; 224 | } 225 | public unsafe static bool operator !=(nuint l, uint r) 226 | { 227 | return Is32Bit ? (uint)l.Value != r 228 | : (ulong)l.Value != r; 229 | } 230 | public unsafe static bool operator !=(uint l, nuint r) 231 | { 232 | return Is32Bit ? l != (uint)r.Value 233 | : l != (ulong)r.Value; 234 | } 235 | public unsafe static bool operator !=(nuint l, ulong r) 236 | { 237 | return Is32Bit ? (uint)l.Value != r 238 | : (ulong)l.Value != r; 239 | } 240 | public unsafe static bool operator !=(ulong l, nuint r) 241 | { 242 | return Is32Bit ? l != (uint)r.Value 243 | : l != (ulong)r.Value; 244 | } 245 | public unsafe static bool operator !=(nuint l, UIntPtr r) 246 | { 247 | return l.Value != r; 248 | } 249 | public unsafe static bool operator !=(UIntPtr l, nuint r) 250 | { 251 | return l != r.Value; 252 | } 253 | 254 | public unsafe static bool operator >(nuint l, nuint r) 255 | { 256 | return Is32Bit ? (uint)l.Value > (uint)r.Value 257 | : (ulong)l.Value > (ulong)r.Value; 258 | } 259 | public unsafe static bool operator >(nuint l, uint r) 260 | { 261 | return Is32Bit ? (uint)l.Value > r 262 | : (ulong)l.Value > r; 263 | } 264 | public unsafe static bool operator >(uint l, nuint r) 265 | { 266 | return Is32Bit ? l > (uint)r.Value 267 | : l > (ulong)r.Value; 268 | } 269 | public unsafe static bool operator >(nuint l, ulong r) 270 | { 271 | return Is32Bit ? (uint)l.Value > r 272 | : (ulong)l.Value > r; 273 | } 274 | public unsafe static bool operator >(ulong l, nuint r) 275 | { 276 | return Is32Bit ? l > (uint)r.Value 277 | : l > (ulong)r.Value; 278 | } 279 | public unsafe static bool operator >(nuint l, UIntPtr r) 280 | { 281 | return Is32Bit ? (uint)l.Value > (uint)r 282 | : (ulong)l.Value > (ulong)r; 283 | } 284 | public unsafe static bool operator >(UIntPtr l, nuint r) 285 | { 286 | return Is32Bit ? (uint)l > (uint)r.Value 287 | : (ulong)l > (ulong)r.Value; 288 | } 289 | 290 | public unsafe static bool operator <(nuint l, nuint r) 291 | { 292 | return Is32Bit ? (uint)l.Value < (uint)r.Value 293 | : (ulong)l.Value < (ulong)r.Value; 294 | } 295 | public unsafe static bool operator <(nuint l, uint r) 296 | { 297 | return Is32Bit ? (uint)l.Value < r 298 | : (ulong)l.Value < r; 299 | } 300 | public unsafe static bool operator <(uint l, nuint r) 301 | { 302 | return Is32Bit ? l < (uint)r.Value 303 | : l < (ulong)r.Value; 304 | } 305 | public unsafe static bool operator <(nuint l, ulong r) 306 | { 307 | return Is32Bit ? (uint)l.Value < r 308 | : (ulong)l.Value < r; 309 | } 310 | public unsafe static bool operator <(ulong l, nuint r) 311 | { 312 | return Is32Bit ? l < (uint)r.Value 313 | : l < (ulong)r.Value; 314 | } 315 | public unsafe static bool operator <(nuint l, UIntPtr r) 316 | { 317 | return Is32Bit ? (uint)l.Value < (uint)r 318 | : (ulong)l.Value < (ulong)r; 319 | } 320 | public unsafe static bool operator <(UIntPtr l, nuint r) 321 | { 322 | return Is32Bit ? (uint)l < (uint)r.Value 323 | : (ulong)l < (ulong)r.Value; 324 | } 325 | 326 | public unsafe static bool operator >=(nuint l, nuint r) 327 | { 328 | return Is32Bit ? (uint)l.Value >= (uint)r.Value 329 | : (ulong)l.Value >= (ulong)r.Value; 330 | } 331 | public unsafe static bool operator >=(nuint l, uint r) 332 | { 333 | return Is32Bit ? (uint)l.Value >= r 334 | : (ulong)l.Value >= r; 335 | } 336 | public unsafe static bool operator >=(uint l, nuint r) 337 | { 338 | return Is32Bit ? l >= (uint)r.Value 339 | : l >= (ulong)r.Value; 340 | } 341 | public unsafe static bool operator >=(nuint l, ulong r) 342 | { 343 | return Is32Bit ? (uint)l.Value >= r 344 | : (ulong)l.Value >= r; 345 | } 346 | public unsafe static bool operator >=(ulong l, nuint r) 347 | { 348 | return Is32Bit ? l >= (uint)r.Value 349 | : l >= (ulong)r.Value; 350 | } 351 | public unsafe static bool operator >=(nuint l, UIntPtr r) 352 | { 353 | return Is32Bit ? (uint)l.Value >= (uint)r 354 | : (ulong)l.Value >= (ulong)r; 355 | } 356 | public unsafe static bool operator >=(UIntPtr l, nuint r) 357 | { 358 | return Is32Bit ? (uint)l >= (uint)r.Value 359 | : (ulong)l >= (ulong)r.Value; 360 | } 361 | 362 | public unsafe static bool operator <=(nuint l, nuint r) 363 | { 364 | return Is32Bit ? (uint)l.Value <= (uint)r.Value 365 | : (ulong)l.Value <= (ulong)r.Value; 366 | } 367 | public unsafe static bool operator <=(nuint l, uint r) 368 | { 369 | return Is32Bit ? (uint)l.Value <= r 370 | : (ulong)l.Value <= r; 371 | } 372 | public unsafe static bool operator <=(uint l, nuint r) 373 | { 374 | return Is32Bit ? l <= (uint)r.Value 375 | : l <= (ulong)r.Value; 376 | } 377 | public unsafe static bool operator <=(nuint l, ulong r) 378 | { 379 | return Is32Bit ? (uint)l.Value <= r 380 | : (ulong)l.Value <= r; 381 | } 382 | public unsafe static bool operator <=(ulong l, nuint r) 383 | { 384 | return Is32Bit ? l <= (uint)r.Value 385 | : l <= (ulong)r.Value; 386 | } 387 | public unsafe static bool operator <=(nuint l, UIntPtr r) 388 | { 389 | return Is32Bit ? (uint)l.Value <= (uint)r 390 | : (ulong)l.Value <= (ulong)r; 391 | } 392 | public unsafe static bool operator <=(UIntPtr l, nuint r) 393 | { 394 | return Is32Bit ? (uint)l <= (uint)r.Value 395 | : (ulong)l <= (ulong)r.Value; 396 | } 397 | 398 | // <<= cannot be overloaded directly in C# 399 | 400 | // >>= cannot be overloaded directly in C# 401 | 402 | public bool Equals(nuint other) => this == other; 403 | 404 | public int CompareTo(nuint other) 405 | { 406 | return Is32Bit ? ((uint)this.Value).CompareTo((uint)other.Value) 407 | : ((ulong)this.Value).CompareTo((ulong)other.Value); 408 | } 409 | 410 | public override bool Equals(object obj) 411 | { 412 | if (obj is nuint) 413 | { 414 | return (Value == ((nuint)obj).Value); 415 | } 416 | return false; 417 | } 418 | 419 | public override int GetHashCode() 420 | { 421 | return Value.GetHashCode(); 422 | } 423 | 424 | public override string ToString() 425 | { 426 | return Value.ToString(); 427 | } 428 | 429 | private static readonly bool Is32Bit = CheckIs32Bit(); 430 | private static unsafe bool CheckIs32Bit() 431 | { 432 | return sizeof(UIntPtr) == sizeof(uint); 433 | } 434 | } 435 | } 436 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # NativeInts 2 | Straightforward `nint` and `nuint` native integers, written in IL, 3 | probably second best to proper compiler support. 4 | Fully functional. 5 | 6 | ### NuGet Package `DotNetCross.NativeInts` 7 | [![NuGet version (DotNetCross.NativeInts)](https://img.shields.io/nuget/v/DotNetCross.NativeInts.svg?style=flat-square)](https://www.nuget.org/packages/DotNetCross.NativeInts/) 8 | 9 | https://www.nuget.org/packages/DotNetCross.NativeInts 10 | 11 | ### `nint` API 12 | ```csharp 13 | public struct nint : IEquatable, IComparable 14 | { 15 | public static readonly nint Zero; 16 | public IntPtr Value; 17 | 18 | public nint(IntPtr value); 19 | public nint(int value); 20 | public nint(long value); 21 | 22 | public int CompareTo(nint other); 23 | public override bool Equals(object obj); 24 | public bool Equals(nint other); 25 | public override int GetHashCode(); 26 | public override string ToString(); 27 | 28 | public static nint operator +(nint value); 29 | public static nint operator +(nint l, nint r); 30 | public static nint operator +(nint l, int r); 31 | public static nint operator +(int l, nint r); 32 | public static nint operator -(nint value); 33 | public static nint operator -(nint l, int r); 34 | public static nint operator -(nint l, nint r); 35 | public static nint operator -(int l, nint r); 36 | public static nint operator ~(nint value); 37 | public static nint operator ++(nint value); 38 | public static nint operator --(nint value); 39 | public static nint operator *(nint l, nint r); 40 | public static nint operator *(nint l, int r); 41 | public static nint operator /(nint l, int r); 42 | public static nint operator /(nint l, nint r); 43 | public static nint operator %(nint l, nint r); 44 | public static nint operator %(nint l, int r); 45 | public static nint operator &(nint l, int r); 46 | public static nint operator &(nint l, nint r); 47 | public static nint operator |(nint l, nint r); 48 | public static nint operator |(nint l, int r); 49 | public static nint operator ^(nint l, nint r); 50 | public static nint operator ^(nint l, int r); 51 | public static nint operator <<(nint l, nint r); 52 | public static nint operator <<(nint l, int r); 53 | public static nint operator >>(nint l, nint r); 54 | public static nint operator >>(nint l, int r); 55 | public static bool operator ==(IntPtr l, nint r); 56 | public static bool operator ==(nint l, IntPtr r); 57 | public static bool operator ==(nint l, long r); 58 | public static bool operator ==(long l, nint r); 59 | public static bool operator ==(nint l, int r); 60 | public static bool operator ==(nint l, nint r); 61 | public static bool operator ==(int l, nint r); 62 | public static bool operator !=(IntPtr l, nint r); 63 | public static bool operator !=(long l, nint r); 64 | public static bool operator !=(nint l, IntPtr r); 65 | public static bool operator !=(nint l, long r); 66 | public static bool operator !=(int l, nint r); 67 | public static bool operator !=(nint l, int r); 68 | public static bool operator !=(nint l, nint r); 69 | public static bool operator <(IntPtr l, nint r); 70 | public static bool operator <(nint l, IntPtr r); 71 | public static bool operator <(long l, nint r); 72 | public static bool operator <(nint l, long r); 73 | public static bool operator <(int l, nint r); 74 | public static bool operator <(nint l, nint r); 75 | public static bool operator <(nint l, int r); 76 | public static bool operator >(IntPtr l, nint r); 77 | public static bool operator >(long l, nint r); 78 | public static bool operator >(nint l, long r); 79 | public static bool operator >(int l, nint r); 80 | public static bool operator >(nint l, int r); 81 | public static bool operator >(nint l, nint r); 82 | public static bool operator >(nint l, IntPtr r); 83 | public static bool operator <=(nint l, int r); 84 | public static bool operator <=(IntPtr l, nint r); 85 | public static bool operator <=(nint l, IntPtr r); 86 | public static bool operator <=(long l, nint r); 87 | public static bool operator <=(nint l, long r); 88 | public static bool operator <=(int l, nint r); 89 | public static bool operator <=(nint l, nint r); 90 | public static bool operator >=(nint l, nint r); 91 | public static bool operator >=(nint l, IntPtr r); 92 | public static bool operator >=(long l, nint r); 93 | public static bool operator >=(nint l, long r); 94 | public static bool operator >=(int l, nint r); 95 | public static bool operator >=(nint l, int r); 96 | public static bool operator >=(IntPtr l, nint r); 97 | 98 | public static implicit operator long(nint value); 99 | public static implicit operator IntPtr(nint value); 100 | public static implicit operator nint(int value); 101 | public static implicit operator nint(IntPtr value); 102 | public static explicit operator nint(long value); 103 | public static explicit operator int(nint value); 104 | } 105 | ``` 106 | 107 | ### `nuint` API 108 | ```csharp 109 | public struct nuint : IEquatable, IComparable 110 | { 111 | public static readonly nuint Zero; 112 | public UIntPtr Value; 113 | 114 | public nuint(UIntPtr value); 115 | public nuint(uint value); 116 | public nuint(ulong value); 117 | 118 | public int CompareTo(nuint other); 119 | public override bool Equals(object obj); 120 | public bool Equals(nuint other); 121 | public override int GetHashCode(); 122 | public override string ToString(); 123 | 124 | public static nuint operator +(nuint value); 125 | public static nuint operator +(nuint l, nuint r); 126 | public static nuint operator +(nuint l, uint r); 127 | public static nuint operator +(uint l, nuint r); 128 | public static nuint operator -(nuint l, uint r); 129 | public static nuint operator -(nuint l, nuint r); 130 | public static nuint operator -(uint l, nuint r); 131 | public static nuint operator ~(nuint value); 132 | public static nuint operator ++(nuint value); 133 | public static nuint operator --(nuint value); 134 | public static nuint operator *(nuint l, nuint r); 135 | public static nuint operator *(nuint l, uint r); 136 | public static nuint operator /(nuint l, nuint r); 137 | public static nuint operator /(nuint l, uint r); 138 | public static nuint operator %(nuint l, nuint r); 139 | public static nuint operator %(nuint l, uint r); 140 | public static nuint operator &(nuint l, uint r); 141 | public static nuint operator &(nuint l, nuint r); 142 | public static nuint operator |(nuint l, uint r); 143 | public static nuint operator |(nuint l, nuint r); 144 | public static nuint operator ^(nuint l, uint r); 145 | public static nuint operator ^(nuint l, nuint r); 146 | public static nuint operator <<(nuint l, nuint r); 147 | public static nuint operator <<(nuint l, uint r); 148 | public static nuint operator >>(nuint l, nuint r); 149 | public static nuint operator >>(nuint l, uint r); 150 | public static bool operator ==(nuint l, UIntPtr r); 151 | public static bool operator ==(UIntPtr l, nuint r); 152 | public static bool operator ==(nuint l, ulong r); 153 | public static bool operator ==(ulong l, nuint r); 154 | public static bool operator ==(nuint l, uint r); 155 | public static bool operator ==(nuint l, nuint r); 156 | public static bool operator ==(uint l, nuint r); 157 | public static bool operator !=(UIntPtr l, nuint r); 158 | public static bool operator !=(ulong l, nuint r); 159 | public static bool operator !=(nuint l, UIntPtr r); 160 | public static bool operator !=(nuint l, ulong r); 161 | public static bool operator !=(uint l, nuint r); 162 | public static bool operator !=(nuint l, uint r); 163 | public static bool operator !=(nuint l, nuint r); 164 | public static bool operator <(UIntPtr l, nuint r); 165 | public static bool operator <(nuint l, UIntPtr r); 166 | public static bool operator <(ulong l, nuint r); 167 | public static bool operator <(nuint l, ulong r); 168 | public static bool operator <(uint l, nuint r); 169 | public static bool operator <(nuint l, nuint r); 170 | public static bool operator <(nuint l, uint r); 171 | public static bool operator >(UIntPtr l, nuint r); 172 | public static bool operator >(ulong l, nuint r); 173 | public static bool operator >(nuint l, ulong r); 174 | public static bool operator >(uint l, nuint r); 175 | public static bool operator >(nuint l, uint r); 176 | public static bool operator >(nuint l, nuint r); 177 | public static bool operator >(nuint l, UIntPtr r); 178 | public static bool operator <=(nuint l, uint r); 179 | public static bool operator <=(UIntPtr l, nuint r); 180 | public static bool operator <=(nuint l, UIntPtr r); 181 | public static bool operator <=(ulong l, nuint r); 182 | public static bool operator <=(nuint l, ulong r); 183 | public static bool operator <=(uint l, nuint r); 184 | public static bool operator <=(nuint l, nuint r); 185 | public static bool operator >=(nuint l, nuint r); 186 | public static bool operator >=(nuint l, UIntPtr r); 187 | public static bool operator >=(ulong l, nuint r); 188 | public static bool operator >=(nuint l, ulong r); 189 | public static bool operator >=(uint l, nuint r); 190 | public static bool operator >=(nuint l, uint r); 191 | public static bool operator >=(UIntPtr l, nuint r); 192 | 193 | public static implicit operator ulong(nuint value); 194 | public static implicit operator UIntPtr(nuint value); 195 | public static implicit operator nuint(uint value); 196 | public static implicit operator nuint(UIntPtr value); 197 | public static explicit operator nuint(ulong value); 198 | public static explicit operator uint(nuint value); 199 | } 200 | ``` 201 | 202 | ## Comparing `IntPtr`/`UIntPtr` vs `nint`/`nuint` 203 | Based on: 204 | * **I.10.3 Operator overloading** section from the ECMA 205 | * [Operator Overloads](https://msdn.microsoft.com/en-us/library/ms229032.aspx) 206 | * [Overloadable Operators (C# Programming Guide)](https://msdn.microsoft.com/en-us/library/8edha89s.aspx) 207 | 208 | ### Conversions 209 | Besides supporting implicit conversion for: 210 | 211 | * `IntPtr` to/from `nint` 212 | * `UIntPtr` to/from `nuint` 213 | 214 | The following conversions are supported compared to available conversions for `IntPtr`/`UIntPtr`. 215 | 216 | #### From a Primitive Type 217 | |**Name** |**From** |`IntPtr` C# | `UIntPtr` C# | `nint` C# | `nuint` C# | 218 | |--|--|--|--|--|--| 219 | |op_Implicit |`int` |N/A |N/A |Yes |N/A | 220 | |op_Implicit |`uint` |N/A |N/A |N/A |Yes | 221 | |op_Explicit |`long` |N/A |N/A |Yes |N/A | 222 | |op_Explicit |`ulong` |N/A |N/A |N/A |Yes | 223 | 224 | #### To a Primitive Type 225 | |**Name** |**To** |`IntPtr` C# | `UIntPtr` C# | `nint` C# | `nuint` C# | 226 | |--|--|--|--|--|--| 227 | |op_Implicit |`long` |N/A |N/A |Yes |N/A | 228 | |op_Implicit |`ulong` |N/A |N/A |N/A |Yes | 229 | |op_Explicit |`int` |N/A |N/A |Yes |N/A | 230 | |op_Explicit |`uint` |N/A |N/A |N/A |Yes | 231 | 232 | 233 | ### Unary Operators 234 | |**Name** |**C++ Operator Symbol** |`IntPtr` C# | `UIntPtr` C# | `nint` C# | `nuint` C# | 235 | |--|--|--|--|--|--| 236 | |op_Decrement | --1 | N/A | N/A | Yes | Yes | 237 | |op_Increment | ++1 | N/A | N/A | Yes | Yes | 238 | |op_UnaryNegation | - (unary) | N/A | N/A | Yes | Yes | 239 | |op_UnaryPlus | + (unary) | N/A | N/A | Yes | Yes | 240 | |op_LogicalNot | ! | N/A | N/A | N/A | N/A | 241 | |op_True | ND2 | N/A | N/A | N/A | N/A | 242 | |op_False | ND2 | N/A | N/A | N/A | N/A | 243 | |op_AddressOf | & (unary) | N/A | N/A | N/A | N/A | 244 | |op_OnesComplement | ~ | N/A | N/A | Yes | Yes | 245 | |op_PointerDereference | * (unary) | N/A | N/A | N/A | N/A | 246 | 247 | 1 From a pure C++ point of view, the way one must write these functions for the CLI differs in 248 | one very important aspect. In C++, these methods must increment or decrement their operand 249 | directly, whereas, in CLI, they must not; instead, they simply return the value of their operand 250 | +/- 1, as appropriate, without modifying their operand. The operand must be incremented or 251 | decremented by the compiler that generates the code for the ++/-- operator, separate from the call 252 | to these methods. 253 | 254 | 2 The op_True and op_False operators do not exist in C++. They are provided to support tristate 255 | Boolean types, such as those used in database languages. 256 | 257 | ### Binary Operators 258 | The table below shows the types that can be used together with the type in header for a given binary operator. 259 | By default this is for the type on either left or right side of expression (binary operator). 260 | However, for some cases an operator might only be available for a type at a specific side of the expression, 261 | with `(R)` meaning "right only". 262 | 263 | |**Name** |**C++ Operator Symbol** |`IntPtr` C# | `UIntPtr` C# | `nint` C# | `nuint` C# | 264 | |-------------------|-------------------------|--------------|----------------|--------------------------|------------------| 265 | |op_Addition | + |`int (R)` |`int (R)` |`nint`, `IntPtr`, `int` |`nuint`, `UIntPtr`, `uint` | 266 | |op_Subtraction | - |`int (R)` |`int (R)` |`nint`, `IntPtr`, `int` |`nuint`, `UIntPtr`, `uint` | 267 | |op_Multiply | \* |N/A |N/A |`nint`, `IntPtr`, `int (R)` |`nuint`, `UIntPtr`, `uint (R)` | 268 | |op_Division | / |N/A |N/A |`nint`, `IntPtr`, `int (R)` |`nuint`, `UIntPtr`, `uint (R)` | 269 | |op_Modulus | % |N/A |N/A |`nint`, `IntPtr`, `int (R)` |`nuint`, `UIntPtr`, `uint (R)` | 270 | |op_ExclusiveOr | ^ |N/A |N/A |`nint`, `IntPtr`, `int (R)` |`nuint`, `UIntPtr`, `uint (R)` | 271 | |op_BitwiseAnd | & |N/A |N/A |`nint`, `IntPtr`, `int (R)` |`nuint`, `UIntPtr`, `uint (R)` | 272 | |op_BitwiseOr | \| |N/A |N/A |`nint`, `IntPtr`, `int (R)` |`nuint`, `UIntPtr`, `uint (R)` | 273 | |op_LogicalAnd | && |N/A |N/A |N/A |N/A | 274 | |op_LogicalOr | \|\| |N/A |N/A |N/A |N/A | 275 | |op_Assign | N/D (= is not the same) |N/A |N/A |N/A |N/A | 276 | |op_LeftShift | << |N/A |N/A |`nint`, `IntPtr`, `int (R)` |`nuint`, `UIntPtr`, `uint (R)` | 277 | |op_RightShift | >> |N/A |N/A |`nint`, `IntPtr`, `int (R)` |`nuint`, `UIntPtr`, `uint (R)` | 278 | |op_SignedRightShift | N/D |N/A |N/A |N/A |N/A | 279 | |op_UnsignedRightShift | N/D |N/A |N/A |N/A |N/A | 280 | |op_Equality | == |`IntPtr` |`UIntPtr` |`nint`, `IntPtr`, `int`, `long` |`nuint`, `UIntPtr`, `uint`, `ulong` | 281 | |op_GreaterThan | > |`IntPtr` |`UIntPtr` |`nint`, `IntPtr`, `int`, `long` |`nuint`, `UIntPtr`, `uint`, `ulong` | 282 | |op_LessThan | < |`IntPtr` |`UIntPtr` |`nint`, `IntPtr`, `int`, `long` |`nuint`, `UIntPtr`, `uint`, `ulong` | 283 | |op_Inequality | != |`IntPtr` |`UIntPtr` |`nint`, `IntPtr`, `int`, `long` |`nuint`, `UIntPtr`, `uint`, `ulong` | 284 | |op_GreaterThanOrEqual | >= |`IntPtr` |`UIntPtr` |`nint`, `IntPtr`, `int`, `long` |`nuint`, `UIntPtr`, `uint`, `ulong` | 285 | |op_LessThanOrEqual | \<= |`IntPtr` |`UIntPtr` |`nint`, `IntPtr`, `int`, `long` |`nuint`, `UIntPtr`, `uint`, `ulong` | 286 | |op_UnsignedRightShiftAssignment| Not defined |N/A |N/A |N/A |N/A | 287 | |op_MemberSelection | -> |N/A (N/O C#) |N/A (N/O C#) |N/A (N/O C#) |N/A (N/O C#) | 288 | |op_RightShiftAssignment | >>= |N/A (N/O C#) |N/A (N/O C#) |`nint`, `IntPtr`, `int` |`nuint`, `UIntPtr`, `uint` TODO | 289 | |op_MultiplicationAssignment | \*= |N/A (N/O C#) |N/A (N/O C#) |`nint`, `IntPtr`, `int` |`nuint`, `UIntPtr`, `uint` TODO | 290 | |op_PointerToMemberSelection | ->\* |N/A (N/O C#) |N/A (N/O C#) |N/A (N/O C#) |N/A (N/O C#) | 291 | |op_SubtractionAssignment | -= |`int` |`int` |`nint`, `IntPtr`, `int` |`nuint`, `UIntPtr`, `uint` TODO | 292 | |op_ExclusiveOrAssignment | ^= |N/A (N/O C#) |N/A (N/O C#) |`nint`, `IntPtr`, `int` |`nuint`, `UIntPtr`, `uint` TODO | 293 | |op_LeftShiftAssignment | <<= |N/A (N/O C#) |N/A (N/O C#) |`nint`, `IntPtr`, `int` |`nuint`, `UIntPtr`, `uint` TODO | 294 | |op_ModulusAssignment | %= |N/A (N/O C#) |N/A (N/O C#) |`nint`, `IntPtr`, `int` |`nuint`, `UIntPtr`, `uint` TODO | 295 | |op_AdditionAssignment | += |`int` |`int` |`nint`, `IntPtr`, `int` |`nuint`, `UIntPtr`, `uint` TODO | 296 | |op_BitwiseAndAssignment | &= |N/A (N/O C#) |N/A (N/O C#) |`nint`, `IntPtr`, `int` |`nuint`, `UIntPtr`, `uint` TODO | 297 | |op_BitwiseOrAssignment | \|= |N/A (N/O C#) |N/A (N/O C#) |`nint`, `IntPtr`, `int` |`nuint`, `UIntPtr`, `uint` TODO | 298 | |op_Comma | , |N/A (N/O C#) |N/A (N/O C#) |N/A (N/O C#) |N/A (N/O C#) | 299 | |op_DivisionAssignment | /= |N/A (N/O C#) |N/A (N/O C#) |`nint`, `IntPtr`, `int` |`nuint`, `UIntPtr`, `uint` TODO | 300 | 301 | * `N/A` = Not Available 302 | * `N/O` = Not Overloadable, but for assignment operators usably available via binary static operator e.g. `+=` is available via `+`. 303 | 304 | ### Object Methods 305 | The following methods have the same or forward to the equivalent `IntPtr`/`UIntPtr` implementation. 306 | 307 | ```csharp 308 | public override bool Equals(object obj) 309 | public override int GetHashCode() 310 | public override string ToString() 311 | ``` 312 | 313 | ### Interface Methods 314 | The following interfaces have been implemented for the given type `nint` or `nuint`: 315 | ```csharp 316 | IEquatable 317 | IComparable 318 | ``` 319 | -------------------------------------------------------------------------------- /src/DotNetCross.NativeInts.Tests/nuintTest.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Diagnostics; 3 | using Microsoft.VisualStudio.TestTools.UnitTesting; 4 | 5 | namespace DotNetCross.NativeInts.TestsFramework 6 | { 7 | [TestClass] 8 | public class nuintTest 9 | { 10 | [TestMethod] 11 | public unsafe void TraceSize() 12 | { 13 | Trace.WriteLine($"Sizeof {nameof(nuint)}:{sizeof(nuint)}"); 14 | Console.WriteLine($"Sizeof {nameof(nuint)}:{sizeof(nuint)}"); 15 | //Assert.AreEqual(8, sizeof(nuint)); 16 | } 17 | 18 | [TestMethod] 19 | public void Zero() 20 | { 21 | Assert.AreEqual(nuint.Zero.Value, UIntPtr.Zero); 22 | } 23 | 24 | [TestMethod] 25 | public void ctor_UIntPtr_0() 26 | { 27 | nuint ni = new nuint(UIntPtr.Zero); 28 | Assert.AreEqual(ni.Value, UIntPtr.Zero); 29 | } 30 | [TestMethod] 31 | public void ctor_UIntPtr_1() 32 | { 33 | nuint ni = new nuint(new UIntPtr(1)); 34 | Assert.AreEqual(ni.Value, new UIntPtr(1)); 35 | } 36 | 37 | [TestMethod] 38 | public void ctor_int_0() 39 | { 40 | nuint ni = new nuint(0); 41 | Assert.AreEqual(ni.Value, new UIntPtr(0)); 42 | } 43 | [TestMethod] 44 | public void ctor_int_1() 45 | { 46 | nuint ni = new nuint(1); 47 | Assert.AreEqual(ni.Value, new UIntPtr(1)); 48 | } 49 | 50 | [TestMethod] 51 | public void ctor_ulong_0() 52 | { 53 | nuint ni = new nuint(0UL); 54 | Assert.AreEqual(ni.Value, new UIntPtr(0UL)); 55 | } 56 | [TestMethod] 57 | public void ctor_ulong_1() 58 | { 59 | nuint ni = new nuint(1UL); 60 | Assert.AreEqual(ni.Value, new UIntPtr(1UL)); 61 | } 62 | 63 | [TestMethod] 64 | public unsafe void ctor_ulong_MaxValue() 65 | { 66 | if (sizeof(nuint) == sizeof(ulong)) 67 | { 68 | nuint ni = new nuint(ulong.MaxValue); 69 | Assert.AreEqual(ni.Value, new UIntPtr(ulong.MaxValue)); 70 | } 71 | else 72 | { 73 | try 74 | { 75 | new nuint(ulong.MaxValue); 76 | Assert.Fail("ctor for 64-bit checks overflow"); 77 | } 78 | catch (OverflowException e) 79 | { 80 | Assert.IsNotNull(e); 81 | } 82 | } 83 | } 84 | 85 | [TestMethod] 86 | public void implicit_conversion_from_UIntPtr() 87 | { 88 | nuint ni = new UIntPtr(42); 89 | Assert.AreEqual(ni.Value, new UIntPtr(42)); 90 | } 91 | [TestMethod] 92 | public void implicit_conversion_from_int() 93 | { 94 | nuint ni = 42; 95 | Assert.AreEqual(ni.Value, new UIntPtr(42)); 96 | } 97 | [TestMethod] 98 | public void explicit_conversion_from_long() 99 | { 100 | nuint ni = (nuint)42L; 101 | Assert.AreEqual(ni.Value, new UIntPtr(42L)); 102 | } 103 | 104 | [TestMethod] 105 | public void implicit_conversion_to_UIntPtr() 106 | { 107 | UIntPtr ip = new nuint(42); 108 | Assert.AreEqual(ip, new UIntPtr(42)); 109 | } 110 | [TestMethod] 111 | public void implicit_conversion_to_ulong() 112 | { 113 | ulong l = new nuint(42U); 114 | Assert.AreEqual(l, 42UL); 115 | } 116 | [TestMethod] 117 | public void explicit_conversion_to_uint() 118 | { 119 | uint i = (uint)new nuint(42u); 120 | Assert.AreEqual(i, 42u); 121 | } 122 | 123 | [TestMethod] 124 | public void explicit_conversion_to_byte_via_int() 125 | { 126 | Assert.AreEqual((byte)new nuint(0), (byte)0); 127 | Assert.AreEqual((byte)new nuint(42), (byte)42); 128 | Assert.AreEqual((byte)new nuint(255), byte.MaxValue); 129 | Assert.AreEqual((byte)new nuint(256), (byte)0); 130 | } 131 | 132 | [TestMethod] 133 | public void operator_Increment_Pre() 134 | { 135 | var ni = nuint.Zero; 136 | ++ni; 137 | Assert.AreEqual(ni, new nuint(1)); 138 | } 139 | [TestMethod] 140 | public void operator_Increment_Post() 141 | { 142 | var ni = nuint.Zero; 143 | ni++; 144 | Assert.AreEqual(ni, new nuint(1)); 145 | } 146 | 147 | [TestMethod] 148 | public void operator_Decrement_Pre() 149 | { 150 | var ni = nuint.Zero + 1; 151 | --ni; 152 | Assert.AreEqual(ni, new nuint(0)); 153 | } 154 | [TestMethod] 155 | public void operator_Decrement_Post() 156 | { 157 | var ni = nuint.Zero + 1; 158 | ni--; 159 | Assert.AreEqual(ni, new nuint(0)); 160 | } 161 | 162 | [TestMethod] 163 | public void operator_UnaryPlus() 164 | { 165 | var ni = new nuint(1); 166 | Assert.AreEqual(+ni, new nuint(1)); 167 | } 168 | // NOTE: Not available 169 | //[TestMethod] 170 | //public void operator_UnaryNegate() 171 | //{ 172 | // var ni = new nuint(1); 173 | // Assert.AreEqual(-ni, new nuint(-1)); 174 | //} 175 | 176 | [TestMethod] 177 | public void operator_OnesComplement() 178 | { 179 | Assert.AreEqual(~new nuint(1), (nuint)(unchecked((ulong)-2))); 180 | Assert.AreEqual(~new nuint(0), (nuint)(unchecked((ulong)-1))); 181 | Assert.AreEqual(~(nuint)(unchecked((ulong)-1)), new nuint(0)); 182 | } 183 | 184 | [TestMethod] 185 | public void operator_Addition_nuint() 186 | { 187 | var ni = new nuint(1); 188 | Assert.AreEqual(ni + new nuint(1), new nuint(2)); 189 | Assert.AreEqual(new nuint(1) + ni, new nuint(2)); 190 | } 191 | [TestMethod] 192 | public void operator_Addition_UIntPtr() 193 | { 194 | var ni = new nuint(1); 195 | Assert.AreEqual(ni + new UIntPtr(1), new nuint(2)); 196 | Assert.AreEqual(new UIntPtr(1) + ni, new nuint(2)); 197 | } 198 | [TestMethod] 199 | public void operator_Addition_int() 200 | { 201 | var ni = new nuint(1); 202 | Assert.AreEqual(ni + 1, new nuint(2)); 203 | Assert.AreEqual(1 + ni, new nuint(2)); 204 | } 205 | 206 | [TestMethod] 207 | public void operator_Subtraction_nuint() 208 | { 209 | var ni = new nuint(1); 210 | Assert.AreEqual(ni - new nuint(1), new nuint(0)); 211 | Assert.AreEqual(new nuint(1) - ni, new nuint(0)); 212 | } 213 | [TestMethod] 214 | public void operator_Subtraction_UIntPtr() 215 | { 216 | var ni = new nuint(1); 217 | Assert.AreEqual(ni - new UIntPtr(1), new nuint(0)); 218 | Assert.AreEqual(new UIntPtr(1) - ni, new nuint(0)); 219 | } 220 | [TestMethod] 221 | public void operator_Subtraction_int() 222 | { 223 | var ni = new nuint(1); 224 | Assert.AreEqual(ni - 1, new nuint(0)); 225 | Assert.AreEqual(1 - ni, new nuint(0)); 226 | } 227 | 228 | [TestMethod] 229 | public void operator_Multiply_nuint() 230 | { 231 | var ni = new nuint(7); 232 | Assert.AreEqual(ni * new nuint(2), new nuint(14)); 233 | } 234 | [TestMethod] 235 | public void operator_Multiply_UIntPtr() 236 | { 237 | var ni = new nuint(7); 238 | Assert.AreEqual(ni * new UIntPtr(2), new nuint(14)); 239 | } 240 | [TestMethod] 241 | public void operator_Multiply_int() 242 | { 243 | var ni = new nuint(7); 244 | Assert.AreEqual(ni * 2, new nuint(14)); 245 | } 246 | 247 | [TestMethod] 248 | public void operator_Division_nuint() 249 | { 250 | var ni = new nuint(7); 251 | Assert.AreEqual(ni / new nuint(2), new nuint(3)); 252 | } 253 | [TestMethod] 254 | public void operator_Division_UIntPtr() 255 | { 256 | var ni = new nuint(7); 257 | Assert.AreEqual(ni / new UIntPtr(2), new nuint(3)); 258 | } 259 | [TestMethod] 260 | public void operator_Division_int() 261 | { 262 | var ni = new nuint(7); 263 | Assert.AreEqual(ni / 2, new nuint(3)); 264 | } 265 | 266 | [TestMethod] 267 | public void operator_Modulus_nuint() 268 | { 269 | var ni = new nuint(7); 270 | Assert.AreEqual(ni % new nuint(4), new nuint(3)); 271 | } 272 | [TestMethod] 273 | public void operator_Modulus_UIntPtr() 274 | { 275 | var ni = new nuint(7); 276 | Assert.AreEqual(ni % new UIntPtr(4), new nuint(3)); 277 | } 278 | [TestMethod] 279 | public void operator_Modulus_int() 280 | { 281 | var ni = new nuint(7); 282 | Assert.AreEqual(ni % 4, new nuint(3)); 283 | } 284 | 285 | [TestMethod] 286 | public void operator_ExclusiveOr_nuint() 287 | { 288 | var ni = new nuint(7); 289 | Assert.AreEqual(ni ^ new nuint(4), new nuint(3)); 290 | 291 | } 292 | [TestMethod] 293 | public void operator_ExclusiveOr_UIntPtr() 294 | { 295 | var ni = new nuint(7); 296 | Assert.AreEqual(ni ^ new UIntPtr(4), new nuint(3)); 297 | } 298 | [TestMethod] 299 | public void operator_ExclusiveOr_int() 300 | { 301 | var ni = new nuint(7); 302 | Assert.AreEqual(ni ^ 4, new nuint(3)); 303 | } 304 | 305 | [TestMethod] 306 | public void operator_BitwiseAnd_nuint() 307 | { 308 | var ni = new nuint(7); 309 | Assert.AreEqual(ni & new nuint(12), new nuint(4)); 310 | } 311 | [TestMethod] 312 | public void operator_BitwiseAnd_UIntPtr() 313 | { 314 | var ni = new nuint(7); 315 | Assert.AreEqual(ni & new UIntPtr(12), new nuint(4)); 316 | } 317 | [TestMethod] 318 | public void operator_BitwiseAnd_int() 319 | { 320 | var ni = new nuint(7); 321 | Assert.AreEqual(ni & 12, new nuint(4)); 322 | } 323 | 324 | [TestMethod] 325 | public void operator_BitwiseOr_nuint() 326 | { 327 | var ni = new nuint(7); 328 | Assert.AreEqual(ni | new nuint(8), new nuint(15)); 329 | } 330 | [TestMethod] 331 | public void operator_BitwiseOr_UIntPtr() 332 | { 333 | var ni = new nuint(7); 334 | Assert.AreEqual(ni | new UIntPtr(8), new nuint(15)); 335 | } 336 | [TestMethod] 337 | public void operator_BitwiseOr_int() 338 | { 339 | var ni = new nuint(7); 340 | Assert.AreEqual(ni | 8, new nuint(15)); 341 | } 342 | 343 | [TestMethod] 344 | public void operator_LeftShift_nuint() 345 | { 346 | var ni = new nuint(7); 347 | Assert.AreEqual(ni << new nuint(2), new nuint(28)); 348 | } 349 | [TestMethod] 350 | public void operator_LeftShift_UIntPtr() 351 | { 352 | var ni = new nuint(7); 353 | Assert.AreEqual(ni << new UIntPtr(2), new nuint(28)); 354 | } 355 | [TestMethod] 356 | public void operator_LeftShift_int() 357 | { 358 | var ni = new nuint(7); 359 | Assert.AreEqual(ni << 2, new nuint(28)); 360 | } 361 | 362 | [TestMethod] 363 | public void operator_RightShift_nuint() 364 | { 365 | var ni = new nuint(28); 366 | Assert.AreEqual(ni >> new nuint(2), new nuint(7)); 367 | } 368 | [TestMethod] 369 | public void operator_RightShift_UIntPtr() 370 | { 371 | var ni = new nuint(28); 372 | Assert.AreEqual(ni >> new UIntPtr(2), new nuint(7)); 373 | } 374 | [TestMethod] 375 | public void operator_RightShift_int() 376 | { 377 | var ni = new nuint(28); 378 | Assert.AreEqual(ni >> 2, new nuint(7)); 379 | } 380 | 381 | [TestMethod] 382 | public void operator_Equality_nuint() 383 | { 384 | Assert.IsTrue(nuint.Zero == new nuint(0)); 385 | Assert.IsTrue(new nuint(0) == new nuint(0)); 386 | Assert.IsTrue(new nuint(1) == new nuint(1)); 387 | Assert.IsTrue(new nuint(uint.MaxValue) == new nuint(uint.MaxValue)); 388 | Assert.IsTrue((nuint)(ulong.MaxValue) == (nuint)(ulong.MaxValue)); 389 | 390 | Assert.IsFalse(new nuint(0) == new nuint(1)); 391 | Assert.IsFalse(new nuint(1) == new nuint(0)); 392 | Assert.IsFalse(new nuint(1) == new nuint(2)); 393 | Assert.IsFalse(new nuint(2) == new nuint(1)); 394 | Assert.IsFalse(new nuint(uint.MaxValue - 1) == new nuint(uint.MaxValue)); 395 | Assert.IsFalse(new nuint(uint.MaxValue) == new nuint(uint.MaxValue - 1)); 396 | } 397 | [TestMethod] 398 | public unsafe void operator_Equality_uint() 399 | { 400 | Assert.IsTrue(nuint.Zero == 0u); 401 | Assert.IsTrue(new nuint(0) == 0u); 402 | Assert.IsTrue(new nuint(1) == 1u); 403 | Assert.IsTrue(new nuint(uint.MaxValue) == uint.MaxValue); 404 | Assert.AreEqual(sizeof(nuint) == sizeof(uint), (nuint)(ulong.MaxValue) == (unchecked((uint)ulong.MaxValue))); 405 | Assert.IsFalse(new nuint(0) == 1u); 406 | Assert.IsFalse(new nuint(1) == 0u); 407 | Assert.IsFalse(new nuint(1) == 2u); 408 | Assert.IsFalse(new nuint(2) == 1u); 409 | 410 | Assert.IsTrue(0u == nuint.Zero); 411 | Assert.IsTrue(0u == new nuint(0)); 412 | Assert.IsTrue(1u == new nuint(1)); 413 | Assert.IsTrue(uint.MaxValue == new nuint(uint.MaxValue)); 414 | Assert.AreEqual(sizeof(nuint) == sizeof(uint), (unchecked((uint)ulong.MaxValue) == (nuint)(ulong.MaxValue))); 415 | Assert.IsFalse(0 == new nuint(1)); 416 | Assert.IsFalse(1 == new nuint(0)); 417 | Assert.IsFalse(1 == new nuint(2)); 418 | Assert.IsFalse(2 == new nuint(1)); 419 | } 420 | [TestMethod] 421 | public unsafe void operator_Equality_ulong() 422 | { 423 | Assert.IsTrue(nuint.Zero == 0UL); 424 | Assert.IsTrue(new nuint(0) == 0UL); 425 | Assert.IsTrue(new nuint(1) == 1UL); 426 | Assert.AreEqual(sizeof(uint) != sizeof(UIntPtr), (nuint)(ulong.MaxValue) == ulong.MaxValue); 427 | Assert.IsFalse(new nuint(0) == 1UL); 428 | Assert.IsFalse(new nuint(1) == 0UL); 429 | Assert.IsFalse(new nuint(1) == 2UL); 430 | Assert.IsFalse(new nuint(2) == 1UL); 431 | 432 | Assert.IsTrue(0UL == nuint.Zero); 433 | Assert.IsTrue(0UL == new nuint(0)); 434 | Assert.IsTrue(1UL == new nuint(1)); 435 | Assert.AreEqual(sizeof(uint) != sizeof(UIntPtr), ulong.MaxValue == (nuint)(ulong.MaxValue)); 436 | Assert.IsFalse(0UL == new nuint(1)); 437 | Assert.IsFalse(1UL == new nuint(0)); 438 | Assert.IsFalse(1UL == new nuint(2)); 439 | Assert.IsFalse(2UL == new nuint(1)); 440 | } 441 | [TestMethod] 442 | public unsafe void operator_Equality_UIntPtr() 443 | { 444 | Assert.IsTrue(nuint.Zero == UIntPtr.Zero); 445 | Assert.IsTrue(new nuint(0) == new UIntPtr(0)); 446 | Assert.IsTrue(new nuint(1) == new UIntPtr(1)); 447 | Assert.IsTrue(new nuint(uint.MaxValue) == new UIntPtr(uint.MaxValue)); 448 | if (sizeof(ulong) == sizeof(UIntPtr)) 449 | { 450 | // ulong -> UIntPtr overflows if 32-bit, nuint doesn't 451 | Assert.IsTrue((nuint)(ulong.MaxValue) == new UIntPtr(ulong.MaxValue)); 452 | } 453 | Assert.IsFalse(new nuint(0) == new UIntPtr(1)); 454 | Assert.IsFalse(new nuint(1) == new UIntPtr(0)); 455 | Assert.IsFalse(new nuint(1) == new UIntPtr(2)); 456 | Assert.IsFalse(new nuint(2) == new UIntPtr(1)); 457 | 458 | Assert.IsTrue(UIntPtr.Zero == nuint.Zero); 459 | Assert.IsTrue(new UIntPtr(0) == new nuint(0)); 460 | Assert.IsTrue(new UIntPtr(1) == new nuint(1)); 461 | Assert.IsTrue(new UIntPtr(uint.MaxValue) == new nuint(uint.MaxValue)); 462 | if (sizeof(ulong) == sizeof(UIntPtr)) 463 | { 464 | // ulong -> UIntPtr overflows if 32-bit, nuint doesn't 465 | Assert.IsTrue(new UIntPtr(ulong.MaxValue) == (nuint)(ulong.MaxValue)); 466 | } 467 | Assert.IsFalse(new UIntPtr(0) == new nuint(1)); 468 | Assert.IsFalse(new UIntPtr(1) == new nuint(0)); 469 | Assert.IsFalse(new UIntPtr(1) == new nuint(2)); 470 | Assert.IsFalse(new UIntPtr(2) == new nuint(1)); 471 | } 472 | 473 | [TestMethod] 474 | public void operator_Inequality_nuint() 475 | { 476 | Assert.IsFalse(nuint.Zero != new nuint(0)); 477 | Assert.IsFalse(new nuint(0) != new nuint(0)); 478 | Assert.IsFalse(new nuint(1) != new nuint(1)); 479 | Assert.IsFalse(new nuint(uint.MaxValue) != new nuint(uint.MaxValue)); 480 | Assert.IsFalse((nuint)(ulong.MaxValue) != (nuint)(ulong.MaxValue)); 481 | 482 | Assert.IsTrue(new nuint(0) != new nuint(1)); 483 | Assert.IsTrue(new nuint(1) != new nuint(0)); 484 | Assert.IsTrue(new nuint(1) != new nuint(2)); 485 | Assert.IsTrue(new nuint(2) != new nuint(1)); 486 | } 487 | [TestMethod] 488 | public unsafe void operator_Inequality_uint() 489 | { 490 | Assert.IsFalse(nuint.Zero != 0u); 491 | Assert.IsFalse(new nuint(0) != 0u); 492 | Assert.IsFalse(new nuint(1) != 1u); 493 | Assert.IsFalse(new nuint(uint.MaxValue) != uint.MaxValue); 494 | Assert.AreEqual(sizeof(uint) != sizeof(UIntPtr), (nuint)(ulong.MaxValue) != (unchecked((uint)ulong.MaxValue))); 495 | Assert.IsTrue(new nuint(0) != 1u); 496 | Assert.IsTrue(new nuint(1) != 0u); 497 | Assert.IsTrue(new nuint(1) != 2u); 498 | Assert.IsTrue(new nuint(2) != 1u); 499 | 500 | Assert.IsFalse(0u != nuint.Zero); 501 | Assert.IsFalse(0u != new nuint(0)); 502 | Assert.IsFalse(1u != new nuint(1)); 503 | Assert.IsFalse(uint.MaxValue != new nuint(uint.MaxValue)); 504 | Assert.AreEqual(sizeof(uint) != sizeof(UIntPtr), (unchecked((uint)ulong.MaxValue) != (nuint)(ulong.MaxValue))); 505 | Assert.IsTrue(0u != new nuint(1)); 506 | Assert.IsTrue(1u != new nuint(0)); 507 | Assert.IsTrue(1u != new nuint(2)); 508 | Assert.IsTrue(2u != new nuint(1)); 509 | } 510 | [TestMethod] 511 | public unsafe void operator_Inequality_ulong() 512 | { 513 | Assert.IsFalse(nuint.Zero != 0UL); 514 | Assert.IsFalse(new nuint(0) != 0UL); 515 | Assert.IsFalse(new nuint(1) != 1UL); 516 | Assert.AreEqual(sizeof(uint) == sizeof(UIntPtr), (nuint)(ulong.MaxValue) != ulong.MaxValue); 517 | Assert.IsTrue(new nuint(0) != 1UL); 518 | Assert.IsTrue(new nuint(1) != 0UL); 519 | Assert.IsTrue(new nuint(1) != 2UL); 520 | Assert.IsTrue(new nuint(2) != 1UL); 521 | 522 | Assert.IsFalse(0UL != nuint.Zero); 523 | Assert.IsFalse(0UL != new nuint(0)); 524 | Assert.IsFalse(1UL != new nuint(1)); 525 | Assert.AreEqual(sizeof(uint) == sizeof(UIntPtr), ulong.MaxValue != (nuint)(ulong.MaxValue)); 526 | Assert.IsTrue(0UL != new nuint(1)); 527 | Assert.IsTrue(1UL != new nuint(0)); 528 | Assert.IsTrue(1UL != new nuint(2)); 529 | Assert.IsTrue(2UL != new nuint(1)); 530 | } 531 | [TestMethod] 532 | public unsafe void operator_Inequality_UIntPtr() 533 | { 534 | Assert.IsFalse(nuint.Zero != UIntPtr.Zero); 535 | Assert.IsFalse(new nuint(0) != new UIntPtr(0)); 536 | Assert.IsFalse(new nuint(1) != new UIntPtr(1)); 537 | Assert.IsFalse(new nuint(uint.MaxValue) != new UIntPtr(uint.MaxValue)); 538 | if (sizeof(ulong) == sizeof(UIntPtr)) 539 | { 540 | // ulong -> UIntPtr overflows if 32-bit, nuint doesn't 541 | Assert.IsFalse((nuint)(ulong.MaxValue) != new UIntPtr(ulong.MaxValue)); 542 | } 543 | Assert.IsTrue(new nuint(0) != new UIntPtr(1)); 544 | Assert.IsTrue(new nuint(1) != new UIntPtr(0)); 545 | Assert.IsTrue(new nuint(1) != new UIntPtr(2)); 546 | Assert.IsTrue(new nuint(2) != new UIntPtr(1)); 547 | 548 | Assert.IsFalse(UIntPtr.Zero != nuint.Zero); 549 | Assert.IsFalse(new UIntPtr(0) != new nuint(0)); 550 | Assert.IsFalse(new UIntPtr(1) != new nuint(1)); 551 | Assert.IsFalse(new UIntPtr(uint.MaxValue) != new nuint(uint.MaxValue)); 552 | if (sizeof(ulong) == sizeof(UIntPtr)) 553 | { 554 | // ulong -> UIntPtr overflows if 32-bit, nuint doesn't 555 | Assert.IsFalse(new UIntPtr(ulong.MaxValue) != (nuint)(ulong.MaxValue)); 556 | } 557 | Assert.IsTrue(new UIntPtr(0) != new nuint(1)); 558 | Assert.IsTrue(new UIntPtr(1) != new nuint(0)); 559 | Assert.IsTrue(new UIntPtr(1) != new nuint(2)); 560 | Assert.IsTrue(new UIntPtr(2) != new nuint(1)); 561 | } 562 | 563 | 564 | [TestMethod] 565 | public void operator_GreaterThan_nuint() 566 | { 567 | Assert.IsFalse(nuint.Zero > new nuint(0)); 568 | Assert.IsFalse(new nuint(0) > new nuint(0)); 569 | Assert.IsFalse(new nuint(1) > new nuint(1)); 570 | Assert.IsFalse(new nuint(uint.MaxValue) > new nuint(uint.MaxValue)); 571 | Assert.IsFalse((nuint)(ulong.MaxValue) > (nuint)(ulong.MaxValue)); 572 | 573 | Assert.IsFalse(new nuint(0) > new nuint(1)); 574 | Assert.IsFalse(new nuint(1) > new nuint(2)); 575 | Assert.IsFalse((nuint)(uint.MaxValue - 1) > (nuint)(uint.MaxValue)); 576 | 577 | Assert.IsTrue(new nuint(1) > new nuint(0)); 578 | Assert.IsTrue(new nuint(2) > new nuint(1)); 579 | Assert.IsTrue((nuint)(uint.MaxValue) > (nuint)(uint.MaxValue - 1)); 580 | } 581 | [TestMethod] 582 | public unsafe void operator_GreaterThan_uint() 583 | { 584 | // nuint left, uint right 585 | Assert.IsFalse(nuint.Zero > 0u); 586 | Assert.IsFalse(new nuint(0) > 0u); 587 | Assert.IsFalse(new nuint(1) > 1u); 588 | Assert.IsFalse(new nuint(uint.MaxValue) > uint.MaxValue); 589 | 590 | Assert.IsFalse(new nuint(0) > 1u); 591 | Assert.IsFalse(new nuint(1) > 2u); 592 | Assert.IsFalse((nuint)(uint.MaxValue - 1) > uint.MaxValue); 593 | 594 | Assert.IsTrue(new nuint(1) > 0u); 595 | Assert.IsTrue(new nuint(2) > 1u); 596 | Assert.IsTrue((nuint)(uint.MaxValue) > uint.MaxValue - 1); 597 | 598 | Assert.IsTrue(ulong.MaxValue > (ulong)uint.MaxValue); 599 | 600 | Assert.AreEqual(sizeof(ulong) == sizeof(UIntPtr), ((nuint)ulong.MaxValue) > (nuint)uint.MaxValue); 601 | 602 | // uint left, nuint right 603 | Assert.IsFalse(0u > new nuint(0)); 604 | Assert.IsFalse(0u > new nuint(0)); 605 | Assert.IsFalse(1u > new nuint(1)); 606 | Assert.IsFalse(uint.MaxValue > new nuint(uint.MaxValue)); 607 | 608 | Assert.IsFalse(0u > new nuint(1)); 609 | Assert.IsFalse(1u > new nuint(2)); 610 | Assert.IsFalse((uint.MaxValue - 1) > new nuint(uint.MaxValue)); 611 | 612 | Assert.IsTrue(1u > new nuint(0)); 613 | Assert.IsTrue(2u > new nuint(1)); 614 | Assert.IsTrue(uint.MaxValue > new nuint(uint.MaxValue - 1)); 615 | 616 | Assert.AreEqual(false, uint.MaxValue > (nuint)(ulong.MaxValue)); 617 | } 618 | [TestMethod] 619 | public unsafe void operator_GreaterThan_ulong() 620 | { 621 | // nuint left, ulong right 622 | Assert.IsFalse(nuint.Zero > 0UL); 623 | Assert.IsFalse(new nuint(0) > 0UL); 624 | Assert.IsFalse(new nuint(1) > 1UL); 625 | Assert.IsFalse(new nuint(uint.MaxValue) > (ulong)uint.MaxValue); 626 | Assert.IsFalse((nuint)(ulong.MaxValue) > ulong.MaxValue); 627 | 628 | Assert.IsFalse(new nuint(0) > 1UL); 629 | Assert.IsFalse(new nuint(1) > 2UL); 630 | Assert.IsFalse((nuint)(uint.MaxValue - 1) > (ulong)uint.MaxValue); 631 | Assert.IsFalse((nuint)(ulong.MaxValue - 1) > ulong.MaxValue); 632 | 633 | Assert.IsTrue(new nuint(1) > 0UL); 634 | Assert.IsTrue(new nuint(2) > 1UL); 635 | Assert.IsTrue((nuint)(uint.MaxValue) > (ulong)(uint.MaxValue - 1)); 636 | Assert.AreEqual(sizeof(ulong) == sizeof(UIntPtr), (nuint)(ulong.MaxValue) > (ulong.MaxValue - 1)); 637 | 638 | Assert.AreEqual(sizeof(ulong) == sizeof(UIntPtr), (nuint)(ulong.MaxValue) > (ulong)uint.MaxValue); 639 | Assert.AreEqual(sizeof(ulong) == sizeof(UIntPtr), (nuint)(ulong.MaxValue) > (ulong)uint.MaxValue); 640 | 641 | // ulong left, nuint right 642 | Assert.IsFalse(0UL > new nuint(0)); 643 | Assert.IsFalse(0UL > new nuint(0)); 644 | Assert.IsFalse(1UL > new nuint(1)); 645 | Assert.IsFalse((ulong)uint.MaxValue > new nuint(uint.MaxValue)); 646 | Assert.AreEqual(sizeof(ulong) != sizeof(UIntPtr), ulong.MaxValue > (nuint)(ulong.MaxValue)); 647 | 648 | Assert.IsFalse(0UL > new nuint(1)); 649 | Assert.IsFalse(1UL > new nuint(2)); 650 | Assert.IsFalse((ulong)(uint.MaxValue - 1) > new nuint(uint.MaxValue)); 651 | Assert.IsTrue((ulong.MaxValue - 1) > new nuint(uint.MaxValue)); 652 | 653 | Assert.IsTrue(1UL > new nuint(0)); 654 | Assert.IsTrue(2UL > new nuint(1)); 655 | Assert.IsTrue((ulong)uint.MaxValue > new nuint(uint.MaxValue - 1)); 656 | Assert.IsTrue(ulong.MaxValue > (nuint)(ulong.MaxValue - 1)); 657 | 658 | Assert.IsFalse((ulong)uint.MaxValue > (nuint)(ulong.MaxValue)); 659 | } 660 | [TestMethod] 661 | public unsafe void operator_GreaterThan_UIntPtr() 662 | { 663 | // nuint left, UIntPtr right 664 | Assert.IsFalse(nuint.Zero > new UIntPtr(0L)); 665 | Assert.IsFalse(new nuint(0) > new UIntPtr(0)); 666 | Assert.IsFalse(new nuint(1) > new UIntPtr(1)); 667 | Assert.IsFalse(new nuint(uint.MaxValue) > new UIntPtr((ulong)uint.MaxValue)); 668 | if (sizeof(ulong) == sizeof(UIntPtr)) 669 | { 670 | Assert.IsFalse((nuint)(ulong.MaxValue) > new UIntPtr(ulong.MaxValue)); 671 | } 672 | 673 | Assert.IsFalse(new nuint(0) > new UIntPtr(1)); 674 | Assert.IsFalse(new nuint(1) > new UIntPtr(2)); 675 | Assert.IsFalse((nuint)(uint.MaxValue - 1) > new UIntPtr((ulong)uint.MaxValue)); 676 | if (sizeof(ulong) == sizeof(UIntPtr)) 677 | { 678 | Assert.IsFalse((nuint)(ulong.MaxValue - 1) > new UIntPtr(ulong.MaxValue)); 679 | } 680 | 681 | Assert.IsTrue(new nuint(1) > new UIntPtr(0)); 682 | Assert.IsTrue(new nuint(2) > new UIntPtr(1)); 683 | Assert.IsTrue((nuint)(uint.MaxValue) > new UIntPtr((ulong)(uint.MaxValue - 1))); 684 | if (sizeof(ulong) == sizeof(UIntPtr)) 685 | { 686 | Assert.AreEqual(sizeof(ulong) == sizeof(UIntPtr), (nuint)(ulong.MaxValue) > new UIntPtr((ulong.MaxValue - 1))); 687 | } 688 | 689 | Assert.AreEqual(sizeof(ulong) == sizeof(UIntPtr), (nuint)(ulong.MaxValue) > new UIntPtr((ulong)uint.MaxValue)); 690 | Assert.AreEqual(sizeof(ulong) == sizeof(UIntPtr), (nuint)(ulong.MaxValue) > new UIntPtr((ulong)uint.MaxValue)); 691 | 692 | // UIntPtr left, nuint right 693 | Assert.IsFalse(new UIntPtr(0) > new nuint(0)); 694 | Assert.IsFalse(new UIntPtr(1) > new nuint(1)); 695 | Assert.IsFalse((ulong)uint.MaxValue > new nuint(uint.MaxValue)); 696 | Assert.AreEqual(sizeof(ulong) != sizeof(UIntPtr), ulong.MaxValue > (nuint)(ulong.MaxValue)); 697 | Assert.IsFalse((nuint)(ulong.MaxValue) > ulong.MaxValue); 698 | 699 | Assert.IsFalse(new UIntPtr(0) > new nuint(1)); 700 | Assert.IsFalse(new UIntPtr(1) > new nuint(2)); 701 | Assert.IsFalse((ulong)(uint.MaxValue - 1) > new nuint(uint.MaxValue)); 702 | Assert.IsTrue((ulong.MaxValue - 1) > new nuint(uint.MaxValue)); 703 | 704 | Assert.IsTrue(new UIntPtr(1) > new nuint(0)); 705 | Assert.IsTrue(new UIntPtr(2) > new nuint(1)); 706 | Assert.IsTrue((ulong)uint.MaxValue > new nuint(uint.MaxValue - 1)); 707 | Assert.IsTrue(ulong.MaxValue > (nuint)(ulong.MaxValue - 1)); 708 | 709 | Assert.IsFalse((ulong)uint.MaxValue > (nuint)(ulong.MaxValue)); 710 | } 711 | 712 | 713 | [TestMethod] 714 | public void operator_GreaterThanOrEqual_nuint() 715 | { 716 | Assert.IsTrue(nuint.Zero >= new nuint(0)); 717 | Assert.IsTrue(new nuint(0) >= new nuint(0)); 718 | Assert.IsTrue(new nuint(1) >= new nuint(1)); 719 | Assert.IsTrue(new nuint(uint.MaxValue) >= new nuint(uint.MaxValue)); 720 | Assert.IsTrue((nuint)(ulong.MaxValue) >= (nuint)(ulong.MaxValue)); 721 | 722 | Assert.IsFalse(new nuint(0) >= new nuint(1)); 723 | Assert.IsFalse(new nuint(1) >= new nuint(2)); 724 | Assert.IsFalse((nuint)(uint.MaxValue - 1) >= (nuint)(uint.MaxValue)); 725 | 726 | Assert.IsTrue(new nuint(1) >= new nuint(0)); 727 | Assert.IsTrue(new nuint(2) >= new nuint(1)); 728 | Assert.IsTrue((nuint)(uint.MaxValue) >= (nuint)(uint.MaxValue - 1)); 729 | } 730 | [TestMethod] 731 | public unsafe void operator_GreaterThanOrEqual_int() 732 | { 733 | // nuint left, uint right 734 | Assert.IsTrue(nuint.Zero >= 0); 735 | Assert.IsTrue(new nuint(0) >= 0); 736 | Assert.IsTrue(new nuint(1) >= 1); 737 | Assert.IsTrue(new nuint(uint.MaxValue) >= uint.MaxValue); 738 | 739 | Assert.IsFalse(new nuint(0) >= 1); 740 | Assert.IsFalse(new nuint(1) >= 2); 741 | Assert.IsFalse((nuint)(uint.MaxValue - 1) >= uint.MaxValue); 742 | 743 | Assert.IsTrue(new nuint(1) >= 0); 744 | Assert.IsTrue(new nuint(2) >= 1); 745 | Assert.IsTrue((nuint)(uint.MaxValue) >= uint.MaxValue - 1); 746 | 747 | Assert.IsTrue((nuint)(ulong.MaxValue) >= uint.MaxValue); 748 | 749 | // uint left, nuint right 750 | Assert.IsTrue(0 >= new nuint(0)); 751 | Assert.IsTrue(0 >= new nuint(0)); 752 | Assert.IsTrue(1 >= new nuint(1)); 753 | Assert.IsTrue(uint.MaxValue >= new nuint(uint.MaxValue)); 754 | 755 | Assert.IsFalse(0 >= new nuint(1)); 756 | Assert.IsFalse(1 >= new nuint(2)); 757 | Assert.IsFalse((uint.MaxValue - 1) >= new nuint(uint.MaxValue)); 758 | 759 | Assert.IsTrue(1 >= new nuint(0)); 760 | Assert.IsTrue(2 >= new nuint(1)); 761 | Assert.IsTrue(uint.MaxValue >= new nuint(uint.MaxValue - 1)); 762 | 763 | Assert.AreEqual(sizeof(ulong) != sizeof(UIntPtr), uint.MaxValue >= (nuint)(ulong.MaxValue)); 764 | Assert.AreEqual(sizeof(ulong) != sizeof(UIntPtr), uint.MaxValue >= (nuint)(ulong.MaxValue)); 765 | } 766 | [TestMethod] 767 | public unsafe void operator_GreaterThanOrEqual_long() 768 | { 769 | // nuint left, ulong right 770 | Assert.IsTrue(nuint.Zero >= 0L); 771 | Assert.IsTrue(new nuint(0) >= 0L); 772 | Assert.IsTrue(new nuint(1) >= 1L); 773 | Assert.IsTrue(new nuint(uint.MaxValue) >= (ulong)uint.MaxValue); 774 | Assert.AreEqual(sizeof(ulong) == sizeof(UIntPtr), (nuint)(ulong.MaxValue) >= ulong.MaxValue); 775 | 776 | Assert.IsFalse(new nuint(0) >= 1L); 777 | Assert.IsFalse(new nuint(1) >= 2L); 778 | Assert.IsFalse((nuint)(uint.MaxValue - 1) >= (ulong)uint.MaxValue); 779 | Assert.IsFalse((nuint)(ulong.MaxValue - 1) >= ulong.MaxValue); 780 | 781 | Assert.IsTrue(new nuint(1) >= 0L); 782 | Assert.IsTrue(new nuint(2) >= 1L); 783 | Assert.IsTrue((nuint)(uint.MaxValue) >= (ulong)(uint.MaxValue - 1)); 784 | Assert.AreEqual(sizeof(ulong) == sizeof(UIntPtr), (nuint)(ulong.MaxValue) >= (ulong.MaxValue - 1)); 785 | 786 | Assert.IsTrue((nuint)(ulong.MaxValue) >= (ulong)uint.MaxValue); 787 | 788 | // ulong left, nuint right 789 | Assert.IsTrue(0L >= new nuint(0)); 790 | Assert.IsTrue(0L >= new nuint(0)); 791 | Assert.IsTrue(1L >= new nuint(1)); 792 | Assert.IsTrue((ulong)uint.MaxValue >= new nuint(uint.MaxValue)); 793 | Assert.IsTrue(ulong.MaxValue >= (nuint)(ulong.MaxValue)); 794 | 795 | Assert.IsFalse(0L >= new nuint(1)); 796 | Assert.IsFalse(1L >= new nuint(2)); 797 | Assert.IsFalse((ulong)(uint.MaxValue - 1) >= new nuint(uint.MaxValue)); 798 | Assert.IsTrue((ulong.MaxValue - 1) >= new nuint(uint.MaxValue)); 799 | 800 | Assert.IsTrue(1L >= new nuint(0)); 801 | Assert.IsTrue(2L >= new nuint(1)); 802 | Assert.IsTrue((ulong)uint.MaxValue >= new nuint(uint.MaxValue - 1)); 803 | Assert.IsTrue(ulong.MaxValue >= (nuint)(ulong.MaxValue - 1)); 804 | 805 | Assert.AreEqual(sizeof(ulong) != sizeof(UIntPtr), (ulong)uint.MaxValue >= (nuint)(ulong.MaxValue)); 806 | } 807 | [TestMethod] 808 | public unsafe void operator_GreaterThanOrEqual_UIntPtr() 809 | { 810 | // nuint left, UIntPtr right 811 | Assert.IsTrue(nuint.Zero >= new UIntPtr(0L)); 812 | Assert.IsTrue(new nuint(0) >= new UIntPtr(0)); 813 | Assert.IsTrue(new nuint(1) >= new UIntPtr(1)); 814 | Assert.IsTrue(new nuint(uint.MaxValue) >= new UIntPtr((ulong)uint.MaxValue)); 815 | if (sizeof(ulong) == sizeof(UIntPtr)) 816 | { 817 | Assert.IsTrue((nuint)(ulong.MaxValue) >= new UIntPtr(ulong.MaxValue)); 818 | } 819 | 820 | Assert.IsFalse(new nuint(0) >= new UIntPtr(1)); 821 | Assert.IsFalse(new nuint(1) >= new UIntPtr(2)); 822 | Assert.IsFalse((nuint)(uint.MaxValue - 1) >= new UIntPtr((ulong)uint.MaxValue)); 823 | if (sizeof(ulong) == sizeof(UIntPtr)) 824 | { 825 | Assert.IsFalse((nuint)(ulong.MaxValue - 1) >= new UIntPtr(ulong.MaxValue)); 826 | } 827 | 828 | Assert.IsTrue(new nuint(1) >= new UIntPtr(0)); 829 | Assert.IsTrue(new nuint(2) >= new UIntPtr(1)); 830 | Assert.IsTrue((nuint)(uint.MaxValue) >= new UIntPtr((ulong)(uint.MaxValue - 1))); 831 | if (sizeof(ulong) == sizeof(UIntPtr)) 832 | { 833 | Assert.AreEqual(sizeof(ulong) == sizeof(UIntPtr), (nuint)(ulong.MaxValue) >= new UIntPtr((ulong.MaxValue - 1))); 834 | } 835 | 836 | Assert.IsTrue((nuint)(ulong.MaxValue) >= new UIntPtr((ulong)uint.MaxValue)); 837 | 838 | // UIntPtr left, nuint right 839 | Assert.IsTrue(new UIntPtr(0) >= new nuint(0)); 840 | Assert.IsTrue(new UIntPtr(1) >= new nuint(1)); 841 | Assert.IsTrue((ulong)uint.MaxValue >= new nuint(uint.MaxValue)); 842 | Assert.IsTrue(ulong.MaxValue >= (nuint)(ulong.MaxValue)); 843 | 844 | Assert.IsFalse(new UIntPtr(0) >= new nuint(1)); 845 | Assert.IsFalse(new UIntPtr(1) >= new nuint(2)); 846 | Assert.IsFalse((ulong)(uint.MaxValue - 1) >= new nuint(uint.MaxValue)); 847 | Assert.IsTrue((ulong.MaxValue - 1) >= new nuint(uint.MaxValue)); 848 | 849 | Assert.IsTrue(new UIntPtr(1) >= new nuint(0)); 850 | Assert.IsTrue(new UIntPtr(2) >= new nuint(1)); 851 | Assert.IsTrue((ulong)uint.MaxValue >= new nuint(uint.MaxValue - 1)); 852 | Assert.IsTrue(ulong.MaxValue >= (nuint)(ulong.MaxValue - 1)); 853 | 854 | Assert.AreEqual(sizeof(ulong) != sizeof(UIntPtr), (ulong)uint.MaxValue >= (nuint)(ulong.MaxValue)); 855 | } 856 | 857 | 858 | [TestMethod] 859 | public void operator_LessThan_nuint() 860 | { 861 | Assert.IsFalse(nuint.Zero < new nuint(0)); 862 | Assert.IsFalse(new nuint(0) < new nuint(0)); 863 | Assert.IsFalse(new nuint(1) < new nuint(1)); 864 | Assert.IsFalse(new nuint(uint.MaxValue) < new nuint(uint.MaxValue)); 865 | Assert.IsFalse((nuint)(ulong.MaxValue) < (nuint)(ulong.MaxValue)); 866 | 867 | Assert.IsTrue(new nuint(0) < new nuint(1)); 868 | Assert.IsTrue(new nuint(1) < new nuint(2)); 869 | Assert.IsTrue((nuint)(uint.MaxValue - 1) < (nuint)(uint.MaxValue)); 870 | 871 | Assert.IsFalse(new nuint(1) < new nuint(0)); 872 | Assert.IsFalse(new nuint(2) < new nuint(1)); 873 | Assert.IsFalse((nuint)(uint.MaxValue) < (nuint)(uint.MaxValue - 1)); 874 | } 875 | [TestMethod] 876 | public unsafe void operator_LessThan_int() 877 | { 878 | // nuint left, uint right 879 | Assert.IsFalse(nuint.Zero < 0); 880 | Assert.IsFalse(new nuint(0) < 0); 881 | Assert.IsFalse(new nuint(1) < 1); 882 | Assert.IsFalse(new nuint(uint.MaxValue) < uint.MaxValue); 883 | 884 | Assert.IsTrue(new nuint(0) < 1); 885 | Assert.IsTrue(new nuint(1) < 2); 886 | Assert.IsTrue((nuint)(uint.MaxValue - 1) < uint.MaxValue); 887 | 888 | Assert.IsFalse(new nuint(1) < 0); 889 | Assert.IsFalse(new nuint(2) < 1); 890 | Assert.IsFalse((nuint)(uint.MaxValue) < uint.MaxValue - 1); 891 | 892 | Assert.IsFalse((nuint)(ulong.MaxValue) < uint.MaxValue); 893 | Assert.IsFalse((nuint)(ulong.MaxValue) < uint.MaxValue); 894 | 895 | // uint left, nuint right 896 | Assert.IsFalse(0 < new nuint(0)); 897 | Assert.IsFalse(1 < new nuint(1)); 898 | Assert.IsFalse(uint.MaxValue < new nuint(uint.MaxValue)); 899 | 900 | Assert.IsTrue(0 < new nuint(1)); 901 | Assert.IsTrue(1 < new nuint(2)); 902 | Assert.IsTrue((uint.MaxValue - 1) < new nuint(uint.MaxValue)); 903 | 904 | Assert.IsFalse(1 < new nuint(0)); 905 | Assert.IsFalse(2 < new nuint(1)); 906 | Assert.IsFalse(uint.MaxValue < new nuint(uint.MaxValue - 1)); 907 | 908 | Assert.AreEqual(sizeof(ulong) == sizeof(UIntPtr), uint.MaxValue < (nuint)(ulong.MaxValue)); 909 | Assert.AreEqual(sizeof(ulong) == sizeof(UIntPtr), uint.MaxValue < (nuint)(ulong.MaxValue)); 910 | } 911 | [TestMethod] 912 | public unsafe void operator_LessThan_long() 913 | { 914 | // nuint left, ulong right 915 | Assert.IsFalse(nuint.Zero < 0L); 916 | Assert.IsFalse(new nuint(0) < 0L); 917 | Assert.IsFalse(new nuint(1) < 1L); 918 | Assert.IsFalse(new nuint(uint.MaxValue) < (ulong)uint.MaxValue); 919 | Assert.AreEqual(sizeof(ulong) != sizeof(UIntPtr), (nuint)(ulong.MaxValue) < ulong.MaxValue); 920 | 921 | Assert.IsTrue(new nuint(0) < 1L); 922 | Assert.IsTrue(new nuint(1) < 2L); 923 | Assert.IsTrue((nuint)(uint.MaxValue - 1) < (ulong)uint.MaxValue); 924 | Assert.IsTrue((nuint)(ulong.MaxValue - 1) < ulong.MaxValue); 925 | 926 | Assert.IsFalse(new nuint(1) < 0L); 927 | Assert.IsFalse(new nuint(2) < 1L); 928 | Assert.IsFalse((nuint)(uint.MaxValue) < (ulong)(uint.MaxValue - 1)); 929 | Assert.AreEqual(sizeof(ulong) != sizeof(UIntPtr), (nuint)(ulong.MaxValue) < (ulong.MaxValue - 1)); 930 | 931 | Assert.IsFalse((nuint)(ulong.MaxValue) < (ulong)uint.MaxValue); 932 | 933 | // ulong left, nuint right 934 | Assert.IsFalse(0L < new nuint(0)); 935 | Assert.IsFalse(1L < new nuint(1)); 936 | Assert.IsFalse((ulong)uint.MaxValue < new nuint(uint.MaxValue)); 937 | Assert.IsFalse(ulong.MaxValue < (nuint)(ulong.MaxValue)); 938 | 939 | Assert.IsTrue(0L < new nuint(1)); 940 | Assert.IsTrue(1L < new nuint(2)); 941 | Assert.IsTrue((ulong)(uint.MaxValue - 1) < new nuint(uint.MaxValue)); 942 | Assert.IsFalse((ulong.MaxValue - 1) < new nuint(uint.MaxValue)); 943 | 944 | Assert.IsFalse(1L < new nuint(0)); 945 | Assert.IsFalse(2L < new nuint(1)); 946 | Assert.IsFalse((ulong)uint.MaxValue < new nuint(uint.MaxValue - 1)); 947 | Assert.IsFalse(ulong.MaxValue < (nuint)(ulong.MaxValue - 1)); 948 | 949 | Assert.AreEqual(sizeof(ulong) == sizeof(UIntPtr), (ulong)uint.MaxValue < (nuint)(ulong.MaxValue)); 950 | } 951 | [TestMethod] 952 | public unsafe void operator_LessThan_UIntPtr() 953 | { 954 | // nuint left, UIntPtr right 955 | Assert.IsFalse(nuint.Zero < new UIntPtr(0)); 956 | Assert.IsFalse(new nuint(0) < new UIntPtr(0)); 957 | Assert.IsFalse(new nuint(1) < new UIntPtr(1)); 958 | Assert.IsFalse(new nuint(uint.MaxValue) < new UIntPtr((ulong)uint.MaxValue)); 959 | if (sizeof(ulong) == sizeof(UIntPtr)) 960 | { 961 | Assert.IsFalse((nuint)(ulong.MaxValue) < new UIntPtr(ulong.MaxValue)); 962 | } 963 | 964 | Assert.IsTrue(new nuint(0) < new UIntPtr(1)); 965 | Assert.IsTrue(new nuint(1) < new UIntPtr(2)); 966 | Assert.IsTrue((nuint)(uint.MaxValue - 1) < new UIntPtr((ulong)uint.MaxValue)); 967 | if (sizeof(ulong) == sizeof(UIntPtr)) 968 | { 969 | Assert.IsTrue((nuint)(ulong.MaxValue - 1) < new UIntPtr(ulong.MaxValue)); 970 | } 971 | 972 | Assert.IsFalse(new nuint(1) < new UIntPtr(0)); 973 | Assert.IsFalse(new nuint(2) < new UIntPtr(1)); 974 | Assert.IsFalse((nuint)(uint.MaxValue) < new UIntPtr((ulong)(uint.MaxValue - 1))); 975 | if (sizeof(ulong) == sizeof(UIntPtr)) 976 | { 977 | Assert.IsFalse((nuint)(ulong.MaxValue) < new UIntPtr((ulong.MaxValue - 1))); 978 | } 979 | 980 | Assert.IsFalse((nuint)(ulong.MaxValue) < new UIntPtr((ulong)uint.MaxValue)); 981 | 982 | // UIntPtr left, nuint right 983 | Assert.IsFalse(new UIntPtr(0) < new nuint(0)); 984 | Assert.IsFalse(new UIntPtr(1) < new nuint(1)); 985 | Assert.IsFalse((ulong)uint.MaxValue < new nuint(uint.MaxValue)); 986 | Assert.IsFalse(ulong.MaxValue < (nuint)(ulong.MaxValue)); 987 | 988 | Assert.IsTrue(new UIntPtr(0) < new nuint(1)); 989 | Assert.IsTrue(new UIntPtr(1) < new nuint(2)); 990 | Assert.IsTrue((ulong)(uint.MaxValue - 1) < new nuint(uint.MaxValue)); 991 | Assert.IsFalse((ulong.MaxValue - 1) < new nuint(uint.MaxValue)); 992 | 993 | Assert.IsFalse(new UIntPtr(1) < new nuint(0)); 994 | Assert.IsFalse(new UIntPtr(2) < new nuint(1)); 995 | Assert.IsFalse((ulong)uint.MaxValue < new nuint(uint.MaxValue - 1)); 996 | Assert.IsFalse(ulong.MaxValue < (nuint)(ulong.MaxValue - 1)); 997 | 998 | Assert.AreEqual(sizeof(ulong) == sizeof(UIntPtr), (ulong)uint.MaxValue < (nuint)(ulong.MaxValue)); 999 | } 1000 | 1001 | 1002 | [TestMethod] 1003 | public void operator_LessThanOrEqual_nuint() 1004 | { 1005 | Assert.IsTrue(nuint.Zero <= new nuint(0)); 1006 | Assert.IsTrue(new nuint(0) <= new nuint(0)); 1007 | Assert.IsTrue(new nuint(1) <= new nuint(1)); 1008 | Assert.IsTrue(new nuint(uint.MaxValue) <= new nuint(uint.MaxValue)); 1009 | Assert.IsTrue((nuint)(ulong.MaxValue) <= (nuint)(ulong.MaxValue)); 1010 | 1011 | Assert.IsTrue(new nuint(0) <= new nuint(1)); 1012 | Assert.IsTrue(new nuint(1) <= new nuint(2)); 1013 | Assert.IsTrue((nuint)(uint.MaxValue - 1) <= (nuint)(uint.MaxValue)); 1014 | 1015 | Assert.IsFalse(new nuint(1) <= new nuint(0)); 1016 | Assert.IsFalse(new nuint(2) <= new nuint(1)); 1017 | Assert.IsFalse((nuint)(uint.MaxValue) <= (nuint)(uint.MaxValue - 1)); 1018 | } 1019 | [TestMethod] 1020 | public unsafe void operator_LessThanOrEqual_int() 1021 | { 1022 | // nuint left, uint right 1023 | Assert.IsTrue(nuint.Zero <= 0); 1024 | Assert.IsTrue(new nuint(0) <= 0); 1025 | Assert.IsTrue(new nuint(1) <= 1); 1026 | Assert.IsTrue(new nuint(uint.MaxValue) <= uint.MaxValue); 1027 | 1028 | Assert.IsTrue(new nuint(0) <= 1); 1029 | Assert.IsTrue(new nuint(1) <= 2); 1030 | Assert.IsTrue((nuint)(uint.MaxValue - 1) <= uint.MaxValue); 1031 | 1032 | Assert.IsFalse(new nuint(1) <= 0); 1033 | Assert.IsFalse(new nuint(2) <= 1); 1034 | Assert.IsFalse((nuint)(uint.MaxValue) <= uint.MaxValue - 1); 1035 | 1036 | Assert.AreEqual(sizeof(ulong) != sizeof(UIntPtr), (nuint)(ulong.MaxValue) <= uint.MaxValue); 1037 | Assert.AreEqual(sizeof(ulong) != sizeof(UIntPtr), (nuint)(ulong.MaxValue) <= uint.MaxValue); 1038 | 1039 | // uint left, nuint right 1040 | Assert.IsTrue(0 <= new nuint(0)); 1041 | Assert.IsTrue(1 <= new nuint(1)); 1042 | Assert.IsTrue(uint.MaxValue <= new nuint(uint.MaxValue)); 1043 | 1044 | Assert.IsTrue(0 <= new nuint(1)); 1045 | Assert.IsTrue(1 <= new nuint(2)); 1046 | Assert.IsTrue((uint.MaxValue - 1) <= new nuint(uint.MaxValue)); 1047 | 1048 | Assert.IsFalse(1 <= new nuint(0)); 1049 | Assert.IsFalse(2 <= new nuint(1)); 1050 | Assert.IsFalse(uint.MaxValue <= new nuint(uint.MaxValue - 1)); 1051 | 1052 | Assert.IsTrue(uint.MaxValue <= (nuint)(ulong.MaxValue)); 1053 | } 1054 | [TestMethod] 1055 | public unsafe void operator_LessThanOrEqual_long() 1056 | { 1057 | // nuint left, ulong right 1058 | Assert.IsTrue(nuint.Zero <= 0L); 1059 | Assert.IsTrue(new nuint(0) <= 0L); 1060 | Assert.IsTrue(new nuint(1) <= 1L); 1061 | Assert.IsTrue(new nuint(uint.MaxValue) <= (ulong)uint.MaxValue); 1062 | Assert.IsTrue((nuint)(ulong.MaxValue) <= ulong.MaxValue); 1063 | 1064 | Assert.IsTrue(new nuint(0) <= 1L); 1065 | Assert.IsTrue(new nuint(1) <= 2L); 1066 | Assert.IsTrue((nuint)(uint.MaxValue - 1) <= (ulong)uint.MaxValue); 1067 | Assert.IsTrue((nuint)(ulong.MaxValue - 1) <= ulong.MaxValue); 1068 | 1069 | Assert.IsFalse(new nuint(1) <= 0L); 1070 | Assert.IsFalse(new nuint(2) <= 1L); 1071 | Assert.IsFalse((nuint)(uint.MaxValue) <= (ulong)(uint.MaxValue - 1)); 1072 | Assert.AreEqual(sizeof(ulong) != sizeof(UIntPtr), (nuint)(ulong.MaxValue) <= (ulong.MaxValue - 1)); 1073 | 1074 | Assert.AreEqual(sizeof(ulong) != sizeof(UIntPtr), (nuint)(ulong.MaxValue) <= (ulong)uint.MaxValue); 1075 | Assert.AreEqual(sizeof(ulong) != sizeof(UIntPtr), (nuint)(ulong.MaxValue) <= (ulong)uint.MaxValue); 1076 | 1077 | // ulong left, nuint right 1078 | Assert.IsTrue(0L <= new nuint(0)); 1079 | Assert.IsTrue(1L <= new nuint(1)); 1080 | Assert.IsTrue((ulong)uint.MaxValue <= new nuint(uint.MaxValue)); 1081 | Assert.AreEqual(sizeof(ulong) == sizeof(UIntPtr), ulong.MaxValue <= (nuint)(ulong.MaxValue)); 1082 | 1083 | Assert.IsTrue(0L <= new nuint(1)); 1084 | Assert.IsTrue(1L <= new nuint(2)); 1085 | Assert.IsTrue((ulong)(uint.MaxValue - 1) <= new nuint(uint.MaxValue)); 1086 | Assert.IsFalse((ulong.MaxValue - 1) <= new nuint(uint.MaxValue)); 1087 | 1088 | Assert.IsFalse(1L <= new nuint(0)); 1089 | Assert.IsFalse(2L <= new nuint(1)); 1090 | Assert.IsFalse((ulong)uint.MaxValue <= new nuint(uint.MaxValue - 1)); 1091 | Assert.IsFalse(ulong.MaxValue <= (nuint)(ulong.MaxValue - 1)); 1092 | 1093 | Assert.IsTrue((ulong)uint.MaxValue <= (nuint)(ulong.MaxValue)); 1094 | } 1095 | [TestMethod] 1096 | public unsafe void operator_LessThanOrEqual_UIntPtr() 1097 | { 1098 | // nuint left, UIntPtr right 1099 | Assert.IsTrue(nuint.Zero <= new UIntPtr(0)); 1100 | Assert.IsTrue(new nuint(0) <= new UIntPtr(0)); 1101 | Assert.IsTrue(new nuint(1) <= new UIntPtr(1)); 1102 | Assert.IsTrue(new nuint(uint.MaxValue) <= new UIntPtr((ulong)uint.MaxValue)); 1103 | if (sizeof(ulong) == sizeof(UIntPtr)) 1104 | { 1105 | Assert.IsTrue((nuint)(ulong.MaxValue) <= new UIntPtr(ulong.MaxValue)); 1106 | } 1107 | 1108 | Assert.IsTrue(new nuint(0) <= new UIntPtr(1)); 1109 | Assert.IsTrue(new nuint(1) <= new UIntPtr(2)); 1110 | Assert.IsTrue((nuint)(uint.MaxValue - 1) <= new UIntPtr((ulong)uint.MaxValue)); 1111 | if (sizeof(ulong) == sizeof(UIntPtr)) 1112 | { 1113 | Assert.IsTrue((nuint)(ulong.MaxValue - 1) <= new UIntPtr(ulong.MaxValue)); 1114 | } 1115 | 1116 | Assert.IsFalse(new nuint(1) <= new UIntPtr(0)); 1117 | Assert.IsFalse(new nuint(2) <= new UIntPtr(1)); 1118 | Assert.IsFalse((nuint)(uint.MaxValue) <= new UIntPtr((ulong)(uint.MaxValue - 1))); 1119 | if (sizeof(ulong) == sizeof(UIntPtr)) 1120 | { 1121 | Assert.AreEqual(sizeof(ulong) != sizeof(UIntPtr), (nuint)(ulong.MaxValue) <= new UIntPtr((ulong.MaxValue - 1))); 1122 | } 1123 | 1124 | Assert.AreEqual(sizeof(ulong) != sizeof(UIntPtr), (nuint)(ulong.MaxValue) <= new UIntPtr((ulong)uint.MaxValue)); 1125 | Assert.AreEqual(sizeof(ulong) != sizeof(UIntPtr), (nuint)(ulong.MaxValue) <= new UIntPtr((ulong)uint.MaxValue)); 1126 | 1127 | // UIntPtr left, nuint right 1128 | Assert.IsTrue(new UIntPtr(0) <= new nuint(0)); 1129 | Assert.IsTrue(new UIntPtr(1) <= new nuint(1)); 1130 | Assert.IsTrue((ulong)uint.MaxValue <= new nuint(uint.MaxValue)); 1131 | Assert.AreEqual(sizeof(ulong) == sizeof(UIntPtr), ulong.MaxValue <= (nuint)(ulong.MaxValue)); 1132 | 1133 | Assert.IsTrue(new UIntPtr(0) <= new nuint(1)); 1134 | Assert.IsTrue(new UIntPtr(1) <= new nuint(2)); 1135 | Assert.IsTrue((ulong)(uint.MaxValue - 1) <= new nuint(uint.MaxValue)); 1136 | Assert.IsFalse((ulong.MaxValue - 1) <= new nuint(uint.MaxValue)); 1137 | 1138 | Assert.IsFalse(new UIntPtr(1) <= new nuint(0)); 1139 | Assert.IsFalse(new UIntPtr(2) <= new nuint(1)); 1140 | Assert.IsFalse((ulong)uint.MaxValue <= new nuint(uint.MaxValue - 1)); 1141 | Assert.IsFalse(ulong.MaxValue <= (nuint)(ulong.MaxValue - 1)); 1142 | 1143 | Assert.IsTrue((ulong)uint.MaxValue <= (nuint)(ulong.MaxValue)); 1144 | } 1145 | 1146 | [TestMethod] 1147 | public void Equals_nuint() 1148 | { 1149 | Assert.IsTrue(nuint.Zero.Equals(new nuint(0))); 1150 | Assert.IsTrue(new nuint(0).Equals(new nuint(0))); 1151 | Assert.IsTrue(new nuint(1).Equals(new nuint(1))); 1152 | Assert.IsTrue(new nuint(uint.MaxValue).Equals(new nuint(uint.MaxValue))); 1153 | Assert.IsTrue(((nuint)(ulong.MaxValue)).Equals((nuint)(ulong.MaxValue))); 1154 | 1155 | Assert.IsFalse(new nuint(0).Equals(new nuint(1))); 1156 | Assert.IsFalse(new nuint(1).Equals(new nuint(0))); 1157 | Assert.IsFalse(new nuint(1).Equals(new nuint(2))); 1158 | Assert.IsFalse(new nuint(2).Equals(new nuint(1))); 1159 | } 1160 | 1161 | [TestMethod] 1162 | public void CompareTo_nuint() 1163 | { 1164 | Assert.AreEqual(0, nuint.Zero.CompareTo(new nuint(0))); 1165 | Assert.AreEqual(0, new nuint(0).CompareTo(new nuint(0))); 1166 | Assert.AreEqual(0, new nuint(1).CompareTo(new nuint(1))); 1167 | Assert.AreEqual(0, new nuint(uint.MaxValue).CompareTo(new nuint(uint.MaxValue))); 1168 | Assert.AreEqual(0, ((nuint)(ulong.MaxValue)).CompareTo((nuint)(ulong.MaxValue))); 1169 | 1170 | Assert.AreEqual(-1, new nuint(0).CompareTo(new nuint(1))); 1171 | Assert.AreEqual(-1, new nuint(1).CompareTo(new nuint(2))); 1172 | 1173 | Assert.AreEqual(1, new nuint(1).CompareTo(new nuint(0))); 1174 | Assert.AreEqual(1, new nuint(2).CompareTo(new nuint(1))); 1175 | } 1176 | 1177 | [TestMethod] 1178 | public void Equals_object_nuint() 1179 | { 1180 | Assert.IsTrue(nuint.Zero.Equals((object)new nuint(0))); 1181 | Assert.IsTrue(new nuint(0).Equals((object)new nuint(0))); 1182 | Assert.IsTrue(new nuint(1).Equals((object)new nuint(1))); 1183 | Assert.IsTrue(new nuint(uint.MaxValue).Equals((object)new nuint(uint.MaxValue))); 1184 | Assert.IsTrue(((nuint)(ulong.MaxValue)).Equals((object)(nuint)(ulong.MaxValue))); 1185 | 1186 | Assert.IsFalse(new nuint(0).Equals((object)new nuint(1))); 1187 | Assert.IsFalse(new nuint(1).Equals((object)new nuint(0))); 1188 | Assert.IsFalse(new nuint(1).Equals((object)new nuint(2))); 1189 | Assert.IsFalse(new nuint(2).Equals((object)new nuint(1))); 1190 | Assert.IsFalse(new nuint(2).Equals((object)new nuint(1))); 1191 | 1192 | Assert.IsFalse(new nuint(0).Equals((object)null)); 1193 | Assert.IsFalse(new nuint(0).Equals((object)"")); 1194 | Assert.IsFalse(new nuint(0).Equals((object)"test")); 1195 | } 1196 | 1197 | [TestMethod] 1198 | public void GetHashCode_nuint() 1199 | { 1200 | Assert.AreEqual(0, new nuint(0).GetHashCode()); 1201 | Assert.AreEqual(1, new nuint(1).GetHashCode()); 1202 | Assert.AreEqual(2, new nuint(2).GetHashCode()); 1203 | Assert.AreEqual(3, new nuint(3).GetHashCode()); 1204 | Assert.AreEqual(4, new nuint(4).GetHashCode()); 1205 | Assert.AreEqual(5, new nuint(5).GetHashCode()); 1206 | Assert.AreEqual(int.MaxValue, new nuint(uint.MaxValue / 2).GetHashCode()); 1207 | Assert.AreEqual(int.MaxValue, new nuint(uint.MaxValue).GetHashCode()); 1208 | Assert.AreEqual(int.MaxValue, ((UIntPtr)(uint.MaxValue / 2)).GetHashCode()); 1209 | Assert.AreEqual(int.MaxValue, ((UIntPtr)uint.MaxValue).GetHashCode()); 1210 | 1211 | // UIntPtr overflows if 32-bit 1212 | //Assert.AreEqual(int.MaxValue, ((UIntPtr)ulong.MaxValue).GetHashCode()); 1213 | Assert.AreEqual(int.MaxValue, ((nuint)ulong.MaxValue).GetHashCode()); 1214 | Assert.AreEqual(0, ((UIntPtr)ulong.MinValue).GetHashCode()); 1215 | Assert.AreEqual(0, ((nuint)ulong.MinValue).GetHashCode()); 1216 | } 1217 | 1218 | // // UIntPtr hashcode impl 1219 | // public unsafe override int GetHashCode() 1220 | // { 1221 | //#if BIT64 1222 | // ulong l = (ulong)_value; 1223 | // return (unchecked((int)l) ^ (int)(l >> 32)); 1224 | //#else 1225 | // return unchecked((int)_value); 1226 | //#endif 1227 | // } 1228 | } 1229 | } -------------------------------------------------------------------------------- /src/DotNetCross.NativeInts.Tests/nintTest.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Diagnostics; 3 | using Microsoft.VisualStudio.TestTools.UnitTesting; 4 | 5 | namespace DotNetCross.NativeInts.TestsFramework 6 | { 7 | [TestClass] 8 | public class nintTest 9 | { 10 | [TestMethod] 11 | public unsafe void TraceSize() 12 | { 13 | Trace.WriteLine($"Sizeof {nameof(nint)}:{sizeof(nint)}"); 14 | Console.WriteLine($"Sizeof {nameof(nint)}:{sizeof(nint)}"); 15 | //Assert.AreEqual(8, sizeof(nint)); 16 | } 17 | 18 | [TestMethod] 19 | public void Zero() 20 | { 21 | Assert.AreEqual(nint.Zero.Value, IntPtr.Zero); 22 | } 23 | 24 | [TestMethod] 25 | public void ctor_IntPtr_0() 26 | { 27 | nint ni = new nint(IntPtr.Zero); 28 | Assert.AreEqual(ni.Value, IntPtr.Zero); 29 | } 30 | [TestMethod] 31 | public void ctor_IntPtr_1() 32 | { 33 | nint ni = new nint(new IntPtr(1)); 34 | Assert.AreEqual(ni.Value, new IntPtr(1)); 35 | } 36 | 37 | [TestMethod] 38 | public void ctor_int_0() 39 | { 40 | nint ni = new nint(0); 41 | Assert.AreEqual(ni.Value, new IntPtr(0)); 42 | } 43 | [TestMethod] 44 | public void ctor_int_1() 45 | { 46 | nint ni = new nint(1); 47 | Assert.AreEqual(ni.Value, new IntPtr(1)); 48 | } 49 | 50 | [TestMethod] 51 | public void ctor_long_0() 52 | { 53 | nint ni = new nint(0L); 54 | Assert.AreEqual(ni.Value, new IntPtr(0L)); 55 | } 56 | [TestMethod] 57 | public void ctor_long_1() 58 | { 59 | nint ni = new nint(1L); 60 | Assert.AreEqual(ni.Value, new IntPtr(1L)); 61 | } 62 | 63 | [TestMethod] 64 | public unsafe void ctor_long_MaxValue() 65 | { 66 | if (sizeof(nint) == sizeof(long)) 67 | { 68 | nint ni = new nint(long.MaxValue); 69 | Assert.AreEqual(ni.Value, new IntPtr(long.MaxValue)); 70 | } 71 | else 72 | { 73 | try 74 | { 75 | new nint(long.MaxValue); 76 | Assert.Fail("ctor for 64-bit checks overflow"); 77 | } 78 | catch (OverflowException e) 79 | { 80 | Assert.IsNotNull(e); 81 | } 82 | } 83 | } 84 | 85 | [TestMethod] 86 | public void implicit_conversion_from_IntPtr() 87 | { 88 | nint ni = new IntPtr(42); 89 | Assert.AreEqual(ni.Value, new IntPtr(42)); 90 | } 91 | [TestMethod] 92 | public void implicit_conversion_from_int() 93 | { 94 | nint ni = 42; 95 | Assert.AreEqual(ni.Value, new IntPtr(42)); 96 | } 97 | [TestMethod] 98 | public void explicit_conversion_from_long() 99 | { 100 | nint ni = (nint)42L; 101 | Assert.AreEqual(ni.Value, new IntPtr(42L)); 102 | } 103 | 104 | [TestMethod] 105 | public void implicit_conversion_to_IntPtr() 106 | { 107 | IntPtr ip = new nint(42); 108 | Assert.AreEqual(ip, new IntPtr(42)); 109 | } 110 | [TestMethod] 111 | public void implicit_conversion_to_long() 112 | { 113 | long l = new nint(42); 114 | Assert.AreEqual(l, 42L); 115 | } 116 | [TestMethod] 117 | public void explicit_conversion_to_int() 118 | { 119 | int i = (int)new nint(42); 120 | Assert.AreEqual(i, 42); 121 | } 122 | 123 | [TestMethod] 124 | public void explicit_conversion_to_byte_via_int() 125 | { 126 | Assert.AreEqual((byte)new nint(0), (byte)0); 127 | Assert.AreEqual((byte)new nint(42), (byte)42); 128 | Assert.AreEqual((byte)new nint(255), byte.MaxValue); 129 | Assert.AreEqual((byte)new nint(256), (byte)0); 130 | } 131 | 132 | [TestMethod] 133 | public void operator_Increment_Pre() 134 | { 135 | var ni = nint.Zero; 136 | ++ni; 137 | Assert.AreEqual(ni, new nint(1)); 138 | } 139 | [TestMethod] 140 | public void operator_Increment_Post() 141 | { 142 | var ni = nint.Zero; 143 | ni++; 144 | Assert.AreEqual(ni, new nint(1)); 145 | } 146 | 147 | [TestMethod] 148 | public void operator_Decrement_Pre() 149 | { 150 | var ni = nint.Zero; 151 | --ni; 152 | Assert.AreEqual(ni, new nint(-1)); 153 | } 154 | [TestMethod] 155 | public void operator_Decrement_Post() 156 | { 157 | var ni = nint.Zero; 158 | ni--; 159 | Assert.AreEqual(ni, new nint(-1)); 160 | } 161 | 162 | [TestMethod] 163 | public void operator_UnaryPlus() 164 | { 165 | var ni = new nint(1); 166 | Assert.AreEqual(+ni, new nint(1)); 167 | } 168 | [TestMethod] 169 | public void operator_UnaryNegate() 170 | { 171 | var ni = new nint(1); 172 | Assert.AreEqual(-ni, new nint(-1)); 173 | } 174 | 175 | [TestMethod] 176 | public void operator_OnesComplement() 177 | { 178 | Assert.AreEqual(~new nint(1), new nint(-2)); 179 | Assert.AreEqual(~new nint(0), new nint(-1)); 180 | Assert.AreEqual(~new nint(-1), new nint(0)); 181 | } 182 | 183 | [TestMethod] 184 | public void operator_Addition_nint() 185 | { 186 | var ni = new nint(1); 187 | Assert.AreEqual(ni + new nint(1), new nint(2)); 188 | Assert.AreEqual(new nint(1) + ni, new nint(2)); 189 | } 190 | [TestMethod] 191 | public void operator_Addition_IntPtr() 192 | { 193 | var ni = new nint(1); 194 | Assert.AreEqual(ni + new IntPtr(1), new nint(2)); 195 | Assert.AreEqual(new IntPtr(1) + ni, new nint(2)); 196 | } 197 | [TestMethod] 198 | public void operator_Addition_int() 199 | { 200 | var ni = new nint(1); 201 | Assert.AreEqual(ni + 1, new nint(2)); 202 | Assert.AreEqual(1 + ni, new nint(2)); 203 | } 204 | 205 | [TestMethod] 206 | public void operator_Subtraction_nint() 207 | { 208 | var ni = new nint(1); 209 | Assert.AreEqual(ni - new nint(1), new nint(0)); 210 | Assert.AreEqual(new nint(1) - ni, new nint(0)); 211 | } 212 | [TestMethod] 213 | public void operator_Subtraction_IntPtr() 214 | { 215 | var ni = new nint(1); 216 | Assert.AreEqual(ni - new IntPtr(1), new nint(0)); 217 | Assert.AreEqual(new IntPtr(1) - ni, new nint(0)); 218 | } 219 | [TestMethod] 220 | public void operator_Subtraction_int() 221 | { 222 | var ni = new nint(1); 223 | Assert.AreEqual(ni - 1, new nint(0)); 224 | Assert.AreEqual(1 - ni, new nint(0)); 225 | } 226 | 227 | [TestMethod] 228 | public void operator_Multiply_nint() 229 | { 230 | var ni = new nint(7); 231 | Assert.AreEqual(ni * new nint(2), new nint(14)); 232 | } 233 | [TestMethod] 234 | public void operator_Multiply_IntPtr() 235 | { 236 | var ni = new nint(7); 237 | Assert.AreEqual(ni * new IntPtr(2), new nint(14)); 238 | } 239 | [TestMethod] 240 | public void operator_Multiply_int() 241 | { 242 | var ni = new nint(7); 243 | Assert.AreEqual(ni * 2, new nint(14)); 244 | } 245 | 246 | [TestMethod] 247 | public void operator_Division_nint() 248 | { 249 | var ni = new nint(7); 250 | Assert.AreEqual(ni / new nint(2), new nint(3)); 251 | } 252 | [TestMethod] 253 | public void operator_Division_IntPtr() 254 | { 255 | var ni = new nint(7); 256 | Assert.AreEqual(ni / new IntPtr(2), new nint(3)); 257 | } 258 | [TestMethod] 259 | public void operator_Division_int() 260 | { 261 | var ni = new nint(7); 262 | Assert.AreEqual(ni / 2, new nint(3)); 263 | } 264 | 265 | [TestMethod] 266 | public void operator_Modulus_nint() 267 | { 268 | var ni = new nint(7); 269 | Assert.AreEqual(ni % new nint(4), new nint(3)); 270 | } 271 | [TestMethod] 272 | public void operator_Modulus_IntPtr() 273 | { 274 | var ni = new nint(7); 275 | Assert.AreEqual(ni % new IntPtr(4), new nint(3)); 276 | } 277 | [TestMethod] 278 | public void operator_Modulus_int() 279 | { 280 | var ni = new nint(7); 281 | Assert.AreEqual(ni % 4, new nint(3)); 282 | } 283 | 284 | [TestMethod] 285 | public void operator_ExclusiveOr_nint() 286 | { 287 | var ni = new nint(7); 288 | Assert.AreEqual(ni ^ new nint(4), new nint(3)); 289 | 290 | } 291 | [TestMethod] 292 | public void operator_ExclusiveOr_IntPtr() 293 | { 294 | var ni = new nint(7); 295 | Assert.AreEqual(ni ^ new IntPtr(4), new nint(3)); 296 | } 297 | [TestMethod] 298 | public void operator_ExclusiveOr_int() 299 | { 300 | var ni = new nint(7); 301 | Assert.AreEqual(ni ^ 4, new nint(3)); 302 | } 303 | 304 | [TestMethod] 305 | public void operator_BitwiseAnd_nint() 306 | { 307 | var ni = new nint(7); 308 | Assert.AreEqual(ni & new nint(12), new nint(4)); 309 | } 310 | [TestMethod] 311 | public void operator_BitwiseAnd_IntPtr() 312 | { 313 | var ni = new nint(7); 314 | Assert.AreEqual(ni & new IntPtr(12), new nint(4)); 315 | } 316 | [TestMethod] 317 | public void operator_BitwiseAnd_int() 318 | { 319 | var ni = new nint(7); 320 | Assert.AreEqual(ni & 12, new nint(4)); 321 | } 322 | 323 | [TestMethod] 324 | public void operator_BitwiseOr_nint() 325 | { 326 | var ni = new nint(7); 327 | Assert.AreEqual(ni | new nint(8), new nint(15)); 328 | } 329 | [TestMethod] 330 | public void operator_BitwiseOr_IntPtr() 331 | { 332 | var ni = new nint(7); 333 | Assert.AreEqual(ni | new IntPtr(8), new nint(15)); 334 | } 335 | [TestMethod] 336 | public void operator_BitwiseOr_int() 337 | { 338 | var ni = new nint(7); 339 | Assert.AreEqual(ni | 8, new nint(15)); 340 | } 341 | 342 | [TestMethod] 343 | public void operator_LeftShift_nint() 344 | { 345 | var ni = new nint(7); 346 | Assert.AreEqual(ni << new nint(2), new nint(28)); 347 | } 348 | [TestMethod] 349 | public void operator_LeftShift_IntPtr() 350 | { 351 | var ni = new nint(7); 352 | Assert.AreEqual(ni << new IntPtr(2), new nint(28)); 353 | } 354 | [TestMethod] 355 | public void operator_LeftShift_int() 356 | { 357 | var ni = new nint(7); 358 | Assert.AreEqual(ni << 2, new nint(28)); 359 | } 360 | 361 | [TestMethod] 362 | public void operator_RightShift_nint() 363 | { 364 | var ni = new nint(28); 365 | Assert.AreEqual(ni >> new nint(2), new nint(7)); 366 | } 367 | [TestMethod] 368 | public void operator_RightShift_IntPtr() 369 | { 370 | var ni = new nint(28); 371 | Assert.AreEqual(ni >> new IntPtr(2), new nint(7)); 372 | } 373 | [TestMethod] 374 | public void operator_RightShift_int() 375 | { 376 | var ni = new nint(28); 377 | Assert.AreEqual(ni >> 2, new nint(7)); 378 | } 379 | 380 | [TestMethod] 381 | public void operator_Equality_nint() 382 | { 383 | Assert.IsTrue(nint.Zero == new nint(0)); 384 | Assert.IsTrue(new nint(-1) == new nint(-1)); 385 | Assert.IsTrue(new nint(0) == new nint(0)); 386 | Assert.IsTrue(new nint(1) == new nint(1)); 387 | Assert.IsTrue(new nint(int.MaxValue) == new nint(int.MaxValue)); 388 | Assert.IsTrue((nint)(long.MaxValue) == (nint)(long.MaxValue)); 389 | 390 | Assert.IsFalse(new nint(-2) == new nint(-1)); 391 | Assert.IsFalse(new nint(-1) == new nint(-2)); 392 | Assert.IsFalse(new nint(-1) == new nint(0)); 393 | Assert.IsFalse(new nint(0) == new nint(-1)); 394 | Assert.IsFalse(new nint(0) == new nint(1)); 395 | Assert.IsFalse(new nint(1) == new nint(0)); 396 | Assert.IsFalse(new nint(1) == new nint(2)); 397 | Assert.IsFalse(new nint(2) == new nint(1)); 398 | } 399 | [TestMethod] 400 | public unsafe void operator_Equality_int() 401 | { 402 | Assert.IsTrue(nint.Zero == 0); 403 | Assert.IsTrue(new nint(-1) == -1); 404 | Assert.IsTrue(new nint(0) == 0); 405 | Assert.IsTrue(new nint(1) == 1); 406 | Assert.IsTrue(new nint(int.MaxValue) == int.MaxValue); 407 | Assert.AreEqual(sizeof(int) == sizeof(IntPtr), (nint)(long.MaxValue) == (unchecked((int)long.MaxValue))); 408 | Assert.IsFalse(new nint(-2) == -1); 409 | Assert.IsFalse(new nint(-1) == -2); 410 | Assert.IsFalse(new nint(-1) == 0); 411 | Assert.IsFalse(new nint(0) == -1); 412 | Assert.IsFalse(new nint(0) == 1); 413 | Assert.IsFalse(new nint(1) == 0); 414 | Assert.IsFalse(new nint(1) == 2); 415 | Assert.IsFalse(new nint(2) == 1); 416 | 417 | Assert.IsTrue(0 == nint.Zero); 418 | Assert.IsTrue(-1 == new nint(-1)); 419 | Assert.IsTrue(0 == new nint(0)); 420 | Assert.IsTrue(1 == new nint(1)); 421 | Assert.IsTrue(int.MaxValue == new nint(int.MaxValue)); 422 | Assert.AreEqual(sizeof(int) == sizeof(IntPtr), (unchecked((int)long.MaxValue) == (nint)(long.MaxValue))); 423 | Assert.IsFalse(-2 == new nint(-1)); 424 | Assert.IsFalse(-1 == new nint(-2)); 425 | Assert.IsFalse(-1 == new nint(0)); 426 | Assert.IsFalse(0 == new nint(-1)); 427 | Assert.IsFalse(0 == new nint(1)); 428 | Assert.IsFalse(1 == new nint(0)); 429 | Assert.IsFalse(1 == new nint(2)); 430 | Assert.IsFalse(2 == new nint(1)); 431 | } 432 | [TestMethod] 433 | public unsafe void operator_Equality_long() 434 | { 435 | Assert.IsTrue(nint.Zero == 0L); 436 | Assert.IsTrue(new nint(-1) == -1L); 437 | Assert.IsTrue(new nint(0) == 0L); 438 | Assert.IsTrue(new nint(1) == 1L); 439 | Assert.AreEqual(sizeof(int) != sizeof(IntPtr), (nint)(long.MaxValue) == long.MaxValue); 440 | Assert.IsFalse(new nint(-2) == -1L); 441 | Assert.IsFalse(new nint(-1) == -2L); 442 | Assert.IsFalse(new nint(-1) == 0L); 443 | Assert.IsFalse(new nint(0) == -1L); 444 | Assert.IsFalse(new nint(0) == 1L); 445 | Assert.IsFalse(new nint(1) == 0L); 446 | Assert.IsFalse(new nint(1) == 2L); 447 | Assert.IsFalse(new nint(2) == 1L); 448 | 449 | Assert.IsTrue(0L == nint.Zero); 450 | Assert.IsTrue(-1L == new nint(-1)); 451 | Assert.IsTrue(0L == new nint(0)); 452 | Assert.IsTrue(1L == new nint(1)); 453 | Assert.AreEqual(sizeof(int) != sizeof(IntPtr), long.MaxValue == (nint)(long.MaxValue)); 454 | Assert.IsFalse(-2L == new nint(-1)); 455 | Assert.IsFalse(-1L == new nint(-2)); 456 | Assert.IsFalse(-1L == new nint(0)); 457 | Assert.IsFalse(0L == new nint(-1)); 458 | Assert.IsFalse(0L == new nint(1)); 459 | Assert.IsFalse(1L == new nint(0)); 460 | Assert.IsFalse(1L == new nint(2)); 461 | Assert.IsFalse(2L == new nint(1)); 462 | } 463 | [TestMethod] 464 | public unsafe void operator_Equality_IntPtr() 465 | { 466 | Assert.IsTrue(nint.Zero == IntPtr.Zero); 467 | Assert.IsTrue(new nint(-1) == new IntPtr(-1)); 468 | Assert.IsTrue(new nint(0) == new IntPtr(0)); 469 | Assert.IsTrue(new nint(1) == new IntPtr(1)); 470 | Assert.IsTrue(new nint(int.MaxValue) == new IntPtr(int.MaxValue)); 471 | if (sizeof(long) == sizeof(IntPtr)) 472 | { 473 | // long -> IntPtr overflows if 32-bit, nint doesn't 474 | Assert.IsTrue((nint)(long.MaxValue) == new IntPtr(long.MaxValue)); 475 | } 476 | Assert.IsFalse(new nint(-2) == new IntPtr(-1)); 477 | Assert.IsFalse(new nint(-1) == new IntPtr(-2)); 478 | Assert.IsFalse(new nint(-1) == new IntPtr(0)); 479 | Assert.IsFalse(new nint(0) == new IntPtr(-1)); 480 | Assert.IsFalse(new nint(0) == new IntPtr(1)); 481 | Assert.IsFalse(new nint(1) == new IntPtr(0)); 482 | Assert.IsFalse(new nint(1) == new IntPtr(2)); 483 | Assert.IsFalse(new nint(2) == new IntPtr(1)); 484 | 485 | Assert.IsTrue(IntPtr.Zero == nint.Zero); 486 | Assert.IsTrue(new IntPtr(-1) == new nint(-1)); 487 | Assert.IsTrue(new IntPtr(0) == new nint(0)); 488 | Assert.IsTrue(new IntPtr(1) == new nint(1)); 489 | Assert.IsTrue(new IntPtr(int.MaxValue) == new nint(int.MaxValue)); 490 | if (sizeof(long) == sizeof(IntPtr)) 491 | { 492 | // long -> IntPtr overflows if 32-bit, nint doesn't 493 | Assert.IsTrue(new IntPtr(long.MaxValue) == (nint)(long.MaxValue)); 494 | } 495 | Assert.IsFalse(new IntPtr(-2) == new nint(-1)); 496 | Assert.IsFalse(new IntPtr(-1) == new nint(-2)); 497 | Assert.IsFalse(new IntPtr(-1) == new nint(0)); 498 | Assert.IsFalse(new IntPtr(0) == new nint(-1)); 499 | Assert.IsFalse(new IntPtr(0) == new nint(1)); 500 | Assert.IsFalse(new IntPtr(1) == new nint(0)); 501 | Assert.IsFalse(new IntPtr(1) == new nint(2)); 502 | Assert.IsFalse(new IntPtr(2) == new nint(1)); 503 | } 504 | 505 | [TestMethod] 506 | public void operator_Inequality_nint() 507 | { 508 | Assert.IsFalse(nint.Zero != new nint(0)); 509 | Assert.IsFalse(new nint(-1) != new nint(-1)); 510 | Assert.IsFalse(new nint(0) != new nint(0)); 511 | Assert.IsFalse(new nint(1) != new nint(1)); 512 | Assert.IsFalse(new nint(int.MaxValue) != new nint(int.MaxValue)); 513 | Assert.IsFalse((nint)(long.MaxValue) != (nint)(long.MaxValue)); 514 | 515 | Assert.IsTrue(new nint(-2) != new nint(-1)); 516 | Assert.IsTrue(new nint(-1) != new nint(-2)); 517 | Assert.IsTrue(new nint(-1) != new nint(0)); 518 | Assert.IsTrue(new nint(0) != new nint(-1)); 519 | Assert.IsTrue(new nint(0) != new nint(1)); 520 | Assert.IsTrue(new nint(1) != new nint(0)); 521 | Assert.IsTrue(new nint(1) != new nint(2)); 522 | Assert.IsTrue(new nint(2) != new nint(1)); 523 | } 524 | [TestMethod] 525 | public unsafe void operator_Inequality_int() 526 | { 527 | Assert.IsFalse(nint.Zero != 0); 528 | Assert.IsFalse(new nint(-1) != -1); 529 | Assert.IsFalse(new nint(0) != 0); 530 | Assert.IsFalse(new nint(1) != 1); 531 | Assert.IsFalse(new nint(int.MaxValue) != int.MaxValue); 532 | Assert.AreEqual(sizeof(int) != sizeof(IntPtr), (nint)(long.MaxValue) != (unchecked((int)long.MaxValue))); 533 | Assert.IsTrue(new nint(-2) != -1); 534 | Assert.IsTrue(new nint(-1) != -2); 535 | Assert.IsTrue(new nint(-1) != 0); 536 | Assert.IsTrue(new nint(0) != -1); 537 | Assert.IsTrue(new nint(0) != 1); 538 | Assert.IsTrue(new nint(1) != 0); 539 | Assert.IsTrue(new nint(1) != 2); 540 | Assert.IsTrue(new nint(2) != 1); 541 | 542 | Assert.IsFalse(0 != nint.Zero); 543 | Assert.IsFalse(-1 != new nint(-1)); 544 | Assert.IsFalse(0 != new nint(0)); 545 | Assert.IsFalse(1 != new nint(1)); 546 | Assert.IsFalse(int.MaxValue != new nint(int.MaxValue)); 547 | Assert.AreEqual(sizeof(int) != sizeof(IntPtr), (unchecked((int)long.MaxValue) != (nint)(long.MaxValue))); 548 | Assert.IsTrue(-2 != new nint(-1)); 549 | Assert.IsTrue(-1 != new nint(-2)); 550 | Assert.IsTrue(-1 != new nint(0)); 551 | Assert.IsTrue(0 != new nint(-1)); 552 | Assert.IsTrue(0 != new nint(1)); 553 | Assert.IsTrue(1 != new nint(0)); 554 | Assert.IsTrue(1 != new nint(2)); 555 | Assert.IsTrue(2 != new nint(1)); 556 | } 557 | [TestMethod] 558 | public unsafe void operator_Inequality_long() 559 | { 560 | Assert.IsFalse(nint.Zero != 0L); 561 | Assert.IsFalse(new nint(-1) != -1L); 562 | Assert.IsFalse(new nint(0) != 0L); 563 | Assert.IsFalse(new nint(1) != 1L); 564 | Assert.AreEqual(sizeof(int) == sizeof(IntPtr), (nint)(long.MaxValue) != long.MaxValue); 565 | Assert.IsTrue(new nint(-2) != -1L); 566 | Assert.IsTrue(new nint(-1) != -2L); 567 | Assert.IsTrue(new nint(-1) != 0L); 568 | Assert.IsTrue(new nint(0) != -1L); 569 | Assert.IsTrue(new nint(0) != 1L); 570 | Assert.IsTrue(new nint(1) != 0L); 571 | Assert.IsTrue(new nint(1) != 2L); 572 | Assert.IsTrue(new nint(2) != 1L); 573 | 574 | Assert.IsFalse(0L != nint.Zero); 575 | Assert.IsFalse(-1L != new nint(-1)); 576 | Assert.IsFalse(0L != new nint(0)); 577 | Assert.IsFalse(1L != new nint(1)); 578 | Assert.AreEqual(sizeof(int) == sizeof(IntPtr), long.MaxValue != (nint)(long.MaxValue)); 579 | Assert.IsTrue(-2L != new nint(-1)); 580 | Assert.IsTrue(-1L != new nint(-2)); 581 | Assert.IsTrue(-1L != new nint(0)); 582 | Assert.IsTrue(0L != new nint(-1)); 583 | Assert.IsTrue(0L != new nint(1)); 584 | Assert.IsTrue(1L != new nint(0)); 585 | Assert.IsTrue(1L != new nint(2)); 586 | Assert.IsTrue(2L != new nint(1)); 587 | } 588 | [TestMethod] 589 | public unsafe void operator_Inequality_IntPtr() 590 | { 591 | Assert.IsFalse(nint.Zero != IntPtr.Zero); 592 | Assert.IsFalse(new nint(-1) != new IntPtr(-1)); 593 | Assert.IsFalse(new nint(0) != new IntPtr(0)); 594 | Assert.IsFalse(new nint(1) != new IntPtr(1)); 595 | Assert.IsFalse(new nint(int.MaxValue) != new IntPtr(int.MaxValue)); 596 | if (sizeof(long) == sizeof(IntPtr)) 597 | { 598 | // long -> IntPtr overflows if 32-bit, nint doesn't 599 | Assert.IsFalse((nint)(long.MaxValue) != new IntPtr(long.MaxValue)); 600 | } 601 | Assert.IsTrue(new nint(-2) != new IntPtr(-1)); 602 | Assert.IsTrue(new nint(-1) != new IntPtr(-2)); 603 | Assert.IsTrue(new nint(-1) != new IntPtr(0)); 604 | Assert.IsTrue(new nint(0) != new IntPtr(-1)); 605 | Assert.IsTrue(new nint(0) != new IntPtr(1)); 606 | Assert.IsTrue(new nint(1) != new IntPtr(0)); 607 | Assert.IsTrue(new nint(1) != new IntPtr(2)); 608 | Assert.IsTrue(new nint(2) != new IntPtr(1)); 609 | 610 | Assert.IsFalse(IntPtr.Zero != nint.Zero); 611 | Assert.IsFalse(new IntPtr(-1) != new nint(-1)); 612 | Assert.IsFalse(new IntPtr(0) != new nint(0)); 613 | Assert.IsFalse(new IntPtr(1) != new nint(1)); 614 | Assert.IsFalse(new IntPtr(int.MaxValue) != new nint(int.MaxValue)); 615 | if (sizeof(long) == sizeof(IntPtr)) 616 | { 617 | // long -> IntPtr overflows if 32-bit, nint doesn't 618 | Assert.IsFalse(new IntPtr(long.MaxValue) != (nint)(long.MaxValue)); 619 | } 620 | Assert.IsTrue(new IntPtr(-2) != new nint(-1)); 621 | Assert.IsTrue(new IntPtr(-1) != new nint(-2)); 622 | Assert.IsTrue(new IntPtr(-1) != new nint(0)); 623 | Assert.IsTrue(new IntPtr(0) != new nint(-1)); 624 | Assert.IsTrue(new IntPtr(0) != new nint(1)); 625 | Assert.IsTrue(new IntPtr(1) != new nint(0)); 626 | Assert.IsTrue(new IntPtr(1) != new nint(2)); 627 | Assert.IsTrue(new IntPtr(2) != new nint(1)); 628 | } 629 | 630 | 631 | [TestMethod] 632 | public void operator_GreaterThan_nint() 633 | { 634 | Assert.IsFalse(nint.Zero > new nint(0)); 635 | Assert.IsFalse(new nint(-1) > new nint(-1)); 636 | Assert.IsFalse(new nint(0) > new nint(0)); 637 | Assert.IsFalse(new nint(1) > new nint(1)); 638 | Assert.IsFalse(new nint(int.MaxValue) > new nint(int.MaxValue)); 639 | Assert.IsFalse((nint)(long.MaxValue) > (nint)(long.MaxValue)); 640 | 641 | Assert.IsFalse(new nint(-2) > new nint(-1)); 642 | Assert.IsFalse(new nint(-1) > new nint(0)); 643 | Assert.IsFalse(new nint(0) > new nint(1)); 644 | Assert.IsFalse(new nint(1) > new nint(2)); 645 | Assert.IsFalse((nint)(int.MaxValue - 1) > (nint)(int.MaxValue)); 646 | 647 | Assert.IsTrue(new nint(-1) > new nint(-2)); 648 | Assert.IsTrue(new nint(0) > new nint(-1)); 649 | Assert.IsTrue(new nint(1) > new nint(0)); 650 | Assert.IsTrue(new nint(2) > new nint(1)); 651 | Assert.IsTrue((nint)(int.MaxValue) > (nint)(int.MaxValue - 1)); 652 | } 653 | [TestMethod] 654 | public unsafe void operator_GreaterThan_int() 655 | { 656 | // nint left, int right 657 | Assert.IsFalse(nint.Zero > 0); 658 | Assert.IsFalse(new nint(-1) > -1); 659 | Assert.IsFalse(new nint(0) > 0); 660 | Assert.IsFalse(new nint(1) > 1); 661 | Assert.IsFalse(new nint(int.MaxValue) > int.MaxValue); 662 | 663 | Assert.IsFalse(new nint(-2) > -1); 664 | Assert.IsFalse(new nint(-1) > 0); 665 | Assert.IsFalse(new nint(0) > 1); 666 | Assert.IsFalse(new nint(1) > 2); 667 | Assert.IsFalse((nint)(int.MaxValue - 1) > int.MaxValue); 668 | 669 | Assert.IsTrue(new nint(-1) > -2); 670 | Assert.IsTrue(new nint(0) > -1); 671 | Assert.IsTrue(new nint(1) > 0); 672 | Assert.IsTrue(new nint(2) > 1); 673 | Assert.IsTrue((nint)(int.MaxValue) > int.MaxValue - 1); 674 | 675 | Assert.AreEqual(sizeof(long) == sizeof(IntPtr), (nint)(long.MaxValue) > int.MaxValue); 676 | Assert.AreEqual(sizeof(long) == sizeof(IntPtr), (nint)(long.MaxValue) > int.MaxValue); 677 | 678 | // int left, nint right 679 | Assert.IsFalse(0 > new nint(0)); 680 | Assert.IsFalse(-1 > new nint(-1)); 681 | Assert.IsFalse(0 > new nint(0)); 682 | Assert.IsFalse(1 > new nint(1)); 683 | Assert.IsFalse(int.MaxValue > new nint(int.MaxValue)); 684 | 685 | Assert.IsFalse(-2 > new nint(-1)); 686 | Assert.IsFalse(-1 > new nint(0)); 687 | Assert.IsFalse(0 > new nint(1)); 688 | Assert.IsFalse(1 > new nint(2)); 689 | Assert.IsFalse((int.MaxValue - 1) > new nint(int.MaxValue)); 690 | 691 | Assert.IsTrue(-1 > new nint(-2)); 692 | Assert.IsTrue(0 > new nint(-1)); 693 | Assert.IsTrue(1 > new nint(0)); 694 | Assert.IsTrue(2 > new nint(1)); 695 | Assert.IsTrue(int.MaxValue > new nint(int.MaxValue - 1)); 696 | 697 | Assert.AreEqual(sizeof(long) != sizeof(IntPtr), int.MaxValue > (nint)(long.MaxValue)); 698 | Assert.AreEqual(sizeof(long) != sizeof(IntPtr), int.MaxValue > (nint)(long.MaxValue)); 699 | } 700 | [TestMethod] 701 | public unsafe void operator_GreaterThan_long() 702 | { 703 | // nint left, long right 704 | Assert.IsFalse(nint.Zero > 0L); 705 | Assert.IsFalse(new nint(-1) > -1L); 706 | Assert.IsFalse(new nint(0) > 0L); 707 | Assert.IsFalse(new nint(1) > 1L); 708 | Assert.IsFalse(new nint(int.MaxValue) > (long)int.MaxValue); 709 | Assert.IsFalse((nint)(long.MaxValue) > long.MaxValue); 710 | 711 | Assert.IsFalse(new nint(-2) > -1L); 712 | Assert.IsFalse(new nint(-1) > 0L); 713 | Assert.IsFalse(new nint(0) > 1L); 714 | Assert.IsFalse(new nint(1) > 2L); 715 | Assert.IsFalse((nint)(int.MaxValue - 1) > (long)int.MaxValue); 716 | Assert.IsFalse((nint)(long.MaxValue - 1) > long.MaxValue); 717 | 718 | Assert.IsTrue(new nint(-1) > -2L); 719 | Assert.IsTrue(new nint(0) > -1L); 720 | Assert.IsTrue(new nint(1) > 0L); 721 | Assert.IsTrue(new nint(2) > 1L); 722 | Assert.IsTrue((nint)(int.MaxValue) > (long)(int.MaxValue - 1)); 723 | Assert.AreEqual(sizeof(long) == sizeof(IntPtr), (nint)(long.MaxValue) > (long.MaxValue - 1)); 724 | 725 | Assert.AreEqual(sizeof(long) == sizeof(IntPtr), (nint)(long.MaxValue) > (long)int.MaxValue); 726 | Assert.AreEqual(sizeof(long) == sizeof(IntPtr), (nint)(long.MaxValue) > (long)int.MaxValue); 727 | 728 | // long left, nint right 729 | Assert.IsFalse(0L > new nint(0)); 730 | Assert.IsFalse(-1L > new nint(-1)); 731 | Assert.IsFalse(0L > new nint(0)); 732 | Assert.IsFalse(1L > new nint(1)); 733 | Assert.IsFalse((long)int.MaxValue > new nint(int.MaxValue)); 734 | Assert.AreEqual(sizeof(long) != sizeof(IntPtr), long.MaxValue > (nint)(long.MaxValue)); 735 | 736 | Assert.IsFalse(-2L > new nint(-1)); 737 | Assert.IsFalse(-1L > new nint(0)); 738 | Assert.IsFalse(0L > new nint(1)); 739 | Assert.IsFalse(1L > new nint(2)); 740 | Assert.IsFalse((long)(int.MaxValue - 1) > new nint(int.MaxValue)); 741 | Assert.IsTrue(((long)int.MaxValue + 1L) > new nint(int.MaxValue)); 742 | 743 | Assert.IsTrue(-1L > new nint(-2)); 744 | Assert.IsTrue(0L > new nint(-1)); 745 | Assert.IsTrue(1L > new nint(0)); 746 | Assert.IsTrue(2L > new nint(1)); 747 | Assert.IsTrue((long)int.MaxValue > new nint(int.MaxValue - 1)); 748 | Assert.IsTrue(long.MaxValue > (nint)(long.MaxValue - 1)); 749 | 750 | Assert.AreEqual(sizeof(long) != sizeof(IntPtr), (long)int.MaxValue > (nint)(long.MaxValue)); 751 | Assert.AreEqual(sizeof(long) != sizeof(IntPtr), (long)int.MaxValue > (nint)(long.MaxValue)); 752 | } 753 | [TestMethod] 754 | public unsafe void operator_GreaterThan_IntPtr() 755 | { 756 | // nint left, IntPtr right 757 | Assert.IsFalse(nint.Zero > new IntPtr(0L)); 758 | Assert.IsFalse(new nint(-1) > new IntPtr(-1)); 759 | Assert.IsFalse(new nint(0) > new IntPtr(0)); 760 | Assert.IsFalse(new nint(1) > new IntPtr(1)); 761 | Assert.IsFalse(new nint(int.MaxValue) > new IntPtr((long)int.MaxValue)); 762 | if (sizeof(long) == sizeof(IntPtr)) 763 | { 764 | Assert.IsFalse((nint)(long.MaxValue) > new IntPtr(long.MaxValue)); 765 | } 766 | 767 | Assert.IsFalse(new nint(-2) > new IntPtr(-1)); 768 | Assert.IsFalse(new nint(-1) > new IntPtr(0)); 769 | Assert.IsFalse(new nint(0) > new IntPtr(1)); 770 | Assert.IsFalse(new nint(1) > new IntPtr(2)); 771 | Assert.IsFalse((nint)(int.MaxValue - 1) > new IntPtr((long)int.MaxValue)); 772 | if (sizeof(long) == sizeof(IntPtr)) 773 | { 774 | Assert.IsFalse((nint)(long.MaxValue - 1) > new IntPtr(long.MaxValue)); 775 | } 776 | 777 | Assert.IsTrue(new nint(-1) > new IntPtr(-2)); 778 | Assert.IsTrue(new nint(0) > new IntPtr(-1)); 779 | Assert.IsTrue(new nint(1) > new IntPtr(0)); 780 | Assert.IsTrue(new nint(2) > new IntPtr(1)); 781 | Assert.IsTrue((nint)(int.MaxValue) > new IntPtr((long)(int.MaxValue - 1))); 782 | if (sizeof(long) == sizeof(IntPtr)) 783 | { 784 | Assert.AreEqual(sizeof(long) == sizeof(IntPtr), (nint)(long.MaxValue) > new IntPtr((long.MaxValue - 1))); 785 | } 786 | 787 | Assert.AreEqual(sizeof(long) == sizeof(IntPtr), (nint)(long.MaxValue) > new IntPtr((long)int.MaxValue)); 788 | Assert.AreEqual(sizeof(long) == sizeof(IntPtr), (nint)(long.MaxValue) > new IntPtr((long)int.MaxValue)); 789 | 790 | // IntPtr left, nint right 791 | Assert.IsFalse(new IntPtr(0) > new nint(0)); 792 | Assert.IsFalse(new IntPtr(-1) > new nint(-1)); 793 | Assert.IsFalse(new IntPtr(0) > new nint(0)); 794 | Assert.IsFalse(new IntPtr(1) > new nint(1)); 795 | Assert.IsFalse((long)int.MaxValue > new nint(int.MaxValue)); 796 | Assert.AreEqual(sizeof(long) != sizeof(IntPtr), long.MaxValue > (nint)(long.MaxValue)); 797 | 798 | Assert.IsFalse(new IntPtr(-2) > new nint(-1)); 799 | Assert.IsFalse(new IntPtr(-1) > new nint(0)); 800 | Assert.IsFalse(new IntPtr(0) > new nint(1)); 801 | Assert.IsFalse(new IntPtr(1) > new nint(2)); 802 | Assert.IsFalse((long)(int.MaxValue - 1) > new nint(int.MaxValue)); 803 | Assert.IsTrue((int.MaxValue + 1L) > new nint(int.MaxValue)); 804 | 805 | Assert.IsTrue(new IntPtr(-1) > new nint(-2)); 806 | Assert.IsTrue(new IntPtr(0) > new nint(-1)); 807 | Assert.IsTrue(new IntPtr(1) > new nint(0)); 808 | Assert.IsTrue(new IntPtr(2) > new nint(1)); 809 | Assert.IsTrue((long)int.MaxValue > new nint(int.MaxValue - 1)); 810 | Assert.IsTrue(long.MaxValue > (nint)(long.MaxValue - 1)); 811 | 812 | Assert.AreEqual(sizeof(long) != sizeof(IntPtr), (long)int.MaxValue > (nint)(long.MaxValue)); 813 | Assert.AreEqual(sizeof(long) != sizeof(IntPtr), (long)int.MaxValue > (nint)(long.MaxValue)); 814 | } 815 | 816 | 817 | [TestMethod] 818 | public void operator_GreaterThanOrEqual_nint() 819 | { 820 | Assert.IsTrue(nint.Zero >= new nint(0)); 821 | Assert.IsTrue(new nint(-1) >= new nint(-1)); 822 | Assert.IsTrue(new nint(0) >= new nint(0)); 823 | Assert.IsTrue(new nint(1) >= new nint(1)); 824 | Assert.IsTrue(new nint(int.MaxValue) >= new nint(int.MaxValue)); 825 | Assert.IsTrue((nint)(long.MaxValue) >= (nint)(long.MaxValue)); 826 | 827 | Assert.IsFalse(new nint(-2) >= new nint(-1)); 828 | Assert.IsFalse(new nint(-1) >= new nint(0)); 829 | Assert.IsFalse(new nint(0) >= new nint(1)); 830 | Assert.IsFalse(new nint(1) >= new nint(2)); 831 | Assert.IsFalse((nint)(int.MaxValue - 1) >= (nint)(int.MaxValue)); 832 | 833 | Assert.IsTrue(new nint(-1) >= new nint(-2)); 834 | Assert.IsTrue(new nint(0) >= new nint(-1)); 835 | Assert.IsTrue(new nint(1) >= new nint(0)); 836 | Assert.IsTrue(new nint(2) >= new nint(1)); 837 | Assert.IsTrue((nint)(int.MaxValue) >= (nint)(int.MaxValue - 1)); 838 | } 839 | [TestMethod] 840 | public unsafe void operator_GreaterThanOrEqual_int() 841 | { 842 | // nint left, int right 843 | Assert.IsTrue(nint.Zero >= 0); 844 | Assert.IsTrue(new nint(-1) >= -1); 845 | Assert.IsTrue(new nint(0) >= 0); 846 | Assert.IsTrue(new nint(1) >= 1); 847 | Assert.IsTrue(new nint(int.MaxValue) >= int.MaxValue); 848 | 849 | Assert.IsFalse(new nint(-2) >= -1); 850 | Assert.IsFalse(new nint(-1) >= 0); 851 | Assert.IsFalse(new nint(0) >= 1); 852 | Assert.IsFalse(new nint(1) >= 2); 853 | Assert.IsFalse((nint)(int.MaxValue - 1) >= int.MaxValue); 854 | 855 | Assert.IsTrue(new nint(-1) >= -2); 856 | Assert.IsTrue(new nint(0) >= -1); 857 | Assert.IsTrue(new nint(1) >= 0); 858 | Assert.IsTrue(new nint(2) >= 1); 859 | Assert.IsTrue((nint)(int.MaxValue) >= int.MaxValue - 1); 860 | 861 | Assert.AreEqual(sizeof(long) == sizeof(IntPtr), (nint)(long.MaxValue) >= int.MaxValue); 862 | Assert.AreEqual(sizeof(long) == sizeof(IntPtr), (nint)(long.MaxValue) >= int.MaxValue); 863 | 864 | // int left, nint right 865 | Assert.IsTrue(0 >= new nint(0)); 866 | Assert.IsTrue(-1 >= new nint(-1)); 867 | Assert.IsTrue(0 >= new nint(0)); 868 | Assert.IsTrue(1 >= new nint(1)); 869 | Assert.IsTrue(int.MaxValue >= new nint(int.MaxValue)); 870 | 871 | Assert.IsFalse(-2 >= new nint(-1)); 872 | Assert.IsFalse(-1 >= new nint(0)); 873 | Assert.IsFalse(0 >= new nint(1)); 874 | Assert.IsFalse(1 >= new nint(2)); 875 | Assert.IsFalse((int.MaxValue - 1) >= new nint(int.MaxValue)); 876 | 877 | Assert.IsTrue(-1 >= new nint(-2)); 878 | Assert.IsTrue(0 >= new nint(-1)); 879 | Assert.IsTrue(1 >= new nint(0)); 880 | Assert.IsTrue(2 >= new nint(1)); 881 | Assert.IsTrue(int.MaxValue >= new nint(int.MaxValue - 1)); 882 | 883 | Assert.AreEqual(sizeof(long) != sizeof(IntPtr), int.MaxValue >= (nint)(long.MaxValue)); 884 | Assert.AreEqual(sizeof(long) != sizeof(IntPtr), int.MaxValue >= (nint)(long.MaxValue)); 885 | } 886 | [TestMethod] 887 | public unsafe void operator_GreaterThanOrEqual_long() 888 | { 889 | // nint left, long right 890 | Assert.IsTrue(nint.Zero >= 0L); 891 | Assert.IsTrue(new nint(-1) >= -1L); 892 | Assert.IsTrue(new nint(0) >= 0L); 893 | Assert.IsTrue(new nint(1) >= 1L); 894 | Assert.IsTrue(new nint(int.MaxValue) >= (long)int.MaxValue); 895 | Assert.AreEqual(sizeof(long) == sizeof(IntPtr), (nint)(long.MaxValue) >= long.MaxValue); 896 | 897 | Assert.IsFalse(new nint(-2) >= -1L); 898 | Assert.IsFalse(new nint(-1) >= 0L); 899 | Assert.IsFalse(new nint(0) >= 1L); 900 | Assert.IsFalse(new nint(1) >= 2L); 901 | Assert.IsFalse((nint)(int.MaxValue - 1) >= (long)int.MaxValue); 902 | Assert.IsFalse((nint)(long.MaxValue - 1) >= long.MaxValue); 903 | 904 | Assert.IsTrue(new nint(-1) >= -2L); 905 | Assert.IsTrue(new nint(0) >= -1L); 906 | Assert.IsTrue(new nint(1) >= 0L); 907 | Assert.IsTrue(new nint(2) >= 1L); 908 | Assert.IsTrue((nint)(int.MaxValue) >= (long)(int.MaxValue - 1)); 909 | Assert.AreEqual(sizeof(long) == sizeof(IntPtr), (nint)(long.MaxValue) >= (long.MaxValue - 1)); 910 | 911 | Assert.AreEqual(sizeof(long) == sizeof(IntPtr), (nint)(long.MaxValue) >= (long)int.MaxValue); 912 | Assert.AreEqual(sizeof(long) == sizeof(IntPtr), (nint)(long.MaxValue) >= (long)int.MaxValue); 913 | 914 | // long left, nint right 915 | Assert.IsTrue(0L >= new nint(0)); 916 | Assert.IsTrue(-1L >= new nint(-1)); 917 | Assert.IsTrue(0L >= new nint(0)); 918 | Assert.IsTrue(1L >= new nint(1)); 919 | Assert.IsTrue((long)int.MaxValue >= new nint(int.MaxValue)); 920 | Assert.IsTrue(long.MaxValue >= (nint)(long.MaxValue)); 921 | 922 | Assert.IsFalse(-2L >= new nint(-1)); 923 | Assert.IsFalse(-1L >= new nint(0)); 924 | Assert.IsFalse(0L >= new nint(1)); 925 | Assert.IsFalse(1L >= new nint(2)); 926 | Assert.IsFalse((long)(int.MaxValue - 1) >= new nint(int.MaxValue)); 927 | Assert.IsTrue((long.MaxValue - 1) >= new nint(int.MaxValue)); 928 | 929 | Assert.IsTrue(-1L >= new nint(-2)); 930 | Assert.IsTrue(0L >= new nint(-1)); 931 | Assert.IsTrue(1L >= new nint(0)); 932 | Assert.IsTrue(2L >= new nint(1)); 933 | Assert.IsTrue((long)int.MaxValue >= new nint(int.MaxValue - 1)); 934 | Assert.IsTrue(long.MaxValue >= (nint)(long.MaxValue - 1)); 935 | 936 | Assert.AreEqual(sizeof(long) != sizeof(IntPtr), (long)int.MaxValue >= (nint)(long.MaxValue)); 937 | Assert.AreEqual(sizeof(long) != sizeof(IntPtr), (long)int.MaxValue >= (nint)(long.MaxValue)); 938 | } 939 | [TestMethod] 940 | public unsafe void operator_GreaterThanOrEqual_IntPtr() 941 | { 942 | // nint left, IntPtr right 943 | Assert.IsTrue(nint.Zero >= new IntPtr(0L)); 944 | Assert.IsTrue(new nint(-1) >= new IntPtr(-1)); 945 | Assert.IsTrue(new nint(0) >= new IntPtr(0)); 946 | Assert.IsTrue(new nint(1) >= new IntPtr(1)); 947 | Assert.IsTrue(new nint(int.MaxValue) >= new IntPtr((long)int.MaxValue)); 948 | if (sizeof(long) == sizeof(IntPtr)) 949 | { 950 | Assert.IsTrue((nint)(long.MaxValue) >= new IntPtr(long.MaxValue)); 951 | } 952 | Assert.IsFalse(new nint(-2) >= new IntPtr(-1)); 953 | Assert.IsFalse(new nint(-1) >= new IntPtr(0)); 954 | Assert.IsFalse(new nint(0) >= new IntPtr(1)); 955 | Assert.IsFalse(new nint(1) >= new IntPtr(2)); 956 | Assert.IsFalse((nint)(int.MaxValue - 1) >= new IntPtr((long)int.MaxValue)); 957 | if (sizeof(long) == sizeof(IntPtr)) 958 | { 959 | Assert.IsFalse((nint)(long.MaxValue - 1) >= new IntPtr(long.MaxValue)); 960 | } 961 | 962 | Assert.IsTrue(new nint(-1) >= new IntPtr(-2)); 963 | Assert.IsTrue(new nint(0) >= new IntPtr(-1)); 964 | Assert.IsTrue(new nint(1) >= new IntPtr(0)); 965 | Assert.IsTrue(new nint(2) >= new IntPtr(1)); 966 | Assert.IsTrue((nint)(int.MaxValue) >= new IntPtr((long)(int.MaxValue - 1))); 967 | if (sizeof(long) == sizeof(IntPtr)) 968 | { 969 | Assert.AreEqual(sizeof(long) == sizeof(IntPtr), (nint)(long.MaxValue) >= new IntPtr((long.MaxValue - 1))); 970 | } 971 | 972 | Assert.AreEqual(sizeof(long) == sizeof(IntPtr), (nint)(long.MaxValue) >= new IntPtr((long)int.MaxValue)); 973 | Assert.AreEqual(sizeof(long) == sizeof(IntPtr), (nint)(long.MaxValue) >= new IntPtr((long)int.MaxValue)); 974 | 975 | // IntPtr left, nint right 976 | Assert.IsTrue(new IntPtr(0) >= new nint(0)); 977 | Assert.IsTrue(new IntPtr(-1) >= new nint(-1)); 978 | Assert.IsTrue(new IntPtr(0) >= new nint(0)); 979 | Assert.IsTrue(new IntPtr(1) >= new nint(1)); 980 | Assert.IsTrue((long)int.MaxValue >= new nint(int.MaxValue)); 981 | Assert.IsTrue(long.MaxValue >= (nint)(long.MaxValue)); 982 | 983 | Assert.IsFalse(new IntPtr(-2) >= new nint(-1)); 984 | Assert.IsFalse(new IntPtr(-1) >= new nint(0)); 985 | Assert.IsFalse(new IntPtr(0) >= new nint(1)); 986 | Assert.IsFalse(new IntPtr(1) >= new nint(2)); 987 | Assert.IsFalse((long)(int.MaxValue - 1) >= new nint(int.MaxValue)); 988 | Assert.IsTrue((long.MaxValue - 1) >= new nint(int.MaxValue)); 989 | 990 | Assert.IsTrue(new IntPtr(-1) >= new nint(-2)); 991 | Assert.IsTrue(new IntPtr(0) >= new nint(-1)); 992 | Assert.IsTrue(new IntPtr(1) >= new nint(0)); 993 | Assert.IsTrue(new IntPtr(2) >= new nint(1)); 994 | Assert.IsTrue((long)int.MaxValue >= new nint(int.MaxValue - 1)); 995 | Assert.IsTrue(long.MaxValue >= (nint)(long.MaxValue - 1)); 996 | 997 | Assert.AreEqual(sizeof(long) != sizeof(IntPtr), (long)int.MaxValue >= (nint)(long.MaxValue)); 998 | Assert.AreEqual(sizeof(long) != sizeof(IntPtr), (long)int.MaxValue >= (nint)(long.MaxValue)); 999 | } 1000 | 1001 | 1002 | [TestMethod] 1003 | public void operator_LessThan_nint() 1004 | { 1005 | Assert.IsFalse(nint.Zero < new nint(0)); 1006 | Assert.IsFalse(new nint(-1) < new nint(-1)); 1007 | Assert.IsFalse(new nint(0) < new nint(0)); 1008 | Assert.IsFalse(new nint(1) < new nint(1)); 1009 | Assert.IsFalse(new nint(int.MaxValue) < new nint(int.MaxValue)); 1010 | Assert.IsFalse((nint)(long.MaxValue) < (nint)(long.MaxValue)); 1011 | 1012 | Assert.IsTrue(new nint(-2) < new nint(-1)); 1013 | Assert.IsTrue(new nint(-1) < new nint(0)); 1014 | Assert.IsTrue(new nint(0) < new nint(1)); 1015 | Assert.IsTrue(new nint(1) < new nint(2)); 1016 | Assert.IsTrue((nint)(int.MaxValue - 1) < (nint)(int.MaxValue)); 1017 | 1018 | Assert.IsFalse(new nint(-1) < new nint(-2)); 1019 | Assert.IsFalse(new nint(0) < new nint(-1)); 1020 | Assert.IsFalse(new nint(1) < new nint(0)); 1021 | Assert.IsFalse(new nint(2) < new nint(1)); 1022 | Assert.IsFalse((nint)(int.MaxValue) < (nint)(int.MaxValue - 1)); 1023 | } 1024 | [TestMethod] 1025 | public unsafe void operator_LessThan_int() 1026 | { 1027 | // nint left, int right 1028 | Assert.IsFalse(nint.Zero < 0); 1029 | Assert.IsFalse(new nint(-1) < -1); 1030 | Assert.IsFalse(new nint(0) < 0); 1031 | Assert.IsFalse(new nint(1) < 1); 1032 | Assert.IsFalse(new nint(int.MaxValue) < int.MaxValue); 1033 | 1034 | Assert.IsTrue(new nint(-2) < -1); 1035 | Assert.IsTrue(new nint(-1) < 0); 1036 | Assert.IsTrue(new nint(0) < 1); 1037 | Assert.IsTrue(new nint(1) < 2); 1038 | Assert.IsTrue((nint)(int.MaxValue - 1) < int.MaxValue); 1039 | 1040 | Assert.IsFalse(new nint(-1) < -2); 1041 | Assert.IsFalse(new nint(0) < -1); 1042 | Assert.IsFalse(new nint(1) < 0); 1043 | Assert.IsFalse(new nint(2) < 1); 1044 | Assert.IsFalse((nint)(int.MaxValue) < int.MaxValue - 1); 1045 | 1046 | Assert.AreEqual(sizeof(long) != sizeof(IntPtr), (nint)(long.MaxValue) < int.MaxValue); 1047 | Assert.AreEqual(sizeof(long) != sizeof(IntPtr), (nint)(long.MaxValue) < int.MaxValue); 1048 | 1049 | // int left, nint right 1050 | Assert.IsFalse(0 < new nint(0)); 1051 | Assert.IsFalse(-1 < new nint(-1)); 1052 | Assert.IsFalse(0 < new nint(0)); 1053 | Assert.IsFalse(1 < new nint(1)); 1054 | Assert.IsFalse(int.MaxValue < new nint(int.MaxValue)); 1055 | 1056 | Assert.IsTrue(-2 < new nint(-1)); 1057 | Assert.IsTrue(-1 < new nint(0)); 1058 | Assert.IsTrue(0 < new nint(1)); 1059 | Assert.IsTrue(1 < new nint(2)); 1060 | Assert.IsTrue((int.MaxValue - 1) < new nint(int.MaxValue)); 1061 | 1062 | Assert.IsFalse(-1 < new nint(-2)); 1063 | Assert.IsFalse(0 < new nint(-1)); 1064 | Assert.IsFalse(1 < new nint(0)); 1065 | Assert.IsFalse(2 < new nint(1)); 1066 | Assert.IsFalse(int.MaxValue < new nint(int.MaxValue - 1)); 1067 | 1068 | Assert.AreEqual(sizeof(long) == sizeof(IntPtr), int.MaxValue < (nint)(long.MaxValue)); 1069 | Assert.AreEqual(sizeof(long) == sizeof(IntPtr), int.MaxValue < (nint)(long.MaxValue)); 1070 | } 1071 | [TestMethod] 1072 | public unsafe void operator_LessThan_long() 1073 | { 1074 | // nint left, long right 1075 | Assert.IsFalse(nint.Zero < 0L); 1076 | Assert.IsFalse(new nint(-1) < -1L); 1077 | Assert.IsFalse(new nint(0) < 0L); 1078 | Assert.IsFalse(new nint(1) < 1L); 1079 | Assert.IsFalse(new nint(int.MaxValue) < (long)int.MaxValue); 1080 | Assert.AreEqual(sizeof(long) != sizeof(IntPtr), (nint)(long.MaxValue) < long.MaxValue); 1081 | 1082 | Assert.IsTrue(new nint(-2) < -1L); 1083 | Assert.IsTrue(new nint(-1) < 0L); 1084 | Assert.IsTrue(new nint(0) < 1L); 1085 | Assert.IsTrue(new nint(1) < 2L); 1086 | Assert.IsTrue((nint)(int.MaxValue - 1) < (long)int.MaxValue); 1087 | Assert.IsTrue((nint)(long.MaxValue - 1) < long.MaxValue); 1088 | 1089 | Assert.IsFalse(new nint(-1) < -2L); 1090 | Assert.IsFalse(new nint(0) < -1L); 1091 | Assert.IsFalse(new nint(1) < 0L); 1092 | Assert.IsFalse(new nint(2) < 1L); 1093 | Assert.IsFalse((nint)(int.MaxValue) < (long)(int.MaxValue - 1)); 1094 | Assert.AreEqual(sizeof(long) != sizeof(IntPtr), (nint)(long.MaxValue) < (long.MaxValue - 1)); 1095 | 1096 | Assert.AreEqual(sizeof(long) != sizeof(IntPtr), (nint)(long.MaxValue) < (long)int.MaxValue); 1097 | Assert.AreEqual(sizeof(long) != sizeof(IntPtr), (nint)(long.MaxValue) < (long)int.MaxValue); 1098 | 1099 | // long left, nint right 1100 | Assert.IsFalse(0L < new nint(0)); 1101 | Assert.IsFalse(-1L < new nint(-1)); 1102 | Assert.IsFalse(0L < new nint(0)); 1103 | Assert.IsFalse(1L < new nint(1)); 1104 | Assert.IsFalse((long)int.MaxValue < new nint(int.MaxValue)); 1105 | Assert.IsFalse(long.MaxValue < (nint)(long.MaxValue)); 1106 | 1107 | Assert.IsTrue(-2L < new nint(-1)); 1108 | Assert.IsTrue(-1L < new nint(0)); 1109 | Assert.IsTrue(0L < new nint(1)); 1110 | Assert.IsTrue(1L < new nint(2)); 1111 | Assert.IsTrue((long)(int.MaxValue - 1) < new nint(int.MaxValue)); 1112 | Assert.IsFalse((long.MaxValue - 1) < new nint(int.MaxValue)); 1113 | 1114 | Assert.IsFalse(-1L < new nint(-2)); 1115 | Assert.IsFalse(0L < new nint(-1)); 1116 | Assert.IsFalse(1L < new nint(0)); 1117 | Assert.IsFalse(2L < new nint(1)); 1118 | Assert.IsFalse((long)int.MaxValue < new nint(int.MaxValue - 1)); 1119 | Assert.IsFalse(long.MaxValue < (nint)(long.MaxValue - 1)); 1120 | 1121 | Assert.AreEqual(sizeof(long) == sizeof(IntPtr), (long)int.MaxValue < (nint)(long.MaxValue)); 1122 | } 1123 | [TestMethod] 1124 | public unsafe void operator_LessThan_IntPtr() 1125 | { 1126 | // nint left, IntPtr right 1127 | Assert.IsFalse(nint.Zero < new IntPtr(0)); 1128 | Assert.IsFalse(new nint(-1) < new IntPtr(-1)); 1129 | Assert.IsFalse(new nint(0) < new IntPtr(0)); 1130 | Assert.IsFalse(new nint(1) < new IntPtr(1)); 1131 | Assert.IsFalse(new nint(int.MaxValue) < new IntPtr((long)int.MaxValue)); 1132 | if (sizeof(long) == sizeof(IntPtr)) 1133 | { 1134 | Assert.IsFalse((nint)(long.MaxValue) < new IntPtr(long.MaxValue)); 1135 | } 1136 | 1137 | Assert.IsTrue(new nint(-2) < new IntPtr(-1)); 1138 | Assert.IsTrue(new nint(-1) < new IntPtr(0)); 1139 | Assert.IsTrue(new nint(0) < new IntPtr(1)); 1140 | Assert.IsTrue(new nint(1) < new IntPtr(2)); 1141 | Assert.IsTrue((nint)(int.MaxValue - 1) < new IntPtr((long)int.MaxValue)); 1142 | if (sizeof(long) == sizeof(IntPtr)) 1143 | { 1144 | Assert.IsTrue((nint)(long.MaxValue - 1) < new IntPtr(long.MaxValue)); 1145 | } 1146 | 1147 | Assert.IsFalse(new nint(-1) < new IntPtr(-2)); 1148 | Assert.IsFalse(new nint(0) < new IntPtr(-1)); 1149 | Assert.IsFalse(new nint(1) < new IntPtr(0)); 1150 | Assert.IsFalse(new nint(2) < new IntPtr(1)); 1151 | Assert.IsFalse((nint)(int.MaxValue) < new IntPtr((long)(int.MaxValue - 1))); 1152 | if (sizeof(long) == sizeof(IntPtr)) 1153 | { 1154 | Assert.IsFalse((nint)(long.MaxValue) < new IntPtr((long.MaxValue - 1))); 1155 | } 1156 | 1157 | Assert.AreEqual(sizeof(long) != sizeof(IntPtr), (nint)(long.MaxValue) < new IntPtr((long)int.MaxValue)); 1158 | Assert.AreEqual(sizeof(long) != sizeof(IntPtr), (nint)(long.MaxValue) < new IntPtr((long)int.MaxValue)); 1159 | 1160 | // IntPtr left, nint right 1161 | Assert.IsFalse(new IntPtr(0) < new nint(0)); 1162 | Assert.IsFalse(new IntPtr(-1) < new nint(-1)); 1163 | Assert.IsFalse(new IntPtr(0) < new nint(0)); 1164 | Assert.IsFalse(new IntPtr(1) < new nint(1)); 1165 | Assert.IsFalse((long)int.MaxValue < new nint(int.MaxValue)); 1166 | Assert.IsFalse(long.MaxValue < (nint)(long.MaxValue)); 1167 | 1168 | Assert.IsTrue(new IntPtr(-2) < new nint(-1)); 1169 | Assert.IsTrue(new IntPtr(-1) < new nint(0)); 1170 | Assert.IsTrue(new IntPtr(0) < new nint(1)); 1171 | Assert.IsTrue(new IntPtr(1) < new nint(2)); 1172 | Assert.IsTrue((long)(int.MaxValue - 1) < new nint(int.MaxValue)); 1173 | Assert.IsFalse((long.MaxValue - 1) < new nint(int.MaxValue)); 1174 | 1175 | Assert.IsFalse(new IntPtr(-1) < new nint(-2)); 1176 | Assert.IsFalse(new IntPtr(0) < new nint(-1)); 1177 | Assert.IsFalse(new IntPtr(1) < new nint(0)); 1178 | Assert.IsFalse(new IntPtr(2) < new nint(1)); 1179 | Assert.IsFalse((long)int.MaxValue < new nint(int.MaxValue - 1)); 1180 | Assert.IsFalse(long.MaxValue < (nint)(long.MaxValue - 1)); 1181 | 1182 | Assert.AreEqual(sizeof(long) == sizeof(IntPtr), (long)int.MaxValue < (nint)(long.MaxValue)); 1183 | } 1184 | 1185 | 1186 | [TestMethod] 1187 | public void operator_LessThanOrEqual_nint() 1188 | { 1189 | Assert.IsTrue(nint.Zero <= new nint(0)); 1190 | Assert.IsTrue(new nint(-1) <= new nint(-1)); 1191 | Assert.IsTrue(new nint(0) <= new nint(0)); 1192 | Assert.IsTrue(new nint(1) <= new nint(1)); 1193 | Assert.IsTrue(new nint(int.MaxValue) <= new nint(int.MaxValue)); 1194 | Assert.IsTrue((nint)(long.MaxValue) <= (nint)(long.MaxValue)); 1195 | 1196 | Assert.IsTrue(new nint(-2) <= new nint(-1)); 1197 | Assert.IsTrue(new nint(-1) <= new nint(0)); 1198 | Assert.IsTrue(new nint(0) <= new nint(1)); 1199 | Assert.IsTrue(new nint(1) <= new nint(2)); 1200 | Assert.IsTrue((nint)(int.MaxValue - 1) <= (nint)(int.MaxValue)); 1201 | 1202 | Assert.IsFalse(new nint(-1) <= new nint(-2)); 1203 | Assert.IsFalse(new nint(0) <= new nint(-1)); 1204 | Assert.IsFalse(new nint(1) <= new nint(0)); 1205 | Assert.IsFalse(new nint(2) <= new nint(1)); 1206 | Assert.IsFalse((nint)(int.MaxValue) <= (nint)(int.MaxValue - 1)); 1207 | } 1208 | [TestMethod] 1209 | public unsafe void operator_LessThanOrEqual_int() 1210 | { 1211 | // nint left, int right 1212 | Assert.IsTrue(nint.Zero <= 0); 1213 | Assert.IsTrue(new nint(-1) <= -1); 1214 | Assert.IsTrue(new nint(0) <= 0); 1215 | Assert.IsTrue(new nint(1) <= 1); 1216 | Assert.IsTrue(new nint(int.MaxValue) <= int.MaxValue); 1217 | 1218 | Assert.IsTrue(new nint(-2) <= -1); 1219 | Assert.IsTrue(new nint(-1) <= 0); 1220 | Assert.IsTrue(new nint(0) <= 1); 1221 | Assert.IsTrue(new nint(1) <= 2); 1222 | Assert.IsTrue((nint)(int.MaxValue - 1) <= int.MaxValue); 1223 | 1224 | Assert.IsFalse(new nint(-1) <= -2); 1225 | Assert.IsFalse(new nint(0) <= -1); 1226 | Assert.IsFalse(new nint(1) <= 0); 1227 | Assert.IsFalse(new nint(2) <= 1); 1228 | Assert.IsFalse((nint)(int.MaxValue) <= int.MaxValue - 1); 1229 | 1230 | Assert.AreEqual(sizeof(long) != sizeof(IntPtr), (nint)(long.MaxValue) <= int.MaxValue); 1231 | Assert.AreEqual(sizeof(long) != sizeof(IntPtr), (nint)(long.MaxValue) <= int.MaxValue); 1232 | 1233 | // int left, nint right 1234 | Assert.IsTrue(0 <= new nint(0)); 1235 | Assert.IsTrue(-1 <= new nint(-1)); 1236 | Assert.IsTrue(0 <= new nint(0)); 1237 | Assert.IsTrue(1 <= new nint(1)); 1238 | Assert.IsTrue(int.MaxValue <= new nint(int.MaxValue)); 1239 | 1240 | Assert.IsTrue(-2 <= new nint(-1)); 1241 | Assert.IsTrue(-1 <= new nint(0)); 1242 | Assert.IsTrue(0 <= new nint(1)); 1243 | Assert.IsTrue(1 <= new nint(2)); 1244 | Assert.IsTrue((int.MaxValue - 1) <= new nint(int.MaxValue)); 1245 | 1246 | Assert.IsFalse(-1 <= new nint(-2)); 1247 | Assert.IsFalse(0 <= new nint(-1)); 1248 | Assert.IsFalse(1 <= new nint(0)); 1249 | Assert.IsFalse(2 <= new nint(1)); 1250 | Assert.IsFalse(int.MaxValue <= new nint(int.MaxValue - 1)); 1251 | 1252 | Assert.AreEqual(sizeof(long) == sizeof(IntPtr), int.MaxValue <= (nint)(long.MaxValue)); 1253 | Assert.AreEqual(sizeof(long) == sizeof(IntPtr), int.MaxValue <= (nint)(long.MaxValue)); 1254 | } 1255 | [TestMethod] 1256 | public unsafe void operator_LessThanOrEqual_long() 1257 | { 1258 | // nint left, long right 1259 | Assert.IsTrue(nint.Zero <= 0L); 1260 | Assert.IsTrue(new nint(-1) <= -1L); 1261 | Assert.IsTrue(new nint(0) <= 0L); 1262 | Assert.IsTrue(new nint(1) <= 1L); 1263 | Assert.IsTrue(new nint(int.MaxValue) <= (long)int.MaxValue); 1264 | Assert.IsTrue((nint)(long.MaxValue) <= long.MaxValue); 1265 | 1266 | Assert.IsTrue(new nint(-2) <= -1L); 1267 | Assert.IsTrue(new nint(-1) <= 0L); 1268 | Assert.IsTrue(new nint(0) <= 1L); 1269 | Assert.IsTrue(new nint(1) <= 2L); 1270 | Assert.IsTrue((nint)(int.MaxValue - 1) <= (long)int.MaxValue); 1271 | Assert.IsTrue((nint)(long.MaxValue - 1) <= long.MaxValue); 1272 | 1273 | Assert.IsFalse(new nint(-1) <= -2L); 1274 | Assert.IsFalse(new nint(0) <= -1L); 1275 | Assert.IsFalse(new nint(1) <= 0L); 1276 | Assert.IsFalse(new nint(2) <= 1L); 1277 | Assert.IsFalse((nint)(int.MaxValue) <= (long)(int.MaxValue - 1)); 1278 | Assert.AreEqual(sizeof(long) != sizeof(IntPtr), (nint)(long.MaxValue) <= (long.MaxValue - 1)); 1279 | 1280 | Assert.AreEqual(sizeof(long) != sizeof(IntPtr), (nint)(long.MaxValue) <= (long)int.MaxValue); 1281 | Assert.AreEqual(sizeof(long) != sizeof(IntPtr), (nint)(long.MaxValue) <= (long)int.MaxValue); 1282 | 1283 | // long left, nint right 1284 | Assert.IsTrue(0L <= new nint(0)); 1285 | Assert.IsTrue(-1L <= new nint(-1)); 1286 | Assert.IsTrue(0L <= new nint(0)); 1287 | Assert.IsTrue(1L <= new nint(1)); 1288 | Assert.IsTrue((long)int.MaxValue <= new nint(int.MaxValue)); 1289 | Assert.AreEqual(sizeof(long) == sizeof(IntPtr), long.MaxValue <= (nint)(long.MaxValue)); 1290 | 1291 | Assert.IsTrue(-2L <= new nint(-1)); 1292 | Assert.IsTrue(-1L <= new nint(0)); 1293 | Assert.IsTrue(0L <= new nint(1)); 1294 | Assert.IsTrue(1L <= new nint(2)); 1295 | Assert.IsTrue((long)(int.MaxValue - 1) <= new nint(int.MaxValue)); 1296 | Assert.IsFalse((long.MaxValue - 1) <= new nint(int.MaxValue)); 1297 | 1298 | Assert.IsFalse(-1L <= new nint(-2)); 1299 | Assert.IsFalse(0L <= new nint(-1)); 1300 | Assert.IsFalse(1L <= new nint(0)); 1301 | Assert.IsFalse(2L <= new nint(1)); 1302 | Assert.IsFalse((long)int.MaxValue <= new nint(int.MaxValue - 1)); 1303 | Assert.IsFalse(long.MaxValue <= (nint)(long.MaxValue - 1)); 1304 | 1305 | Assert.AreEqual(sizeof(long) == sizeof(IntPtr), (long)int.MaxValue <= (nint)(long.MaxValue)); 1306 | } 1307 | [TestMethod] 1308 | public unsafe void operator_LessThanOrEqual_IntPtr() 1309 | { 1310 | // nint left, IntPtr right 1311 | Assert.IsTrue(nint.Zero <= new IntPtr(0)); 1312 | Assert.IsTrue(new nint(-1) <= new IntPtr(-1)); 1313 | Assert.IsTrue(new nint(0) <= new IntPtr(0)); 1314 | Assert.IsTrue(new nint(1) <= new IntPtr(1)); 1315 | Assert.IsTrue(new nint(int.MaxValue) <= new IntPtr((long)int.MaxValue)); 1316 | if (sizeof(long) == sizeof(IntPtr)) 1317 | { 1318 | Assert.IsTrue((nint)(long.MaxValue) <= new IntPtr(long.MaxValue)); 1319 | } 1320 | 1321 | Assert.IsTrue(new nint(-2) <= new IntPtr(-1)); 1322 | Assert.IsTrue(new nint(-1) <= new IntPtr(0)); 1323 | Assert.IsTrue(new nint(0) <= new IntPtr(1)); 1324 | Assert.IsTrue(new nint(1) <= new IntPtr(2)); 1325 | Assert.IsTrue((nint)(int.MaxValue - 1) <= new IntPtr((long)int.MaxValue)); 1326 | if (sizeof(long) == sizeof(IntPtr)) 1327 | { 1328 | Assert.IsTrue((nint)(long.MaxValue - 1) <= new IntPtr(long.MaxValue)); 1329 | } 1330 | 1331 | Assert.IsFalse(new nint(-1) <= new IntPtr(-2)); 1332 | Assert.IsFalse(new nint(0) <= new IntPtr(-1)); 1333 | Assert.IsFalse(new nint(1) <= new IntPtr(0)); 1334 | Assert.IsFalse(new nint(2) <= new IntPtr(1)); 1335 | Assert.IsFalse((nint)(int.MaxValue) <= new IntPtr((long)(int.MaxValue - 1))); 1336 | if (sizeof(long) == sizeof(IntPtr)) 1337 | { 1338 | Assert.IsFalse((nint)(long.MaxValue) <= new IntPtr((long.MaxValue - 1))); 1339 | } 1340 | 1341 | Assert.AreEqual(sizeof(long) != sizeof(IntPtr), (nint)(long.MaxValue) <= new IntPtr((long)int.MaxValue)); 1342 | Assert.AreEqual(sizeof(long) != sizeof(IntPtr), (nint)(long.MaxValue) <= new IntPtr((long)int.MaxValue)); 1343 | 1344 | // IntPtr left, nint right 1345 | Assert.IsTrue(new IntPtr(0) <= new nint(0)); 1346 | Assert.IsTrue(new IntPtr(-1) <= new nint(-1)); 1347 | Assert.IsTrue(new IntPtr(0) <= new nint(0)); 1348 | Assert.IsTrue(new IntPtr(1) <= new nint(1)); 1349 | Assert.IsTrue((long)int.MaxValue <= new nint(int.MaxValue)); 1350 | Assert.AreEqual(sizeof(long) == sizeof(IntPtr), long.MaxValue <= (nint)(long.MaxValue)); 1351 | 1352 | Assert.IsTrue(new IntPtr(-2) <= new nint(-1)); 1353 | Assert.IsTrue(new IntPtr(-1) <= new nint(0)); 1354 | Assert.IsTrue(new IntPtr(0) <= new nint(1)); 1355 | Assert.IsTrue(new IntPtr(1) <= new nint(2)); 1356 | Assert.IsTrue((long)(int.MaxValue - 1) <= new nint(int.MaxValue)); 1357 | Assert.IsFalse((long.MaxValue - 1) <= new nint(int.MaxValue)); 1358 | 1359 | Assert.IsFalse(new IntPtr(-1) <= new nint(-2)); 1360 | Assert.IsFalse(new IntPtr(0) <= new nint(-1)); 1361 | Assert.IsFalse(new IntPtr(1) <= new nint(0)); 1362 | Assert.IsFalse(new IntPtr(2) <= new nint(1)); 1363 | Assert.IsFalse((long)int.MaxValue <= new nint(int.MaxValue - 1)); 1364 | Assert.IsFalse(long.MaxValue <= (nint)(long.MaxValue - 1)); 1365 | 1366 | Assert.AreEqual(sizeof(long) == sizeof(IntPtr), (long)int.MaxValue <= (nint)(long.MaxValue)); 1367 | } 1368 | 1369 | [TestMethod] 1370 | public void Equals_nint() 1371 | { 1372 | Assert.IsTrue(nint.Zero.Equals(new nint(0))); 1373 | Assert.IsTrue(new nint(-1).Equals(new nint(-1))); 1374 | Assert.IsTrue(new nint(0).Equals(new nint(0))); 1375 | Assert.IsTrue(new nint(1).Equals(new nint(1))); 1376 | Assert.IsTrue(new nint(int.MaxValue).Equals(new nint(int.MaxValue))); 1377 | Assert.IsTrue(((nint)(long.MaxValue)).Equals((nint)(long.MaxValue))); 1378 | 1379 | Assert.IsFalse(new nint(-2).Equals(new nint(-1))); 1380 | Assert.IsFalse(new nint(-1).Equals(new nint(-2))); 1381 | Assert.IsFalse(new nint(-1).Equals(new nint(0))); 1382 | Assert.IsFalse(new nint(0).Equals(new nint(-1))); 1383 | Assert.IsFalse(new nint(0).Equals(new nint(1))); 1384 | Assert.IsFalse(new nint(1).Equals(new nint(0))); 1385 | Assert.IsFalse(new nint(1).Equals(new nint(2))); 1386 | Assert.IsFalse(new nint(2).Equals(new nint(1))); 1387 | } 1388 | 1389 | [TestMethod] 1390 | public void CompareTo_nint() 1391 | { 1392 | Assert.AreEqual(0, nint.Zero.CompareTo(new nint(0))); 1393 | Assert.AreEqual(0, new nint(-1).CompareTo(new nint(-1))); 1394 | Assert.AreEqual(0, new nint(0).CompareTo(new nint(0))); 1395 | Assert.AreEqual(0, new nint(1).CompareTo(new nint(1))); 1396 | Assert.AreEqual(0, new nint(int.MaxValue).CompareTo(new nint(int.MaxValue))); 1397 | Assert.AreEqual(0, ((nint)(long.MaxValue)).CompareTo((nint)(long.MaxValue))); 1398 | 1399 | Assert.AreEqual(-1,new nint(-2).CompareTo(new nint(-1))); 1400 | Assert.AreEqual(-1,new nint(-1).CompareTo(new nint(0))); 1401 | Assert.AreEqual(-1,new nint(0).CompareTo(new nint(1))); 1402 | Assert.AreEqual(-1,new nint(1).CompareTo(new nint(2))); 1403 | 1404 | Assert.AreEqual(1, new nint(-1).CompareTo(new nint(-2))); 1405 | Assert.AreEqual(1, new nint(0).CompareTo(new nint(-1))); 1406 | Assert.AreEqual(1, new nint(1).CompareTo(new nint(0))); 1407 | Assert.AreEqual(1,new nint(2).CompareTo(new nint(1))); 1408 | } 1409 | 1410 | [TestMethod] 1411 | public void Equals_object_nint() 1412 | { 1413 | Assert.IsTrue(nint.Zero.Equals((object)new nint(0))); 1414 | Assert.IsTrue(new nint(-1).Equals((object)new nint(-1))); 1415 | Assert.IsTrue(new nint(0).Equals((object)new nint(0))); 1416 | Assert.IsTrue(new nint(1).Equals((object)new nint(1))); 1417 | Assert.IsTrue(new nint(int.MaxValue).Equals((object)new nint(int.MaxValue))); 1418 | Assert.IsTrue(((nint)(long.MaxValue)).Equals((object)(nint)(long.MaxValue))); 1419 | 1420 | Assert.IsFalse(new nint(-2).Equals((object)new nint(-1))); 1421 | Assert.IsFalse(new nint(-1).Equals((object)new nint(-2))); 1422 | Assert.IsFalse(new nint(-1).Equals((object)new nint(0))); 1423 | Assert.IsFalse(new nint(0).Equals((object)new nint(-1))); 1424 | Assert.IsFalse(new nint(0).Equals((object)new nint(1))); 1425 | Assert.IsFalse(new nint(1).Equals((object)new nint(0))); 1426 | Assert.IsFalse(new nint(1).Equals((object)new nint(2))); 1427 | Assert.IsFalse(new nint(2).Equals((object)new nint(1))); 1428 | Assert.IsFalse(new nint(2).Equals((object)new nint(1))); 1429 | 1430 | Assert.IsFalse(new nint(0).Equals((object)null)); 1431 | Assert.IsFalse(new nint(0).Equals((object)"")); 1432 | Assert.IsFalse(new nint(0).Equals((object)"test")); 1433 | } 1434 | 1435 | [TestMethod] 1436 | public void GetHashCode_nint() 1437 | { 1438 | Assert.AreEqual(-5, new nint(-5).GetHashCode()); 1439 | Assert.AreEqual(-4, new nint(-4).GetHashCode()); 1440 | Assert.AreEqual(-3, new nint(-3).GetHashCode()); 1441 | Assert.AreEqual(-2, new nint(-2).GetHashCode()); 1442 | Assert.AreEqual(-1, new nint(-1).GetHashCode()); 1443 | Assert.AreEqual(0, new nint(0).GetHashCode()); 1444 | Assert.AreEqual(1, new nint(1).GetHashCode()); 1445 | Assert.AreEqual(2, new nint(2).GetHashCode()); 1446 | Assert.AreEqual(3, new nint(3).GetHashCode()); 1447 | Assert.AreEqual(4, new nint(4).GetHashCode()); 1448 | Assert.AreEqual(5, new nint(5).GetHashCode()); 1449 | 1450 | Assert.AreEqual(int.MaxValue, ((IntPtr)int.MaxValue).GetHashCode()); 1451 | Assert.AreEqual(int.MaxValue, ((nint)int.MaxValue).GetHashCode()); 1452 | Assert.AreEqual(int.MinValue, ((IntPtr)int.MinValue).GetHashCode()); 1453 | Assert.AreEqual(int.MinValue, ((nint)int.MinValue).GetHashCode()); 1454 | 1455 | //Assert.AreEqual(-1, ((IntPtr)long.MaxValue).GetHashCode()); 1456 | Assert.AreEqual(-1, ((nint)long.MaxValue).GetHashCode()); 1457 | //Assert.AreEqual(0, ((IntPtr)long.MinValue).GetHashCode()); 1458 | Assert.AreEqual(0, ((nint)long.MinValue).GetHashCode()); 1459 | } 1460 | } 1461 | } 1462 | --------------------------------------------------------------------------------