|
| 1 | +use { |
| 2 | + crate::{ |
| 3 | + accounts::PriceAccount, |
| 4 | + processor::c_upd_aggregate, |
| 5 | + }, |
| 6 | + bytemuck::Zeroable, |
| 7 | + serde::{ |
| 8 | + Deserialize, |
| 9 | + Serialize, |
| 10 | + }, |
| 11 | + std::fs::File, |
| 12 | +}; |
| 13 | +extern crate test_generator; |
| 14 | + |
| 15 | +use test_generator::test_resources; |
| 16 | + |
| 17 | +#[test_resources("program/rust/test_data/aggregation/*.json")] |
| 18 | +fn test_quote_set(input_path_raw: &str) { |
| 19 | + // For some reason these tests have a different working directory than the macro. |
| 20 | + let input_path = input_path_raw.replace("program/rust/", ""); |
| 21 | + let result_path = input_path.replace(".json", ".result"); |
| 22 | + |
| 23 | + let file = File::open(input_path).expect("Test file not found"); |
| 24 | + let quote_set: QuoteSet = serde_json::from_reader(&file).expect("Unable to parse JSON"); |
| 25 | + |
| 26 | + let result_file = File::open(result_path).expect("Test file not found"); |
| 27 | + let expected_result: QuoteSetResult = |
| 28 | + serde_json::from_reader(&result_file).expect("Unable to parse JSON"); |
| 29 | + |
| 30 | + let current_slot = 1000; // arbitrary |
| 31 | + let current_timestamp = 1234; // also arbitrary |
| 32 | + let mut price_account: PriceAccount = PriceAccount::zeroed(); |
| 33 | + |
| 34 | + price_account.last_slot_ = current_slot; |
| 35 | + price_account.agg_.pub_slot_ = current_slot; |
| 36 | + price_account.exponent = quote_set.exponent; |
| 37 | + price_account.num_ = quote_set.quotes.len() as u32; |
| 38 | + for quote_idx in 0..quote_set.quotes.len() { |
| 39 | + let mut current_component = &mut price_account.comp_[quote_idx]; |
| 40 | + let quote = "e_set.quotes[quote_idx]; |
| 41 | + current_component.latest_.status_ = quote.status; |
| 42 | + current_component.latest_.price_ = quote.price; |
| 43 | + current_component.latest_.conf_ = quote.conf; |
| 44 | + let slot_diff = quote.slot_diff.unwrap_or(0); |
| 45 | + assert!(slot_diff > -(current_slot as i64)); |
| 46 | + current_component.latest_.pub_slot_ = ((current_slot as i64) + slot_diff) as u64; |
| 47 | + } |
| 48 | + |
| 49 | + unsafe { |
| 50 | + c_upd_aggregate( |
| 51 | + (&mut price_account as *mut PriceAccount) as *mut u8, |
| 52 | + current_slot + 1, |
| 53 | + current_timestamp, |
| 54 | + ); |
| 55 | + } |
| 56 | + |
| 57 | + // For some idiotic reason the status in the input is a number and the output is a string. |
| 58 | + let result_status: String = match price_account.agg_.status_ { |
| 59 | + 0 => "unknown", |
| 60 | + 1 => "trading", |
| 61 | + 2 => "halted", |
| 62 | + 3 => "auction", |
| 63 | + 4 => "ignored", |
| 64 | + _ => "invalid status", |
| 65 | + } |
| 66 | + .into(); |
| 67 | + |
| 68 | + let actual_result = QuoteSetResult { |
| 69 | + exponent: price_account.exponent, |
| 70 | + price: price_account.agg_.price_, |
| 71 | + conf: price_account.agg_.conf_, |
| 72 | + status: result_status, |
| 73 | + }; |
| 74 | + |
| 75 | + assert_eq!(expected_result, actual_result); |
| 76 | +} |
| 77 | + |
| 78 | +#[derive(Serialize, Deserialize, Debug)] |
| 79 | +struct Quote { |
| 80 | + price: i64, |
| 81 | + conf: u64, |
| 82 | + status: u32, |
| 83 | + slot_diff: Option<i64>, |
| 84 | +} |
| 85 | + |
| 86 | +#[derive(Serialize, Deserialize, Debug)] |
| 87 | +struct QuoteSet { |
| 88 | + exponent: i32, |
| 89 | + quotes: Vec<Quote>, |
| 90 | +} |
| 91 | + |
| 92 | +#[derive(Serialize, Deserialize, Debug, PartialEq)] |
| 93 | +struct QuoteSetResult { |
| 94 | + exponent: i32, |
| 95 | + price: i64, |
| 96 | + conf: u64, |
| 97 | + status: String, |
| 98 | +} |
0 commit comments