Skip to content

Commit 340320b

Browse files
committed
Favouring variables instead of magic numbers in tests
1 parent 899c9f7 commit 340320b

11 files changed

Lines changed: 176 additions & 131 deletions

src/HdrHistogram.UnitTests/ConcurrentHistogramTestBase.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ public abstract class ConcurrentHistogramTestBase : HistogramTestBase
1010
[Test]
1111
public void Can_support_multiple_concurrent_recorders()
1212
{
13-
var target = Create(1, long.MaxValue - 1, 3);
13+
var target = Create(DefautltLowestDiscernibleValue, DefaultHighestTrackableValue, DefaultSignificantFigures);
1414
const int loopcount = 10 * 1000 * 1000;
1515
var concurrency = Environment.ProcessorCount;
1616
var expected = loopcount * concurrency;

src/HdrHistogram.UnitTests/HdrHistogram.UnitTests.csproj

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,10 +50,12 @@
5050
</ItemGroup>
5151
<ItemGroup>
5252
<Compile Include="ConcurrentHistogramTestBase.cs" />
53+
<Compile Include="HistogramEncodingTestBase.cs" />
5354
<Compile Include="IntConcurrentHistogramTests.cs" />
55+
<Compile Include="IntHistogramEncodingTests.cs" />
5456
<Compile Include="LongConcurrentHistogramTests.cs" />
5557
<Compile Include="HistogramAssert.cs" />
56-
<Compile Include="HistogramEncodingTest.cs" />
58+
<Compile Include="LongHistogramEncodingTests.cs" />
5759
<Compile Include="HistogramIterationValueComparer.cs" />
5860
<Compile Include="Persistence\LongConcurrentHistogramLogReaderWriterTests.cs" />
5961
<Compile Include="Persistence\HistogramLogExtensions.cs" />
@@ -75,6 +77,7 @@
7577
<Compile Include="Recording\RecorderTestWithLongConcurrentHistogram.cs" />
7678
<Compile Include="Recording\RecorderTestWithLongHistogram.cs" />
7779
<Compile Include="Recording\RecorderTestWithLShortHistogram.cs" />
80+
<Compile Include="ShortHistogramEncodingTests.cs" />
7881
<Compile Include="ShortHistogramTests.cs" />
7982
<Compile Include="TimeStampTests.cs" />
8083
</ItemGroup>

src/HdrHistogram.UnitTests/HgrmPercentileDistrubutionOutputTests.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ public void PercentileDistrubution_hgrm_output_is_in_correct_format(
2727
histogram.OutputPercentileDistribution(writer, percentileTicksPerHalfDistance, scaling);
2828
var actual = writer.ToString();
2929

30-
3130
Assert.AreEqual(expected, actual);
3231
}
3332

@@ -68,7 +67,7 @@ private string GetEmbeddedFileText(string filename)
6867
}
6968
}
7069

71-
private static void LoadHistogram(HistogramBase histogram)
70+
private static void LoadHistogram(IRecorder histogram)
7271
{
7372
for (int i = 0; i < 10000; i += 1000)
7473
{
Lines changed: 24 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,100 +1,86 @@
1-
/*
2-
* Written by Matt Warren, and released to the public domain,
3-
* as explained at
4-
* http://creativecommons.org/publicdomain/zero/1.0/
5-
*
6-
* This is a .NET port of the original Java version, which was written by
7-
* Gil Tene as described in
8-
* https://github.com/HdrHistogram/HdrHistogram
9-
*/
10-
11-
using HdrHistogram.Encoding;
1+
using HdrHistogram.Encoding;
122
using HdrHistogram.Utilities;
133
using NUnit.Framework;
144

155
namespace HdrHistogram.UnitTests
166
{
17-
[TestFixture]
18-
public sealed class LongHistogramEncodingTests
7+
public abstract class HistogramEncodingTestBase
198
{
20-
private static readonly HistogramEncoderV2 EncoderV2 = new Encoding.HistogramEncoderV2();
21-
private const long HighestTrackableValue = 3600L * 1000 * 1000; // e.g. for 1 hr in usec units
9+
protected const long DefautltLowestDiscernibleValue = 1;
10+
protected const long DefaultHighestTrackableValue = 7716549600;//TimeStamp.Hours(1); // e.g. for 1 hr in system clock ticks (StopWatch.Frequency)
11+
protected const int DefaultSignificantFigures = 3;
2212

23-
13+
private static readonly HistogramEncoderV2 EncoderV2 = new Encoding.HistogramEncoderV2();
2414

2515
[Test]
2616
public void Given_a_populated_Histogram_When_encoded_and_decoded_Then_data_is_preserved()
2717
{
28-
var source = Create(HighestTrackableValue, 3);
18+
var source = Create(DefaultHighestTrackableValue, DefaultSignificantFigures);
2919
Load(source);
3020
var result = EncodeDecode(source);
31-
HistogramAssert.AreEqual(source, result);
21+
HistogramAssert.AreValueEqual(source, result);
3222
}
3323

3424
[Test]
3525
public void Given_a_populated_Histogram_When_encoded_and_decoded_with_compression_Then_data_is_preserved()
3626
{
37-
var source = Create(HighestTrackableValue, 3);
27+
var source = Create(DefaultHighestTrackableValue, DefaultSignificantFigures);
3828
Load(source);
3929
var result = CompressedEncodeDecode(source);
40-
HistogramAssert.AreEqual(source, result);
30+
HistogramAssert.AreValueEqual(source, result);
4131
}
4232

4333
[Test]
4434
public void Given_a_Histogram_populated_with_full_range_of_values_When_encoded_and_decoded_Then_data_is_preserved()
4535
{
46-
var source = Create(HighestTrackableValue, 3);
36+
var source = Create(DefaultHighestTrackableValue, DefaultSignificantFigures);
4737
LoadFullRange(source);
4838
var result = EncodeDecode(source);
49-
HistogramAssert.AreEqual(source, result);
39+
HistogramAssert.AreValueEqual(source, result);
5040
}
5141

5242
[Test]
5343
public void Given_a_Histogram_populated_with_full_range_of_values_When_encoded_and_decoded_with_compression_Then_data_is_preserved()
5444
{
55-
var source = Create(HighestTrackableValue, 3);
45+
var source = Create(DefaultHighestTrackableValue, DefaultSignificantFigures);
5646
LoadFullRange(source);
5747
var result = CompressedEncodeDecode(source);
58-
HistogramAssert.AreEqual(source, result);
48+
HistogramAssert.AreValueEqual(source, result);
5949
}
6050

61-
private LongHistogram Create(long highestTrackableValue, int numberOfSignificantDigits)
62-
{
63-
return new LongHistogram(highestTrackableValue, numberOfSignificantDigits);
64-
}
51+
protected abstract HistogramBase Create(long highestTrackableValue, int numberOfSignificantDigits);
6552

66-
private static LongHistogram EncodeDecode(LongHistogram source)
53+
private static HistogramBase EncodeDecode(HistogramBase source)
6754
{
6855
var targetBuffer = ByteBuffer.Allocate(source.GetNeededByteBufferCapacity());
6956
source.Encode(targetBuffer, EncoderV2);
7057
targetBuffer.Position = 0;
71-
return (LongHistogram)HistogramEncoding.DecodeFromByteBuffer(targetBuffer, 0);
58+
return HistogramEncoding.DecodeFromByteBuffer(targetBuffer, 0);
7259
}
7360

74-
private static LongHistogram CompressedEncodeDecode(LongHistogram source)
61+
private static HistogramBase CompressedEncodeDecode(HistogramBase source)
7562
{
7663
var targetBuffer = ByteBuffer.Allocate(source.GetNeededByteBufferCapacity());
7764
source.EncodeIntoCompressedByteBuffer(targetBuffer);
7865
targetBuffer.Position = 0;
79-
return (LongHistogram)HistogramEncoding.DecodeFromCompressedByteBuffer(targetBuffer, 0);
66+
return HistogramEncoding.DecodeFromCompressedByteBuffer(targetBuffer, 0);
8067
}
8168

82-
private static void Load(LongHistogram source)
69+
private static void Load(IRecorder source)
8370
{
8471
for (long i = 0L; i < 10000L; i++)
8572
{
8673
source.RecordValue(1000L * i);
8774
}
8875
}
8976

90-
private static void LoadFullRange(LongHistogram source)
77+
protected virtual void LoadFullRange(IRecorder source)
9178
{
92-
for (long i = 0L; i < HighestTrackableValue; i += 100L)
79+
for (long i = 0L; i < DefaultHighestTrackableValue; i += 100L)
9380
{
9481
source.RecordValue(i);
9582
}
96-
source.RecordValue(HighestTrackableValue);
83+
source.RecordValue(DefaultHighestTrackableValue);
9784
}
98-
9985
}
100-
}
86+
}

0 commit comments

Comments
 (0)