Comparing Structurers in ATEasy

Knowledge Base Article # Q200172

Read Prior Article Read Next Article
Summary This article demonstrates a simple procedure for comparing struct fields.
  
Login to rate article
Programming languages typically do not support structure comparison directly.  In ATEasy, structures can be compared by converting them to a Variant and then comparing the variants.  In ATEasy, when a structure is assigned to a variant, the structure is converted to an array of variant data types, where each field of the structure becomes an element in the array.

A generic structure comparison procedure can be coded as follows:

Procedure CompareStructs(vrStruct1, vrStruct2): Bool
! Compare structs of the same type, return TRUE if equal, FALSE otherwise
--------------------------------------------------------------------------------
vrStruct1: Val Variant
vrStruct2: Val Variant
i: Long
{
! Must have the same number of fields
If VarDimSize(vrStruct1, 0)<> VarDimSize(vrStruct2, 0) Then Return FALSE

! Compare each element/field
For i=0 To VarDimSize(vrStruct1, 0)-1
   ! Test if element is another array/structure
   If VarDimSize(vrStruct1[i], 0)
      ! Re-call CompareStructs() procedure
      Return CompareStructs(vrStruct1[i],vrStruct2[i])
   Else
      ! Test for same data type
      If VarType(vrStruct1[i])<>VarType(vrStruct2[i]) Then Return FALSE
      ! Test for same value
      If vrStruct1[i]<>vrStruct2[i] Then Return FALSE
  EndIf
Next

! structs are equal
Return TRUE
}


Following are example tests to compare two structs of the same type with both Pass and Fail results, and two nested structures with both Pass and Fail results:

Types
================================================================================
strTest: Struct
{
  Field1: Long
  Field2: Long
  Field3: String
}

strTest2: Struct
{
  Field1: String
  Field2: strTest
}
================================================================================

Variables
================================================================================
  st1: strTest
  st2: strTest
  nst1: strTest2
  nst2: strTest2
================================================================================

Tests
================================================================================
Task 1 : "Test Compare Structs"
--------------------------------------------------------------------------------

Test 1.1 : "Struct Compare"
--------------------------------------------------------------------------------
Type = Precise
Value = -1
{
  st1.Field1=1
  st1.Field2=2
  st1.Field3="123"

  st2.Field1=1
  st2.Field2=2
  st2.Field3="123"

  TestResult=CompareStructs(st1,st2)
}

Test 1.2 : "Struct Not Compare"
--------------------------------------------------------------------------------
Type = Precise
Value = -1
{
  st1.Field1=1
  st1.Field2=2
  st1.Field3="123"

  st2.Field1=1
  st2.Field2=2
  st2.Field3="132"

  TestResult=CompareStructs(st1,st2)
}

Task 2 : "Test Compare Nested Structs"
--------------------------------------------------------------------------------

Test 2.1 : "Struct Compare"
--------------------------------------------------------------------------------
Type = Precise
Value = -1
{
  nst1.Field1="Test"
  nst1.Field2.Field1=1
  nst1.Field2.Field2=2
  nst1.Field2.Field3="123"

  nst2.Field1="Test"
  nst2.Field2.Field1=1
  nst2.Field2.Field2=2
  nst2.Field2.Field3="123"

  TestResult=CompareStructs(nst1,nst2)
}

Test 2.2 : "Struct Not Compare"
--------------------------------------------------------------------------------
Type = Precise
Value = -1
{
  nst1.Field1="Test"
  nst1.Field2.Field1=1
  nst1.Field2.Field2=2
  nst1.Field2.Field3="123"

  nst2.Field1="Test"
  nst2.Field2.Field1=1
  nst2.Field2.Field2=2
  nst2.Field2.Field3="132"

  TestResult=CompareStructs(nst1,nst2)
}


Running the Task/Test programs produces the following results:

Test Log Results:
Task 1 : Test Compare Structs
------------------------------------------

#   Test Name          Pin    Unit     Value      Result   Status
--- ------------------ ------ ------ ---------- ---------- ------
001 Struct Compare     -      -         -1.0000    -1.0000 Pass
002 Struct Not Compare -      -         -1.0000    +0.0000 Fail*

Task 2 : Test Compare Structs
------------------------------------------

#   Test Name          Pin    Unit     Value      Result   Status
--- ------------------ ------ ------ ---------- ---------- ------
001 Struct Compare     -      -         -1.0000    -1.0000 Pass
002 Struct Not Compare -      -         -1.0000    +0.0000 Fail*
Article Date 2/1/2010
Keywords Struct, Compare Structures, ATEasy


Login to rate article

Read Prior Article Read Next Article
>