在下面的Ada代碼:奇怪串長度限定(/微調)中VectorCAST的 - 阿達
package body TestMyApp is
use type Base_Types.Natural16;
use type Base_Types.Integer32;
use type C.Strings.Chars_Ptr;
use type C.Size_T;
-- Error Messages length should be limited by upper bound 'Err_Msg_Max_Len'
Err_Msg_Max_Length : constant C.Size_T := 100;
Glb_C_Err_Msg_String : aliased C.Char_Array := (1..(Err_Msg_Max_Length + 1) => C.nul);
function Fixed_String_To_Chr_Ptr (Source_String : String;
Trim_For_Whitespaces : Boolean) return C.Strings.Chars_Ptr is
Ptr : C.Strings.Chars_Ptr := C.Strings.Null_Ptr;
Elem_Copied_Count : C.Size_T := 0;
begin
-- Reset each character in Glb_C_Err_Msg_String array to nul
Glb_C_Err_Msg_String := (1..(Err_Msg_Max_Length + 1) => C.nul);
-- Check whether source string is of acceptable length
if Source_String'Length <= Natural(Err_Msg_Max_Length) then
if Trim_For_Whitespaces = True then
-- Copy fixed string elements into char_array with
-- source string's both side trimmed for whitespaces
C.To_C(Item => Ada.Strings.Fixed.Trim(Source => Source_String, Side => Ada.Strings.Both),
Target => Glb_C_Err_Msg_String,
Count => Elem_Copied_Count,
Append_Nul => True);
else
-- Copy fixed string elements into char_array
C.To_C(Item => Source_String,
Target => Glb_C_Err_Msg_String,
Count => Elem_Copied_Count,
Append_Nul => True);
end if;
-- Convert char_array into char_ptr
Ptr := C.Strings.To_Chars_Ptr(Item => Glb_C_Err_Msg_String'Access,
Nul_Check => True);
else
Ptr := C.Strings.Null_Ptr;
end if;
-- Return the char_ptr
return Ptr;
end Fixed_String_To_Chr_Ptr;
end TestMyApp;
雖然單元測試在VectorCAST的-ADA,如果字符串(Source_String)的長度被傳遞給函數Fixed_String_To_Chr_Ptr()小於或等於100 (Err_Msg_Max_Length),該函數得到了正確執行,並且如果條件如下:
if Source_String'Length <= Natural(Err_Msg_Max_Length) then
正確評估爲True。但是奇怪的是,如果如果字符串(Source_String)的長度被傳遞給函數Fixed_String_To_Chr_Ptr()大於100 (Err_Msg_Max_Length),函數仍然TRUE評價上述條件爲真,其應該不會發生,因爲字符串的長度大於Err_Msg_Max_Length。在調試時,觀察到即使字符串(Source_String)被傳遞給函數Fixed_String_To_Chr_Ptr()的長度大於100,該函數在內部限制/修剪其長度爲100 **(Err_Msg_Max_Length)* * 只要。
任何想法這裏有什麼錯?
什麼是矢量直播? – darkestkhan
https://www.vectorcast.com/software-testing-products/ada-unit-testing –
我的猜測是,您並未完全測試此處顯示的功能。 –