File tree Expand file tree Collapse file tree 9 files changed +157
-16
lines changed Expand file tree Collapse file tree 9 files changed +157
-16
lines changed Original file line number Diff line number Diff line change 11[package ]
2- name = " algorithm- exercises-rust "
2+ name = " exercises"
33version = " 0.1.0"
44edition = " 2024"
55
@@ -8,3 +8,6 @@ name = "exercises"
88crate-type = [" rlib" , " cdylib" ]
99
1010[dependencies ]
11+ serde_json = " 1.0"
12+ serde = { version = " 1.0" , features = [" derive" ] }
13+ once_cell = " 1.21.3"
Original file line number Diff line number Diff line change @@ -91,6 +91,7 @@ update: dependencies outdated
9191upgrade :
9292
9393clean :
94+ $(PACKAGE_MANAGER ) clean
9495 rm -vf lcov.info
9596 rm -vf my-clippy-report.json
9697 rm -vf sonar-issues.json
Original file line number Diff line number Diff line change 1+ pub fn add ( left : u64 , right : u64 ) -> u64 {
2+ left + right
3+ }
Original file line number Diff line number Diff line change 1- pub fn add ( left : u64 , right : u64 ) -> u64 {
2- left + right
3- }
4-
5- #[ cfg( test) ]
6- mod tests {
7- use super :: * ;
8-
9- #[ test]
10- fn it_works ( ) {
11- let result = add ( 2 , 2 ) ;
12- assert_eq ! ( result, 4 ) ;
13- }
14- }
1+ pub mod add;
Original file line number Diff line number Diff line change 1+ [
2+ {
3+ "title" : " Example" ,
4+ "a" : 2 ,
5+ "b" : 2 ,
6+ "result" : 4
7+ }
8+ ]
Original file line number Diff line number Diff line change 1+ use exercises:: add:: add;
2+ use once_cell:: sync:: Lazy ;
3+ use serde:: Deserialize ;
4+
5+ mod common;
6+ use common:: utils:: load_json;
7+
8+ #[ derive( Debug , Deserialize ) ]
9+ struct AdderTestCase {
10+ title : String ,
11+ a : u64 ,
12+ b : u64 ,
13+ result : u64
14+ }
15+
16+ static TEST_DATA : Lazy < Vec < AdderTestCase > > =
17+ Lazy :: new ( || load_json ( "tests/add.json" ) ) ;
18+
19+ #[ cfg( test) ]
20+ mod tests {
21+ use super :: * ;
22+
23+ #[ test]
24+ fn test_adder ( ) {
25+ for test_case in TEST_DATA . iter ( ) {
26+ println ! ( "Probando AdderTestCase : {:?}" , test_case. title) ;
27+
28+ let result = add ( test_case. a , test_case. b ) ;
29+ assert_eq ! ( result, test_case. result) ;
30+ }
31+ }
32+ }
Original file line number Diff line number Diff line change 1+ pub mod utils;
Original file line number Diff line number Diff line change 1+ use serde:: de:: DeserializeOwned ;
2+ use std:: fs;
3+
4+ /// Carga un archivo JSON en un `Vec<T>`.
5+ pub fn load_json < T : DeserializeOwned > ( path : & str ) -> Vec < T > {
6+ let contents = fs:: read_to_string ( path)
7+ . unwrap_or_else ( |_| panic ! ( "No se pudo leer el archivo: {}" , path) ) ;
8+ serde_json:: from_str ( & contents)
9+ . unwrap_or_else ( |_| panic ! ( "JSON inválido en {}" , path) )
10+ }
You can’t perform that action at this time.
0 commit comments