Новые примеры - тесты StrToInt
This commit is contained in:
parent
83ec52d6e8
commit
b9f6e31958
96
InstallerSamples/WhatsNew/3_12/a_test_StrToInt.pas
Normal file
96
InstallerSamples/WhatsNew/3_12/a_test_StrToInt.pas
Normal file
|
|
@ -0,0 +1,96 @@
|
||||||
|
// Исправлены ошибки в StrToInt
|
||||||
|
|
||||||
|
procedure TestOK(s: string; expected: integer);
|
||||||
|
begin
|
||||||
|
var x := StrToInt(s);
|
||||||
|
if x <> expected then
|
||||||
|
raise new Exception($'FAIL OK: "{s}" → {x}, expected {expected}');
|
||||||
|
end;
|
||||||
|
|
||||||
|
procedure TestFormat(s: string);
|
||||||
|
begin
|
||||||
|
try
|
||||||
|
var x := StrToInt(s);
|
||||||
|
raise new Exception($'FAIL Format: "{s}" → {x}, expected FormatException');
|
||||||
|
except
|
||||||
|
on e: System.FormatException do ;
|
||||||
|
on e: Exception do
|
||||||
|
raise new Exception($'FAIL Format: "{s}" → wrong exception type: {e.GetType}');
|
||||||
|
end;
|
||||||
|
end;
|
||||||
|
|
||||||
|
procedure TestOverflow(s: string);
|
||||||
|
begin
|
||||||
|
try
|
||||||
|
var x := StrToInt(s);
|
||||||
|
raise new Exception($'FAIL Overflow: "{s}" → {x}, expected OverflowException');
|
||||||
|
except
|
||||||
|
on e: System.OverflowException do ;
|
||||||
|
on e: Exception do
|
||||||
|
raise new Exception($'FAIL Overflow: "{s}" → wrong exception type: {e.GetType}');
|
||||||
|
end;
|
||||||
|
end;
|
||||||
|
|
||||||
|
begin
|
||||||
|
// --- helpers ---
|
||||||
|
|
||||||
|
// --- basic ---
|
||||||
|
TestOK('0', 0);
|
||||||
|
TestOK('5', 5);
|
||||||
|
TestOK('123', 123);
|
||||||
|
TestOK('000123', 123);
|
||||||
|
|
||||||
|
// --- signs ---
|
||||||
|
TestOK('+123', 123);
|
||||||
|
TestOK('-123', -123);
|
||||||
|
TestOK('+0', 0);
|
||||||
|
TestOK('-0', 0);
|
||||||
|
|
||||||
|
// --- whitespace ---
|
||||||
|
TestOK(' 123', 123);
|
||||||
|
TestOK('123 ', 123);
|
||||||
|
TestOK(' 123 ', 123);
|
||||||
|
TestOK(' -42 ', -42);
|
||||||
|
|
||||||
|
// --- boundaries ---
|
||||||
|
TestOK('2147483647', 2147483647);
|
||||||
|
TestOK('-2147483648', -2147483648);
|
||||||
|
|
||||||
|
// --- near boundaries ---
|
||||||
|
TestOK('2147483646', 2147483646);
|
||||||
|
TestOK('-2147483647', -2147483647);
|
||||||
|
|
||||||
|
// --- overflow ---
|
||||||
|
TestOverflow('2147483648');
|
||||||
|
TestOverflow('2147483649');
|
||||||
|
TestOverflow('-2147483649');
|
||||||
|
TestOverflow('-9999999999');
|
||||||
|
TestOverflow('9999999999');
|
||||||
|
|
||||||
|
// --- format errors ---
|
||||||
|
TestFormat('');
|
||||||
|
TestFormat(' ');
|
||||||
|
TestFormat('+');
|
||||||
|
TestFormat('-');
|
||||||
|
TestFormat('abc');
|
||||||
|
TestFormat('12a');
|
||||||
|
TestFormat('a12');
|
||||||
|
TestFormat('++1');
|
||||||
|
TestFormat('--1');
|
||||||
|
TestFormat('+-1');
|
||||||
|
|
||||||
|
// --- trailing garbage ---
|
||||||
|
TestFormat('123abc');
|
||||||
|
TestFormat('123 abc');
|
||||||
|
|
||||||
|
// --- internal spaces ---
|
||||||
|
TestFormat('1 23');
|
||||||
|
TestFormat('- 123');
|
||||||
|
|
||||||
|
// --- edge tricky ---
|
||||||
|
TestOK('0000000000', 0);
|
||||||
|
TestOK('0000000001', 1);
|
||||||
|
TestFormat(' + 1');
|
||||||
|
|
||||||
|
Println('All tests passed');
|
||||||
|
end.
|
||||||
80
InstallerSamples/WhatsNew/3_12/a_test_StrToInt2.pas
Normal file
80
InstallerSamples/WhatsNew/3_12/a_test_StrToInt2.pas
Normal file
|
|
@ -0,0 +1,80 @@
|
||||||
|
// Сравнение StrToInt с Int32.Parse
|
||||||
|
|
||||||
|
begin
|
||||||
|
var tests := [
|
||||||
|
// --- OK ---
|
||||||
|
'0', '5', '123', '000123',
|
||||||
|
'+123', '-123', '+0', '-0',
|
||||||
|
' 123', '123 ', ' 123 ', ' -42 ',
|
||||||
|
'2147483647', '-2147483648',
|
||||||
|
'2147483646', '-2147483647',
|
||||||
|
'0000000000', '0000000001',
|
||||||
|
|
||||||
|
// --- Overflow ---
|
||||||
|
'2147483648', '2147483649',
|
||||||
|
'-2147483649', '-9999999999', '9999999999',
|
||||||
|
|
||||||
|
// --- Format ---
|
||||||
|
'', ' ', '+', '-',
|
||||||
|
'abc', '12a', 'a12',
|
||||||
|
'++1', '--1', '+-1',
|
||||||
|
'123abc', '123 abc',
|
||||||
|
'1 23', '- 123', ' + 1'
|
||||||
|
];
|
||||||
|
|
||||||
|
foreach var s in tests do
|
||||||
|
begin
|
||||||
|
var ok1 := true;
|
||||||
|
var ok2 := true;
|
||||||
|
|
||||||
|
var r1 := 0;
|
||||||
|
var r2 := 0;
|
||||||
|
|
||||||
|
var e1: Exception := nil;
|
||||||
|
var e2: Exception := nil;
|
||||||
|
|
||||||
|
// --- StrToInt ---
|
||||||
|
try
|
||||||
|
r1 := StrToInt(s);
|
||||||
|
except
|
||||||
|
on e: Exception do
|
||||||
|
begin
|
||||||
|
ok1 := false;
|
||||||
|
e1 := e;
|
||||||
|
end;
|
||||||
|
end;
|
||||||
|
|
||||||
|
// --- Int32.Parse ---
|
||||||
|
try
|
||||||
|
r2 := System.Int32.Parse(s);
|
||||||
|
except
|
||||||
|
on e: Exception do
|
||||||
|
begin
|
||||||
|
ok2 := false;
|
||||||
|
e2 := e;
|
||||||
|
end;
|
||||||
|
end;
|
||||||
|
|
||||||
|
// --- сравнение ---
|
||||||
|
|
||||||
|
if ok1 and ok2 then
|
||||||
|
begin
|
||||||
|
if r1 <> r2 then
|
||||||
|
raise new Exception($'FAIL value: "{s}" → {r1} vs {r2}');
|
||||||
|
end
|
||||||
|
else if (not ok1) and (not ok2) then
|
||||||
|
begin
|
||||||
|
if e1.GetType <> e2.GetType then
|
||||||
|
raise new Exception($'FAIL exception: "{s}" → {e1.GetType} vs {e2.GetType}');
|
||||||
|
end
|
||||||
|
else
|
||||||
|
begin
|
||||||
|
if ok1 then
|
||||||
|
raise new Exception($'FAIL mismatch: "{s}" → StrToInt={r1}, Parse throws {e2.GetType}')
|
||||||
|
else
|
||||||
|
raise new Exception($'FAIL mismatch: "{s}" → StrToInt throws {e1.GetType}, Parse={r2}');
|
||||||
|
end;
|
||||||
|
end;
|
||||||
|
|
||||||
|
Println('All tests matched Int32.Parse');
|
||||||
|
end.
|
||||||
Loading…
Reference in a new issue