Browse Source

Merge pull request #179 from martindevans/extensions_tests

Extensions Method Unit Tests
tags/v0.6.0
Martin Evans GitHub 2 years ago
parent
commit
54b3eb0223
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 179 additions and 22 deletions
  1. +38
    -0
      LLama.Unittest/DictionaryExtensionsTests.cs
  2. +37
    -0
      LLama.Unittest/EncodingExtensionsTests.cs
  3. +36
    -0
      LLama.Unittest/IEnumerableExtensionsTests.cs
  4. +22
    -0
      LLama.Unittest/IReadOnlyListExtensionsTests.cs
  5. +15
    -0
      LLama.Unittest/KeyValuePairExtensionsTests.cs
  6. +3
    -3
      LLama.Unittest/TokenTests.cs
  7. +6
    -1
      LLama/Extensions/DictionaryExtensions.cs
  8. +15
    -2
      LLama/Extensions/EncodingExtensions.cs
  9. +7
    -2
      LLama/Extensions/IEnumerableExtensions.cs
  10. +0
    -14
      LLama/Extensions/ListExtensions.cs

+ 38
- 0
LLama.Unittest/DictionaryExtensionsTests.cs View File

@@ -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
- 0
LLama.Unittest/EncodingExtensionsTests.cs View File

@@ -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
- 0
LLama.Unittest/IEnumerableExtensionsTests.cs View File

@@ -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
- 0
LLama.Unittest/IReadOnlyListExtensionsTests.cs View File

@@ -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
- 0
LLama.Unittest/KeyValuePairExtensionsTests.cs View File

@@ -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);
}
}

+ 3
- 3
LLama.Unittest/TokenTests.cs View File

@@ -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);
} }
} }

+ 6
- 1
LLama/Extensions/DictionaryExtensions.cs View File

@@ -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;
}
} }
} }

+ 15
- 2
LLama/Extensions/EncodingExtensions.cs View File

@@ -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
- 2
LLama/Extensions/IEnumerableExtensions.cs View File

@@ -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
} }
} }

+ 0
- 14
LLama/Extensions/ListExtensions.cs View File

@@ -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]);
}
}
}

Loading…
Cancel
Save