Created
January 23, 2015 22:24
-
-
Save JohanLarsson/084b4a1ed5ee65feab6b to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Linx | |
{ | |
[Test] | |
public void Select() | |
{ | |
var ints = new[] { 1, 2, 3 }; | |
var squares = ints.Select(x => x * x); | |
CollectionAssert.AreEqual(new[] { 1, 4, 9 }, squares); | |
} | |
[Test] | |
public void Any() | |
{ | |
var ints = new[] { 1, 2, 3 }; | |
Assert.IsTrue(ints.Any(x => x > 2)); | |
} | |
[Test] | |
public void All() | |
{ | |
var ints = new[] { 1, 2, 3 }; | |
Assert.IsTrue(ints.All(x => x < 4)); | |
} | |
[Test] | |
public void Single() | |
{ | |
var ints = new[] { 1, 2, 3 }; | |
Assert.AreEqual(3, ints.Single(x => x == 3)); | |
Assert.Throws<InvalidOperationException>(() => ints.Single(x => x > 0)); | |
} | |
[Test] | |
public void SingleOrDefault() | |
{ | |
var ints = new[] { 1, 2, 3 }; | |
Assert.AreEqual(3, ints.SingleOrDefault(x => x == 3)); | |
Assert.Throws<InvalidOperationException>(() => ints.SingleOrDefault(x => x > 0)); | |
Assert.AreEqual(0, ints.SingleOrDefault(x => x == 4)); | |
} | |
[Test] | |
public void First() | |
{ | |
var ints = new[] { 1, 2, 3 }; | |
Assert.AreEqual(2, ints.First(x => x > 1)); | |
Assert.Throws<InvalidOperationException>(() => ints.First(x => x < 0)); | |
} | |
[Test] | |
public void FirstOrDefault() | |
{ | |
var ints = new[] { 1, 2, 3 }; | |
Assert.AreEqual(2, ints.FirstOrDefault(x => x > 1)); | |
Assert.AreEqual(0, ints.FirstOrDefault(x => x < 0)); | |
} | |
[Test] | |
public void Sum() | |
{ | |
var ints = new[] { 1, 2, 3 }; | |
Assert.AreEqual(6, ints.Sum()); | |
} | |
[Test] | |
public void Average() | |
{ | |
var ints = new[] { 1, 2, 3 }; | |
Assert.AreEqual(2, ints.Average()); | |
} | |
[Test] | |
public void Chained() | |
{ | |
var ints = new[] { 1, 2, 3 }; | |
var squares = ints.Where(x => x % 2 != 0) | |
.Select(x => x * x) | |
.OrderByDescending(x => x); | |
CollectionAssert.AreEqual(new[] { 9, 1 }, squares); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment