Browse code

correct test for shape errors

This test was previously incorrect, but because we didn't actually
check the number of orbitals on constant values on finalization
this was not caught.

Joseph Weston authored on 11/12/2019 21:59:13
Showing 1 changed files
... ...
@@ -75,19 +75,18 @@ def test_hamiltonian_submatrix(vectorize):
75 75
     raises(ValueError, kwant.solvers.default.greens_function, syst3, 0.2)
76 76
 
77 77
     # Test for shape errors.
78
-    syst[chain2(0), chain(2)] = np.array([[1, 2]])
79
-    syst2 = syst.finalized()
80
-    raises(ValueError, syst2.hamiltonian_submatrix)
81
-    raises(ValueError, syst2.hamiltonian_submatrix, sparse=True)
82
-    syst[chain2(0), chain(2)] = 1
83
-    syst2 = syst.finalized()
84
-    raises(ValueError, syst2.hamiltonian_submatrix)
85
-    raises(ValueError, syst2.hamiltonian_submatrix, sparse=True)
86
-    if vectorize:  # non-vectorized systems don't check this at finalization
87
-        # Add another hopping of the same type but with a different
88
-        # (and still incompatible) shape.
89
-        syst[chain2(0), chain(1)] = np.array([[1, 2]])
90
-        raises(ValueError, syst.finalized)
78
+    badly_shaped_hoppings = [
79
+        1,
80
+        [[1, 2]],  # shape (1, 2) instead of (2, 1)
81
+        lambda a, b: 1,
82
+        lambda a, b: [[1, 2]],
83
+    ]
84
+    for hopping in badly_shaped_hoppings:
85
+        syst[chain2(0), chain(2)] = hopping
86
+        with raises(ValueError):
87
+            syst.finalized().hamiltonian_submatrix(sparse=False)
88
+        with raises(ValueError):
89
+            syst.finalized().hamiltonian_submatrix(sparse=True)
91 90
 
92 91
 
93 92
 @pytest.mark.parametrize("vectorize", [False, True])