Created
February 25, 2019 19:02
-
-
Save adam-kral/c4b2f68e3a0ced8d0f2d4ead4ca14298 to your computer and use it in GitHub Desktop.
This file contains 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
using System; | |
using System.Collections.Generic; | |
using Xunit; | |
namespace deque_test { | |
public class TestsIList { | |
bool ListCmp(List<int> l1, List<int> l2) { | |
if (l1.Count != l2.Count) | |
return false; | |
for (var i = 0; i < l1.Count; ++i) | |
if (l1[i] != l2[i]) | |
return false; | |
return true; | |
} | |
Deque<int> GetDeque() { | |
return new Deque<int>(); | |
} | |
Deque<int> GetSomeDeque() { | |
return new Deque<int> {-10, -9, -8, -7, -6}; | |
} | |
[Fact] | |
public void Insert() { | |
var d = new Deque<int>(); | |
d.Insert(0, 1); | |
Assert.Equal(d[0], 1); | |
Assert.Equal(d.Front, 1); | |
} | |
[Fact] | |
public void InsertAtBeginning() { | |
var d = new Deque<int>(); | |
d.Insert(0, 1); | |
d.Insert(0, 5); | |
Assert.Equal(d[0], 5); | |
Assert.Equal(d[1], 1); | |
Assert.Equal(d.Front, 5); | |
} | |
[Fact] | |
public void Count() { | |
var d = new Deque<int>(); | |
d.Insert(0, 1); | |
d.Insert(0, 5); | |
Assert.Equal(d.Count, 2); | |
} | |
[Fact] | |
public void Add() { | |
var d = new Deque<int>(); | |
d.Insert(0, 1); | |
d.Add(5); | |
Assert.Equal(d.Back, 5); | |
} | |
[Fact] | |
public void ForEach() { | |
var d = new Deque<int> {-10, -9, -8, -7, -6}; | |
var items = new List<int> {-10, -9, -8, -7, -6}; | |
Assert.True(ForEachEqual(d, items)); | |
} | |
[Fact] | |
public void ReverseIndexOf() { | |
var d = new Deque<int> {1, 2, 3, 4, 5, 6}; | |
var reverseView = d.GetReverseView(); | |
Assert.Equal(d.Count - 1, reverseView.IndexOf(1)); | |
Assert.Equal(3, reverseView.IndexOf(3)); | |
} | |
[Fact] | |
public void ReverseInsert() { | |
var d = new Deque<int> {1,2,3,4,5}; | |
var r = d.GetReverseView(); | |
r.Insert(2, 11); | |
r.Insert(0, 12); | |
r.Insert(7, 13); | |
r.RemoveAt(0); | |
r.Insert(0, 15); | |
r.Insert(0, 16); | |
var expected = new List<int> {16, 15, 5, 4, 11, 3, 2, 1, 13}; | |
Assert.True(ForEachEqual(r, expected)); | |
expected.Reverse(); | |
Assert.True(ForEachEqual(r.GetReverseView(), expected)); | |
} | |
[Fact] | |
public void ReverseForEach() { | |
var d = new Deque<int> {-10, -9, -8, -7, -6}; | |
var items = new List<int> {-6, -7, -8, -9, -10}; | |
Assert.True(ForEachEqual(d.GetReverseView(), items)); | |
} | |
bool ForEachEqual(IDeque<int> d, List<int> to) { | |
var actualItems = new List<int>(); | |
foreach (var item in d) { | |
actualItems.Add(item); | |
} | |
return ListCmp(to, actualItems); | |
} | |
Deque<int> GetWrappedDq() { | |
var d = new Deque<int> {1,1,1,1,1}; | |
d.RemoveAt(0); | |
d.Add(1); | |
Assert.Equal(d.Count, 5); | |
d.RemoveAt(0); | |
d.Add(2); | |
Assert.Equal(d.Count, 5); | |
d.RemoveAt(0); | |
d.Add(3); | |
Assert.Equal(d.Count, 5); | |
d.RemoveAt(0); | |
d.Add(4); | |
return d; | |
} | |
[Fact] | |
public void Wrapping() { | |
var d = GetWrappedDq(); | |
Assert.Equal(d.Count, 5); | |
Assert.False(d.Remove(5)); | |
Assert.True(ForEachEqual(d, new List<int> {1,1,2,3,4})); | |
} | |
[Fact] | |
public void Clear() { | |
var d = GetSomeDeque(); | |
Assert.NotEqual(0, d.Count); | |
d.Clear(); | |
Assert.Equal(0, d.Count); | |
Assert.True(ForEachEqual(d, new List<int>())); | |
// works after clearing | |
d.Insert(0, 10); | |
Assert.Equal(10, d[0]); | |
} | |
[Fact] | |
public void Remove() { | |
var d = new Deque<int>(); | |
d.Insert(0, 1); | |
d.Insert(0, 5); | |
Assert.True(d.Remove(5)); | |
Assert.Equal(d.Count, 1); | |
Assert.Equal(d[0], 1); | |
d.Insert(0, 5); | |
d.Insert(0, 6); | |
} | |
[Fact] | |
public void RemoveAt() { | |
var d = new Deque<int> {1, 2, 3, 4, 5, 6}; | |
d.RemoveAt(3); | |
Assert.True(ForEachEqual(d, new List<int>{1,2,3,5,6})); | |
d.RemoveAt(d.Count -1); | |
Assert.True(ForEachEqual(d, new List<int>{1,2,3,5})); | |
d.RemoveAt(0); | |
Assert.True(ForEachEqual(d, new List<int>{2,3,5})); | |
} | |
[Fact] | |
public void RemoveNonExistent() { | |
var d = new Deque<int>(); | |
d.Insert(0, 1); | |
d.Insert(0, 5); | |
Assert.False(d.Remove(2)); | |
Assert.Equal(d.Count, 2); | |
} | |
[Fact] | |
public void RemoveEmpty() { | |
var d = new Deque<int>(); | |
Assert.False(d.Remove(5)); | |
} | |
[Fact] | |
public void RemoveAtOutOfRange() { | |
var d = new Deque<int>(); | |
Assert.Throws<ArgumentOutOfRangeException>(() => d.RemoveAt(5)); | |
Assert.Throws<ArgumentOutOfRangeException>(() => d.RemoveAt(-1)); | |
} | |
[Fact] | |
public void Remove2() { | |
var d = GetWrappedDq(); | |
d.RemoveAt(3); | |
Assert.True(ForEachEqual(d, new List<int> {1, 1, 2, 4})); | |
Assert.True(d.Remove(1)); | |
Assert.True(ForEachEqual(d, new List<int> {1, 2, 4})); | |
Assert.Throws<ArgumentOutOfRangeException>(() => d.RemoveAt(5)); | |
Assert.Throws<ArgumentOutOfRangeException>(() => d.RemoveAt(-1)); | |
} | |
[Fact] | |
public void Indexer() { | |
var d = GetDeque(); | |
d.Add(1); | |
d.Insert(0, 2); | |
d.RemoveAt(0); | |
d.Insert(0, 3); | |
Assert.Equal(d[0], 3); | |
Assert.Equal(d[1], 1); | |
} | |
[Fact] | |
public void IndexerOutOfRange() { | |
var d = GetDeque(); | |
d.Insert(0, 1); | |
Assert.Throws<ArgumentOutOfRangeException>(() => d[1]); | |
Assert.Throws<ArgumentOutOfRangeException>(() => d[-1]); | |
Assert.Throws<ArgumentOutOfRangeException>(() => d[10]); | |
} | |
[Fact] | |
public void Recodex4() { | |
// z outputu, co mi poslal Honza | |
var d = new Deque<int>{1, 2, 3}; | |
Assert.True(ForEachEqual(d, new List<int>{1, 2, 3})); | |
d.Insert(0, -1); | |
Assert.True(ForEachEqual(d, new List<int>{-1, 1, 2, 3})); | |
d.Insert(0, -2); | |
Assert.True(ForEachEqual(d, new List<int>{-2, -1, 1, 2, 3})); | |
d.Insert(0, -3); | |
Assert.True(ForEachEqual(d, new List<int>{-3, -2, -1, 1, 2, 3})); | |
d.Remove(1); | |
Assert.True(ForEachEqual(d, new List<int>{-3, -2, -1, 2, 3})); | |
d.Remove(-3); | |
Assert.True(ForEachEqual(d, new List<int>{-2, -1, 2, 3})); | |
d.Remove(3); | |
Assert.True(ForEachEqual(d, new List<int>{-2, -1, 2})); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment