Click here to Skip to main content
15,892,697 members
Articles / Programming Languages / C# 4.0

Extended Thread Pool

Rate me:
Please Sign up or sign in to vote.
4.98/5 (25 votes)
6 Apr 2013Ms-PL3 min read 81.8K   1.8K   119  
Your own extensible and configurable Thread Pool.
using System;
using Moq;
using Nelibur.Sword.DataStructures;
using Nelibur.Sword.Extensions;
using Xunit;

namespace UnitTests.Nelibur.Sword.DataStructures
{
    public class OptionTests
    {
        [Fact]
        public void Default_ValueAbsent_True()
        {
            const int Value = 1;
            var bag = new Option<int>(Value, false);
            Assert.False(bag.HasValue);
            Assert.True(bag.HasNoValue);
            Assert.Equal(Value, bag.Value);
        }

        [Fact]
        public void Default_ValueExist_True()
        {
            const int Value = 1;
            var bag = new Option<int>(Value);
            Assert.True(bag.HasValue);
            Assert.False(bag.HasNoValue);
            Assert.Equal(Value, bag.Value);
        }

        [Fact]
        public void Empty_ReferenceType_Null()
        {
            Option<string> option = Option<string>.Empty;
            Assert.Equal(null, option.Value);
        }

        [Fact]
        public void Empty_ValueType_Zero()
        {
            Option<int> option = Option<int>.Empty;
            Assert.Equal(0, option.Value);
        }

        [Fact]
        public void MatchType_Empty_NotExecuted()
        {
            Option<Letter> option = Option<Letter>.Empty;

            var mock = new Mock<OptionTests>();

            option
                .MatchType<A>(x => mock.Object.OnMatchA(x));

            mock.Verify(x => x.OnMatchB(It.IsAny<B>()), Times.Never());
        }

        [Fact]
        public void MatchType_Matched_Executed()
        {
            Letter letter = new A();
            Option<Letter> option = letter.ToOption();

            var mock = new Mock<OptionTests>();

            option
                .MatchType<A>(x => mock.Object.OnMatchA(x));

            mock.Verify(x => x.OnMatchA(It.IsAny<A>()));
        }

        [Fact]
        public void MatchType_NotMatched_NotExecuted()
        {
            Letter letter = new A();
            Option<Letter> option = letter.ToOption();

            var mock = new Mock<OptionTests>();

            option
                .MatchType<B>(x => mock.Object.OnMatchB(x));

            mock.Verify(x => x.OnMatchB(It.IsAny<B>()), Times.Never());
        }

        [Fact]
        public void Match_Mathed_Executed()
        {
            var option = new Option<int>(1);
            var mock = new Mock<OptionTests>();

            var result = option.Match(x => x == 1, mock.Object.OnMatch);

            mock.Verify(x => x.OnMatch(option.Value), Times.Once);
            Assert.Equal(1, result.Value);
        }

        [Fact]
        public void Match_NotMathed_NotExecuted()
        {
            Option<int> option = Option<int>.Empty;
            var mock = new Mock<OptionTests>();

            var result = option.Match(x => x == 1, mock.Object.OnMatch);

            mock.Verify(x => x.OnMatch(option.Value), Times.Never());
            Assert.Equal(default(int), result.Value);
        }

        [Fact]
        public void ThrowOnEmpty_Empty_ThrowCustomException()
        {
            Assert.ThrowsDelegateWithReturn func = () =>
            {
                Option<int> empty = Option<int>.Empty;
                return empty.ThrowOnEmpty<NullReferenceException>();
            };
            Assert.Throws(typeof(NullReferenceException), func);
        }

        [Fact]
        public void ThrowOnEmpty_Empty_ThrowException()
        {
            Assert.ThrowsDelegateWithReturn func = () => Option<int>.Empty.ThrowOnEmpty(() => new NullReferenceException());
            Assert.Throws(typeof(NullReferenceException), func);
        }

        [Fact]
        public void ThrowOnEmpty_NotEmpty_NotThrowException()
        {
            Assert.DoesNotThrow(() => new Option<int>(1).ThrowOnEmpty(() => new NullReferenceException()));
        }

        protected virtual void OnMatch(int value)
        {
        }

        protected virtual void OnMatchA(A value)
        {
        }

        protected virtual void OnMatchB(B value)
        {
        }

        protected sealed class A : Letter
        {
        }

        protected sealed class B : Letter
        {
        }

        protected abstract class Letter
        {
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Microsoft Public License (Ms-PL)


Written By
Software Developer (Senior)
United States United States
B.Sc. in Computer Science.

Comments and Discussions