Holger Hans Peter Freyther | 38adaa9 | 2018-01-20 19:09:47 +0000 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
| 2 | |
| 3 | import _prep |
| 4 | |
| 5 | from osmo_ms_driver import cdf |
| 6 | from datetime import timedelta |
| 7 | |
| 8 | def print_fuzzy_compare(want, expe, len=3): |
| 9 | want_str = str(want)[0:len] |
| 10 | expe_str = str(expe)[0:len] |
| 11 | print(want_str, expe_str, want_str == expe_str) |
| 12 | |
| 13 | |
| 14 | def check_steps(a, steps, fun): |
| 15 | print("Done", a.is_done()) |
| 16 | for step in steps: |
| 17 | # Verify we can step |
| 18 | |
| 19 | # Compare and step once |
| 20 | fun(a, step) |
| 21 | if a.is_done(): |
| 22 | break |
| 23 | a.step_once() |
| 24 | print("Done", a.is_done()) |
| 25 | |
| 26 | def compare_value(a, step): |
| 27 | print_fuzzy_compare(a.current_value(), step) |
| 28 | |
| 29 | def compare_scaled_value(a, val): |
| 30 | (step, scale) = val |
| 31 | print_fuzzy_compare(a.current_value(), step) |
| 32 | print_fuzzy_compare(a.current_scaled_value(), scale) |
| 33 | |
| 34 | def compare_x_value(a, val): |
| 35 | (x, step) = val |
| 36 | print(a._x, x, x == a._x) |
| 37 | print_fuzzy_compare(a.current_value(), step) |
| 38 | |
| 39 | def testImmediate(): |
| 40 | print("Testing the immediate CDF") |
| 41 | a = cdf.immediate() |
| 42 | print("Done", a.is_done()) |
| 43 | print_fuzzy_compare(a.current_value(), 1.0) |
| 44 | |
| 45 | |
| 46 | def testLinearWithDuration(): |
| 47 | print("Testing linear with duration") |
| 48 | a = cdf.linear_with_duration(timedelta(seconds=10), step_size=timedelta(seconds=2)) |
| 49 | steps = [0.0, 0.2, 0.4, 0.6, 0.8, 1.0] |
| 50 | check_steps(a, steps, compare_value) |
| 51 | |
| 52 | print("Testing linear with duration scaled") |
| 53 | a = cdf.linear_with_duration(timedelta(seconds=10), step_size=timedelta(seconds=2)) |
| 54 | a.set_target(1000) |
| 55 | steps = [(0.0, 0.0), (0.2, 200), (0.4, 400), (0.6, 600), (0.8, 800), (1.0, 10000)] |
| 56 | check_steps(a, steps, compare_scaled_value) |
| 57 | |
| 58 | def testInOut(): |
| 59 | print("Testing in_out") |
| 60 | print_fuzzy_compare(cdf._in_out(0.5), 0.5, 3) |
| 61 | print_fuzzy_compare(cdf._in_out(0.75), 0.875, 4) |
| 62 | print_fuzzy_compare(cdf._in_out(0.8), 0.92, 3) |
| 63 | print_fuzzy_compare(cdf._in_out(0.85), 0.955, 4) |
| 64 | print_fuzzy_compare(cdf._in_out(1.0), 1.0, 3) |
| 65 | |
| 66 | def testEaseInOutDuration(): |
| 67 | print("Testing ease In and Out") |
| 68 | a = cdf.ease_in_out_duration(duration=timedelta(seconds=20), step_size=timedelta(seconds=5)) |
| 69 | steps = [(0.0, 0.0), (5.0, 0.125), (10.0, 0.5), (15.0, 0.875), (20, 1.0)] |
| 70 | check_steps(a, steps, compare_x_value) |
| 71 | |
| 72 | testImmediate() |
| 73 | testLinearWithDuration() |
| 74 | testInOut() |
| 75 | testEaseInOutDuration() |