Skip to content

Commit ba79c93

Browse files
committed
add proptest and fix 0 case
1 parent 544abaa commit ba79c93

File tree

2 files changed

+31
-1
lines changed

2 files changed

+31
-1
lines changed

postgres-protocol/Cargo.toml

+3
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,6 @@ rand = "0.9"
2424
sha2 = "0.10"
2525
stringprep = "0.1"
2626
getrandom = { version = "0.3", optional = true }
27+
28+
[dev-dependencies]
29+
proptest = "1.6.0"

postgres-protocol/src/types/numeric.rs

+28-1
Original file line numberDiff line numberDiff line change
@@ -261,7 +261,7 @@ impl FromStr for Numeric {
261261
integer.pop_front();
262262
}
263263

264-
let mut weight = if integer.is_empty() || integer == b"0" {
264+
let mut weight = if integer.is_empty() {
265265
-1
266266
} else {
267267
integer.len().div_ceil(4) as i16 - 1
@@ -365,6 +365,15 @@ mod test {
365365
#[test]
366366
fn test_string_deserialization_and_serialization() {
367367
let cases = &[
368+
(
369+
"0",
370+
Numeric {
371+
sign: NumericSign::Positive,
372+
scale: 0,
373+
weight: 0,
374+
digits: vec![],
375+
},
376+
),
368377
(
369378
"1",
370379
Numeric {
@@ -810,4 +819,22 @@ mod test {
810819
assert_eq!(num.to_string(), *str, "{e} back to string");
811820
}
812821
}
822+
823+
use proptest::prelude::*;
824+
proptest! {
825+
#[test]
826+
fn test_arbitrary_f64_from_string_and_back(value in any::<f64>()) {
827+
let prop_val = value.to_string();
828+
let numeric = Numeric::from_str(&prop_val).expect("parse numeric");
829+
let str_val = numeric.to_string();
830+
assert_eq!(prop_val, str_val, "proprty test value {value}");
831+
}
832+
#[test]
833+
fn test_arbitrary_i64_from_string_and_back(value in any::<i64>()) {
834+
let prop_val = value.to_string();
835+
let numeric = Numeric::from_str(&prop_val).expect("parse numeric");
836+
let str_val = numeric.to_string();
837+
assert_eq!(prop_val, str_val, "proprty test value {value}");
838+
}
839+
}
813840
}

0 commit comments

Comments
 (0)