Browse Source

Adding unit tests.

Former-commit-id: 9221e0553a40b8e5cb8c3b2b031703e7a072a206
af/merge-core
James South 13 years ago
parent
commit
44a9c7f9a8
  1. 11
      README.md
  2. 14
      src/ImageProcessor.Tests/ImageProcessor.Tests.csproj
  3. 81
      src/ImageProcessor.Tests/RegularExpressionUnitTests.cs
  4. 18
      src/ImageProcessor.Tests/UnitTest1.cs
  5. 2
      src/ImageProcessor.Web/Caching/SQLContext.cs
  6. 21
      src/ImageProcessor/Helpers/Extensions/StringExtensions.cs
  7. 25
      src/ImageProcessor/ImageFactory.cs
  8. 2
      src/ImageProcessor/Imaging/Filters/BlackWhiteMatrixFilter.cs
  9. 2
      src/ImageProcessor/Imaging/Filters/ComicMatrixFilter.cs
  10. 2
      src/ImageProcessor/Imaging/Filters/GothamMatrixFilter.cs
  11. 2
      src/ImageProcessor/Imaging/Filters/GreyScaleMatrixFilter.cs
  12. 2
      src/ImageProcessor/Imaging/Filters/HiSatchMatrixFilter.cs
  13. 2
      src/ImageProcessor/Imaging/Filters/IMatrixFilter.cs
  14. 2
      src/ImageProcessor/Imaging/Filters/InvertMatrixFilter.cs
  15. 2
      src/ImageProcessor/Imaging/Filters/LoSatchMatrixFilter.cs
  16. 2
      src/ImageProcessor/Imaging/Filters/LomographMatrixFilter.cs
  17. 2
      src/ImageProcessor/Imaging/Filters/PolaroidMatrixFilter.cs
  18. 2
      src/ImageProcessor/Imaging/Filters/SepiaMatrixFilter.cs
  19. 2
      src/ImageProcessor/Processors/Filter.cs
  20. 7
      src/ImageProcessor/Processors/Flip.cs
  21. 32
      src/Test/Test/Controllers/HomeController.cs
  22. 3
      src/Test/Test/Images/negative.png
  23. 1
      src/Test/Test/Images/negative2.png.REMOVED.git-id
  24. 20
      src/Test/Test/Views/Home/Index.cshtml

11
README.md

@ -121,11 +121,22 @@ Current filters include:
- gotham
- hisatch
- losatch
- invert
e.g.
<img src="/images.yourimage.jpg?filter=polaroid" alt="classic polaroid look"/>
Flip
=======
Imageprocessor can flip your image either horizontally or vertically.
e.g.
<img src="/images.yourimage.jpg?flip=horizontal" alt="horizontal"/>
<img src="/images.yourimage.jpg?flip=vertical" alt="vertical"/>
Watermark
=========

14
src/ImageProcessor.Tests/ImageProcessor.Tests.csproj

@ -53,7 +53,9 @@
<CodeAnalysisRuleSet>MinimumRecommendedRules.ruleset</CodeAnalysisRuleSet>
</PropertyGroup>
<ItemGroup>
<Reference Include="Microsoft.CSharp" />
<Reference Include="System" />
<Reference Include="System.Drawing" />
</ItemGroup>
<Choose>
<When Condition="('$(VisualStudioVersion)' == '10.0' or '$(VisualStudioVersion)' == '') and '$(TargetFrameworkVersion)' == 'v3.5'">
@ -68,9 +70,19 @@
</Otherwise>
</Choose>
<ItemGroup>
<Compile Include="UnitTest1.cs" />
<Compile Include="RegularExpressionUnitTests.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\ImageProcessor.Web\ImageProcessor.Web.csproj">
<Project>{4f7050f2-465f-4e10-8db2-2fb97ac6aa43}</Project>
<Name>ImageProcessor.Web</Name>
</ProjectReference>
<ProjectReference Include="..\ImageProcessor\ImageProcessor.csproj">
<Project>{3b5dd734-fb7a-487d-8ce6-55e7af9aea7e}</Project>
<Name>ImageProcessor</Name>
</ProjectReference>
</ItemGroup>
<Choose>
<When Condition="'$(VisualStudioVersion)' == '10.0' And '$(IsCodedUITest)' == 'True'">
<ItemGroup>

81
src/ImageProcessor.Tests/RegularExpressionUnitTests.cs

@ -0,0 +1,81 @@
namespace ImageProcessor.Tests
{
#region Using
using System;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Text.RegularExpressions;
using ImageProcessor.Imaging;
using ImageProcessor.Processors;
using Microsoft.VisualStudio.TestTools.UnitTesting;
#endregion
[TestClass]
public class RegularExpressionUnitTests
{
#region Regular Expression Tests
[TestMethod]
public void TestAlphaRegex()
{
string querystring = "alpha=56";
int expected = 56;
Alpha alpha = new Alpha();
alpha.MatchRegexIndex(querystring);
int actual = alpha.DynamicParameter;
Assert.AreEqual<int>(expected, actual);
}
[TestMethod]
public void TestFormatRegex()
{
string querystring = "format=gif";
string expected = "gif";
Format format = new Format();
format.MatchRegexIndex(querystring);
string actual = format.DynamicParameter;
Assert.AreEqual(expected, actual);
}
[TestMethod]
public void TestQualityRegex()
{
string querystring = "quality=56";
int expected = 56;
Quality quality = new Quality();
quality.MatchRegexIndex(querystring);
int actual = quality.DynamicParameter;
Assert.AreEqual<int>(expected, actual);
}
[TestMethod]
public void TestRotateRegex()
{
string querystring = "rotate=270";
RotateLayer expected = new RotateLayer
{
Angle = 270,
BackgroundColor = Color.Transparent
};
Rotate rotate = new Rotate();
rotate.MatchRegexIndex(querystring);
RotateLayer actual = rotate.DynamicParameter;
Debug.Print("{0}{1}", actual.Angle, actual.BackgroundColor);
Assert.AreEqual<RotateLayer>(expected, actual);
}
#endregion
}
}

18
src/ImageProcessor.Tests/UnitTest1.cs

@ -1,18 +0,0 @@
using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace ImageProcessor.Tests
{
using System.IO;
[TestClass]
public class UnitTest1
{
[TestMethod]
public void TestMethod1()
{
}
}
}

2
src/ImageProcessor.Web/Caching/SQLContext.cs

@ -29,7 +29,7 @@ namespace ImageProcessor.Web.Caching
/// <summary>
/// The cached index location.
/// </summary>
private static readonly string IndexLocation = Path.Combine(HostingEnvironment.MapPath(VirtualCachePath), "imagecache.sqlite");
private static readonly string IndexLocation = Path.Combine(HostingEnvironment.MapPath(VirtualCachePath), "cache.db");
/// <summary>
/// The connection string.

21
src/ImageProcessor/Helpers/Extensions/StringExtensions.cs

@ -85,6 +85,27 @@ namespace ImageProcessor.Helpers.Extensions
.ToString().ToLowerInvariant();
}
}
/// <summary>
/// Creates an SHA512 fingerprint of the String.
/// </summary>
/// <param name="expression">The <see cref="T:System.String">String</see> instance that this method extends.</param>
/// <returns>An SHA256 fingerprint of the String.</returns>
public static string ToSHA512Fingerprint(this string expression)
{
byte[] bytes = Encoding.ASCII.GetBytes(expression.ToCharArray());
using (SHA512CryptoServiceProvider sha512 = new SHA512CryptoServiceProvider())
{
byte[] hash = sha512.ComputeHash(bytes);
// Concatenate the hash bytes into one long String.
return hash.Aggregate(
new StringBuilder(70),
(sb, b) => sb.Append(b.ToString("X2", CultureInfo.InvariantCulture)))
.ToString().ToLowerInvariant();
}
}
#endregion
#region Numbers

25
src/ImageProcessor/ImageFactory.cs

@ -254,6 +254,31 @@ namespace ImageProcessor
return this;
}
/// <summary>
/// Flips an image either horizontally or vertically.
/// </summary>
/// <param name="flipVertically">
/// Whether to flip the image vertically.
/// </param>
/// <returns>
/// The current instance of the <see cref="T:ImageProcessor.ImageFactory"/> class.
/// </returns>
public ImageFactory Flip(bool flipVertically)
{
if (this.ShouldProcess)
{
RotateFlipType rotateFlipType = flipVertically == false
? RotateFlipType.RotateNoneFlipX
: RotateFlipType.RotateNoneFlipY;
Flip flip = new Flip { DynamicParameter = rotateFlipType };
this.Image = flip.ProcessImage(this);
}
return this;
}
/// <summary>
/// Sets the output format of the image to the matching <see cref="T:System.Drawing.Imaging.ImageFormat"/>.
/// </summary>

2
src/ImageProcessor/Imaging/Filters/BlackWhiteMatrixFilter.cs

@ -41,7 +41,7 @@ namespace ImageProcessor.Imaging.Filters
/// <returns>
/// The processed image from the current instance of the <see cref="T:ImageProcessor.ImageFactory"/> class.
/// </returns>
public Image ProcessImage(ImageFactory factory, Image image, Image newImage)
public Image TransformImage(ImageFactory factory, Image image, Image newImage)
{
using (Graphics graphics = Graphics.FromImage(newImage))
{

2
src/ImageProcessor/Imaging/Filters/ComicMatrixFilter.cs

@ -65,7 +65,7 @@ namespace ImageProcessor.Imaging.Filters
/// <returns>
/// The processed image from the current instance of the <see cref="T:ImageProcessor.ImageFactory"/> class.
/// </returns>
public Image ProcessImage(ImageFactory factory, Image image, Image newImage)
public Image TransformImage(ImageFactory factory, Image image, Image newImage)
{
// Bitmaps for comic pattern
Bitmap hisatchBitmap = null;

2
src/ImageProcessor/Imaging/Filters/GothamMatrixFilter.cs

@ -42,7 +42,7 @@ namespace ImageProcessor.Imaging.Filters
/// <returns>
/// The processed image from the current instance of the <see cref="T:ImageProcessor.ImageFactory"/> class.
/// </returns>
public Image ProcessImage(ImageFactory factory, Image image, Image newImage)
public Image TransformImage(ImageFactory factory, Image image, Image newImage)
{
using (Graphics graphics = Graphics.FromImage(newImage))
{

2
src/ImageProcessor/Imaging/Filters/GreyScaleMatrixFilter.cs

@ -41,7 +41,7 @@ namespace ImageProcessor.Imaging.Filters
/// <returns>
/// The processed image from the current instance of the <see cref="T:ImageProcessor.ImageFactory"/> class.
/// </returns>
public Image ProcessImage(ImageFactory factory, Image image, Image newImage)
public Image TransformImage(ImageFactory factory, Image image, Image newImage)
{
using (Graphics graphics = Graphics.FromImage(newImage))
{

2
src/ImageProcessor/Imaging/Filters/HiSatchMatrixFilter.cs

@ -41,7 +41,7 @@ namespace ImageProcessor.Imaging.Filters
/// <returns>
/// The processed image from the current instance of the <see cref="T:ImageProcessor.ImageFactory"/> class.
/// </returns>
public Image ProcessImage(ImageFactory factory, Image image, Image newImage)
public Image TransformImage(ImageFactory factory, Image image, Image newImage)
{
using (Graphics graphics = Graphics.FromImage(newImage))
{

2
src/ImageProcessor/Imaging/Filters/IMatrixFilter.cs

@ -35,7 +35,7 @@ namespace ImageProcessor.Imaging.Filters
/// <returns>
/// The processed image from the current instance of the <see cref="T:ImageProcessor.ImageFactory"/> class.
/// </returns>
Image ProcessImage(ImageFactory factory, Image image, Image newImage);
Image TransformImage(ImageFactory factory, Image image, Image newImage);
#endregion
}
}

2
src/ImageProcessor/Imaging/Filters/InvertMatrixFilter.cs

@ -41,7 +41,7 @@ namespace ImageProcessor.Imaging.Filters
/// <returns>
/// The processed image from the current instance of the <see cref="T:ImageProcessor.ImageFactory"/> class.
/// </returns>
public Image ProcessImage(ImageFactory factory, Image image, Image newImage)
public Image TransformImage(ImageFactory factory, Image image, Image newImage)
{
using (Graphics graphics = Graphics.FromImage(newImage))
{

2
src/ImageProcessor/Imaging/Filters/LoSatchMatrixFilter.cs

@ -41,7 +41,7 @@ namespace ImageProcessor.Imaging.Filters
/// <returns>
/// The processed image from the current instance of the <see cref="T:ImageProcessor.ImageFactory"/> class.
/// </returns>
public Image ProcessImage(ImageFactory factory, Image image, Image newImage)
public Image TransformImage(ImageFactory factory, Image image, Image newImage)
{
using (Graphics graphics = Graphics.FromImage(newImage))
{

2
src/ImageProcessor/Imaging/Filters/LomographMatrixFilter.cs

@ -42,7 +42,7 @@ namespace ImageProcessor.Imaging.Filters
/// <returns>
/// The processed image from the current instance of the <see cref="T:ImageProcessor.ImageFactory"/> class.
/// </returns>
public Image ProcessImage(ImageFactory factory, Image image, Image newImage)
public Image TransformImage(ImageFactory factory, Image image, Image newImage)
{
using (Graphics graphics = Graphics.FromImage(newImage))
{

2
src/ImageProcessor/Imaging/Filters/PolaroidMatrixFilter.cs

@ -43,7 +43,7 @@ namespace ImageProcessor.Imaging.Filters
/// <returns>
/// The processed image from the current instance of the <see cref="T:ImageProcessor.ImageFactory"/> class.
/// </returns>
public Image ProcessImage(ImageFactory factory, Image image, Image newImage)
public Image TransformImage(ImageFactory factory, Image image, Image newImage)
{
using (Graphics graphics = Graphics.FromImage(newImage))
{

2
src/ImageProcessor/Imaging/Filters/SepiaMatrixFilter.cs

@ -41,7 +41,7 @@ namespace ImageProcessor.Imaging.Filters
/// <returns>
/// The processed image from the current instance of the <see cref="T:ImageProcessor.ImageFactory"/> class.
/// </returns>
public Image ProcessImage(ImageFactory factory, Image image, Image newImage)
public Image TransformImage(ImageFactory factory, Image image, Image newImage)
{
using (Graphics graphics = Graphics.FromImage(newImage))
{

2
src/ImageProcessor/Processors/Filter.cs

@ -178,7 +178,7 @@ namespace ImageProcessor.Processors
if (matrix != null)
{
return matrix.ProcessImage(factory, image, newImage);
return matrix.TransformImage(factory, image, newImage);
}
}
catch

7
src/ImageProcessor/Processors/Flip.cs

@ -109,7 +109,7 @@ namespace ImageProcessor.Processors
{
// Set the index on the first instance only.
this.SortOrder = match.Index;
string direction = match.Value;
string direction = match.Value.Split('=')[1];
this.DynamicParameter = direction == "horizontal"
? RotateFlipType.RotateNoneFlipX
@ -143,9 +143,10 @@ namespace ImageProcessor.Processors
RotateFlipType rotateFlipType = this.DynamicParameter;
newImage = (Bitmap)image.Clone();
// Might not need this.
// Tag doesn't get cloned.
newImage.Tag = image.Tag;
// Flip
newImage.RotateFlip(rotateFlipType);
image.Dispose();

32
src/Test/Test/Controllers/HomeController.cs

@ -60,27 +60,29 @@ namespace Test.Controllers
DateTime start = DateTime.Now;
List<double> collisions = new List<double>();
const int Iterations = 1;
const int Maxitems = 360000;
const int Iterations = 1;
const int Maxitems = 360000;
for (int i = 0; i < Iterations; i++)
{
List<string> paths = new List<string>();
for (int i = 0; i < Iterations; i++)
{
List<string> paths = new List<string>();
for (int j = 0; j < Maxitems; j++)
{
string path = Path.GetRandomFileName().ToSHA256Fingerprint().Substring(0, 8);
for (int j = 0; j < Maxitems; j++)
{
string path = Path.GetRandomFileName().ToMD5Fingerprint();
paths.Add(path);
}
path = string.Format("{0}{1}", path.Substring(0, 4), path.Substring(16, 4));
int count = paths.Distinct().Count();
paths.Add(path);
}
double collisionRate = ((Maxitems - count) * 100D) / Maxitems;
collisions.Add(collisionRate);
}
int count = paths.Distinct().Count();
double collisionRate = ((Maxitems - count) * 100D) / Maxitems;
collisions.Add(collisionRate);
}
double averageCollisionRate = collisions.Average();
double averageCollisionRate = collisions.Average();
//PersistantDictionary persistantDictionary = PersistantDictionary.Instance;

3
src/Test/Test/Images/negative.png

@ -0,0 +1,3 @@
version https://git-lfs.github.com/spec/v1
oid sha256:977cc9071257655c9923d267ea5bd417b69754367c2f1aa8765247b68e2bb878
size 1539

1
src/Test/Test/Images/negative2.png.REMOVED.git-id

@ -0,0 +1 @@
ba8235d9051c4409559b4ec943172574041c0cf0

20
src/Test/Test/Views/Home/Index.cshtml

@ -55,6 +55,14 @@
<h3>losatch</h3>
<img src="/images/Penguins.jpg?width=300&filter=losatch" />
</li>
<li>
<h3>invert</h3>
<img src="/images/negative.png?filter=invert" />
</li>
<li>
<h3>invert</h3>
<img src="/images/negative2.png?filter=invert" />
</li>
</ul>
</div>
</section>
@ -94,6 +102,18 @@
</div>
</div>
</section>
<section>
<div class="container">
<div class="clmn2">
<h2>Flip - horizontal</h2>
<img src="/images/Penguins.jpg?width=300&flip=horizontal" />
</div>
<div class="clmn2">
<h2>Flip - vertical</h2>
<img src="/images/Penguins.jpg?width=300&flip=vertical" />
</div>
</div>
</section>
</article>
<article>
<h1 class="container">Gif</h1>

Loading…
Cancel
Save