Skip to content

Commit

Permalink
Fix issue 47 with enumerating certain high value flag values (#48)
Browse files Browse the repository at this point in the history
* Fix issue 47 with enumerating certain high value flag values

* Target .NET 7 for test
  • Loading branch information
TylerBrinkley authored Jan 17, 2024
1 parent a35664d commit 6ef93d0
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 13 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,6 @@ jobs:
- name: Test .NET 4.5
run: dotnet test -f net45 --no-restore --verbosity normal
working-directory: ./Src
- name: Test .NET Core 3.0
run: dotnet test -f netcoreapp3.0 --no-restore --verbosity normal
- name: Test .NET 7
run: dotnet test -f net7 --no-restore --verbosity normal
working-directory: ./Src
6 changes: 3 additions & 3 deletions Src/Enums.NET.Test/Enums.NET.Test.csproj
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFrameworks>netcoreapp3.0;netcoreapp2.0;netcoreapp1.1;netcoreapp1.0;net46;net45</TargetFrameworks>
<TargetFrameworks>net7;netcoreapp2.0;netcoreapp1.1;netcoreapp1.0;net46;net45</TargetFrameworks>
<RootNamespace>EnumsNET.Tests</RootNamespace>
<LangVersion>latest</LangVersion>
</PropertyGroup>
Expand All @@ -26,7 +26,7 @@
<Reference Include="..\SpecialtyEnums\netstandard1.0\SpecialtyEnums.dll" />
</ItemGroup>

<ItemGroup Condition="'$(TargetFramework)' == 'netcoreapp3.0' OR '$(TargetFramework)' == 'netcoreapp2.0'">
<ItemGroup Condition="'$(TargetFramework)' == 'net7' OR '$(TargetFramework)' == 'netcoreapp2.0'">
<Reference Include="..\SpecialtyEnums\netcoreapp2.0\SpecialtyEnums.dll" />
</ItemGroup>

Expand All @@ -38,7 +38,7 @@
<AssemblyTitle>Enums.NET Test .NET 4.5</AssemblyTitle>
<DefineConstants>DISPLAY_ATTRIBUTE;ICONVERTIBLE;GET_TYPE_CODE;TYPE_REFLECTION</DefineConstants>
</PropertyGroup>
<PropertyGroup Condition="'$(TargetFramework)' == 'netcoreapp3.0'">
<PropertyGroup Condition="'$(TargetFramework)' == 'net7'">
<AssemblyTitle>Enums.NET Test .NET Core App 3.0</AssemblyTitle>
<DefineConstants>NETSTANDARD;DISPLAY_ATTRIBUTE;ICONVERTIBLE;GET_TYPE_CODE;TYPE_REFLECTION;BENCHMARKS;SPAN</DefineConstants>
</PropertyGroup>
Expand Down
34 changes: 34 additions & 0 deletions Src/Enums.NET.Test/Issues/Issue47.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
using System;
using NUnit.Framework;

namespace EnumsNET.Tests.Issues
{
[TestFixture]
public class Issue47
{
[Test]
public void AsString_SuccessfullyReturnsValue_WhenUsingLargeFlagEnumValue()
{
Assert.AreEqual("Val1, Val30", (MyEnum.Val1 | MyEnum.Val30).AsString());
}

#if SPAN
[Test]
public void TryFormat_SuccessfullyReturnsValue_WhenUsingLargeFlagEnumValue()
{
var destination = new char[20];
Assert.True((MyEnum.Val1 | MyEnum.Val30).TryFormat(destination, out var charsWritten));
Assert.AreEqual(11, charsWritten);
Assert.AreEqual("Val1, Val30", new string(destination[..charsWritten]));
}
#endif

[Flags]
public enum MyEnum
{
Unknown = 0,
Val1 = 1 << 0,
Val30 = 1 << 30,
}
}
}
12 changes: 7 additions & 5 deletions Src/Enums.NET/EnumCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1203,10 +1203,11 @@ public sealed override void GetAllFlags(ref byte result)

var sb = new StringBuilder();
TUnderlyingOperations operations = default;
var isLessThanZero = operations.LessThan(value, default);
for (var currentValue = operations.One; isLessThanZero ? !currentValue.Equals(default) : !operations.LessThan(value, currentValue); currentValue = operations.LeftShift(currentValue, 1))
var validValue = operations.And(value, _allFlags);
var checkForZero = operations.LessThan(validValue, default) || operations.LessThan(operations.LeftShift(validValue, 1), validValue);
for (var currentValue = operations.One; checkForZero ? !currentValue.Equals(default) : !operations.LessThan(validValue, currentValue); currentValue = operations.LeftShift(currentValue, 1))
{
if (HasAnyFlags(value, currentValue))
if (HasAnyFlags(validValue, currentValue))
{
if (sb.Length > 0)
{
Expand Down Expand Up @@ -1264,8 +1265,9 @@ int Iterate(ReadOnlySpan<char> delimiter, Span<char> dest, int maxLength)
var original = dest;
var length = 0;
TUnderlyingOperations operations = default;
var isLessThanZero = operations.LessThan(value, default);
for (var currentValue = operations.One; isLessThanZero ? !currentValue.Equals(default) : !operations.LessThan(value, currentValue); currentValue = operations.LeftShift(currentValue, 1))
var validValue = operations.And(value, _allFlags);
var checkForZero = operations.LessThan(validValue, default) || operations.LessThan(operations.LeftShift(validValue, 1), validValue);
for (var currentValue = operations.One; checkForZero ? !currentValue.Equals(default) : !operations.LessThan(validValue, currentValue); currentValue = operations.LeftShift(currentValue, 1))
{
if (HasAnyFlags(value, currentValue))
{
Expand Down
6 changes: 3 additions & 3 deletions Src/Enums.NET/Enums.NET.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
<PropertyGroup>
<TargetFrameworks>net45;netcoreapp3.0;netstandard2.1;netstandard2.0;netstandard1.3;netstandard1.1;netstandard1.0</TargetFrameworks>
<AssemblyVersion>4.0.0.0</AssemblyVersion>
<FileVersion>4.0.1</FileVersion>
<VersionPrefix>4.0.1</VersionPrefix>
<FileVersion>4.0.2</FileVersion>
<VersionPrefix>4.0.2</VersionPrefix>
<VersionSufix></VersionSufix>
<PackageVersion>4.0.1</PackageVersion>
<PackageVersion>4.0.2</PackageVersion>
<GeneratePackageOnBuild>False</GeneratePackageOnBuild>
<Authors>Tyler Brinkley</Authors>
<Description>Enums.NET is a high-performance type-safe .NET enum utility library</Description>
Expand Down

0 comments on commit 6ef93d0

Please sign in to comment.