Repository navigation
Expand file tree
/
Copy pathtext.rs
More file actions
387 lines (370 loc) · 12.5 KB
/
Copy pathtext.rs
File metadata and controls
387 lines (370 loc) · 12.5 KB
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
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
//! 中文文本契约:独立预期、严格边界与全部干支配对。
use core::{error::Error, fmt, fmt::Write};
use matharts_core::{
Branch, CyclicSequence, Element, Ganzhi, InvalidGanzhi, Nayin, ParseError, Stem, Trigram, Xun,
YinYang,
};
use proptest::prelude::*;
const STEMS: [(Stem, &str); 10] = [
(Stem::Jia, "甲"),
(Stem::Yi, "乙"),
(Stem::Bing, "丙"),
(Stem::Ding, "丁"),
(Stem::Wu, "戊"),
(Stem::Ji, "己"),
(Stem::Geng, "庚"),
(Stem::Xin, "辛"),
(Stem::Ren, "壬"),
(Stem::Gui, "癸"),
];
const BRANCHES: [(Branch, &str); 12] = [
(Branch::Zi, "子"),
(Branch::Chou, "丑"),
(Branch::Yin, "寅"),
(Branch::Mao, "卯"),
(Branch::Chen, "辰"),
(Branch::Si, "巳"),
(Branch::Wu, "午"),
(Branch::Wei, "未"),
(Branch::Shen, "申"),
(Branch::You, "酉"),
(Branch::Xu, "戌"),
(Branch::Hai, "亥"),
];
const GANZHI: [&str; 60] = [
"甲子", "乙丑", "丙寅", "丁卯", "戊辰", "己巳", "庚午", "辛未", "壬申", "癸酉", "甲戌", "乙亥",
"丙子", "丁丑", "戊寅", "己卯", "庚辰", "辛巳", "壬午", "癸未", "甲申", "乙酉", "丙戌", "丁亥",
"戊子", "己丑", "庚寅", "辛卯", "壬辰", "癸巳", "甲午", "乙未", "丙申", "丁酉", "戊戌", "己亥",
"庚子", "辛丑", "壬寅", "癸卯", "甲辰", "乙巳", "丙午", "丁未", "戊申", "己酉", "庚戌", "辛亥",
"壬子", "癸丑", "甲寅", "乙卯", "丙辰", "丁巳", "戊午", "己未", "庚申", "辛酉", "壬戌", "癸亥",
];
// 身份序列由测试显式定义;生产 ALL 与名称表不能共同生成预期。
const YIN_YANG_VALUES: [YinYang; 2] = [YinYang::Yang, YinYang::Yin];
const ELEMENTS: [Element; 5] = [
Element::Wood,
Element::Fire,
Element::Earth,
Element::Metal,
Element::Water,
];
const TRIGRAMS: [Trigram; 8] = [
Trigram::Kun,
Trigram::Zhen,
Trigram::Kan,
Trigram::Dui,
Trigram::Gen,
Trigram::Li,
Trigram::Xun,
Trigram::Qian,
];
const XUNS: [Xun; 6] = [
Xun::JiaZi,
Xun::JiaXu,
Xun::JiaShen,
Xun::JiaWu,
Xun::JiaChen,
Xun::JiaYin,
];
const NAYIN: [Nayin; 30] = [
Nayin::HaiZhongJin,
Nayin::LuZhongHuo,
Nayin::DaLinMu,
Nayin::LuPangTu,
Nayin::JianFengJin,
Nayin::ShanTouHuo,
Nayin::JianXiaShui,
Nayin::ChengTouTu,
Nayin::BaiLaJin,
Nayin::YangLiuMu,
Nayin::QuanZhongShui,
Nayin::WuShangTu,
Nayin::PiLiHuo,
Nayin::SongBaiMu,
Nayin::ChangLiuShui,
Nayin::ShaZhongJin,
Nayin::ShanXiaHuo,
Nayin::PingDiMu,
Nayin::BiShangTu,
Nayin::JinBoJin,
Nayin::FuDengHuo,
Nayin::TianHeShui,
Nayin::DaYiTu,
Nayin::ChaiChuanJin,
Nayin::SangZheMu,
Nayin::DaXiShui,
Nayin::ShaZhongTu,
Nayin::TianShangHuo,
Nayin::ShiLiuMu,
Nayin::DaHaiShui,
];
#[test]
fn all_stems_and_branches_match_independent_chinese_names() {
for (value, text) in STEMS {
assert_eq!(value.to_string(), text);
assert_eq!(text.parse::<Stem>(), Ok(value));
assert_eq!(value.to_string().parse::<Stem>(), Ok(value));
}
for (value, text) in BRANCHES {
assert_eq!(value.to_string(), text);
assert_eq!(text.parse::<Branch>(), Ok(value));
assert_eq!(value.to_string().parse::<Branch>(), Ok(value));
}
}
#[test]
fn all_sixty_ganzhi_match_independent_chinese_names() {
for (index, text) in (0_u8..60).zip(GANZHI) {
let value = Ganzhi::try_from(index).unwrap();
assert_eq!(value.to_string(), text);
assert_eq!(text.parse::<Ganzhi>(), Ok(value));
assert_eq!(value.to_string().parse::<Ganzhi>(), Ok(value));
}
}
#[test]
fn all_120_stem_branch_texts_accept_only_the_sixty_named_pairs() {
let mut accepted = 0;
let mut rejected = 0;
for (stem, stem_text) in STEMS {
for (branch, branch_text) in BRANCHES {
let text = format!("{stem_text}{branch_text}");
let parsed = text.parse::<Ganzhi>();
if let Some(index) = GANZHI.iter().position(|expected| *expected == text) {
let value = parsed.unwrap();
assert_eq!(usize::from(value.index()), index);
assert_eq!((value.stem(), value.branch()), (stem, branch));
accepted += 1;
} else {
assert_eq!(parsed, Err(ParseError::InvalidGanzhi(InvalidGanzhi)));
rejected += 1;
}
}
}
assert_eq!((accepted, rejected), (60, 60));
}
#[test]
fn aliases_whitespace_and_extra_characters_are_rejected() {
for text in ["", "Jia", "jia", "0", "子", "甲子", "甲甲", "假", "😀"] {
assert_eq!(text.parse::<Stem>(), Err(ParseError::InvalidStem));
}
for text in ["", "Zi", "zi", "0", "甲", "甲子", "子子", "醜", "😀"] {
assert_eq!(text.parse::<Branch>(), Err(ParseError::InvalidBranch));
}
for padding in [
" ", "\t", "\n", "\r", "\u{a0}", "\u{3000}", "\u{200b}", "\0",
] {
for (_, text) in STEMS {
for input in [format!("{padding}{text}"), format!("{text}{padding}")] {
assert_eq!(input.parse::<Stem>(), Err(ParseError::InvalidStem));
}
}
for (_, text) in BRANCHES {
for input in [format!("{padding}{text}"), format!("{text}{padding}")] {
assert_eq!(input.parse::<Branch>(), Err(ParseError::InvalidBranch));
}
}
for text in GANZHI {
for input in [format!("{padding}{text}"), format!("{text}{padding}")] {
assert_eq!(
input.parse::<Ganzhi>(),
Err(ParseError::InvalidGanzhiFormat)
);
}
}
}
}
#[test]
fn ganzhi_errors_distinguish_format_names_and_invalid_pairs() {
for text in [
"",
"甲",
"甲子丑",
"甲 子",
"甲-子",
"JiaZi",
"甲子年",
"甲子\u{301}",
] {
assert_eq!(text.parse::<Ganzhi>(), Err(ParseError::InvalidGanzhiFormat));
}
// 这些输入都是两个 Unicode 字符;不能按固定字节位置切片。
for text in ["子甲", "A子", "😀子", "𠀀子", "\u{301}子", "\0子", "😃😀"] {
assert_eq!(text.parse::<Ganzhi>(), Err(ParseError::InvalidStem));
}
for text in ["甲甲", "甲A", "甲😀", "甲𠀀", "甲\u{301}", "甲\0", "甲醜"] {
assert_eq!(text.parse::<Ganzhi>(), Err(ParseError::InvalidBranch));
}
assert_eq!(
"甲丑".parse::<Ganzhi>(),
Err(ParseError::InvalidGanzhi(InvalidGanzhi))
);
}
#[test]
fn parse_errors_are_typed_and_preserve_the_pair_error_source() {
for error in [
ParseError::InvalidElement,
ParseError::InvalidYinYang,
ParseError::InvalidTrigram,
ParseError::InvalidXun,
ParseError::InvalidNayin,
ParseError::InvalidStem,
ParseError::InvalidBranch,
ParseError::InvalidGanzhiFormat,
] {
assert_ne!(error.to_string(), "");
assert!(error.source().is_none());
}
let error = "甲丑".parse::<Ganzhi>().unwrap_err();
assert_eq!(error.to_string(), InvalidGanzhi.to_string());
assert!(error.source().unwrap().is::<InvalidGanzhi>());
}
struct Buffer<const N: usize> {
bytes: [u8; N],
len: usize,
}
fn named_text_contract<T>(
values: &[T],
names: &serde_json::Value,
name: fn(T) -> &'static str,
error: ParseError,
aliases: &[&str],
) where
T: Copy + Eq + fmt::Debug + fmt::Display + core::str::FromStr<Err = ParseError>,
{
let names = names.as_array().expect("必需名称数组");
assert_eq!(names.len(), values.len(), "样本不能缺失或缩水");
let mut seen = std::collections::BTreeSet::new();
for (&value, expected) in values.iter().zip(names) {
let expected = expected.as_str().expect("名称必须是字符串");
assert_ne!(expected, "");
assert!(seen.insert(expected), "重复名称不能替代缺失样本");
assert_eq!(name(value), expected);
assert_eq!(value.to_string(), expected);
assert_eq!(expected.parse::<T>(), Ok(value));
for padding in [" ", "\t", "\n", "\u{a0}", "\u{3000}", "\u{200b}", "\0"] {
for input in [
format!("{padding}{expected}"),
format!("{expected}{padding}"),
] {
assert_eq!(input.parse::<T>(), Err(error));
}
}
for input in [
format!("{value:?}"),
format!("{expected}{expected}"),
format!("{expected}\u{301}"),
] {
assert_eq!(input.parse::<T>(), Err(error));
}
let mut buffer = Buffer {
bytes: [0; 16],
len: 0,
};
write!(buffer, "{value}").unwrap();
assert_eq!(&buffer.bytes[..buffer.len], expected.as_bytes());
let mut empty = Buffer { bytes: [], len: 0 };
assert!(write!(empty, "{value}").is_err());
}
for input in ["", "0", "😀"].into_iter().chain(aliases.iter().copied()) {
assert_eq!(input.parse::<T>(), Err(error));
}
}
#[test]
fn named_values_match_all_51_frozen_names_and_reject_non_exact_input() {
let fixture: serde_json::Value =
serde_json::from_str(include_str!("fixtures/text-values-v2.json")).unwrap();
assert_eq!(fixture["version"], 2);
assert_eq!(fixture.as_object().unwrap().len(), 6);
assert_eq!(YinYang::ALL, YIN_YANG_VALUES);
assert_eq!(Element::ALL, ELEMENTS);
assert_eq!(Trigram::ALL, TRIGRAMS);
assert_eq!(Xun::ALL, XUNS);
assert_eq!(Nayin::ALL, NAYIN);
named_text_contract(
&YIN_YANG_VALUES,
&fixture["yin_yang"],
YinYang::name,
ParseError::InvalidYinYang,
&["陰", "陽", "阴阳"],
);
named_text_contract(
&ELEMENTS,
&fixture["element"],
Element::name,
ParseError::InvalidElement,
&["木行", "五行"],
);
named_text_contract(
&TRIGRAMS,
&fixture["trigram"],
Trigram::name,
ParseError::InvalidTrigram,
&["乾卦", "兌", "離"],
);
named_text_contract(
&XUNS,
&fixture["xun"],
Xun::name,
ParseError::InvalidXun,
&["甲子", "甲子旬旬"],
);
named_text_contract(
&NAYIN,
&fixture["nayin"],
Nayin::name,
ParseError::InvalidNayin,
&["海中金命", "佛灯火", "覆燈火", "沙中土命"],
);
}
#[cfg(feature = "serde")]
#[test]
fn chinese_text_names_do_not_become_serde_codes() {
fn rejects<T: serde::de::DeserializeOwned>(names: &serde_json::Value) {
for name in names.as_array().unwrap() {
assert!(serde_json::from_value::<T>(name.clone()).is_err());
}
}
let fixture: serde_json::Value =
serde_json::from_str(include_str!("fixtures/text-values-v2.json")).unwrap();
rejects::<YinYang>(&fixture["yin_yang"]);
rejects::<Element>(&fixture["element"]);
rejects::<Trigram>(&fixture["trigram"]);
rejects::<Xun>(&fixture["xun"]);
rejects::<Nayin>(&fixture["nayin"]);
}
impl<const N: usize> fmt::Write for Buffer<N> {
fn write_str(&mut self, text: &str) -> fmt::Result {
let end = self.len + text.len();
let output = self.bytes.get_mut(self.len..end).ok_or(fmt::Error)?;
output.copy_from_slice(text.as_bytes());
self.len = end;
Ok(())
}
}
#[test]
fn display_works_with_stack_buffers_and_propagates_writer_errors() {
let mut buffer = Buffer {
bytes: [0; 6],
len: 0,
};
let value = Ganzhi::new(Stem::Jia, Branch::Zi).unwrap();
write!(buffer, "{value}").unwrap();
assert_eq!(&buffer.bytes[..buffer.len], "甲子".as_bytes());
let mut short = Buffer {
bytes: [0; 3],
len: 0,
};
assert!(write!(short, "{value}").is_err());
let mut empty = Buffer { bytes: [], len: 0 };
assert!(write!(empty, "{}", Stem::Jia).is_err());
assert!(write!(empty, "{}", Branch::Zi).is_err());
}
proptest! {
#[test]
fn arbitrary_utf8_accepts_only_exact_names(input in any::<String>()) {
let expected_stem = STEMS.iter().find(|(_, name)| *name == input).map(|(value, _)| *value);
let expected_branch = BRANCHES.iter().find(|(_, name)| *name == input).map(|(value, _)| *value);
let expected_ganzhi = GANZHI.iter().position(|name| *name == input);
prop_assert_eq!(input.parse::<Stem>().ok(), expected_stem);
prop_assert_eq!(input.parse::<Branch>().ok(), expected_branch);
prop_assert_eq!(input.parse::<Ganzhi>().map(|value| usize::from(value.index())).ok(), expected_ganzhi);
}
}