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
use alloc::string::String;
use core::{convert::Into, default::Default, option::Option, time::Duration};
use serde::{Deserialize, Serialize};
use crate::{
bolts::serdeany::SerdeAnyMap,
inputs::{HasLen, Input},
state::HasMetadata,
Error,
};
#[derive(Default, Serialize, Deserialize, Clone, Debug)]
#[serde(bound = "I: serde::de::DeserializeOwned")]
pub struct Testcase<I>
where
I: Input,
{
input: Option<I>,
filename: Option<String>,
metadata: SerdeAnyMap,
exec_time: Option<Duration>,
cached_len: Option<usize>,
}
impl<I> HasMetadata for Testcase<I>
where
I: Input,
{
#[inline]
fn metadata(&self) -> &SerdeAnyMap {
&self.metadata
}
#[inline]
fn metadata_mut(&mut self) -> &mut SerdeAnyMap {
&mut self.metadata
}
}
impl<I> Testcase<I>
where
I: Input,
{
pub fn load_input(&mut self) -> Result<&I, Error> {
if self.input.is_none() {
self.input = Some(I::from_file(self.filename.as_ref().unwrap())?);
}
Ok(self.input.as_ref().unwrap())
}
pub fn store_input(&mut self) -> Result<bool, Error> {
let fname;
match self.filename() {
Some(f) => {
fname = f.clone();
}
None => {
return Ok(false);
}
};
match self.input_mut() {
None => Ok(false),
Some(i) => {
i.to_file(fname)?;
Ok(true)
}
}
}
#[inline]
pub fn input(&self) -> &Option<I> {
&self.input
}
#[inline]
pub fn input_mut(&mut self) -> &mut Option<I> {
&mut self.input
}
#[inline]
pub fn set_input(&mut self, input: I) {
self.input = Some(input);
}
#[inline]
pub fn filename(&self) -> &Option<String> {
&self.filename
}
#[inline]
pub fn filename_mut(&mut self) -> &mut Option<String> {
&mut self.filename
}
#[inline]
pub fn set_filename(&mut self, filename: String) {
self.filename = Some(filename);
}
pub fn exec_time(&self) -> &Option<Duration> {
&self.exec_time
}
pub fn exec_time_mut(&mut self) -> &mut Option<Duration> {
&mut self.exec_time
}
#[inline]
pub fn new<T>(input: T) -> Self
where
T: Into<I>,
{
Testcase {
input: Some(input.into()),
filename: None,
metadata: SerdeAnyMap::new(),
exec_time: None,
cached_len: None,
}
}
#[inline]
pub fn with_filename(input: I, filename: String) -> Self {
Testcase {
input: Some(input),
filename: Some(filename),
metadata: SerdeAnyMap::new(),
exec_time: None,
cached_len: None,
}
}
#[must_use]
#[inline]
pub fn default() -> Self {
Testcase {
input: None,
filename: None,
metadata: SerdeAnyMap::new(),
exec_time: None,
cached_len: None,
}
}
}
impl<I> Testcase<I>
where
I: Input + HasLen,
{
#[inline]
pub fn cached_len(&mut self) -> Result<usize, Error> {
Ok(match &self.input {
Some(i) => {
let l = i.len();
self.cached_len = Some(l);
l
}
None => {
if let Some(l) = self.cached_len {
l
} else {
let l = self.load_input()?.len();
self.cached_len = Some(l);
l
}
}
})
}
}
impl<I> From<I> for Testcase<I>
where
I: Input,
{
fn from(input: I) -> Self {
Testcase::new(input)
}
}