|
| 1 | +// Copyright (c) 2024 Contributors to the Eclipse Foundation |
| 2 | +// |
| 3 | +// See the NOTICE file(s) distributed with this work for additional |
| 4 | +// information regarding copyright ownership. |
| 5 | +// |
| 6 | +// This program and the accompanying materials are made available under the |
| 7 | +// terms of the Apache Software License 2.0 which is available at |
| 8 | +// https://www.apache.org/licenses/LICENSE-2.0, or the MIT license |
| 9 | +// which is available at https://opensource.org/licenses/MIT. |
| 10 | +// |
| 11 | +// SPDX-License-Identifier: Apache-2.0 OR MIT |
| 12 | + |
| 13 | +#[generic_tests::define] |
| 14 | +mod event_id_tracker { |
| 15 | + use std::collections::HashSet; |
| 16 | + |
| 17 | + use iceoryx2_bb_lock_free::mpmc::bit_set::BitSet; |
| 18 | + use iceoryx2_bb_testing::assert_that; |
| 19 | + use iceoryx2_cal::event::{id_tracker::IdTracker, TriggerId}; |
| 20 | + |
| 21 | + trait NewSut { |
| 22 | + fn new_sut(capacity: usize) -> Self; |
| 23 | + } |
| 24 | + |
| 25 | + impl NewSut for BitSet { |
| 26 | + fn new_sut(capacity: usize) -> Self { |
| 27 | + BitSet::new(capacity) |
| 28 | + } |
| 29 | + } |
| 30 | + #[test] |
| 31 | + fn max_trigger_id_must_be_at_least_capacity<Sut: IdTracker + NewSut>() { |
| 32 | + const CAPACITY: usize = 5234; |
| 33 | + |
| 34 | + let sut = Sut::new_sut(CAPACITY); |
| 35 | + assert_that!(sut.trigger_id_max().as_u64(), ge CAPACITY as u64); |
| 36 | + } |
| 37 | + |
| 38 | + #[test] |
| 39 | + fn add_and_acquire_works<Sut: IdTracker + NewSut>() { |
| 40 | + const CAPACITY: usize = 1234; |
| 41 | + |
| 42 | + let sut = Sut::new_sut(CAPACITY); |
| 43 | + |
| 44 | + assert_that!(sut.acquire(), eq None); |
| 45 | + for i in 0..CAPACITY { |
| 46 | + let id = TriggerId::new(i as u64); |
| 47 | + assert_that!(sut.add(id), is_ok); |
| 48 | + assert_that!(sut.acquire(), eq Some(id)); |
| 49 | + assert_that!(sut.acquire(), is_none); |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + #[test] |
| 54 | + fn add_until_full_and_then_acquire_works<Sut: IdTracker + NewSut>() { |
| 55 | + const CAPACITY: usize = 1234; |
| 56 | + |
| 57 | + let sut = Sut::new_sut(CAPACITY); |
| 58 | + |
| 59 | + for i in 0..CAPACITY { |
| 60 | + let id = TriggerId::new((i as u64).min(sut.trigger_id_max().as_u64())); |
| 61 | + assert_that!(sut.add(id), is_ok); |
| 62 | + } |
| 63 | + |
| 64 | + let mut ids = HashSet::new(); |
| 65 | + for _ in 0..CAPACITY { |
| 66 | + let result = sut.acquire().unwrap(); |
| 67 | + assert_that!(result, le sut.trigger_id_max()); |
| 68 | + assert_that!(ids.insert(result), eq true); |
| 69 | + } |
| 70 | + |
| 71 | + assert_that!(sut.acquire(), is_none); |
| 72 | + } |
| 73 | + |
| 74 | + #[test] |
| 75 | + fn add_and_acquire_all_works<Sut: IdTracker + NewSut>() { |
| 76 | + const CAPACITY: usize = 3234; |
| 77 | + |
| 78 | + let sut = Sut::new_sut(CAPACITY); |
| 79 | + |
| 80 | + for i in 0..CAPACITY { |
| 81 | + let id = TriggerId::new((i as u64).min(sut.trigger_id_max().as_u64())); |
| 82 | + assert_that!(sut.add(id), is_ok); |
| 83 | + } |
| 84 | + |
| 85 | + let mut ids = HashSet::new(); |
| 86 | + sut.acquire_all(|id| { |
| 87 | + assert_that!(id, le sut.trigger_id_max()); |
| 88 | + assert_that!(ids.insert(id), eq true); |
| 89 | + }); |
| 90 | + |
| 91 | + let mut callback_called = false; |
| 92 | + sut.acquire_all(|_| callback_called = true); |
| 93 | + assert_that!(callback_called, eq false); |
| 94 | + |
| 95 | + assert_that!(ids, len CAPACITY); |
| 96 | + } |
| 97 | + |
| 98 | + #[test] |
| 99 | + fn add_acquire_and_acquire_all_works<Sut: IdTracker + NewSut>() { |
| 100 | + const CAPACITY: usize = 234; |
| 101 | + |
| 102 | + let sut = Sut::new_sut(CAPACITY); |
| 103 | + |
| 104 | + for i in 0..CAPACITY { |
| 105 | + let id = TriggerId::new((i as u64).min(sut.trigger_id_max().as_u64())); |
| 106 | + assert_that!(sut.add(id), is_ok); |
| 107 | + } |
| 108 | + |
| 109 | + let mut ids = HashSet::new(); |
| 110 | + for _ in 0..CAPACITY / 2 { |
| 111 | + let result = sut.acquire().unwrap(); |
| 112 | + assert_that!(result, le sut.trigger_id_max()); |
| 113 | + assert_that!(ids.insert(result), eq true); |
| 114 | + } |
| 115 | + |
| 116 | + sut.acquire_all(|id| { |
| 117 | + assert_that!(id, le sut.trigger_id_max()); |
| 118 | + assert_that!(ids.insert(id), eq true); |
| 119 | + }); |
| 120 | + |
| 121 | + assert_that!(ids, len CAPACITY); |
| 122 | + } |
| 123 | + |
| 124 | + #[instantiate_tests(<BitSet>)] |
| 125 | + mod bitset {} |
| 126 | +} |
0 commit comments