OSDN Git Service

検索対象のインデックス番号の生成を MyCommon.CircularCount{Up,Down}() メソッドに分離
authorKimura Youichi <kim.upsilon@bucyou.net>
Sat, 12 Aug 2017 18:29:55 +0000 (03:29 +0900)
committerKimura Youichi <kim.upsilon@bucyou.net>
Sat, 12 Aug 2017 18:29:55 +0000 (03:29 +0900)
OpenTween.Tests/MyCommonTest.cs
OpenTween/Models/TabModel.cs
OpenTween/MyCommon.cs

index a78bb1e..81e67de 100644 (file)
@@ -322,5 +322,37 @@ namespace OpenTween
 
             Assert.Empty(actual);
         }
+
+        [Fact]
+        public void CircularCountUp_Test()
+        {
+            var actual = MyCommon.CircularCountUp(length: 6, startIndex: 3);
+
+            Assert.Equal(new[] { 3, 4, 5, 0, 1, 2 }, actual);
+        }
+
+        [Fact]
+        public void CircularCountUp_StartFromZeroTest()
+        {
+            var actual = MyCommon.CircularCountUp(length: 6, startIndex: 0);
+
+            Assert.Equal(new[] { 0, 1, 2, 3, 4, 5 }, actual);
+        }
+
+        [Fact]
+        public void CircularCountDown_Test()
+        {
+            var actual = MyCommon.CircularCountDown(length: 6, startIndex: 3);
+
+            Assert.Equal(new[] { 3, 2, 1, 0, 5, 4 }, actual);
+        }
+
+        [Fact]
+        public void CircularCountDown_StartFromLastIndexTest()
+        {
+            var actual = MyCommon.CircularCountDown(length: 6, startIndex: 5);
+
+            Assert.Equal(new[] { 5, 4, 3, 2, 1, 0 }, actual);
+        }
     }
 }
index 89c912c..37cb512 100644 (file)
@@ -393,28 +393,12 @@ namespace OpenTween.Models
             if (this.AllCount == 0)
                 yield break;
 
-            var searchIndices = Enumerable.Empty<int>();
+            IEnumerable<int> searchIndices;
 
             if (!reverse)
-            {
-                // startindex ...末尾
-                if (startIndex != this.AllCount - 1)
-                    searchIndices = MyCommon.CountUp(startIndex, this.AllCount - 1);
-
-                // 先頭 ... (startIndex - 1)
-                if (startIndex != 0)
-                    searchIndices = searchIndices.Concat(MyCommon.CountUp(0, startIndex - 1));
-            }
+                searchIndices = MyCommon.CircularCountUp(this.AllCount, startIndex);
             else
-            {
-                // startIndex ... 先頭
-                if (startIndex != 0)
-                    searchIndices = MyCommon.CountDown(startIndex, 0);
-
-                // 末尾 ... (startIndex + 1)
-                if (startIndex != this.AllCount - 1)
-                    searchIndices = searchIndices.Concat(MyCommon.CountDown(this.AllCount - 1, startIndex + 1));
-            }
+                searchIndices = MyCommon.CircularCountDown(this.AllCount, startIndex);
 
             foreach (var index in searchIndices)
             {
index 14f1ad8..1ed1596 100644 (file)
@@ -1071,6 +1071,40 @@ namespace OpenTween
                 yield return i;
         }
 
+        public static IEnumerable<int> CircularCountUp(int length, int startIndex)
+        {
+            if (length < 1)
+                throw new ArgumentOutOfRangeException(nameof(length));
+            if (startIndex < 0 || startIndex >= length)
+                throw new ArgumentOutOfRangeException(nameof(startIndex));
+
+            // startindex ... 末尾
+            var indices = MyCommon.CountUp(startIndex, length - 1);
+
+            // 先頭 ... (startIndex - 1)
+            if (startIndex != 0)
+                indices = indices.Concat(MyCommon.CountUp(0, startIndex - 1));
+
+            return indices;
+        }
+
+        public static IEnumerable<int> CircularCountDown(int length, int startIndex)
+        {
+            if (length < 1)
+                throw new ArgumentOutOfRangeException(nameof(length));
+            if (startIndex < 0 || startIndex >= length)
+                throw new ArgumentOutOfRangeException(nameof(startIndex));
+
+            // startIndex ... 先頭
+            var indices = MyCommon.CountDown(startIndex, 0);
+
+            // 末尾 ... (startIndex + 1)
+            if (startIndex != length - 1)
+                indices = indices.Concat(MyCommon.CountDown(length - 1, startIndex + 1));
+
+            return indices;
+        }
+
         /// <summary>
         /// 2バイト文字も考慮したUrlエンコード
         /// </summary>