util.rs 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. /* This file is part of DarkFi (https://dark.fi)
  2. *
  3. * Copyright (C) 2020-2023 Dyne.org foundation
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU Affero General Public License as
  7. * published by the Free Software Foundation, either version 3 of the
  8. * License, or (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU Affero General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Affero General Public License
  16. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  17. */
  18. pub fn find_free_id(task_ids: &[u32]) -> u32 {
  19. for i in 1.. {
  20. if !task_ids.contains(&i) {
  21. return i
  22. }
  23. }
  24. 1
  25. }
  26. #[cfg(test)]
  27. mod tests {
  28. use super::*;
  29. use darkfi::Result;
  30. #[test]
  31. fn find_free_id_test() -> Result<()> {
  32. let mut ids: Vec<u32> = vec![1, 3, 8, 9, 10, 3];
  33. let ids_empty: Vec<u32> = vec![];
  34. let ids_duplicate: Vec<u32> = vec![1; 100];
  35. let find_id = find_free_id(&ids);
  36. assert_eq!(find_id, 2);
  37. ids.push(find_id);
  38. assert_eq!(find_free_id(&ids), 4);
  39. assert_eq!(find_free_id(&ids_empty), 1);
  40. assert_eq!(find_free_id(&ids_duplicate), 2);
  41. Ok(())
  42. }
  43. }