mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2024-09-11 13:36:13 +03:00
LibCrypto: Parse negative input correctly in BigFraction::from_string()
Previously, when calling `BigFraction::from_string()`, the fractional part of the number was always treated as positive. This led to an incorrect result if the input string was negative.
This commit is contained in:
parent
e0fd5beb36
commit
d545fb2b60
Notes:
sideshowbarker
2024-07-17 06:40:21 +09:00
Author: https://github.com/tcl3 Commit: https://github.com/SerenityOS/serenity/commit/d545fb2b60 Pull-request: https://github.com/SerenityOS/serenity/pull/22747 Reviewed-by: https://github.com/gmta ✅
@ -1,6 +1,7 @@
|
||||
set(TEST_SOURCES
|
||||
TestAES.cpp
|
||||
TestASN1.cpp
|
||||
TestBigFraction.cpp
|
||||
TestBigInteger.cpp
|
||||
TestChecksum.cpp
|
||||
TestChaCha20.cpp
|
||||
|
28
Tests/LibCrypto/TestBigFraction.cpp
Normal file
28
Tests/LibCrypto/TestBigFraction.cpp
Normal file
@ -0,0 +1,28 @@
|
||||
/*
|
||||
* Copyright (c) 2024, Tim Ledbetter <timledbetter@gmail.com>
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*/
|
||||
|
||||
#include <LibCrypto/BigFraction/BigFraction.h>
|
||||
#include <LibTest/TestCase.h>
|
||||
|
||||
TEST_CASE(roundtrip_from_string)
|
||||
{
|
||||
Array valid_number_strings {
|
||||
"0.1"sv,
|
||||
"-0.1"sv,
|
||||
"0.9"sv,
|
||||
"-0.9"sv,
|
||||
"1.2"sv,
|
||||
"-1.2"sv,
|
||||
"610888968122787804679.305596150292503043363"sv,
|
||||
"-610888968122787804679.305596150292503043363"sv
|
||||
};
|
||||
|
||||
for (auto valid_number_string : valid_number_strings) {
|
||||
auto result = TRY_OR_FAIL(Crypto::BigFraction::from_string(valid_number_string));
|
||||
auto precision = valid_number_string.length() - valid_number_string.find('.').value();
|
||||
EXPECT_EQ(result.to_byte_string(precision), valid_number_string);
|
||||
}
|
||||
}
|
@ -36,6 +36,9 @@ ErrorOr<BigFraction> BigFraction::from_string(StringView sv)
|
||||
auto fractional_part = TRY(SignedBigInteger::from_base(10, fraction_part_view));
|
||||
auto fraction_length = UnsignedBigInteger(static_cast<u64>(fraction_part_view.length()));
|
||||
|
||||
if (!sv.is_empty() && sv[0] == '-')
|
||||
fractional_part.negate();
|
||||
|
||||
return BigFraction(move(integer_part)) + BigFraction(move(fractional_part), NumberTheory::Power("10"_bigint, move(fraction_length)));
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user