public void RemoveAtTest() { try { int[] removeAt = Enumerable.Range(0, 5).RemoveAt(-1).ToArray(); Assert.Fail(string.Join(", ", removeAt.Select(int32 => int32.ToString()))); } catch (ArgumentOutOfRangeException exception) { Assert.AreEqual("index", exception.ParamName); } EnumerableAssert.AreSequentialEqual(new int[] { 1, 2, 3, 4 }, Enumerable.Range(0, 5).RemoveAt(0)); EnumerableAssert.AreSequentialEqual(new int[] { 0, 2, 3, 4 }, Enumerable.Range(0, 5).RemoveAt(1)); EnumerableAssert.AreSequentialEqual(new int[] { 0, 1, 3, 4 }, Enumerable.Range(0, 5).RemoveAt(2)); EnumerableAssert.AreSequentialEqual(new int[] { 0, 1, 2, 4 }, Enumerable.Range(0, 5).RemoveAt(3)); EnumerableAssert.AreSequentialEqual(new int[] { 0, 1, 2, 3 }, Enumerable.Range(0, 5).RemoveAt(4)); try { int[] removeAt = Enumerable.Range(0, 5).RemoveAt(5).ToArray(); Assert.Fail(string.Join(", ", removeAt.Select(int32 => int32.ToString()))); } catch (ArgumentOutOfRangeException exception) { Assert.AreEqual("index", exception.ParamName); } try { int[] removeAt = Enumerable.Range(0, 5).RemoveAt(6).ToArray(); Assert.Fail(string.Join(", ", removeAt.Select(int32 => int32.ToString()))); } catch (ArgumentOutOfRangeException exception) { Assert.AreEqual("index", exception.ParamName); } }
public void EnumerableTest() { bool isExecuted1 = false; Func <int, int> addOne = x => { isExecuted1 = true; return(x + 1); }; IEnumerable <int> numbers = new int[] { 0, 1, 2 }; IEnumerable <int> query = addOne.Enumerable().Apply(numbers); Assert.IsFalse(isExecuted1); // Deferred and lazy. EnumerableAssert.AreSequentialEqual(new int[] { 1, 2, 3 }, query); // Execution. Assert.IsTrue(isExecuted1); // f.Functor().Apply(F) == F.Select(f) EnumerableAssert.AreSequentialEqual(addOne.Enumerable().Apply(numbers), numbers.Select(addOne)); // id.Functor().Apply(F) == F Func <int, int> id = Functions.Id; EnumerableAssert.AreSequentialEqual(id.Enumerable().Apply(numbers), numbers); // o.Functor().Apply(F1).Apply(F2).Apply(F3) == F1.Apply(F2.Apply(F3)) Func <int, int> addTwo = x => x + 2; EnumerableAssert.AreSequentialEqual( Functions <int, int, int> .o.Enumerable().Apply(addOne.Enumerable()).Apply(addTwo.Enumerable()).Apply(numbers), addOne.Enumerable().Apply(addTwo.Enumerable().Apply(numbers))); // f.Functor().Apply(a.Functor()) == f(a).Functor() EnumerableAssert.AreSequentialEqual(addOne.Enumerable().Apply(1.Enumerable()), addOne(1).Enumerable()); // F.Apply(a.Functor()) == (f => f(a)).Functor().Apply(F) EnumerableAssert.AreSequentialEqual( addOne.Enumerable().Apply(1.Enumerable()), new Func <Func <int, int>, int>(f => f(1)).Enumerable().Apply(addOne.Enumerable())); }
public void ExceptTest() { int[] first = new int[] { 0, 1, 2 }; int[] second = new int[] { 3, 4, 5 }; EnumerableAssert.AreSequentialEqual( Enumerable.Except(first, second), EnumerableExtensions.Except(first, second, EqualityComparer <int> .Default)); first = new int[] { 0, 1, 2 }; second = new int[] { }; EnumerableAssert.AreSequentialEqual( Enumerable.Except(first, second), EnumerableExtensions.Except(first, second, EqualityComparer <int> .Default)); first = new int[] { }; second = new int[] { 3, 4, 5 }; EnumerableAssert.AreSequentialEqual( Enumerable.Except(first, second), EnumerableExtensions.Except(first, second, EqualityComparer <int> .Default)); first = new int[] { 0, 1, 2 }; second = new int[] { 2, 3, 4 }; EnumerableAssert.AreSequentialEqual( Enumerable.Except(first, second), EnumerableExtensions.Except(first, second, EqualityComparer <int> .Default)); }
public void InsertTest() { try { int[] insert = Enumerable.Range(0, 5).Insert(-1, 5).ToArray(); Assert.Fail(string.Join(", ", insert.Select(int32 => int32.ToString()))); } catch (ArgumentOutOfRangeException exception) { Assert.AreEqual("index", exception.ParamName); } EnumerableAssert.AreSequentialEqual(new int[] { 5, 0, 1, 2, 3, 4 }, Enumerable.Range(0, 5).Insert(0, 5)); EnumerableAssert.AreSequentialEqual(new int[] { 0, 5, 1, 2, 3, 4 }, Enumerable.Range(0, 5).Insert(1, 5)); EnumerableAssert.AreSequentialEqual(new int[] { 0, 1, 5, 2, 3, 4 }, Enumerable.Range(0, 5).Insert(2, 5)); EnumerableAssert.AreSequentialEqual(new int[] { 0, 1, 2, 5, 3, 4 }, Enumerable.Range(0, 5).Insert(3, 5)); EnumerableAssert.AreSequentialEqual(new int[] { 0, 1, 2, 3, 5, 4 }, Enumerable.Range(0, 5).Insert(4, 5)); EnumerableAssert.AreSequentialEqual(new int[] { 0, 1, 2, 3, 4, 5 }, Enumerable.Range(0, 5).Insert(5, 5)); try { int[] insert = Enumerable.Range(0, 5).Insert(6, 5).ToArray(); Assert.Fail(string.Join(", ", insert.Select(int32 => int32.ToString()))); } catch (ArgumentOutOfRangeException exception) { Assert.AreEqual("index", exception.ParamName); } }
public void StateMachineTest() { IEnumerable <int> expected = Enumerable.Range(0, 3).Push(4).Value2.Pop().Value2; IEnumerable <int> actual = StateQuery.Stack(); EnumerableAssert.AreSequentialEqual(expected, actual); }
public void EnumerableCSharpTest() { bool isExecuted1 = false; IEnumerable <int> enumerable = new int[] { 0, 1, 2 }; Func <int, int> f1 = x => { isExecuted1 = true; return(x + 1); }; IEnumerable <int> query1 = from x in enumerable select f1(x); Assert.IsFalse(isExecuted1); // Deferred and lazy. EnumerableAssert.AreSequentialEqual(new int[] { 1, 2, 3 }, query1); // Execution. Assert.IsTrue(isExecuted1); // Functor law 1: F.Select(Id) == Id(F) EnumerableAssert.AreSequentialEqual(enumerable.Select(Functions.Id), Functions.Id(enumerable)); // Functor law 2: F.Select(f2.o(f1)) == F.Select(f1).Select(f2) Func <int, string> f2 = x => (x + 2).ToString(CultureInfo.InvariantCulture); EnumerableAssert.AreSequentialEqual( enumerable.Select(f2.o(f1)), enumerable.Select(f1).Select(f2)); // Functor law 2: F.Select(f2.o(f1)) == F.Select(f1).Select(f2) EnumerableAssert.AreSequentialEqual( from value in enumerable select f2.o(f1)(value), from value in enumerable select f1(value) into value select f2(value)); }
public void TakeTest() { int[] enumerable = new int[] { 0, 1, 2 }; EnumerableAssert.AreSequentialEqual( Enumerable.Take(enumerable, 2), EnumerableExtensions.Take(enumerable, 2)); enumerable = new int[] { 0, 1, 1, 1, 2, 2 }; EnumerableAssert.AreSequentialEqual( Enumerable.Take(enumerable, 0), EnumerableExtensions.Take(enumerable, 0)); enumerable = new int[] { 0, 1, 1, 1, 2, 2 }; EnumerableAssert.AreSequentialEqual( Enumerable.Take(enumerable, -1), EnumerableExtensions.Take(enumerable, -1)); enumerable = new int[] { 0, 1, 1, 1, 2, 2 }; EnumerableAssert.AreSequentialEqual( Enumerable.Take(enumerable, 100), EnumerableExtensions.Take(enumerable, 100)); enumerable = new int[] { }; EnumerableAssert.AreSequentialEqual( Enumerable.Take(enumerable, 100), EnumerableExtensions.Take(enumerable, 100)); }
public void EnumerableTest() { bool isExecuted1 = false; IEnumerable <int> enumerable1 = new int[] { 0, 1 }; IEnumerable <int> enumerable2 = new int[] { 1, 2 }; Func <int, Func <int, int> > f = x => y => { isExecuted1 = true; return(x + y); }; IEnumerable <int> query1 = from x in enumerable1 from y in enumerable2 let z = f(x)(y) where z > 1 select z; Assert.IsFalse(isExecuted1); // Deferred and lazy. EnumerableAssert.AreSequentialEqual(new int[] { 2, 2, 3 }, query1); // Execution. Assert.IsTrue(isExecuted1); // Monad law 1: m.Monad().SelectMany(f) == f(m) Func <int, IEnumerable <int> > addOne = x => (x + 1).Enumerable(); EnumerableAssert.AreSequentialEqual(1.Enumerable().SelectMany(addOne), addOne(1)); // Monad law 2: M.SelectMany(Monad) == M EnumerableAssert.AreSequentialEqual(enumerable1.SelectMany(EnumerableExtensions.Enumerable), enumerable1); // Monad law 3: M.SelectMany(f1).SelectMany(f2) == M.SelectMany(x => f1(x).SelectMany(f2)) Func <int, IEnumerable <int> > addTwo = x => (x + 2).Enumerable(); EnumerableAssert.AreSequentialEqual( enumerable2.SelectMany(addOne).SelectMany(addTwo), enumerable2.SelectMany(x => addOne(x).SelectMany(addTwo))); }
public void SkipTest() { int[] enumerable = new int[] { 0, 1, 2 }; EnumerableAssert.AreSequentialEqual( Enumerable.Skip(enumerable, 2), EnumerableMonadExtensions.Skip(enumerable, 2)); enumerable = new int[] { 0, 1, 1, 1, 2, 2 }; EnumerableAssert.AreSequentialEqual( Enumerable.Skip(enumerable, 0), EnumerableMonadExtensions.Skip(enumerable, 0)); enumerable = new int[] { 0, 1, 1, 1, 2, 2 }; EnumerableAssert.AreSequentialEqual( Enumerable.Skip(enumerable, -1), EnumerableMonadExtensions.Skip(enumerable, -1)); enumerable = new int[] { 0, 1, 1, 1, 2, 2 }; EnumerableAssert.AreSequentialEqual( Enumerable.Skip(enumerable, 100), EnumerableMonadExtensions.Skip(enumerable, 100)); enumerable = new int[] { }; EnumerableAssert.AreSequentialEqual( Enumerable.Skip(enumerable, 100), EnumerableMonadExtensions.Skip(enumerable, 100)); }
public void DynamicPartitionerTest() { Partitioning.QueryDynamicPartitioner(); int partitionCount = Environment.ProcessorCount * 2; int valueCount = partitionCount * 10000; IEnumerable <int> source = Enumerable.Range(1, valueCount); IEnumerable <int> partitionsSource = new Partitioning.DynamicPartitioner <int>(source).GetDynamicPartitions(); IEnumerable <int> values = GetPartitions(partitionsSource, partitionCount).Concat().OrderBy(value => value); EnumerableAssert.AreSequentialEqual(source, values); }
public void RemoveTest() { EnumerableAssert.AreSequentialEqual(new int[] { 0, 1, 2, 3, 4 }, Enumerable.Range(0, 5).Remove(-1)); EnumerableAssert.AreSequentialEqual(new int[] { 1, 2, 3, 4 }, Enumerable.Range(0, 5).Remove(0)); EnumerableAssert.AreSequentialEqual(new int[] { 0, 2, 3, 4 }, Enumerable.Range(0, 5).Remove(1)); EnumerableAssert.AreSequentialEqual(new int[] { 0, 1, 3, 4 }, Enumerable.Range(0, 5).Remove(2)); EnumerableAssert.AreSequentialEqual(new int[] { 0, 1, 2, 4 }, Enumerable.Range(0, 5).Remove(3)); EnumerableAssert.AreSequentialEqual(new int[] { 0, 1, 2, 3 }, Enumerable.Range(0, 5).Remove(4)); EnumerableAssert.AreSequentialEqual(new int[] { 0, 1, 2, 3, 4 }, Enumerable.Range(0, 5).Remove(5)); EnumerableAssert.AreSequentialEqual(new int[] { 0, 1, 2, 3, 4 }, Enumerable.Range(0, 5).Remove(6)); }
public void DistinctTest() { int[] enumerable = new int[] { 0, 1, 2 }; EnumerableAssert.AreSequentialEqual( Enumerable.Distinct(enumerable), EnumerableExtensions.Distinct(enumerable, EqualityComparer <int> .Default)); enumerable = new int[] { 0, 1, 1, 1, 2, 2 }; EnumerableAssert.AreSequentialEqual( Enumerable.Distinct(enumerable), EnumerableExtensions.Distinct(enumerable, EqualityComparer <int> .Default)); }
public void Reverse2Test() { int[] enumerable = new int[] { 0, 1, 2 }; EnumerableAssert.AreSequentialEqual( Enumerable.Reverse(enumerable), DeferredExecution.CompiledReverseGenerator(enumerable)); enumerable = new int[] { }; EnumerableAssert.AreSequentialEqual( Enumerable.Reverse(enumerable), DeferredExecution.CompiledReverseGenerator(enumerable)); }
public void WhereTest() { int[] enumerable = new int[] { 0, 1, 2 }; EnumerableAssert.AreSequentialEqual( Enumerable.Where(enumerable, x => x > 0), EnumerableExtensions.Where(enumerable, x => x > 0)); enumerable = new int[] { }; EnumerableAssert.AreSequentialEqual( Enumerable.Where(enumerable, x => x > 0), EnumerableExtensions.Where(enumerable, x => x > 0)); }
public void SelectTest() { int[] enumerable = new int[] { 0, 1, 2 }; EnumerableAssert.AreSequentialEqual( Enumerable.Select(enumerable, x => x.ToString()), EnumerableExtensions.Select(enumerable, x => x.ToString())); enumerable = new int[] { }; EnumerableAssert.AreSequentialEqual( Enumerable.Select(enumerable, x => x.ToString()), EnumerableExtensions.Select(enumerable, x => x.ToString())); }
public void WhereTest() { int[] enumerable = new int[] { 0, 1, 2 }; EnumerableAssert.AreSequentialEqual( Enumerable.Where(enumerable, x => x > 0), IteratorPattern.Where(enumerable, x => x > 0)); enumerable = new int[] { }; EnumerableAssert.AreSequentialEqual( Enumerable.Where(enumerable, x => x > 0), IteratorPattern.Where(enumerable, x => x > 0)); }
public void SelectTest() { int[] enumerable = new int[] { 0, 1, 2 }; EnumerableAssert.AreSequentialEqual( Enumerable.Select(enumerable, x => x.ToString()), IteratorPattern.Select(enumerable, x => x.ToString())); enumerable = new int[] { }; EnumerableAssert.AreSequentialEqual( Enumerable.Select(enumerable, x => x.ToString()), IteratorPattern.Select(enumerable, x => x.ToString())); }
public void EnumerableTest() { EnumerableAssert.AreSequentialEqual( EnumerableEx.Return(1), IteratorPattern.FromValue(1)); object value = new object(); EnumerableAssert.AreSequentialEqual( EnumerableEx.Return(value), IteratorPattern.FromValue(value)); }
public void RemoveTest() { try { int[] remove = Enumerable.Range(0, 5).Remove(-1).ToArray(); Assert.Fail(string.Join(", ", remove.Select(int32 => int32.ToString()))); } catch (ArgumentOutOfRangeException exception) { Assert.AreEqual("remove", exception.ParamName); } EnumerableAssert.AreSequentialEqual(new int[] { 1, 2, 3, 4 }, Enumerable.Range(0, 5).Remove(0)); EnumerableAssert.AreSequentialEqual(new int[] { 0, 2, 3, 4 }, Enumerable.Range(0, 5).Remove(1)); EnumerableAssert.AreSequentialEqual(new int[] { 0, 1, 3, 4 }, Enumerable.Range(0, 5).Remove(2)); EnumerableAssert.AreSequentialEqual(new int[] { 0, 1, 2, 4 }, Enumerable.Range(0, 5).Remove(3)); EnumerableAssert.AreSequentialEqual(new int[] { 0, 1, 2, 3 }, Enumerable.Range(0, 5).Remove(4)); try { int[] remove = Enumerable.Range(0, 5).Remove(5).ToArray(); Assert.Fail(string.Join(", ", remove.Select(int32 => int32.ToString()))); } catch (ArgumentOutOfRangeException exception) { Assert.AreEqual("remove", exception.ParamName); } try { int[] remove = Enumerable.Range(0, 5).Remove(6).ToArray(); Assert.Fail(string.Join(", ", remove.Select(int32 => int32.ToString()))); } catch (ArgumentOutOfRangeException exception) { Assert.AreEqual("remove", exception.ParamName); } EnumerableAssert.AreSequentialEqual(new int[] { 0, 1, 2, 3, 4 }, Enumerable.Range(0, 5).Remove(-1, null, ListQueryMode.Normalize)); EnumerableAssert.AreSequentialEqual(new int[] { 1, 2, 3, 4 }, Enumerable.Range(0, 5).Remove(0, null, ListQueryMode.Normalize)); EnumerableAssert.AreSequentialEqual(new int[] { 0, 2, 3, 4 }, Enumerable.Range(0, 5).Remove(1, null, ListQueryMode.Normalize)); EnumerableAssert.AreSequentialEqual(new int[] { 0, 1, 3, 4 }, Enumerable.Range(0, 5).Remove(2, null, ListQueryMode.Normalize)); EnumerableAssert.AreSequentialEqual(new int[] { 0, 1, 2, 4 }, Enumerable.Range(0, 5).Remove(3, null, ListQueryMode.Normalize)); EnumerableAssert.AreSequentialEqual(new int[] { 0, 1, 2, 3 }, Enumerable.Range(0, 5).Remove(4, null, ListQueryMode.Normalize)); EnumerableAssert.AreSequentialEqual(new int[] { 0, 1, 2, 3, 4 }, Enumerable.Range(0, 5).Remove(5, null, ListQueryMode.Normalize)); EnumerableAssert.AreSequentialEqual(new int[] { 0, 1, 2, 3, 4 }, Enumerable.Range(0, 5).Remove(6, null, ListQueryMode.Normalize)); EnumerableAssert.AreSequentialEqual(new int[] { 0, 1, 2, 3, 4 }, Enumerable.Range(0, 5).Remove(-1, null, ListQueryMode.Ignore)); EnumerableAssert.AreSequentialEqual(new int[] { 1, 2, 3, 4 }, Enumerable.Range(0, 5).Remove(0, null, ListQueryMode.Ignore)); EnumerableAssert.AreSequentialEqual(new int[] { 0, 2, 3, 4 }, Enumerable.Range(0, 5).Remove(1, null, ListQueryMode.Ignore)); EnumerableAssert.AreSequentialEqual(new int[] { 0, 1, 3, 4 }, Enumerable.Range(0, 5).Remove(2, null, ListQueryMode.Ignore)); EnumerableAssert.AreSequentialEqual(new int[] { 0, 1, 2, 4 }, Enumerable.Range(0, 5).Remove(3, null, ListQueryMode.Ignore)); EnumerableAssert.AreSequentialEqual(new int[] { 0, 1, 2, 3 }, Enumerable.Range(0, 5).Remove(4, null, ListQueryMode.Ignore)); EnumerableAssert.AreSequentialEqual(new int[] { 0, 1, 2, 3, 4 }, Enumerable.Range(0, 5).Remove(5, null, ListQueryMode.Ignore)); EnumerableAssert.AreSequentialEqual(new int[] { 0, 1, 2, 3, 4 }, Enumerable.Range(0, 5).Remove(6, null, ListQueryMode.Ignore)); }
public void MultipleTest() { SinglyLinkedListNode <int> head = new SinglyLinkedListNode <int>(0, new SinglyLinkedListNode <int>(1, new SinglyLinkedListNode <int>(2, new SinglyLinkedListNode <int>(3)))); LinkedListSequence <int> sequence = new LinkedListSequence <int>(head); List <int> list = new List <int>(); foreach (int value in sequence) { list.Add(value); } EnumerableAssert.AreSequentialEqual(new int[] { 0, 1, 2, 3 }, list); }
public void EnumerableGeneralTest() { IEnumerable <int> functor = new int[] { 0, 1, 2 }; Func <int, int> addOne = x => x + 1; // Functor law 1: F.Select(Id) == Id(F) EnumerableAssert.AreSequentialEqual(functor.Select(Functions.Id), Functions.Id(functor)); // Functor law 2: F.Select(f2.o(f1)) == F.Select(f1).Select(f2) Func <int, string> addTwo = x => (x + 2).ToString(CultureInfo.InvariantCulture); EnumerableAssert.AreSequentialEqual( EnumerableExtensions.Select(addTwo.o(addOne)).Invoke(functor), EnumerableExtensions.Select(addTwo).o(EnumerableExtensions.Select(addOne)).Invoke(functor)); }
public void OrderablePartitionerTest() { int partitionCount = Environment.ProcessorCount * 2; int valueCount = partitionCount * 10000; IEnumerable <int> source = Enumerable.Range(0, valueCount); IEnumerable <KeyValuePair <long, int> > partitionsSource = new OrderableDynamicPartitioner <int>(source).GetOrderableDynamicPartitions(); IEnumerable <KeyValuePair <long, int> > result = Partitioning.GetPartitions(partitionsSource, partitionCount).Concat(); IOrderedEnumerable <int> indexes = result.Select(value => Convert.ToInt32(value.Key)).OrderBy(index => index); EnumerableAssert.AreSequentialEqual(source, indexes); IOrderedEnumerable <int> values = result.Select(value => value.Value).OrderBy(value => value); EnumerableAssert.AreSequentialEqual(source, values); }
public void RemoveAllTest() { EnumerableAssert.AreSequentialEqual(new int[] { 0, 1, 2, 3, 4 }, Enumerable.Range(0, 5).RemoveAll(-1)); EnumerableAssert.AreSequentialEqual(new int[] { 1, 2, 3, 4 }, Enumerable.Range(0, 5).RemoveAll(0)); EnumerableAssert.AreSequentialEqual(new int[] { 0, 2, 3, 4 }, Enumerable.Range(0, 5).RemoveAll(1)); EnumerableAssert.AreSequentialEqual(new int[] { 0, 1, 3, 4 }, Enumerable.Range(0, 5).RemoveAll(2)); EnumerableAssert.AreSequentialEqual(new int[] { 0, 1, 2, 4 }, Enumerable.Range(0, 5).RemoveAll(3)); EnumerableAssert.AreSequentialEqual(new int[] { 0, 1, 2, 3 }, Enumerable.Range(0, 5).RemoveAll(4)); EnumerableAssert.AreSequentialEqual(new int[] { 0, 1, 2, 3, 4 }, Enumerable.Range(0, 5).RemoveAll(5)); EnumerableAssert.AreSequentialEqual(new int[] { 0, 1, 2, 3, 4 }, Enumerable.Range(0, 5).RemoveAll(6)); EnumerableAssert.AreSequentialEqual(new int[] { }, Enumerable.Repeat(0, 5).RemoveAll(0)); EnumerableAssert.AreSequentialEqual(new int[] { 0, 0, 0, 0, 0 }, Enumerable.Repeat(0, 5).RemoveAll(1)); EnumerableAssert.AreSequentialEqual(new int[] { 0, 1, 2, 3, 0, 1, 2, 3 }, Enumerable.Range(0, 5).Concat(Enumerable.Range(0, 5)).RemoveAll(4)); EnumerableAssert.AreSequentialEqual(new int[] { 1, 2, 3, 4, 1, 2, 3, 4 }, Enumerable.Range(0, 5).Concat(Enumerable.Range(0, 5)).RemoveAll(0)); }
public void StaticPartitionerTest() { Partitioning.StaticPartitioner(); int partitionCount = Environment.ProcessorCount * 2; int valueCount = partitionCount * 10000; IEnumerable <int> source = Enumerable.Range(1, valueCount); IEnumerable <int> values = new StaticPartitioner <int>(source) .GetPartitions(partitionCount) .Select(partition => EnumerableEx.Create(() => partition)) .Concat() .OrderBy(value => value); EnumerableAssert.AreSequentialEqual(source, values); }
public void EnumerableTest2() { bool isExecuted1 = false; bool isExecuted2 = false; Func <int, int> addOne = x => { isExecuted1 = true; return(x + 1); }; IEnumerable <int> numbers = new int[] { 0, 1, 2 }; IEnumerable <Func <int, int> > addTwoAddOne = new Func <int, int>( x => { isExecuted2 = true; return(x + 2); }).Enumerable().Concat(addOne.Enumerable()); IEnumerable <int> query = addTwoAddOne.Apply(numbers); Assert.IsFalse(isExecuted1); // Deferred and lazy. Assert.IsFalse(isExecuted2); // Deferred and lazy. EnumerableAssert.AreSequentialEqual(new int[] { 2, 3, 4, 1, 2, 3 }, query); // Execution. Assert.IsTrue(isExecuted1); Assert.IsTrue(isExecuted2); }
public void EnumerableGeneralTest() { IEnumerable <int> functor = new int[] { 0, 1, 2 }; Func <int, int> addOne = x => x + 1; // Functor law 1: F.Select(Id) == Id(F) EnumerableAssert.AreSequentialEqual(functor.Select(Functions.Id), Functions.Id(functor)); // Functor law 2: F.Select(f2.o(f1)) == F.Select(f1).Select(f2) Func <int, string> addTwo = x => (x + 2).ToString(CultureInfo.InvariantCulture); IMorphism <int, int, DotNet> addOneMorphism = addOne.DotNetMorphism(); IMorphism <int, string, DotNet> addTwoMorphism = addTwo.DotNetMorphism(); EnumerableAssert.AreSequentialEqual( addTwoMorphism.o(addOneMorphism).Select().Invoke(functor), addTwoMorphism.Select().o(addOneMorphism.Select()).Invoke(functor)); }
public void EnumerableMonoidTest() { IEnumerable <int> enumerable = new int[] { 0, 1, 2, 3, 4 }; // Left unit preservation: Unit(f).Multiply() == f. EnumerableAssert.AreSequentialEqual( EnumerableExtensions.Unit(enumerable).Multiply(), enumerable); // Right unit preservation: f == f.Select(Unit).Multiply(). EnumerableAssert.AreSequentialEqual( enumerable, enumerable.Select(EnumerableExtensions.Unit).Multiply()); // Associativity preservation: f.Wrap().Multiply().Wrap().Multiply() == f.Wrap().Wrap().Multiply().Multiply(). EnumerableAssert.AreSequentialEqual( enumerable.Enumerable().Multiply().Enumerable().Multiply(), enumerable.Enumerable().Enumerable().Multiply().Multiply()); }
public void SkipWhileTest() { int[] enumerable = new int[] { 0, 1, 2 }; EnumerableAssert.AreSequentialEqual( Enumerable.SkipWhile(enumerable, x => x > 0), EnumerableSelectManyExtensions.SkipWhile(enumerable, x => x > 0)); enumerable = new int[] { 2, 1, 0, -1 }; EnumerableAssert.AreSequentialEqual( Enumerable.SkipWhile(enumerable, x => x > 0), EnumerableSelectManyExtensions.SkipWhile(enumerable, x => x > 0)); enumerable = new int[] { }; EnumerableAssert.AreSequentialEqual( Enumerable.SkipWhile(enumerable, x => x > 0), EnumerableSelectManyExtensions.SkipWhile(enumerable, x => x > 0)); }
public void TakeWhileTest() { int[] enumerable = new int[] { 0, 1, 2 }; EnumerableAssert.AreSequentialEqual( Enumerable.TakeWhile(enumerable, x => x > 0), EnumerableExtensions.TakeWhile(enumerable, x => x > 0)); enumerable = new int[] { 2, 1, 0, -1 }; EnumerableAssert.AreSequentialEqual( Enumerable.TakeWhile(enumerable, x => x > 0), EnumerableExtensions.TakeWhile(enumerable, x => x > 0)); enumerable = new int[] { }; EnumerableAssert.AreSequentialEqual( Enumerable.TakeWhile(enumerable, x => x > 0), EnumerableExtensions.TakeWhile(enumerable, x => x > 0)); }
public void RepeatTest() { EnumerableAssert.AreSequentialEqual( Enumerable.Repeat(1, 5), IteratorPattern.Repeat(1, 5)); EnumerableAssert.AreSequentialEqual( Enumerable.Repeat(1, 0), IteratorPattern.Repeat(1, 0)); EnumerableAssert.AreSequentialEqual( Enumerable.Repeat(1, 1), IteratorPattern.Repeat(1, 1)); object value = new object(); EnumerableAssert.AreSequentialEqual( Enumerable.Repeat(value, 10), IteratorPattern.Repeat(value, 10)); }