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 ;
122using HdrHistogram . Utilities ;
133using NUnit . Framework ;
144
155namespace 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