Skip to content

Commit

Permalink
add tests for running-average
Browse files Browse the repository at this point in the history
  • Loading branch information
maloel committed Dec 14, 2022
1 parent be8b141 commit 01fea25
Show file tree
Hide file tree
Showing 2 changed files with 117 additions and 0 deletions.
45 changes: 45 additions & 0 deletions unit-tests/rsutils/number/running-average/test-double.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# License: Apache 2.0. See LICENSE file in root directory.
# Copyright(c) 2022 Intel Corporation. All Rights Reserved.

from rspy import log, test
from pyrsutils import running_average
import random

random.seed()


def test_around( median, plus_minus, reps = 50, sets = 10 ):
test.start( f"double, {median} +/- {plus_minus}" )

for s in range(sets):
avg = running_average()
tot = 0.
log.d( f" # value | average | expected" )
for r in range(reps):
d = random.random() # [0,1)
d -= 0.5 # [-0.5,0.5)
d *= 2 # [-1,1)
d *= plus_minus
d += median
avg.add( d )
tot += d
log.d( f"{avg.size():>3} {d:>12.2f} | {avg.get():>12.2f} | {tot / avg.size():>12.2f}" )
test.check_equal( avg.size(), reps )
golden = tot / avg.size()
test.check_approx_abs( avg.get(), golden, .00001 )
print()

test.finish()


#############################################################################################
#
test_around( 5000, 100 )
test_around( 100, 99 )
test_around( 0, 100 )
test_around( -100, 150 )
test_around( -10, 5 )
test_around( 100000000, 50000000 )
#
#############################################################################################
test.print_results_and_exit()
72 changes: 72 additions & 0 deletions unit-tests/rsutils/number/running-average/test-int.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
# License: Apache 2.0. See LICENSE file in root directory.
# Copyright(c) 2022 Intel Corporation. All Rights Reserved.

from rspy import log, test
from pyrsutils import running_average_i as running_average
import random

random.seed()


def signed( n ):
s = str(n)
if n >= 0:
s = '+' + s
return s


def test_set( median, plus_minus, reps = 50 ):
avg = running_average()
tot = 0
log.d( f" # value | average | expected" )
for r in range(reps):
d = random.random() # [0,1)
d -= 0.5 # [-0.5,0.5)
d *= 2 # [-1,1)
d *= plus_minus
d += median
d = int(d)
prev = avg.get()
lo = avg.leftover()
avg.add( d )
tot += d
rounding = int(avg.size() / 2)
if prev + lo < 0:
rounding = -rounding
log.d( f"{avg.size():>3} {d:>12} | {str(prev)+'+('+signed(d-prev)+signed(lo)+signed(rounding)+')/'+str(avg.size()):>35}= {str(avg.get())+signed(avg.leftover()):>15}= {avg.get_double():>15.2f} | {tot / avg.size():>12.2f}" )
test.check_equal( avg.size(), reps )
golden = tot / avg.size()
test.check_approx_abs( avg.get(), golden, 1. )
# We have higher expectations! We should get a pretty nice match:
test.check_approx_abs( float( avg ), golden, .001 )
print()


def test_around( median, plus_minus, reps = 50, sets = 10 ):
test.start( f"double, {median} +/- {plus_minus}" )

for s in range(sets):
# in case we want to reproduce, you can take the output, a tuple:
# random.setstate( (3, (...), None) )
# test_set(
log.d( f'random.setstate( {random.getstate()} ) && test_set( {median}, {plus_minus}, {reps} )' )
test_set( median, plus_minus, reps )

test.finish()


#############################################################################################
#
# These use random numbers, so the output will be different each time...
# See the above debug output if something needs to be reproduced.
#
test_around( 5000, 100 )
test_around( 100, 99 )
test_around( 0, 100 )
test_around( -100, 150 )
test_around( -10, 5 )
test_around( 100000000, 50000000 )
test_around( 0, 50000000 )
#
#############################################################################################
test.print_results_and_exit()

0 comments on commit 01fea25

Please sign in to comment.