Main Content

matlab.unittest.constraints.BooleanConstraint 클래스

네임스페이스: matlab.unittest.constraints
슈퍼클래스: matlab.unittest.constraints.Constraint

부울 연산을 지원하는 제약 조건에 대한 기본 인터페이스

설명

matlab.unittest.constraints.BooleanConstraint 클래스는 and(&), or(|) 및 not(~) 연산자로 제약 조건을 조합하고 부정(Negate)할 수 있는 인터페이스를 제공합니다.

부울 연산을 지원하는 사용자 지정 제약 조건 클래스를 만들려면 matlab.unittest.constraints.BooleanConstraint에서 클래스를 파생하고 다음과 같은 필수 추상 메서드를 구현하십시오.

  • 비교 논리를 코드로 작성한 satisfiedBy 메서드를 구현합니다. BooleanConstraint 클래스는 matlab.unittest.constraints.Constraint로부터 이 메서드를 상속합니다.

  • 테스트 프레임워크가 실제 값을 제약 조건과 비교하여 평가할 때 진단 정보를 생성하도록 getDiagnosticFor 메서드를 구현합니다. BooleanConstraint 클래스는 Constraint 클래스로부터 이 메서드를 상속합니다.

  • 프레임워크가 실제 값을 부정(Negate)된 제약 조건과 비교하여 평가할 때 진단 정보를 생성하도록 getNegativeDiagnosticFor 메서드를 구현합니다. 제약 조건이 부정되는 경우, 표준적인 사용 방식(즉, 부정이 아닌 방식)과는 다른 형식의 진단을 작성해야 합니다.

BooleanConstraint 클래스는 Constraint 클래스에서 파생되므로, BooleanConstraint 서브클래스는 Constraint 서브클래스가 제공하는 기능을 지원합니다. 예를 들어, 이러한 서브클래스를 matlab.unittest.qualifications 네임스페이스의 assertThat, assumeThat, fatalAssertThat, verifyThat 가설 검정 메서드와 함께 사용할 수 있습니다. 또한 BooleanConstraint 객체를 부정하거나 다른 BooleanConstraint 객체와 조합할 수 있습니다.

클래스 특성

Abstract
true

클래스 특성에 대한 자세한 내용은 클래스 특성 항목을 참조하십시오.

메서드

모두 확장

예제

모두 축소

HasElementCountHasLength와 같은 몇몇 내장된 제약 조건은 BooleanConstraint의 서브클래스입니다. 테스트에서 이러한 제약 조건을 조합하고 부정(Negate)할 수 있습니다.

먼저 이 예제에서 사용되는 클래스를 가져옵니다.

import matlab.unittest.TestCase
import matlab.unittest.constraints.HasElementCount
import matlab.unittest.constraints.HasLength
import matlab.unittest.constraints.HasInf
import matlab.unittest.constraints.HasNaN
import matlab.unittest.constraints.IsEqualTo
import matlab.unittest.constraints.IsGreaterThanOrEqualTo
import matlab.unittest.constraints.IsReal

대화형 방식 테스트를 위한 테스트 케이스를 생성합니다.

testCase = TestCase.forInteractiveUse;

3이 실수이고, 3보다 크거나 같은지 확인합니다

testCase.verifyThat(3,IsReal & IsGreaterThanOrEqualTo(3))
Verification passed.

34와 같지 않은지 확인합니다.

testCase.verifyThat(3,~IsEqualTo(4))
Verification passed.

행렬 [1 2 3; 4 5 6]의 길이가 6이거나, 6개의 요소를 갖는지 확인합니다. or 조건 중 하나가 true이므로 이 테스트 결과는 통과입니다.

testCase.verifyThat([1 2 3; 4 5 6],HasLength(6) | HasElementCount(6))
Verification passed.

벡터 [3 NaN 5]NaN 값과 무한대 값을 갖는지 테스트합니다. 이 테스트는 and 조건 중 하나가 false이므로 실패합니다.

testCase.verifyThat([3 NaN 5],HasNaN & HasInf)
Verification failed.
    ---------------------
    Framework Diagnostic:
    ---------------------
    AndConstraint failed.
    --> + [First Condition]:
         |   HasNaN passed.
         |   --> Indices that have NaN values:
         |           2
         |   
         |   Actual Value:
         |        3   NaN     5
    --> AND
        + [Second Condition]:
         |   HasInf failed.
         |   --> At least one element must be Inf or -Inf.
         |   
         |   Actual Value:
         |        3   NaN     5
        -+---------------------

값이 예상 값과 동일한 크기인지를 확인하는 부울 제약 조건을 만듭니다.

현재 폴더에 있는 파일에서 matlab.unittest.constraints.BooleanConstraint에서 파생된 IsSameSizeAs라는 클래스를 만들고 satisfiedBy, getDiagnosticForgetNegativeDiagnosticFor 메서드를 구현합니다.

classdef IsSameSizeAs < matlab.unittest.constraints.BooleanConstraint
    properties (SetAccess=immutable)
        ValueWithExpectedSize
    end

    methods
        % Class constructor
        function constraint = IsSameSizeAs(value)
            constraint.ValueWithExpectedSize = value;
        end

        % Determine if the actual value satisfies the constraint
        function tf = satisfiedBy(constraint,actual)
            tf = constraint.sizeMatchesExpected(actual);
        end

        % Produce a diagnostic for the constraint
        function diagnostic = getDiagnosticFor(constraint,actual)
            import matlab.automation.diagnostics.StringDiagnostic
            if constraint.sizeMatchesExpected(actual)
                diagnostic = StringDiagnostic("IsSameSizeAs passed.");
            else
                diagnostic = StringDiagnostic( ...
                    "IsSameSizeAs failed." + newline + "Actual Size: [" ...
                    + int2str(size(actual)) + "]" + newline ...
                    + "Expected Size: [" ...
                    + int2str(size(constraint.ValueWithExpectedSize)) ...
                    + "]");
            end
        end
    end

    methods (Access=protected)
        % Produce a diagnostic for the negated constraint
        function diagnostic = getNegativeDiagnosticFor(constraint,actual)
            import matlab.automation.diagnostics.StringDiagnostic
            if constraint.sizeMatchesExpected(actual)
                diagnostic = StringDiagnostic( ...
                    "Negated IsSameSizeAs failed." + newline + ...
                    "Actual and expected sizes were the same ([" ...
                    + int2str(size(actual)) + ...
                    "]) but should not have been.");
            else
                diagnostic = StringDiagnostic( ...
                    "Negated IsSameSizeAs passed.");
            end
        end
    end

    methods (Access=private)
        % Determine if the actual and expected values are the same size
        function tf = sizeMatchesExpected(constraint,actual)
            tf = isequal(size(actual), ...
                size(constraint.ValueWithExpectedSize));
        end
    end
end

대화형 방식 테스트를 위한 테스트 케이스를 생성합니다.

testCase = matlab.unittest.TestCase.forInteractiveUse;

IsSameSizeAs 부울 제약 조건을 사용하여 0으로 구성된 5×5 행렬이 1로 구성된 5×5 행렬과는 크기가 같고 1로 구성된 1×5 벡터와는 크기가 같지 않다는 점을 확인합니다.

testCase.verifyThat(zeros(5), ...
    IsSameSizeAs(ones(5)) & ~IsSameSizeAs(ones(1,5)))
Verification passed.

버전 내역

R2013a에 개발됨