From da3221401b932008229ffc72fac28b2e92bbf3bb Mon Sep 17 00:00:00 2001 From: Jakob Sagatowski Date: Sun, 9 Dec 2018 13:11:57 +0100 Subject: [PATCH] Added more types of arrays that are supported by assertions. --- TcUnit/TcUnit/POUs/FB_Assert.TcPOU | 421 +++++++++++++++++++++++++---- 1 file changed, 374 insertions(+), 47 deletions(-) diff --git a/TcUnit/TcUnit/POUs/FB_Assert.TcPOU b/TcUnit/TcUnit/POUs/FB_Assert.TcPOU index df7e84d..b27f155 100644 --- a/TcUnit/TcUnit/POUs/FB_Assert.TcPOU +++ b/TcUnit/TcUnit/POUs/FB_Assert.TcPOU @@ -53,23 +53,31 @@ VAR TestInstancePath : Tc2_System.T_MaxString; SizeOfExpecteds : DINT; SizeOfActuals : DINT; + ExpectedsIndex : DINT; + ActualsIndex : DINT; END_VAR]]> LOWER_BOUND(Actuals, 1)) OR - (UPPER_BOUND(Expecteds, 1) <> UPPER_BOUND(Actuals, 1)) THEN - SizeOfExpecteds := UPPER_BOUND(Expecteds, 1) - LOWER_BOUND(Expecteds, 1) + 1; - SizeOfActuals := UPPER_BOUND(Actuals, 1) - LOWER_BOUND(Actuals, 1) + 1; +SizeOfExpecteds := ABS(UPPER_BOUND(Expecteds, 1) - LOWER_BOUND(Expecteds, 1)) + 1; +SizeOfActuals := ABS(UPPER_BOUND(Actuals, 1) - LOWER_BOUND(Actuals, 1)) + 1; + +IF SizeOfExpecteds <> SizeOfActuals THEN Equals := FALSE; SizeEquals := FALSE; ELSE - FOR Index := LOWER_BOUND(Expecteds, 1) TO UPPER_BOUND(Expecteds, 1) DO - IF Expecteds[Index] <> Actuals[Index] THEN + (* Even though we know that both arrays are equal in size, the two arrays can start at two completely different + indexes, which needs to be taken into account for. *) + ExpectedsIndex := LOWER_BOUND(Expecteds, 1); // The start position for the expecteds + ActualsIndex := LOWER_BOUND(Actuals, 1); // The start position for the actuals + FOR Index := 1 TO SizeOfExpecteds BY 1 DO + IF Expecteds[ExpectedsIndex] <> Actuals[ActualsIndex] THEN Equals := FALSE; EXIT; - END_IF - END_FOR + END_IF + ExpectedsIndex := ExpectedsIndex + 1; + ActualsIndex := ActualsIndex + 1; + END_FOR END_IF AssertResults.ReportResult(Expected := Expecteds, @@ -89,14 +97,14 @@ IF NOT AlreadyReported AND NOT Equals THEN ActualString := CONCAT(STR1 := ActualString, STR2 := DINT_TO_STRING(SizeOfActuals)); ELSE ExpectedString := 'ARRAY['; - ExpectedString := CONCAT(STR1 := ExpectedString, STR2 := DINT_TO_STRING(Index)); + ExpectedString := CONCAT(STR1 := ExpectedString, STR2 := DINT_TO_STRING(ExpectedsIndex)); ExpectedString := CONCAT(STR1 := ExpectedString, STR2 := '] = '); - ExpectedString := CONCAT(STR1 := ExpectedString, STR2 := BOOL_TO_STRING(Expecteds[Index])); + ExpectedString := CONCAT(STR1 := ExpectedString, STR2 := BOOL_TO_STRING(Expecteds[ExpectedsIndex])); ActualString := 'ARRAY['; - ActualString := CONCAT(STR1 := ActualString, STR2 := DINT_TO_STRING(Index)); + ActualString := CONCAT(STR1 := ActualString, STR2 := DINT_TO_STRING(ActualsIndex)); ActualString := CONCAT(STR1 := ActualString, STR2 := '] = '); - ActualString := CONCAT(STR1 := ActualString, STR2 := BOOL_TO_STRING(Actuals[Index])); + ActualString := CONCAT(STR1 := ActualString, STR2 := BOOL_TO_STRING(Actuals[ActualsIndex])); END_IF AssertMessageFormatter.LogAssertFailure(Expected := ExpectedString, @@ -130,23 +138,31 @@ VAR SizeOfActuals : DINT; ExpectedByteString : STRING; ActualByteString : STRING; + ExpectedsIndex : DINT; + ActualsIndex : DINT; END_VAR]]> LOWER_BOUND(Actuals, 1)) OR - (UPPER_BOUND(Expecteds, 1) <> UPPER_BOUND(Actuals, 1)) THEN - SizeOfExpecteds := UPPER_BOUND(Expecteds, 1) - LOWER_BOUND(Expecteds, 1) + 1; - SizeOfActuals := UPPER_BOUND(Actuals, 1) - LOWER_BOUND(Actuals, 1) + 1; +SizeOfExpecteds := ABS(UPPER_BOUND(Expecteds, 1) - LOWER_BOUND(Expecteds, 1)) + 1; +SizeOfActuals := ABS(UPPER_BOUND(Actuals, 1) - LOWER_BOUND(Actuals, 1)) + 1; + +IF SizeOfExpecteds <> SizeOfActuals THEN Equals := FALSE; SizeEquals := FALSE; ELSE - FOR Index := LOWER_BOUND(Expecteds, 1) TO UPPER_BOUND(Expecteds, 1) DO - IF Expecteds[Index] <> Actuals[Index] THEN + (* Even though we know that both arrays are equal in size, the two arrays can start at two completely different + indexes, which needs to be taken into account for. *) + ExpectedsIndex := LOWER_BOUND(Expecteds, 1); // The start position for the expecteds + ActualsIndex := LOWER_BOUND(Actuals, 1); // The start position for the actuals + FOR Index := 1 TO SizeOfExpecteds BY 1 DO + IF Expecteds[ExpectedsIndex] <> Actuals[ActualsIndex] THEN Equals := FALSE; EXIT; - END_IF - END_FOR + END_IF + ExpectedsIndex := ExpectedsIndex + 1; + ActualsIndex := ActualsIndex + 1; + END_FOR END_IF AssertResults.ReportResult(Expected := Expecteds, @@ -166,20 +182,20 @@ IF NOT AlreadyReported AND NOT Equals THEN ActualString := CONCAT(STR1 := ActualString, STR2 := DINT_TO_STRING(SizeOfActuals)); ELSE ExpectedByteString := Tc2_Standard.CONCAT(STR1 := '0x', - STR2 := Tc2_Utilities.BYTE_TO_HEXSTR(in := Expecteds[Index], + STR2 := Tc2_Utilities.BYTE_TO_HEXSTR(in := Expecteds[ExpectedsIndex], iPrecision := 2, bLoCase := FALSE)); ExpectedString := 'ARRAY['; - ExpectedString := CONCAT(STR1 := ExpectedString, STR2 := DINT_TO_STRING(Index)); + ExpectedString := CONCAT(STR1 := ExpectedString, STR2 := DINT_TO_STRING(ExpectedsIndex)); ExpectedString := CONCAT(STR1 := ExpectedString, STR2 := '] = '); ExpectedString := CONCAT(STR1 := ExpectedString, STR2 := ExpectedByteString); ActualByteString := Tc2_Standard.CONCAT(STR1 := '0x', - STR2 := Tc2_Utilities.BYTE_TO_HEXSTR(in := Actuals[Index], + STR2 := Tc2_Utilities.BYTE_TO_HEXSTR(in := Actuals[ActualsIndex], iPrecision := 2, bLoCase := FALSE)); ActualString := 'ARRAY['; - ActualString := CONCAT(STR1 := ActualString, STR2 := DINT_TO_STRING(Index)); + ActualString := CONCAT(STR1 := ActualString, STR2 := DINT_TO_STRING(ActualsIndex)); ActualString := CONCAT(STR1 := ActualString, STR2 := '] = '); ActualString := CONCAT(STR1 := ActualString, STR2 := ActualByteString); END_IF @@ -213,23 +229,290 @@ VAR TestInstancePath : Tc2_System.T_MaxString; SizeOfExpecteds : DINT; SizeOfActuals : DINT; + ExpectedsIndex : DINT; + ActualsIndex : DINT; END_VAR]]> LOWER_BOUND(Actuals, 1)) OR - (UPPER_BOUND(Expecteds, 1) <> UPPER_BOUND(Actuals, 1)) THEN - SizeOfExpecteds := UPPER_BOUND(Expecteds, 1) - LOWER_BOUND(Expecteds, 1) + 1; - SizeOfActuals := UPPER_BOUND(Actuals, 1) - LOWER_BOUND(Actuals, 1) + 1; +SizeOfExpecteds := ABS(UPPER_BOUND(Expecteds, 1) - LOWER_BOUND(Expecteds, 1)) + 1; +SizeOfActuals := ABS(UPPER_BOUND(Actuals, 1) - LOWER_BOUND(Actuals, 1)) + 1; + +IF SizeOfExpecteds <> SizeOfActuals THEN Equals := FALSE; SizeEquals := FALSE; ELSE - FOR Index := LOWER_BOUND(Expecteds, 1) TO UPPER_BOUND(Expecteds, 1) DO - IF Expecteds[Index] <> Actuals[Index] THEN + (* Even though we know that both arrays are equal in size, the two arrays can start at two completely different + indexes, which needs to be taken into account for. *) + ExpectedsIndex := LOWER_BOUND(Expecteds, 1); // The start position for the expecteds + ActualsIndex := LOWER_BOUND(Actuals, 1); // The start position for the actuals + FOR Index := 1 TO SizeOfExpecteds BY 1 DO + IF Expecteds[ExpectedsIndex] <> Actuals[ActualsIndex] THEN Equals := FALSE; EXIT; - END_IF - END_FOR + END_IF + ExpectedsIndex := ExpectedsIndex + 1; + ActualsIndex := ActualsIndex + 1; + END_FOR +END_IF + +AssertResults.ReportResult(Expected := Expecteds, + Actual := Actuals, + Message := Message, + TestInstancePath := TestInstancePath, + AlreadyReported => AlreadyReported); + +IF NOT AlreadyReported AND NOT Equals THEN + SetTestFailed(); + + IF NOT SizeEquals THEN + Message := CONCAT(STR1 := Message, STR2 := ', size of arrays not matching.'); + ExpectedString := 'SIZE = '; + ExpectedString := CONCAT(STR1 := ExpectedString, STR2 := DINT_TO_STRING(SizeOfExpecteds)); + ActualString := 'SIZE = '; + ActualString := CONCAT(STR1 := ActualString, STR2 := DINT_TO_STRING(SizeOfActuals)); + ELSE + ExpectedString := 'ARRAY['; + ExpectedString := CONCAT(STR1 := ExpectedString, STR2 := DINT_TO_STRING(ExpectedsIndex)); + ExpectedString := CONCAT(STR1 := ExpectedString, STR2 := '] = '); + ExpectedString := CONCAT(STR1 := ExpectedString, STR2 := DINT_TO_STRING(Expecteds[ExpectedsIndex])); + + ActualString := 'ARRAY['; + ActualString := CONCAT(STR1 := ActualString, STR2 := DINT_TO_STRING(ActualsIndex)); + ActualString := CONCAT(STR1 := ActualString, STR2 := '] = '); + ActualString := CONCAT(STR1 := ActualString, STR2 := DINT_TO_STRING(Actuals[ActualsIndex])); + END_IF + + AssertMessageFormatter.LogAssertFailure(Expected := ExpectedString, + Actual := ActualString, + Message := Message, + TestInstancePath := TestInstancePath); +END_IF]]> + + + + + + SizeOfActuals THEN + Equals := FALSE; + SizeEquals := FALSE; +ELSE + (* Even though we know that both arrays are equal in size, the two arrays can start at two completely different + indexes, which needs to be taken into account for. *) + ExpectedsIndex := LOWER_BOUND(Expecteds, 1); // The start position for the expecteds + ActualsIndex := LOWER_BOUND(Actuals, 1); // The start position for the actuals + FOR Index := 1 TO SizeOfExpecteds BY 1 DO + IF Expecteds[ExpectedsIndex] <> Actuals[ActualsIndex] THEN + Equals := FALSE; + EXIT; + END_IF + ExpectedsIndex := ExpectedsIndex + 1; + ActualsIndex := ActualsIndex + 1; + END_FOR +END_IF + +AssertResults.ReportResult(Expected := Expecteds, + Actual := Actuals, + Message := Message, + TestInstancePath := TestInstancePath, + AlreadyReported => AlreadyReported); + +IF NOT AlreadyReported AND NOT Equals THEN + SetTestFailed(); + + IF NOT SizeEquals THEN + Message := CONCAT(STR1 := Message, STR2 := ', size of arrays not matching.'); + ExpectedString := 'SIZE = '; + ExpectedString := CONCAT(STR1 := ExpectedString, STR2 := DINT_TO_STRING(SizeOfExpecteds)); + ActualString := 'SIZE = '; + ActualString := CONCAT(STR1 := ActualString, STR2 := DINT_TO_STRING(SizeOfActuals)); + ELSE + ExpectedDWordString := Tc2_Standard.CONCAT(STR1 := '0x', + STR2 := Tc2_Utilities.DWORD_TO_HEXSTR(in := Expecteds[ExpectedsIndex], + iPrecision := 8, + bLoCase := FALSE)); + ExpectedString := 'ARRAY['; + ExpectedString := CONCAT(STR1 := ExpectedString, STR2 := DINT_TO_STRING(ExpectedsIndex)); + ExpectedString := CONCAT(STR1 := ExpectedString, STR2 := '] = '); + ExpectedString := CONCAT(STR1 := ExpectedString, STR2 := ExpectedDWordString); + + ActualDWordString := Tc2_Standard.CONCAT(STR1 := '0x', + STR2 := Tc2_Utilities.DWORD_TO_HEXSTR(in := Actuals[ActualsIndex], + iPrecision := 8, + bLoCase := FALSE)); + ActualString := 'ARRAY['; + ActualString := CONCAT(STR1 := ActualString, STR2 := DINT_TO_STRING(ActualsIndex)); + ActualString := CONCAT(STR1 := ActualString, STR2 := '] = '); + ActualString := CONCAT(STR1 := ActualString, STR2 := ActualDWordString); + END_IF + + AssertMessageFormatter.LogAssertFailure(Expected := ExpectedString, + Actual := ActualString, + Message := Message, + TestInstancePath := TestInstancePath); +END_IF]]> + + + + + + SizeOfActuals THEN + Equals := FALSE; + SizeEquals := FALSE; +ELSE + (* Even though we know that both arrays are equal in size, the two arrays can start at two completely different + indexes, which needs to be taken into account for. *) + ExpectedsIndex := LOWER_BOUND(Expecteds, 1); // The start position for the expecteds + ActualsIndex := LOWER_BOUND(Actuals, 1); // The start position for the actuals + FOR Index := 1 TO SizeOfExpecteds BY 1 DO + IF Expecteds[ExpectedsIndex] <> Actuals[ActualsIndex] THEN + Equals := FALSE; + EXIT; + END_IF + ExpectedsIndex := ExpectedsIndex + 1; + ActualsIndex := ActualsIndex + 1; + END_FOR +END_IF + +AssertResults.ReportResult(Expected := Expecteds, + Actual := Actuals, + Message := Message, + TestInstancePath := TestInstancePath, + AlreadyReported => AlreadyReported); + +IF NOT AlreadyReported AND NOT Equals THEN + SetTestFailed(); + + IF NOT SizeEquals THEN + Message := CONCAT(STR1 := Message, STR2 := ', size of arrays not matching.'); + ExpectedString := 'SIZE = '; + ExpectedString := CONCAT(STR1 := ExpectedString, STR2 := DINT_TO_STRING(SizeOfExpecteds)); + ActualString := 'SIZE = '; + ActualString := CONCAT(STR1 := ActualString, STR2 := DINT_TO_STRING(SizeOfActuals)); + ELSE + ExpectedString := 'ARRAY['; + ExpectedString := CONCAT(STR1 := ExpectedString, STR2 := DINT_TO_STRING(ExpectedsIndex)); + ExpectedString := CONCAT(STR1 := ExpectedString, STR2 := '] = '); + ExpectedString := CONCAT(STR1 := ExpectedString, STR2 := INT_TO_STRING(Expecteds[ExpectedsIndex])); + + ActualString := 'ARRAY['; + ActualString := CONCAT(STR1 := ActualString, STR2 := DINT_TO_STRING(ActualsIndex)); + ActualString := CONCAT(STR1 := ActualString, STR2 := '] = '); + ActualString := CONCAT(STR1 := ActualString, STR2 := INT_TO_STRING(Actuals[ActualsIndex])); + END_IF + + AssertMessageFormatter.LogAssertFailure(Expected := ExpectedString, + Actual := ActualString, + Message := Message, + TestInstancePath := TestInstancePath); +END_IF]]> + + + + + + SizeOfActuals THEN + Equals := FALSE; + SizeEquals := FALSE; +ELSE + (* Even though we know that both arrays are equal in size, the two arrays can start at two completely different + indexes, which needs to be taken into account for. *) + ExpectedsIndex := LOWER_BOUND(Expecteds, 1); // The start position for the expecteds + ActualsIndex := LOWER_BOUND(Actuals, 1); // The start position for the actuals + FOR Index := 1 TO SizeOfExpecteds BY 1 DO + IF Expecteds[ExpectedsIndex] <> Actuals[ActualsIndex] THEN + Equals := FALSE; + EXIT; + END_IF + ExpectedsIndex := ExpectedsIndex + 1; + ActualsIndex := ActualsIndex + 1; + END_FOR END_IF AssertResults.ReportResult(Expected := Expecteds, @@ -249,14 +532,14 @@ IF NOT AlreadyReported AND NOT Equals THEN ActualString := CONCAT(STR1 := ActualString, STR2 := DINT_TO_STRING(SizeOfActuals)); ELSE ExpectedString := 'ARRAY['; - ExpectedString := CONCAT(STR1 := ExpectedString, STR2 := DINT_TO_STRING(Index)); + ExpectedString := CONCAT(STR1 := ExpectedString, STR2 := DINT_TO_STRING(ExpectedsIndex)); ExpectedString := CONCAT(STR1 := ExpectedString, STR2 := '] = '); - ExpectedString := CONCAT(STR1 := ExpectedString, STR2 := DINT_TO_STRING(Expecteds[Index])); + ExpectedString := CONCAT(STR1 := ExpectedString, STR2 := LINT_TO_STRING(Expecteds[ExpectedsIndex])); ActualString := 'ARRAY['; - ActualString := CONCAT(STR1 := ActualString, STR2 := DINT_TO_STRING(Index)); + ActualString := CONCAT(STR1 := ActualString, STR2 := DINT_TO_STRING(ActualsIndex)); ActualString := CONCAT(STR1 := ActualString, STR2 := '] = '); - ActualString := CONCAT(STR1 := ActualString, STR2 := DINT_TO_STRING(Actuals[Index])); + ActualString := CONCAT(STR1 := ActualString, STR2 := LINT_TO_STRING(Actuals[ActualsIndex])); END_IF AssertMessageFormatter.LogAssertFailure(Expected := ExpectedString, @@ -991,10 +1274,8 @@ END_FOR]]> - - - - + + @@ -1002,10 +1283,8 @@ END_FOR]]> - - - - + + @@ -1019,9 +1298,57 @@ END_FOR]]> - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +