-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #23 from opendilab/dev/delay
dev(hansbug): add delay feature in treevalue && add mode and missing option to union and subside
- Loading branch information
Showing
32 changed files
with
1,280 additions
and
128 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
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
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
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,68 @@ | ||
import pytest | ||
|
||
from treevalue.tree.common import DelayedProxy, delayed_partial | ||
|
||
|
||
@pytest.mark.unittest | ||
class TestTreeDelay: | ||
def test_delayed_partial_simple(self): | ||
cnt = 0 | ||
|
||
def f(): | ||
nonlocal cnt | ||
cnt += 1 | ||
return 1 | ||
|
||
pv = delayed_partial(f) | ||
assert cnt == 0 | ||
assert isinstance(pv, DelayedProxy) | ||
|
||
assert pv.value() == 1 | ||
assert cnt == 1 | ||
|
||
assert pv.value() == 1 | ||
assert cnt == 1 | ||
|
||
def test_delayed_partial_func(self): | ||
cnt = 0 | ||
|
||
def f(x, y): | ||
nonlocal cnt | ||
cnt += 1 | ||
return x + y * 2 + 1 | ||
|
||
pv = delayed_partial(f, 2, y=3) | ||
assert cnt == 0 | ||
assert isinstance(pv, DelayedProxy) | ||
|
||
assert pv.value() == 9 | ||
assert cnt == 1 | ||
|
||
assert pv.value() == 9 | ||
assert cnt == 1 | ||
|
||
def test_delayed_partial_complex(self): | ||
cnt1, cnt2 = 0, 0 | ||
|
||
def f1(): | ||
nonlocal cnt1 | ||
cnt1 += 1 | ||
return 1 | ||
|
||
def f2(x, y): | ||
nonlocal cnt2 | ||
cnt2 += 1 | ||
return (x + 1) ** 2 + y + 2 | ||
|
||
pv = delayed_partial(f2, delayed_partial(f1), delayed_partial(f1)) | ||
assert cnt1 == 0 | ||
assert cnt2 == 0 | ||
assert isinstance(pv, DelayedProxy) | ||
|
||
assert pv.value() == 7 | ||
assert cnt1 == 2 | ||
assert cnt2 == 1 | ||
|
||
assert pv.value() == 7 | ||
assert cnt1 == 2 | ||
assert cnt2 == 1 |
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
Oops, something went wrong.