You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

79 lines
2.1KB

  1. #![feature(exclusive_range_pattern)]
  2. pub mod windows;
  3. pub mod encoding;
  4. #[allow(unused)]
  5. #[cfg(test)]
  6. mod tests {
  7. use serde::Deserialize;
  8. #[derive(Debug, Deserialize)]
  9. struct Trade {
  10. pub time: i64,
  11. pub price: f64,
  12. pub amount: f64,
  13. }
  14. #[test]
  15. fn serde_deserialize_json_example() {
  16. assert!(matches!(
  17. serde_json::from_str::<Trade>(r#"{"time":1527811201900505632,"price":7492.279785,"amount":0.048495,"exch":"bits","server_time":0,"side":null}"#),
  18. Ok(Trade { time: 1527811201900505632, price: 7492.279785, amount: 0.048495 })
  19. ));
  20. }
  21. #[test]
  22. fn serde_deserialize_csv_example() {
  23. let csv = "time,amount,exch,price,server_time,side\n\
  24. 1527811201900505632,0.048495,bits,7492.279785,0,";
  25. let mut csv_reader = csv::Reader::from_reader(csv.as_bytes());
  26. let headers = csv_reader
  27. .headers()
  28. .expect("parsing row headers failed")
  29. .clone();
  30. let mut row = csv::StringRecord::new();
  31. assert!(matches!(
  32. csv_reader.read_record(&mut row),
  33. Ok(true)
  34. ));
  35. assert!(matches!(
  36. row.deserialize(Some(&headers)),
  37. Ok(Trade { time: 1527811201900505632, price: 7492.279785, amount: 0.048495 })
  38. ));
  39. assert!(matches!(
  40. csv_reader.read_record(&mut row),
  41. Ok(false)
  42. ));
  43. }
  44. #[test]
  45. fn memory_size_of_trades_struct() {
  46. // tests an example in one of the baselines articles showing how with
  47. // no particular effort this struct is 48 bytes, compared to the
  48. // CSV per-row representation of an average of 73 bytes
  49. use markets::crypto::{Exchange, Ticker, Side};
  50. struct Trade {
  51. pub time: u64,
  52. pub price: f64,
  53. pub amount: f64,
  54. pub exch: Exchange,
  55. pub ticker: Ticker,
  56. pub server_time: Option<u64>,
  57. pub side: Option<Side>,
  58. }
  59. assert_eq!(std::mem::size_of::<Trade>(), 48);
  60. assert_eq!(std::mem::size_of::<Ticker>(), 2);
  61. }
  62. }