Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 9323e2d

Browse files
committedJun 30, 2019
Add basic test cases for some of the imported crates
- modtype - modtype-derive - regex - lazy_static
1 parent c4940e2 commit 9323e2d

File tree

1 file changed

+151
-2
lines changed

1 file changed

+151
-2
lines changed
 

‎src/main.rs

Lines changed: 151 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,15 @@ type UnitResult = Result<(), Box<dyn std::error::Error>>;
44

55
fn main() -> UnitResult {
66
run_proconio();
7+
run_modtype()?;
8+
run_modtype_derive();
79
run_ascii()?;
810
run_bitset_fixed();
911
run_superslice();
1012
run_itertools();
1113
run_rand_family()?;
1214
run_sfmt()?;
15+
run_regex()?;
1316
Ok(())
1417
}
1518

@@ -54,6 +57,114 @@ fn test_proconio() {
5457
// ordered-float
5558

5659
// modtype
60+
// these codes were taken from examples at https://github.com/qryxip/modtype/tree/master/examples
61+
fn run_modtype() -> UnitResult {
62+
use modtype::use_modtype;
63+
64+
#[use_modtype]
65+
type F = modtype::u64::F<1_000_000_007u64>;
66+
67+
let mut a = "13".parse::<F>()?;
68+
a += F(1_000_000_000);
69+
assert_eq!(a, F(6));
70+
71+
Ok(())
72+
}
73+
74+
#[test]
75+
fn test_modtype() -> UnitResult {
76+
run_modtype()
77+
}
78+
79+
// these codes were taken from examples at https://github.com/qryxip/modtype/blob/master/examples/derive.rs
80+
fn run_modtype_derive() {
81+
use modtype::{use_modtype, ConstValue};
82+
use std::marker::PhantomData;
83+
84+
#[use_modtype]
85+
type F = F_<17u32>;
86+
87+
#[derive(
88+
modtype::new,
89+
modtype::new_unchecked,
90+
modtype::get,
91+
Default,
92+
Clone,
93+
Copy,
94+
PartialEq,
95+
Eq,
96+
PartialOrd,
97+
Ord,
98+
modtype::From,
99+
modtype::Into,
100+
modtype::Display,
101+
modtype::Debug,
102+
modtype::FromStr,
103+
modtype::Deref,
104+
modtype::Neg,
105+
modtype::Add,
106+
modtype::AddAssign,
107+
modtype::Sub,
108+
modtype::SubAssign,
109+
modtype::Mul,
110+
modtype::MulAssign,
111+
modtype::Div,
112+
modtype::DivAssign,
113+
modtype::Rem,
114+
modtype::RemAssign,
115+
modtype::Num,
116+
modtype::Unsigned,
117+
modtype::Bounded,
118+
modtype::Zero,
119+
modtype::One,
120+
modtype::FromPrimitive,
121+
modtype::Inv,
122+
modtype::CheckedNeg,
123+
modtype::CheckedAdd,
124+
modtype::CheckedSub,
125+
modtype::CheckedMul,
126+
modtype::CheckedDiv,
127+
modtype::CheckedRem,
128+
modtype::Pow,
129+
modtype::Integer,
130+
)]
131+
#[modtype(
132+
modulus = "M::VALUE",
133+
std = "std",
134+
num_traits = "num::traits",
135+
num_integer = "num::integer",
136+
num_bigint = "num::bigint",
137+
from(InnerValue, BigUint, BigInt),
138+
debug(SingleTuple),
139+
neg(for_ref = true),
140+
add(for_ref = true),
141+
add_assign(for_ref = true),
142+
sub(for_ref = true),
143+
sub_assign(for_ref = true),
144+
mul(for_ref = true),
145+
mul_assign(for_ref = true),
146+
div(for_ref = true),
147+
div_assign(for_ref = true),
148+
rem(for_ref = true),
149+
rem_assign(for_ref = true),
150+
inv(for_ref = true),
151+
pow(for_ref = true)
152+
)]
153+
struct F_<M: ConstValue<Value = u32>> {
154+
#[modtype(value)]
155+
__value: u32,
156+
phantom: PhantomData<fn() -> M>,
157+
}
158+
assert_eq!(F(7) + F(13), F(3));
159+
assert_eq!(F(5) - F(11), F(11));
160+
assert_eq!(F(3), F(4) * F(5));
161+
assert_eq!(F(3) / F(4), F(5));
162+
}
163+
164+
#[test]
165+
fn test_modtype_derive() {
166+
run_modtype_derive();
167+
}
57168

58169
// ascii
59170
fn run_ascii() -> UnitResult {
@@ -219,6 +330,44 @@ fn calc_mean(rng: &mut impl rand::Rng) -> f64 {
219330
.fold(0.0, |mean, (t, x)| mean + (x - mean) / (t + 1) as f64)
220331
}
221332

222-
// regex
333+
// regex and lazy_static
334+
// these codes were taken from examples on: https://docs.rs/regex/1.1.7/regex/
335+
fn run_regex() -> UnitResult {
336+
use lazy_static::lazy_static;
337+
use regex::{Regex, RegexSet};
338+
339+
// ...
340+
lazy_static! {
341+
static ref RE_YYYYMMDD: Regex = Regex::new(r"(\d{4})-(\d{2})-(\d{2})").unwrap();
342+
static ref RE_SET: RegexSet =
343+
RegexSet::new(&[r"\w+", r"\d+", r"\pL+", r"foo", r"bar", r"barfoo", r"foobar",])
344+
.unwrap();
345+
}
346+
347+
let text = "2012-03-14, 2013-01-01 and 2014-07-05";
348+
let mut iter = RE_YYYYMMDD.captures_iter(text);
349+
350+
let mut cap;
351+
cap = iter.next().ok_or_else(|| "got none")?;
352+
assert_eq!((&cap[1], &cap[2], &cap[3]), ("2012", "03", "14"));
353+
cap = iter.next().ok_or_else(|| "got none")?;
354+
assert_eq!((&cap[1], &cap[2], &cap[3]), ("2013", "01", "01"));
355+
cap = iter.next().ok_or_else(|| "got none")?;
356+
assert_eq!((&cap[1], &cap[2], &cap[3]), ("2014", "07", "05"));
357+
358+
// Iterate over and collect all of the matches.
359+
let matches: Vec<_> = RE_SET.matches("foobar").into_iter().collect();
360+
assert_eq!(matches, vec![0, 2, 3, 4, 6]);
223361

224-
// jemallocator
362+
// You can also test whether a particular regex matched:
363+
let matches = RE_SET.matches("foobar");
364+
assert!(!matches.matched(5));
365+
assert!(matches.matched(6));
366+
367+
Ok(())
368+
}
369+
370+
#[test]
371+
fn test_regex() -> UnitResult {
372+
run_regex()
373+
}

0 commit comments

Comments
 (0)
Please sign in to comment.