Extensions Method Unit Teststags/v0.6.0
| @@ -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)); | |||||
| } | |||||
| } | |||||
| } | |||||
| @@ -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("猫坐在垫子上"); | |||||
| } | |||||
| } | |||||
| } | |||||
| @@ -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..])); | |||||
| } | |||||
| } | |||||
| @@ -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)); | |||||
| } | |||||
| } | |||||
| @@ -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); | |||||
| } | |||||
| } | |||||
| @@ -43,7 +43,7 @@ public sealed class TokenTests | |||||
| { | { | ||||
| var tokens = _model.NativeHandle.Tokenize("The cat sat on the edge of the mat", false, Encoding.UTF8); | 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", | "at", | ||||
| }, _model.NativeHandle, Encoding.UTF8); | }, _model.NativeHandle, Encoding.UTF8); | ||||
| @@ -55,7 +55,7 @@ public sealed class TokenTests | |||||
| { | { | ||||
| var tokens = _model.NativeHandle.Tokenize("The cat sat on the edge of the mat", false, Encoding.UTF8); | 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", | "a fish", | ||||
| "The cat sat on the edge of the ma", | "The cat sat on the edge of the ma", | ||||
| @@ -69,7 +69,7 @@ public sealed class TokenTests | |||||
| { | { | ||||
| var tokens = _model.NativeHandle.Tokenize("The cat sat on the edge of the mat", false, Encoding.UTF8); | 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); | Assert.False(result); | ||||
| } | } | ||||
| } | } | ||||
| @@ -7,8 +7,13 @@ namespace LLama.Extensions | |||||
| #if NETSTANDARD2_0 | #if NETSTANDARD2_0 | ||||
| public static TValue GetValueOrDefault<TKey, TValue>(this IReadOnlyDictionary<TKey, TValue> dictionary, TKey key, TValue defaultValue) | 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 | #endif | ||||
| internal static TValue GetValueOrDefaultImpl<TKey, TValue>(IReadOnlyDictionary<TKey, TValue> dictionary, TKey key, TValue defaultValue) | |||||
| { | |||||
| return dictionary.TryGetValue(key, out var value) ? value : defaultValue; | |||||
| } | |||||
| } | } | ||||
| } | } | ||||
| @@ -9,6 +9,20 @@ internal static class EncodingExtensions | |||||
| #if NETSTANDARD2_0 | #if NETSTANDARD2_0 | ||||
| public static int GetChars(this Encoding encoding, ReadOnlySpan<byte> bytes, Span<char> output) | 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 | unsafe | ||||
| { | { | ||||
| fixed (byte* bytePtr = bytes) | fixed (byte* bytePtr = bytes) | ||||
| @@ -19,7 +33,7 @@ internal static class EncodingExtensions | |||||
| } | } | ||||
| } | } | ||||
| public static int GetCharCount(this Encoding encoding, ReadOnlySpan<byte> bytes) | |||||
| internal static int GetCharCountImpl(Encoding encoding, ReadOnlySpan<byte> bytes) | |||||
| { | { | ||||
| unsafe | unsafe | ||||
| { | { | ||||
| @@ -29,5 +43,4 @@ internal static class EncodingExtensions | |||||
| } | } | ||||
| } | } | ||||
| } | } | ||||
| #endif | |||||
| } | } | ||||
| @@ -7,15 +7,20 @@ namespace LLama.Extensions | |||||
| { | { | ||||
| #if NETSTANDARD2_0 | #if NETSTANDARD2_0 | ||||
| public static IEnumerable<T> TakeLast<T>(this IEnumerable<T> source, int count) | 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(); | var list = source.ToList(); | ||||
| if (count >= list.Count) | if (count >= list.Count) | ||||
| return list; | return list; | ||||
| list.RemoveRange(0, list.Count - count); | list.RemoveRange(0, list.Count - count); | ||||
| return list; | return list; | ||||
| } | } | ||||
| #endif | |||||
| } | } | ||||
| } | } | ||||
| @@ -1,14 +0,0 @@ | |||||
| using System; | |||||
| using System.Collections.Generic; | |||||
| namespace LLama.Extensions | |||||
| { | |||||
| internal static class ListExtensions | |||||
| { | |||||
| public static void AddRangeSpan<T>(this List<T> list, ReadOnlySpan<T> span) | |||||
| { | |||||
| for (var i = 0; i < span.Length; i++) | |||||
| list.Add(span[i]); | |||||
| } | |||||
| } | |||||
| } | |||||