preload

1-N Comparison: isValueInSet

Posted by Albert Gareev on Jul 29, 2010 | Categories: Source codeVerification

Reference Page: Comparison Rules

Automatic Comparison Rules

Relationship: One-to-Many / Rule: Is Value In Set

Definitions

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

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

Scalar data structure is presented by any single value.
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 value is in set if is equal (matching for strings comparison) to at least one member of a set.
Example: 5 is in a set {2,10, 5, -1, 20}; 6 is NOT in the same set. See more examples in Test Code section.

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 isValueInSet(ByVal sActualValue, ByVal stSet, ByVal sFormat, ByVal sRule, ByVal boolNegativeRule)
  Dim dvArray, Iter
  Dim boolRC, boolResult

  'Note. The format MUST BE checked separately to distinguish this kind of fails
  dvArray = Set2Array(stSet, CMP_Var_DefaultSetSeparator)
  boolRC = isSameFormat1N(sActualValue, dvArray, sFormat)
  If Not boolRC Then
    isValueInSet = False
    Exit Function
  End If
 
  dvArray = Set2TypedArray(stSet, CMP_Var_DefaultSetSeparator, sFormat)
 
  boolResult = False
 
 For Iter=0 to Ubound(dvArray)
   Select Case UCase(sRule)
   Case "EQUAL"
    boolRC = isValueEqual(sActualValue, dvArray(Iter), sFormat)
   Case "MATCHIN"
     boolRC = Regex_Test(dvArray(Iter), sActualValue)
   Case "MATCHFOR"
     boolRC = Regex_Test(sActualValue, dvArray(Iter))
   Case Else
    isValueInSet = False
    Exit Function
   End Select
    boolResult = boolResult OR boolRC
   
    If boolResult Then
     If boolNegativeRule Then
        isValueInSet = False
      Else
        isValueInSet = True
      End If
     
      Exit Function
    End If

 Next
 
 If boolNegativeRule Then
    isValueInSet = Not boolResult
  Else
    isValueInSet = boolResult
 End If
 
End Function

Test Code

  Log.Message("1-N Comparison: isValueInSet")

  boolRC = isValueInSet(15, "10, 15, 20, 30, 45", "Number", "Equal", False)
  If Not boolRC Then
    Log.Error("isValueInSet failed")
  End If
  boolRC = isValueInSet(15, "10, 20, 30, 45", "Number", "Equal", False)
  If boolRC Then
    Log.Error("isValueInSet failed")
  End If
  boolRC = isValueInSet(15, "10, 20, 30, 45", "Number", "Equal", True)
  If Not boolRC Then
    Log.Error("isValueInSet failed")
  End If

  boolRC = isValueInSet("May", "January, March, May, July, September", "String", "Equal", False)
  If Not boolRC Then
    Log.Error("isValueInSet failed")
  End If
  boolRC = isValueInSet("May", "January, March, May, July, September", "String", "Equal", True)
  If boolRC Then
    Log.Error("isValueInSet failed")
  End If
  boolRC = isValueInSet("....y", "January, March, May, July, September", "String", "MatchIn", False)
  If Not boolRC Then
    Log.Error("isValueInSet failed")
  End If
  boolRC = isValueInSet("....y", "March, May, July, September", "String", "MatchIn", False)
  If boolRC Then
    Log.Error("isValueInSet failed")
  End If
  boolRC = isValueInSet("Mar.*", "January, March, May, July, September", "String", "MatchIn", False)
  If Not boolRC Then
    Log.Error("isValueInSet failed")
  End If
  boolRC = isValueInSet("Ma.*y", "January, March, May, July, September", "String", "MatchIn", False)
  If Not boolRC Then
    Log.Error("isValueInSet failed")
  End If
  boolRC = isValueInSet("Ma..*y", "January, March, May, July, September", "String", "MatchIn", False)
  If boolRC Then
    Log.Error("isValueInSet failed")
  End If
  boolRC = isValueInSet("CAD", "USD, AUD, CAD, GBP, JPY", "String", "MatchFor", False)
  If Not boolRC Then
    Log.Error("isValueInSet failed")
  End If

  boolRC = isValueInSet("10/01/2000", "5/01/2000, 10/01/2000, 15/01/2000, 20/01/2000, 25/01/2000", "Date", "Equal", False)
  If Not boolRC Then
    Log.Error("isValueInSet failed")
  End If
  boolRC = isValueInSet("10/01/2000", "5/01/2000, 10/01/2000, 15/01/2000, 20/01/2000, 25/01/2000", "Date", "Equal", True)
  If boolRC Then
    Log.Error("isValueInSet failed")
  End If
  boolRC = isValueInSet("16/01/2000", "5/01/2000, 10/01/2000, 15/01/2000, 20/01/2000, 25/01/2000", "Date", "Equal", False)
  If boolRC Then
    Log.Error("isValueInSet failed")
  End If


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.