1
0

HeightScaleConverterTests.cs 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. using System;
  2. using System.Globalization;
  3. namespace DesktopClock.Tests;
  4. public class HeightScaleConverterTests
  5. {
  6. private static readonly CultureInfo TestCulture = CultureInfo.InvariantCulture;
  7. [Theory]
  8. [InlineData(15)]
  9. [InlineData(48)]
  10. [InlineData(64)]
  11. [InlineData(665)]
  12. public void ConvertBack_AfterConvert_ShouldRoundTripHeight(int height)
  13. {
  14. var converter = new HeightScaleConverter();
  15. var logHeight = converter.Convert(height, typeof(double), null, TestCulture);
  16. var roundTrippedHeight = converter.ConvertBack(logHeight, typeof(int), null, TestCulture);
  17. Assert.Equal(height, roundTrippedHeight);
  18. }
  19. [Fact]
  20. public void Convert_ShouldReturnNaturalLogOfHeight()
  21. {
  22. var converter = new HeightScaleConverter();
  23. var result = converter.Convert(48, typeof(double), null, TestCulture);
  24. Assert.Equal(Math.Log(48), (double)result, 10);
  25. }
  26. [Fact]
  27. public void ConvertBack_ShouldReturnExponentiatedHeightAsInteger()
  28. {
  29. var converter = new HeightScaleConverter();
  30. var result = converter.ConvertBack(Math.Log(48), typeof(int), null, TestCulture);
  31. Assert.Equal(48, result);
  32. }
  33. [Fact]
  34. public void ScaleHeight_OneStepUp_ShouldMatchSliderConversion()
  35. {
  36. var converter = new HeightScaleConverter();
  37. const int initialHeight = 48;
  38. var logHeight = (double)converter.Convert(initialHeight, typeof(double), null, TestCulture);
  39. var scaledHeight = HeightScaleConverter.ScaleHeight(initialHeight, 1);
  40. var sliderHeight = (int)converter.ConvertBack(logHeight + 0.1, typeof(int), null, TestCulture);
  41. Assert.Equal(sliderHeight, scaledHeight);
  42. }
  43. [Fact]
  44. public void ScaleHeight_ShouldAdjustAndClamp()
  45. {
  46. var minHeight = (int)Math.Round(Math.Exp(HeightScaleConverter.MinSizeLog));
  47. var maxHeight = (int)Math.Round(Math.Exp(HeightScaleConverter.MaxSizeLog));
  48. Assert.Equal(59, HeightScaleConverter.ScaleHeight(48, 2));
  49. Assert.Equal(52, HeightScaleConverter.ScaleHeight(64, -2));
  50. Assert.Equal(maxHeight, HeightScaleConverter.ScaleHeight(48, 10_000));
  51. Assert.Equal(minHeight, HeightScaleConverter.ScaleHeight(48, -10_000));
  52. }
  53. [Fact]
  54. public void ProvideValue_ShouldReturnSameInstance()
  55. {
  56. var converter = new HeightScaleConverter();
  57. var providedValue = converter.ProvideValue(serviceProvider: null);
  58. Assert.Same(converter, providedValue);
  59. }
  60. }