preload

N-N Comparison: SetMatchFor

Posted by Albert Gareev on Aug 12, 2010 | Categories: Source codeVerification

Reference Page: Comparison Rules

Automatic Comparison Rules

Relationship: Many-to-Many / Rule: Match For Set in Another Set

Definitions

Supported data types: String, Number, Date
Supported data structures: Vector

Comparison of values of different data types always returns False.
Comparison of values of unsupported data types always returns False.

Vector data structure is presented by a set (un-ordered array) of values of the same type. Externally, Set is defined as a comma-separated sequence (might be encoded in string). Internally, Set is transformed into an array to optimize performance.

A set matches another set if all the members of the first set are also members of the second set. That is, the first set is a subset of the second one.
Example: a set {2, 3} is a subset of a set {2, 3, 10}; a set {3, 3} is not a subset of a set {10, 2, 3}.

Data type: String

Strings are compared based on total length and encoding of each character.

Two strings are considered equal if they have equal length (same number of characters) AND all characters, compared one by one, were equal.

Data type: Number

Numeric values are considered equal if there is no difference (near-zero-difference) between them (matters for floating point numbers with precision loss).

Data type: Date

Date/time values are compared per component values: Year, Month, Day, Hour, Second. Two dates are considered equal if there is zero seconds difference between them.

Implementation

Used resources:

1. Service Functions – Math (QTP, VBScript)

2. Service Functions – String (QTP, VBScript)

Public Function SetMatchFor(ByVal stActualSet, ByVal stExpectedSet, ByVal sFormat, ByVal sRule, ByVal boolNegativeRule)
  Dim dvActualArray, dvExpectedArray, Iter, Jter
  Dim boolRC, boolResult
  Dim objDictionary
 
  'Note. The format MUST BE checked separately to distinguish this kind of fails
  dvActualArray = Set2Array(stActualSet, CMP_Var_DefaultSetSeparator)
  dvExpectedArray = Set2Array(stExpectedSet, CMP_Var_DefaultSetSeparator)
  boolRC = isSameFormatNN(dvActualArray, dvExpectedArray, sFormat)
  If Not boolRC Then
    SetMatchFor = False
    Exit Function
  End If
                           
  dvActualArray = Set2TypedArray(stActualSet, CMP_Var_DefaultSetSeparator, sFormat)
  dvExpectedArray = Set2TypedArray(stExpectedSet, CMP_Var_DefaultSetSeparator, sFormat)
 
  If UBound(dvActualArray) < UBound(dvExpectedArray) Then
    If boolNegativeRule Then
      SetMatchFor = True
    Else
      SetMatchFor = False
    End If
    Exit Function
  End If
 
  Set objDictionary = CreateObject("Scripting.Dictionary")
  boolResult = True
 
 For Iter=0 to Ubound(dvExpectedArray)

    For Jter=0 To UBound(dvActualArray)
      Do While True
     
        If objDictionary.Exists(Jter) Then
          Exit Do
        End If
       
        Select Case UCase(sRule)
        Case "EQUAL"
        boolRC = isValueEqual(dvActualArray(Jter), dvExpectedArray(Iter), sFormat)
        Case "MATCHIN"
         boolRC = Regex_Test(dvExpectedArray(Iter), dvActualArray(Jter))
       Case "MATCHFOR"
         boolRC = Regex_Test(dvActualArray(Jter), dvExpectedArray(Iter))
        Case Else
        SetMatchFor = False
            Set objDictionary = Nothing
        Exit Function
        End Select

        If boolRC Then
          objDictionary.Item(Jter) = Jter
          Exit For
        End If

        Exit Do
      Loop
     
    Next
   
    boolResult = boolResult AND boolRC
   
 Next

 If boolNegativeRule Then
    SetMatchFor = Not boolResult
  Else
    SetMatchFor = boolResult
  End If
 
  Set objDictionary = Nothing
 
End Function

Test Code

  Log.Message("N-N Comparison: SetMatchFor")
 
  boolRC = SetMatchFor("3, 5, 10", "3, 5, 10", "Number", "Equal", False)
  If Not boolRC Then
    Log.Error("SetMatchFor failed")
  End If
  boolRC = SetMatchFor("3, 5, 10", "3, 5, 10", "Number", "Equal", True)
  If boolRC Then
    Log.Error("SetMatchFor failed")
  End If
  boolRC = SetMatchFor("3, 5, 10", "3, 5", "Number", "Equal", False)
  If Not boolRC Then
    Log.Error("SetMatchFor failed")
  End If
  boolRC = SetMatchFor("3, 5, 10", "3, 5", "Number", "Equal", True)
  If boolRC Then
    Log.Error("SetMatchFor failed")
  End If
  boolRC = SetMatchFor("3, 5, 10", "10, 3", "Number", "Equal", False)
  If Not boolRC Then
    Log.Error("SetMatchFor failed")
  End If
  boolRC = SetMatchFor("3, 5, 10", "10, 3", "Number", "Equal", True)
  If boolRC Then
    Log.Error("SetMatchFor failed")
  End If

  boolRC = SetMatchFor("3, 5, 10", "10, 12", "Number", "Equal", False)
  If boolRC Then
    Log.Error("SetMatchFor failed")
  End If
  boolRC = SetMatchFor("3, 5, 10", "10, 12", "Number", "Equal", True)
  If Not boolRC Then
    Log.Error("SetMatchFor failed")
  End If
  boolRC = SetMatchFor("3, 5, 10", "3, 5, 10, 3", "Number", "Equal", False)
  If boolRC Then
    Log.Error("SetMatchFor failed")
  End If
  boolRC = SetMatchFor("3, 5, 10", "3, 5, 10, 3", "Number", "Equal", True)
  If Not boolRC Then
    Log.Error("SetMatchFor failed")
  End If
  boolRC = SetMatchFor("3, 5, 10", "3, 3", "Number", "Equal", False)
  If boolRC Then
    Log.Error("SetMatchFor failed")
  End If
  boolRC = SetMatchFor("3, 5, 10", "3, 3", "Number", "Equal", True)
  If Not boolRC Then
    Log.Error("SetMatchFor failed")
  End If
  boolRC = SetMatchFor("3, 5, 3", "3, 3", "Number", "Equal", False)
  If Not boolRC Then
    Log.Error("SetMatchFor failed")
  End If
  boolRC = SetMatchFor("3, 5, 3", "3, 3", "Number", "Equal", True)
  If boolRC Then
    Log.Error("SetMatchFor failed")
  End If
  boolRC = SetMatchFor("3, 5, 10", "3, 5, 3", "Number", "Equal", False)
  If boolRC Then
    Log.Error("SetMatchFor failed")
  End If

  boolRC = SetMatchFor("USD, CAD, AUD", "USD, CAD, AUD", "String", "Equal", False)
  If Not boolRC Then
    Log.Error("SetMatchFor failed")
  End If
  boolRC = SetMatchFor("USD, CAD, AUD", "USD, CAD, AUD", "String", "MatchIn", False)
  If Not boolRC Then
    Log.Error("SetMatchFor failed")
  End If
  boolRC = SetMatchFor("USD, CAD, AUD", "USD, CAD, AUD", "String", "MatchIn", True)
  If boolRC Then
    Log.Error("SetMatchFor failed")
  End If
  boolRC = SetMatchFor("USD, CAD, AUD", "USD, CAD, AUD", "String", "MatchFor", False)
  If Not boolRC Then
    Log.Error("SetMatchFor failed")
  End If
  boolRC = SetMatchFor("USD, CAD, AUD", "USD, , AUD", "String", "Equal", False)
  If boolRC Then
    Log.Error("SetMatchFor failed")
  End If
  boolRC = SetMatchFor("USD, CAD, AUD", "USD, , AUD", "String", "Equal", True)
  If Not boolRC Then
    Log.Error("SetMatchFor failed")
  End If
  boolRC = SetMatchFor("USD, CAD, AUD", "USD, AUD", "String", "Equal", False)
  If Not boolRC Then
    Log.Error("SetMatchFor failed")
  End If

  boolRC = SetMatchFor("[A-Z][A-Z][A-Z][A-Z], [0-9]{4}, [A-Z]{3}", "GBP, 2010", "String", "MatchIn", False)
  If Not boolRC Then
    Log.Error("SetMatchFor failed")
  End If
  boolRC = SetMatchFor("GBP, 2010, EUR, 011", "[A-Z][A-Z][A-Z], [0-9]{4}", "String", "MatchFor", False)
  If Not boolRC Then
    Log.Error("SetMatchFor failed")
  End If


  • One response to "N-N Comparison: SetMatchFor"

  • ranjith ashok
    14th May 2014 at 12:11

    good website highly benefical for qtp experts

Creative Commons Attribution-NonCommercial-NoDerivs 3.0 Unported
This work by Albert Gareev is licensed under a Creative Commons Attribution-NonCommercial-NoDerivs 3.0 Unported.