preload

N-N Comparison: isRecordEqual

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

Reference Page: Comparison Rules

Automatic Comparison Rules

Relationship: Many-to-Many / Rule: Is Data Record [in Dictionary] Equal To Another One

Definitions

Supported data types: String, Number, Date
Supported data structures: Dictionary (Associative Array)

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

A record is equal to another record if all elements of the first record, compared one by one, are equal (match for string comparison) to the elements of the second record, respectively. Comparison is performed by indices and values.

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

Public Function isRecordEqual(ByVal objActualRecord, ByVal objExpectedRecord, ByVal boolNegativeRule)
  Dim dvAArray, dvEArray, Iter
  Dim sAKey, sEKey
  Dim boolRC
 
  dvAArray = objActualRecord.Keys()
  dvEArray = objExpectedRecord.Keys()
 
  If UBound(dvAArray) <> UBound(dvEArray) Then
    If boolNegativeRule Then
      isRecordEqual = True
    Else
      isRecordEqual = False
    End If
    Exit Function
  End If
 
  For Iter=0 to UBound(dvAArray)
    If Not objExpectedRecord.Exists(dvAArray(Iter)) Then
      If boolNegativeRule Then
        isRecordEqual = True
      Else
        isRecordEqual = False
      End If
      Exit Function
    End If
  Next 
 
  For Iter=0 to UBound(dvEArray)
    If Not objActualRecord.Exists(dvEArray(Iter)) Then
      If boolNegativeRule Then
        isRecordEqual = True
      Else
        isRecordEqual = False
      End If
      Exit Function
    End If
  Next 
 
 For Iter=0 to UBound(dvAArray)
  boolRC = (objActualRecord.Item(dvAArray(Iter)) = objExpectedRecord.Item(dvEArray(Iter)))
  If boolNegativeRule Then
      boolRC = Not boolRC
    End If
  If Not boolRC Then
   isRecordEqual = False
   Exit Function
  End If
 Next
 
  isRecordEqual = True
 
End Function

Test Code

  Log.Message("N-N Comparison: isRecordEqual")
 
  boolRC = isRecordEqual(AssociateParameters("month = July, place = Aruba, price = 1000"), AssociateParameters("month = July, place = Aruba, price = 1000"), False)
  If Not boolRC Then
    Log.Error("isRecordEqual failed")
  End If
  boolRC = isRecordEqual(AssociateParameters("month = July, place = Aruba, price = 1000"), AssociateParameters("month = July, place = Aruba, price = 1000"), True)
  If boolRC Then
    Log.Error("isRecordEqual failed")
  End If
  boolRC = isRecordEqual(AssociateParameters("month = August, place = Aruba, price = 1000"), AssociateParameters("month = July, place = Aruba, price = 1000"), False)
  If boolRC Then
    Log.Error("isRecordEqual failed")
  End If
  boolRC = isRecordEqual(AssociateParameters("month = July, place = Aruba, price = 1000"), AssociateParameters("month = July, flight = 747A, place = Aruba, price = 1000"), False)
  If boolRC Then
    Log.Error("isRecordEqual failed")
  End If
  boolRC = isRecordEqual(AssociateParameters("month = July, place = Aruba, price = 1000"), AssociateParameters("month = July, flight = 747A, place = Aruba, price = 1000"), True)
  If Not boolRC Then
    Log.Error("isRecordEqual 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.