diff --git a/.gitignore b/.gitignore index 7acc6cc..cbefbbf 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,5 @@ -Elwig/obj/ -Elwig/bin/ +obj/ +bin/ *.user .vs .idea diff --git a/Elwig.sln b/Elwig.sln index 9cdfaf0..d308563 100644 --- a/Elwig.sln +++ b/Elwig.sln @@ -1,10 +1,11 @@ - Microsoft Visual Studio Solution File, Format Version 12.00 # Visual Studio Version 17 VisualStudioVersion = 17.3.32929.385 MinimumVisualStudioVersion = 10.0.40219.1 Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Elwig", "Elwig\Elwig.csproj", "{00868460-16F6-4B48-AA9B-998F6263693B}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Tests", "Tests\Tests.csproj", "{AF4B84F8-08E6-409C-9E94-D7D990469597}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -15,6 +16,10 @@ Global {00868460-16F6-4B48-AA9B-998F6263693B}.Debug|Any CPU.Build.0 = Debug|Any CPU {00868460-16F6-4B48-AA9B-998F6263693B}.Release|Any CPU.ActiveCfg = Release|Any CPU {00868460-16F6-4B48-AA9B-998F6263693B}.Release|Any CPU.Build.0 = Release|Any CPU + {AF4B84F8-08E6-409C-9E94-D7D990469597}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {AF4B84F8-08E6-409C-9E94-D7D990469597}.Debug|Any CPU.Build.0 = Debug|Any CPU + {AF4B84F8-08E6-409C-9E94-D7D990469597}.Release|Any CPU.ActiveCfg = Release|Any CPU + {AF4B84F8-08E6-409C-9E94-D7D990469597}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/Elwig/Helpers/Utils.cs b/Elwig/Helpers/Utils.cs index e4a350d..6ff4b59 100644 --- a/Elwig/Helpers/Utils.cs +++ b/Elwig/Helpers/Utils.cs @@ -108,17 +108,17 @@ namespace Elwig.Helpers { } public static double OeToKmw(double oe) { - return Math.Round((-4.54 + Math.Sqrt(4.54 * 4.54 - 4 * 0.022 * -oe)) / 2 * 0.022, 1); + return Math.Round((-4.54 + Math.Sqrt(4.54 * 4.54 - 4 * 0.022 * -oe)) / (2 * 0.022), 1); } public static decimal DecFromDb(long value, byte precision) { bool neg = value < 0; - if (neg) value = -value; - return new decimal((int)(value & 0xFFFFFFFF), (int)((value >> 32) & 0x7FFFFFFF), 0, neg, precision); + if (neg) value = -value; + return new((int)(value & 0xFFFFFFFF), (int)((value >> 32) & 0x7FFFFFFF), 0, neg, precision); } public static long DecToDb(decimal value, byte precision) { - return (long)decimal.Round(value * precision, 0); + return (long)decimal.Round(value * (decimal)Math.Pow(10, precision), 0); } } } diff --git a/Tests/HelpersUtilsTest.cs b/Tests/HelpersUtilsTest.cs new file mode 100644 index 0000000..a775177 --- /dev/null +++ b/Tests/HelpersUtilsTest.cs @@ -0,0 +1,46 @@ +using Elwig.Helpers; + +namespace Tests { + [TestClass] + public class HelpersUtilsTest { + + private static readonly double[,] Gradation = new double[,] { + { 14.0, 68.0 }, + { 15.0, 73.0 }, + { 17.1, 84.0 }, + { 19.0, 94.0 }, + { 21.0, 105.0 }, + { 25.0, 127.0 }, + { 27.1, 139.0 }, + { 30.0, 156.0 }, + }; + + [TestMethod] + public void Test_KmwToOe() { + for (int i = 0; i < Gradation.GetLength(0); i++) { + Assert.AreEqual(Gradation[i, 1], Utils.KmwToOe(Gradation[i, 0])); + } + } + + [TestMethod] + public void Test_OeToKmw() { + for (int i = 0; i < Gradation.GetLength(0); i++) { + Assert.AreEqual(Gradation[i, 0], Utils.OeToKmw(Gradation[i, 1])); + } + } + + [TestMethod] + public void Test_DecFromDb() { + Assert.AreEqual(10.67M, Utils.DecFromDb(10670, 3)); + Assert.AreEqual(-100.9999M, Utils.DecFromDb(-1009999, 4)); + Assert.AreEqual(0.01M, Utils.DecFromDb(1, 2)); + } + + [TestMethod] + public void Test_DecToDb() { + Assert.AreEqual(21948, Utils.DecToDb(219.48M, 2)); + Assert.AreEqual(-12345, Utils.DecToDb(-1.2345M, 4)); + Assert.AreEqual(99190, Utils.DecToDb(99190, 0)); + } + } +} \ No newline at end of file diff --git a/Tests/Tests.csproj b/Tests/Tests.csproj new file mode 100644 index 0000000..d92d858 --- /dev/null +++ b/Tests/Tests.csproj @@ -0,0 +1,23 @@ + + + + net7.0-windows + enable + enable + + false + true + + + + + + + + + + + + + + diff --git a/Tests/Usings.cs b/Tests/Usings.cs new file mode 100644 index 0000000..ab67c7e --- /dev/null +++ b/Tests/Usings.cs @@ -0,0 +1 @@ +global using Microsoft.VisualStudio.TestTools.UnitTesting; \ No newline at end of file