11use crate :: ifn:: IFn ;
2- use crate :: value:: { Value , ToValue } ;
2+ use crate :: value:: { ToValue , Value } ;
33use std:: rc:: Rc ;
44
55use crate :: error_message;
@@ -16,41 +16,45 @@ impl ToValue for DivideFn {
1616impl IFn for DivideFn {
1717 fn invoke ( & self , args : Vec < Rc < Value > > ) -> Value {
1818 match args. len ( ) {
19- 0 => { error_message:: zero_arg_count ( args. len ( ) ) } ,
19+ 0 => error_message:: zero_arg_count ( args. len ( ) ) ,
2020 1 => {
2121 let val = args. get ( 0 ) . unwrap ( ) . to_value ( ) ;
2222 match val {
2323 Value :: I32 ( a_) => Value :: F64 ( 1.0 / a_ as f64 ) ,
2424 Value :: F64 ( f_) => Value :: F64 ( 1.0 / f_) ,
25- _ => Value :: Condition ( format ! ( // TODO: what error message should be returned regarding using typetags?
26- "Type mismatch; Expecting: (i32 | i64 | f32 | f64), Found: {}" ,
27- val. type_tag( )
28- ) )
25+ _ => Value :: Condition ( format ! (
26+ // TODO: what error message should be returned regarding using typetags?
27+ "Type mismatch; Expecting: (i32 | i64 | f32 | f64), Found: {}" ,
28+ val. type_tag( )
29+ ) ) ,
2930 }
30- } ,
31+ }
3132 _ => {
3233 let mut args_iterator = args. into_iter ( ) ;
3334 let first_arg = args_iterator. next ( ) . unwrap ( ) ;
3435 args_iterator. fold ( first_arg. to_value ( ) , |a, b| match a {
3536 Value :: I32 ( a_) => match * b {
3637 Value :: I32 ( b_) => Value :: I32 ( a_ / b_) ,
3738 Value :: F64 ( b_) => Value :: F64 ( a_ as f64 / b_) ,
38- _ => Value :: Condition ( format ! ( // TODO: what error message should be returned regarding using typetags?
39- "Type mismatch; Expecting: (i32 | i64 | f32 | f64), Found: {}" ,
40- b. type_tag( )
39+ _ => Value :: Condition ( format ! (
40+ // TODO: what error message should be returned regarding using typetags?
41+ "Type mismatch; Expecting: (i32 | i64 | f32 | f64), Found: {}" ,
42+ b. type_tag( )
4143 ) ) ,
4244 } ,
4345 Value :: F64 ( a_) => match * b {
4446 Value :: I32 ( b_) => Value :: F64 ( a_ / b_ as f64 ) ,
4547 Value :: F64 ( b_) => Value :: F64 ( a_ / b_) ,
46- _ => Value :: Condition ( format ! ( // TODO: what error message should be returned regarding using typetags?
47- "Type mismatch; Expecting: (i32 | i64 | f32 | f64), Found: {}" ,
48- b. type_tag( )
48+ _ => Value :: Condition ( format ! (
49+ // TODO: what error message should be returned regarding using typetags?
50+ "Type mismatch; Expecting: (i32 | i64 | f32 | f64), Found: {}" ,
51+ b. type_tag( )
4952 ) ) ,
5053 } ,
51- _ => Value :: Condition ( format ! ( // TODO: what error message should be returned regarding using typetags?
52- "Type mismatch: Expecting: (i32 | i64 | f32 | f64), Found: {}" ,
53- a. type_tag( )
54+ _ => Value :: Condition ( format ! (
55+ // TODO: what error message should be returned regarding using typetags?
56+ "Type mismatch: Expecting: (i32 | i64 | f32 | f64), Found: {}" ,
57+ a. type_tag( )
5458 ) ) ,
5559 } )
5660 }
@@ -60,68 +64,73 @@ impl IFn for DivideFn {
6064
6165#[ cfg( test) ]
6266mod tests {
63- mod Divide_tests {
64- use crate :: rust_core:: _divide_:: DivideFn ;
67+ mod divide_tests {
6568 use crate :: ifn:: IFn ;
69+ use crate :: rust_core:: _divide_:: DivideFn ;
6670 use crate :: value:: Value ;
6771 use std:: rc:: Rc ;
6872
6973 #[ test]
70- fn Divide_without_arguments_returns_one ( ) {
71- let Divide = DivideFn { } ;
74+ fn divide_without_arguments_returns_one ( ) {
75+ let divide = DivideFn { } ;
7276 let args = vec ! [ ] ;
73- assert_eq ! ( Value :: Condition ( String :: from( "Wrong number of arguments given to function (Given: 0)" ) ) ,
74- Divide . invoke( args) ) ;
77+ assert_eq ! (
78+ Value :: Condition ( String :: from(
79+ "Wrong number of arguments given to function (Given: 0)"
80+ ) ) ,
81+ divide. invoke( args)
82+ ) ;
7583 }
7684
7785 #[ test]
78- fn Divide_with_one_positive_argument_returns_reciprocal ( ) {
86+ fn divide_with_one_positive_argument_returns_reciprocal ( ) {
7987 let divide = DivideFn { } ;
8088 let args = vec ! [ Rc :: new( Value :: I32 ( 5 ) ) ] ;
81- assert_eq ! ( Value :: F64 ( 1.0 / 5.0 ) , divide. invoke( args) ) ;
89+ assert_eq ! ( Value :: F64 ( 1.0 / 5.0 ) , divide. invoke( args) ) ;
8290 }
8391
8492 #[ test]
85- fn Divide_with_one_negative_argument_returns_reciprocal ( ) {
93+ fn divide_with_one_negative_argument_returns_reciprocal ( ) {
8694 let divide = DivideFn { } ;
8795 let args = vec ! [ Rc :: new( Value :: I32 ( -5 ) ) ] ;
8896 assert_eq ! ( Value :: F64 ( 1.0 / -5.0 ) , divide. invoke( args) ) ;
8997 }
9098
9199 #[ test]
92- fn Divide_with_two_integer_argument_returns_quotient ( ) {
100+ fn divide_with_two_integer_argument_returns_quotient ( ) {
93101 let divide = DivideFn { } ;
94102 let args = vec ! [ Rc :: new( Value :: I32 ( 24 ) ) , Rc :: new( Value :: I32 ( 6 ) ) ] ;
95103 assert_eq ! ( Value :: I32 ( 4 ) , divide. invoke( args) ) ;
96104 }
97105
98106 #[ test]
99- fn Divide_with_one_double_argument_returns_quotient ( ) {
107+ fn divide_with_one_double_argument_returns_quotient ( ) {
100108 let divide = DivideFn { } ;
101109 let args = vec ! [ Rc :: new( Value :: I32 ( 24 ) ) , Rc :: new( Value :: F64 ( 1.5 ) ) ] ;
102110 assert_eq ! ( Value :: F64 ( 16.0 ) , divide. invoke( args) ) ;
103111 }
104112
105113 #[ test]
106- fn Divide_with_multiple_integer_arguments_returns_quotient ( ) {
107- let Divide = DivideFn { } ;
114+ fn divide_with_multiple_integer_arguments_returns_quotient ( ) {
115+ let divide = DivideFn { } ;
108116 let args = vec ! [
109117 Rc :: new( Value :: I32 ( 100 ) ) ,
110118 Rc :: new( Value :: I32 ( 5 ) ) ,
111- Rc :: new( Value :: I32 ( 4 ) ) ] ;
112- assert_eq ! ( Value :: I32 ( 5 ) , Divide . invoke( args) ) ;
119+ Rc :: new( Value :: I32 ( 4 ) ) ,
120+ ] ;
121+ assert_eq ! ( Value :: I32 ( 5 ) , divide. invoke( args) ) ;
113122 }
114123
115124 #[ test]
116- fn Divide_with_multiple_mixed_arguments_returns_quotient ( ) {
117- let Divide = DivideFn { } ;
125+ fn divide_with_multiple_mixed_arguments_returns_quotient ( ) {
126+ let divide = DivideFn { } ;
118127 let args = vec ! [
119128 Rc :: new( Value :: I32 ( 100 ) ) ,
120129 Rc :: new( Value :: I32 ( 5 ) ) ,
121130 Rc :: new( Value :: I32 ( 4 ) ) ,
122- Rc :: new( Value :: F64 ( 2.0 ) ) ] ;
123- assert_eq ! ( Value :: F64 ( 2.5 ) , Divide . invoke( args) ) ;
131+ Rc :: new( Value :: F64 ( 2.0 ) ) ,
132+ ] ;
133+ assert_eq ! ( Value :: F64 ( 2.5 ) , divide. invoke( args) ) ;
124134 }
125-
126135 }
127- }
136+ }
0 commit comments