Skip to content

Commit

Permalink
[test] Add jupyter notebook to tests (taichi-dev#6717)
Browse files Browse the repository at this point in the history
  • Loading branch information
neozhaoliang authored and quadpixels committed May 13, 2023
1 parent 4d0c3e5 commit 53df283
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 1 deletion.
1 change: 1 addition & 0 deletions requirements_test.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,4 @@ matplotlib
cffi
scipy
setproctitle
nbmake
68 changes: 68 additions & 0 deletions tests/python/test_ipython.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
{
"cells": [
{
"cell_type": "markdown",
"id": "51b3b00e",
"metadata": {},
"source": [
"# Count primes below a given bound"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "83062572",
"metadata": {},
"outputs": [],
"source": [
"import taichi as ti\n",
"\n",
"def test_kernel_print():\n",
" N = 10000\n",
" ti.init()\n",
" @ti.func\n",
" def is_prime(n: int):\n",
" result = True\n",
" for k in range(2, int(n**0.5) + 1):\n",
" if n % k == 0:\n",
" result = False\n",
" break\n",
" return result\n",
"\n",
" @ti.kernel\n",
" def count_primes(n: int) -> int:\n",
" count = 0\n",
" for k in range(2, n):\n",
" if is_prime(k):\n",
" count += 1\n",
"\n",
" return count\n",
"\n",
" count_primes(N)\n",
" \n",
"test_kernel_print()"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.10.6"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
3 changes: 2 additions & 1 deletion tests/run_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ def _test_python(args):
# auto-complete file names
if not f.startswith('test_'):
f = 'test_' + f
if not f.endswith('.py'):
if not (f.endswith('.py') or f.endswith('.ipynb')):
f = f + '.py'
file = os.path.join(test_dir, f)
has_tests = False
Expand All @@ -164,6 +164,7 @@ def _test_python(args):
else:
# run all the tests
pytest_args = [test_dir]
pytest_args += ['--nbmake']
if args.verbose:
pytest_args += ['-v']
if args.rerun:
Expand Down

0 comments on commit 53df283

Please sign in to comment.