N-N Comparison: isSetEqual

Categories: Source codeVerification

Reference Page: Comparison Rules

Automatic Comparison Rules

Relationship: Many-to-Many / Rule: Is Set Equal

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.

The set is equal to another set if all elements of the first set, compared one by one, are equal (match for string comparison) to the elements of the second set, respectively. Per Boolean flag specified, sets can be sorted first.
Example: a set {2, 3, 10} is equal to a set {2, 3, 10}; a set {2, 3, 10} is equal to a set {10, 2, 3} only if sorting order doesn’t matter.

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 isSetEqual(ByVal stActualSet, ByVal stExpectedSet, ByVal sFormat, ByVal boolOrderMatters, ByVal sRule, ByVal boolNegativeRule)
 Dim dvActualArray, dvExpectedArray, Iter
 Dim boolRC
 '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
 isSetEqual = 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
 isSetEqual = True
 Else
 isSetEqual = False
 End If
 Exit Function
 End If
 If Not boolOrderMatters Then
 dvActualArray = TypedArraySort(dvActualArray, sFormat, True)
 dvExpectedArray = TypedArraySort(dvExpectedArray, sFormat, True)
 End If
 For Iter=0 to Ubound(dvActualArray)
 Select Case UCase(sRule)
 Case "EQUAL"
 boolRC = isValueEqual(dvActualArray(Iter), dvExpectedArray(Iter), sFormat)
 Case "MATCHIN"
 boolRC = Regex_Test(dvExpectedArray(Iter), dvActualArray(Iter))
 Case "MATCHFOR"
 boolRC = Regex_Test(dvActualArray(Iter), dvExpectedArray(Iter))
 Case Else
 isSetEqual = False
 Exit Function
 End Select
 If boolNegativeRule Then
 boolRC = Not boolRC
 End If
 If Not boolRC Then
 isSetEqual = False
 Exit Function
 End If
 Next
 isSetEqual = True
End Function

Test Code

 Log.Message("N-N Comparison: isSetEqual")
 boolRC = isSetEqual("3, 5, 10", "3, 5, 10", "Number", True, "Equal", False)
 If Not boolRC Then
 Log.Error("isSetEqual failed")
 End If
 boolRC = isSetEqual("3, 5, 10, 15", "3, 5, 10", "Number", True, "Equal", False)
 If boolRC Then
 Log.Error("isSetEqual failed")
 End If
 boolRC = isSetEqual("3, 5, 10, 15", "3, 5, 10", "Number", True, "Equal", True)
 If Not boolRC Then
 Log.Error("isSetEqual failed")
 End If
 boolRC = isSetEqual("3, 5, 10", "3, 5, 10", "Number", False, "Equal", False)
 If Not boolRC Then
 Log.Error("isSetEqual failed")
 End If
 boolRC = isSetEqual("3, 5, 7, 10", "3, 5, 10", "Number", True, "Equal", False)
 If boolRC Then
 Log.Error("isSetEqual failed")
 End If
 boolRC = isSetEqual("3, 5, 10", "5, 3, 10", "Number", True, "Equal", False)
 If boolRC Then
 Log.Error("isSetEqual failed")
 End If
 boolRC = isSetEqual("3, 5, 10", "5, 3, 10", "Number", False, "Equal", False)
 If Not boolRC Then
 Log.Error("isSetEqual failed")
 End If
 boolRC = isSetEqual("3, 5, 10", "3, 5, 10", "Number", True, "Equal", True)
 If boolRC Then
 Log.Error("isSetEqual failed")
 End If
 boolRC = isSetEqual("3, 5, 10", "3, 5, 10", "Number", False, "Equal", True)
 If boolRC Then
 Log.Error("isSetEqual failed")
 End If
 boolRC = isSetEqual("3, 3, 3", "3, 5, 10", "Number", False, "Equal", False)
 If boolRC Then
 Log.Error("isSetEqual failed")
 End If
 boolRC = isSetEqual("USD, CAD, AUD", "USD, CAD, AUD", "String", True, "Equal", False)
 If Not boolRC Then
 Log.Error("isSetEqual failed")
 End If
 boolRC = isSetEqual("USD, CAD, AUD", "CAD, USD, AUD", "String", False, "Equal", False)
 If Not boolRC Then
 Log.Error("isSetEqual failed")
 End If
 boolRC = isSetEqual("..D, ..D, ..D, ..P", "CAD, USD, AUD, GBP", "String", True, "MatchIn", False)
 If Not boolRC Then
 Log.Error("isSetEqual failed")
 End If
 boolRC = isSetEqual("CAD, ONTARIO, 2010", "[A-Z][A-Z][A-Z], [A-Z]+, [0-9]{4}", "String", True, "MatchFor", False)
 If Not boolRC Then
 Log.Error("isSetEqual 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.