Skip to content

Commit 74c6366

Browse files
committed
fix tests
1 parent 796a762 commit 74c6366

File tree

1 file changed

+25
-10
lines changed

1 file changed

+25
-10
lines changed

program/rust/src/tests/test_twap.rs

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

22+
pub const TEST_MAX_LATENCY: u8 = 5;
23+
2224
#[derive(Clone, Debug, Copy)]
2325
pub struct DataEvent {
2426
price: i64,
@@ -34,7 +36,7 @@ impl Arbitrary for DataEvent {
3436
* has been a succesful aggregation */
3537
price: i64::arbitrary(g),
3638
conf: u64::arbitrary(g),
37-
max_latency: u8::arbitrary(g),
39+
max_latency: 0,
3840
}
3941
}
4042
}
@@ -94,12 +96,18 @@ impl PriceCumulative {
9496
}
9597
pub fn check_num_down_slots(&self, data: &[DataEvent]) {
9698
assert_eq!(
97-
data.iter()
98-
.fold(0, |acc, x| if x.slot_gap > PC_MAX_SEND_LATENCY.into() {
99-
acc + (x.slot_gap - PC_MAX_SEND_LATENCY as u64)
99+
data.iter().fold(0, |acc, x| {
100+
let latency_threshold = if x.max_latency == 0 {
101+
PC_MAX_SEND_LATENCY.into()
102+
} else {
103+
x.max_latency as u64
104+
};
105+
if x.slot_gap > latency_threshold {
106+
acc + (x.slot_gap - latency_threshold)
100107
} else {
101108
acc
102-
}),
109+
}
110+
}),
103111
self.num_down_slots
104112
);
105113
}
@@ -122,19 +130,19 @@ fn test_twap_unit() {
122130
price: 1,
123131
conf: 2,
124132
slot_gap: 4,
125-
max_latency: 1,
133+
max_latency: TEST_MAX_LATENCY,
126134
},
127135
DataEvent {
128136
price: i64::MAX,
129137
conf: u64::MAX,
130138
slot_gap: 1,
131-
max_latency: 2,
139+
max_latency: TEST_MAX_LATENCY,
132140
},
133141
DataEvent {
134142
price: -10,
135143
conf: 4,
136144
slot_gap: 30,
137-
max_latency: 3,
145+
max_latency: TEST_MAX_LATENCY,
138146
},
139147
];
140148

@@ -168,7 +176,9 @@ fn test_twap_unit() {
168176
);
169177
assert_eq!(price_cumulative.price, 9_223_372_036_854_775_512i128);
170178
assert_eq!(price_cumulative.conf, 18_446_744_073_709_551_745u128);
171-
assert_eq!(price_cumulative.num_down_slots, 8);
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);
172182
assert_eq!(price_cumulative.unused, 0);
173183

174184
let mut price_cumulative_overflow = PriceCumulative {
@@ -188,7 +198,12 @@ fn test_twap_unit() {
188198
);
189199
assert_eq!(
190200
price_cumulative_overflow.num_down_slots,
191-
u64::MAX - PC_MAX_SEND_LATENCY as u64
201+
u64::MAX
202+
- if TEST_MAX_LATENCY == 0 {
203+
u64::from(PC_MAX_SEND_LATENCY)
204+
} else {
205+
u64::from(u8::MAX)
206+
}
192207
);
193208
assert_eq!(price_cumulative_overflow.unused, 0);
194209
}

0 commit comments

Comments
 (0)