@@ -4,12 +4,15 @@ type UnitResult = Result<(), Box<dyn std::error::Error>>;
4
4
5
5
fn main ( ) -> UnitResult {
6
6
run_proconio ( ) ;
7
+ run_modtype ( ) ?;
8
+ run_modtype_derive ( ) ;
7
9
run_ascii ( ) ?;
8
10
run_bitset_fixed ( ) ;
9
11
run_superslice ( ) ;
10
12
run_itertools ( ) ;
11
13
run_rand_family ( ) ?;
12
14
run_sfmt ( ) ?;
15
+ run_regex ( ) ?;
13
16
Ok ( ( ) )
14
17
}
15
18
@@ -54,6 +57,114 @@ fn test_proconio() {
54
57
// ordered-float
55
58
56
59
// 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
+ }
57
168
58
169
// ascii
59
170
fn run_ascii ( ) -> UnitResult {
@@ -219,6 +330,44 @@ fn calc_mean(rng: &mut impl rand::Rng) -> f64 {
219
330
. fold ( 0.0 , |mean, ( t, x) | mean + ( x - mean) / ( t + 1 ) as f64 )
220
331
}
221
332
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 ] ) ;
223
361
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