Skip to content

Commit c81852a

Browse files
authored
Merge pull request #29 from LeeCampbell/TestRefactor
Test refactor
2 parents 81e0fdb + f8b5174 commit c81852a

21 files changed

Lines changed: 325 additions & 94 deletions

src/HdrHistogram.Examples/RecorderExample.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,10 @@ namespace HdrHistogram.Examples
1414
/// </summary>
1515
static class RecorderExample
1616
{
17-
private static readonly Recorder Recorder = new Recorder(1, TimeSpan.TicksPerHour, 3, (id, low, high, sf) => new LongHistogram(id, low, high, sf));
17+
private static readonly Recorder Recorder = new Recorder(1, TimeStamp.Hours(1), 3, (id, low, high, sf) => new LongHistogram(id, low, high, sf));
1818
private static readonly Lazy<AddressFamily> AddressFamily = new Lazy<AddressFamily>(() => GetAddressFamily("google.com"));
1919
private static readonly TimeSpan RunPeriod = TimeSpan.FromSeconds(10);
20-
private static readonly LongHistogram AccumulatingHistogram = new LongHistogram(TimeSpan.TicksPerHour, 3);
20+
private static readonly LongHistogram AccumulatingHistogram = new LongHistogram(TimeStamp.Hours(1), 3);
2121
private const string LogPath = "DatagramSocket.histogram.log";
2222
private static HistogramLogWriter _logWriter;
2323
private static FileStream _outputStream;

src/HdrHistogram.Examples/SimpleHistogramExample.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ private static void RecordMeasurements()
4949
}
5050

5151
/// <summary>
52-
/// Write to the console the Memory footprint of the histogram instance and
52+
/// Write to the console the memory footprint of the histogram instance and
5353
/// the percentile distribution of all the recorded values.
5454
/// </summary>
5555
private static void OutputMeasurements()

src/HdrHistogram.UnitTests/HdrHistogram.UnitTests.csproj

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,12 @@
6464
<Compile Include="Persistence\ShortHistogramLogReaderWriterTests.cs" />
6565
<Compile Include="Persistence\TestCaseGenerator.cs" />
6666
<Compile Include="Properties\AssemblyInfo.cs" />
67-
<Compile Include="RecorderTests.cs" />
67+
<Compile Include="Recording\RecorderTestsBase.cs" />
68+
<Compile Include="Recording\RecorderTestWithIntHistogram.cs" />
69+
<Compile Include="Recording\RecorderTestWithLongHistogram.cs" />
70+
<Compile Include="Recording\RecorderTestWithLShortHistogram.cs" />
6871
<Compile Include="ShortHistogramTests.cs" />
72+
<Compile Include="TimeStampTests.cs" />
6973
</ItemGroup>
7074
<ItemGroup>
7175
<Service Include="{82A7F48D-3B50-4B1E-B82E-3ADA8210C358}" />

src/HdrHistogram.UnitTests/HistogramAssert.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,15 @@ public static void AreEqual(HistogramBase expected, HistogramBase actual)
1313

1414
public static void AreValueEqual(HistogramBase expected, HistogramBase actual)
1515
{
16-
Assert.AreEqual(expected.TotalCount, actual.TotalCount);
17-
Assert.AreEqual(expected.StartTimeStamp, actual.StartTimeStamp);
18-
Assert.AreEqual(expected.EndTimeStamp, actual.EndTimeStamp);
19-
Assert.AreEqual(expected.LowestTrackableValue, actual.LowestTrackableValue);
20-
Assert.AreEqual(expected.HighestTrackableValue, actual.HighestTrackableValue);
21-
Assert.AreEqual(expected.NumberOfSignificantValueDigits, actual.NumberOfSignificantValueDigits);
16+
Assert.AreEqual(expected.TotalCount, actual.TotalCount, "TotalCount property is not equal.");
17+
Assert.AreEqual(expected.StartTimeStamp, actual.StartTimeStamp, "StartTimeStamp property is not equal.");
18+
Assert.AreEqual(expected.EndTimeStamp, actual.EndTimeStamp, "EndTimeStamp property is not equal.");
19+
Assert.AreEqual(expected.LowestTrackableValue, actual.LowestTrackableValue, "LowestTrackableValue property is not equal.");
20+
Assert.AreEqual(expected.HighestTrackableValue, actual.HighestTrackableValue, "HighestTrackableValue property is not equal.");
21+
Assert.AreEqual(expected.NumberOfSignificantValueDigits, actual.NumberOfSignificantValueDigits, "NumberOfSignificantValueDigits property is not equal.");
2222
var expectedValues = expected.AllValues().ToArray();
2323
var actualValues = actual.AllValues().ToArray();
24-
CollectionAssert.AreEqual(expectedValues, actualValues, HistogramIterationValueComparer.Instance);
24+
CollectionAssert.AreEqual(expectedValues, actualValues, HistogramIterationValueComparer.Instance, "Recorded values differ");
2525
}
2626
}
2727
}

src/HdrHistogram.UnitTests/HistogramTestBase.cs

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
using System;
2+
using System.Collections.Generic;
23
using System.IO;
4+
using System.Linq;
35
using System.Threading;
46
using NUnit.Framework;
57

@@ -11,6 +13,14 @@ public abstract class HistogramTestBase
1113
private const int NumberOfSignificantValueDigits = 3;
1214
private const long TestValueLevel = 4;
1315

16+
private static readonly IDictionary<int, Func<long, long, int, HistogramBase>> WordSizeToFactory =
17+
new Dictionary<int, Func<long, long, int, HistogramBase>>()
18+
{
19+
{ 2, (low, high, sf) => new ShortHistogram(low, high, sf) },
20+
{ 4, (low,high,sf) => new IntHistogram(low, high, sf) },
21+
{ 8, (low,high,sf) => new LongHistogram(low, high, sf) }
22+
};
23+
1424
[TestCase(0, 1, NumberOfSignificantValueDigits, "lowestTrackableValue", "lowestTrackableValue must be >= 1")]
1525
[TestCase(1, 1, NumberOfSignificantValueDigits, "highestTrackableValue", "highestTrackableValue must be >= 2 * lowestTrackableValue")]
1626
[TestCase(1, HighestTrackableValue, 6, "numberOfSignificantValueDigits", "numberOfSignificantValueDigits must be between 0 and 5")]
@@ -92,6 +102,37 @@ public void RecordValue_Overflow_ShouldThrowException()
92102
Assert.Throws<IndexOutOfRangeException>(() => longHistogram.RecordValue(HighestTrackableValue * 3));
93103
}
94104

105+
[TestCase(5)]
106+
[TestCase(100)]
107+
public void RecordValueWithCount_increments_TotalCount(long multiplier)
108+
{
109+
var histogram = Create(HighestTrackableValue, NumberOfSignificantValueDigits);
110+
for (int i = 1; i < 5; i++)
111+
{
112+
histogram.RecordValueWithCount(i, multiplier);
113+
Assert.AreEqual(i * multiplier, histogram.TotalCount);
114+
}
115+
}
116+
117+
[TestCase(5)]
118+
[TestCase(100)]
119+
public void RecordValueWithCount_increments_CountAtValue(long multiplier)
120+
{
121+
var histogram = Create(HighestTrackableValue, NumberOfSignificantValueDigits);
122+
for (int i = 1; i < 5; i++)
123+
{
124+
histogram.RecordValueWithCount(TestValueLevel, multiplier);
125+
Assert.AreEqual(i * multiplier, histogram.GetCountAtValue(TestValueLevel));
126+
}
127+
}
128+
129+
[Test]
130+
public void RecordValueWithCount_Overflow_ShouldThrowException()
131+
{
132+
var histogram = Create(HighestTrackableValue, NumberOfSignificantValueDigits);
133+
Assert.Throws<IndexOutOfRangeException>(() => histogram.RecordValueWithCount(HighestTrackableValue * 3, 10));
134+
}
135+
95136

96137
[Test]
97138
public void RecordValueWithExpectedInterval()
@@ -269,6 +310,34 @@ public void When_more_items_are_recorded_than_totalCount_can_hold_Then_set_HasOv
269310
Assert.True(histogram.HasOverflowed());
270311
}
271312

313+
[Test]
314+
public void Can_add_Histograms_with_larger_wordSize_when_values_are_in_range()
315+
{
316+
var largerHistogramFactory = WordSizeToFactory.Where(kvp => kvp.Key >= WordSize).Select(kvp => kvp.Value);
317+
foreach (var sourceFactory in largerHistogramFactory)
318+
{
319+
CreateAndAdd(sourceFactory(1, HighestTrackableValue, NumberOfSignificantValueDigits));
320+
}
321+
}
322+
323+
[Test]
324+
public void Copy_retains_all_public_properties()
325+
{
326+
var source = Create(1, HighestTrackableValue, NumberOfSignificantValueDigits);
327+
var copy = source.Copy();
328+
HistogramAssert.AreValueEqual(source, copy);
329+
}
330+
331+
private void CreateAndAdd(HistogramBase source)
332+
{
333+
source.RecordValueWithCount(1, 100);
334+
source.RecordValueWithCount(int.MaxValue - 1, 1000);
335+
336+
var target = Create(source.LowestTrackableValue, source.HighestTrackableValue, source.NumberOfSignificantValueDigits);
337+
target.Add(source);
338+
339+
HistogramAssert.AreValueEqual(source, target);
340+
}
272341

273342
private static int GetBucketsNeededToCoverValue(int subBucketSize, long value)
274343
{

src/HdrHistogram.UnitTests/IntHistogramTests.cs

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -16,18 +16,5 @@ protected override HistogramBase Create(long lowestTrackableValue, long highestT
1616
{
1717
return new IntHistogram(lowestTrackableValue, highestTrackableValue, numberOfSignificantValueDigits);
1818
}
19-
20-
[Test]
21-
public void Can_add_LongHistogram_with_values_in_range()
22-
{
23-
var longHistogram = new LongHistogram(int.MaxValue - 1, 3);
24-
longHistogram.RecordValueWithCount(1, 100);
25-
longHistogram.RecordValueWithCount(int.MaxValue - 1, 1000);
26-
27-
var shortHistogram = new ShortHistogram(int.MaxValue - 1, 3);
28-
shortHistogram.Add(longHistogram);
29-
30-
HistogramAssert.AreValueEqual(longHistogram, shortHistogram);
31-
}
3219
}
3320
}

src/HdrHistogram.UnitTests/Persistence/HistogramLogReaderWriterTestBase.cs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,32 @@ public void CanRoundTripSingleHistogramsWithSparseValues()
7878
HistogramAssert.AreValueEqual(histogram, actualHistograms.Single());
7979
}
8080

81+
[Test]
82+
public void CanAppendHistogram()
83+
{
84+
var histogram1 = Create(highestTrackableValue: long.MaxValue - 1, numberOfSignificantValueDigits: 3);
85+
histogram1.RecordValue(1);
86+
histogram1.RecordValue((long.MaxValue / 2) + 1);
87+
histogram1.SetTimes();
88+
var histogram2 = Create(highestTrackableValue: long.MaxValue - 1, numberOfSignificantValueDigits: 3);
89+
histogram2.RecordValue(2);
90+
histogram2.SetTimes();
91+
92+
byte[] data;
93+
using (var writerStream = new MemoryStream())
94+
using(var log = new HistogramLogWriter(writerStream))
95+
{
96+
log.Append(histogram1);
97+
log.Append(histogram2);
98+
data = writerStream.ToArray();
99+
}
100+
var actualHistograms = data.ReadHistograms();
101+
102+
Assert.AreEqual(2, actualHistograms.Length);
103+
HistogramAssert.AreValueEqual(histogram1, actualHistograms.First());
104+
HistogramAssert.AreValueEqual(histogram2, actualHistograms.Skip(1).First());
105+
}
106+
81107
[TestCase("jHiccup-2.0.7S.logV2.hlog")]
82108
public void CanReadv2Logs(string logPath)
83109
{
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
using NUnit.Framework;
2+
3+
namespace HdrHistogram.UnitTests.Recording
4+
{
5+
[TestFixture]
6+
public sealed class RecorderTestWithIntHistogram : RecorderTestsBase
7+
{
8+
protected override HistogramBase Create(long id, long min, long max, int sf)
9+
{
10+
return new IntHistogram(id, min, max, sf);
11+
}
12+
}
13+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
using NUnit.Framework;
2+
3+
namespace HdrHistogram.UnitTests.Recording
4+
{
5+
[TestFixture]
6+
public sealed class RecorderTestWithLShortHistogram : RecorderTestsBase
7+
{
8+
protected override HistogramBase Create(long id, long min, long max, int sf)
9+
{
10+
return new ShortHistogram(id, min, max, sf);
11+
}
12+
}
13+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
using NUnit.Framework;
2+
3+
namespace HdrHistogram.UnitTests.Recording
4+
{
5+
[TestFixture]
6+
public sealed class RecorderTestWithLongHistogram : RecorderTestsBase
7+
{
8+
protected override HistogramBase Create(long id, long min, long max, int sf)
9+
{
10+
return new LongHistogram(id, min, max, sf);
11+
}
12+
}
13+
}

0 commit comments

Comments
 (0)