Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
105 changes: 105 additions & 0 deletions UnitTests/EnumIterator.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
//-----------------------------------------------------------------------
// <copyright file="EnumIterator.cs" company="Masaru Tsuchiyama">
// Copyright (c) Masaru Tsuchiyama. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------
namespace UnitTests
{
using System;
using System.Collections.Generic;

/// <summary>
/// sequential iterator for enum
/// </summary>
/// <typeparam name="T"></typeparam>
public class SequentialEnumIterator<T>
{
public SequentialEnumIterator()
{
}

/// <summary>
/// sequential iterator
/// </summary>
public IEnumerable<T> Enumerable
{
get
{
foreach (T data in Enum.GetValues(typeof(T)))
{
yield return data;
}
}
}
}

/// <summary>
/// random iterator for enum
/// </summary>
/// <typeparam name="T"></typeparam>
public class RandomEnumIterator<T>
{
public RandomEnumIterator()
{
}

/// <summary>
/// random iterator
/// </summary>
public IEnumerable<T> Enumerable
{
get
{
var random = new Random();
Array values = Enum.GetValues(typeof(T));
var length = values.Length;

for(int i = 0; i < length; i++)
{
T data = (T)values.GetValue(random.Next(length - 1));
yield return data;
}
}
}
}

/// <summary>
/// enum iterator selector
/// </summary>
/// <typeparam name="T"></typeparam>
public class EnumIterator<T>
{
/// <summary>
/// iterator method
/// </summary>
public enum ReturnMethodType
{
Sequential,
Random,
}

/// <summary>
/// enum iterator selector
/// </summary>
/// <param name="method">iterator method</param>
/// <returns></returns>
static public IEnumerable<T> Enumerable(ReturnMethodType method)
{
switch(method)
{
case ReturnMethodType.Sequential:
{
var iterator = new SequentialEnumIterator<T>();
return iterator.Enumerable;
}
case ReturnMethodType.Random:
{
var iterator = new RandomEnumIterator<T>();
return iterator.Enumerable;
}
default:
throw new ArgumentException(method.ToString());
}
}
}
}
114 changes: 114 additions & 0 deletions UnitTests/GuidQueue.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
//-----------------------------------------------------------------------
// <copyright file="GuidQueue.cs" company="Masaru Tsuchiyama">
// Copyright (c) Masaru Tsuchiyama. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------
namespace UnitTests
{
using System;
using System.Collections.Generic;

/// <summary>
/// Tester Class for providing custom GUID generator
/// </summary>
public class GuidQueue
{
/// <summary>
/// Guid Method
/// </summary>
public enum GuidGeneratorMethod
{
Normal,
GuidGenerater,
}

/// <summary>
/// delegate for creating new GUID
/// </summary>
/// <param name="guid"></param>
/// <returns></returns>
public delegate Guid NewGuid();

/// <summary>
/// seed to create a new GUID.
/// </summary>
private Queue<Guid> fifo;

/// <summary>
/// delegate for creating brand-new GUID
/// </summary>
private NewGuid newGuid;

/// <summary>
/// guid Generator Method
/// </summary>
private GuidGeneratorMethod guidGeneratorMethod;

/// <summary>
/// Set and Get guid generation method
/// </summary>
public GuidGeneratorMethod GuidMethod
{
get
{
return this.guidGeneratorMethod;
}
set
{
switch(value)
{
case GuidGeneratorMethod.Normal:
this.newGuid = delegate () { return Guid.NewGuid(); };
break;
case GuidGeneratorMethod.GuidGenerater:
var guidGenerator = new GuidGenerater();
this.newGuid = guidGenerator.NewGuid;
break;
}
this.guidGeneratorMethod = value;
this.fifo.Clear();
}
}

/// <summary>
/// Initializes a new instance of the <see cref="GuidQueue" /> class.
/// </summary>
public GuidQueue()
{
this.fifo = new Queue<Guid>();
this.GuidMethod = GuidGeneratorMethod.Normal;
}

/// <summary>
/// create brand-new GUID with queueing
/// </summary>
/// <returns>new GUID</returns>
public Guid BrandNewGuid()
{
var guid = this.newGuid();
this.fifo.Enqueue(guid);
return guid;
}

/// <summary>
/// create brand-new GUID without queueing
/// </summary>
/// <returns>new GUID</returns>
public Guid BrandNewGuidNoCache()
{
var guid = this.newGuid();
this.fifo.Clear();
return guid;
}

/// <summary>
/// create new GUID from cache.
/// </summary>
/// <returns>new GUID</returns>
public Guid NewGuidFromCache()
{
var guid = this.fifo.Dequeue();
return guid;
}
}
}
98 changes: 98 additions & 0 deletions UnitTests/TestEnumIterator.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
//-----------------------------------------------------------------------
// <copyright file="TestEnumIterator.cs" company="Masaru Tsuchiyama">
// Copyright (c) Masaru Tsuchiyama. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------
namespace UnitTests
{
using System;
using System.Collections.Generic;
using NUnit.Framework;

/// <summary>
/// unit test for Template
/// </summary>
[TestFixture]
public class TestEnumIterator
{
/// <summary>
/// enum type1
/// </summary>
public enum TestEnumType1
{
Type1,
Type2,
Type3,
Type4,
}

/// <summary>
/// enum type2
/// </summary>
public enum TestEnumType2
{
Type1,
Type2,
Type3,
Type4,
}

/// <summary>
/// setup method to be called to initialize unit tests
/// </summary>
[SetUp]
public void SetUp()
{
}

/// <summary>
/// cleanup method to be called to cleanup unit tests
/// </summary>
[TearDown]
public void TearDown()
{
}

/// <summary>
/// Test for TestEnumType1
/// </summary>
/// <param name="returnMethod">returnMethod</param>
[Test, Pairwise]
public void TestForTestEnumType1(
[Values] EnumIterator<TestEnumType1>.ReturnMethodType returnMethod)
{
TestEnumTypeGeneric<TestEnumType1>(returnMethod);
}

/// <summary>
/// Test for TestEnumType2
/// </summary>
/// <param name="returnMethod">returnMethod</param>
[Test, Pairwise]
public void TestForTestEnumType2(
[Values] EnumIterator<TestEnumType2>.ReturnMethodType returnMethod)
{
TestEnumTypeGeneric<TestEnumType2>(returnMethod);
}

/// <summary>
/// utility function to test EnumIterator.Enumerable
/// </summary>
/// <typeparam name="T">enum type</typeparam>
/// <param name="returnMethod">returnMethod</param>
private void TestEnumTypeGeneric<T>(EnumIterator<T>.ReturnMethodType returnMethod)
{
Array values = Enum.GetValues(typeof(T));
var length = values.Length;

int count = 0;
IEnumerable<T> iterater = EnumIterator<T>.Enumerable(returnMethod);
foreach (T targetType in iterater)
{
Console.WriteLine(targetType.ToString());
count++;
}
Assert.That(count, Is.EqualTo(length));
}
}
}
Loading