From ac425cab658bf518d027723e05648a00e82bd131 Mon Sep 17 00:00:00 2001 From: Diego Giacomelli Date: Sun, 18 Jun 2017 08:17:52 -0300 Subject: [PATCH] Improving code coverage. --- .../AutoConfig/AutoConfigTest.cs | 2 +- .../Drawing/BitmapTest.cs | 10 +- .../Drawing/BitmapEqualityFitness.cs | 147 +++++++++--------- .../Commons/BinaryStringRepresentationTest.cs | 10 ++ .../Threading/LinearTaskExecutorTest.cs | 134 ++++++++-------- .../Libraries/GeneticSharp.Domain.dll | Bin 65536 -> 65536 bytes .../Libraries/GeneticSharp.Domain.dll.mdb | Bin 33973 -> 33973 bytes .../Libraries/GeneticSharp.Extensions.dll | Bin 28672 -> 28672 bytes .../Libraries/GeneticSharp.Extensions.dll.mdb | Bin 12498 -> 12498 bytes .../GeneticSharp.Infrastructure.Framework.dll | Bin 15872 -> 15872 bytes ...eticSharp.Infrastructure.Framework.dll.mdb | Bin 4447 -> 4447 bytes .../GeneticSharp.Infrastructure.Threading.dll | Bin 6656 -> 6656 bytes ...eticSharp.Infrastructure.Threading.dll.mdb | Bin 1189 -> 1189 bytes 13 files changed, 160 insertions(+), 143 deletions(-) diff --git a/src/GeneticSharp.Extensions.UnitTests/AutoConfig/AutoConfigTest.cs b/src/GeneticSharp.Extensions.UnitTests/AutoConfig/AutoConfigTest.cs index f59be5a0..332d2548 100644 --- a/src/GeneticSharp.Extensions.UnitTests/AutoConfig/AutoConfigTest.cs +++ b/src/GeneticSharp.Extensions.UnitTests/AutoConfig/AutoConfigTest.cs @@ -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); diff --git a/src/GeneticSharp.Extensions.UnitTests/Drawing/BitmapTest.cs b/src/GeneticSharp.Extensions.UnitTests/Drawing/BitmapTest.cs index c6a07f50..593b1db6 100644 --- a/src/GeneticSharp.Extensions.UnitTests/Drawing/BitmapTest.cs +++ b/src/GeneticSharp.Extensions.UnitTests/Drawing/BitmapTest.cs @@ -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); } } } diff --git a/src/GeneticSharp.Extensions/Drawing/BitmapEqualityFitness.cs b/src/GeneticSharp.Extensions/Drawing/BitmapEqualityFitness.cs index b6ffc16b..5825adef 100644 --- a/src/GeneticSharp.Extensions/Drawing/BitmapEqualityFitness.cs +++ b/src/GeneticSharp.Extensions/Drawing/BitmapEqualityFitness.cs @@ -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 -{ - /// - /// Bitmap equality fitness. - /// - public class BitmapEqualityFitness : IFitness - { +{ + /// + /// Bitmap equality fitness. + /// + public class BitmapEqualityFitness : IFitness + { #region Fields private IList m_targetBitmapPixels; - private int m_pixelsCount; + private int m_pixelsCount; #endregion - + #region Constructors - /// - /// Initializes a new instance of the class. - /// - /// The target bitmap. - public BitmapEqualityFitness(Bitmap targetBitmap) - { - Initialize(targetBitmap); - } - - /// - /// Initializes a new instance of the class. - /// - public BitmapEqualityFitness() - { - } + /// + /// Initializes a new instance of the class. + /// + /// The target bitmap. + public BitmapEqualityFitness(Bitmap targetBitmap) + : this() + { + Initialize(targetBitmap); + } + + /// + /// Initializes a new instance of the class. + /// + public BitmapEqualityFitness() + { + } #endregion - + #region Properties - /// - /// Gets the width of the bitmap. - /// - /// - /// The width of the bitmap. - /// - public int BitmapWidth { get; private set; } - - /// - /// Gets the height of the bitmap. - /// - /// - /// The height of the bitmap. - /// - public int BitmapHeight { get; private set; } + /// + /// Gets the width of the bitmap. + /// + /// + /// The width of the bitmap. + /// + public int BitmapWidth { get; private set; } + + /// + /// Gets the height of the bitmap. + /// + /// + /// The height of the bitmap. + /// + public int BitmapHeight { get; private set; } #endregion - + #region Methods - /// - /// Initializes the specified target bitmap. - /// - /// The target bitmap. - public void Initialize(Bitmap targetBitmap) - { - BitmapWidth = targetBitmap.Width; + /// + /// Initializes the specified target bitmap. + /// + /// The target bitmap. + 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; + } + + /// + /// Evaluates the specified chromosome. + /// + /// The chromosome. + /// The chromosome fitness. + 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; } - /// - /// Evaluates the specified chromosome. - /// - /// The chromosome. - /// The chromosome fitness. - 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 } } \ No newline at end of file diff --git a/src/GeneticSharp.Infrastructure.Framework.UnitTests/Commons/BinaryStringRepresentationTest.cs b/src/GeneticSharp.Infrastructure.Framework.UnitTests/Commons/BinaryStringRepresentationTest.cs index 95b52a8c..fe45327f 100644 --- a/src/GeneticSharp.Infrastructure.Framework.UnitTests/Commons/BinaryStringRepresentationTest.cs +++ b/src/GeneticSharp.Infrastructure.Framework.UnitTests/Commons/BinaryStringRepresentationTest.cs @@ -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() { diff --git a/src/GeneticSharp.Infrastructure.Framework.UnitTests/Threading/LinearTaskExecutorTest.cs b/src/GeneticSharp.Infrastructure.Framework.UnitTests/Threading/LinearTaskExecutorTest.cs index d09eda3e..e942b9ff 100644 --- a/src/GeneticSharp.Infrastructure.Framework.UnitTests/Threading/LinearTaskExecutorTest.cs +++ b/src/GeneticSharp.Infrastructure.Framework.UnitTests/Threading/LinearTaskExecutorTest.cs @@ -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(); + }); + } + } +} + diff --git a/src/Samples/Unity/Checkers/Assets/_Checkers/Libraries/GeneticSharp.Domain.dll b/src/Samples/Unity/Checkers/Assets/_Checkers/Libraries/GeneticSharp.Domain.dll index 04e2e4fba35fddfdb728ffacaa01104cfa1fbcb1..e60aac27bd6f058a8ac3145c08cf53be3fb32a0e 100755 GIT binary patch delta 187 zcmV;s07Uzq#IAJUvq&Z^m797!b z1slSxs>@D>{g@pbcpt%m7VymD3xG5CVO9hDNb)$DZAE8}b z`*+jWiH#2+lri+jpW{#Vj%o}fZP+?PvLH?9$+f=+8VhQjZhZ;_Bt;DX>eB%Sv+z(f pMFN3YlO~oV0)JVvOqRS#5YX)3?o6Q^Mu|B=XPZDTTC*78=pI1tR4o7i delta 187 zcmV;s07U_|Fy*)Un`pY)(aEisRF zP;Z^d|BeL7S$7SI)-=B?9xd@89;~Ph#iu=VY4=3qOkW3)BW&8>S^8OH(f$Ekv+z(f pMFN&QlO~oV0+l?oOqRS#5P(gZE8o!DO}9&VtD^?w?z0%-=pM6NTeJWG diff --git a/src/Samples/Unity/Checkers/Assets/_Checkers/Libraries/GeneticSharp.Domain.dll.mdb b/src/Samples/Unity/Checkers/Assets/_Checkers/Libraries/GeneticSharp.Domain.dll.mdb index 0b99a033e796b639f1effdb22a0bf8761a0845c8..508efe96c3dee162d3a4b5d0b42717cc9a8590b2 100644 GIT binary patch delta 43 vcmdnm$+WeTNkL>;{a;{aI)xZ)sCp; zzr9cQO}`}eH*wabnoDaJZQJ*Jv3}e9R^{&50;#@*T0yJ!%{b&?eBQ+=hu5klY2g;% z%=oVCbxJvmI-6CL)1%XRSj3a@h|jj`5&slWVMECb4c_W5diEST)zMS delta 230 zcmZp8z}WDBal#Fz%_leB>QP`}WMJUhoTX^RSbwITS=F7l`eaS}KIhqT+)J;>ccq!k zwcNe!#?{Gdg4V3D^JLi-eO>6(|B1`){0S6`=qnLz&WybF{4HBubxfpVN&J>+mfL;9 zAKdy<6L(d%Y1gjK=Wm#gNZwfGtLD-Ar>pI0>hr_l&D);xaU>*W=E$1``nx(-?or)v z`pk#u(~IvjT5eWRPLtAlDe)y7=qQFVt|$ox21X!!DKX6kD8SIbyOj$>L&T=qY~Er! dK~`Xe+nvt>lbl`WqkPgGHTv0i_MKL37ym?E delta 213 zcmcbVcqvgqWLf=RWwjTsMhp?$VEd1L#PZ_q`h_4Q#6xn0}M%jfuaV(jCY-P z(-*)K$^pGDd}Ban4A{3=ab|%uToe-7SWmb(eZDcmyWMR^dRCMEq`9-Q*ufb^V7+p2 zq-~G-ZXJk=!IERO)3d}ZJP`~5QdIx| delta 179 zcmV;k08IaYe1LqA*aP@u0I}O041ZiyBH~P(N6ezr$+KeOPU%bQW2p$dNd$f9Zre2d zaCkbL&(NYBb^amPiZ(Fq15g(G@619_(gPmzOo@C**t`L*I-O1v&SpjAs%WqjaA0$|;K}(A$B+&v>wT_#S(5-qjeNcI`mjDAr2!eUcnzQo h0+l?o8XB4s5U%b}eFS)7O{k&FM#AX3(X+%XJP|b&R)+up diff --git a/src/Samples/Unity/Checkers/Assets/_Checkers/Libraries/GeneticSharp.Infrastructure.Framework.dll.mdb b/src/Samples/Unity/Checkers/Assets/_Checkers/Libraries/GeneticSharp.Infrastructure.Framework.dll.mdb index 9a3889e5701186d15fa3d4a7745ea9ed10df2bbc..2ea35d649f781f5b77759c31db4e62110a595629 100644 GIT binary patch delta 41 tcmcbwbYDq9WLf=RWwjTsMhp-jpLO;2vfY#YTYrZ!9Y~&Yd82~8AOJ%r5C{MO delta 41 tcmcbwbYDq9WLf=RWwjTsMhpd0I}Nz1b<~-{@!z)o-vy{)>k+`kFR0#!oSeu2p%FT5u9nE z_?_^92szq0i=1f#Zk+WB>DY@)C)uvlj(aS-BvoEy4a}MFmiOGC!J0;_j>w%g1S78( znCvm-ddgAB$a0ISUSeJq2qnU;K$NOzlBX4!eCFeofOs9lkTivAuv2bYh5p#pK4i40vS4Evj`9P3WzpT8~^|S delta 186 zcmV;r07d_RG=Maa*aK4*0kPW!1b<#RN2XY^G^P!6U9LObH|cl{~X@35Ep_?*z%uk3_>opDu*;wfn+3vj`9P3M-ITtN;K2 diff --git a/src/Samples/Unity/Checkers/Assets/_Checkers/Libraries/GeneticSharp.Infrastructure.Threading.dll.mdb b/src/Samples/Unity/Checkers/Assets/_Checkers/Libraries/GeneticSharp.Infrastructure.Threading.dll.mdb index 4159e9491d44d5cc9f1a5947dee0829682c6e2f9..3624d9b2c39c0a011970f80119db4f47afcf8238 100644 GIT binary patch delta 41 scmZ3=xs+2uWLf=RWwjTsMhp<3qb2jyNaefR{H#cCCP}O0jS8(S0RQO>1ONa4 delta 41 tcmZ3=xs+2uWLf=RWwjTsMhp<}p5^5EewV|p^YvQ3ZvB18a-%{k3ji+}5DWkS