preload

1-N Comparison: isValueInRange

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

Reference Page: Comparison Rules

Automatic Comparison Rules

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

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 array to optimize performance.

The value is in range if it is greater (equal or greater for inclusive comparison) then a minimal value member of a set, and is less (equal or less for inclusive comparison) then a maximal value member of a set.
Example: 5 is in range of a set {2,10, 5, -1, 20}

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: Service Functions – Math (QTP, VBScript)

Public Function isValueInRange(ByVal sActualValue, ByVal stSet, ByVal sFormat, ByVal boolInclusive, ByVal boolNegativeRule)
  Dim dvArray, RangeMin, RangeMax
  Dim boolRC
 
  '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
    isValueInRange = False
    Exit Function
  End If

  dvArray = TypedArraySort(Set2TypedArray(stSet, CMP_Var_DefaultSetSeparator, sFormat), sFormat, True)
 
  RangeMin = dvArray(0)
  RangeMax = dvArray(UBound(dvArray))
 
  If boolInclusive Then
    boolRC = isValueEqualOrGreater(sActualValue, RangeMin, sFormat)
    boolRC = boolRC AND isValueEqualOrLess(sActualValue, RangeMax, sFormat) 
  Else
    boolRC = isValueGreater(sActualValue, RangeMin, sFormat)
    boolRC = boolRC AND isValueLess(sActualValue, RangeMax, sFormat) 
  End If
 
 If boolNegativeRule Then
    isValueInRange = Not boolRC
  Else
    isValueInRange = boolRC
  End If
 
End Function

Test Code

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

  boolRC = isValueInRange(10, "10, 20, 30", "Float", True, False) 
  If boolRC Then
    Log.Error("isValueInRange failed")
  End If
 
  boolRC = isValueInRange(10, "10, 20, 30", "Number", True, False) 
  If Not boolRC Then
    Log.Error("isValueInRange failed")
  End If
  boolRC = isValueInRange(10, "10, 20, 30", "Number", True, True) 
  If boolRC Then
    Log.Error("isValueInRange failed")
  End If
  boolRC = isValueInRange(10, "10, 20, 30", "Number", False, False) 
  If boolRC Then
    Log.Error("isValueInRange failed")
  End If
  boolRC = isValueInRange(5, "10, 100, -5, 15, 5, 20, 30", "Number", False, False) 
  If Not boolRC Then
    Log.Error("isValueInRange failed")
  End If
  boolRC = isValueInRange(5, "10, 100, -5, 15, 5, 20, 30", "Number", False, True) 
  If boolRC Then
    Log.Error("isValueInRange failed")
  End If
  boolRC = isValueInRange(5, "10, 100, 7, 15, 5, 20, 30", "Number", False, False) 
  If boolRC Then
    Log.Error("isValueInRange failed")
  End If
  boolRC = isValueInRange(200, "10, 100, 7, 15, 5, 20, 30", "Number", False, False) 
  If boolRC Then
    Log.Error("isValueInRange failed")
  End If
 
  boolRC = isValueInRange("10", "10.0, 20, 30", "String", True, False) 
  If boolRC Then
    Log.Error("isValueInRange failed")
  End If
  boolRC = isValueInRange("aaa", "abc, abb, dac, cdaafsfe, afsdf", "String",  False, False) 
  If boolRC Then
    Log.Error("isValueInRange failed")
  End If
  boolRC = isValueInRange("aaa", "aa, aaaa", "String",  False, False) 
  If Not boolRC Then
    Log.Error("isValueInRange failed")
  End If
  boolRC = isValueInRange("aaa", "ab, aaab", "String",  False, False) 
  If boolRC Then
    Log.Error("isValueInRange failed")
  End If

  boolRC = isValueInRange("01/06/1984", "01/01/1900, 01/01/2000, 12/14/1937, 22/06/1941, 09/05/1945", "Date", True, False) 
  If Not boolRC Then
    Log.Error("isValueInRange failed")
  End If
  boolRC = isValueInRange("01/06/1984", "01/01/1900, 01/01/2000, 12/14/1937, 22/06/19415, 09/05/1945", "Date", True, False) 
  If boolRC Then
    Log.Error("isValueInRange failed")
  End If


  • One response to "1-N Comparison: isValueInRange"

  • Joe Strazzere
    27th July 2010 at 8:58

    It seems like your Test Code for string datatype should have cases where the comparison set includes values that are shorter.

    For example:
    boolRC = isValueInRange(“aaa”, “aa, aaaa”, False, False)
    boolRC = isValueInRange(“aaa”, “ab, aaab”, False, False)

    etc.

    [ Albert’s reply. Done! (1) – True; (2) – False ]

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.