@@ -106,11 +106,7 @@ mod tests {
106
106
max_ulps = max_ulps
107
107
) ;
108
108
// nothing typed
109
- assert_ulps_eq ! (
110
- get_toipe_results( 0 , 0 ) . accuracy( ) ,
111
- 0.0 ,
112
- max_ulps = max_ulps
113
- ) ;
109
+ assert_ulps_eq ! ( get_toipe_results( 0 , 0 ) . accuracy( ) , 0.0 , max_ulps = max_ulps) ;
114
110
// all wrong
115
111
assert_ulps_eq ! (
116
112
get_toipe_results( 100 , 100 ) . accuracy( ) ,
@@ -130,4 +126,79 @@ mod tests {
130
126
max_ulps = max_ulps
131
127
) ;
132
128
}
129
+
130
+ #[ test]
131
+ fn wpm ( ) {
132
+ fn get_toipe_results (
133
+ final_chars_typed_correctly : usize ,
134
+ final_uncorrected_errors : usize ,
135
+ duration : f64 ,
136
+ ) -> ToipeResults {
137
+ let started_at = Instant :: now ( ) ;
138
+ let seconds = duration. round ( ) ;
139
+ let nanoseconds = ( duration - seconds) * 1_000_000_000.0 ;
140
+ let ended_at = started_at + Duration :: new ( seconds as u64 , nanoseconds as u32 ) ;
141
+ ToipeResults {
142
+ total_words : 0 ,
143
+ total_chars_typed : 0 ,
144
+ total_chars_in_text : 0 ,
145
+ total_char_errors : 0 ,
146
+ final_chars_typed_correctly,
147
+ final_uncorrected_errors,
148
+ started_at,
149
+ ended_at,
150
+ }
151
+ }
152
+
153
+ let max_ulps = 1 ;
154
+ assert_ulps_eq ! (
155
+ get_toipe_results( 100 , 5 , 30.0 ) . wpm( ) ,
156
+ 30.0 ,
157
+ max_ulps = max_ulps
158
+ ) ;
159
+ assert_ulps_eq ! (
160
+ get_toipe_results( 1000 , 50 , 30.0 ) . wpm( ) ,
161
+ 300.0 ,
162
+ max_ulps = max_ulps
163
+ ) ;
164
+ assert_ulps_eq ! (
165
+ get_toipe_results( 200 , 0 , 30.0 ) . wpm( ) ,
166
+ 80.0 ,
167
+ max_ulps = max_ulps
168
+ ) ;
169
+ assert_ulps_eq ! (
170
+ get_toipe_results( 200 , 30 , 30.0 ) . wpm( ) ,
171
+ 20.0 ,
172
+ max_ulps = max_ulps
173
+ ) ;
174
+ // too many errors - cancels out
175
+ assert_ulps_eq ! (
176
+ get_toipe_results( 200 , 40 , 30.0 ) . wpm( ) ,
177
+ 0.0 ,
178
+ max_ulps = max_ulps
179
+ ) ;
180
+ // no negative wpms
181
+ assert_ulps_eq ! (
182
+ get_toipe_results( 200 , 50 , 30.0 ) . wpm( ) ,
183
+ 0.0 ,
184
+ max_ulps = max_ulps
185
+ ) ;
186
+ assert_ulps_eq ! (
187
+ get_toipe_results( 1 , 0 , 1.0 ) . wpm( ) ,
188
+ 12.0 ,
189
+ max_ulps = max_ulps
190
+ ) ;
191
+ // skdlhaslkd won't give you any score!
192
+ assert_ulps_eq ! (
193
+ get_toipe_results( 0 , 10 , 1.0 ) . wpm( ) ,
194
+ 0.0 ,
195
+ max_ulps = max_ulps
196
+ ) ;
197
+ assert_ulps_eq ! (
198
+ get_toipe_results( 0 , 0 , 0.01 ) . wpm( ) ,
199
+ 0.0 ,
200
+ max_ulps = max_ulps
201
+ ) ;
202
+ // we don't consider the case of duration = 0 because that seems impossible
203
+ }
133
204
}
0 commit comments