Ferdig intro til unit testing
This commit is contained in:
parent
3cbfff200e
commit
8ff1f69ea3
|
@ -5,7 +5,30 @@ public class StatsTest
|
||||||
[Test]
|
[Test]
|
||||||
public void TestWith3And4()
|
public void TestWith3And4()
|
||||||
{
|
{
|
||||||
|
// arrange
|
||||||
var stats = new Stats();
|
var stats = new Stats();
|
||||||
Assert.Pass();
|
|
||||||
|
//act
|
||||||
|
stats.Add(3);
|
||||||
|
stats.Add(4);
|
||||||
|
|
||||||
|
// assert
|
||||||
|
Assert.AreEqual(2, stats.Count);
|
||||||
|
Assert.AreEqual(7, stats.Sum);
|
||||||
|
Assert.AreEqual(4, stats.Max);
|
||||||
|
Assert.AreEqual(3, stats.Min);
|
||||||
|
Assert.AreEqual(3.5, stats.Mean, 0.0001);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void TestEmptyStats()
|
||||||
|
{
|
||||||
|
var stats = new Stats();
|
||||||
|
|
||||||
|
Assert.AreEqual(0, stats.Count);
|
||||||
|
Assert.AreEqual(0, stats.Sum);
|
||||||
|
Assert.IsNull(stats.Max);
|
||||||
|
Assert.IsNull(stats.Min);
|
||||||
|
Assert.AreEqual(float.NaN, stats.Mean);
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -6,14 +6,14 @@ public class Stats
|
||||||
{
|
{
|
||||||
public int Count {get; private set;}
|
public int Count {get; private set;}
|
||||||
public int Sum {get; private set;}
|
public int Sum {get; private set;}
|
||||||
public int Max {get; private set;}
|
public int? Max { get; private set; }
|
||||||
public int Min {get; private set;}
|
public int? Min {get; private set;}
|
||||||
public float Mean => (float)Sum / Count;
|
public float Mean => (float)Sum / Count;
|
||||||
|
|
||||||
public void Add(int number)
|
public void Add(int number)
|
||||||
{
|
{
|
||||||
if (Max == -1 || number > Max) Max = number;
|
if (Max == null || number > Max) Max = number;
|
||||||
if (Min == -1 || number < Min) Min = number;
|
if (Min == null || number < Min) Min = number;
|
||||||
Count++;
|
Count++;
|
||||||
Sum += number;
|
Sum += number;
|
||||||
}
|
}
|
||||||
|
@ -23,20 +23,22 @@ public class Stats
|
||||||
return
|
return
|
||||||
Format("Antall tall", Count) +
|
Format("Antall tall", Count) +
|
||||||
Format("Sum", Sum) +
|
Format("Sum", Sum) +
|
||||||
Format("Max", Max) +
|
Format("Max", Max.Value) +
|
||||||
Format("Min", Min) +
|
Format("Min", Min.Value) +
|
||||||
Format("Gjennomsnitt", Mean);
|
Format("Gjennomsnitt", Mean);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private static string Format(string label, float number)
|
private static string Format(string label, float? number)
|
||||||
{
|
{
|
||||||
return FormatImpl(label, number.ToString("####.##"));
|
if (number == null) return String.Empty;
|
||||||
|
return FormatImpl(label, number.Value.ToString("####.##"));
|
||||||
}
|
}
|
||||||
|
|
||||||
private static string Format(string label, int number)
|
private static string Format(string label, int? number)
|
||||||
{
|
{
|
||||||
return FormatImpl(label, number.ToString("####"));
|
if (number == null) return String.Empty;
|
||||||
|
return FormatImpl(label, number.Value.ToString("####"));
|
||||||
}
|
}
|
||||||
|
|
||||||
private static string FormatImpl(string label, string number)
|
private static string FormatImpl(string label, string number)
|
||||||
|
|
Loading…
Reference in a new issue