Skip to content

Commit

Permalink
Fix analyzer suggestions for unit tests and benchmarks
Browse files Browse the repository at this point in the history
  • Loading branch information
afxres committed Dec 24, 2023
1 parent 3a53f06 commit 98c03d0
Show file tree
Hide file tree
Showing 44 changed files with 154 additions and 146 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<TargetFramework>net8.0</TargetFramework>
<AssemblyName>Mikodev.Binary.Benchmarks.Abstractions</AssemblyName>
<RootNamespace>Mikodev.Binary.Benchmarks.Abstractions</RootNamespace>
<LangVersion>10.0</LangVersion>
<LangVersion>12</LangVersion>
<Nullable>enable</Nullable>
</PropertyGroup>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<TargetFramework>net8.0</TargetFramework>
<AssemblyName>Mikodev.Binary.Benchmarks.AllocatorStringTests</AssemblyName>
<RootNamespace>Mikodev.Binary.Benchmarks.AllocatorStringTests</RootNamespace>
<LangVersion>10.0</LangVersion>
<LangVersion>12</LangVersion>
<Nullable>enable</Nullable>
</PropertyGroup>

Expand Down
5 changes: 2 additions & 3 deletions code/Benchmarks.AllocatorTests/AllocatorBenchmarks.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
namespace Mikodev.Binary.Benchmarks.AllocatorTests;

using BenchmarkDotNet.Attributes;
using System;

[MemoryDiagnoser]
public class AllocatorBenchmarks
Expand All @@ -23,8 +22,8 @@ public class AllocatorBenchmarks
[GlobalSetup]
public void Setup()
{
this.buffer0 = Array.Empty<byte>();
this.buffer1 = new byte[] { 0x7F };
this.buffer0 = [];
this.buffer1 = [0x7F];
this.buffer1024 = new byte[1024];
this.maxCapacity512 = 512;
this.maxCapacity2048 = 2048;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<TargetFramework>net8.0</TargetFramework>
<AssemblyName>Mikodev.Binary.Benchmarks.AllocatorTests</AssemblyName>
<RootNamespace>Mikodev.Binary.Benchmarks.AllocatorTests</RootNamespace>
<LangVersion>10.0</LangVersion>
<LangVersion>12</LangVersion>
<Nullable>enable</Nullable>
</PropertyGroup>

Expand Down
6 changes: 3 additions & 3 deletions code/Benchmarks.ArrayTests/ArrayBenchmarks.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,9 @@ static IEnumerable<IConverter> Select(string? flag)
.AddConverters(Select(this.Flag))
.Build();

this.array01 = new[] { 1313 };
this.list01 = new List<int> { 1313 };
this.memory01 = new Memory<int>(new int[] { 1313 });
this.array01 = [1313];
this.list01 = [1313];
this.memory01 = new Memory<int>([1313]);

this.arrayConverter = generator.GetConverter<int[]>();
this.listConverter = generator.GetConverter<List<int>>();
Expand Down
2 changes: 1 addition & 1 deletion code/Benchmarks.ArrayTests/Benchmarks.ArrayTests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<TargetFramework>net8.0</TargetFramework>
<AssemblyName>Mikodev.Binary.Benchmarks.ArrayTests</AssemblyName>
<RootNamespace>Mikodev.Binary.Benchmarks.ArrayTests</RootNamespace>
<LangVersion>10.0</LangVersion>
<LangVersion>12</LangVersion>
<Nullable>enable</Nullable>
</PropertyGroup>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,14 @@
<TargetFramework>net8.0</TargetFramework>
<AssemblyName>Mikodev.Binary.Benchmarks.BinaryDictionaryTests</AssemblyName>
<RootNamespace>Mikodev.Binary.Benchmarks.BinaryDictionaryTests</RootNamespace>
<LangVersion>10.0</LangVersion>
<LangVersion>12</LangVersion>
<Nullable>enable</Nullable>
</PropertyGroup>

<PropertyGroup>
<NoWarn>CA1861</NoWarn>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="BenchmarkDotNet" Version="0.13.11" />
</ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,15 +54,15 @@ public void Setup()
GetValueOrDefault CreateLongData(byte[][] keys)
{
var source = keys.Select(x => new ReadOnlyMemory<byte>(x)).ToImmutableArray();
var target = createLongData.Invoke(null, new[] { (object)source });
var target = createLongData.Invoke(null, [(object)source]);
var result = Delegate.CreateDelegate(typeof(GetValueOrDefault), target!, "GetValue");
return (GetValueOrDefault)result;
}

GetValueOrDefault CreateHashCode(byte[][] keys)
{
var source = keys.Select((x, i) => KeyValuePair.Create(new ReadOnlyMemory<byte>(x), i)).ToImmutableArray();
var target = createHashCode.Invoke(null, new[] { (object)source, -1 });
var target = createHashCode.Invoke(null, [(object)source, -1]);
var result = Delegate.CreateDelegate(typeof(GetValueOrDefault), target!, "GetValue");
return (GetValueOrDefault)result;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<TargetFramework>net8.0</TargetFramework>
<AssemblyName>Mikodev.Binary.Benchmarks.CollectionDecodeTests</AssemblyName>
<RootNamespace>Mikodev.Binary.Benchmarks.CollectionDecodeTests</RootNamespace>
<LangVersion>10.0</LangVersion>
<LangVersion>12</LangVersion>
<Nullable>enable</Nullable>
</PropertyGroup>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public static Decoder<T> GetDecoder<T, E>(Converter<E> converter, Expression<Act

public static Func<IEnumerable<int>, T> GetConstructor<T, E>() where T : IEnumerable<E>
{
var constructor = typeof(T).GetConstructor(new[] { typeof(IEnumerable<E>) }) ?? throw new Exception();
var constructor = typeof(T).GetConstructor([typeof(IEnumerable<E>)]) ?? throw new Exception();
var source = Expression.Parameter(typeof(IEnumerable<E>), "source");
var lambda = Expression.Lambda<Func<IEnumerable<int>, T>>(Expression.New(constructor, source), source);
return lambda.Compile();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,14 @@
<TargetFramework>net8.0</TargetFramework>
<AssemblyName>Mikodev.Binary.Benchmarks.CollectionTests</AssemblyName>
<RootNamespace>Mikodev.Binary.Benchmarks.CollectionTests</RootNamespace>
<LangVersion>10.0</LangVersion>
<LangVersion>12</LangVersion>
<Nullable>enable</Nullable>
</PropertyGroup>

<PropertyGroup>
<NoWarn>CA1861</NoWarn>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="BenchmarkDotNet" Version="0.13.11" />
</ItemGroup>
Expand Down
2 changes: 1 addition & 1 deletion code/Benchmarks.CollectionTests/CollectionBenchmarks.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public void Setup()
: new VariableNativeConverter<int>() as Converter<int>;
var generator = Generator.CreateDefaultBuilder().AddConverter(converter).Build();

this.hashSet = new HashSet<int> { 1313 };
this.hashSet = [1313];
this.linkedList = new LinkedList<int>(new[] { 1313 });
this.dictionary = new Dictionary<int, int> { [1313] = 1313 };

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<TargetFramework>net8.0</TargetFramework>
<AssemblyName>Mikodev.Binary.Benchmarks.ConverterTests</AssemblyName>
<RootNamespace>Mikodev.Binary.Benchmarks.ConverterTests</RootNamespace>
<LangVersion>10.0</LangVersion>
<LangVersion>12</LangVersion>
<Nullable>enable</Nullable>
</PropertyGroup>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<TargetFramework>net8.0</TargetFramework>
<AssemblyName>Mikodev.Binary.Benchmarks.EnumerationTests</AssemblyName>
<RootNamespace>Mikodev.Binary.Benchmarks.EnumerationTests</RootNamespace>
<LangVersion>10.0</LangVersion>
<LangVersion>12</LangVersion>
<Nullable>enable</Nullable>
</PropertyGroup>

Expand Down
2 changes: 1 addition & 1 deletion code/Benchmarks.EnumerationTests/EnumerationBenchmarks.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public void Setup()
var generator = Generator.CreateDefault();
this.buffer = new byte[1 << 24];
this.converter = generator.GetConverter<T>();
this.collection = source.ToHashSet();
this.collection = [.. source];
this.dictionary = source.ToDictionary(x => x);
this.collectionConverter = generator.GetConverter<HashSet<T>>();
this.dictionaryConverter = generator.GetConverter<Dictionary<T, T>>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<TargetFramework>net8.0</TargetFramework>
<AssemblyName>Mikodev.Binary.Benchmarks.GeneratorTests</AssemblyName>
<RootNamespace>Mikodev.Binary.Benchmarks.GeneratorTests</RootNamespace>
<LangVersion>10.0</LangVersion>
<LangVersion>12</LangVersion>
<Nullable>enable</Nullable>
</PropertyGroup>

Expand Down
4 changes: 2 additions & 2 deletions code/Benchmarks.IntegrationTests/IntegrationBenchmarks.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,11 @@ public void Setup()
{
Id = 1024,
Name = "csharp",
List = new[] { 7, 11, 555, 1313 },
List = [7, 11, 555, 1313],
Item = new Type02
{
Data = 2.2D,
Tags = new List<string> { "one", "two", "three" },
Tags = ["one", "two", "three"],
}
};
this.tuple = (1024, "csharp", new[] { 7, 11, 555, 1313 }, (2.2D, new List<string> { "one", "two", "three" }));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<TargetFramework>net8.0</TargetFramework>
<AssemblyName>Mikodev.Binary.Benchmarks.LengthPrefixEncodeTests</AssemblyName>
<RootNamespace>Mikodev.Binary.Benchmarks.LengthPrefixEncodeTests</RootNamespace>
<LangVersion>10.0</LangVersion>
<LangVersion>12</LangVersion>
<Nullable>enable</Nullable>
</PropertyGroup>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ private struct InlineBuffer<T>
private static List<T> Decode<T>(Converter<T> converter, ReadOnlySpan<byte> span)
{
if (span.Length is 0)
return new List<T>();
return [];
const int FallbackCapacity = 8;
var intent = span;
var result = new List<T>(FallbackCapacity);
Expand All @@ -32,7 +32,7 @@ private static List<T> Decode<T>(Converter<T> converter, ReadOnlySpan<byte> span
private static List<T> DecodeStackBased<T>(Converter<T> converter, ReadOnlySpan<byte> span)
{
if (span.Length is 0)
return new List<T>();
return [];
var buffer = new InlineBuffer<T>();
var target = (Span<T>)buffer;
var cursor = 0;
Expand All @@ -48,7 +48,7 @@ private static List<T> DecodeStackBased<T>(Converter<T> converter, ReadOnlySpan<
private static List<T> DecodeRecursively<T>(Converter<T> converter, ReadOnlySpan<byte> span)
{
if (span.Length is 0)
return new List<T>();
return [];
return DecodeRecursively(converter, ref span, 0);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<TargetFramework>net8.0</TargetFramework>
<AssemblyName>Mikodev.Binary.Benchmarks.ObjectConverterTests</AssemblyName>
<RootNamespace>Mikodev.Binary.Benchmarks.ObjectConverterTests</RootNamespace>
<LangVersion>10.0</LangVersion>
<LangVersion>12</LangVersion>
<Nullable>enable</Nullable>
</PropertyGroup>

Expand Down
2 changes: 1 addition & 1 deletion code/Benchmarks.ObjectTests/Benchmarks.ObjectTests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<TargetFramework>net8.0</TargetFramework>
<AssemblyName>Mikodev.Binary.Benchmarks.ObjectTests</AssemblyName>
<RootNamespace>Mikodev.Binary.Benchmarks.ObjectTests</RootNamespace>
<LangVersion>10.0</LangVersion>
<LangVersion>12</LangVersion>
<Nullable>enable</Nullable>
</PropertyGroup>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@
<IsPackable>false</IsPackable>
</PropertyGroup>

<PropertyGroup>
<NoWarn>xUnit1042</NoWarn>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0" />
<PackageReference Include="xunit" Version="2.6.4" />
Expand Down
2 changes: 1 addition & 1 deletion code/Binary.Experimental.Tests/BitArrayConverterTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public void EmptyValue(byte[] header)
var converter = new BitArrayConverter();
var source = new BitArray(0);
var buffer = converter.Encode(source);
Assert.Equal(new byte[] { 0 }, buffer);
Assert.Equal([0], buffer);
var result = converter.Decode(header);
Assert.NotNull(result);
Assert.Equal(0, result.Length);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@ public void EmptyArrayTest()
{
var generator = Generator.CreateAot();
var converter = new LengthHeadedArrayConverter<int>(generator.GetConverter<int>());
var buffer = converter.Encode(Array.Empty<int>());
Assert.Equal(new byte[] { 0 }, buffer);
var buffer = converter.Encode([]);
Assert.Equal([0], buffer);
var a = converter.Decode(new byte[] { 0 });
var b = converter.Decode(new byte[] { 0x80, 0, 0, 0 });
Assert.NotNull(a);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@
<IsPackable>false</IsPackable>
</PropertyGroup>

<PropertyGroup>
<NoWarn>CA1861;xUnit1042</NoWarn>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0" />
<PackageReference Include="xunit" Version="2.6.4" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,17 @@ public interface IAbstractDictionary<K, V> : IDictionary<K, V> { }

public interface IAbstractReadOnlyDictionary<K, V> : IReadOnlyDictionary<K, V> { }

public abstract class AbstractEnumerable<T> : IAbstractEnumerable<T>
public abstract class AbstractEnumerable<T>(IEnumerable<T> collection) : IAbstractEnumerable<T>
{
public IEnumerable<T> Collection { get; }

public AbstractEnumerable(IEnumerable<T> collection) => Collection = collection;
public IEnumerable<T> Collection { get; } = collection;

public IEnumerator<T> GetEnumerator() => Collection.GetEnumerator();

IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
}

public abstract class AbstractDictionary<K, V> : AbstractEnumerable<KeyValuePair<K, V>>, IAbstractDictionary<K, V>
public abstract class AbstractDictionary<K, V>(IEnumerable<KeyValuePair<K, V>> collection) : AbstractEnumerable<KeyValuePair<K, V>>(collection), IAbstractDictionary<K, V>
{
public AbstractDictionary(IEnumerable<KeyValuePair<K, V>> collection) : base(collection) { }

void IDictionary<K, V>.Add(K key, V value) => throw new NotImplementedException();

bool IDictionary<K, V>.ContainsKey(K key) => throw new NotImplementedException();
Expand Down Expand Up @@ -57,10 +53,8 @@ public AbstractDictionary(IEnumerable<KeyValuePair<K, V>> collection) : base(col
bool ICollection<KeyValuePair<K, V>>.IsReadOnly => throw new NotImplementedException();
}

public abstract class AbstractReadOnlyDictionary<K, V> : AbstractEnumerable<KeyValuePair<K, V>>, IAbstractReadOnlyDictionary<K, V>
public abstract class AbstractReadOnlyDictionary<K, V>(IEnumerable<KeyValuePair<K, V>> collection) : AbstractEnumerable<KeyValuePair<K, V>>(collection), IAbstractReadOnlyDictionary<K, V>
{
public AbstractReadOnlyDictionary(IEnumerable<KeyValuePair<K, V>> collection) : base(collection) { }

bool IReadOnlyDictionary<K, V>.ContainsKey(K key) => throw new NotImplementedException();

bool IReadOnlyDictionary<K, V>.TryGetValue(K key, out V value) => throw new NotImplementedException();
Expand All @@ -74,35 +68,20 @@ public AbstractReadOnlyDictionary(IEnumerable<KeyValuePair<K, V>> collection) :
int IReadOnlyCollection<KeyValuePair<K, V>>.Count => throw new NotImplementedException();
}

public class CustomEnumerable<T> : AbstractEnumerable<T>
{
public CustomEnumerable(IEnumerable<T> collection) : base(collection) { }
}
public class CustomEnumerable<T>(IEnumerable<T> collection) : AbstractEnumerable<T>(collection) { }

public class CustomEnumerableInternalConstructor<T> : AbstractEnumerable<T>
{
internal CustomEnumerableInternalConstructor(IEnumerable<T> collection) : base(collection) { }
}

public class CustomDictionary<K, V> : AbstractDictionary<K, V>
{
public CustomDictionary(IDictionary<K, V> collection) : base(collection) { }
}
public class CustomDictionary<K, V>(IDictionary<K, V> collection) : AbstractDictionary<K, V>(collection) { }

public class CustomReadOnlyDictionary<K, V> : AbstractReadOnlyDictionary<K, V>
{
public CustomReadOnlyDictionary(IReadOnlyDictionary<K, V> collection) : base(collection) { }
}
public class CustomReadOnlyDictionary<K, V>(IReadOnlyDictionary<K, V> collection) : AbstractReadOnlyDictionary<K, V>(collection) { }

public class CustomDictionaryEnumerableConstructor<K, V> : AbstractDictionary<K, V>
{
public CustomDictionaryEnumerableConstructor(IEnumerable<KeyValuePair<K, V>> collection) : base(collection) { }
}
public class CustomDictionaryEnumerableConstructor<K, V>(IEnumerable<KeyValuePair<K, V>> collection) : AbstractDictionary<K, V>(collection) { }

public class CustomReadOnlyDictionaryEnumerableConstructor<K, V> : AbstractReadOnlyDictionary<K, V>
{
public CustomReadOnlyDictionaryEnumerableConstructor(IEnumerable<KeyValuePair<K, V>> collection) : base(collection) { }
}
public class CustomReadOnlyDictionaryEnumerableConstructor<K, V>(IEnumerable<KeyValuePair<K, V>> collection) : AbstractReadOnlyDictionary<K, V>(collection) { }

public class CustomDictionaryInternalConstructor<K, V> : AbstractDictionary<K, V>
{
Expand All @@ -114,11 +93,9 @@ public class CustomReadOnlyDictionaryInternalConstructor<K, V> : AbstractReadOnl
internal CustomReadOnlyDictionaryInternalConstructor(IEnumerable<KeyValuePair<K, V>> collection) : base(collection) { }
}

public readonly struct CustomValueEnumerable<T> : IEnumerable<T>
public readonly struct CustomValueEnumerable<T>(IEnumerable<T>? collection) : IEnumerable<T>
{
public readonly IEnumerable<T>? Collection;

public CustomValueEnumerable(IEnumerable<T>? collection) => this.Collection = collection;
public readonly IEnumerable<T>? Collection = collection;

public IEnumerator<T> GetEnumerator() => (this.Collection ?? Array.Empty<T>()).GetEnumerator();

Expand Down Expand Up @@ -286,8 +263,8 @@ public void EnumerableInterfaceOrAbstractClassTest<T, E>(Type wantedType, T sour
{
Assert.True(wantedType.IsInterface || wantedType.IsAbstract);
var method = new Action<IEnumerable<object>, List<object>>(EnumerableEncodeOnlyTest).Method;
var target = method.GetGenericMethodDefinition().MakeGenericMethod(new Type[] { wantedType, typeof(E) });
var result = target.Invoke(this, new object?[] { source, actual });
var target = method.GetGenericMethodDefinition().MakeGenericMethod([wantedType, typeof(E)]);
var result = target.Invoke(this, [source, actual]);
Assert.Null(result);
}
}
Loading

0 comments on commit 98c03d0

Please sign in to comment.