Skip to content

Commit 6d6b787

Browse files
committed
test(lazer): add more Price and Rate tests
1 parent c11d8ab commit 6d6b787

File tree

3 files changed

+87
-5
lines changed

3 files changed

+87
-5
lines changed

lazer/sdk/rust/protocol/src/lib.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,11 @@ pub enum PriceFeedProperty {
6969
// More fields may be added later.
7070
}
7171

72+
// Operation and coefficient for converting value to mantissa.
7273
enum ExponentFactor {
74+
// mantissa = value * factor
7375
Mul(i64),
76+
// mantissa = value / factor
7477
Div(i64),
7578
}
7679

lazer/sdk/rust/protocol/src/price/tests.rs

Lines changed: 51 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ fn price_constructs() {
2020
}
2121

2222
#[test]
23-
fn price_constructs_negative_mantissa() {
23+
fn price_constructs_with_negative_mantissa() {
2424
let price = Price::parse_str("-42.68", -8).unwrap();
2525
assert_eq!(price.0.get(), -4_268_000_000);
2626
assert_float_absolute_eq!(price.to_f64(-8).unwrap(), -42.68);
@@ -39,7 +39,7 @@ fn price_constructs_negative_mantissa() {
3939
}
4040

4141
#[test]
42-
fn price_constructs_zero_exponent() {
42+
fn price_constructs_with_zero_exponent() {
4343
let price = Price::parse_str("42", 0).unwrap();
4444
assert_eq!(price.0.get(), 42);
4545
assert_float_absolute_eq!(price.to_f64(0).unwrap(), 42.);
@@ -57,19 +57,46 @@ fn price_constructs_zero_exponent() {
5757
assert_float_absolute_eq!(price4.to_f64(0).unwrap(), 42.);
5858
}
5959

60+
#[test]
61+
fn price_constructs_with_positive_exponent() {
62+
let price = Price::parse_str("42_680_000", 3).unwrap();
63+
assert_eq!(price.0.get(), 42_680);
64+
assert_float_absolute_eq!(price.to_f64(3).unwrap(), 42_680_000.);
65+
66+
let price2 = Price::from_integer(200_000, 3).unwrap();
67+
assert_eq!(price2.0.get(), 200);
68+
assert_float_absolute_eq!(price2.to_f64(3).unwrap(), 200_000.);
69+
70+
let price3 = Price::from_mantissa(123_456).unwrap();
71+
assert_eq!(price3.0.get(), 123_456);
72+
assert_float_absolute_eq!(price3.to_f64(3).unwrap(), 123_456_000.);
73+
74+
let price4 = Price::from_f64(42_680_000., 3).unwrap();
75+
assert_eq!(price4.0.get(), 42_680);
76+
assert_float_absolute_eq!(price4.to_f64(3).unwrap(), 42_680_000.);
77+
}
78+
6079
#[test]
6180
fn price_rejects_zero_mantissa() {
6281
Price::parse_str("0.0", -8).unwrap_err();
6382
Price::from_integer(0, -8).unwrap_err();
6483
Price::from_mantissa(0).unwrap_err();
6584
Price::from_f64(-0.0, -8).unwrap_err();
85+
86+
Price::parse_str("0.0", 8).unwrap_err();
87+
Price::from_integer(0, 8).unwrap_err();
88+
Price::from_f64(-0.0, 8).unwrap_err();
6689
}
6790

6891
#[test]
6992
fn price_rejects_too_precise() {
7093
Price::parse_str("42.68", 0).unwrap_err();
7194
Price::parse_str("42.68", -1).unwrap_err();
7295
Price::parse_str("42.68", -2).unwrap();
96+
97+
Price::parse_str("42_680", 3).unwrap_err();
98+
Price::parse_str("42_600", 3).unwrap_err();
99+
Price::parse_str("42_000", 3).unwrap();
73100
}
74101

75102
#[test]
@@ -84,6 +111,14 @@ fn price_ops() {
84111
.unwrap(),
85112
12.34 + 23.45
86113
);
114+
assert_float_absolute_eq!(
115+
price1
116+
.sub_with_same_mantissa(price2)
117+
.unwrap()
118+
.to_f64(-8)
119+
.unwrap(),
120+
12.34 - 23.45
121+
);
87122
assert_float_absolute_eq!(
88123
price1.mul_integer(2).unwrap().to_f64(-8).unwrap(),
89124
12.34 * 2.
@@ -97,4 +132,18 @@ fn price_ops() {
97132
price1.mul_decimal(3456, -2).unwrap().to_f64(-8).unwrap(),
98133
12.34 * 34.56
99134
);
135+
136+
let price2 = Price::parse_str("42_000", 3).unwrap();
137+
assert_float_absolute_eq!(
138+
price2.mul_integer(2).unwrap().to_f64(3).unwrap(),
139+
42_000. * 2.
140+
);
141+
assert_float_absolute_eq!(
142+
price2.div_integer(2).unwrap().to_f64(3).unwrap(),
143+
42_000. / 2.
144+
);
145+
assert_float_absolute_eq!(
146+
price2.mul_decimal(3456, -2).unwrap().to_f64(3).unwrap(),
147+
(42_000_f64 * 34.56 / 1000.).floor() * 1000.
148+
);
100149
}

lazer/sdk/rust/protocol/src/rate/tests.rs

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ fn rate_constructs() {
2020
}
2121

2222
#[test]
23-
fn rate_constructs_negative_mantissa() {
23+
fn rate_constructs_with_negative_mantissa() {
2424
let rate = Rate::parse_str("-42.68", -8).unwrap();
2525
assert_eq!(rate.0, -4_268_000_000);
2626
assert_float_absolute_eq!(rate.to_f64(-8).unwrap(), -42.68);
@@ -39,7 +39,7 @@ fn rate_constructs_negative_mantissa() {
3939
}
4040

4141
#[test]
42-
fn rate_constructs_zero_exponent() {
42+
fn rate_constructs_with_zero_exponent() {
4343
let rate = Rate::parse_str("42", 0).unwrap();
4444
assert_eq!(rate.0, 42);
4545
assert_float_absolute_eq!(rate.to_f64(0).unwrap(), 42.);
@@ -58,7 +58,7 @@ fn rate_constructs_zero_exponent() {
5858
}
5959

6060
#[test]
61-
fn rate_constructs_zero_mantissa() {
61+
fn rate_constructs_with_zero_mantissa() {
6262
let rate1 = Rate::parse_str("0.0", -8).unwrap();
6363
assert_eq!(rate1.0, 0);
6464
let rate2 = Rate::from_integer(0, -8).unwrap();
@@ -67,11 +67,41 @@ fn rate_constructs_zero_mantissa() {
6767
assert_eq!(rate3.0, 0);
6868
let rate4 = Rate::from_f64(-0.0, -8).unwrap();
6969
assert_eq!(rate4.0, 0);
70+
71+
let rate1 = Rate::parse_str("0.0", 8).unwrap();
72+
assert_eq!(rate1.0, 0);
73+
let rate2 = Rate::from_integer(0, 8).unwrap();
74+
assert_eq!(rate2.0, 0);
75+
let rate4 = Rate::from_f64(-0.0, 8).unwrap();
76+
assert_eq!(rate4.0, 0);
77+
}
78+
79+
#[test]
80+
fn rate_constructs_with_positive_exponent() {
81+
let rate = Rate::parse_str("42_680_000", 3).unwrap();
82+
assert_eq!(rate.0, 42_680);
83+
assert_float_absolute_eq!(rate.to_f64(3).unwrap(), 42_680_000.);
84+
85+
let rate2 = Rate::from_integer(200_000, 3).unwrap();
86+
assert_eq!(rate2.0, 200);
87+
assert_float_absolute_eq!(rate2.to_f64(3).unwrap(), 200_000.);
88+
89+
let rate3 = Rate::from_mantissa(123_456);
90+
assert_eq!(rate3.0, 123_456);
91+
assert_float_absolute_eq!(rate3.to_f64(3).unwrap(), 123_456_000.);
92+
93+
let rate4 = Rate::from_f64(42_680_000., 3).unwrap();
94+
assert_eq!(rate4.0, 42_680);
95+
assert_float_absolute_eq!(rate4.to_f64(3).unwrap(), 42_680_000.);
7096
}
7197

7298
#[test]
7399
fn rate_rejects_too_precise() {
74100
Rate::parse_str("42.68", 0).unwrap_err();
75101
Rate::parse_str("42.68", -1).unwrap_err();
76102
Rate::parse_str("42.68", -2).unwrap();
103+
104+
Rate::parse_str("42_680", 3).unwrap_err();
105+
Rate::parse_str("42_600", 3).unwrap_err();
106+
Rate::parse_str("42_000", 3).unwrap();
77107
}

0 commit comments

Comments
 (0)