Skip to content

Instantly share code, notes, and snippets.

@NikiforovAll
Last active June 7, 2019 15:56
Show Gist options
  • Save NikiforovAll/2619980fdd524d700956f65825176f17 to your computer and use it in GitHub Desktop.
Save NikiforovAll/2619980fdd524d700956f65825176f17 to your computer and use it in GitHub Desktop.
using System;
using System.Collections;
using System.Collections.Generic;
namespace Iterator {
public class ConcreteIterator<T> : Iterator.Iterator<T> {
public ConcreteIterator (CustomCollection<T> collection) {
_collection = collection;
}
public int Step { get; set; } = 2;
private CustomCollection<T> _collection;
private int _currentIndex;
public override T CurrentItem () {
return _collection[_currentIndex];
}
public override T First () {
return _collection[0];
}
public override bool IsDone () {
return _currentIndex > _collection.Count - 1;
}
public override T Next () {
T result = default (T);
if (!IsDone ()) {
result = _collection[_currentIndex];
_currentIndex += Step;
}
return result;
}
}
}
namespace Iterator {
public class CustomCollection<T> : ICustomCollection<T>, IEnumerable<T> {
private List<T> _values = new List<T> ();
public T this [int index] {
get => _values[index];
set => _values[index] = value;
}
public int Count { get => _values.Count; }
public Iterator<T> CreateIterator () {
return new ConcreteIterator<T> (this);
}
public void Add (T item) {
_values.Add (item);
}
//it is better approach to use IEnumerable interfaces to implement Iterator pattern for language support and compatability, although I tryied to stick to canonized version
public IEnumerator<T> GetEnumerator () {
return _values.GetEnumerator ();
}
IEnumerator IEnumerable.GetEnumerator () {
return _values.GetEnumerator ();
}
}
}
namespace Iterator {
public interface ICustomCollection<T> {
Iterator.Iterator<T> CreateIterator ();
T this [int index] {
get;
set;
}
}
}
namespace Iterator {
public abstract class Iterator<T> {
public abstract T First ();
public abstract T Next ();
public abstract bool IsDone ();
public abstract T CurrentItem ();
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using Iterator;
public class Program {
public static void Main () {
var collection = new CustomCollection<int> { 1, 2, 3, 4, 5 };
var iterator = collection.CreateIterator ();
Assert.Equal (new int[] { 1, 3, 5 }, iterator.getCollectionFromIterator ().ToList ());
TestRunner.Print ();
}
}
public static class IteratorExtensions {
public static IEnumerable<int> getCollectionFromIterator (this Iterator<int> iterator) {
while (!iterator.IsDone ()) {
yield return iterator.Next ();
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.CompilerServices;
public static class Assert {
public static void Equal<T> (T a, T b, [CallerMemberName] string callerName = "", [CallerLineNumber] int callerLine = 0) {
if (!a.Equals (b)) {
// throw new Exception ($"{a} doesn't equal to {b}");
TestRunner.AddResult (
new TestRunResult () {
CallerName = callerName,
CallerLine = callerLine.ToString (),
Success = false,
Assertion = "Equal<T>",
ErrorMessage = $"{a} doesn't equal to {b}"
});
} else {
TestRunner.AddResult (
new TestRunResult () {
CallerName = callerName,
CallerLine = callerLine.ToString (),
Success = true,
Assertion = "Single<T>"
});
}
}
public static void Equal<T> (IEnumerable<T> a,
IEnumerable<T> b, [CallerMemberName] string callerName = "", [CallerLineNumber] int callerLine = 0) {
if (a.Count () != b.Count ()) {
TestRunner.AddResult (
new TestRunResult () {
CallerName = callerName,
CallerLine = callerLine.ToString (),
Success = false,
Assertion = "Equal<T>",
ErrorMessage = $"a.{a.Count()} != b.{b.Count()}"
});
} else if (!a.SequenceEqual (b)) {
TestRunner.AddResult (
new TestRunResult () {
CallerName = callerName,
CallerLine = callerLine.ToString (),
Success = false,
Assertion = "SequenceEqual<T>",
ErrorMessage = $"Different sequences"
});
} else {
TestRunner.AddResult (
new TestRunResult () {
CallerName = callerName,
CallerLine = callerLine.ToString (),
Success = true,
Assertion = "Equal<T>"
});
}
}
public static void Single<T> (IEnumerable<T> collection, [CallerMemberName] string callerName = "", [CallerLineNumber] int callerLine = 0) {
if (collection.Count () != 1) {
// throw new Exception ("Collection doesn't contain 1 element");
TestRunner.AddResult (
new TestRunResult () {
CallerName = callerName,
CallerLine = callerLine.ToString (),
Success = false,
Assertion = "Single<T>",
ErrorMessage = "Collection doesn't contain 1 element"
});
}
TestRunner.AddResult (
new TestRunResult () {
CallerName = callerName,
CallerLine = callerLine.ToString (),
Success = true,
Assertion = "Single<T>"
});
}
public static void Contains<T> (T token1, T token2, [CallerMemberName] string callerName = "", [CallerLineNumber] int callerLine = 0) {
if (typeof (T) == typeof (System.String)) {
if (!((token2 as string)).Contains (token1 as string)) {
// throw new Exception ($"{token2} doesn't contain {token1}");
TestRunner.AddResult (
new TestRunResult () {
CallerName = callerName,
CallerLine = callerLine.ToString (),
Success = false,
Assertion = "Contains<T>",
ErrorMessage = $"{token2} doesn't contain {token1}"
});
} else {
TestRunner.AddResult (
new TestRunResult () {
CallerName = callerName,
CallerLine = callerLine.ToString (),
Success = true,
Assertion = "Contains<T>"
});
}
} else {
throw new Exception ("It is not possible to use .Contains() method");
}
}
}
public static class TestRunner {
public static List<TestRunResult> Results { get; } = new List<TestRunResult> ();
public static void AddResult (TestRunResult res) {
Results.Add (res);
}
public static void Print () {
System.Console.WriteLine ("TestRunner.Print");
System.Console.WriteLine ("============================================");
foreach (var log in Results) {
System.Console.WriteLine (log);
}
System.Console.WriteLine ("============================================");
}
}
public class TestRunResult {
public string CallerName { get; set; }
public string CallerLine { get; set; }
public string Assertion { get; set; }
public bool Success { get; set; }
public string ErrorMessage { get; set; }
public override string ToString () {
return $"Assertion: {Assertion, 15}; MethodName: {CallerName, 10}; Line: {CallerLine, 3}; " + (Success? $"Success: {Success}": $"ErrorMessage: {ErrorMessage}");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment