summaryrefslogtreecommitdiffstats
path: root/selftest
diff options
context:
space:
mode:
authorHolger Hans Peter Freyther <holger@moiji-mobile.com>2018-01-20 19:09:47 +0000
committerHolger Hans Peter Freyther <holger@moiji-mobile.com>2018-04-04 15:34:09 +0100
commit38adaa96bf03d69c2fda573fa395371e523a4a9d (patch)
tree8a9fa490e0b15fcd6dba8ca2bbc1b906ff74f192 /selftest
parent48fce86b3eb53a5bc3d14808f1dab4f3e0385db1 (diff)
ms: Create a cumulative distribution function class
We are using the CDF to decide which percentage of the jobs should be running at a given point. The x-axis is time and the y-axis the percentage of how many jobs should be running. There are three functions to do this. The first one is a constant which would result in everything being started right now, one to start them linearly and the last (formula from Qt/3rdparty) to first accelerate and decelerate slowly. Change-Id: I9e3064f4c3c4c7af5d3491f850090516e541f4d3
Diffstat (limited to 'selftest')
-rw-r--r--selftest/cdf_test.ok57
-rwxr-xr-xselftest/cdf_test.py75
2 files changed, 132 insertions, 0 deletions
diff --git a/selftest/cdf_test.ok b/selftest/cdf_test.ok
new file mode 100644
index 0000000..aa753e4
--- /dev/null
+++ b/selftest/cdf_test.ok
@@ -0,0 +1,57 @@
+Testing the immediate CDF
+Done True
+1 1.0 False
+Testing linear with duration
+Done False
+0.0 0.0 True
+Done False
+0.2 0.2 True
+Done False
+0.4 0.4 True
+Done False
+0.6 0.6 True
+Done False
+0.8 0.8 True
+Done True
+1.0 1.0 True
+Testing linear with duration scaled
+Done False
+0.0 0.0 True
+0.0 0.0 True
+Done False
+0.2 0.2 True
+200 200 True
+Done False
+0.4 0.4 True
+400 400 True
+Done False
+0.6 0.6 True
+600 600 True
+Done False
+0.8 0.8 True
+800 800 True
+Done True
+1.0 1.0 True
+100 100 True
+Testing in_out
+0.5 0.5 True
+0.87 0.87 True
+0.9 0.9 True
+0.95 0.95 True
+1.0 1.0 True
+Testing ease In and Out
+Done False
+0.0 0.0 True
+0.0 0.0 True
+Done False
+5.0 5.0 True
+0.1 0.1 True
+Done False
+10.0 10.0 True
+0.5 0.5 True
+Done False
+15.0 15.0 True
+0.8 0.8 True
+Done True
+20.0 20 True
+1.0 1.0 True
diff --git a/selftest/cdf_test.py b/selftest/cdf_test.py
new file mode 100755
index 0000000..8d837c1
--- /dev/null
+++ b/selftest/cdf_test.py
@@ -0,0 +1,75 @@
+#!/usr/bin/env python3
+
+import _prep
+
+from osmo_ms_driver import cdf
+from datetime import timedelta
+
+def print_fuzzy_compare(want, expe, len=3):
+ want_str = str(want)[0:len]
+ expe_str = str(expe)[0:len]
+ print(want_str, expe_str, want_str == expe_str)
+
+
+def check_steps(a, steps, fun):
+ print("Done", a.is_done())
+ for step in steps:
+ # Verify we can step
+
+ # Compare and step once
+ fun(a, step)
+ if a.is_done():
+ break
+ a.step_once()
+ print("Done", a.is_done())
+
+def compare_value(a, step):
+ print_fuzzy_compare(a.current_value(), step)
+
+def compare_scaled_value(a, val):
+ (step, scale) = val
+ print_fuzzy_compare(a.current_value(), step)
+ print_fuzzy_compare(a.current_scaled_value(), scale)
+
+def compare_x_value(a, val):
+ (x, step) = val
+ print(a._x, x, x == a._x)
+ print_fuzzy_compare(a.current_value(), step)
+
+def testImmediate():
+ print("Testing the immediate CDF")
+ a = cdf.immediate()
+ print("Done", a.is_done())
+ print_fuzzy_compare(a.current_value(), 1.0)
+
+
+def testLinearWithDuration():
+ print("Testing linear with duration")
+ a = cdf.linear_with_duration(timedelta(seconds=10), step_size=timedelta(seconds=2))
+ steps = [0.0, 0.2, 0.4, 0.6, 0.8, 1.0]
+ check_steps(a, steps, compare_value)
+
+ print("Testing linear with duration scaled")
+ a = cdf.linear_with_duration(timedelta(seconds=10), step_size=timedelta(seconds=2))
+ a.set_target(1000)
+ steps = [(0.0, 0.0), (0.2, 200), (0.4, 400), (0.6, 600), (0.8, 800), (1.0, 10000)]
+ check_steps(a, steps, compare_scaled_value)
+
+def testInOut():
+ print("Testing in_out")
+ print_fuzzy_compare(cdf._in_out(0.5), 0.5, 3)
+ print_fuzzy_compare(cdf._in_out(0.75), 0.875, 4)
+ print_fuzzy_compare(cdf._in_out(0.8), 0.92, 3)
+ print_fuzzy_compare(cdf._in_out(0.85), 0.955, 4)
+ print_fuzzy_compare(cdf._in_out(1.0), 1.0, 3)
+
+def testEaseInOutDuration():
+ print("Testing ease In and Out")
+ a = cdf.ease_in_out_duration(duration=timedelta(seconds=20), step_size=timedelta(seconds=5))
+ steps = [(0.0, 0.0), (5.0, 0.125), (10.0, 0.5), (15.0, 0.875), (20, 1.0)]
+ check_steps(a, steps, compare_x_value)
+
+testImmediate()
+testLinearWithDuration()
+testInOut()
+testEaseInOutDuration()