N-N Comparison: SetMatchIn

Categories: Source codeVerification

Reference Page: Comparison Rules

Automatic Comparison Rules

Relationship: Many-to-Many / Rule: Set Match 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 SetMatchIn(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
 SetMatchIn = 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
 SetMatchIn = True
 Else
 SetMatchIn = False
 End If
 Exit Function
 End If
 Set objDictionary = CreateObject("Scripting.Dictionary")
 boolResult = True
 For Iter=0 to Ubound(dvActualArray)
 For Jter=0 To UBound(dvExpectedArray)
 Do While True
 If objDictionary.Exists(Jter) Then
 Exit Do
 End If
 Select Case UCase(sRule)
 Case "EQUAL"
 boolRC = isValueEqual(dvActualArray(Iter), dvExpectedArray(Jter), sFormat)
 Case "MATCHIN"
 boolRC = Regex_Test(dvExpectedArray(Jter), dvActualArray(Iter))
 Case "MATCHFOR"
 boolRC = Regex_Test(dvActualArray(Iter), dvExpectedArray(Jter))
 Case Else
 SetMatchIn = 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
 SetMatchIn = Not boolResult
 Else
 SetMatchIn = boolResult
 End If
 Set objDictionary = Nothing
End Function

Test Code

 Log.Message("N-N Comparison: SetMatchIn")
 boolRC = SetMatchIn("3, 5, 10", "3, 5, 10", "Number", "Equal", False)
 If Not boolRC Then
 Log.Error("SetMatchIn failed")
 End If
 boolRC = SetMatchIn("3, 5, 10", "3, 5, 10", "Number", "Equal", True)
 If boolRC Then
 Log.Error("SetMatchIn failed")
 End If
 boolRC = SetMatchIn("3, 5", "3, 5, 10", "Number", "Equal", False)
 If Not boolRC Then
 Log.Error("SetMatchIn failed")
 End If
 boolRC = SetMatchIn("3, 5", "3, 5, 10", "Number", "Equal", True)
 If boolRC Then
 Log.Error("SetMatchIn failed")
 End If
 boolRC = SetMatchIn("10, 3", "3, 5, 10", "Number", "Equal", False)
 If Not boolRC Then
 Log.Error("SetMatchIn failed")
 End If
 boolRC = SetMatchIn("10, 3", "3, 5, 10", "Number", "Equal", True)
 If boolRC Then
 Log.Error("SetMatchIn failed")
 End If
 boolRC = SetMatchIn("10, 12", "3, 5, 10", "Number", "Equal", False)
 If boolRC Then
 Log.Error("SetMatchIn failed")
 End If
 boolRC = SetMatchIn("10, 12", "3, 5, 10", "Number", "Equal", True)
 If Not boolRC Then
 Log.Error("SetMatchIn failed")
 End If
 boolRC = SetMatchIn("3, 5, 10, 3", "3, 5, 10", "Number", "Equal", False)
 If boolRC Then
 Log.Error("SetMatchIn failed")
 End If
 boolRC = SetMatchIn("3, 5, 10, 3", "3, 5, 10", "Number", "Equal", True)
 If Not boolRC Then
 Log.Error("SetMatchIn failed")
 End If
 boolRC = SetMatchIn("3, 3", "3, 5, 10", "Number", "Equal", False)
 If boolRC Then
 Log.Error("SetMatchIn failed")
 End If
 boolRC = SetMatchIn("3, 3", "3, 5, 10", "Number", "Equal", True)
 If Not boolRC Then
 Log.Error("SetMatchIn failed")
 End If
 boolRC = SetMatchIn("3, 3", "3, 5, 3", "Number", "Equal", False)
 If Not boolRC Then
 Log.Error("SetMatchIn failed")
 End If
 boolRC = SetMatchIn("3, 3", "3, 5, 3", "Number", "Equal", True)
 If boolRC Then
 Log.Error("SetMatchIn failed")
 End If
 boolRC = SetMatchIn("3, 5, 3", "3, 5, 10", "Number", "Equal", False)
 If boolRC Then
 Log.Error("SetMatchIn failed")
 End If
 boolRC = SetMatchIn("USD, CAD, AUD", "USD, CAD, AUD", "String", "Equal", False)
 If Not boolRC Then
 Log.Error("SetMatchIn failed")
 End If
 boolRC = SetMatchIn("USD, CAD, AUD", "USD, CAD, AUD", "String", "MatchIn", False)
 If Not boolRC Then
 Log.Error("SetMatchIn failed")
 End If
 boolRC = SetMatchIn("USD, CAD, AUD", "USD, CAD, AUD", "String", "MatchIn", True)
 If boolRC Then
 Log.Error("SetMatchIn failed")
 End If
 boolRC = SetMatchIn("USD, CAD, AUD", "USD, CAD, AUD", "String", "MatchFor", False)
 If Not boolRC Then
 Log.Error("SetMatchIn failed")
 End If
 boolRC = SetMatchIn("USD, , AUD", "USD, CAD, AUD", "String", "Equal", False)
 If boolRC Then
 Log.Error("SetMatchIn failed")
 End If
 boolRC = SetMatchIn("USD, , AUD", "USD, CAD, AUD", "String", "Equal", True)
 If Not boolRC Then
 Log.Error("SetMatchIn failed")
 End If
 boolRC = SetMatchIn("USD, AUD", "USD, CAD, AUD", "String", "Equal", False)
 If Not boolRC Then
 Log.Error("SetMatchIn failed")
 End If
 boolRC = SetMatchIn("GBP, 2010", "[A-Z][A-Z][A-Z][A-Z], [0-9]{4}, [A-Z]{3}", "String", "MatchFor", False)
 If Not boolRC Then
 Log.Error("SetMatchIn failed")
 End If
 boolRC = SetMatchIn("[A-Z][A-Z][A-Z], [0-9]{4}", "GBP, 2010, EUR, 011", "String", "MatchIn", False)
 If Not boolRC Then
 Log.Error("SetMatchIn failed")
 End If

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