Browse Source

Add UnitConverter tests

af/merge-core
James Jackson-South 8 years ago
parent
commit
d56010a9d9
  1. 23
      src/ImageSharp/Common/Helpers/UnitConverter.cs
  2. 41
      tests/ImageSharp.Tests/Helpers/UnitConverterHelperTests.cs

23
src/ImageSharp/Common/Helpers/UnitConverter.cs

@ -1,7 +1,6 @@
// Copyright (c) Six Labors and contributors.
// Licensed under the Apache License, Version 2.0.
using System;
using System.Runtime.CompilerServices;
using SixLabors.ImageSharp.MetaData;
using SixLabors.ImageSharp.MetaData.Profiles.Exif;
@ -19,6 +18,12 @@ namespace SixLabors.ImageSharp.Common.Helpers
/// </summary>
private const double CmsInMeter = 1 / 0.01D;
/// <summary>
/// The number of centimeters in an inch.
/// 1 inch is equal to exactly 2.54 centimeters.
/// </summary>
private const double CmsInInch = 2.54D;
/// <summary>
/// The number of inches in a meter.
/// 1 inch is equal to exactly 0.0254 meters.
@ -57,6 +62,22 @@ namespace SixLabors.ImageSharp.Common.Helpers
[MethodImpl(InliningOptions.ShortMethod)]
public static double InchToMeter(double x) => x * InchesInMeter;
/// <summary>
/// Scales the value from centimeters to inches.
/// </summary>
/// <param name="x">The value to scale.</param>
/// <returns>The <see cref="double"/>.</returns>
[MethodImpl(InliningOptions.ShortMethod)]
public static double CmToInch(double x) => x / CmsInInch;
/// <summary>
/// Scales the value from inches to centimeters.
/// </summary>
/// <param name="x">The value to scale.</param>
/// <returns>The <see cref="double"/>.</returns>
[MethodImpl(InliningOptions.ShortMethod)]
public static double InchToCm(double x) => x * CmsInInch;
/// <summary>
/// Converts an <see cref="ExifTag.ResolutionUnit"/> to a <see cref="PixelResolutionUnit"/>.
/// </summary>

41
tests/ImageSharp.Tests/Helpers/UnitConverterHelperTests.cs

@ -0,0 +1,41 @@
// Copyright (c) Six Labors and contributors.
// Licensed under the Apache License, Version 2.0.
using SixLabors.ImageSharp.Common.Helpers;
using Xunit;
namespace SixLabors.ImageSharp.Tests.Helpers
{
public class UnitConverterHelperTests
{
[Fact]
public void InchToFromMeter()
{
const double expected = 96D;
double actual = UnitConverter.InchToMeter(expected);
actual = UnitConverter.MeterToInch(actual);
Assert.Equal(expected, actual, 15);
}
[Fact]
public void InchToFromCm()
{
const double expected = 96D;
double actual = UnitConverter.InchToCm(expected);
actual = UnitConverter.CmToInch(actual);
Assert.Equal(expected, actual, 15);
}
[Fact]
public void CmToFromMeter()
{
const double expected = 96D;
double actual = UnitConverter.CmToMeter(expected);
actual = UnitConverter.MeterToCm(actual);
Assert.Equal(expected, actual, 15);
}
}
}
Loading…
Cancel
Save