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
#![allow(
dead_code,
unused_imports,
unused_variables,
unused_macros,
unused_assignments,
unused_mut,
non_snake_case,
unused_must_use,
non_upper_case_globals,
non_camel_case_types
)]
#[cfg(test)]
mod random {
use rstest::rstest;
use pretty_assertions::assert_eq;
#[cfg(test)]
mod fn_random_choice {
use super::{
rstest,
assert_eq,
};
use core_dev::random::random_choice;
#[rstest]
// #[case(vec![1, 2, 3, 4, 5, 6, 7, 8, 9])]
#[case(vec![], None)]
#[case(vec![1], Some(&1))]
fn special_cases(
#[case] collection: Vec<i32>,
#[case] expected_result: Option<&i32>,
) {
let item = random_choice(&collection);
assert_eq!(item, expected_result);
}
#[rstest]
#[case(vec![1, 2, 3i32])]
#[case(vec![100, 100, 100, 100, 100, 100, 100, 100i32])]
fn against_vec_i32(#[case] collection: Vec<i32>) {
dbg!(&collection);
let item = random_choice(&collection);
// first of all to check if the function works
// the returned option must be != None
assert_ne!(item, None);
let item = *item.unwrap();
let mut found = false;
for number in collection {
if item == number {
found = true;
break;
}
}
// to double check we must find the item in the original collection
assert_eq!(found, true);
// doesnt work in cargo doc -
// println!("{}", item);
}
}
#[cfg(test)]
mod trait_random_choice {
use super::rstest;
use core_dev::random::traits::RandomChoice;
#[rstest]
// #[case(vec![1, 2, 3, 4, 5, 6, 7, 8, 9])]
#[case(vec![], None)]
#[case(vec![1], Some(&1))]
fn special_cases(
#[case] collection: Vec<i32>,
#[case] expected_result: Option<&i32>,
) {
let item = collection.random_choice();
assert_eq!(item, expected_result);
}
#[rstest]
#[case(vec![1, 2, 3i32])]
#[case(vec![100, 100, 100, 100, 100, 100, 100, 100i32])]
fn against_vec_i32(#[case] collection: Vec<i32>) {
dbg!(&collection);
let item = collection.random_choice();
// first of all to check if the function works
// the returned option must be != None
assert_ne!(item, None);
let item = *item.unwrap();
let mut found = false;
for number in collection {
if item == number {
found = true;
break;
}
}
// to double check we must find the item in the original collection
assert_eq!(found, true);
// doesnt work in cargo doc -
// println!("{}", item);
}
#[rstest]
#[case(vec!["asd", "1231", "123123"])]
#[case(vec!["hello", "rust", "is", "the", "best", "lang"])]
fn against_vec_string(#[case] collection: Vec<&str>) {
dbg!(&collection);
// im too lazy to write .to_string() for every string in the above cases
let collection: Vec<String> = collection
.into_iter()
.map(|s| s.to_string())
.collect();
let item = collection.random_choice();
// first of all to check if the function works
// the returned option must be != None
assert_ne!(item, None);
let item = item.unwrap();
let mut found = false;
for _string in collection.iter() {
if item == _string {
found = true;
break;
}
}
// to double check we must find the item in the original collection
assert_eq!(found, true);
// doesnt work in cargo doc -
// println!("{}", item);
}
}
#[cfg(test)]
mod random_float {
use super::{
rstest,
assert_eq,
};
use core_dev::random::traits::RandomFloat;
use core_dev::random::traits::RandomInt;
#[test]
fn random_float() {
let rf = f32::random_float();
println!("{}", rf);
let rf = f64::random_float();
println!("{}", rf);
let rf = i32::random_int();
println!("{}", rf);
}
}
}