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

513 lines
23 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;
right = 60;
top = 40;
bottom = 80;
width = 100;
height = 100;
Location location = new(bottom, confidence, left, 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 TestAreaPermille()
{
Location location;
double confidence = 0.1D;
int areaPermille, left, top, right, bottom, width, height;
left = 0;
right = 100;
top = 0;
bottom = 100;
width = 100;
height = 100;
areaPermille = IMapping.GetAreaPermille(bottom, height, left, right, top, width);
Assert.IsTrue(areaPermille == 1000);
left = 0;
right = 50;
top = 0;
bottom = 50;
width = 100;
height = 100;
location = new(bottom, confidence, height, left, Shared.Models.Stateless.ILocation.Digits, Shared.Models.Stateless.ILocation.Factor, right, top, width, 1);
areaPermille = IMapping.GetAreaPermille(height, location, width);
Assert.IsTrue(areaPermille == 250);
left = 0;
right = 25;
top = 0;
bottom = 25;
width = 100;
height = 100;
location = new(bottom, confidence, height, left, Shared.Models.Stateless.ILocation.Digits, Shared.Models.Stateless.ILocation.Factor, right, top, width, 1);
OutputResolution outputResolution = new(height, 0, width);
areaPermille = IMapping.GetAreaPermille(location, outputResolution);
Assert.IsTrue(areaPermille == 62);
}
[TestMethod]
public void TestGetPixelPercentageA()
{
int? x, y;
int normalizedPixelPercentage;
int bottom, height, left, right, top, width;
string normalizedPixelPercentagePadded;
left = 7678;
right = 7680;
top = 4318;
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);
normalizedPixelPercentagePadded = ILocation.GetLeftPadded(Shared.Models.Stateless.ILocation.Digits, normalizedPixelPercentage);
(x, y) = ILocation.GetXY(Shared.Models.Stateless.ILocation.Digits, Shared.Models.Stateless.ILocation.Factor, width, height, normalizedPixelPercentagePadded);
Assert.IsTrue(x.HasValue && x.Value == 7679);
Assert.IsTrue(y.HasValue && y.Value == 4319);
left = 7680;
right = 7680;
top = 4320;
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);
normalizedPixelPercentagePadded = ILocation.GetLeftPadded(Shared.Models.Stateless.ILocation.Digits, normalizedPixelPercentage);
(x, y) = ILocation.GetXY(Shared.Models.Stateless.ILocation.Digits, Shared.Models.Stateless.ILocation.Factor, width, height, normalizedPixelPercentagePadded);
Assert.IsTrue(x.HasValue && x.Value == 7679);
Assert.IsTrue(y.HasValue && y.Value == 4320);
}
[TestMethod]
public void TestGetPixelPercentageB()
{
int? x, y;
int normalizedPixelPercentage;
int bottom, height, left, right, top, width;
string normalizedPixelPercentagePadded;
left = 20;
right = 60;
top = 40;
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);
normalizedPixelPercentagePadded = ILocation.GetLeftPadded(Shared.Models.Stateless.ILocation.Digits, normalizedPixelPercentage);
(x, y) = ILocation.GetXY(Shared.Models.Stateless.ILocation.Digits, Shared.Models.Stateless.ILocation.Factor, width, height, normalizedPixelPercentagePadded);
Assert.IsTrue(x.HasValue && x.Value == 40);
Assert.IsTrue(y.HasValue && y.Value == 60);
left = 45;
right = 55;
top = 45;
bottom = 55;
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 == 500050002);
normalizedPixelPercentagePadded = ILocation.GetLeftPadded(Shared.Models.Stateless.ILocation.Digits, normalizedPixelPercentage);
(x, y) = ILocation.GetXY(Shared.Models.Stateless.ILocation.Digits, Shared.Models.Stateless.ILocation.Factor, width, height, normalizedPixelPercentagePadded);
Assert.IsTrue(x.HasValue && x.Value == 50);
Assert.IsTrue(y.HasValue && y.Value == 50);
}
[TestMethod]
public void TestGetPixelPercentageC()
{
int? x, y;
int normalizedPixelPercentage;
string normalizedPixelPercentagePadded;
int bottom, height, left, right, top, width;
left = 1;
right = 3;
top = 1;
bottom = 3;
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 == 20002002);
normalizedPixelPercentagePadded = ILocation.GetLeftPadded(Shared.Models.Stateless.ILocation.Digits, normalizedPixelPercentage);
(x, y) = ILocation.GetXY(Shared.Models.Stateless.ILocation.Digits, Shared.Models.Stateless.ILocation.Factor, width, height, normalizedPixelPercentagePadded);
Assert.IsTrue(x.HasValue && x.Value == 2);
Assert.IsTrue(y.HasValue && y.Value == 2);
left = 50;
right = 60;
top = 50;
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);
normalizedPixelPercentagePadded = ILocation.GetLeftPadded(Shared.Models.Stateless.ILocation.Digits, normalizedPixelPercentage);
(x, y) = ILocation.GetXY(Shared.Models.Stateless.ILocation.Digits, Shared.Models.Stateless.ILocation.Factor, width, height, normalizedPixelPercentagePadded);
Assert.IsTrue(x.HasValue && x.Value == 55);
Assert.IsTrue(y.HasValue && y.Value == 55);
}
[TestMethod]
public void TestGetPixelPercentageD()
{
int? x, y;
int normalizedPixelPercentage;
int bottom, height, left, right, top, width;
string normalizedPixelPercentagePadded;
left = 240;
right = 260;
top = 240;
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);
normalizedPixelPercentagePadded = ILocation.GetLeftPadded(Shared.Models.Stateless.ILocation.Digits, normalizedPixelPercentage);
(x, y) = ILocation.GetXY(Shared.Models.Stateless.ILocation.Digits, Shared.Models.Stateless.ILocation.Factor, width, height, normalizedPixelPercentagePadded);
Assert.IsTrue(x.HasValue && x.Value == 250);
Assert.IsTrue(y.HasValue && y.Value == 250);
left = 490;
right = 510;
top = 490;
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);
normalizedPixelPercentagePadded = ILocation.GetLeftPadded(Shared.Models.Stateless.ILocation.Digits, normalizedPixelPercentage);
(x, y) = ILocation.GetXY(Shared.Models.Stateless.ILocation.Digits, Shared.Models.Stateless.ILocation.Factor, width, height, normalizedPixelPercentagePadded);
Assert.IsTrue(x.HasValue && x.Value == 500);
Assert.IsTrue(y.HasValue && y.Value == 500);
left++;
normalizedPixelPercentage = ILocation.GetNormalizedPixelPercentage(bottom, height, left, Shared.Models.Stateless.ILocation.Digits, Shared.Models.Stateless.ILocation.Factor, right, top, width);
Assert.IsTrue(normalizedPixelPercentage == 500550001);
normalizedPixelPercentagePadded = ILocation.GetLeftPadded(Shared.Models.Stateless.ILocation.Digits, normalizedPixelPercentage);
(x, y) = ILocation.GetXY(Shared.Models.Stateless.ILocation.Digits, Shared.Models.Stateless.ILocation.Factor, width, height, normalizedPixelPercentagePadded);
Assert.IsTrue(x.HasValue && x.Value == 500);
Assert.IsTrue(y.HasValue && y.Value == 500);
left++;
normalizedPixelPercentage = ILocation.GetNormalizedPixelPercentage(bottom, height, left, Shared.Models.Stateless.ILocation.Digits, Shared.Models.Stateless.ILocation.Factor, right, top, width);
Assert.IsTrue(normalizedPixelPercentage == 501050001);
normalizedPixelPercentagePadded = ILocation.GetLeftPadded(Shared.Models.Stateless.ILocation.Digits, normalizedPixelPercentage);
(x, y) = ILocation.GetXY(Shared.Models.Stateless.ILocation.Digits, Shared.Models.Stateless.ILocation.Factor, width, height, normalizedPixelPercentagePadded);
Assert.IsTrue(x.HasValue && x.Value == 501);
Assert.IsTrue(y.HasValue && y.Value == 500);
}
[TestMethod]
public void TestGetPixelPercentageE()
{
int? x, y;
int normalizedPixelPercentage;
int bottom, height, left, right, top, width;
string normalizedPixelPercentagePadded;
left = 1477;
right = 1477;
top = 641;
bottom = 641;
width = 2408;
height = 2077;
normalizedPixelPercentage = ILocation.GetNormalizedPixelPercentage(bottom, height, left, Shared.Models.Stateless.ILocation.Digits, Shared.Models.Stateless.ILocation.Factor, right, top, width);
Assert.IsTrue(normalizedPixelPercentage == 613430861);
normalizedPixelPercentagePadded = ILocation.GetLeftPadded(Shared.Models.Stateless.ILocation.Digits, normalizedPixelPercentage);
(x, y) = ILocation.GetXY(Shared.Models.Stateless.ILocation.Digits, Shared.Models.Stateless.ILocation.Factor, width, height, normalizedPixelPercentagePadded);
Assert.IsTrue(x.HasValue && x.Value == 1477);
Assert.IsTrue(y.HasValue && y.Value == 641);
}
[TestMethod]
public void TestGetPixelPercentageF()
{
int? x, y;
int normalizedPixelPercentage;
int bottom, height, left, right, top, width;
string normalizedPixelPercentagePadded;
width = 2048;
height = 1365;
normalizedPixelPercentagePadded = "617214031";
(x, y) = ILocation.GetXY(Shared.Models.Stateless.ILocation.Digits, Shared.Models.Stateless.ILocation.Factor, width, height, normalizedPixelPercentagePadded);
if (x is null || y is null)
throw new Exception();
Assert.IsTrue(x.Value == 1264);
Assert.IsTrue(y.Value == 192);
left = x.Value;
right = x.Value;
top = y.Value;
bottom = y.Value;
normalizedPixelPercentage = ILocation.GetNormalizedPixelPercentage(bottom, height, left, Shared.Models.Stateless.ILocation.Digits, Shared.Models.Stateless.ILocation.Factor, right, top, width);
Assert.IsTrue(normalizedPixelPercentage == 617214071);
double x1, x2, y1, y2;
x1 = x.Value;
x2 = x.Value;
y1 = y.Value;
y2 = y.Value - 3;
double distance = Math.Sqrt(Math.Pow(x1 - x2, 2) + Math.Pow(y1 - y2, 2));
Assert.IsTrue(distance == 3);
}
[TestMethod]
public void TestGetDistance()
{
double x1, x2, y1, y2;
x1 = 12f;
x2 = 13f;
y1 = 11f;
y2 = 10f;
double distance = Math.Sqrt(Math.Pow(x1 - x2, 2) + Math.Pow(y1 - y2, 2));
Assert.IsTrue(distance == 1.4142135623730951);
}
private static (string?, string, string) Get(string[] segments)
{
(string?, string, string) result;
if ((!segments[0].Contains('#') && (segments[3].Contains('~') || segments[3].Contains('#'))) || (segments[0].Contains('#') && !segments[3].Contains('#')))
result = new(null, segments[3], segments[4]);
else
result = new(segments[0], segments[3], segments[4]);
return result;
}
[TestMethod]
public void TestMoveToVerify()
{
string http;
string? minusOne;
string? minusTwo;
string[] segments;
string[] beforeSegments;
string[] matchDirectories;
string? personKeyFormattedNew;
string? personDisplayDirectory;
string personDisplayDirectoryNew;
string personDisplayDirectoryNameNew;
string personKeyFormattedDirectoryNew;
string sourceDirectory = @"";
string sourceFile = @"\People - C.tsv";
if (!File.Exists(sourceFile))
throw new Exception();
string[] lines = File.ReadAllLines(sourceFile);
for (int i = 0; i < lines.Length; i++)
{
if (!lines[i].Contains("https://"))
continue;
segments = lines[i].Split('\t');
beforeSegments = lines[i - 1].Split('\t');
if (beforeSegments.Length < 4 || segments.Length < 5)
continue;
matchDirectories = Directory.GetDirectories(sourceDirectory, beforeSegments[0], SearchOption.AllDirectories);
if (matchDirectories.Length != 1)
continue;
personDisplayDirectory = Path.GetDirectoryName(matchDirectories[0]);
if (personDisplayDirectory is null)
continue;
minusOne = Path.GetDirectoryName(personDisplayDirectory);
if (minusOne is null)
continue;
minusTwo = Path.GetDirectoryName(minusOne);
if (minusTwo is null)
continue;
(personKeyFormattedNew, personDisplayDirectoryNameNew, http) = Get(segments);
if (personKeyFormattedNew is null)
continue;
personDisplayDirectoryNew = Path.Combine(minusTwo, "Verify", personDisplayDirectoryNameNew);
if (Directory.Exists(personDisplayDirectoryNew))
continue;
personKeyFormattedDirectoryNew = Path.Combine(personDisplayDirectoryNew, personKeyFormattedNew);
Directory.Move(personDisplayDirectory, personDisplayDirectoryNew);
_ = Directory.CreateDirectory(personKeyFormattedDirectoryNew);
File.WriteAllText(Path.Combine(personKeyFormattedDirectoryNew, "Facebook.txt"), http);
}
Assert.IsTrue(lines.Any());
}
}