Created
December 5, 2017 16:25
-
-
Save lilith/a21533ab6b6ba94cefeb120dcb48f67e to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Drawing; | |
using Xunit; | |
using Xunit.Abstractions; | |
namespace AspectTesting | |
{ | |
public class TestConstraintMethods | |
{ | |
private readonly ITestOutputHelper output; | |
public TestConstraintMethods(ITestOutputHelper output) | |
{ | |
this.output = output; | |
} | |
SizeF ConstrainConcise(int imageWidth, int imageHeight, int maxWidth, int maxHeight){ | |
// Downscale by the smallest ratio (never upscale) | |
var scale = Math.Min(1, Math.Min(maxWidth / (float)imageWidth, maxHeight / (float) imageHeight)); | |
return new SizeF(scale * imageWidth, scale * imageHeight); | |
} | |
SizeF ConstrainVerbose(int imageWidth, int imageHeight, int maxWidth, int maxHeight){ | |
// Coalculate the aspect ratios of the image and bounding box | |
var maxAspect = (float) maxWidth / (float) maxHeight; | |
var aspect = (float) imageWidth / (float) imageHeight; | |
// Bounding box aspect is narrower | |
if (maxAspect <= aspect && imageWidth > maxWidth) | |
{ | |
// Use the width bound and calculate the height | |
return new SizeF(maxWidth, Math.Min(maxHeight, maxWidth / aspect)); | |
} | |
else if (maxAspect > aspect && imageHeight > maxHeight) | |
{ | |
// Use the height bound and calculate the width | |
return new SizeF(Math.Min(maxWidth, maxHeight * aspect), maxHeight); | |
}else{ | |
return new SizeF(imageWidth, imageHeight); | |
} | |
} | |
Size RoundSize(SizeF size) => new Size((int)Math.Round(size.Width), (int)Math.Round(size.Height)); | |
Size CeilSize(SizeF size) => new Size((int)Math.Ceiling(size.Width), (int)Math.Ceiling(size.Height)); | |
[Fact] | |
public void TestAll() | |
{ | |
int failures = 0; | |
int upperTestBound = 34; | |
for (var w1 = 1; w1 < upperTestBound; w1++) | |
for (var h1 = 1; h1 < upperTestBound; h1++) | |
for (var w2 = 1; w2 < upperTestBound; w2++) | |
for (var h2 = 1; h2 < upperTestBound; h2++) | |
{ | |
var verboseResult = ConstrainVerbose(w1,h1,w2,h2); | |
var conciseResult = ConstrainConcise(w1,h1,w2,h2); | |
var totalDelta = Math.Abs(verboseResult.Width - conciseResult.Width) + Math.Abs(verboseResult.Height - conciseResult.Height); | |
if (totalDelta > 0.00001){ | |
output.WriteLine($"Bounds {w2}x{h2} Image {w1}x{h1}: Conscise result: {conciseResult.Width}x{conciseResult.Height} Verbose result: {verboseResult.Width}x{verboseResult.Height}"); | |
failures++; | |
} | |
verboseResult = CeilSize(verboseResult); | |
conciseResult = CeilSize(conciseResult); | |
if (verboseResult.Width > w1 || verboseResult.Height > h1 || | |
verboseResult.Width > w2 || verboseResult.Height > h2){ | |
output.WriteLine($"Bounds {w2}x{h2} Image {w1}x{h1}: Conscise result: {conciseResult.Width}x{conciseResult.Height} Verbose result: {verboseResult.Width}x{verboseResult.Height}"); | |
failures++; | |
} | |
} | |
Assert.Equal(failures, 0); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment