Skip to content

Commit

Permalink
Code cleanup, improve Windows and Wine version fetching (#61)
Browse files Browse the repository at this point in the history
* Code cleanup, improve Windows and Wine version fetching

* Improve obsolete test

* Return to pool, format
  • Loading branch information
MichalPetryka authored Jun 30, 2024
1 parent 460ece5 commit e07d03f
Show file tree
Hide file tree
Showing 25 changed files with 1,366 additions and 1,305 deletions.
153 changes: 76 additions & 77 deletions NorthwoodLib.Tests/ActionDispatcherTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,81 +3,81 @@
using System.Threading;
using Xunit;

namespace NorthwoodLib.Tests
namespace NorthwoodLib.Tests;

public class ActionDispatcherTest
{
public class ActionDispatcherTest
[Fact]
public void InvokeTest()
{
[Fact]
public void InvokeTest()
{
bool executed = false;
ActionDispatcher dispatcher = new();
dispatcher.Dispatch(() => executed = true);
dispatcher.Invoke();
Assert.True(executed);
}
bool executed = false;
ActionDispatcher dispatcher = new();
dispatcher.Dispatch(() => executed = true);
dispatcher.Invoke();
Assert.True(executed);
}

[Fact]
public void OrderTest()
{
List<int> list = new();
ActionDispatcher dispatcher = new();
dispatcher.Dispatch(() => list.Add(1));
dispatcher.Dispatch(() => list.Add(2));
dispatcher.Dispatch(() => list.Add(3));
dispatcher.Dispatch(() => list.Add(4));
dispatcher.Dispatch(() => list.Add(5));
dispatcher.Invoke();
Assert.True(list.SequenceEqual(new[] { 1, 2, 3, 4, 5 }));
}
[Fact]
public void OrderTest()
{
List<int> list = [];
ActionDispatcher dispatcher = new();
dispatcher.Dispatch(() => list.Add(1));
dispatcher.Dispatch(() => list.Add(2));
dispatcher.Dispatch(() => list.Add(3));
dispatcher.Dispatch(() => list.Add(4));
dispatcher.Dispatch(() => list.Add(5));
dispatcher.Invoke();
Assert.True(list.SequenceEqual([1, 2, 3, 4, 5]));
}

[Fact]
public void WaitActionTest()
{
bool threadRunning = true;
ActionDispatcher dispatcher = new();
new Thread(() =>
[Fact]
public void WaitActionTest()
{
bool threadRunning = true;
ActionDispatcher dispatcher = new();
new Thread(() =>
{
// ReSharper disable once AccessToModifiedClosure
while (Volatile.Read(ref threadRunning))
{
// ReSharper disable once AccessToModifiedClosure
while (Volatile.Read(ref threadRunning))
{
dispatcher.Invoke();
Thread.Sleep(15);
}
})
dispatcher.Invoke();
Thread.Sleep(15);
}
})
{ IsBackground = true }.Start();
bool done = false;
dispatcher.Wait(() => done = true, 5);
Volatile.Write(ref threadRunning, false);
Assert.True(done);
}
bool done = false;
dispatcher.Wait(() => { done = true; }, 5);
Volatile.Write(ref threadRunning, false);
Assert.True(done);
}

[Fact]
public void WaitFuncTest()
{
bool threadRunning = true;
ActionDispatcher dispatcher = new();
new Thread(() =>
[Fact]
public void WaitFuncTest()
{
bool threadRunning = true;
ActionDispatcher dispatcher = new();
new Thread(() =>
{
// ReSharper disable once AccessToModifiedClosure
while (Volatile.Read(ref threadRunning))
{
// ReSharper disable once AccessToModifiedClosure
while (Volatile.Read(ref threadRunning))
{
dispatcher.Invoke();
Thread.Sleep(15);
}
})
dispatcher.Invoke();
Thread.Sleep(15);
}
})
{ IsBackground = true }.Start();
bool done = dispatcher.Wait(() => true, 5);
Volatile.Write(ref threadRunning, false);
Assert.True(done);
}
bool done = dispatcher.Wait(() => true, 5);
Volatile.Write(ref threadRunning, false);
Assert.True(done);
}

[Fact]
public void WaitArrayTest()
{
bool threadRunning = true;
ActionDispatcher dispatcher = new();
new Thread(() =>
[Fact]
public void WaitArrayTest()
{
bool threadRunning = true;
ActionDispatcher dispatcher = new();
new Thread(() =>
{
// ReSharper disable once AccessToModifiedClosure
while (Volatile.Read(ref threadRunning))
Expand All @@ -87,17 +87,16 @@ public void WaitArrayTest()
}
})
{ IsBackground = true }.Start();
List<int> list = new();
dispatcher.Wait(new[]
{
() => list.Add(1),
() => list.Add(2),
() => list.Add(3),
() => list.Add(4),
() => list.Add(5)
}, 5);
Volatile.Write(ref threadRunning, false);
Assert.True(list.SequenceEqual(new[] { 1, 2, 3, 4, 5 }));
}
List<int> list = [];
dispatcher.Wait(
[
() => list.Add(1),
() => list.Add(2),
() => list.Add(3),
() => list.Add(4),
() => list.Add(5)
], 5);
Volatile.Write(ref threadRunning, false);
Assert.True(list.SequenceEqual([1, 2, 3, 4, 5]));
}
}
56 changes: 27 additions & 29 deletions NorthwoodLib.Tests/NativeMemoryTest.cs
Original file line number Diff line number Diff line change
@@ -1,42 +1,40 @@
using System;
using Xunit;

namespace NorthwoodLib.Tests
namespace NorthwoodLib.Tests;

public unsafe class NativeMemoryTest
{
public class NativeMemoryTest
[Theory]
[InlineData(0)]
[InlineData(10)]
[InlineData(16384)]
public void CreateTest(int size)
{
[Theory]
[InlineData(0)]
[InlineData(10)]
[InlineData(16384)]
public void CreateTest(int size)
using (NativeMemory memory = new(size))
{
using (NativeMemory memory = new(size))
{
Assert.NotEqual(IntPtr.Zero, memory.Data);
Assert.Equal(size, memory.Length);
}
Assert.NotEqual(nuint.Zero, (nuint)memory.Data);
Assert.Equal(size, memory.Length);
}
}

[Fact]
public unsafe void AccessTest()
[Fact]
public void AccessTest()
{
using (NativeMemory memory = new(sizeof(int) * 10))
{
using (NativeMemory memory = new(sizeof(int) * 10))
{
int* ptr = memory.ToPointer<int>();
for (int i = 0; i < 10; i++)
ptr[i] = i;
int* ptr = memory.ToPointer<int>();
for (int i = 0; i < 10; i++)
ptr[i] = i;

for (int i = 0; i < 10; i++)
Assert.Equal(i, ptr[i]);
}
for (int i = 0; i < 10; i++)
Assert.Equal(i, ptr[i]);
}
}

[Fact]
public unsafe void PointerTest()
{
using (NativeMemory memory = new(sizeof(int) * 10))
Assert.Equal(memory.Data, new IntPtr(memory.ToPointer<int>()));
}
[Fact]
public void PointerTest()
{
using (NativeMemory memory = new(sizeof(int) * 10))
Assert.Equal((nuint)memory.Data, (nuint)memory.ToPointer<int>());
}
}
83 changes: 40 additions & 43 deletions NorthwoodLib.Tests/OperatingSystemTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,55 +5,52 @@
using Xunit;
using Xunit.Abstractions;

namespace NorthwoodLib.Tests
namespace NorthwoodLib.Tests;

public class OperatingSystemTest(ITestOutputHelper output) : LoggingTest(output)
{
public class OperatingSystemTest : LoggingTest
[Fact]
public void UsesNativeDataTest()
{
public OperatingSystemTest(ITestOutputHelper output) : base(output)
{
}

[Fact]
public void UsesNativeDataTest()
{
Assert.Equal(RuntimeInformation.IsOSPlatform(OSPlatform.Windows) || File.Exists("/etc/os-release"), OperatingSystem.UsesNativeData);
}
Assert.Equal(RuntimeInformation.IsOSPlatform(OSPlatform.Windows) || File.Exists("/etc/os-release"), OperatingSystem.UsesNativeData);
}

[Fact]
[Obsolete("UsesWine is obsolete")]
public void UsesWineTest()
{
Assert.Equal(WineInfo.UsesWine, OperatingSystem.UsesWine);
}
[Fact]
[Obsolete("UsesWine is obsolete")]
public void UsesWineTest()
{
Assert.Equal(WineInfo.UsesWine, OperatingSystem.UsesWine);
}

[Fact]
public void CorrectStringTest()
{
string version = OperatingSystem.VersionString;
Logger.WriteLine(version);
Assert.NotNull(version);
Assert.NotEqual("", version);
}
[Fact]
public void ValidStringTest()
{
string version = OperatingSystem.VersionString;
Logger.WriteLine(version);
Assert.NotNull(version);
Assert.NotEqual("", version);
}

[Fact]
public void CorrectVersionTest()
{
Version version = OperatingSystem.Version;
Logger.WriteLine(version.ToString());
Assert.NotEqual(new Version(0, 0, 0), version);
}
[Fact]
public void ValidVersionTest()
{
Version version = OperatingSystem.Version;
Logger.WriteLine(version.ToString());
Assert.NotEqual(new Version(0, 0, 0), version);
}

[Fact]
public void TrueVersionTest()
{
if (!RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
return;
[Fact]
public void TrueVersionTest()
{
if (!RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
return;

Version version = OperatingSystem.Version;
OperatingSystem.TryCheckWindowsFileVersion(out Version v);
Assert.Equal(version.Major, v.Major);
Assert.Equal(version.Minor, v.Minor);
Assert.Equal(version.Build, v.Build);
}
Version version = OperatingSystem.Version;
OperatingSystem.TryCheckWindowsFileVersion(out Version v, OperatingSystem.GetWindowsRegistryBuild());
Logger.WriteLine(version.ToString());
Logger.WriteLine(v.ToString());
Assert.Equal(version.Major, v.Major);
Assert.Equal(version.Minor, v.Minor);
Assert.Equal(version.Build, v.Build);
}
}
Loading

0 comments on commit e07d03f

Please sign in to comment.