From 4b7b9975c57a179670e5c1c22aeb31220aaaa97e Mon Sep 17 00:00:00 2001 From: mvenditto Date: Tue, 20 Jun 2023 21:25:11 +0200 Subject: [PATCH] add lib loading tests + remove dummy test --- .../csharp/Gpt4All.Tests/ModelFactoryTests.cs | 5 -- .../Gpt4All.Tests/NativeLibraryLoaderTests.cs | 56 +++++++++++++++++++ .../PlatformSpecificFactAttribute.cs | 27 +++++++++ gpt4all-bindings/csharp/Gpt4All/Gpt4All.cs | 3 + 4 files changed, 86 insertions(+), 5 deletions(-) create mode 100644 gpt4all-bindings/csharp/Gpt4All.Tests/NativeLibraryLoaderTests.cs create mode 100644 gpt4all-bindings/csharp/Gpt4All.Tests/PlatformSpecificFactAttribute.cs diff --git a/gpt4all-bindings/csharp/Gpt4All.Tests/ModelFactoryTests.cs b/gpt4all-bindings/csharp/Gpt4All.Tests/ModelFactoryTests.cs index bc5b50ed..d7b0569e 100644 --- a/gpt4all-bindings/csharp/Gpt4All.Tests/ModelFactoryTests.cs +++ b/gpt4all-bindings/csharp/Gpt4All.Tests/ModelFactoryTests.cs @@ -31,9 +31,4 @@ public class ModelFactoryTests { using var model = _modelFactory.LoadModel(Constants.MPT_MODEL_PATH); } - - [Fact] - public void DummyTest() - { - } } diff --git a/gpt4all-bindings/csharp/Gpt4All.Tests/NativeLibraryLoaderTests.cs b/gpt4all-bindings/csharp/Gpt4All.Tests/NativeLibraryLoaderTests.cs new file mode 100644 index 00000000..aaf35174 --- /dev/null +++ b/gpt4all-bindings/csharp/Gpt4All.Tests/NativeLibraryLoaderTests.cs @@ -0,0 +1,56 @@ +using System.IO; +using Gpt4All.LibraryLoader; +using Xunit; + +namespace Gpt4All.Tests; + +public class NativeLibraryLoaderTests +{ + [Fact] + public void NativeLibraryShouldLoad() + { + var result = NativeLibraryLoader.LoadNativeLibrary(bypassLoading: false); + Assert.True(result.IsSuccess); + } + + private const string LLModelLib = "libllmodel.{0}"; + + [PlatformSpecificFact(Platforms.Windows)] + public void NativeLibraryShouldLoad_Windows() + { + var libraryLoader = new WindowsLibraryLoader(); + + var libraryPath = Path.Combine( + Environment.CurrentDirectory, + string.Format(LLModelLib, "dll")); + + var result = libraryLoader.OpenLibrary(libraryPath); + Assert.True(result.IsSuccess); + } + + [PlatformSpecificFact(Platforms.Linux)] + public void NativeLibraryShouldLoad_Linux() + { + var libraryLoader = new WindowsLibraryLoader(); + + var libraryPath = Path.Combine( + Environment.CurrentDirectory, + string.Format(LLModelLib, "so")); + + var result = libraryLoader.OpenLibrary(libraryPath); + Assert.True(result.IsSuccess); + } + + [PlatformSpecificFact(Platforms.MacOS)] + public void NativeLibraryShouldLoad_MacOS() + { + var libraryLoader = new WindowsLibraryLoader(); + + var libraryPath = Path.Combine( + Environment.CurrentDirectory, + string.Format(LLModelLib, "dylib")); + + var result = libraryLoader.OpenLibrary(libraryPath); + Assert.True(result.IsSuccess); + } +} diff --git a/gpt4all-bindings/csharp/Gpt4All.Tests/PlatformSpecificFactAttribute.cs b/gpt4all-bindings/csharp/Gpt4All.Tests/PlatformSpecificFactAttribute.cs new file mode 100644 index 00000000..9f322f6a --- /dev/null +++ b/gpt4all-bindings/csharp/Gpt4All.Tests/PlatformSpecificFactAttribute.cs @@ -0,0 +1,27 @@ +using Xunit; + +namespace Gpt4All.Tests; + +public static class Platforms +{ + public const string Windows = "windows"; + public const string Linux = "linux"; + public const string MacOS = "macOS"; +} + +/// +/// This attribute ensures the Fact is only run on the specified platform. +/// +/// +/// for info about the platform string. +/// +public class PlatformSpecificFactAttribute : FactAttribute +{ + public PlatformSpecificFactAttribute(string platform) + { + if (!OperatingSystem.IsOSPlatform(platform)) + { + Skip = $"Test only runs on {platform}."; + } + } +} diff --git a/gpt4all-bindings/csharp/Gpt4All/Gpt4All.cs b/gpt4all-bindings/csharp/Gpt4All/Gpt4All.cs index 318361a0..f24f5ba1 100644 --- a/gpt4all-bindings/csharp/Gpt4All/Gpt4All.cs +++ b/gpt4all-bindings/csharp/Gpt4All/Gpt4All.cs @@ -1,8 +1,11 @@ using System.Diagnostics; +using System.Runtime.CompilerServices; using Gpt4All.Bindings; using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging.Abstractions; +[assembly: InternalsVisibleTo("Gpt4All.Tests")] + namespace Gpt4All; public class Gpt4All : IGpt4AllModel