コード例 #1
0
        public void When_creating_a_sub_array()
        {
            var srcArray = new[] { 1, 2, 3, 4, 5 };

            var emptySub = new SubArray <int>(srcArray, 4, 0);

            emptySub.ShouldBeEmpty();
            emptySub.Segment.Array.ShouldBe(srcArray);
            emptySub.Segment.Offset.ShouldBe(4);
            emptySub.Segment.ShouldBeEmpty();

            var subOne = new SubArray <int>(srcArray, 0, 2);

            subOne.Length.ShouldBe(2);
            subOne[0].ShouldBe(1);
            subOne[1].ShouldBe(2);

            subOne.Segment.Array.ShouldBe(srcArray);
            subOne.Segment.Offset.ShouldBe(0);
            subOne.Segment.Count.ShouldBe(subOne.Length);

            var subTwo = new SubArray <int>(srcArray, 1, 3);

            subTwo.Length.ShouldBe(3);
            subTwo[0].ShouldBe(2);
            subTwo[1].ShouldBe(3);
            subTwo[2].ShouldBe(4);

            subTwo.Segment.Array.ShouldBe(srcArray);
            subTwo.Segment.Offset.ShouldBe(1);
            subTwo.Segment.Count.ShouldBe(subTwo.Length);

            // ReSharper disable once ObjectCreationAsStatement
            Should.Throw <ArgumentException>(() => new SubArray <int>(srcArray, 3, 5))
            .Message.ShouldBe("Offset and length were out of bounds for the array or count is greater than the number of elements from index to the end of the source collection.");

            var subThree = new SubArray <int>(srcArray, 1, 4);

            subThree.Length.ShouldBe(4);
            subThree[0].ShouldBe(2);
            subThree[1].ShouldBe(3);
            subThree[2].ShouldBe(4);
            subThree[3].ShouldBe(5);

            subThree.Segment.Array.ShouldBe(srcArray);
            subThree.Segment.Offset.ShouldBe(1);
            subThree.Segment.Count.ShouldBe(subThree.Length);
        }