-
Notifications
You must be signed in to change notification settings - Fork 393
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Generates algorithmic environment from algpseudocode #152
Merged
Merged
Changes from 7 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
4ada4b5
init alg
abac34b
comments and annotations
c0e4b27
fix indentation
bbd3f74
tests, procedure
349464b
forgot one
af7a3c8
expose Style, tests
8c91639
bug
280f416
rm visit_Expr from expr_codegen
0c00706
rm line
a66f8af
inline some code
119de91
specify codegen
5841f93
too long
3634a67
suggestions
628a2da
rm todo
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,79 @@ | ||
"""End-to-end test cases of algorithmic style.""" | ||
|
||
from __future__ import annotations | ||
|
||
from typing import Any, Callable | ||
|
||
from latexify import frontend | ||
|
||
|
||
def check_algorithm( | ||
fn: Callable[..., Any], | ||
latex: str, | ||
**kwargs, | ||
) -> None: | ||
"""Helper to check if the obtained function has the expected LaTeX form. | ||
Args: | ||
ZibingZhang marked this conversation as resolved.
Show resolved
Hide resolved
|
||
fn: Function to check. | ||
latex: LaTeX form of `fn`. | ||
**kwargs: Arguments passed to `frontend.get_latex`. | ||
""" | ||
# Checks the syntax: | ||
# def fn(...): | ||
# ... | ||
# latexified = get_latex(fn, style=ALGORITHM, **kwargs) | ||
latexified = frontend.get_latex(fn, style=frontend.Style.ALGORITHMIC, **kwargs) | ||
assert latexified == latex | ||
|
||
|
||
# TODO(ZibingZhang) changing fact -> factorial breaks because factorial is replaced by | ||
ZibingZhang marked this conversation as resolved.
Show resolved
Hide resolved
|
||
# !, substitutions should not occur for the name of the procedure | ||
def test_factorial() -> None: | ||
def fact(n): | ||
if n == 0: | ||
return 1 | ||
else: | ||
return n * fact(n - 1) | ||
|
||
latex = ( | ||
r"\begin{algorithmic} " | ||
ZibingZhang marked this conversation as resolved.
Show resolved
Hide resolved
|
||
r"\Procedure{fact}{$n$} " | ||
r"\If{$n = 0$} " | ||
r"\State \Return $1$ " | ||
r"\Else " | ||
r"\State \Return $n \mathrm{fact} \mathopen{}\left( n - 1 \mathclose{}\right)$ " | ||
r"\EndIf " | ||
r"\EndProcedure " | ||
r"\end{algorithmic}" | ||
) | ||
check_algorithm(fact, latex) | ||
|
||
|
||
def test_collatz() -> None: | ||
def collatz(n): | ||
iterations = 0 | ||
while n > 1: | ||
if n % 2 == 0: | ||
n = n / 2 | ||
ZibingZhang marked this conversation as resolved.
Show resolved
Hide resolved
|
||
else: | ||
n = 3 * n + 1 | ||
iterations = iterations + 1 | ||
return iterations | ||
|
||
latex = ( | ||
r"\begin{algorithmic} " | ||
r"\Procedure{collatz}{$n$} " | ||
r"\State $\mathrm{iterations} \gets 0$ " | ||
r"\While{$n > 1$} " | ||
r"\If{$n \mathbin{\%} 2 = 0$} " | ||
r"\State $n \gets \frac{n}{2}$ " | ||
r"\Else \State $n \gets 3 n + 1$ " | ||
r"\EndIf " | ||
r"\State $\mathrm{iterations} \gets \mathrm{iterations} + 1$ " | ||
r"\EndWhile " | ||
r"\State \Return $\mathrm{iterations}$ " | ||
r"\EndProcedure " | ||
r"\end{algorithmic}" | ||
) | ||
|
||
check_algorithm(collatz, latex) |
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
4 changes: 4 additions & 0 deletions
4
src/integration_tests/utils.py → src/integration_tests/integration_utils.py
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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this file should be a unit test
frontend_{algorithmic?}_test.py
becausefrontend
is not intended to be used directly.Or it'd be okay to use just exported functions and
Style
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I can move it, but
function_expansion_test
andregression_test
basically usefrontend
directly, as they usefrontend.function
.Similarly, this file uses$LaTeX$ output
function.get_latex
. Both of these functions are exported in__init__.py
, so it feels somewhat like an integration test, because we're testing end-to-end behavior from function to generatedThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So it turns that the issue comes from the old implementation of these tests, will consider it later.