Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions LLama.Unittest/DictionaryExtensionsTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
using LLama.Extensions;

namespace LLama.Unittest
{
public class DictionaryExtensionsTests
{
[Fact]
public void GetDefaultValueEmptyDict()
{
var dict = new Dictionary<int, int>();

Assert.Equal(42, DictionaryExtensions.GetValueOrDefaultImpl(dict, 0, 42));
}

[Fact]
public void GetDefaultValueMissingKey()
{
var dict = new Dictionary<int, int>()
{
{ 3, 4 }
};

Assert.Equal(43, DictionaryExtensions.GetValueOrDefaultImpl(dict, 0, 43));
}

[Fact]
public void GetValue()
{
var dict = new Dictionary<int, int>()
{
{ 3, 4 },
{ 4, 5 },
};

Assert.Equal(4, DictionaryExtensions.GetValueOrDefaultImpl(dict, 3, 42));
}
}
}
37 changes: 37 additions & 0 deletions LLama.Unittest/EncodingExtensionsTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
using System.Text;
using EncodingExtensions = LLama.Extensions.EncodingExtensions;

namespace LLama.Unittest
{
public class EncodingExtensionsTests
{
private static void GetCharsTest(string str)
{
var bytes = Encoding.UTF8.GetBytes(str);

var chars = new char[128];
var count = EncodingExtensions.GetCharsImpl(Encoding.UTF8, bytes, chars);

Assert.Equal(str.Length, count);
Assert.True(chars[..count].SequenceEqual(str));
}

[Fact]
public void GetCharsEmptyString()
{
GetCharsTest("");
}

[Fact]
public void GetCharsString()
{
GetCharsTest("Hello World");
}

[Fact]
public void GetCharsChineseString()
{
GetCharsTest("猫坐在垫子上");
}
}
}
36 changes: 36 additions & 0 deletions LLama.Unittest/IEnumerableExtensionsTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
using LLama.Extensions;

namespace LLama.Unittest;

public class IEnumerableExtensionsTests
{
[Fact]
public void TakeLastEmpty()
{
var arr = Array.Empty<int>();

var last = IEnumerableExtensions.TakeLastImpl(arr, 5).ToList();

Assert.Empty(last);
}

[Fact]
public void TakeLastAll()
{
var arr = new[] { 1, 2, 3, 4, 5 };

var last = IEnumerableExtensions.TakeLastImpl(arr, 5).ToList();

Assert.True(last.SequenceEqual(arr));
}

[Fact]
public void TakeLast()
{
var arr = new[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };

var last = IEnumerableExtensions.TakeLastImpl(arr, 5).ToList();

Assert.True(last.SequenceEqual(arr[5..]));
}
}
22 changes: 22 additions & 0 deletions LLama.Unittest/IReadOnlyListExtensionsTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using LLama.Extensions;

namespace LLama.Unittest;

public class IReadOnlyListExtensionsTests
{
[Fact]
public void IndexOfItem()
{
var items = (IReadOnlyList<int>)new List<int> { 1, 2, 3, 4, };

Assert.Equal(2, items.IndexOf(3));
}

[Fact]
public void IndexOfItemNotFound()
{
var items = (IReadOnlyList<int>)new List<int> { 1, 2, 3, 4, };

Assert.Null(items.IndexOf(42));
}
}
15 changes: 15 additions & 0 deletions LLama.Unittest/KeyValuePairExtensionsTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
namespace LLama.Unittest;

public class KeyValuePairExtensionsTests
{
[Fact]
public void Deconstruct()
{
var kvp = new KeyValuePair<int, string>(1, "2");

var (a, b) = kvp;

Assert.Equal(1, a);
Assert.Equal("2", b);
}
}
6 changes: 3 additions & 3 deletions LLama.Unittest/TokenTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public void TokensEndSubstring()
{
var tokens = _model.NativeHandle.Tokenize("The cat sat on the edge of the mat", false, Encoding.UTF8);

var result = tokens.TokensEndsWithAnyString(new[]
var result = tokens.TokensEndsWithAnyString((IList<string>)new[]
{
"at",
}, _model.NativeHandle, Encoding.UTF8);
Expand All @@ -55,7 +55,7 @@ public void TokensNotEndWith()
{
var tokens = _model.NativeHandle.Tokenize("The cat sat on the edge of the mat", false, Encoding.UTF8);

var result = tokens.TokensEndsWithAnyString(new[]
var result = tokens.TokensEndsWithAnyString((IList<string>)new[]
{
"a fish",
"The cat sat on the edge of the ma",
Expand All @@ -69,7 +69,7 @@ public void TokensNotEndWithNothing()
{
var tokens = _model.NativeHandle.Tokenize("The cat sat on the edge of the mat", false, Encoding.UTF8);

var result = tokens.TokensEndsWithAnyString(Array.Empty<string>(), _model.NativeHandle, Encoding.UTF8);
var result = tokens.TokensEndsWithAnyString((IList<string>)Array.Empty<string>(), _model.NativeHandle, Encoding.UTF8);
Assert.False(result);
}
}
7 changes: 6 additions & 1 deletion LLama/Extensions/DictionaryExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,13 @@ internal static class DictionaryExtensions
#if NETSTANDARD2_0
public static TValue GetValueOrDefault<TKey, TValue>(this IReadOnlyDictionary<TKey, TValue> dictionary, TKey key, TValue defaultValue)
{
return dictionary.TryGetValue(key, out var value) ? value : defaultValue;
return GetValueOrDefaultImpl(dictionary, key, defaultValue);
}
#endif

internal static TValue GetValueOrDefaultImpl<TKey, TValue>(IReadOnlyDictionary<TKey, TValue> dictionary, TKey key, TValue defaultValue)
{
return dictionary.TryGetValue(key, out var value) ? value : defaultValue;
}
}
}
17 changes: 15 additions & 2 deletions LLama/Extensions/EncodingExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,20 @@ internal static class EncodingExtensions
#if NETSTANDARD2_0
public static int GetChars(this Encoding encoding, ReadOnlySpan<byte> bytes, Span<char> output)
{
return GetCharsImpl(encoding, bytes, output);
}

public static int GetCharCount(this Encoding encoding, ReadOnlySpan<byte> bytes)
{
return GetCharCountImpl(encoding, bytes);
}
#endif

internal static int GetCharsImpl(Encoding encoding, ReadOnlySpan<byte> bytes, Span<char> output)
{
if (bytes.Length == 0)
return 0;

unsafe
{
fixed (byte* bytePtr = bytes)
Expand All @@ -19,7 +33,7 @@ public static int GetChars(this Encoding encoding, ReadOnlySpan<byte> bytes, Spa
}
}

public static int GetCharCount(this Encoding encoding, ReadOnlySpan<byte> bytes)
internal static int GetCharCountImpl(Encoding encoding, ReadOnlySpan<byte> bytes)
{
unsafe
{
Expand All @@ -29,5 +43,4 @@ public static int GetCharCount(this Encoding encoding, ReadOnlySpan<byte> bytes)
}
}
}
#endif
}
9 changes: 7 additions & 2 deletions LLama/Extensions/IEnumerableExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,20 @@ internal static class IEnumerableExtensions
{
#if NETSTANDARD2_0
public static IEnumerable<T> TakeLast<T>(this IEnumerable<T> source, int count)
{
return TakeLastImpl(source, count);
}
#endif

internal static IEnumerable<T> TakeLastImpl<T>(IEnumerable<T> source, int count)
{
var list = source.ToList();

if (count >= list.Count)
return list;

list.RemoveRange(0, list.Count - count);
return list;
}
#endif
}
}
14 changes: 0 additions & 14 deletions LLama/Extensions/ListExtensions.cs

This file was deleted.