2
0
Эх сурвалжийг харах

Add tests for alpha numeric sorting

Bond_009 5 жил өмнө
parent
commit
ada3f96668

+ 12 - 1
MediaBrowser.Common/Extensions/ShuffleExtensions.cs

@@ -16,12 +16,23 @@ namespace MediaBrowser.Common.Extensions
         /// <param name="list">The list that should get shuffled.</param>
         /// <typeparam name="T">The type.</typeparam>
         public static void Shuffle<T>(this IList<T> list)
+        {
+            list.Shuffle(_rng);
+        }
+
+        /// <summary>
+        /// Shuffles the items in a list.
+        /// </summary>
+        /// <param name="list">The list that should get shuffled.</param>
+        /// <param name="rng">The random number generator to use.</param>
+        /// <typeparam name="T">The type.</typeparam>
+        public static void Shuffle<T>(this IList<T> list, Random rng)
         {
             int n = list.Count;
             while (n > 1)
             {
                 n--;
-                int k = _rng.Next(n + 1);
+                int k = rng.Next(n + 1);
                 T value = list[k];
                 list[k] = list[n];
                 list[n] = value;

+ 7 - 0
MediaBrowser.sln

@@ -60,6 +60,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Jellyfin.Api.Tests", "tests
 EndProject
 Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Jellyfin.Server.Implementations.Tests", "tests\Jellyfin.Server.Implementations.Tests\Jellyfin.Server.Implementations.Tests.csproj", "{2E3A1B4B-4225-4AAA-8B29-0181A84E7AEE}"
 EndProject
+Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Jellyfin.Controller.Tests", "tests\Jellyfin.Controller.Tests\Jellyfin.Controller.Tests.csproj", "{462584F7-5023-4019-9EAC-B98CA458C0A0}"
+EndProject
 Global
 	GlobalSection(SolutionConfigurationPlatforms) = preSolution
 		Debug|Any CPU = Debug|Any CPU
@@ -170,6 +172,10 @@ Global
 		{2E3A1B4B-4225-4AAA-8B29-0181A84E7AEE}.Debug|Any CPU.Build.0 = Debug|Any CPU
 		{2E3A1B4B-4225-4AAA-8B29-0181A84E7AEE}.Release|Any CPU.ActiveCfg = Release|Any CPU
 		{2E3A1B4B-4225-4AAA-8B29-0181A84E7AEE}.Release|Any CPU.Build.0 = Release|Any CPU
+		{462584F7-5023-4019-9EAC-B98CA458C0A0}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
+		{462584F7-5023-4019-9EAC-B98CA458C0A0}.Debug|Any CPU.Build.0 = Debug|Any CPU
+		{462584F7-5023-4019-9EAC-B98CA458C0A0}.Release|Any CPU.ActiveCfg = Release|Any CPU
+		{462584F7-5023-4019-9EAC-B98CA458C0A0}.Release|Any CPU.Build.0 = Release|Any CPU
 	EndGlobalSection
 	GlobalSection(SolutionProperties) = preSolution
 		HideSolutionNode = FALSE
@@ -201,5 +207,6 @@ Global
 		{3998657B-1CCC-49DD-A19F-275DC8495F57} = {FBBB5129-006E-4AD7-BAD5-8B7CA1D10ED6}
 		{A2FD0A10-8F62-4F9D-B171-FFDF9F0AFA9D} = {FBBB5129-006E-4AD7-BAD5-8B7CA1D10ED6}
 		{2E3A1B4B-4225-4AAA-8B29-0181A84E7AEE} = {FBBB5129-006E-4AD7-BAD5-8B7CA1D10ED6}
+		{462584F7-5023-4019-9EAC-B98CA458C0A0} = {FBBB5129-006E-4AD7-BAD5-8B7CA1D10ED6}
 	EndGlobalSection
 EndGlobal

+ 43 - 0
tests/Jellyfin.Controller.Tests/AlphanumComparatorTests.cs

@@ -0,0 +1,43 @@
+using System;
+using System.Linq;
+using MediaBrowser.Common.Extensions;
+using MediaBrowser.Controller.Sorting;
+using Xunit;
+
+namespace Jellyfin.Controller.Tests
+{
+    public class AlphanumComparatorTests
+    {
+        private readonly Random _rng = new Random(42);
+
+        [Theory]
+        [InlineData(null, "", "1", "9", "10", "a", "z")]
+        [InlineData("50F", "100F", "SR9", "SR100")]
+        [InlineData("image-1.jpg", "image-02.jpg", "image-4.jpg", "image-9.jpg", "image-10.jpg", "image-11.jpg", "image-22.jpg")]
+        [InlineData("Hard drive 2GB", "Hard drive 20GB")]
+        [InlineData("b", "e", "è", "ě", "f", "g", "k")]
+        [InlineData("123456789", "123456789a", "abc", "abcd")]
+        [InlineData("12345678912345678912345678913234567891", "123456789123456789123456789132345678912")]
+        [InlineData("12345678912345678912345678913234567891", "12345678912345678912345678913234567891")]
+        [InlineData("12345678912345678912345678913234567891", "12345678912345678912345678913234567892")]
+        [InlineData("12345678912345678912345678913234567891a", "12345678912345678912345678913234567891a")]
+        [InlineData("12345678912345678912345678913234567891a", "12345678912345678912345678913234567891b")]
+        public void AlphanumComparatorTest(params string?[] strings)
+        {
+            var copy = (string?[])strings.Clone();
+            if (strings.Length == 2)
+            {
+                var tmp = copy[0];
+                copy[0] = copy[1];
+                copy[1] = tmp;
+            }
+            else
+            {
+                copy.Shuffle(_rng);
+            }
+
+            Array.Sort(copy, new AlphanumComparator());
+            Assert.True(strings.SequenceEqual(copy));
+        }
+    }
+}

+ 21 - 0
tests/Jellyfin.Controller.Tests/Jellyfin.Controller.Tests.csproj

@@ -0,0 +1,21 @@
+<Project Sdk="Microsoft.NET.Sdk">
+
+  <PropertyGroup>
+    <TargetFramework>netcoreapp3.1</TargetFramework>
+    <IsPackable>false</IsPackable>
+    <TreatWarningsAsErrors>true</TreatWarningsAsErrors>
+    <Nullable>enable</Nullable>
+  </PropertyGroup>
+
+  <ItemGroup>
+    <PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.4.0" />
+    <PackageReference Include="xunit" Version="2.4.1" />
+    <PackageReference Include="xunit.runner.visualstudio" Version="2.4.1" />
+    <PackageReference Include="coverlet.collector" Version="1.2.0" />
+  </ItemGroup>
+
+  <ItemGroup>
+    <ProjectReference Include="../../MediaBrowser.Controller/MediaBrowser.Controller.csproj" />
+  </ItemGroup>
+
+</Project>