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