Skip to content

Commit

Permalink
creating new file for tests
Browse files Browse the repository at this point in the history
  • Loading branch information
William Zijie Zhang authored and William Zijie Zhang committed Aug 7, 2024
1 parent ad53ec7 commit 652e453
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 29 deletions.
30 changes: 30 additions & 0 deletions cvxpy/tests/test_attributes.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import numpy as np

import cvxpy as cp
from cvxpy.constraints.psd import PSD
from cvxpy.reductions.cvx_attr2constr import CvxAttr2Constr


class Test_Attributes():

def test_multiple_attributes(self) -> None:
x = cp.Variable(shape=(2,2), symmetric=True, nonneg=True, integer=True)
target = np.array(np.eye(2) * 5)
prob = cp.Problem(cp.Minimize(0), [x == target])
prob.solve()
assert np.allclose(x.value, target)

def test_nonneg_PSD(self) -> None:
x = cp.Variable(shape=(2,2), PSD=True, nonneg=True)
target = np.array(np.eye(2) * 5)
prob = cp.Problem(cp.Minimize(0), [x == target])
prob.solve()
assert np.allclose(x.value, target)

def test_nonneg_NSD(self) -> None:
x = cp.Variable(shape=(2,2), NSD=True, nonpos=True)
target = np.array(np.eye(2) * 5)
prob = cp.Problem(cp.Minimize(0), [x == -target])

new_prob = CvxAttr2Constr(prob)
assert type(new_prob.apply(prob)[0].constraints[0]) is PSD
29 changes: 0 additions & 29 deletions cvxpy/tests/test_expressions.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
"""

import warnings
from itertools import combinations

import numpy as np
import pytest
Expand Down Expand Up @@ -1610,31 +1609,3 @@ def is_zero_dim_output(axis):
prob = cp.Problem(self.obj, [expr == y])
prob.solve(canon_backend=cp.SCIPY_CANON_BACKEND)
assert np.allclose(expr.value, y)

class Test_Attributes():

def test_multiple_attributes(self) -> None:
x = cp.Variable(shape=(2,2), symmetric=True, nonneg=True, integer=True, boolean=True)
target = np.array(np.eye(2))
prob = cp.Problem(cp.Minimize(0), [x == target])
prob.solve()
assert np.allclose(x.value, target)

# Define the attributes to test
attributes = [
'nonneg', 'pos', 'nonpos', 'neg', 'diag', 'PSD', 'NSD', 'imag', 'boolean', 'integer'
]

# Generate combinations of attributes
combinations_of_attributes = []
for r in range(1, len(attributes) + 1):
combinations_of_attributes.extend(combinations(attributes, r))

@pytest.mark.parametrize("attributes", combinations_of_attributes)
def test_variable_combinations(self, attributes):
kwargs = {attr: True for attr in attributes}
var = cp.Variable(shape=(2, 2), **kwargs)
target = np.array([[0, 1], [1, 0]])
prob = cp.Problem(cp.Minimize(0), [var == target])
prob.solve()
assert np.allclose(var.value, target)

0 comments on commit 652e453

Please sign in to comment.