Browse code

factor a loop out of a test

Joseph Weston authored on 16/03/2017 14:17:41
Showing 1 changed files
... ...
@@ -1,4 +1,4 @@
1
-# Copyright 2016 Kwant authors.
1
+# Copyright 2011-2017 Kwant authors.
2 2
 #
3 3
 # This file is part of Kwant.  It is subject to the license terms in the file
4 4
 # LICENSE.rst found in the top-level directory of this distribution and at
... ...
@@ -12,6 +12,7 @@ import numpy as np
12 12
 import tinyarray as ta
13 13
 import numpy.linalg as la
14 14
 from scipy.sparse import coo_matrix
15
+import pytest
15 16
 from pytest import raises
16 17
 # needed to get round odd bug in test_mask_interpolate
17 18
 from contextlib import contextmanager
... ...
@@ -406,7 +407,8 @@ def test_tocoo():
406 407
     raises(ValueError, op.tocoo, [1])
407 408
 
408 409
 
409
-def test_arg_passing():
410
+@pytest.mark.parametrize("A", opservables)
411
+def test_arg_passing(A):
410 412
     lat1 = kwant.lattice.chain(norbs=1)
411 413
 
412 414
     syst = kwant.Builder()
... ...
@@ -416,71 +418,70 @@ def test_arg_passing():
416 418
 
417 419
     wf = np.ones(len(fsyst.sites))
418 420
 
419
-    for A in opservables:
420
-        op = A(fsyst)
421
-        canonical_args = (1, 2)
422
-        params = dict(a=1, b=2)
423
-        call_should_be = op(wf, args=canonical_args)
424
-        act_should_be = op.act(wf, args=canonical_args)
425
-        has_tocoo = hasattr(op, 'tocoo')
426
-        if has_tocoo:
427
-            tocoo_should_be = op.tocoo(args=canonical_args).todense()
428
-
429
-        with raises(TypeError) as exc:
430
-            op(wf, args=canonical_args, params=params)
431
-        assert 'mutually exclusive' in str(exc)
432
-        with raises(TypeError) as exc:
433
-            op.act(wf, args=canonical_args, params=params)
434
-        assert 'mutually exclusive' in str(exc)
421
+    op = A(fsyst)
422
+    canonical_args = (1, 2)
423
+    params = dict(a=1, b=2)
424
+    call_should_be = op(wf, args=canonical_args)
425
+    act_should_be = op.act(wf, args=canonical_args)
426
+    has_tocoo = hasattr(op, 'tocoo')
427
+    if has_tocoo:
428
+        tocoo_should_be = op.tocoo(args=canonical_args).todense()
429
+
430
+    with raises(TypeError) as exc:
431
+        op(wf, args=canonical_args, params=params)
432
+    assert 'mutually exclusive' in str(exc)
433
+    with raises(TypeError) as exc:
434
+        op.act(wf, args=canonical_args, params=params)
435
+    assert 'mutually exclusive' in str(exc)
436
+    with raises(TypeError) as exc:
437
+        op.bind(args=canonical_args, params=params)
438
+    assert 'mutually exclusive' in str(exc)
439
+    if has_tocoo:
435 440
         with raises(TypeError) as exc:
436
-            op.bind(args=canonical_args, params=params)
441
+            op.tocoo(args=canonical_args, params=params)
437 442
         assert 'mutually exclusive' in str(exc)
438
-        if has_tocoo:
439
-            with raises(TypeError) as exc:
440
-                op.tocoo(args=canonical_args, params=params)
441
-            assert 'mutually exclusive' in str(exc)
442
-
443
-        np.testing.assert_array_equal(
444
-            call_should_be, op(wf, params=params))
445
-        np.testing.assert_array_equal(
446
-            act_should_be, op.act(wf, params=params))
447
-        if has_tocoo:
448
-            np.testing.assert_array_equal(
449
-                tocoo_should_be, op.tocoo(params=params).todense())
450
-        # after binding
451
-        op2 = op.bind(params=params)
452
-        np.testing.assert_array_equal(
453
-            call_should_be, op2(wf))
454
-        np.testing.assert_array_equal(
455
-            act_should_be, op2.act(wf))
456
-        if has_tocoo:
457
-            np.testing.assert_array_equal(
458
-                tocoo_should_be, op2.tocoo().todense())
459
-
460
-        # system and onsite having different args
461
-        def onsite(site, flip):
462
-            return -1 if flip else 1
463
-
464
-        op = A(fsyst, onsite=onsite)
465
-        params['flip'] = True
466
-        call_should_be = -call_should_be
467
-        act_should_be = -act_should_be
468
-        if has_tocoo:
469
-            tocoo_should_be = -tocoo_should_be
470 443
 
444
+    np.testing.assert_array_equal(
445
+        call_should_be, op(wf, params=params))
446
+    np.testing.assert_array_equal(
447
+        act_should_be, op.act(wf, params=params))
448
+    if has_tocoo:
471 449
         np.testing.assert_array_equal(
472
-            call_should_be, op(wf, params=params))
450
+            tocoo_should_be, op.tocoo(params=params).todense())
451
+    # after binding
452
+    op2 = op.bind(params=params)
453
+    np.testing.assert_array_equal(
454
+        call_should_be, op2(wf))
455
+    np.testing.assert_array_equal(
456
+        act_should_be, op2.act(wf))
457
+    if has_tocoo:
473 458
         np.testing.assert_array_equal(
474
-            act_should_be, op.act(wf, params=params))
475
-        if has_tocoo:
476
-            np.testing.assert_array_equal(
477
-                tocoo_should_be, op.tocoo(params=params).todense())
478
-        # after binding
479
-        op2 = op.bind(params=params)
459
+            tocoo_should_be, op2.tocoo().todense())
460
+
461
+    # system and onsite having different args
462
+    def onsite(site, flip):
463
+        return -1 if flip else 1
464
+
465
+    op = A(fsyst, onsite=onsite)
466
+    params['flip'] = True
467
+    call_should_be = -call_should_be
468
+    act_should_be = -act_should_be
469
+    if has_tocoo:
470
+        tocoo_should_be = -tocoo_should_be
471
+
472
+    np.testing.assert_array_equal(
473
+        call_should_be, op(wf, params=params))
474
+    np.testing.assert_array_equal(
475
+        act_should_be, op.act(wf, params=params))
476
+    if has_tocoo:
480 477
         np.testing.assert_array_equal(
481
-            call_should_be, op2(wf))
478
+            tocoo_should_be, op.tocoo(params=params).todense())
479
+    # after binding
480
+    op2 = op.bind(params=params)
481
+    np.testing.assert_array_equal(
482
+        call_should_be, op2(wf))
483
+    np.testing.assert_array_equal(
484
+        act_should_be, op2.act(wf))
485
+    if has_tocoo:
482 486
         np.testing.assert_array_equal(
483
-            act_should_be, op2.act(wf))
484
-        if has_tocoo:
485
-            np.testing.assert_array_equal(
486
-                tocoo_should_be, op2.tocoo().todense())
487
+            tocoo_should_be, op2.tocoo().todense())