-
Notifications
You must be signed in to change notification settings - Fork 4.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
117 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |