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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
use crate::{
ebi_framework::{
ebi_file_handler::{EBI_FILE_HANDLERS, EbiFileHandler},
ebi_input::{self, EbiInputType},
ebi_trait::EbiTrait,
},
multiple_reader::MultipleReader,
};
use ebi_objects::{EbiObject, ebi_arithmetic::Fraction};
use itertools::Itertools;
use std::{
fmt::Debug,
fs::{self, File},
path::PathBuf,
};
#[allow(unused)]
#[derive(Clone)]
pub(crate) enum TestInput {
Trait(EbiTrait, PathBuf), //a trait cannot be cloned, thus we must parse it every time in the cartesian product
Object(EbiObject, &'static EbiFileHandler, PathBuf),
String(String, &'static EbiInputType),
Usize(usize, &'static EbiInputType),
FileHandler(EbiFileHandler),
Fraction(Fraction, &'static EbiInputType),
}
impl TestInput {
#[cfg(feature = "python")]
pub(crate) fn to_ebi_input(self) -> EbiInput {
match self {
TestInput::Trait(etrait, file) => {
use crate::{
ebi_framework::ebi_input::{self, EbiInput},
multiple_reader::MultipleReader,
};
use std::fs::File;
let mut reader = MultipleReader::from_file(File::open(file).unwrap());
match ebi_input::read_as_trait(&etrait, &mut reader, None, 0) {
Ok((object, file_handler)) => EbiInput::Trait(object, file_handler),
Err(_) => panic!(),
}
}
TestInput::Object(o, fh, _) => EbiInput::Object(o, fh),
TestInput::String(s, input_type) => EbiInput::String(s, input_type),
TestInput::Usize(u, input_type) => EbiInput::Usize(u, input_type),
TestInput::FileHandler(fh) => EbiInput::FileHandler(fh),
TestInput::Fraction(f, input_type) => EbiInput::Fraction(f, input_type),
}
}
// #[cfg(test)]
// pub(crate) fn get_input_type(&self) -> Option<&'static EbiInputType> {
// match self {
// TestInput::Trait(_, _) => None,
// TestInput::Object(_, _, _) => None,
// TestInput::String(_, ebi_input_type) => Some(ebi_input_type),
// TestInput::Usize(_, ebi_input_type) => Some(ebi_input_type),
// TestInput::FileHandler(_) => None,
// TestInput::Fraction(_, ebi_input_type) => Some(ebi_input_type),
// }
// }
pub fn to_unique_string(&self) -> String {
match self {
TestInput::Trait(ebi_trait, path_buf) => {
format!("trait {}#{}", ebi_trait, path_buf.to_str().unwrap())
}
TestInput::Object(ebi_object, _, path_buf) => {
format!(
"object {}#{}",
ebi_object.get_type(),
path_buf.to_str().unwrap()
)
}
TestInput::String(s, _) => format!("string {s}"),
TestInput::Usize(u, _) => format!("usize {u}"),
TestInput::FileHandler(ebi_file_handler) => format!("filehandler {ebi_file_handler}"),
TestInput::Fraction(f, _) => format!("fraction {f}"),
}
}
// pub fn from_unique_string(string: &str, input_types: &[&'static EbiInputType]) -> Result<Self> {
// let (variant, value) = string
// .split_once(' ')
// .ok_or_else(|| anyhow!("not a valid testinput {}", string))?;
// match variant {
// "trait" => {
// let (trait_name, path) = value
// .split_once('#')
// .ok_or_else(|| anyhow!("not a valid testinput {}", string))?;
// let path_buf = path.parse::<PathBuf>()?;
// let ebi_trait = trait_name.parse::<EbiTrait>()?;
// Ok(Self::Trait(ebi_trait, path_buf))
// }
// "object" => {
// let (_object_name, path) = value
// .split_once('#')
// .ok_or_else(|| anyhow!("not a valid testinput {}", string))?;
// let path_buf = path.parse::<PathBuf>()?;
// let reader = MultipleReader::File(File::open(path)?);
// if let Ok(EbiInput::Object(ebi_object, file_handler)) =
// attempt_parse(input_types, reader)
// {
// Ok(Self::Object(ebi_object, file_handler, path_buf))
// } else {
// Err(anyhow!("not a valid testinput {}", string))
// }
// }
// "string" => {
// for input_type in input_types {
// if let EbiInputType::String(allowed_values, _) = input_type {
// if let Some(allowed_values) = allowed_values
// && allowed_values.contains(&value)
// {
// return Ok(TestInput::String(value.to_string(), input_type));
// } else if allowed_values.is_none() {
// return Ok(TestInput::String(value.to_string(), input_type));
// }
// }
// }
// Err(anyhow!("not a valid testinput {}", string))
// }
// "usize" => {
// let reader = MultipleReader::String(value.to_string());
// if let Ok(EbiInput::Usize(val, file_handler)) = attempt_parse(input_types, reader) {
// Ok(Self::Usize(val, file_handler))
// } else {
// Err(anyhow!("not a valid testinput {}", string))
// }
// }
// "filehandler" => {
// let reader = MultipleReader::String(value.to_string());
// if let Ok(EbiInput::FileHandler(file_handler)) = attempt_parse(input_types, reader)
// {
// Ok(Self::FileHandler(file_handler))
// } else {
// Err(anyhow!("not a valid testinput {}", string))
// }
// }
// "fraction" => {
// let reader = MultipleReader::String(value.to_string());
// if let Ok(EbiInput::Fraction(val, file_handler)) =
// attempt_parse(input_types, reader)
// {
// Ok(Self::Fraction(val, file_handler))
// } else {
// Err(anyhow!("not a valid testinput {}", string))
// }
// }
// _ => Err(anyhow!("not a valid testinput {}", string)),
// }
// }
pub(crate) fn find_inputs(input_typess: &[&[&'static EbiInputType]]) -> Vec<Vec<TestInput>> {
let mut it = input_typess.iter();
let mut result = if let Some(input_types) = it.next() {
Self::find_inputs_for_position(input_types)
.into_iter()
.map(|x| vec![x])
.collect()
} else {
vec![]
};
while let Some(input_types) = it.next() {
let add = Self::find_inputs_for_position(input_types);
result = result
.into_iter()
.cartesian_product(add.into_iter())
.map(|(mut xs, x)| {
xs.push(x);
xs
})
.collect();
}
return result;
}
fn find_inputs_for_position(input_types: &[&'static EbiInputType]) -> Vec<TestInput> {
let mut result = vec![];
for input_type in input_types {
match input_type {
EbiInputType::FileHandler => {
result.append(
&mut EBI_FILE_HANDLERS
.iter()
.map(|f| TestInput::FileHandler(f.clone()))
.collect::<Vec<_>>(),
);
}
EbiInputType::Fraction(_, _, Some(default)) => {
result.push(TestInput::Fraction(default.to_fraction(), &input_type));
}
EbiInputType::Fraction(Some(min), _, _) => {
result.push(TestInput::Fraction(min.to_fraction(), &input_type));
}
EbiInputType::Fraction(_, Some(max), _) => {
result.push(TestInput::Fraction(max.to_fraction(), &input_type));
}
EbiInputType::Fraction(_, _, _) => {
result.push(TestInput::Fraction(Fraction::from((1, 2)), &input_type));
}
EbiInputType::String(_, Some(default)) => {
result.push(TestInput::String(default.to_string(), &input_type));
}
EbiInputType::String(Some(allowed_values), None) => {
result.push(TestInput::String(
allowed_values[0].to_string(),
&input_type,
));
}
EbiInputType::String(None, None) => {
result.push(TestInput::String("some string".to_string(), &input_type));
}
EbiInputType::Usize(_, _, Some(default)) => {
//to keep testing feasible, do not use a default more than 10
result.push(TestInput::Usize(*default.min(&10), &input_type));
}
EbiInputType::Usize(Some(min), _, _) => {
result.push(TestInput::Usize(*min, &input_type));
}
EbiInputType::Usize(_, Some(max), _) => {
result.push(TestInput::Usize(*max, &input_type));
}
EbiInputType::Usize(_, _, _) => {
result.push(TestInput::Usize(10, &input_type));
}
_ => {
//look through all files
let files = fs::read_dir("./testfiles").unwrap();
for path in files {
let file = path.unwrap();
//for now, we skip files with loops and livelocks, as the techniques cannot handle them yet
if !file.file_name().into_string().unwrap().contains("livelock")
&& !file.file_name().into_string().unwrap().contains("infinite")
&& !file
.file_name()
.into_string()
.unwrap()
.contains("unbounded")
&& !file.file_name().into_string().unwrap().contains("loop")
&& !file.file_name().into_string().unwrap().contains("pdc")
&& !file
.file_name()
.into_string()
.unwrap()
.contains("irregular")
{
let mut reader =
MultipleReader::from_file(File::open(file.path()).unwrap());
match input_type {
EbiInputType::Trait(etrait) => {
//try to parse a trait
match ebi_input::read_as_trait(etrait, &mut reader, None, 0) {
Ok((_, _)) => {
result.push(TestInput::Trait(*etrait, file.path()));
}
Err(_) => {}
}
}
EbiInputType::Object(etype) => {
//try to parse a specific object
match ebi_input::read_as_object(etype, &mut reader, None, 0) {
Ok((object, file_handler)) => {
result.push(TestInput::Object(
object,
file_handler,
file.path(),
));
}
Err(_) => {}
}
}
EbiInputType::AnyObject => {
match ebi_input::read_as_any_object(&mut reader, None, 0) {
Ok((object, file_handler)) => {
result.push(TestInput::Object(
object,
file_handler,
file.path(),
));
}
Err(_) => {}
}
}
_ => {}
}
}
}
}
}
}
result
}
}
impl Debug for TestInput {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::Trait(arg0, arg1) => f.debug_tuple("Trait").field(arg0).field(arg1).finish(),
Self::Object(_, _, arg2) => f.debug_tuple("Object").field(arg2).finish(),
Self::String(arg0, _) => f.debug_tuple("String").field(arg0).finish(),
Self::Usize(arg0, _) => f.debug_tuple("Usize").field(arg0).finish(),
Self::FileHandler(arg0) => f.debug_tuple("FileHandler").field(&arg0.name).finish(),
Self::Fraction(arg0, _) => f.debug_tuple("Fraction").field(arg0).finish(),
}
}
}
impl Ord for TestInput {
fn cmp(&self, other: &Self) -> std::cmp::Ordering {
self.to_unique_string().cmp(&other.to_unique_string())
}
}
impl PartialOrd for TestInput {
fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
Some(self.cmp(other))
}
}
impl Eq for TestInput {}
impl PartialEq for TestInput {
fn eq(&self, other: &Self) -> bool {
match (self, other) {
(Self::Trait(l0, l1), Self::Trait(r0, r1)) => l0 == r0 && l1 == r1,
(Self::Object(_, l1, l2), Self::Object(_, r1, r2)) => l1 == r1 && l2 == r2,
(Self::String(l0, l1), Self::String(r0, r1)) => l0 == r0 && l1 == r1,
(Self::Usize(l0, l1), Self::Usize(r0, r1)) => l0 == r0 && l1 == r1,
(Self::FileHandler(l0), Self::FileHandler(r0)) => l0 == r0,
(Self::Fraction(l0, l1), Self::Fraction(r0, r1)) => l0 == r0 && l1 == r1,
_ => false,
}
}
}