@@ -396,6 +396,44 @@ func testWithinToleranceZeroTolerance(t *rapid.T) {
396396 require .True (t , result )
397397}
398398
399+ // testAddToleranceProp is a property-based test which tests that the
400+ // AddTolerance helper correctly applies the provided tolerance margin to any
401+ // given value.
402+ func testAddToleranceProp (t * rapid.T ) {
403+ value := NewBigIntFromUint64 (rapid .Uint64Min (1 ).Draw (t , "value" ))
404+ tolerancePpm := NewBigIntFromUint64 (
405+ rapid .Uint64Range (0 , 1_000_000 ).Draw (t , "tolerance_ppm" ),
406+ )
407+
408+ result := AddTolerance (value , tolerancePpm )
409+
410+ if tolerancePpm .ToUint64 () == 0 {
411+ require .True (t , result .Equals (value ))
412+ return
413+ }
414+
415+ // First off, let's just check that the result is at all greater than
416+ // the input.
417+ require .True (t , result .Gte (value ))
418+
419+ // Let's now convert the values to a fixed point type in order to use
420+ // the WithinTolerance method.
421+ valueFixed := BigIntFixedPoint {
422+ Coefficient : value ,
423+ Scale : 0 ,
424+ }
425+ resultFixed := BigIntFixedPoint {
426+ Coefficient : result ,
427+ Scale : 0 ,
428+ }
429+
430+ // The value with the applied tolerance and the original value should be
431+ // within tolerance.
432+ res , err := resultFixed .WithinTolerance (valueFixed , tolerancePpm )
433+ require .NoError (t , err )
434+ require .True (t , res )
435+ }
436+
399437// testWithinToleranceSymmetric is a property-based test which ensures that the
400438// WithinTolerance method is symmetric (swapping the order of the fixed-point
401439// values does not change the result).
@@ -600,6 +638,11 @@ func testWithinTolerance(t *testing.T) {
600638 "within_tolerance_float_reproduce" ,
601639 rapid .MakeCheck (testWithinToleranceFloatReproduce ),
602640 )
641+
642+ t .Run (
643+ "add_tolerance_property" ,
644+ rapid .MakeCheck (testAddToleranceProp ),
645+ )
603646}
604647
605648// TestFixedPoint runs a series of property-based tests on the FixedPoint type
@@ -619,5 +662,5 @@ func TestFixedPoint(t *testing.T) {
619662
620663 t .Run ("from_uint64" , rapid .MakeCheck (testFromUint64 [BigInt ]))
621664
622- t .Run ("within_tolerance " , testWithinTolerance )
665+ t .Run ("tolerance " , testWithinTolerance )
623666}
0 commit comments