From b9f6e31958a071b9f0091b54c6e840ff6699dd35 Mon Sep 17 00:00:00 2001 From: Mikhalkovich Stanislav Date: Wed, 29 Apr 2026 15:40:41 +0300 Subject: [PATCH] =?UTF-8?q?=D0=9D=D0=BE=D0=B2=D1=8B=D0=B5=20=D0=BF=D1=80?= =?UTF-8?q?=D0=B8=D0=BC=D0=B5=D1=80=D1=8B=20-=20=D1=82=D0=B5=D1=81=D1=82?= =?UTF-8?q?=D1=8B=20StrToInt?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../WhatsNew/3_12/a_test_StrToInt.pas | 96 +++++++++++++++++++ .../WhatsNew/3_12/a_test_StrToInt2.pas | 80 ++++++++++++++++ 2 files changed, 176 insertions(+) create mode 100644 InstallerSamples/WhatsNew/3_12/a_test_StrToInt.pas create mode 100644 InstallerSamples/WhatsNew/3_12/a_test_StrToInt2.pas diff --git a/InstallerSamples/WhatsNew/3_12/a_test_StrToInt.pas b/InstallerSamples/WhatsNew/3_12/a_test_StrToInt.pas new file mode 100644 index 000000000..6aacf0e6c --- /dev/null +++ b/InstallerSamples/WhatsNew/3_12/a_test_StrToInt.pas @@ -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. \ No newline at end of file diff --git a/InstallerSamples/WhatsNew/3_12/a_test_StrToInt2.pas b/InstallerSamples/WhatsNew/3_12/a_test_StrToInt2.pas new file mode 100644 index 000000000..e2e5683c6 --- /dev/null +++ b/InstallerSamples/WhatsNew/3_12/a_test_StrToInt2.pas @@ -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. \ No newline at end of file