@@ -25,71 +25,65 @@ fn round(value:f64)->f64{
2525 ( value * 100.0 ) . round ( ) / 100.0
2626}
2727
28+
2829#[ cfg( test) ]
2930mod tests {
3031 use super :: * ;
3132
32- #[ test]
33- fn test_present_value ( ) {
34- assert_eq ! (
35- 4.69 ,
36- present_value( 0.13 , vec![ 10.0 , 20.70 , -293.0 , 297.0 ] ) . unwrap( )
37- ) ;
38-
39- assert_eq ! (
40- -42739.63 ,
41- present_value( 0.07 , vec![ -109129.39 , 30923.23 , 15098.93 , 29734.0 , 39.0 ] ) . unwrap( )
42- ) ;
43-
44- assert_eq ! (
45- 175519.15 ,
46- present_value( 0.07 , vec![ 109129.39 , 30923.23 , 15098.93 , 29734.0 , 39.0 ] ) . unwrap( )
47- ) ;
33+ macro_rules! test_present_value {
34+ ( $( $name: ident: $inputs: expr, ) * ) => {
35+ $(
36+ #[ test]
37+ fn $name( ) {
38+ let ( ( discount_rate, cash_flows) , expected) = $inputs;
39+ assert_eq!( present_value( discount_rate, cash_flows) . unwrap( ) , expected) ;
40+ }
41+ ) *
42+ }
4843 }
4944
50- #[ test]
51- fn test_present_value_negative_discount_rate ( ) {
52- assert_eq ! (
53- PresentValueError :: NegetiveDiscount ,
54- present_value( -1.0 , vec![ 10.0 , 20.70 , -293.0 , 297.0 ] ) . unwrap_err( )
55- ) ;
45+ macro_rules! test_present_value_Err {
46+ ( $( $name: ident: $inputs: expr, ) * ) => {
47+ $(
48+ #[ test]
49+ fn $name( ) {
50+ let ( ( discount_rate, cash_flows) , expected) = $inputs;
51+ assert_eq!( present_value( discount_rate, cash_flows) . unwrap_err( ) , expected) ;
52+ }
53+ ) *
54+ }
5655 }
5756
58- #[ test]
59- fn test_present_value_empty_cash_flow ( ) {
60- assert_eq ! (
61- PresentValueError :: EmptyCashFlow ,
62- present_value( 1.0 , vec![ ] ) . unwrap_err( )
63- ) ;
57+ macro_rules! test_round {
58+ ( $( $name: ident: $inputs: expr, ) * ) => {
59+ $(
60+ #[ test]
61+ fn $name( ) {
62+ let ( input, expected) = $inputs;
63+ assert_eq!( round( input) , expected) ;
64+ }
65+ ) *
66+ }
6467 }
6568
66- #[ test]
67- fn test_present_value_zero_discount_rate ( ) {
68- assert_eq ! (
69- 184924.55 ,
70- present_value( 0.0 , vec![ 109129.39 , 30923.23 , 15098.93 , 29734.0 , 39.0 ] ) . unwrap( )
71- ) ;
72- }
73- #[ test]
74- fn test_round_function ( ) {
75-
76- assert_eq ! (
77- 0.55 ,
78- round( 0.55434 )
79- ) ;
8069
81- assert_eq ! (
82- 10.45 ,
83- round( 10.453 )
84- ) ;
70+ test_present_value ! {
71+ general_inputs1: ( ( 0.13 , vec![ 10.0 , 20.70 , -293.0 , 297.0 ] ) , 4.69 ) ,
72+ general_inputs2: ( ( 0.07 , vec![ -109129.39 , 30923.23 , 15098.93 , 29734.0 , 39.0 ] ) , -42739.63 ) ,
73+ general_inputs3: ( ( 0.07 , vec![ 109129.39 , 30923.23 , 15098.93 , 29734.0 , 39.0 ] ) , 175519.15 ) ,
74+ zero_input: ( ( 0.0 , vec![ 109129.39 , 30923.23 , 15098.93 , 29734.0 , 39.0 ] ) , 184924.55 ) ,
8575
86-
87- assert_eq ! (
88- 1111_f64 ,
89- round( 1111_f64 )
90- ) ;
76+ }
9177
78+ test_present_value_Err ! {
79+ negative_discount_rate: ( ( -1.0 , vec![ 10.0 , 20.70 , -293.0 , 297.0 ] ) , PresentValueError :: NegetiveDiscount ) ,
80+ empty_cash_flow: ( ( 1.0 , vec![ ] ) , PresentValueError :: EmptyCashFlow ) ,
9281
9382 }
94-
83+ test_round ! {
84+ test1: ( 0.55434 , 0.55 ) ,
85+ test2: ( 10.453 , 10.45 ) ,
86+ test3: ( 1111_f64 , 1111_f64 ) ,
87+ }
9588}
89+
0 commit comments