Skip to content

Instantly share code, notes, and snippets.

@adilsoncarvalho
Created November 3, 2010 14:01
Show Gist options
  • Save adilsoncarvalho/661105 to your computer and use it in GitHub Desktop.
Save adilsoncarvalho/661105 to your computer and use it in GitHub Desktop.
Creates a circular list using a LinkedList
using System.Collections.Generic;
/// <summary>
/// Class that implements a circular list
/// </summary>
/// <typeparam name="T">Type</typeparam>
public class CircularList<T>
: LinkedList<T>
{
/// <summary>
/// Currently selected item (null for no item selected yet)
/// </summary>
public LinkedListNode<T> Current { get; internal set; }
/// <summary>
/// Sets the Current property to null
/// </summary>
public void Reset()
{
Current = null;
}
/// <summary>
/// Goes to the first item on the list
/// </summary>
/// <returns>List node</returns>
public LinkedListNode<T> GoFirst()
{
return Current = First;
}
/// <summary>
/// Goes to the last item on the list
/// </summary>
/// <returns>List node</returns>
public LinkedListNode<T> GoLast()
{
return Current = Last;
}
/// <summary>
/// Moves to the next item in the list. If none was previously selected
/// then move to the first one
/// </summary>
/// <returns>List node</returns>
public LinkedListNode<T> GoNext()
{
if (Current == null || Current.Next == null)
return GoFirst();
return Current = Current.Next;
}
/// <summary>
/// Moves to the previous item in the list. If none was previously selected
/// then move to the last one
/// </summary>
/// <returns>List node</returns>
public LinkedListNode<T> GoPrevious()
{
if (Current == null || Current.Previous == null)
return GoLast();
return Current = Current.Previous;
}
}
using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace UtilsTest
{
[TestClass]
public class CircularListTest
{
CircularList<int> target;
[TestInitialize]
public void Setup()
{
target = new CircularList<int>();
}
[TestCleanup]
public void TearDown()
{
target = null;
}
[TestMethod]
public void Current_AntesDeMoverPriorOuNext_RetornaNull()
{
Assert.IsNull(target.Current);
}
[TestMethod]
public void Reset_Invocar_DeixaCurrentNulo()
{
target.AddLast(1);
target.GoNext();
Assert.IsNotNull(target.Current);
target.Reset();
Assert.IsNull(target.Current);
}
[TestMethod]
public void GoFirst_Invocar_ApontaParaOPrimeiroElemento()
{
target.AddLast(1);
target.AddLast(2);
target.AddLast(3);
var item = target.GoFirst();
Assert.AreEqual(1, item.Value);
Assert.AreEqual(item, target.Current);
}
[TestMethod]
public void GoLast_Invocar_ApontaParaOUltimoElemento()
{
target.AddLast(1);
target.AddLast(2);
target.AddLast(3);
var item = target.GoLast();
Assert.AreEqual(3, item.Value);
Assert.AreEqual(item, target.Current);
}
[TestMethod]
public void GoNext()
{
target.AddLast(1);
target.AddLast(2);
target.AddLast(3);
//
// no início não aponta para ninguém
Assert.IsNull(target.Current);
//
// no primeiro goNext aponta para 1
var actual = target.GoNext();
Assert.AreEqual(actual, target.Current);
Assert.AreEqual(1, actual.Value);
//
// no segundo goNext aponta para 2
actual = target.GoNext();
Assert.AreEqual(actual, target.Current);
Assert.AreEqual(2, actual.Value);
//
// no terceiro goNext aponta para 3
actual = target.GoNext();
Assert.AreEqual(actual, target.Current);
Assert.AreEqual(3, actual.Value);
//
// no quarto goNext aponta para 1
actual = target.GoNext();
Assert.AreEqual(actual, target.Current);
Assert.AreEqual(1, actual.Value);
}
[TestMethod]
public void GoPrevious()
{
target.AddLast(1);
target.AddLast(2);
target.AddLast(3);
//
// no início não aponta para ninguém
Assert.IsNull(target.Current);
//
// no primeiro GoPrevious aponta para 3
var actual = target.GoPrevious();
Assert.AreEqual(actual, target.Current);
Assert.AreEqual(3, actual.Value);
//
// no segundo GoPrevious aponta para 2
actual = target.GoPrevious();
Assert.AreEqual(actual, target.Current);
Assert.AreEqual(2, actual.Value);
//
// no terceiro GoPrevious aponta para 1
actual = target.GoPrevious();
Assert.AreEqual(actual, target.Current);
Assert.AreEqual(1, actual.Value);
//
// no quarto GoPrevious aponta para 3
actual = target.GoPrevious();
Assert.AreEqual(actual, target.Current);
Assert.AreEqual(3, actual.Value);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment