Skip to content

Commit

Permalink
Improving code coverage.
Browse files Browse the repository at this point in the history
  • Loading branch information
giacomelli committed Jun 18, 2017
1 parent 3843bd5 commit ac425ca
Show file tree
Hide file tree
Showing 13 changed files with 160 additions and 143 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public void Evolve_ManyGenerations_Fast()
MaxThreads = 20
};

ga.Termination = new GenerationNumberTermination(2);
ga.Termination = new GenerationNumberTermination(10);
ga.Start();

Assert.NotNull(ga.BestChromosome);
Expand Down
10 changes: 8 additions & 2 deletions src/GeneticSharp.Extensions.UnitTests/Drawing/BitmapTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,16 @@ public void Evolve_ManyGenerations_Fast()
MaxThreads = 20
};

ga.Termination = new GenerationNumberTermination(1);
ga.Termination = new GenerationNumberTermination(5);
ga.Start();

Assert.NotNull(ga.BestChromosome);
var c = ga.BestChromosome as BitmapChromosome;
Assert.IsNotNull(c);

var bitmap = c.BuildBitmap();
Assert.IsNotNull(bitmap);
Assert.AreEqual(32, bitmap.Width);
Assert.AreEqual(32, bitmap.Height);
}
}
}
147 changes: 74 additions & 73 deletions src/GeneticSharp.Extensions/Drawing/BitmapEqualityFitness.cs
Original file line number Diff line number Diff line change
@@ -1,93 +1,94 @@
using System;
using System.Collections.Generic;
using System.Collections.Generic;
using System.Drawing;
using GeneticSharp.Domain.Chromosomes;
using GeneticSharp.Domain.Fitnesses;


namespace GeneticSharp.Extensions.Drawing
{
/// <summary>
/// Bitmap equality fitness.
/// </summary>
public class BitmapEqualityFitness : IFitness
{
{
/// <summary>
/// Bitmap equality fitness.
/// </summary>
public class BitmapEqualityFitness : IFitness
{
#region Fields
private IList<Color> m_targetBitmapPixels;
private int m_pixelsCount;
private int m_pixelsCount;
#endregion


#region Constructors
/// <summary>
/// Initializes a new instance of the <see cref="BitmapEqualityFitness"/> class.
/// </summary>
/// <param name="targetBitmap">The target bitmap.</param>
public BitmapEqualityFitness(Bitmap targetBitmap)
{
Initialize(targetBitmap);
}

/// <summary>
/// Initializes a new instance of the <see cref="BitmapEqualityFitness"/> class.
/// </summary>
public BitmapEqualityFitness()
{
}
/// <summary>
/// Initializes a new instance of the <see cref="BitmapEqualityFitness"/> class.
/// </summary>
/// <param name="targetBitmap">The target bitmap.</param>
public BitmapEqualityFitness(Bitmap targetBitmap)
: this()
{
Initialize(targetBitmap);
}

/// <summary>
/// Initializes a new instance of the <see cref="BitmapEqualityFitness"/> class.
/// </summary>
public BitmapEqualityFitness()
{
}
#endregion


#region Properties
/// <summary>
/// Gets the width of the bitmap.
/// </summary>
/// <value>
/// The width of the bitmap.
/// </value>
public int BitmapWidth { get; private set; }

/// <summary>
/// Gets the height of the bitmap.
/// </summary>
/// <value>
/// The height of the bitmap.
/// </value>
public int BitmapHeight { get; private set; }
/// <summary>
/// Gets the width of the bitmap.
/// </summary>
/// <value>
/// The width of the bitmap.
/// </value>
public int BitmapWidth { get; private set; }

/// <summary>
/// Gets the height of the bitmap.
/// </summary>
/// <value>
/// The height of the bitmap.
/// </value>
public int BitmapHeight { get; private set; }
#endregion


#region Methods
/// <summary>
/// Initializes the specified target bitmap.
/// </summary>
/// <param name="targetBitmap">The target bitmap.</param>
public void Initialize(Bitmap targetBitmap)
{
BitmapWidth = targetBitmap.Width;
/// <summary>
/// Initializes the specified target bitmap.
/// </summary>
/// <param name="targetBitmap">The target bitmap.</param>
public void Initialize(Bitmap targetBitmap)
{
BitmapWidth = targetBitmap.Width;
BitmapHeight = targetBitmap.Height;

m_targetBitmapPixels = BitmapChromosome.GetPixels(targetBitmap);
m_pixelsCount = m_targetBitmapPixels.Count;
m_targetBitmapPixels = BitmapChromosome.GetPixels(targetBitmap);
m_pixelsCount = m_targetBitmapPixels.Count;
}

/// <summary>
/// Evaluates the specified chromosome.
/// </summary>
/// <param name="chromosome">The chromosome.</param>
/// <returns>The chromosome fitness.</returns>
public double Evaluate(IChromosome chromosome)
{
double fitness = 0;

for (int i = 0; i < m_pixelsCount; i++)
{
var targetPixel = m_targetBitmapPixels[i];
var chromosomePixel = (Color)chromosome.GetGene(i).Value;

fitness -= Math.Abs(targetPixel.R - chromosomePixel.R);
fitness -= Math.Abs(targetPixel.G - chromosomePixel.G);
fitness -= Math.Abs(targetPixel.B - chromosomePixel.B);
}

return fitness;
}

/// <summary>
/// Evaluates the specified chromosome.
/// </summary>
/// <param name="chromosome">The chromosome.</param>
/// <returns>The chromosome fitness.</returns>
public double Evaluate(IChromosome chromosome)
{
double fitness = 0;

for (int i = 0; i < m_pixelsCount; i++)
{
var targetPixel = m_targetBitmapPixels[i];
var chromosomePixel = (Color)chromosome.GetGene(i).Value;

fitness -= Math.Abs(targetPixel.R - chromosomePixel.R);
fitness -= Math.Abs(targetPixel.G - chromosomePixel.G);
fitness -= Math.Abs(targetPixel.B - chromosomePixel.B);
}

return fitness;
}

#endregion
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,16 @@ public void ToDouble_NegativeRepresentation_Double()
Assert.AreEqual(-10.123, actual);
}

[Test]
public void ToDouble_DiffArraysLengths_Exception()
{
var actual = Assert.Catch(() => BinaryStringRepresentation.ToDouble("000000000110111010011110001011", new int[] { 16, 14, 1 }, new int[] { 2, 3 }));
Assert.AreEqual("The length of totalBits should be the same of the length of fractionBits.", actual.Message);

actual = Assert.Catch(() => BinaryStringRepresentation.ToDouble("00000000011011101001111000101", new int[] { 16, 14, }, new int[] { 2, 3 }));
Assert.AreEqual("The representation length should be the same of the sum of the totalBits.", actual.Message);
}

[Test]
public void ToDouble_RepresentationAndTotalBitsArray_Doubles()
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,67 +1,67 @@
using System;
using System.Threading;
using System.Threading.Tasks;
using GeneticSharp.Infrastructure.Framework.Threading;
using NUnit.Framework;

namespace GeneticSharp.Infrastructure.Framework.UnitTests.Threading
{
[TestFixture()]
[Category("Infrastructure")]
public class LinearTaskExecutorTest
{
[Test()]
public void Start_Task_TaskRan()
{
var pipeline = "";
var target = new LinearTaskExecutor();
target.Add(() => pipeline += "1");
target.Add(() => pipeline += "2");
target.Add(() => pipeline += "3");

Assert.IsTrue(target.Start());
Assert.AreEqual("123", pipeline);
}

[Test()]
public void Start_TakeMoreThanTimeout_False()
{
var pipeline = "";
var target = new LinearTaskExecutor();
target.Add(() => pipeline += "1");
target.Add(() =>
{
pipeline += "2";
Thread.Sleep(100);
});
target.Add(() => pipeline += "3");

target.Timeout = TimeSpan.FromMilliseconds(50);
Assert.IsFalse(target.Start());
Assert.AreEqual("12", pipeline);
}

[Test()]
public void Stop_ManyTasks_True()
{
var pipeline = "";
var target = new LinearTaskExecutor();
target.Add(() => pipeline += "1");
target.Add(() =>
{
pipeline += "2";
Thread.Sleep(1000);
});
target.Add(() => pipeline += "3");

Parallel.Invoke(
() => Assert.IsTrue(target.Start()),
() =>
{
Thread.Sleep(5);
target.Stop();
});
}
}
}
using System;
using System.Threading;
using System.Threading.Tasks;
using GeneticSharp.Infrastructure.Framework.Threading;
using NUnit.Framework;

namespace GeneticSharp.Infrastructure.Framework.UnitTests.Threading
{
[TestFixture()]
[Category("Infrastructure")]
public class LinearTaskExecutorTest
{
[Test()]
public void Start_Task_TaskRan()
{
var pipeline = "";
var target = new LinearTaskExecutor();
target.Add(() => pipeline += "1");
target.Add(() => pipeline += "2");
target.Add(() => pipeline += "3");

Assert.IsTrue(target.Start());
Assert.AreEqual("123", pipeline);
}

[Test()]
public void Start_TakeMoreThanTimeout_False()
{
var pipeline = "";
var target = new LinearTaskExecutor();
target.Add(() => pipeline += "1");
target.Add(() =>
{
pipeline += "2";
Thread.Sleep(100);
});
target.Add(() => pipeline += "3");

target.Timeout = TimeSpan.FromMilliseconds(100);
Assert.IsFalse(target.Start());
Assert.AreEqual("12", pipeline);
}

[Test()]
public void Stop_ManyTasks_True()
{
var pipeline = "";
var target = new LinearTaskExecutor();
target.Add(() => pipeline += "1");
target.Add(() =>
{
pipeline += "2";
Thread.Sleep(1000);
});
target.Add(() => pipeline += "3");

Parallel.Invoke(
() => Assert.IsTrue(target.Start()),
() =>
{
Thread.Sleep(5);
target.Stop();
});
}
}
}

Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.

0 comments on commit ac425ca

Please sign in to comment.