1 /*
2  * Copyright (C) 2023 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 extern crate ipc_rust;
17 extern crate example_calc_ipc_service;
18 
19 use ipc_rust::{FromRemoteObj, RemoteObjRef, get_service,};
20 use example_calc_ipc_service::{ICalc, EXAMPLE_IPC_CALC_SERVICE_ID};
21 
get_calc_service() -> RemoteObjRef<dyn ICalc>22 fn get_calc_service() -> RemoteObjRef<dyn ICalc>
23 {
24     let object = get_service(EXAMPLE_IPC_CALC_SERVICE_ID).expect("get icalc service failed");
25     let remote = <dyn ICalc as FromRemoteObj>::try_from(object);
26     let remote = match remote {
27         Ok(x) => x,
28         Err(error) => {
29             println!("convert RemoteObj to CalcProxy failed: {}", error);
30             panic!();
31         }
32     };
33     remote
34 }
35 
36 #[test]
calculator_ability()37 fn calculator_ability() {
38     let remote = get_calc_service();
39     // add
40     let ret = remote.add(5, 5).expect("add failed");
41     assert_eq!(ret, 10);
42     // sub
43     let ret = remote.sub(5, 5).expect("sub failed");
44     assert_eq!(ret, 0);
45     // mul
46     let ret = remote.mul(5, 5).expect("mul failed");
47     assert_eq!(ret, 25);
48     // div
49     let ret = remote.div(5, 5).expect("div failed");
50     assert_eq!(ret, 1);
51 }
52