1 /* 2 * Copyright (C) 2024 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 //! Crate to wrap tests of libfmq rust bindings with a trivial C-ABI interface 18 //! to test them from C++. 19 20 use fmq::MessageQueue; 21 22 macro_rules! assert_return { 23 ($e: expr) => { 24 if !$e { 25 eprintln!(stringify!($e)); 26 return false; 27 } 28 }; 29 ($e: expr, $msg: expr) => { 30 if !$e { 31 eprintln!($msg); 32 return false; 33 } 34 }; 35 } 36 test_body() -> bool37fn test_body() -> bool { 38 let mut mq = MessageQueue::<u8>::new(500, false); 39 40 match mq.write_many(4) { 41 Some(mut wc) => { 42 wc.write(200).unwrap(); 43 wc.write(201).unwrap(); 44 wc.write(202).unwrap(); 45 wc.write(203).unwrap(); 46 } 47 None => { 48 eprintln!("failed to write_many(4)"); 49 return false; 50 } 51 }; 52 53 let desc = mq.dupe_desc(); 54 let join_handle = std::thread::spawn(move || { 55 let mut mq2 = MessageQueue::from_desc(&desc, false); 56 match mq2.read_many(1) { 57 Some(mut rc) => { 58 assert_return!(rc.read() == Some(200)); 59 } 60 None => { 61 eprintln!("failed to read_many(1)"); 62 return false; 63 } 64 }; 65 true 66 }); 67 68 assert_return!(join_handle.join().ok() == Some(true)); 69 70 match mq.read_many(3) { 71 Some(mut rc) => { 72 assert_return!(rc.read() == Some(201)); 73 assert_return!(rc.read() == Some(202)); 74 assert_return!(rc.read() == Some(203)); 75 drop(rc); 76 } 77 None => { 78 eprintln!("failed to read_many(4)"); 79 return false; 80 } 81 }; 82 83 true 84 } 85 86 /// Test fmq from Rust. Returns 0 on failure, 1 on success. 87 #[no_mangle] fmq_rust_test() -> u888pub extern "C" fn fmq_rust_test() -> u8 { 89 test_body() as u8 90 } 91