Skip to content

Commit a189fbb

Browse files
committed
Refactor test_twap.rs to use random values for max_latency
1 parent 79cb509 commit a189fbb

File tree

1 file changed

+50
-18
lines changed

1 file changed

+50
-18
lines changed

program/rust/src/tests/test_twap.rs

Lines changed: 50 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,6 @@ use {
1919
solana_program::pubkey::Pubkey,
2020
};
2121

22-
pub const TEST_MAX_LATENCY: u8 = 5;
23-
2422
#[derive(Clone, Debug, Copy)]
2523
pub struct DataEvent {
2624
price: i64,
@@ -36,7 +34,7 @@ impl Arbitrary for DataEvent {
3634
* has been a succesful aggregation */
3735
price: i64::arbitrary(g),
3836
conf: u64::arbitrary(g),
39-
max_latency: 0,
37+
max_latency: u8::arbitrary(g),
4038
}
4139
}
4240
}
@@ -48,6 +46,7 @@ impl Arbitrary for DataEvent {
4846
/// - slot_gap is a random number between 1 and u8::MAX + 1 (256)
4947
/// - price is a random i64
5048
/// - conf is a random u64
49+
/// - max_latency is a random u8
5150
#[quickcheck]
5251
fn test_twap(input: Vec<DataEvent>) -> bool {
5352
let mut price_cumulative = PriceCumulative {
@@ -76,7 +75,6 @@ fn test_twap(input: Vec<DataEvent>) -> bool {
7675
true
7776
}
7877

79-
8078
impl PriceCumulative {
8179
pub fn check_price(&self, data: &[DataEvent]) {
8280
assert_eq!(
@@ -100,7 +98,7 @@ impl PriceCumulative {
10098
let latency_threshold = if x.max_latency == 0 {
10199
PC_MAX_SEND_LATENCY.into()
102100
} else {
103-
x.max_latency as u64
101+
x.max_latency.into()
104102
};
105103
if x.slot_gap > latency_threshold {
106104
acc + (x.slot_gap - latency_threshold)
@@ -130,19 +128,31 @@ fn test_twap_unit() {
130128
price: 1,
131129
conf: 2,
132130
slot_gap: 4,
133-
max_latency: TEST_MAX_LATENCY,
131+
max_latency: 0,
134132
},
135133
DataEvent {
136134
price: i64::MAX,
137135
conf: u64::MAX,
138136
slot_gap: 1,
139-
max_latency: TEST_MAX_LATENCY,
137+
max_latency: 0,
140138
},
141139
DataEvent {
142140
price: -10,
143141
conf: 4,
144142
slot_gap: 30,
145-
max_latency: TEST_MAX_LATENCY,
143+
max_latency: 0,
144+
},
145+
DataEvent {
146+
price: 1,
147+
conf: 2,
148+
slot_gap: 4,
149+
max_latency: 5,
150+
},
151+
DataEvent {
152+
price: 6,
153+
conf: 7,
154+
slot_gap: 8,
155+
max_latency: 5,
146156
},
147157
];
148158

@@ -176,9 +186,7 @@ fn test_twap_unit() {
176186
);
177187
assert_eq!(price_cumulative.price, 9_223_372_036_854_775_512i128);
178188
assert_eq!(price_cumulative.conf, 18_446_744_073_709_551_745u128);
179-
// self.num_down_slots + (30 - TEST_MAX_LATENCY), using saturating subtraction to avoid negative values
180-
// in this case, the result is 28 because TEST_MAX_LATENCY is 5 and self.num_down_slots is 3
181-
assert_eq!(price_cumulative.num_down_slots, 28);
189+
assert_eq!(price_cumulative.num_down_slots, 8);
182190
assert_eq!(price_cumulative.unused, 0);
183191

184192
let mut price_cumulative_overflow = PriceCumulative {
@@ -198,14 +206,39 @@ fn test_twap_unit() {
198206
);
199207
assert_eq!(
200208
price_cumulative_overflow.num_down_slots,
201-
u64::MAX
202-
- if TEST_MAX_LATENCY == 0 {
203-
u64::from(PC_MAX_SEND_LATENCY)
204-
} else {
205-
u64::from(u8::MAX)
206-
}
209+
u64::MAX - u64::from(u8::MAX)
207210
);
211+
// u64::MAX - u64::from(u8::MAX)
208212
assert_eq!(price_cumulative_overflow.unused, 0);
213+
214+
let mut price_cumulative_nonzero_max_latency = PriceCumulative {
215+
price: 1,
216+
conf: 2,
217+
num_down_slots: 3,
218+
unused: 0,
219+
};
220+
221+
price_cumulative_nonzero_max_latency.update(
222+
data[3].price,
223+
data[3].conf,
224+
data[3].slot_gap,
225+
data[3].max_latency,
226+
);
227+
assert_eq!(price_cumulative_nonzero_max_latency.price, 5);
228+
assert_eq!(price_cumulative_nonzero_max_latency.conf, 10);
229+
assert_eq!(price_cumulative_nonzero_max_latency.num_down_slots, 3);
230+
assert_eq!(price_cumulative_nonzero_max_latency.unused, 0);
231+
232+
price_cumulative_nonzero_max_latency.update(
233+
data[4].price,
234+
data[4].conf,
235+
data[4].slot_gap,
236+
data[4].max_latency,
237+
);
238+
assert_eq!(price_cumulative_nonzero_max_latency.price, 53);
239+
assert_eq!(price_cumulative_nonzero_max_latency.conf, 66);
240+
assert_eq!(price_cumulative_nonzero_max_latency.num_down_slots, 6);
241+
assert_eq!(price_cumulative_nonzero_max_latency.unused, 0);
209242
}
210243

211244
#[test]
@@ -264,7 +297,6 @@ fn test_twap_with_price_account() {
264297
Err(OracleError::NeedsSuccesfulAggregation)
265298
);
266299

267-
268300
assert_eq!(price_data.price_cumulative.price, 1 - 2 * 10);
269301
assert_eq!(price_data.price_cumulative.conf, 2 + 2 * 5);
270302
assert_eq!(price_data.price_cumulative.num_down_slots, 3);

0 commit comments

Comments
 (0)