1-1 Comparison: isValueGreater
Reference Page: Comparison Rules
Automatic Comparison Rules
Relationship: One-to-One / Rule: Is Value Greater
Definitions
Supported data types: String, Number, Date
Comparison of values of different data types always returns False.
Comparison of values of unsupported data types always returns False.
Data type: String
Strings are compared based on total length and encoding of each character.
A string of longer length is always considered greater. (Updated 2010/07/20, see comments section)
Comparison of two strings is performed character by character up to the first character that differs. The string with the greater character in that position is considered greater.
Data type: Number
A greater value is considered greater than a smaller one. All negative numbers are less than zero and all positive numbers. Thus, -1 is less than 100; -100 is less than -1.
The floating-point value NaN
(not a number) is greater than any other numeric value and is equal to itself.
Data type: Date
Date/time values are compared per component values: Year, Month, Day, Hour, Second.
A later date is considered greater than an earlier one. A date with a greater month but lesser year is considered less, and so forth.
Implementation
Public Function isValueGreater(ByVal sActualValue, ByVal sExpectedValue, ByVal sFormat) If Not isSameFormat(sActualValue, sExpectedValue, sFormat) Then isValueGreater = False Exit Function End If Select Case UCase(sFormat) Case "NUMBER" If CDbl(sActualValue) > CDbl(sExpectedValue) Then isValueGreater = True Else isValueGreater = False End If Case "DATE" If DateDiff("s", CDate(sActualValue), CDate(sExpectedValue)) < 0 Then isValueGreater = True Else isValueGreater = False End If Case "STRING" If StrComp(sActualValue, sExpectedValue) = 1 Then isValueGreater = True Else isValueGreater = False End If Case Else isValueGreater = False End Select End Function
Test Code
Log.Message("1-1 Comparison: isValueGreater") boolRC = isValueGreater("A", "ZZ", "String") If boolRC Then Log.Error("isValueGreater failed") End If boolRC = isValueGreater("zzzzaaaa", "aaaazzzz", "String") If Not boolRC Then Log.Error("isValueGreater failed") End If boolRC = isValueGreater("abc", "ab", "String") If Not boolRC Then Log.Error("isValueGreater failed") End If boolRC = isValueGreater("abc", "ab ", "String") If Not boolRC Then Log.Error("isValueGreater failed") End If boolRC = isValueGreater(1, 0, "Number") If Not boolRC Then Log.Error("isValueGreater failed") End If boolRC = isValueGreater(dblNearlyZero, 0, "Number") If Not boolRC Then Log.Error("isValueGreater failed") End If boolRC = isValueGreater(0, -1, "Number") If Not boolRC Then Log.Error("isValueGreater failed") End If boolRC = isValueGreater(-1, -10, "Number") If Not boolRC Then Log.Error("isValueGreater failed") End If sValue1 = CDate("1/1/2002") sValue2 = CDate("01/01/01") boolRC = isValueGreater(sValue1, sValue2, "DATE") If Not boolRC Then Log.Error("isValueGreater failed") End If sValue1 = CDate("1/1/2050") sValue2 = CDate("31/12/2050") boolRC = isValueGreater(sValue1, sValue2, "DATE") If boolRC Then Log.Error("isValueGreater failed") End If
2 responses to "1-1 Comparison: isValueGreater"
“A string of longer length is always considered greater.”
That seems odd.
[Albert’s reply.
These functions implement oracles for comparison, and it’s not that simple with strings because they represent complex data. While it may seem odd that “Z” is less than “AA”, you would find perfectly fine that “10 USD” is greater than “9 USD”. Math comparison will fail this test.
Thanks. ]
It does seem odd that ‘Z’ is less than ‘AA’ or ‘A ‘. or ‘ ‘, etc. And it might seem odd that ‘9’ is less than ‘001’.
You are correct that you are free to define your oracles as you choose. I guess I just don’t understand what your oracle is trying to do with strings.
[ Albert’s reply.
Joe, I really appreciate your input and follow-up. Moreover, I got convinced to review these functions (isValueGreater / isValueLess). Although they did work well for me in multiple projects, that discrepancy might cause an issue during maintenance of automated tests.
The best way, I think, to handle a special case of equal length strings comparison. I already have flags like “Match Case”, “Trim”, “Pre-Format”, “Ignore special chars”, etc.; probably, it’s time to add another one.
Thanks again! ]