Main Content

matlab.unittest.constraints.StructComparator 클래스

네임스페이스: matlab.unittest.constraints

구조체형 배열의 비교 연산자

설명

matlab.unittest.constraints.StructComparator 클래스는 구조체형 배열의 비교 연산자를 제공합니다. 테스트에 이 비교 연산자를 사용하려면 StructComparator 인스턴스를 만들고 IsEqualTo 제약 조건 생성자의 Using 이름-값 인수의 값으로 지정합니다.

생성

설명

예제

c = matlab.unittest.constraints.StructComparator는 빈 구조체형 배열과 필드가 없는 구조체형 배열의 비교 연산자를 만듭니다. 이 비교 연산자는 실제 값과 예상 값이 크기와 필드가 동일한 구조체형 배열인 경우에 충족됩니다.

예제

c = matlab.unittest.constraints.StructComparator(comp)는 지정된 비교 연산자 comp를 사용하여 구조체형 배열에 포함된 값을 비교합니다. 이 구문을 사용하면 이 비교 연산자는 실제 값과 예상 값이 크기와 필드가 동일한 구조체형 배열이고 해당 필드의 값이 comp 비교 연산자를 만족할 경우에 충족됩니다.

예제

c = matlab.unittest.constraints.StructComparator(___,Name,Value)는 위에 열거된 구문에 나와 있는 입력 인수 조합 외에, 이름-값 인수를 하나 이상 사용하여 추가 옵션을 설정합니다. 예를 들어, c = matlab.unittest.constraints.StructComparator("Recursively",true)는 구조체형 배열에 포함된 값을 비교할 때 재귀적으로 동작하는 비교 연산자를 만듭니다.

입력 인수

모두 확장

구조체형 배열에 포함된 값을 비교하는 데 사용하는 비교 연산자로, matlab.unittest.constraints 네임스페이스에서 비교 연산자로 분류되는 클래스의 객체 배열로 지정됩니다.

예: matlab.unittest.constraints.NumericComparator

예: matlab.unittest.constraints.StringComparator("IgnoringCase",true)

예: [matlab.unittest.constraints.LogicalComparator matlab.unittest.constraints.NumericComparator]

이름-값 인수

선택적 인수 쌍을 Name1=Value1,...,NameN=ValueN으로 지정합니다. 여기서 Name은 인수 이름이고 Value는 대응값입니다. 이름-값 인수는 다른 인수 뒤에 와야 하지만, 인수 쌍의 순서는 상관없습니다.

예: c = matlab.unittest.constraints.StructComparator(Recursively=true)

R2021a 이전 릴리스에서는 쉼표를 사용하여 각 이름과 값을 구분하고 Name을 따옴표로 묶으십시오.

예: c = matlab.unittest.constraints.StructComparator("Recursively",true)

비교 시 무시할 필드로, string형 배열 또는 문자형 벡터로 구성된 셀형 배열로 지정됩니다. 비교 연산자는 지정된 필드의 값을 비교하지 않습니다.

이 인수는 IgnoredFields 속성을 설정합니다.

예: "IgnoringFields","field1"

재귀적으로 동작을 수행할지 여부로, 숫자형 또는 논리값 0(false) 또는 1(true)로 지정됩니다.

값이 true이면 실제 구조체형 배열과 예상 구조체형 배열의 값이 구조체형 배열도 될 수 있으며, 비교 연산자는 이러한 요소를 재귀적으로 비교합니다. 값이 false이면 실제 구조체형 배열의 필드와 예상 구조체형 배열의 필드의 모든 값은 comp에서 지원하는 유형이어야 합니다. 예를 들어, 다음 코드에서는 c1c2 모두 필드에 숫자형 값을 포함하는 구조체형 배열을 비교할 수 있습니다. 그러나 오로지 c2만이 필드에 구조체형 배열을 포함하는 구조체형 배열이나 필드에 숫자형 값을 포함하는 구조체형 배열을 비교할 수 있습니다.

import matlab.unittest.constraints.StructComparator
import matlab.unittest.constraints.NumericComparator

c1 = StructComparator(NumericComparator);
c2 = StructComparator(NumericComparator,"Recursively",true);

이 인수는 Recursive 속성을 설정합니다.

속성

모두 확장

비교 시 무시할 필드로, 문자형 벡터로 구성된 셀형 배열로 반환됩니다.

이 속성은 IgnoringFields 이름-값 인수에 의해 설정됩니다.

특성:

GetAccess
public
SetAccess
private

재귀적으로 동작을 수행할지 여부로, 논리값 0(false) 또는 1(true)로 반환됩니다.

이 속성은 Recursively 이름-값 인수에 의해 설정됩니다.

특성:

GetAccess
public
SetAccess
private

예제

모두 축소

StructComparator 클래스를 사용하여 데이터가 없는 구조체형 배열을 비교합니다.

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

import matlab.unittest.TestCase
import matlab.unittest.constraints.IsEqualTo
import matlab.unittest.constraints.StructComparator

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

testCase = TestCase.forInteractiveUse;

StructComparator 인스턴스를 사용하여 필드가 없는 구조체형 배열 2개를 비교합니다. 크기가 일치하지 않으므로 테스트가 실패합니다.

testCase.verifyThat(struct,IsEqualTo([struct struct], ...
    "Using",StructComparator))
Verification failed.
    ---------------------
    Framework Diagnostic:
    ---------------------
    IsEqualTo failed.
    --> StructComparator failed.
        --> Sizes do not match.
            
            Actual size:
                 1     1
            Expected size:
                 1     2
        
        Actual Value:
          struct with no fields.
        Expected Value:
          1×2 struct array with no fields.
    ------------------
    Stack Information:
    ------------------
    In C:\work\CompareStructureArraysThatContainNoDataExample.m (CompareStructureArraysThatContainNoDataExample) at 18

2개의 빈 구조체형 배열을 비교합니다. 크기가 동일해도 실제 구조체형 배열과 예상 구조체형 배열의 클래스가 동일하지 않기 때문에 이 테스트는 실패합니다.

testCase.verifyThat(struct([]),IsEqualTo(struct("f1",{},"f2",{}), ...
    "Using",StructComparator))
Verification failed.
    ---------------------
    Framework Diagnostic:
    ---------------------
    IsEqualTo failed.
    --> StructComparator failed.
        --> Structures contain different fields.
            --> Missing fields in Actual:
                    'f1'
                    'f2'
        
        Actual Value:
          0×0 empty struct array with no fields.
        Expected Value:
          0×0 empty struct array with fields:
        
            f1
            f2
    ------------------
    Stack Information:
    ------------------
    In C:\work\CompareStructureArraysThatContainNoDataExample.m (CompareStructureArraysThatContainNoDataExample) at 24

struct([])struct.empty가 동일하게 빈 구조체형 배열인지 확인합니다.

testCase.verifyThat(struct([]),IsEqualTo(struct.empty, ...
    "Using",StructComparator))
Verification passed.

StructComparator 클래스를 사용하여 데이터를 포함하는 구조체를 비교합니다.

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

import matlab.unittest.TestCase
import matlab.unittest.constraints.IsEqualTo
import matlab.unittest.constraints.StructComparator
import matlab.unittest.constraints.NumericComparator
import matlab.unittest.constraints.StringComparator
import matlab.unittest.constraints.RelativeTolerance

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

testCase = TestCase.forInteractiveUse;

필드에 숫자형 값이 있는 구조체를 2개 만듭니다.

s1 = struct("ID",10,"score",90);
s2 = struct("score",90,"ID",10);

StructComparator 인스턴스를 사용하여 구조체를 비교합니다. 구조체형 배열에 데이터가 포함된 경우 적절한 비교 연산자를 StructComparator 생성자에 전달합니다. 테스트가 통과합니다.

testCase.verifyThat(s1,IsEqualTo(s2, ...
    "Using",StructComparator(NumericComparator)))
Verification passed.

s2에 포함된 값 중 하나를 변경하고 구조체를 다시 비교합니다. 테스트가 실패합니다.

s2.score = 95;
testCase.verifyThat(s1,IsEqualTo(s2, ...
    "Using",StructComparator(NumericComparator)))
Verification failed.
    ---------------------
    Framework Diagnostic:
    ---------------------
    IsEqualTo failed.
    --> Path to failure: <Value>.score
        --> NumericComparator failed.
            --> The numeric values are not equal using "isequaln".
            --> Failure table:
                    Actual    Expected    Error       RelativeError   
                    ______    ________    _____    ___________________
                                                                      
                      90         95        -5      -0.0526315789473684
            
            Actual Value:
                90
            Expected Value:
                95
    
    Actual Value:
      struct with fields:
    
           ID: 10
        score: 90
    Expected Value:
      struct with fields:
    
        score: 95
           ID: 10
    ------------------
    Stack Information:
    ------------------
    In C:\work\CompareStructuresThatContainDataExample.m (CompareStructuresThatContainDataExample) at 31

테스트가 통과하도록 하기 위해, 대응하는 값들이 상대 허용오차 0.1 내에서 동일해야 한다고 지정합니다.

testCase.verifyThat(s1,IsEqualTo(s2, ...
    "Using",StructComparator(NumericComparator( ...
    "Within",RelativeTolerance(0.1)))))
Verification passed.

비교 연산자가 재귀적으로 동작하도록 지시하여 string형을 포함하는 중첩 구조체를 비교합니다. 중첩 구조가 동일하지 않으므로 테스트가 실패합니다.

s3 = struct("name",struct("first","Mary","last","Smith"), ...
    "location","Apartment 4");
s4 = struct("name",struct("first","Sam","last","Smith"), ...
    "location","Apartment 4");
testCase.verifyThat(s3,IsEqualTo(s4, ...
    "Using",StructComparator(StringComparator,"Recursively",true)))
Verification failed.
    ---------------------
    Framework Diagnostic:
    ---------------------
    IsEqualTo failed.
    --> Path to failure: <Value>.name.first
        --> StringComparator failed.
            --> The strings are not equal.
            
            Actual Value:
                "Mary"
            Expected Value:
                "Sam"
    
    Actual Value:
      struct with fields:
    
            name: [1×1 struct]
        location: "Apartment 4"
    Expected Value:
      struct with fields:
    
            name: [1×1 struct]
        location: "Apartment 4"
    ------------------
    Stack Information:
    ------------------
    In C:\work\CompareStructuresThatContainDataExample.m (CompareStructuresThatContainDataExample) at 47

  • 대부분의 경우에는 StructComparator 인스턴스를 사용할 필요가 없습니다. IsEqualTo 클래스는 구조체형 배열을 포함하여 다양한 데이터형의 동일성을 테스트하는 제약 조건을 만듭니다.

    IsEqualTo 클래스가 수행하는 비교를 재정의해야 하는 경우 StructComparator 인스턴스를 사용하십시오. 예를 들어, 구조체형 배열이 숫자가 아닌 값을 포함하는 경우 비교가 실패하도록 하려면 테스트에 StructComparator 인스턴스를 포함하십시오. 다음 예제에서는 실제 구조체와 예상 예상 구조체에 숫자형이 아닌 값이 포함되어 있으므로 MATLAB®이 오류를 발생시킵니다.

    import matlab.unittest.TestCase
    import matlab.unittest.constraints.IsEqualTo
    import matlab.unittest.constraints.StructComparator
    import matlab.unittest.constraints.NumericComparator
    
    testCase = TestCase.forInteractiveUse;
    exp = struct("f1",zeros(1,10),"f2",'a',"f3",{'b','c'});
    act = exp;
    testCase.verifyThat(act,IsEqualTo(exp,"Using",StructComparator(NumericComparator)))
    

버전 내역

R2013a에 개발됨