-
Notifications
You must be signed in to change notification settings - Fork 66
/
tests.rs
299 lines (264 loc) · 7.36 KB
/
tests.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
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
extern crate log;
extern crate serde;
extern crate csv;
extern crate filetime;
extern crate quickcheck;
extern crate rand;
extern crate stats;
use std::{env, fmt, mem::transmute, ops};
use quickcheck::{Arbitrary, Gen, QuickCheck, Testable};
use rand::{thread_rng, Rng};
macro_rules! svec[
($($x:expr),*) => (
vec![$($x),*].into_iter()
.map(|s: &'static str| s.to_string())
.collect::<Vec<String>>()
);
($($x:expr,)*) => (svec![$($x),*]);
];
macro_rules! rassert_eq {
($given:expr, $expected:expr) => {{
assert_eq!($given, $expected);
true
}};
}
mod workdir;
mod test_100;
#[cfg(feature = "apply")]
mod test_apply;
#[cfg(feature = "datapusher_plus")]
mod test_applydp;
#[cfg(any(feature = "feature_capable", feature = "lite"))]
mod test_behead;
#[cfg(any(feature = "feature_capable", feature = "lite"))]
mod test_cat;
mod test_combos;
mod test_comments;
mod test_count;
mod test_datefmt;
mod test_dedup;
mod test_describegpt;
mod test_diff;
#[cfg(any(feature = "feature_capable", feature = "lite"))]
mod test_enumerate;
mod test_excel;
mod test_exclude;
#[cfg(any(feature = "feature_capable", feature = "lite"))]
mod test_explode;
mod test_extdedup;
#[cfg(any(feature = "feature_capable", feature = "lite"))]
mod test_extsort;
#[cfg(feature = "fetch")]
mod test_fetch;
#[cfg(any(feature = "feature_capable", feature = "lite"))]
mod test_fill;
#[cfg(any(feature = "feature_capable", feature = "lite"))]
mod test_fixlengths;
#[cfg(any(feature = "feature_capable", feature = "lite"))]
mod test_flatten;
#[cfg(any(feature = "feature_capable", feature = "lite"))]
mod test_fmt;
#[cfg(all(feature = "foreach"))]
mod test_foreach;
mod test_frequency;
#[cfg(all(feature = "feature_capable", feature = "geocode"))]
mod test_geocode;
mod test_headers;
mod test_index;
mod test_input;
#[cfg(any(feature = "feature_capable", feature = "lite"))]
mod test_join;
#[cfg(feature = "polars")]
mod test_joinp;
#[cfg(any(feature = "feature_capable", feature = "lite"))]
mod test_jsonl;
#[cfg(all(feature = "polars", not(feature = "datapusher_plus")))]
mod test_jsonp;
#[cfg(feature = "luau")]
mod test_luau;
#[cfg(any(feature = "feature_capable", feature = "lite"))]
mod test_partition;
mod test_prompt;
mod test_pseudo;
#[cfg(feature = "python")]
mod test_py;
mod test_rename;
mod test_replace;
mod test_reverse;
mod test_safenames;
mod test_sample;
#[cfg(any(feature = "feature_capable", feature = "lite"))]
mod test_schema;
mod test_search;
mod test_searchset;
mod test_select;
mod test_slice;
mod test_snappy;
mod test_sniff;
mod test_sort;
mod test_sortcheck;
#[cfg(any(feature = "feature_capable", feature = "lite"))]
mod test_split;
#[cfg(feature = "polars")]
mod test_sqlp;
mod test_stats;
#[cfg(any(feature = "feature_capable", feature = "lite"))]
mod test_table;
#[cfg(all(feature = "to", feature = "feature_capable"))]
mod test_to;
#[cfg(any(feature = "feature_capable", feature = "lite"))]
mod test_tojsonl;
#[cfg(any(feature = "feature_capable", feature = "lite"))]
mod test_transpose;
mod test_validate;
fn qcheck<T: Testable>(p: T) {
env::set_var("QSV_SKIPUTF8_CHECK", "1");
QuickCheck::new().gen(Gen::new(5)).quickcheck(p);
env::set_var("QSV_SKIPUTF8_CHECK", "");
}
fn qcheck_sized<T: Testable>(p: T, size: usize) {
env::set_var("QSV_SKIPUTF8_CHECK", "1");
QuickCheck::new().gen(Gen::new(size)).quickcheck(p);
env::set_var("QSV_SKIPUTF8_CHECK", "");
}
pub type CsvVecs = Vec<Vec<String>>;
pub trait Csv {
fn to_vecs(self) -> CsvVecs;
fn from_vecs(_: CsvVecs) -> Self;
}
impl Csv for CsvVecs {
fn to_vecs(self) -> CsvVecs {
self
}
fn from_vecs(vecs: CsvVecs) -> CsvVecs {
vecs
}
}
#[derive(Clone, Eq, Ord, PartialEq, PartialOrd)]
struct CsvRecord(Vec<String>);
impl CsvRecord {
fn unwrap(self) -> Vec<String> {
let CsvRecord(v) = self;
v
}
}
#[allow(clippy::needless_lifetimes)]
impl ops::Deref for CsvRecord {
type Target = [String];
fn deref<'a>(&'a self) -> &'a [String] {
&self.0
}
}
#[allow(clippy::needless_lifetimes)]
impl ops::DerefMut for CsvRecord {
fn deref_mut<'a>(&'a mut self) -> &'a mut [String] {
&mut self.0
}
}
impl fmt::Debug for CsvRecord {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let bytes: Vec<_> = self.iter().map(std::string::String::as_bytes).collect();
write!(f, "{bytes:?}")
}
}
impl Arbitrary for CsvRecord {
fn arbitrary(g: &mut Gen) -> CsvRecord {
let size = g.size();
CsvRecord((0..size).map(|_| Arbitrary::arbitrary(g)).collect())
}
fn shrink(&self) -> Box<dyn Iterator<Item = CsvRecord> + 'static> {
Box::new(
self.clone()
.unwrap()
.shrink()
.filter(|r| !r.is_empty())
.map(CsvRecord),
)
}
}
impl Csv for Vec<CsvRecord> {
fn to_vecs(self) -> CsvVecs {
unsafe { transmute(self) }
}
fn from_vecs(vecs: CsvVecs) -> Vec<CsvRecord> {
unsafe { transmute(vecs) }
}
}
#[derive(Clone, Debug, Eq, Ord, PartialOrd)]
struct CsvData {
data: Vec<CsvRecord>,
}
impl CsvData {
fn unwrap(self) -> Vec<CsvRecord> {
self.data
}
fn len(&self) -> usize {
(**self).len()
}
fn is_empty(&self) -> bool {
self.len() == 0
}
}
#[allow(clippy::needless_lifetimes)]
impl ops::Deref for CsvData {
type Target = [CsvRecord];
fn deref<'a>(&'a self) -> &'a [CsvRecord] {
&self.data
}
}
impl Arbitrary for CsvData {
fn arbitrary(g: &mut Gen) -> CsvData {
let record_len = g.size();
let mut rng = thread_rng();
let num_records: usize = rng.gen_range(0..100);
let mut d = CsvData {
data: (0..num_records)
.map(|_| CsvRecord((0..record_len).map(|_| Arbitrary::arbitrary(g)).collect()))
.collect(),
};
// If the CSV data starts with a BOM, strip it, because it wreaks havoc
// with tests that weren't designed to handle it.
if !d.data.is_empty() && !d.data[0].is_empty() {
if let Some(stripped) = d.data[0][0].strip_prefix('\u{FEFF}') {
d.data[0][0] = stripped.to_string();
}
}
d
}
fn shrink(&self) -> Box<dyn Iterator<Item = CsvData> + 'static> {
let len = if self.is_empty() { 0 } else { self[0].len() };
let mut rows: Vec<CsvData> = self
.clone()
.unwrap()
.shrink()
.filter(|rows| rows.iter().all(|r| r.len() == len))
.map(|rows| CsvData { data: rows })
.collect();
// We should also introduce CSV data with fewer columns...
if len > 1 {
rows.extend(
self.clone()
.unwrap()
.shrink()
.filter(|rows| rows.iter().all(|r| r.len() == len - 1))
.map(|rows| CsvData { data: rows }),
);
}
Box::new(rows.into_iter())
}
}
impl Csv for CsvData {
fn to_vecs(self) -> CsvVecs {
unsafe { transmute(self.data) }
}
fn from_vecs(vecs: CsvVecs) -> CsvData {
CsvData {
data: unsafe { transmute(vecs) },
}
}
}
impl PartialEq for CsvData {
fn eq(&self, other: &CsvData) -> bool {
(self.data.is_empty() && other.data.is_empty()) || self.data == other.data
}
}