|
1 | 1 | from qubed import Qube |
2 | 2 |
|
3 | 3 |
|
| 4 | +def set_operation_testcase(testcase): |
| 5 | + q1 = Qube.from_tree(testcase["q1"]) |
| 6 | + q2 = Qube.from_tree(testcase["q2"]) |
| 7 | + assert q1 | q2 == Qube.from_tree(testcase["union"]) |
| 8 | + assert q1 & q2 == Qube.from_tree(testcase["intersection"]) |
| 9 | + assert q1 - q2 == Qube.from_tree(testcase["q1 - q2"]) |
| 10 | + |
| 11 | + |
| 12 | +# These are a bunch of testcases where q1 and q2 are specified and then their union/intersection/difference are checked |
| 13 | +# Generate them with code like: |
| 14 | +# q1 = Qube.from_tree("root, frequency=*, levtype=*, param=*, levelist=*, domain=a/b/c/d") |
| 15 | +# q2 = Qube.from_tree("root, frequency=*, levtype=*, param=*, domain=a/b/c/d") |
| 16 | + |
| 17 | +# test = { |
| 18 | +# "q1": str(q1), |
| 19 | +# "q2": str(q2), |
| 20 | +# "union": str(q1 | q2), |
| 21 | +# "intersection": str(q1 & q2), |
| 22 | +# "q1 - q2": str(q1 - q2), |
| 23 | +# } |
| 24 | +# BUT MANUALLY CHECK THE OUTPUT BEFORE ADDING IT AS A TEST CASE! |
| 25 | + |
| 26 | + |
| 27 | +testcases = [ |
| 28 | + # Simplest case, only leaves differ |
| 29 | + { |
| 30 | + "q1": "root, a=1, b=1, c=1", |
| 31 | + "q2": "root, a=1, b=1, c=2", |
| 32 | + "union": "root, a=1, b=1, c=1/2", |
| 33 | + "intersection": "root", |
| 34 | + "q1 - q2": "root", |
| 35 | + }, |
| 36 | + # Some overlap but also each tree has unique items |
| 37 | + { |
| 38 | + "q1": "root, a=1, b=1, c=1/2/3", |
| 39 | + "q2": "root, a=1, b=1, c=2/3/4", |
| 40 | + "union": "root, a=1, b=1, c=1/2/3/4", |
| 41 | + "intersection": "root, a=1, b=1, c=2/3", |
| 42 | + "q1 - q2": "root", |
| 43 | + }, |
| 44 | + # Overlap at two levels |
| 45 | + { |
| 46 | + "q1": "root, a=1, b=1/2, c=1/2/3", |
| 47 | + "q2": "root, a=1, b=2/3, c=2/3/4", |
| 48 | + "union": """ |
| 49 | + root, a=1 |
| 50 | + ├── b=1, c=1/2/3 |
| 51 | + ├── b=2, c=1/2/3/4 |
| 52 | + └── b=3, c=2/3/4 |
| 53 | + """, |
| 54 | + "intersection": "root, a=1, b=2, c=2/3", |
| 55 | + "q1 - q2": "root", |
| 56 | + }, |
| 57 | + # Check that we can merge even if the divergence point is higher |
| 58 | + { |
| 59 | + "q1": "root, a=1, b=1, c=1", |
| 60 | + "q2": "root, a=2, b=1, c=1", |
| 61 | + "union": "root, a=1/2, b=1, c=1", |
| 62 | + "intersection": "root", |
| 63 | + "q1 - q2": "root, a=1, b=1, c=1", |
| 64 | + }, |
| 65 | + # Two equal qubes |
| 66 | + { |
| 67 | + "q1": "root, a=1, b=1, c=1", |
| 68 | + "q2": "root, a=1, b=1, c=1", |
| 69 | + "union": "root, a=1, b=1, c=1", |
| 70 | + "intersection": "root, a=1, b=1, c=1", |
| 71 | + "q1 - q2": "root", |
| 72 | + }, |
| 73 | + # With wildcards |
| 74 | + { |
| 75 | + "q1": "root, frequency=*, levtype=*, param=*, levelist=*, domain=a/b/c/d", |
| 76 | + "q2": "root, frequency=*, levtype=*, param=*, domain=a/b/c/d", |
| 77 | + "union": """ |
| 78 | + root, frequency=*, levtype=*, param=* |
| 79 | + ├── domain=a/b/c/d |
| 80 | + └── levelist=*, domain=a/b/c/d |
| 81 | + """, |
| 82 | + "intersection": "root", |
| 83 | + "q1 - q2": "root", |
| 84 | + }, |
| 85 | +] |
| 86 | + |
| 87 | + |
| 88 | +def test_cases(): |
| 89 | + for case in testcases: |
| 90 | + set_operation_testcase(case) |
| 91 | + |
| 92 | + |
4 | 93 | def test_leaf_conservation(): |
5 | 94 | q = Qube.from_dict( |
6 | 95 | { |
|
0 commit comments