Exemplo n.º 1
0
        public void Select_SourceIsArray_First()
        {
            var source = new[] { 1, 2, 3, 4 }.Select(i => i * 2);
            Assert.Equal(2, source.First());
            Assert.Equal(2, source.FirstOrDefault());

            Assert.Equal(6, source.Skip(2).First());
            Assert.Equal(6, source.Skip(2).FirstOrDefault());
            Assert.Throws<InvalidOperationException>(() => source.Skip(4).First());
            Assert.Throws<InvalidOperationException>(() => source.Skip(14).First());
            Assert.Equal(0, source.Skip(4).FirstOrDefault());
            Assert.Equal(0, source.Skip(14).FirstOrDefault());

            var empty = new int[0].Select(i => i * 2);
            Assert.Throws<InvalidOperationException>(() => empty.First());
            Assert.Equal(0, empty.FirstOrDefault());
        }
Exemplo n.º 2
0
        public void Select_SourceIsArray_ElementAtOrDefault()
        {
            var source = new[] { 1, 2, 3, 4 }.Select(i => i * 2);
            for (int i = 0; i != 4; ++i)
                Assert.Equal(i * 2 + 2, source.ElementAtOrDefault(i));
            Assert.Equal(0, source.ElementAtOrDefault(-1));
            Assert.Equal(0, source.ElementAtOrDefault(4));
            Assert.Equal(0, source.ElementAtOrDefault(40));

            Assert.Equal(6, source.Skip(1).ElementAtOrDefault(1));
            Assert.Equal(0, source.Skip(2).ElementAtOrDefault(9));
        }
Exemplo n.º 3
0
 public void Select_SourceIsArray_Take()
 {
     var source = new[] { 1, 2, 3, 4 }.Select(i => i * 2);
     Assert.Equal(new[] { 2, 4 }, source.Take(2));
     Assert.Equal(new[] { 2, 4 }, source.Take(3).Take(2));
     Assert.Empty(source.Take(-1));
     Assert.Equal(new[] { 2, 4, 6, 8 }, source.Take(4));
     Assert.Equal(new[] { 2, 4, 6, 8 }, source.Take(40));
     Assert.Equal(new[] { 2 }, source.Take(1));
     Assert.Equal(new[] { 4 }, source.Skip(1).Take(1));
     Assert.Equal(new[] { 6 }, source.Take(3).Skip(2));
     Assert.Equal(new[] { 2 }, source.Take(3).Take(1));
 }
Exemplo n.º 4
0
        public void Select_SourceIsArray_ElementAt()
        {
            var source = new[] { 1, 2, 3, 4 }.Select(i => i * 2);
            for (int i = 0; i != 4; ++i)
                Assert.Equal(i * 2 + 2, source.ElementAt(i));
            Assert.Throws<ArgumentOutOfRangeException>("index", () => source.ElementAt(-1));
            Assert.Throws<ArgumentOutOfRangeException>("index", () => source.ElementAt(4));
            Assert.Throws<ArgumentOutOfRangeException>("index", () => source.ElementAt(40));

            Assert.Equal(6, source.Skip(1).ElementAt(1));
            Assert.Throws<ArgumentOutOfRangeException>("index", () => source.Skip(2).ElementAt(9));
        }
Exemplo n.º 5
0
 public void Select_SourceIsArray_Skip()
 {
     var source = new[] { 1, 2, 3, 4 }.Select(i => i * 2);
     Assert.Equal(new[] { 6, 8 }, source.Skip(2));
     Assert.Equal(new[] { 6, 8 }, source.Skip(2).Skip(-1));
     Assert.Equal(new[] { 6, 8 }, source.Skip(1).Skip(1));
     Assert.Equal(new[] { 2, 4, 6, 8 }, source.Skip(-1));
     Assert.Empty(source.Skip(4));
     Assert.Empty(source.Skip(20));
 }