-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtest_helper.rs
165 lines (135 loc) · 3.38 KB
/
test_helper.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
use lazy_static::lazy_static;
use std::collections::HashMap;
use std::fs;
use std::sync::Once;
use serde::{Deserialize, Serialize};
// use serde_json::{Map, Value};
use super::{Close, High, Low, Open, Volume};
#[derive(Debug, PartialEq)]
pub struct Bar {
open: f64,
high: f64,
low: f64,
close: f64,
volume: f64,
}
impl Bar {
pub fn new() -> Self {
Self {
open: 0.0,
close: 0.0,
low: 0.0,
high: 0.0,
volume: 0.0,
}
}
//pub fn open<T: Into<f64>>(mut self, val :T ) -> Self {
// self.open = val.into();
// self
//}
pub fn high<T: Into<f64>>(mut self, val: T) -> Self {
self.high = val.into();
self
}
pub fn low<T: Into<f64>>(mut self, val: T) -> Self {
self.low = val.into();
self
}
pub fn close<T: Into<f64>>(mut self, val: T) -> Self {
self.close = val.into();
self
}
pub fn volume(mut self, val: f64) -> Self {
self.volume = val;
self
}
}
impl Open for Bar {
fn open(&self) -> f64 {
self.open
}
}
impl Close for Bar {
fn close(&self) -> f64 {
self.close
}
}
impl Low for Bar {
fn low(&self) -> f64 {
self.low
}
}
impl High for Bar {
fn high(&self) -> f64 {
self.high
}
}
impl Volume for Bar {
fn volume(&self) -> f64 {
self.volume
}
}
pub fn round(num: f64) -> f64 {
(num * 1000.0).round() / 1000.00
}
// macro_rules! test_indicator {
// ($i:tt) => {
// #[test]
// fn test_indicator() {
// initialize();
// let bar = Bar::new();
// // ensure Default trait is implemented
// let mut indicator = $i::default();
// // ensure Next<f64> is implemented
// let first_output = indicator.next(12.3);
// // ensure next accepts &DataItem as well
// indicator.next(&bar);
// // ensure Reset is implemented and works correctly
// indicator.reset();
// assert_eq!(indicator.next(12.3), first_output);
// // ensure Display is implemented
// format!("{}", indicator);
// }
// };
// }
// cargo test -- --nocapture
static INIT: Once = Once::new();
#[derive(Debug, Serialize, Deserialize)]
pub struct Slot {
pub input: u32,
pub options: u32,
pub output: u32,
}
#[derive(Debug, Serialize, Deserialize)]
pub struct Test {
pub options: Vec<f64>,
pub inputs: Vec<Vec<f64>>,
pub outputs: Vec<Vec<f64>>,
}
#[derive(Debug, Serialize, Deserialize)]
pub struct TestIndicator {
pub name: String,
pub description: String,
#[serde(rename = "type")]
pub indicatortype: String,
pub slots: Slot,
pub options: String,
pub tests: Vec<Test>,
}
lazy_static! {
pub static ref TESTS: HashMap<String, TestIndicator> = {
// println!(":::::::::::::::::::::::: INIT ::::::::::::::::::::::::");
let contents = fs::read_to_string("tests/data.json")
.expect("Something went wrong reading the file");
serde_json::from_str(&contents).unwrap()
};
}
pub fn initialize() {
INIT.call_once(|| {
// initialization code here
if TESTS.len() == 0 {
println!("Tests uninitialized")
// println!("With text: {}", TESTS["abs"].name);
}
});
}