15ba71b47Sopenharmony_ci// Copyright (C) 2024 Huawei Device Co., Ltd.
25ba71b47Sopenharmony_ci// Licensed under the Apache License, Version 2.0 (the "License");
35ba71b47Sopenharmony_ci// you may not use this file except in compliance with the License.
45ba71b47Sopenharmony_ci// You may obtain a copy of the License at
55ba71b47Sopenharmony_ci//
65ba71b47Sopenharmony_ci//     http://www.apache.org/licenses/LICENSE-2.0
75ba71b47Sopenharmony_ci//
85ba71b47Sopenharmony_ci// Unless required by applicable law or agreed to in writing, software
95ba71b47Sopenharmony_ci// distributed under the License is distributed on an "AS IS" BASIS,
105ba71b47Sopenharmony_ci// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
115ba71b47Sopenharmony_ci// See the License for the specific language governing permissions and
125ba71b47Sopenharmony_ci// limitations under the License.
135ba71b47Sopenharmony_ci
145ba71b47Sopenharmony_ciuse ffi::{GetUniqueSptr, KeepUniqueSptr};
155ba71b47Sopenharmony_ci
165ba71b47Sopenharmony_ci#[cxx::bridge(namespace = "OHOS")]
175ba71b47Sopenharmony_cimod ffi {
185ba71b47Sopenharmony_ci    unsafe extern "C++" {
195ba71b47Sopenharmony_ci        include!("samgr_rust_test_mem.h");
205ba71b47Sopenharmony_ci        type Keeper;
215ba71b47Sopenharmony_ci        type SptrFoo;
225ba71b47Sopenharmony_ci        fn GetUniqueSptr() -> UniquePtr<SptrFoo>;
235ba71b47Sopenharmony_ci        fn KeepUniqueSptr(s: UniquePtr<SptrFoo>) -> UniquePtr<Keeper>;
245ba71b47Sopenharmony_ci    }
255ba71b47Sopenharmony_ci}
265ba71b47Sopenharmony_ci
275ba71b47Sopenharmony_ci#[test]
285ba71b47Sopenharmony_cifn test() {
295ba71b47Sopenharmony_ci    let w = GetUniqueSptr();
305ba71b47Sopenharmony_ci    unsafe {
315ba71b47Sopenharmony_ci        assert_eq!(g_testDrop, 0);
325ba71b47Sopenharmony_ci    }
335ba71b47Sopenharmony_ci    drop(w);
345ba71b47Sopenharmony_ci    unsafe {
355ba71b47Sopenharmony_ci        assert_eq!(g_testDrop, 1);
365ba71b47Sopenharmony_ci    }
375ba71b47Sopenharmony_ci    let w = GetUniqueSptr();
385ba71b47Sopenharmony_ci    let keeper = KeepUniqueSptr(w);
395ba71b47Sopenharmony_ci    unsafe {
405ba71b47Sopenharmony_ci        assert_eq!(g_testDrop, 1);
415ba71b47Sopenharmony_ci    }
425ba71b47Sopenharmony_ci    drop(keeper);
435ba71b47Sopenharmony_ci    unsafe {
445ba71b47Sopenharmony_ci        assert_eq!(g_testDrop, 2);
455ba71b47Sopenharmony_ci    }
465ba71b47Sopenharmony_ci}
475ba71b47Sopenharmony_ci
485ba71b47Sopenharmony_ci#[link(name = "samgr_rust_test_c")]
495ba71b47Sopenharmony_ciextern "C" {
505ba71b47Sopenharmony_ci    static g_testDrop: i32;
515ba71b47Sopenharmony_ci}
52