diff --git a/Emne 3/UnitTesting/UnitTesting.Test/UnitTest1.cs b/Emne 3/UnitTesting/UnitTesting.Test/UnitTest1.cs new file mode 100644 index 0000000..c099fe3 --- /dev/null +++ b/Emne 3/UnitTesting/UnitTesting.Test/UnitTest1.cs @@ -0,0 +1,11 @@ +namespace UnitTesting.Test; + +public class StatsTest +{ + [Test] + public void TestWith3And4() + { + var stats = new Stats(); + Assert.Pass(); + } +} \ No newline at end of file diff --git a/Emne 3/UnitTesting/UnitTesting.Test/UnitTesting.Test.csproj b/Emne 3/UnitTesting/UnitTesting.Test/UnitTesting.Test.csproj new file mode 100644 index 0000000..74a178c --- /dev/null +++ b/Emne 3/UnitTesting/UnitTesting.Test/UnitTesting.Test.csproj @@ -0,0 +1,28 @@ + + + + net8.0 + enable + enable + + false + true + + + + + + + + + + + + + + + + + + + diff --git a/Emne 3/UnitTesting/UnitTesting.sln b/Emne 3/UnitTesting/UnitTesting.sln new file mode 100644 index 0000000..0c312cf --- /dev/null +++ b/Emne 3/UnitTesting/UnitTesting.sln @@ -0,0 +1,22 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "UnitTesting", "UnitTesting\UnitTesting.csproj", "{DB18A478-4E45-453D-9AB4-64D1DE10828D}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "UnitTesting.Test", "UnitTesting.Test\UnitTesting.Test.csproj", "{0EE8BB76-7CF5-49F1-808D-5684B28D482A}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {DB18A478-4E45-453D-9AB4-64D1DE10828D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {DB18A478-4E45-453D-9AB4-64D1DE10828D}.Debug|Any CPU.Build.0 = Debug|Any CPU + {DB18A478-4E45-453D-9AB4-64D1DE10828D}.Release|Any CPU.ActiveCfg = Release|Any CPU + {DB18A478-4E45-453D-9AB4-64D1DE10828D}.Release|Any CPU.Build.0 = Release|Any CPU + {0EE8BB76-7CF5-49F1-808D-5684B28D482A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {0EE8BB76-7CF5-49F1-808D-5684B28D482A}.Debug|Any CPU.Build.0 = Debug|Any CPU + {0EE8BB76-7CF5-49F1-808D-5684B28D482A}.Release|Any CPU.ActiveCfg = Release|Any CPU + {0EE8BB76-7CF5-49F1-808D-5684B28D482A}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection +EndGlobal diff --git a/Emne 3/UnitTesting/UnitTesting/Program.cs b/Emne 3/UnitTesting/UnitTesting/Program.cs new file mode 100644 index 0000000..db73603 --- /dev/null +++ b/Emne 3/UnitTesting/UnitTesting/Program.cs @@ -0,0 +1,22 @@ +using System; + +namespace UnitTesting +{ + internal class Program + { + static void Main(string[] args) + { + var stats = new Stats(); + + while (true) + { + Console.WriteLine("Please enter a number:"); + var numberStr = Console.ReadLine(); + var number = Convert.ToInt32(numberStr); + stats.Add(number); + Console.WriteLine(stats.GetDescription()); + } + } + } +} + diff --git a/Emne 3/UnitTesting/UnitTesting/Stats.cs b/Emne 3/UnitTesting/UnitTesting/Stats.cs new file mode 100644 index 0000000..319bef2 --- /dev/null +++ b/Emne 3/UnitTesting/UnitTesting/Stats.cs @@ -0,0 +1,48 @@ +using System.Security; + +namespace UnitTesting; + +public class Stats +{ + public int Count {get; private set;} + public int Sum {get; private set;} + public int Max {get; private set;} + public int Min {get; private set;} + public float Mean => (float)Sum / Count; + + public void Add(int number) + { + if (Max == -1 || number > Max) Max = number; + if (Min == -1 || number < Min) Min = number; + Count++; + Sum += number; + } + + public string GetDescription() + { + return + Format("Antall tall", Count) + + Format("Sum", Sum) + + Format("Max", Max) + + Format("Min", Min) + + Format("Gjennomsnitt", Mean); + + } + + private static string Format(string label, float number) + { + return FormatImpl(label, number.ToString("####.##")); + } + + private static string Format(string label, int number) + { + return FormatImpl(label, number.ToString("####")); + } + + private static string FormatImpl(string label, string number) + { + return label.PadRight(12, ' ') + ": " + number + '\n'; + } + + +} \ No newline at end of file diff --git a/Emne 3/UnitTesting/UnitTesting/UnitTesting.csproj b/Emne 3/UnitTesting/UnitTesting/UnitTesting.csproj new file mode 100644 index 0000000..2f4fc77 --- /dev/null +++ b/Emne 3/UnitTesting/UnitTesting/UnitTesting.csproj @@ -0,0 +1,10 @@ + + + + Exe + net8.0 + enable + enable + + +