From eb078efc4e73aa9be4b821578bf161e991444c12 Mon Sep 17 00:00:00 2001 From: Anton Chernyatevich Date: Sat, 14 Nov 2020 23:18:44 +0200 Subject: [PATCH] Replace custom test runner with pytest runner --- .gitignore | 7 + README.md | 19 +- setup.cfg | 20 +- tests/__init__.py | 10 - tests/__main__.py | 51 ---- tests/simple/__init__.py | 44 ---- tests/simple/_util.py | 62 ----- tests/simple/algorithm.py | 120 ---------- tests/simple/arith.py | 220 ------------------ tests/simple/array_test.py | 69 ------ tests/simple/data.py | 77 ------ tests/simple/image.py | 100 -------- tests/simple/lapack.py | 92 -------- tests/simple/random.py | 41 ---- tests/simple/signal.py | 129 ---------- tests/simple/sparse.py | 32 --- tests/simple/statistics.py | 67 ------ tests/smoke/__init__.py | 0 tests/smoke/test_algorithm.py | 118 ++++++++++ tests/smoke/test_arith.py | 213 +++++++++++++++++ tests/smoke/test_array.py | 97 ++++++++ tests/{simple/blas.py => smoke/test_blas.py} | 18 +- tests/smoke/test_data.py | 71 ++++++ .../device.py => smoke/test_device.py} | 40 ++-- tests/smoke/test_image.py | 93 ++++++++ .../{simple/index.py => smoke/test_index.py} | 65 +++--- .../interop.py => smoke/test_interop.py} | 9 +- tests/smoke/test_lapack.py | 84 +++++++ tests/smoke/test_random.py | 34 +++ tests/smoke/test_signal.py | 122 ++++++++++ tests/smoke/test_sparse.py | 24 ++ tests/smoke/test_statistics.py | 59 +++++ 32 files changed, 1003 insertions(+), 1204 deletions(-) delete mode 100644 tests/__main__.py delete mode 100644 tests/simple/__init__.py delete mode 100644 tests/simple/_util.py delete mode 100644 tests/simple/algorithm.py delete mode 100644 tests/simple/arith.py delete mode 100644 tests/simple/array_test.py delete mode 100644 tests/simple/data.py delete mode 100644 tests/simple/image.py delete mode 100644 tests/simple/lapack.py delete mode 100644 tests/simple/random.py delete mode 100644 tests/simple/signal.py delete mode 100644 tests/simple/sparse.py delete mode 100644 tests/simple/statistics.py create mode 100644 tests/smoke/__init__.py create mode 100644 tests/smoke/test_algorithm.py create mode 100644 tests/smoke/test_arith.py create mode 100644 tests/smoke/test_array.py rename tests/{simple/blas.py => smoke/test_blas.py} (53%) create mode 100644 tests/smoke/test_data.py rename tests/{simple/device.py => smoke/test_device.py} (66%) create mode 100644 tests/smoke/test_image.py rename tests/{simple/index.py => smoke/test_index.py} (56%) rename tests/{simple/interop.py => smoke/test_interop.py} (96%) create mode 100644 tests/smoke/test_lapack.py create mode 100644 tests/smoke/test_random.py create mode 100644 tests/smoke/test_signal.py create mode 100644 tests/smoke/test_sparse.py create mode 100644 tests/smoke/test_statistics.py diff --git a/.gitignore b/.gitignore index 8d944a440..1b90893b1 100644 --- a/.gitignore +++ b/.gitignore @@ -34,3 +34,10 @@ venv # IDEs .vscode .idea + +# Test coverage +.coverage + +# Cache +.pytest_cache +.mypy_cache diff --git a/README.md b/README.md index fb3a1b8a1..31c717b09 100644 --- a/README.md +++ b/README.md @@ -22,8 +22,7 @@ def calc_pi_device(samples): return 4 * af.count(within_unit_circle) / samples ``` - -Choosing a particular backend can be done using `af.set_backend(name)` where name is either "_cuda_", "_opencl_", or "_cpu_". The default device is chosen in the same order of preference. +Choosing a particular backend can be done using `af.set_backend(name)` where name is either "_cuda_", "_opencl_", or "_cpu_". The default device is chosen in the same order of preference. ## Requirements @@ -41,19 +40,19 @@ Please check the following links for dependencies. **Install the last stable version:** -``` +```bash pip install arrayfire ``` **Install the development version:** -``` +```bash pip install git+git://github.com/arrayfire/arrayfire-python.git@devel ``` **Installing offline:** -``` +```bash cd path/to/arrayfire-python python setup.py install ``` @@ -62,16 +61,16 @@ python setup.py install Please follow [these instructions](https://github.com/arrayfire/arrayfire-python/wiki) to ensure the arrayfire-python can find the arrayfire libraries. -To run arrayfire tests, you can run the following command from command line. +To run arrayfire smoke tests, you can run the following command from command line. -``` -python -m arrayfire.tests +```bash +python setup.py test ``` ## Communication -* [Slack Chat](https://join.slack.com/t/arrayfire-org/shared_invite/MjI4MjIzMDMzMTczLTE1MDI5ODg4NzYtN2QwNGE3ODA5OQ) -* [Google Groups](https://groups.google.com/forum/#!forum/arrayfire-users) +- [Slack Chat](https://join.slack.com/t/arrayfire-org/shared_invite/MjI4MjIzMDMzMTczLTE1MDI5ODg4NzYtN2QwNGE3ODA5OQ) +- [Google Groups](https://groups.google.com/forum/#!forum/arrayfire-users) ## Acknowledgements diff --git a/setup.cfg b/setup.cfg index 5f6ccf0ed..35be5c905 100644 --- a/setup.cfg +++ b/setup.cfg @@ -17,14 +17,32 @@ classifiers = [options] packages = find: python_requires = >=3.6.0 +setup_requires = + pytest-runner +tests_require = + flake8-import-order + flake8-quotes + pytest-cov + pytest-flake8 + pytest-mypy [options.packages.find] -include = arrayfire exclude = examples tests +[aliases] +test = pytest + [flake8] application-import-names = arrayfire import-order-style = pep8 max-line-length = 119 + +[tool:pytest] +addopts = --cache-clear --cov=arrayfire --cov-fail-under=70 +console_output_style = classic + +[coverage:report] +show_missing = true +skip_covered = true diff --git a/tests/__init__.py b/tests/__init__.py index 0c6d787de..e69de29bb 100644 --- a/tests/__init__.py +++ b/tests/__init__.py @@ -1,10 +0,0 @@ -#!/usr/bin/env python - -####################################################### -# Copyright (c) 2019, ArrayFire -# All rights reserved. -# -# This file is distributed under 3-clause BSD license. -# The complete license agreement can be obtained at: -# http://arrayfire.com/licenses/BSD-3-Clause -######################################################## diff --git a/tests/__main__.py b/tests/__main__.py deleted file mode 100644 index 190c5c8f8..000000000 --- a/tests/__main__.py +++ /dev/null @@ -1,51 +0,0 @@ -#!/usr/bin/env python - -####################################################### -# Copyright (c) 2019, ArrayFire -# All rights reserved. -# -# This file is distributed under 3-clause BSD license. -# The complete license agreement can be obtained at: -# http://arrayfire.com/licenses/BSD-3-Clause -######################################################## - -from __future__ import absolute_import - -import sys - -from . import simple - -tests = {} -tests['simple'] = simple.tests - - -def assert_valid(name, name_list, name_str): - is_valid = any([name == val for val in name_list]) - if is_valid: - return - err_str = "The first argument needs to be a %s name\n" % name_str - err_str += "List of supported %ss: %s" % (name_str, str(list(name_list))) - raise RuntimeError(err_str) - - -if __name__ == "__main__": - module_name = None - num_args = len(sys.argv) - - if num_args > 1: - module_name = sys.argv[1].lower() - assert_valid(sys.argv[1].lower(), tests.keys(), "module") - - if module_name is None: - for name in tests: - tests[name].run() - else: - test = tests[module_name] - test_list = None - - if num_args > 2: - test_list = sys.argv[2:] - for test_name in test_list: - assert_valid(test_name.lower(), test.keys(), "test") - - test.run(test_list) diff --git a/tests/simple/__init__.py b/tests/simple/__init__.py deleted file mode 100644 index abedbc4ba..000000000 --- a/tests/simple/__init__.py +++ /dev/null @@ -1,44 +0,0 @@ -#!/usr/bin/env python - -####################################################### -# Copyright (c) 2019, ArrayFire -# All rights reserved. -# -# This file is distributed under 3-clause BSD license. -# The complete license agreement can be obtained at: -# http://arrayfire.com/licenses/BSD-3-Clause -######################################################## - -from ._util import tests -from .algorithm import simple_algorithm -from .arith import simple_arith -from .array_test import simple_array -from .blas import simple_blas -from .data import simple_data -from .device import simple_device -from .image import simple_image -from .index import simple_index -from .interop import simple_interop -from .lapack import simple_lapack -from .random import simple_random -from .signal import simple_signal -from .sparse import simple_sparse -from .statistics import simple_statistics - -__all__ = [ - "tests", - "simple_algorithm", - "simple_arith", - "simple_array", - "simple_blas", - "simple_data", - "simple_device", - "simple_image", - "simple_index", - "simple_interop", - "simple_lapack", - "simple_random", - "simple_signal", - "simple_sparse", - "simple_statistics" -] diff --git a/tests/simple/_util.py b/tests/simple/_util.py deleted file mode 100644 index 339bdd382..000000000 --- a/tests/simple/_util.py +++ /dev/null @@ -1,62 +0,0 @@ -#!/usr/bin/env python - -####################################################### -# Copyright (c) 2019, ArrayFire -# All rights reserved. -# -# This file is distributed under 3-clause BSD license. -# The complete license agreement can be obtained at: -# http://arrayfire.com/licenses/BSD-3-Clause -######################################################## - -import logging -import sys -import traceback - - -class _simple_test_dict(dict): - def __init__(self): - self.print_str = "Simple %16s: %s" - self.failed = False - super(_simple_test_dict, self).__init__() - - def run(self, name_list=None, verbose=False): - test_list = name_list if name_list is not None else self.keys() - for key in test_list: - self.print_log = "" - try: - test = self[key] - except KeyError: - print(self.print_str % (key, "NOTFOUND")) - continue - - try: - test(verbose) - print(self.print_str % (key, "PASSED")) - except Exception: - print(self.print_str % (key, "FAILED")) - self.failed = True - if not verbose: - print(tests.print_log) - logging.error(traceback.format_exc()) - - if self.failed: - sys.exit(1) - - -tests = _simple_test_dict() - - -def print_func(verbose): - def print_func_impl(*args): - _print_log = "" - for arg in args: - _print_log += str(arg) + '\n' - if verbose: - print(_print_log) - tests.print_log += _print_log - return print_func_impl - - -def display_func(verbose): - return print_func(verbose) diff --git a/tests/simple/algorithm.py b/tests/simple/algorithm.py deleted file mode 100644 index d46b6d8cc..000000000 --- a/tests/simple/algorithm.py +++ /dev/null @@ -1,120 +0,0 @@ -#!/usr/bin/env python - -####################################################### -# Copyright (c) 2019, ArrayFire -# All rights reserved. -# -# This file is distributed under 3-clause BSD license. -# The complete license agreement can be obtained at: -# http://arrayfire.com/licenses/BSD-3-Clause -######################################################## - -import arrayfire as af - -from . import _util - - -def simple_algorithm(verbose=False): - display_func = _util.display_func(verbose) - print_func = _util.print_func(verbose) - - a = af.randu(3, 3) - k = af.constant(1, 3, 3, dtype=af.Dtype.u32) - af.eval(k) - - print_func(af.sum(a), af.product(a), af.min(a), af.max(a), af.count(a), af.any_true(a), af.all_true(a)) - - display_func(af.sum(a, 0)) - display_func(af.sum(a, 1)) - - rk = af.constant(1, 3, dtype=af.Dtype.u32) - rk[2] = 0 - af.eval(rk) - display_func(af.sumByKey(rk, a, dim=0)) - display_func(af.sumByKey(rk, a, dim=1)) - - display_func(af.productByKey(rk, a, dim=0)) - display_func(af.productByKey(rk, a, dim=1)) - - display_func(af.minByKey(rk, a, dim=0)) - display_func(af.minByKey(rk, a, dim=1)) - - display_func(af.maxByKey(rk, a, dim=0)) - display_func(af.maxByKey(rk, a, dim=1)) - - display_func(af.anyTrueByKey(rk, a, dim=0)) - display_func(af.anyTrueByKey(rk, a, dim=1)) - - display_func(af.allTrueByKey(rk, a, dim=0)) - display_func(af.allTrueByKey(rk, a, dim=1)) - - display_func(af.countByKey(rk, a, dim=0)) - display_func(af.countByKey(rk, a, dim=1)) - - display_func(af.product(a, 0)) - display_func(af.product(a, 1)) - - display_func(af.min(a, 0)) - display_func(af.min(a, 1)) - - display_func(af.max(a, 0)) - display_func(af.max(a, 1)) - - display_func(af.count(a, 0)) - display_func(af.count(a, 1)) - - display_func(af.any_true(a, 0)) - display_func(af.any_true(a, 1)) - - display_func(af.all_true(a, 0)) - display_func(af.all_true(a, 1)) - - display_func(af.accum(a, 0)) - display_func(af.accum(a, 1)) - - display_func(af.scan(a, 0, af.BINARYOP.ADD)) - display_func(af.scan(a, 1, af.BINARYOP.MAX)) - - display_func(af.scan_by_key(k, a, 0, af.BINARYOP.ADD)) - display_func(af.scan_by_key(k, a, 1, af.BINARYOP.MAX)) - - display_func(af.sort(a, is_ascending=True)) - display_func(af.sort(a, is_ascending=False)) - - b = (a > 0.1) * a - c = (a > 0.4) * a - d = b / c - print_func(af.sum(d)) - print_func(af.sum(d, nan_val=0.0)) - display_func(af.sum(d, dim=0, nan_val=0.0)) - - val, idx = af.sort_index(a, is_ascending=True) - display_func(val) - display_func(idx) - val, idx = af.sort_index(a, is_ascending=False) - display_func(val) - display_func(idx) - - b = af.randu(3, 3) - keys, vals = af.sort_by_key(a, b, is_ascending=True) - display_func(keys) - display_func(vals) - keys, vals = af.sort_by_key(a, b, is_ascending=False) - display_func(keys) - display_func(vals) - - c = af.randu(5, 1) - d = af.randu(5, 1) - cc = af.set_unique(c, is_sorted=False) - dd = af.set_unique(af.sort(d), is_sorted=True) - display_func(cc) - display_func(dd) - - display_func(af.set_union(cc, dd, is_unique=True)) - display_func(af.set_union(cc, dd, is_unique=False)) - - display_func(af.set_intersect(cc, cc, is_unique=True)) - display_func(af.set_intersect(cc, cc, is_unique=False)) - - -_util.tests["algorithm"] = simple_algorithm diff --git a/tests/simple/arith.py b/tests/simple/arith.py deleted file mode 100644 index 05c5dd47a..000000000 --- a/tests/simple/arith.py +++ /dev/null @@ -1,220 +0,0 @@ -#!/usr/bin/env python - -####################################################### -# Copyright (c) 2019, ArrayFire -# All rights reserved. -# -# This file is distributed under 3-clause BSD license. -# The complete license agreement can be obtained at: -# http://arrayfire.com/licenses/BSD-3-Clause -######################################################## - -import arrayfire as af - -from . import _util - - -def simple_arith(verbose=False): - display_func = _util.display_func(verbose) - - a = af.randu(3, 3) - b = af.constant(4, 3, 3) - display_func(a) - display_func(b) - - c = a + b - d = a - d += b - - display_func(c) - display_func(d) - display_func(a + 2) - display_func(3 + a) - - c = a - b - d = a - d -= b - - display_func(c) - display_func(d) - display_func(a - 2) - display_func(3 - a) - - c = a * b - d = a - d *= b - - display_func(c * 2) - display_func(3 * d) - display_func(a * 2) - display_func(3 * a) - - c = a / b - d = a - d /= b - - display_func(c / 2.0) - display_func(3.0 / d) - display_func(a / 2) - display_func(3 / a) - - c = a % b - d = a - d %= b - - display_func(c % 2.0) - display_func(3.0 % d) - display_func(a % 2) - display_func(3 % a) - - c = a ** b - d = a - d **= b - - display_func(c ** 2.0) - display_func(3.0 ** d) - display_func(a ** 2) - display_func(3 ** a) - - display_func(a < b) - display_func(a < 0.5) - display_func(a > 0.5) - - display_func(a <= b) - display_func(a <= 0.5) - display_func(a >= 0.5) - - display_func(a > b) - display_func(a > 0.5) - display_func(a < 0.5) - - display_func(a >= b) - display_func(a >= 0.5) - display_func(a <= 0.5) - - display_func(a != b) - display_func(a != 0.5) - - display_func(a == b) - display_func(a == 0.5) - - a = af.randu(3, 3, dtype=af.Dtype.u32) - b = af.constant(4, 3, 3, dtype=af.Dtype.u32) - - display_func(a & b) - display_func(a & 2) - c = a - c &= 2 - display_func(c) - - display_func(a | b) - display_func(a | 2) - c = a - c |= 2 - display_func(c) - - display_func(a >> b) - display_func(a >> 2) - c = a - c >>= 2 - display_func(c) - - display_func(a << b) - display_func(a << 2) - c = a - c <<= 2 - display_func(c) - - display_func(-a) - display_func(+a) - display_func(~a) - display_func(a) - - display_func(af.cast(a, af.Dtype.c32)) - display_func(af.maxof(a, b)) - display_func(af.minof(a, b)) - - display_func(af.clamp(a, 0, 1)) - display_func(af.clamp(a, 0, b)) - display_func(af.clamp(a, b, 1)) - - display_func(af.rem(a, b)) - - a = af.randu(3, 3) - 0.5 - b = af.randu(3, 3) - 0.5 - - display_func(af.abs(a)) - display_func(af.arg(a)) - display_func(af.sign(a)) - display_func(af.round(a)) - display_func(af.trunc(a)) - display_func(af.floor(a)) - display_func(af.ceil(a)) - display_func(af.hypot(a, b)) - display_func(af.sin(a)) - display_func(af.cos(a)) - display_func(af.tan(a)) - display_func(af.asin(a)) - display_func(af.acos(a)) - display_func(af.atan(a)) - display_func(af.atan2(a, b)) - - c = af.cplx(a) - d = af.cplx(a, b) - display_func(c) - display_func(d) - display_func(af.real(d)) - display_func(af.imag(d)) - display_func(af.conjg(d)) - - display_func(af.sinh(a)) - display_func(af.cosh(a)) - display_func(af.tanh(a)) - display_func(af.asinh(a)) - display_func(af.acosh(a)) - display_func(af.atanh(a)) - - a = af.abs(a) - b = af.abs(b) - - display_func(af.root(a, b)) - display_func(af.pow(a, b)) - display_func(af.pow2(a)) - display_func(af.sigmoid(a)) - display_func(af.exp(a)) - display_func(af.expm1(a)) - display_func(af.erf(a)) - display_func(af.erfc(a)) - display_func(af.log(a)) - display_func(af.log1p(a)) - display_func(af.log10(a)) - display_func(af.log2(a)) - display_func(af.sqrt(a)) - display_func(af.rsqrt(a)) - display_func(af.cbrt(a)) - - a = af.round(5 * af.randu(3, 3) - 1) - b = af.round(5 * af.randu(3, 3) - 1) - - display_func(af.factorial(a)) - display_func(af.tgamma(a)) - display_func(af.lgamma(a)) - display_func(af.iszero(a)) - display_func(af.isinf(a/b)) - display_func(af.isnan(a/a)) - - a = af.randu(5, 1) - b = af.randu(1, 5) - c = af.broadcast(lambda x, y: x+y, a, b) - display_func(a) - display_func(b) - display_func(c) - - @af.broadcast - def test_add(aa, bb): - return aa + bb - - display_func(test_add(a, b)) - - -_util.tests["arith"] = simple_arith diff --git a/tests/simple/array_test.py b/tests/simple/array_test.py deleted file mode 100644 index b1d9d1b18..000000000 --- a/tests/simple/array_test.py +++ /dev/null @@ -1,69 +0,0 @@ -#!/usr/bin/env python - -####################################################### -# Copyright (c) 2019, ArrayFire -# All rights reserved. -# -# This file is distributed under 3-clause BSD license. -# The complete license agreement can be obtained at: -# http://arrayfire.com/licenses/BSD-3-Clause -######################################################## - -import array as host - -import arrayfire as af - -from . import _util - - -def simple_array(verbose=False): - display_func = _util.display_func(verbose) - print_func = _util.print_func(verbose) - - a = af.array.Array([1, 2, 3]) - display_func(a) - display_func(a.T) - display_func(a.H) - print_func(a.shape) - - b = a.as_type(af.Dtype.s32) - display_func(b) - - print_func(a.elements(), a.type(), a.dims(), a.numdims()) - print_func(a.is_empty(), a.is_scalar(), a.is_column(), a.is_row()) - print_func(a.is_complex(), a.is_real(), a.is_double(), a.is_single()) - print_func(a.is_real_floating(), a.is_floating(), a.is_integer(), a.is_bool()) - - a = af.array.Array(host.array("i", [4, 5, 6])) - display_func(a) - print_func(a.elements(), a.type(), a.dims(), a.numdims()) - print_func(a.is_empty(), a.is_scalar(), a.is_column(), a.is_row()) - print_func(a.is_complex(), a.is_real(), a.is_double(), a.is_single()) - print_func(a.is_real_floating(), a.is_floating(), a.is_integer(), a.is_bool()) - - a = af.array.Array(host.array("I", [7, 8, 9] * 3), (3, 3)) - display_func(a) - print_func(a.elements(), a.type(), a.dims(), a.numdims()) - print_func(a.is_empty(), a.is_scalar(), a.is_column(), a.is_row()) - print_func(a.is_complex(), a.is_real(), a.is_double(), a.is_single()) - print_func(a.is_real_floating(), a.is_floating(), a.is_integer(), a.is_bool()) - - c = a.to_ctype() - for n in range(a.elements()): - print_func(c[n]) - - c, s = a.to_ctype(True, True) - for n in range(a.elements()): - print_func(c[n]) - print_func(s) - - arr = a.to_array() - lst = a.to_list(True) - - print_func(arr) - print_func(lst) - - print_func(a.is_sparse()) - - -_util.tests["array"] = simple_array diff --git a/tests/simple/data.py b/tests/simple/data.py deleted file mode 100644 index 3120aec09..000000000 --- a/tests/simple/data.py +++ /dev/null @@ -1,77 +0,0 @@ -#!/usr/bin/env python - -####################################################### -# Copyright (c) 2019, ArrayFire -# All rights reserved. -# -# This file is distributed under 3-clause BSD license. -# The complete license agreement can be obtained at: -# http://arrayfire.com/licenses/BSD-3-Clause -######################################################## - -import arrayfire as af - -from . import _util - - -def simple_data(verbose=False): - display_func = _util.display_func(verbose) - - display_func(af.constant(100, 3, 3, dtype=af.Dtype.f32)) - display_func(af.constant(25, 3, 3, dtype=af.Dtype.c32)) - display_func(af.constant(2**50, 3, 3, dtype=af.Dtype.s64)) - display_func(af.constant(2+3j, 3, 3)) - display_func(af.constant(3+5j, 3, 3, dtype=af.Dtype.c32)) - - display_func(af.range(3, 3)) - display_func(af.iota(3, 3, tile_dims=(2, 2))) - - display_func(af.identity(3, 3, 1, 2, af.Dtype.b8)) - display_func(af.identity(3, 3, dtype=af.Dtype.c32)) - - a = af.randu(3, 4) - b = af.diag(a, extract=True) - c = af.diag(a, 1, extract=True) - - display_func(a) - display_func(b) - display_func(c) - - display_func(af.diag(b, extract=False)) - display_func(af.diag(c, 1, extract=False)) - - display_func(af.join(0, a, a)) - display_func(af.join(1, a, a, a)) - - display_func(af.tile(a, 2, 2)) - - display_func(af.reorder(a, 1, 0)) - - display_func(af.shift(a, -1, 1)) - - display_func(af.moddims(a, 6, 2)) - - display_func(af.flat(a)) - - display_func(af.flip(a, 0)) - display_func(af.flip(a, 1)) - - display_func(af.lower(a, False)) - display_func(af.lower(a, True)) - - display_func(af.upper(a, False)) - display_func(af.upper(a, True)) - - a = af.randu(5, 5) - display_func(af.transpose(a)) - af.transpose_inplace(a) - display_func(a) - - display_func(af.select(a > 0.3, a, -0.3)) - - af.replace(a, a > 0.3, -0.3) - display_func(a) - - display_func(af.pad(a, (1, 1, 0, 0), (2, 2, 0, 0))) - -_util.tests["data"] = simple_data diff --git a/tests/simple/image.py b/tests/simple/image.py deleted file mode 100644 index c3bb48142..000000000 --- a/tests/simple/image.py +++ /dev/null @@ -1,100 +0,0 @@ -#!/usr/bin/env python - -####################################################### -# Copyright (c) 2019, ArrayFire -# All rights reserved. -# -# This file is distributed under 3-clause BSD license. -# The complete license agreement can be obtained at: -# http://arrayfire.com/licenses/BSD-3-Clause -######################################################## - -import arrayfire as af - -from . import _util - - -def simple_image(verbose=False): - display_func = _util.display_func(verbose) - - a = 10 * af.randu(6, 6) - a3 = 10 * af.randu(5, 5, 3) - - dx, dy = af.gradient(a) - display_func(dx) - display_func(dy) - - display_func(af.resize(a, scale=0.5)) - display_func(af.resize(a, odim0=8, odim1=8)) - - t = af.randu(3, 2) - display_func(af.transform(a, t)) - display_func(af.rotate(a, 3.14)) - display_func(af.translate(a, 1, 1)) - display_func(af.scale(a, 1.2, 1.2, 7, 7)) - display_func(af.skew(a, 0.02, 0.02)) - h = af.histogram(a, 3) - display_func(h) - display_func(af.hist_equal(a, h)) - - display_func(af.dilate(a)) - display_func(af.erode(a)) - - display_func(af.dilate3(a3)) - display_func(af.erode3(a3)) - - display_func(af.bilateral(a, 1, 2)) - display_func(af.mean_shift(a, 1, 2, 3)) - - display_func(af.medfilt(a)) - display_func(af.minfilt(a)) - display_func(af.maxfilt(a)) - - display_func(af.regions(af.round(a) > 3)) - display_func(af.confidenceCC(af.randu(10, 10), - (af.randu(2) * 9).as_type(af.Dtype.u32), (af.randu(2) * 9).as_type(af.Dtype.u32), 3, 3, 10, 0.1)) - - - dx, dy = af.sobel_derivatives(a) - display_func(dx) - display_func(dy) - display_func(af.sobel_filter(a)) - display_func(af.gaussian_kernel(3, 3)) - display_func(af.gaussian_kernel(3, 3, 1, 1)) - - ac = af.gray2rgb(a) - display_func(ac) - display_func(af.rgb2gray(ac)) - ah = af.rgb2hsv(ac) - display_func(ah) - display_func(af.hsv2rgb(ah)) - - display_func(af.color_space(a, af.CSPACE.RGB, af.CSPACE.GRAY)) - - a = af.randu(6, 6) - b = af.unwrap(a, 2, 2, 2, 2) - c = af.wrap(b, 6, 6, 2, 2, 2, 2) - display_func(a) - display_func(b) - display_func(c) - display_func(af.sat(a)) - - a = af.randu(10, 10, 3) - display_func(af.rgb2ycbcr(a)) - display_func(af.ycbcr2rgb(a)) - - a = af.randu(10, 10) - b = af.canny(a, low_threshold=0.2, high_threshold=0.8) - - # FIXME: OpenCL Error (-11): Build Program Failure when calling clBuildProgram - # display_func(af.anisotropic_diffusion(a, 0.125, 1.0, 64, af.FLUX.QUADRATIC, af.DIFFUSION.GRAD)) - - a = af.randu(10, 10) - psf = af.gaussian_kernel(3, 3) - cimg = af.convolve(a, psf) - display_func(af.iterativeDeconv(cimg, psf, 100, 0.5, af.ITERATIVE_DECONV.LANDWEBER)) - display_func(af.iterativeDeconv(cimg, psf, 100, 0.5, af.ITERATIVE_DECONV.RICHARDSONLUCY)) - display_func(af.inverseDeconv(cimg, psf, 1.0, af.INVERSE_DECONV.TIKHONOV)) - - -_util.tests["image"] = simple_image diff --git a/tests/simple/lapack.py b/tests/simple/lapack.py deleted file mode 100644 index 600e22605..000000000 --- a/tests/simple/lapack.py +++ /dev/null @@ -1,92 +0,0 @@ -#!/usr/bin/env python - -####################################################### -# Copyright (c) 2019, ArrayFire -# All rights reserved. -# -# This file is distributed under 3-clause BSD license. -# The complete license agreement can be obtained at: -# http://arrayfire.com/licenses/BSD-3-Clause -######################################################## - -import arrayfire as af - -from . import _util - - -def simple_lapack(verbose=False): - display_func = _util.display_func(verbose) - print_func = _util.print_func(verbose) - a = af.randu(5, 5) - - l, u, p = af.lu(a) - - display_func(l) - display_func(u) - display_func(p) - - p = af.lu_inplace(a, "full") - - display_func(a) - display_func(p) - - a = af.randu(5, 3) - - q, r, t = af.qr(a) - - display_func(q) - display_func(r) - display_func(t) - - af.qr_inplace(a) - - display_func(a) - - a = af.randu(5, 5) - a = af.matmulTN(a, a.copy()) + 10 * af.identity(5, 5) - - R, info = af.cholesky(a) - display_func(R) - print_func(info) - - af.cholesky_inplace(a) - display_func(a) - - a = af.randu(5, 5) - ai = af.inverse(a) - - display_func(a) - display_func(ai) - - ai = af.pinverse(a) - display_func(ai) - - x0 = af.randu(5, 3) - b = af.matmul(a, x0) - x1 = af.solve(a, b) - - display_func(x0) - display_func(x1) - - p = af.lu_inplace(a) - - x2 = af.solve_lu(a, p, b) - - display_func(x2) - - print_func(af.rank(a)) - print_func(af.det(a)) - print_func(af.norm(a, af.NORM.EUCLID)) - print_func(af.norm(a, af.NORM.MATRIX_1)) - print_func(af.norm(a, af.NORM.MATRIX_INF)) - print_func(af.norm(a, af.NORM.MATRIX_L_PQ, 1, 1)) - - a = af.randu(10, 10) - display_func(a) - u, s, vt = af.svd(a) - display_func(af.matmul(af.matmul(u, af.diag(s, 0, False)), vt)) - u, s, vt = af.svd_inplace(a) - display_func(af.matmul(af.matmul(u, af.diag(s, 0, False)), vt)) - - -_util.tests["lapack"] = simple_lapack diff --git a/tests/simple/random.py b/tests/simple/random.py deleted file mode 100644 index dcf7fbca6..000000000 --- a/tests/simple/random.py +++ /dev/null @@ -1,41 +0,0 @@ -#!/usr/bin/env python - -####################################################### -# Copyright (c) 2019, ArrayFire -# All rights reserved. -# -# This file is distributed under 3-clause BSD license. -# The complete license agreement can be obtained at: -# http://arrayfire.com/licenses/BSD-3-Clause -######################################################## - -import arrayfire as af - -from . import _util - - -def simple_random(verbose=False): - display_func = _util.display_func(verbose) - - display_func(af.randu(3, 3, 1, 2)) - display_func(af.randu(3, 3, 1, 2, af.Dtype.b8)) - display_func(af.randu(3, 3, dtype=af.Dtype.c32)) - - display_func(af.randn(3, 3, 1, 2)) - display_func(af.randn(3, 3, dtype=af.Dtype.c32)) - - af.set_seed(1024) - assert af.get_seed() == 1024 - - engine = af.Random_Engine(af.RANDOM_ENGINE.MERSENNE_GP11213, 100) - - display_func(af.randu(3, 3, 1, 2, engine=engine)) - display_func(af.randu(3, 3, 1, 2, af.Dtype.s32, engine=engine)) - display_func(af.randu(3, 3, dtype=af.Dtype.c32, engine=engine)) - - display_func(af.randn(3, 3, engine=engine)) - engine.set_seed(100) - assert engine.get_seed() == 100 - - -_util.tests["random"] = simple_random diff --git a/tests/simple/signal.py b/tests/simple/signal.py deleted file mode 100644 index b2a337ed5..000000000 --- a/tests/simple/signal.py +++ /dev/null @@ -1,129 +0,0 @@ -#!/usr/bin/env python - -####################################################### -# Copyright (c) 2019, ArrayFire -# All rights reserved. -# -# This file is distributed under 3-clause BSD license. -# The complete license agreement can be obtained at: -# http://arrayfire.com/licenses/BSD-3-Clause -######################################################## - -import arrayfire as af - -from . import _util - - -def simple_signal(verbose=False): - display_func = _util.display_func(verbose) - - signal = af.randu(10) - x_new = af.randu(10) - x_orig = af.randu(10) - display_func(af.approx1(signal, x_new, xp=x_orig)) - - signal = af.randu(3, 3) - x_new = af.randu(3, 3) - x_orig = af.randu(3, 3) - y_new = af.randu(3, 3) - y_orig = af.randu(3, 3) - - display_func(af.approx2(signal, x_new, y_new, xp=x_orig, yp=y_orig)) - - a = af.randu(8, 1) - display_func(a) - - display_func(af.fft(a)) - display_func(af.dft(a)) - display_func(af.real(af.ifft(af.fft(a)))) - display_func(af.real(af.idft(af.dft(a)))) - - b = af.fft(a) - af.ifft_inplace(b) - display_func(b) - af.fft_inplace(b) - display_func(b) - - b = af.fft_r2c(a) - c = af.fft_c2r(b) - display_func(b) - display_func(c) - - a = af.randu(4, 4) - display_func(a) - - display_func(af.fft2(a)) - display_func(af.dft(a)) - display_func(af.real(af.ifft2(af.fft2(a)))) - display_func(af.real(af.idft(af.dft(a)))) - - b = af.fft2(a) - af.ifft2_inplace(b) - display_func(b) - af.fft2_inplace(b) - display_func(b) - - b = af.fft2_r2c(a) - c = af.fft2_c2r(b) - display_func(b) - display_func(c) - - a = af.randu(4, 4, 2) - display_func(a) - - display_func(af.fft3(a)) - display_func(af.dft(a)) - display_func(af.real(af.ifft3(af.fft3(a)))) - display_func(af.real(af.idft(af.dft(a)))) - - b = af.fft3(a) - af.ifft3_inplace(b) - display_func(b) - af.fft3_inplace(b) - display_func(b) - - b = af.fft3_r2c(a) - c = af.fft3_c2r(b) - display_func(b) - display_func(c) - - a = af.randu(10, 1) - b = af.randu(3, 1) - display_func(af.convolve1(a, b)) - display_func(af.fft_convolve1(a, b)) - display_func(af.convolve(a, b)) - display_func(af.fft_convolve(a, b)) - - a = af.randu(5, 5) - b = af.randu(3, 3) - display_func(af.convolve2(a, b)) - display_func(af.fft_convolve2(a, b)) - display_func(af.convolve(a, b)) - display_func(af.fft_convolve(a, b)) - - c = af.convolve2NN(a, b) - display_func(c) - in_dims = c.dims() - incoming_grad = af.constant(1, in_dims[0], in_dims[1]); - g = af.convolve2GradientNN(incoming_grad, a, b, c) - display_func(g) - - a = af.randu(5, 5, 3) - b = af.randu(3, 3, 2) - display_func(af.convolve3(a, b)) - display_func(af.fft_convolve3(a, b)) - display_func(af.convolve(a, b)) - display_func(af.fft_convolve(a, b)) - - b = af.randu(3, 1) - x = af.randu(10, 1) - a = af.randu(2, 1) - display_func(af.fir(b, x)) - display_func(af.iir(b, a, x)) - - display_func(af.medfilt1(a)) - display_func(af.medfilt2(a)) - display_func(af.medfilt(a)) - - -_util.tests["signal"] = simple_signal diff --git a/tests/simple/sparse.py b/tests/simple/sparse.py deleted file mode 100644 index 49253d97d..000000000 --- a/tests/simple/sparse.py +++ /dev/null @@ -1,32 +0,0 @@ -#!/usr/bin/env python - -####################################################### -# Copyright (c) 2019, ArrayFire -# All rights reserved. -# -# This file is distributed under 3-clause BSD license. -# The complete license agreement can be obtained at: -# http://arrayfire.com/licenses/BSD-3-Clause -######################################################## - -import arrayfire as af - -from . import _util - - -def simple_sparse(verbose=False): - display_func = _util.display_func(verbose) - print_func = _util.print_func(verbose) - - dd = af.randu(5, 5) - ds = dd * (dd > 0.5) - sp = af.create_sparse_from_dense(ds) - display_func(af.sparse_get_info(sp)) - display_func(af.sparse_get_values(sp)) - display_func(af.sparse_get_row_idx(sp)) - display_func(af.sparse_get_col_idx(sp)) - print_func(af.sparse_get_nnz(sp)) - print_func(af.sparse_get_storage(sp)) - - -_util.tests["sparse"] = simple_sparse diff --git a/tests/simple/statistics.py b/tests/simple/statistics.py deleted file mode 100644 index 0da72cb0c..000000000 --- a/tests/simple/statistics.py +++ /dev/null @@ -1,67 +0,0 @@ -#!/usr/bin/env python - -####################################################### -# Copyright (c) 2019, ArrayFire -# All rights reserved. -# -# This file is distributed under 3-clause BSD license. -# The complete license agreement can be obtained at: -# http://arrayfire.com/licenses/BSD-3-Clause -######################################################## - -import arrayfire as af - -from . import _util - - -def simple_statistics(verbose=False): - display_func = _util.display_func(verbose) - print_func = _util.print_func(verbose) - - a = af.randu(5, 5) - b = af.randu(5, 5) - w = af.randu(5, 1) - - display_func(af.mean(a, dim=0)) - display_func(af.mean(a, weights=w, dim=0)) - print_func(af.mean(a)) - print_func(af.mean(a, weights=w)) - - display_func(af.var(a, dim=0)) - display_func(af.var(a, isbiased=True, dim=0)) - display_func(af.var(a, weights=w, dim=0)) - print_func(af.var(a)) - print_func(af.var(a, isbiased=True)) - print_func(af.var(a, weights=w)) - - mean, var = af.meanvar(a, dim=0) - display_func(mean) - display_func(var) - mean, var = af.meanvar(a, weights=w, bias=af.VARIANCE.SAMPLE, dim=0) - display_func(mean) - display_func(var) - - display_func(af.stdev(a, dim=0)) - print_func(af.stdev(a)) - - display_func(af.var(a, dim=0)) - display_func(af.var(a, isbiased=True, dim=0)) - print_func(af.var(a)) - print_func(af.var(a, isbiased=True)) - - display_func(af.median(a, dim=0)) - print_func(af.median(w)) - - print_func(af.corrcoef(a, b)) - - data = af.iota(5, 3) - k = 3 - dim = 0 - order = af.TOPK.DEFAULT # defaults to af.TOPK.MAX - assert dim == 0 # topk currently supports first dim only - values, indices = af.topk(data, k, dim, order) - display_func(values) - display_func(indices) - - -_util.tests["statistics"] = simple_statistics diff --git a/tests/smoke/__init__.py b/tests/smoke/__init__.py new file mode 100644 index 000000000..e69de29bb diff --git a/tests/smoke/test_algorithm.py b/tests/smoke/test_algorithm.py new file mode 100644 index 000000000..43cd5f0ec --- /dev/null +++ b/tests/smoke/test_algorithm.py @@ -0,0 +1,118 @@ +#!/usr/bin/env python + +####################################################### +# Copyright (c) 2020, ArrayFire +# All rights reserved. +# +# This file is distributed under 3-clause BSD license. +# The complete license agreement can be obtained at: +# http://arrayfire.com/licenses/BSD-3-Clause +######################################################## + +import arrayfire as af + + +def test_algorithm() -> None: + a = af.randu(3, 3) + k = af.constant(1, 3, 3, dtype=af.Dtype.u32) + af.eval(k) + + assert af.sum(a) + assert af.product(a) + assert af.min(a) + assert af.max(a) + assert af.count(a) + assert af.any_true(a) + assert af.all_true(a) + + assert af.sum(a, 0) + assert af.sum(a, 1) + + rk = af.constant(1, 3, dtype=af.Dtype.u32) + rk[2] = 0 + af.eval(rk) + assert af.sumByKey(rk, a, dim=0) + assert af.sumByKey(rk, a, dim=1) + + assert af.productByKey(rk, a, dim=0) + assert af.productByKey(rk, a, dim=1) + + assert af.minByKey(rk, a, dim=0) + assert af.minByKey(rk, a, dim=1) + + assert af.maxByKey(rk, a, dim=0) + assert af.maxByKey(rk, a, dim=1) + + assert af.anyTrueByKey(rk, a, dim=0) + assert af.anyTrueByKey(rk, a, dim=1) + + assert af.allTrueByKey(rk, a, dim=0) + assert af.allTrueByKey(rk, a, dim=1) + + assert af.countByKey(rk, a, dim=0) + assert af.countByKey(rk, a, dim=1) + + assert af.product(a, 0) + assert af.product(a, 1) + + assert af.min(a, 0) + assert af.min(a, 1) + + assert af.max(a, 0) + assert af.max(a, 1) + + assert af.count(a, 0) + assert af.count(a, 1) + + assert af.any_true(a, 0) + assert af.any_true(a, 1) + + assert af.all_true(a, 0) + assert af.all_true(a, 1) + + assert af.accum(a, 0) + assert af.accum(a, 1) + + assert af.scan(a, 0, af.BINARYOP.ADD) + assert af.scan(a, 1, af.BINARYOP.MAX) + + assert af.scan_by_key(k, a, 0, af.BINARYOP.ADD) + assert af.scan_by_key(k, a, 1, af.BINARYOP.MAX) + + assert af.sort(a, is_ascending=True) + assert af.sort(a, is_ascending=False) + + b = (a > 0.1) * a + c = (a > 0.4) * a + d = b / c + assert af.sum(d) + assert af.sum(d, nan_val=0.0) + assert af.sum(d, dim=0, nan_val=0.0) + + val, idx = af.sort_index(a, is_ascending=True) + assert val + assert idx + val, idx = af.sort_index(a, is_ascending=False) + assert val + assert idx + + b = af.randu(3, 3) + keys, vals = af.sort_by_key(a, b, is_ascending=True) + assert keys + assert vals + keys, vals = af.sort_by_key(a, b, is_ascending=False) + assert keys + assert vals + + c = af.randu(5, 1) + d = af.randu(5, 1) + cc = af.set_unique(c, is_sorted=False) + dd = af.set_unique(af.sort(d), is_sorted=True) + assert cc + assert dd + + assert af.set_union(cc, dd, is_unique=True) + assert af.set_union(cc, dd, is_unique=False) + + assert af.set_intersect(cc, cc, is_unique=True) + assert af.set_intersect(cc, cc, is_unique=False) diff --git a/tests/smoke/test_arith.py b/tests/smoke/test_arith.py new file mode 100644 index 000000000..334f82b75 --- /dev/null +++ b/tests/smoke/test_arith.py @@ -0,0 +1,213 @@ +#!/usr/bin/env python + +####################################################### +# Copyright (c) 2020, ArrayFire +# All rights reserved. +# +# This file is distributed under 3-clause BSD license. +# The complete license agreement can be obtained at: +# http://arrayfire.com/licenses/BSD-3-Clause +######################################################## + +import arrayfire as af + + +def test_simple_arith() -> None: + a = af.randu(3, 3) + b = af.constant(4, 3, 3) + assert a + assert b + + c = a + b + d = a + d += b + + assert c + assert d + assert a + 2 + assert 3 + a + + c = a - b + d = a + d -= b + + assert c + assert d + assert a - 2 + assert 3 - a + + c = a * b + d = a + d *= b + + assert c * 2 + assert 3 * d + assert a * 2 + assert 3 * a + + c = a / b + d = a + d /= b + + assert c / 2.0 + assert 3.0 / d + assert a / 2 + assert 3 / a + + c = a % b + d = a + d %= b + + assert c % 2.0 + assert 3.0 % d + assert a % 2 + assert 3 % a + + c = a ** b + d = a + d **= b + + assert c ** 2.0 + assert 3.0 ** d + assert a ** 2 + assert 3 ** a + + assert a < b + assert a < 0.5 + assert a > 0.5 + + assert a <= b + assert a <= 0.5 + assert a >= 0.5 + + assert a > b + assert a > 0.5 + assert a < 0.5 + + assert a >= b + assert a >= 0.5 + assert a <= 0.5 + + assert a != b + assert a != 0.5 + + assert a == b + assert a == 0.5 + + a = af.randu(3, 3, dtype=af.Dtype.u32) + b = af.constant(4, 3, 3, dtype=af.Dtype.u32) + + assert a & b + assert a & 2 + c = a + c &= 2 + assert c + + assert a | b + assert a | 2 + c = a + c |= 2 + assert c + + assert a >> b + assert a >> 2 + c = a + c >>= 2 + assert c + + assert a << b + assert a << 2 + c = a + c <<= 2 + assert c + + assert -a + assert +a + assert ~a + assert a + + assert af.cast(a, af.Dtype.c32) + assert af.maxof(a, b) + assert af.minof(a, b) + + assert af.clamp(a, 0, 1) + assert af.clamp(a, 0, b) + assert af.clamp(a, b, 1) + + assert af.rem(a, b) + + a = af.randu(3, 3) - 0.5 + b = af.randu(3, 3) - 0.5 + + assert af.abs(a) + assert af.arg(a) + assert af.sign(a) + assert af.round(a) + assert af.trunc(a) + assert af.floor(a) + assert af.ceil(a) + assert af.hypot(a, b) + assert af.sin(a) + assert af.cos(a) + assert af.tan(a) + assert af.asin(a) + assert af.acos(a) + assert af.atan(a) + assert af.atan2(a, b) + + c = af.cplx(a) + d = af.cplx(a, b) + assert c + assert d + assert af.real(d) + assert af.imag(d) + assert af.conjg(d) + + assert af.sinh(a) + assert af.cosh(a) + assert af.tanh(a) + assert af.asinh(a) + assert af.acosh(a) + assert af.atanh(a) + + a = af.abs(a) + b = af.abs(b) + + assert af.root(a, b) + assert af.pow(a, b) + assert af.pow2(a) + assert af.sigmoid(a) + assert af.exp(a) + assert af.expm1(a) + assert af.erf(a) + assert af.erfc(a) + assert af.log(a) + assert af.log1p(a) + assert af.log10(a) + assert af.log2(a) + assert af.sqrt(a) + assert af.rsqrt(a) + assert af.cbrt(a) + + a = af.round(5 * af.randu(3, 3) - 1) + b = af.round(5 * af.randu(3, 3) - 1) + + assert af.factorial(a) + assert af.tgamma(a) + assert af.lgamma(a) + assert af.iszero(a) + assert af.isinf(a/b) + assert af.isnan(a/a) + + a = af.randu(5, 1) + b = af.randu(1, 5) + c = af.broadcast(lambda x, y: x+y, a, b) + assert a + assert b + assert c + + @af.broadcast + def test_add(aa, bb): + return aa + bb + + assert test_add(a, b) diff --git a/tests/smoke/test_array.py b/tests/smoke/test_array.py new file mode 100644 index 000000000..cb1bc1fdf --- /dev/null +++ b/tests/smoke/test_array.py @@ -0,0 +1,97 @@ +#!/usr/bin/env python + +####################################################### +# Copyright (c) 2020, ArrayFire +# All rights reserved. +# +# This file is distributed under 3-clause BSD license. +# The complete license agreement can be obtained at: +# http://arrayfire.com/licenses/BSD-3-Clause +######################################################## + +import array as host + +import arrayfire as af + + +def test_simple_array() -> None: + a = af.array.Array([1, 2, 3]) + assert a + assert a.T + assert a.H + assert a.shape + + b = a.as_type(af.Dtype.s32) + assert b + + assert b.elements() + assert b.type() + assert b.dims() + assert b.numdims() + assert not b.is_empty() + assert not b.is_scalar() + assert b.is_column() + assert not b.is_row() + assert not b.is_complex() + assert b.is_real() + assert not b.is_double() + assert not b.is_single() + assert not b.is_real_floating() + assert not b.is_floating() + assert b.is_integer() + assert not b.is_bool() + + a = af.array.Array(host.array("i", [4, 5, 6])) + assert a + assert a.elements() + assert a.type() + assert a.dims() + assert a.numdims() + assert not a.is_empty() + assert not a.is_scalar() + assert a.is_column() + assert not a.is_row() + assert not a.is_complex() + assert a.is_real() + assert not a.is_double() + assert not a.is_single() + assert not a.is_real_floating() + assert not a.is_floating() + assert a.is_integer() + assert not a.is_bool() + + a = af.array.Array(host.array("I", [7, 8, 9] * 3), (3, 3)) + assert a + assert a.elements() + assert a.type() + assert a.dims() + assert a.numdims() + assert not a.is_empty() + assert not a.is_scalar() + assert not a.is_column() + assert not a.is_row() + assert not a.is_complex() + assert a.is_real() + assert not a.is_double() + assert not a.is_single() + assert not a.is_real_floating() + assert not a.is_floating() + assert a.is_integer() + assert not a.is_bool() + + c = a.to_ctype() + for n in range(a.elements()): + assert c[n] + + c, s = a.to_ctype(True, True) + for n in range(a.elements()): + assert c[n] + assert s + + arr = a.to_array() + lst = a.to_list(True) + + assert arr + assert lst + + assert not a.is_sparse() diff --git a/tests/simple/blas.py b/tests/smoke/test_blas.py similarity index 53% rename from tests/simple/blas.py rename to tests/smoke/test_blas.py index ac4aa6166..de47f4aec 100644 --- a/tests/simple/blas.py +++ b/tests/smoke/test_blas.py @@ -1,7 +1,7 @@ #!/usr/bin/env python ####################################################### -# Copyright (c) 2019, ArrayFire +# Copyright (c) 2020, ArrayFire # All rights reserved. # # This file is distributed under 3-clause BSD license. @@ -11,20 +11,14 @@ import arrayfire as af -from . import _util - -def simple_blas(verbose=False): - display_func = _util.display_func(verbose) +def test_simple_blas() -> None: a = af.randu(5, 5) b = af.randu(5, 5) - display_func(af.matmul(a, b)) - display_func(af.matmul(a, b, af.MATPROP.TRANS)) - display_func(af.matmul(a, b, af.MATPROP.NONE, af.MATPROP.TRANS)) + assert af.matmul(a, b) + assert af.matmul(a, b, af.MATPROP.TRANS) + assert af.matmul(a, b, af.MATPROP.NONE, af.MATPROP.TRANS) b = af.randu(5, 1) - display_func(af.dot(b, b)) - - -_util.tests["blas"] = simple_blas + assert af.dot(b, b) diff --git a/tests/smoke/test_data.py b/tests/smoke/test_data.py new file mode 100644 index 000000000..9abba2d69 --- /dev/null +++ b/tests/smoke/test_data.py @@ -0,0 +1,71 @@ +#!/usr/bin/env python + +####################################################### +# Copyright (c) 2020, ArrayFire +# All rights reserved. +# +# This file is distributed under 3-clause BSD license. +# The complete license agreement can be obtained at: +# http://arrayfire.com/licenses/BSD-3-Clause +######################################################## + +import arrayfire as af + + +def test_simple_data() -> None: + assert af.constant(100, 3, 3, dtype=af.Dtype.f32) + assert af.constant(25, 3, 3, dtype=af.Dtype.c32) + assert af.constant(2**50, 3, 3, dtype=af.Dtype.s64) + assert af.constant(2+3j, 3, 3) + assert af.constant(3+5j, 3, 3, dtype=af.Dtype.c32) + + assert af.range(3, 3) + assert af.iota(3, 3, tile_dims=(2, 2)) + + assert af.identity(3, 3, 1, 2, af.Dtype.b8) + assert af.identity(3, 3, dtype=af.Dtype.c32) + + a = af.randu(3, 4) + b = af.diag(a, extract=True) + c = af.diag(a, 1, extract=True) + + assert a + assert b + assert c + + assert af.diag(b, extract=False) + assert af.diag(c, 1, extract=False) + + assert af.join(0, a, a) + assert af.join(1, a, a, a) + + assert af.tile(a, 2, 2) + + assert af.reorder(a, 1, 0) + + assert af.shift(a, -1, 1) + + assert af.moddims(a, 6, 2) + + assert af.flat(a) + + assert af.flip(a, 0) + assert af.flip(a, 1) + + assert af.lower(a, False) + assert af.lower(a, True) + + assert af.upper(a, False) + assert af.upper(a, True) + + a = af.randu(5, 5) + assert af.transpose(a) + af.transpose_inplace(a) + assert a + + assert af.select(a > 0.3, a, -0.3) + + af.replace(a, a > 0.3, -0.3) + assert a + + assert af.pad(a, (1, 1, 0, 0), (2, 2, 0, 0)) diff --git a/tests/simple/device.py b/tests/smoke/test_device.py similarity index 66% rename from tests/simple/device.py rename to tests/smoke/test_device.py index f125ab862..cbadf86dd 100644 --- a/tests/simple/device.py +++ b/tests/smoke/test_device.py @@ -1,7 +1,7 @@ #!/usr/bin/env python ####################################################### -# Copyright (c) 2019, ArrayFire +# Copyright (c) 2020, ArrayFire # All rights reserved. # # This file is distributed under 3-clause BSD license. @@ -11,25 +11,20 @@ import arrayfire as af -from . import _util - -def simple_device(verbose=False): - display_func = _util.display_func(verbose) - print_func = _util.print_func(verbose) - print_func(af.device_info()) - print_func(af.get_device_count()) - print_func(af.is_dbl_supported()) +def test_simple_device() -> None: + assert af.device_info() + assert af.get_device_count() + assert af.is_dbl_supported() af.sync() curr_dev = af.get_device() - print_func(curr_dev) for k in range(af.get_device_count()): af.set_device(k) dev = af.get_device() assert k == dev - print_func(af.is_dbl_supported(k)) + assert af.is_dbl_supported(k) af.device_gc() @@ -44,11 +39,11 @@ def simple_device(verbose=False): af.set_device(curr_dev) a = af.randu(10, 10) - display_func(a) + assert a dev_ptr = af.get_device_ptr(a) - print_func(dev_ptr) + assert dev_ptr b = af.Array(src=dev_ptr, dims=a.dims(), dtype=a.dtype(), is_device=True) - display_func(b) + assert b c = af.randu(10, 10) af.lock_array(c) @@ -58,20 +53,17 @@ def simple_device(verbose=False): b = af.constant(2, 3, 3) af.eval(a) af.eval(b) - print_func(a) - print_func(b) + assert a + assert b c = a + b d = a - b af.eval(c, d) - print_func(c) - print_func(d) + assert c + assert d - print_func(af.set_manual_eval_flag(True)) + assert not af.set_manual_eval_flag(True) assert af.get_manual_eval_flag() - print_func(af.set_manual_eval_flag(False)) + assert not af.set_manual_eval_flag(False) assert not af.get_manual_eval_flag() - display_func(af.is_locked_array(a)) - - -_util.tests["device"] = simple_device + assert not af.is_locked_array(a) diff --git a/tests/smoke/test_image.py b/tests/smoke/test_image.py new file mode 100644 index 000000000..b9f2834ca --- /dev/null +++ b/tests/smoke/test_image.py @@ -0,0 +1,93 @@ +#!/usr/bin/env python + +####################################################### +# Copyright (c) 2020, ArrayFire +# All rights reserved. +# +# This file is distributed under 3-clause BSD license. +# The complete license agreement can be obtained at: +# http://arrayfire.com/licenses/BSD-3-Clause +######################################################## + +import arrayfire as af + + +def test_simple_image() -> None: + a = 10 * af.randu(6, 6) + a3 = 10 * af.randu(5, 5, 3) + + dx, dy = af.gradient(a) + assert dx + assert dy + + assert af.resize(a, scale=0.5) + assert af.resize(a, odim0=8, odim1=8) + + t = af.randu(3, 2) + assert af.transform(a, t) + assert af.rotate(a, 3.14) + assert af.translate(a, 1, 1) + assert af.scale(a, 1.2, 1.2, 7, 7) + assert af.skew(a, 0.02, 0.02) + h = af.histogram(a, 3) + assert h + assert af.hist_equal(a, h) + + assert af.dilate(a) + assert af.erode(a) + + assert af.dilate3(a3) + assert af.erode3(a3) + + assert af.bilateral(a, 1, 2) + assert af.mean_shift(a, 1, 2, 3) + + assert af.medfilt(a) + assert af.minfilt(a) + assert af.maxfilt(a) + + assert af.regions(af.round(a) > 3) + assert af.confidenceCC( + af.randu(10, 10), (af.randu(2) * 9).as_type(af.Dtype.u32), (af.randu(2) * 9).as_type(af.Dtype.u32), + 3, 3, 10, 0.1) + + dx, dy = af.sobel_derivatives(a) + assert dx + assert dy + assert af.sobel_filter(a) + assert af.gaussian_kernel(3, 3) + assert af.gaussian_kernel(3, 3, 1, 1) + + ac = af.gray2rgb(a) + assert ac + assert af.rgb2gray(ac) + ah = af.rgb2hsv(ac) + assert ah + assert af.hsv2rgb(ah) + + assert af.color_space(a, af.CSPACE.RGB, af.CSPACE.GRAY) + + a = af.randu(6, 6) + b = af.unwrap(a, 2, 2, 2, 2) + c = af.wrap(b, 6, 6, 2, 2, 2, 2) + assert a + assert b + assert c + assert af.sat(a) + + a = af.randu(10, 10, 3) + assert af.rgb2ycbcr(a) + assert af.ycbcr2rgb(a) + + a = af.randu(10, 10) + b = af.canny(a, low_threshold=0.2, high_threshold=0.8) + + # FIXME: OpenCL Error (-11): Build Program Failure when calling clBuildProgram + # assert af.anisotropic_diffusion(a, 0.125, 1.0, 64, af.FLUX.QUADRATIC, af.DIFFUSION.GRAD) + + a = af.randu(10, 10) + psf = af.gaussian_kernel(3, 3) + cimg = af.convolve(a, psf) + assert af.iterativeDeconv(cimg, psf, 100, 0.5, af.ITERATIVE_DECONV.LANDWEBER) + assert af.iterativeDeconv(cimg, psf, 100, 0.5, af.ITERATIVE_DECONV.RICHARDSONLUCY) + assert af.inverseDeconv(cimg, psf, 1.0, af.INVERSE_DECONV.TIKHONOV) diff --git a/tests/simple/index.py b/tests/smoke/test_index.py similarity index 56% rename from tests/simple/index.py rename to tests/smoke/test_index.py index 1cd6229f2..604184bb5 100644 --- a/tests/simple/index.py +++ b/tests/smoke/test_index.py @@ -1,7 +1,7 @@ #!/usr/bin/env python ####################################################### -# Copyright (c) 2019, ArrayFire +# Copyright (c) 2020, ArrayFire # All rights reserved. # # This file is distributed under 3-clause BSD license. @@ -13,72 +13,65 @@ import arrayfire as af -from . import _util - -def simple_index(verbose=False): - display_func = _util.display_func(verbose) +def test_simple_index() -> None: a = af.randu(5, 5) - display_func(a) + assert a b = af.Array(a) - display_func(b) + assert b c = a.copy() - display_func(c) - display_func(a[0, 0]) - display_func(a[0]) - display_func(a[:]) - display_func(a[:, :]) - display_func(a[0:3, ]) - display_func(a[-2:-1, -1]) - display_func(a[0:5]) - display_func(a[0:5:2]) + assert c + assert a[0, 0] + assert a[0] + assert a[:] + assert a[:, :] + assert a[0:3, ] + assert a[-2:-1, -1] + assert a[0:5] + assert a[0:5:2] idx = af.Array(host.array("i", [0, 3, 2])) - display_func(idx) + assert idx aa = a[idx] - display_func(aa) + assert aa a[0] = 1 - display_func(a) + assert a a[0] = af.randu(1, 5) - display_func(a) + assert a a[:] = af.randu(5, 5) - display_func(a) + assert a a[:, -1] = af.randu(5, 1) - display_func(a) + assert a a[0:5:2] = af.randu(3, 5) - display_func(a) + assert a a[idx, idx] = af.randu(3, 3) - display_func(a) + assert a a = af.randu(5, 1) b = af.randu(5, 1) - display_func(a) - display_func(b) + assert a + assert b for ii in af.ParallelRange(1, 3): a[ii] = b[ii] - display_func(a) + assert a for ii in af.ParallelRange(2, 5): b[ii] = 2 - display_func(b) + assert b a = af.randu(3, 2) rows = af.constant(0, 1, dtype=af.Dtype.s32) b = a[:, rows] - display_func(b) + assert b for r in range(rows.elements()): - display_func(r) - display_func(b[:, r]) + assert b[:, r] a = af.randu(3) c = af.randu(3) b = af.constant(1, 3, dtype=af.Dtype.b8) - display_func(a) + assert a a[b] = c - display_func(a) - - -_util.tests["index"] = simple_index + assert a diff --git a/tests/simple/interop.py b/tests/smoke/test_interop.py similarity index 96% rename from tests/simple/interop.py rename to tests/smoke/test_interop.py index 75515f611..c6418f6a0 100644 --- a/tests/simple/interop.py +++ b/tests/smoke/test_interop.py @@ -1,7 +1,7 @@ #!/usr/bin/env python ####################################################### -# Copyright (c) 2019, ArrayFire +# Copyright (c) 2020, ArrayFire # All rights reserved. # # This file is distributed under 3-clause BSD license. @@ -11,12 +11,10 @@ import arrayfire as af -from . import _util - # BUG: module 'arrayfire' has no 'to_array' member. -def simple_interop(*args): +def test_simple_interop(*args): if af.AF_NUMPY_FOUND: import numpy as np n = np.random.random((5,)) @@ -137,6 +135,3 @@ def simple_interop(*args): a = af.to_array(c) n2 = np.array(a) assert (n == n2).all() - - -_util.tests["interop"] = simple_interop diff --git a/tests/smoke/test_lapack.py b/tests/smoke/test_lapack.py new file mode 100644 index 000000000..a6dc585f5 --- /dev/null +++ b/tests/smoke/test_lapack.py @@ -0,0 +1,84 @@ +#!/usr/bin/env python + +####################################################### +# Copyright (c) 2020, ArrayFire +# All rights reserved. +# +# This file is distributed under 3-clause BSD license. +# The complete license agreement can be obtained at: +# http://arrayfire.com/licenses/BSD-3-Clause +######################################################## + +import arrayfire as af + + +def test_simple_lapack() -> None: + a = af.randu(5, 5) + + l, u, p = af.lu(a) + + assert l + assert u + assert p + + p = af.lu_inplace(a, "full") + + assert a + assert p + + a = af.randu(5, 3) + + q, r, t = af.qr(a) + + assert q + assert r + assert t + + af.qr_inplace(a) + + assert a + + a = af.randu(5, 5) + a = af.matmulTN(a, a.copy()) + 10 * af.identity(5, 5) + + R, info = af.cholesky(a) + assert R + + af.cholesky_inplace(a) + assert a + + a = af.randu(5, 5) + ai = af.inverse(a) + + assert a + assert ai + + ai = af.pinverse(a) + assert ai + + x0 = af.randu(5, 3) + b = af.matmul(a, x0) + x1 = af.solve(a, b) + + assert x0 + assert x1 + + p = af.lu_inplace(a) + + x2 = af.solve_lu(a, p, b) + + assert x2 + + assert af.rank(a) + assert af.det(a) + assert af.norm(a, af.NORM.EUCLID) + assert af.norm(a, af.NORM.MATRIX_1) + assert af.norm(a, af.NORM.MATRIX_INF) + assert af.norm(a, af.NORM.MATRIX_L_PQ, 1, 1) + + a = af.randu(10, 10) + assert a + u, s, vt = af.svd(a) + assert af.matmul(af.matmul(u, af.diag(s, 0, False)), vt) + u, s, vt = af.svd_inplace(a) + assert af.matmul(af.matmul(u, af.diag(s, 0, False)), vt) diff --git a/tests/smoke/test_random.py b/tests/smoke/test_random.py new file mode 100644 index 000000000..1a26a25a8 --- /dev/null +++ b/tests/smoke/test_random.py @@ -0,0 +1,34 @@ +#!/usr/bin/env python + +####################################################### +# Copyright (c) 2020, ArrayFire +# All rights reserved. +# +# This file is distributed under 3-clause BSD license. +# The complete license agreement can be obtained at: +# http://arrayfire.com/licenses/BSD-3-Clause +######################################################## + +import arrayfire as af + + +def test_simple_random() -> None: + assert af.randu(3, 3, 1, 2) + assert af.randu(3, 3, 1, 2, af.Dtype.b8) + assert af.randu(3, 3, dtype=af.Dtype.c32) + + assert af.randn(3, 3, 1, 2) + assert af.randn(3, 3, dtype=af.Dtype.c32) + + af.set_seed(1024) + assert af.get_seed() == 1024 + + engine = af.Random_Engine(af.RANDOM_ENGINE.MERSENNE_GP11213, 100) + + assert af.randu(3, 3, 1, 2, engine=engine) + assert af.randu(3, 3, 1, 2, af.Dtype.s32, engine=engine) + assert af.randu(3, 3, dtype=af.Dtype.c32, engine=engine) + + assert af.randn(3, 3, engine=engine) + engine.set_seed(100) + assert engine.get_seed() == 100 diff --git a/tests/smoke/test_signal.py b/tests/smoke/test_signal.py new file mode 100644 index 000000000..90f1438b2 --- /dev/null +++ b/tests/smoke/test_signal.py @@ -0,0 +1,122 @@ +#!/usr/bin/env python + +####################################################### +# Copyright (c) 2020, ArrayFire +# All rights reserved. +# +# This file is distributed under 3-clause BSD license. +# The complete license agreement can be obtained at: +# http://arrayfire.com/licenses/BSD-3-Clause +######################################################## + +import arrayfire as af + + +def test_simple_signal() -> None: + signal = af.randu(10) + x_new = af.randu(10) + x_orig = af.randu(10) + assert af.approx1(signal, x_new, xp=x_orig) + + signal = af.randu(3, 3) + x_new = af.randu(3, 3) + x_orig = af.randu(3, 3) + y_new = af.randu(3, 3) + y_orig = af.randu(3, 3) + + assert af.approx2(signal, x_new, y_new, xp=x_orig, yp=y_orig) + + a = af.randu(8, 1) + assert a + + assert af.fft(a) + assert af.dft(a) + assert af.real(af.ifft(af.fft(a))) + assert af.real(af.idft(af.dft(a))) + + b = af.fft(a) + af.ifft_inplace(b) + assert b + af.fft_inplace(b) + assert b + + b = af.fft_r2c(a) + c = af.fft_c2r(b) + assert b + assert c + + a = af.randu(4, 4) + assert a + + assert af.fft2(a) + assert af.dft(a) + assert af.real(af.ifft2(af.fft2(a))) + assert af.real(af.idft(af.dft(a))) + + b = af.fft2(a) + af.ifft2_inplace(b) + assert b + af.fft2_inplace(b) + assert b + + b = af.fft2_r2c(a) + c = af.fft2_c2r(b) + assert b + assert c + + a = af.randu(4, 4, 2) + assert a + + assert af.fft3(a) + assert af.dft(a) + assert af.real(af.ifft3(af.fft3(a))) + assert af.real(af.idft(af.dft(a))) + + b = af.fft3(a) + af.ifft3_inplace(b) + assert b + af.fft3_inplace(b) + assert b + + b = af.fft3_r2c(a) + c = af.fft3_c2r(b) + assert b + assert c + + a = af.randu(10, 1) + b = af.randu(3, 1) + assert af.convolve1(a, b) + assert af.fft_convolve1(a, b) + assert af.convolve(a, b) + assert af.fft_convolve(a, b) + + a = af.randu(5, 5) + b = af.randu(3, 3) + assert af.convolve2(a, b) + assert af.fft_convolve2(a, b) + assert af.convolve(a, b) + assert af.fft_convolve(a, b) + + c = af.convolve2NN(a, b) + assert c + in_dims = c.dims() + incoming_grad = af.constant(1, in_dims[0], in_dims[1]); + g = af.convolve2GradientNN(incoming_grad, a, b, c) + assert g + + a = af.randu(5, 5, 3) + b = af.randu(3, 3, 2) + assert af.convolve3(a, b) + assert af.fft_convolve3(a, b) + assert af.convolve(a, b) + assert af.fft_convolve(a, b) + + b = af.randu(3, 1) + x = af.randu(10, 1) + a = af.randu(2, 1) + assert af.fir(b, x) + assert af.iir(b, a, x) + + assert af.medfilt1(a) + assert af.medfilt2(a) + assert af.medfilt(a) diff --git a/tests/smoke/test_sparse.py b/tests/smoke/test_sparse.py new file mode 100644 index 000000000..8efeee64a --- /dev/null +++ b/tests/smoke/test_sparse.py @@ -0,0 +1,24 @@ +#!/usr/bin/env python + +####################################################### +# Copyright (c) 2020, ArrayFire +# All rights reserved. +# +# This file is distributed under 3-clause BSD license. +# The complete license agreement can be obtained at: +# http://arrayfire.com/licenses/BSD-3-Clause +######################################################## + +import arrayfire as af + + +def test_simple_sparse() -> None: + dd = af.randu(5, 5) + ds = dd * (dd > 0.5) + sp = af.create_sparse_from_dense(ds) + assert af.sparse_get_info(sp) + assert af.sparse_get_values(sp) + assert af.sparse_get_row_idx(sp) + assert af.sparse_get_col_idx(sp) + assert af.sparse_get_nnz(sp) + assert af.sparse_get_storage(sp) diff --git a/tests/smoke/test_statistics.py b/tests/smoke/test_statistics.py new file mode 100644 index 000000000..e26b8965a --- /dev/null +++ b/tests/smoke/test_statistics.py @@ -0,0 +1,59 @@ +#!/usr/bin/env python + +####################################################### +# Copyright (c) 2020, ArrayFire +# All rights reserved. +# +# This file is distributed under 3-clause BSD license. +# The complete license agreement can be obtained at: +# http://arrayfire.com/licenses/BSD-3-Clause +######################################################## + +import arrayfire as af + + +def test_simple_statistics() -> None: + a = af.randu(5, 5) + b = af.randu(5, 5) + w = af.randu(5, 1) + + assert af.mean(a, dim=0) + assert af.mean(a, weights=w, dim=0) + assert af.mean(a) + assert af.mean(a, weights=w) + + assert af.var(a, dim=0) + assert af.var(a, isbiased=True, dim=0) + assert af.var(a, weights=w, dim=0) + assert af.var(a) + assert af.var(a, isbiased=True) + assert af.var(a, weights=w) + + mean, var = af.meanvar(a, dim=0) + assert mean + assert var + mean, var = af.meanvar(a, weights=w, bias=af.VARIANCE.SAMPLE, dim=0) + assert mean + assert var + + assert af.stdev(a, dim=0) + assert af.stdev(a) + + assert af.var(a, dim=0) + assert af.var(a, isbiased=True, dim=0) + assert af.var(a) + assert af.var(a, isbiased=True) + + assert af.median(a, dim=0) + assert af.median(w) + + assert af.corrcoef(a, b) + + data = af.iota(5, 3) + k = 3 + dim = 0 + order = af.TOPK.DEFAULT # defaults to af.TOPK.MAX + assert dim == 0 # topk currently supports first dim only + values, indices = af.topk(data, k, dim, order) + assert values + assert indices