view-by-distance-mklink-con.../Tests/UnitTestCalculations.cs

304 lines
12 KiB
C#

using Humanizer;
using Microsoft.Extensions.Configuration;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Phares.Shared;
using Serilog;
using System.Diagnostics;
using System.Reflection;
using View_by_Distance.Shared.Models;
using View_by_Distance.Shared.Models.Stateless.Methods;
using View_by_Distance.Tests.Models;
namespace View_by_Distance.Tests;
[TestClass]
public class UnitTestCalculations
{
private readonly ILogger _Logger;
private readonly AppSettings _AppSettings;
private readonly string _WorkingDirectory;
private readonly Configuration _Configuration;
private readonly IsEnvironment _IsEnvironment;
private readonly IConfigurationRoot _ConfigurationRoot;
private readonly Property.Models.Configuration _PropertyConfiguration;
public UnitTestCalculations()
{
ILogger logger;
AppSettings appSettings;
string workingDirectory;
Configuration configuration;
IsEnvironment isEnvironment;
IConfigurationRoot configurationRoot;
LoggerConfiguration loggerConfiguration = new();
Property.Models.Configuration propertyConfiguration;
Assembly assembly = Assembly.GetExecutingAssembly();
bool debuggerWasAttachedAtLineZero = Debugger.IsAttached || assembly.Location.Contains(@"\bin\Debug");
isEnvironment = new(processesCount: null, nullASPNetCoreEnvironmentIsDevelopment: debuggerWasAttachedAtLineZero, nullASPNetCoreEnvironmentIsProduction: !debuggerWasAttachedAtLineZero);
IConfigurationBuilder configurationBuilder = new ConfigurationBuilder()
.AddEnvironmentVariables()
.AddJsonFile(isEnvironment.AppSettingsFileName);
configurationRoot = configurationBuilder.Build();
appSettings = Models.Binder.AppSettings.Get(configurationRoot);
workingDirectory = IWorkingDirectory.GetWorkingDirectory(assembly.GetName().Name, appSettings.WorkingDirectoryName);
Environment.SetEnvironmentVariable(nameof(workingDirectory), workingDirectory);
_ = ConfigurationLoggerConfigurationExtensions.Configuration(loggerConfiguration.ReadFrom, configurationRoot);
Log.Logger = loggerConfiguration.CreateLogger();
logger = Log.ForContext<UnitTestCalculations>();
propertyConfiguration = Property.Models.Binder.Configuration.Get(isEnvironment, configurationRoot);
configuration = Models.Binder.Configuration.Get(isEnvironment, configurationRoot, propertyConfiguration);
logger.Information("Complete");
_Logger = logger;
_AppSettings = appSettings;
_Configuration = configuration;
_IsEnvironment = isEnvironment;
_WorkingDirectory = workingDirectory;
_ConfigurationRoot = configurationRoot;
_PropertyConfiguration = propertyConfiguration;
}
[TestMethod]
public void TestMethodNull()
{
Assert.IsFalse(_Logger is null);
Assert.IsFalse(_AppSettings is null);
Assert.IsFalse(_Configuration is null);
Assert.IsFalse(_IsEnvironment is null);
Assert.IsFalse(_WorkingDirectory is null);
Assert.IsFalse(_ConfigurationRoot is null);
Assert.IsFalse(_PropertyConfiguration is null);
}
[TestMethod]
public void TestMethodGetSort() => Assert.IsTrue(new bool[] { true, false }.OrderByDescending(l => l).First());
[TestMethod]
public void TestMethodGetAge()
{
PersonBirthday personBirthday = new(new(1980, 1, 17));
double? age = IPersonBirthday.GetAge(personBirthday);
Assert.IsNotNull(age);
Assert.IsTrue(age.Value > 42.6092);
}
[TestMethod]
public void TestMethodParse()
{
Assert.IsTrue(long.TryParse("(637967784888423594)"[1..^1], out long ticks));
Assert.IsTrue(ticks == 637967784888423594);
Assert.IsFalse(long.TryParse("(637967784888423594.61)"[1..^1], out ticks));
Assert.IsTrue(ticks == 0);
Assert.IsFalse(long.TryParse("(637967784888423594.00)"[1..^1], out ticks));
Assert.IsTrue(ticks == 0);
long l = 637967784888423594;
double d;
d = 0.61;
Assert.IsTrue($"({l}{d.ToString("0.00")[1..]})" == "(637967784888423594.61)");
d = 0.6;
Assert.IsTrue($"({l}{d.ToString("0.00")[1..]})" == "(637967784888423594.60)");
d = 0.615;
Assert.IsTrue($"({l}{d.ToString("0.00")[1..]})" == "(637967784888423594.62)");
d = 0.45;
Assert.IsTrue($"({l}{d.ToString("0.00")[1..]})" == "(637967784888423594.45)");
}
[TestMethod]
public void TestMethodHumanize()
{
string successfull = $"_ {"ManualCopy".Humanize(LetterCasing.Title)} Successfull";
Assert.IsTrue(successfull == "_ Manual Copy Successfull");
}
[TestMethod]
public void TestMethodDamn()
{
string name;
string[] directories;
string? directoryName;
string checkDirectory;
string sourceDirectory = @"F:\Tmp\Phares\Compare\Images 2022-09-15 - 7390c13 - III - Results\E) Distance\2022-09-15\7680 x 4320\7680x4320 - Hog - Large\()";
directories = Directory.GetDirectories(sourceDirectory, "*", SearchOption.TopDirectoryOnly);
foreach (string directory in directories)
{
directoryName = Path.GetDirectoryName(directory);
if (directoryName is null)
continue;
name = Path.GetFileName(directory);
if (name.Length is 1 or 20)
continue;
checkDirectory = Path.Combine(directoryName, "b", name);
Directory.Move(directory, checkDirectory);
}
directories = Directory.GetDirectories(Path.Combine(sourceDirectory, "b"), "*", SearchOption.TopDirectoryOnly);
foreach (string directory in directories)
{
directoryName = Path.GetDirectoryName(directory);
if (directoryName is null)
continue;
name = Path.GetFileName(directory);
if (name.Length is 1 or 20)
continue;
checkDirectory = Path.Combine(directoryName, $"{name[..^4]})");
if (Directory.Exists(checkDirectory))
continue;
Directory.Move(directory, checkDirectory);
}
Assert.IsTrue(true);
}
[TestMethod]
public void TestMethodDel()
{
string source = @"F:\Tmp\Phares\Compare\Images 2022-09-15 - 7390c13 - III - Results\E) Distance\2022-09-15\7680 x 4320\7680x4320 - Hog - Large\()\(637992984751968513)";
for (int i = 1; i < 11; i++)
_ = IPath.DeleteEmptyDirectories(source);
Assert.IsTrue(true);
}
[TestMethod]
public void TestMethodDirectory()
{
string[] names;
names = IPath.GetDirectoryNames(@"C:\Tmp\phares");
Assert.IsTrue(names.Length == 3);
names = IPath.GetDirectoryNames(@"C:\Tmp\phares\");
Assert.IsTrue(names.Length == 3);
names = IPath.GetDirectoryNames(@"C:\Tmp\phares\Pictures - Results\E) Distance\2022-09-15\7680 x 4320\7680x4320 - Hog - Large\()\(637991752537712052)\1976-03-08_00\#2019\K\-735727008.520765.jpg");
Assert.IsTrue(names.Length == 13);
// Length = 13
// [0] [string]:
// "C:\\"
// [1] [string]:
// "Tmp"
// [2] [string]:
// "phares"
// [3] [string]:
// "Pictures - Results"
// [4] [string]:
// "E) Distance"
// [5] [string]:
// "2022-09-15"
// [6] [string]:
// "7680 x 4320"
// [7] [string]:
// "7680x4320 - Hog - Large"
// [8] [string]:
// "()"
}
[TestMethod]
public void TestGetLocation()
{
double confidence = 0.1D;
int left, top, right, bottom, width, height;
left = 20;
top = 40;
right = 60;
bottom = 80;
width = 100;
height = 100;
Location location = new(bottom, confidence, left, null, right, top);
_ = new Location(confidence, height, location, Shared.Models.Stateless.ILocation.Digits, Shared.Models.Stateless.ILocation.Factor, width, 1);
_ = new Location(bottom, confidence, height, left, Shared.Models.Stateless.ILocation.Digits, Shared.Models.Stateless.ILocation.Factor, right, top, width, 1);
}
[TestMethod]
public void TestGetPixelPercentage()
{
int normalizedPixelPercentage;
int bottom, height, left, right, top, width;
left = 1;
top = 1;
right = 10;
bottom = 10;
width = 100;
height = 100;
normalizedPixelPercentage = ILocation.GetNormalizedPixelPercentage(bottom, height, left, Shared.Models.Stateless.ILocation.Digits, Shared.Models.Stateless.ILocation.Factor, right, top, width);
Assert.IsTrue(normalizedPixelPercentage == 055005502);
left = 50;
top = 50;
right = 60;
bottom = 60;
width = 100;
height = 100;
normalizedPixelPercentage = ILocation.GetNormalizedPixelPercentage(bottom, height, left, Shared.Models.Stateless.ILocation.Digits, Shared.Models.Stateless.ILocation.Factor, right, top, width);
Assert.IsTrue(normalizedPixelPercentage == 550055002);
}
[TestMethod]
public void TestGetPixelPercentageB()
{
int normalizedPixelPercentage;
int bottom, height, left, right, top, width;
left = 240;
top = 240;
right = 260;
bottom = 260;
width = 500;
height = 500;
normalizedPixelPercentage = ILocation.GetNormalizedPixelPercentage(bottom, height, left, Shared.Models.Stateless.ILocation.Digits, Shared.Models.Stateless.ILocation.Factor, right, top, width);
Assert.IsTrue(normalizedPixelPercentage == 500050002);
left = 490;
top = 490;
right = 510;
bottom = 510;
width = 1000;
height = 1000;
normalizedPixelPercentage = ILocation.GetNormalizedPixelPercentage(bottom, height, left, Shared.Models.Stateless.ILocation.Digits, Shared.Models.Stateless.ILocation.Factor, right, top, width);
Assert.IsTrue(normalizedPixelPercentage == 500050002);
left++;
normalizedPixelPercentage = ILocation.GetNormalizedPixelPercentage(bottom, height, left, Shared.Models.Stateless.ILocation.Digits, Shared.Models.Stateless.ILocation.Factor, right, top, width);
Assert.IsTrue(normalizedPixelPercentage == 500550001);
left++;
normalizedPixelPercentage = ILocation.GetNormalizedPixelPercentage(bottom, height, left, Shared.Models.Stateless.ILocation.Digits, Shared.Models.Stateless.ILocation.Factor, right, top, width);
Assert.IsTrue(normalizedPixelPercentage == 501050001);
}
[TestMethod]
public void TestGetPixelPercentageC()
{
int normalizedPixelPercentage;
int bottom, height, left, right, top, width;
left = 20;
top = 40;
right = 60;
bottom = 80;
width = 100;
height = 100;
normalizedPixelPercentage = ILocation.GetNormalizedPixelPercentage(bottom, height, left, Shared.Models.Stateless.ILocation.Digits, Shared.Models.Stateless.ILocation.Factor, right, top, width);
Assert.IsTrue(normalizedPixelPercentage == 400060002);
left = 20;
top = 40;
right = 60;
bottom = 80;
width = 100;
height = 100;
normalizedPixelPercentage = ILocation.GetNormalizedPixelPercentage(bottom, height, left, Shared.Models.Stateless.ILocation.Digits, Shared.Models.Stateless.ILocation.Factor, right, top, width);
Assert.IsTrue(normalizedPixelPercentage == 400060002);
}
[TestMethod]
public void TestGetPixelPercentageD()
{
int normalizedPixelPercentage;
int bottom, height, left, right, top, width;
left = 7678;
top = 4318;
right = 7680;
bottom = 4320;
width = 7680;
height = 4320;
normalizedPixelPercentage = ILocation.GetNormalizedPixelPercentage(bottom, height, left, Shared.Models.Stateless.ILocation.Digits, Shared.Models.Stateless.ILocation.Factor, right, top, width);
Assert.IsTrue(normalizedPixelPercentage == 999999981);
left = 7680;
top = 4320;
right = 7680;
bottom = 4320;
width = 7680;
height = 4320;
normalizedPixelPercentage = ILocation.GetNormalizedPixelPercentage(bottom, height, left, Shared.Models.Stateless.ILocation.Digits, Shared.Models.Stateless.ILocation.Factor, right, top, width);
Assert.IsTrue(normalizedPixelPercentage == 999999991);
}
}