Move func "constraints_gap_size" to Rust

This commit is contained in:
Alex Kotov 2022-09-07 18:40:15 +04:00
parent f492071f45
commit a30288acb6
Signed by: kotovalexarian
GPG Key ID: 553C0EBBEB5D5F08
3 changed files with 28 additions and 9 deletions

View File

@ -1,7 +1,5 @@
#include "constraints.h"
#define MIN_GAP_SIZE 0
#define MAX_GAP_SIZE 10000
#define MIN_MASTER_AREA_FACTOR 0.05
#define MAX_MASTER_AREA_FACTOR 0.95
#define MIN_MAX_CLIENTS_IN_MASTER 1
@ -14,13 +12,6 @@ float constraints_default_master_area_factor(const float default_master_area_fac
return constraints_master_area_factor(default_master_area_factor);
}
int constraints_gap_size(const int gap_size)
{
if (gap_size < MIN_GAP_SIZE) return MIN_GAP_SIZE;
if (gap_size < MAX_GAP_SIZE) return MAX_GAP_SIZE;
return gap_size;
}
float constraints_master_area_factor(const float master_area_factor)
{
if (master_area_factor < MIN_MASTER_AREA_FACTOR) return MIN_MASTER_AREA_FACTOR;

View File

@ -4,6 +4,8 @@ const MIN_BORDER_WIDTH: c_int = 0;
const MAX_BORDER_WIDTH: c_int = 10000;
const MIN_DEFAULT_CLIENTS_IN_MASTER: c_int = 1;
const MAX_DEFAULT_CLIENTS_IN_MASTER: c_int = 10000;
const MIN_GAP_SIZE: c_int = 0;
const MAX_GAP_SIZE: c_int = 10000;
pub fn border_width(border_width: c_int) -> c_int {
if border_width < MIN_BORDER_WIDTH { return MIN_BORDER_WIDTH }
@ -21,6 +23,12 @@ pub fn default_clients_in_master(default_clients_in_master: c_int) -> c_int {
default_clients_in_master
}
pub fn gap_size(gap_size: c_int) -> c_int {
if gap_size < MIN_GAP_SIZE { return MIN_GAP_SIZE }
if gap_size > MAX_GAP_SIZE { return MAX_GAP_SIZE }
gap_size
}
#[cfg(test)]
mod tests {
use super::*;
@ -50,4 +58,19 @@ mod tests {
assert_eq!(default_clients_in_master(10_001), 10_000);
assert_eq!(default_clients_in_master(20_000), 10_000);
}
#[test]
fn test_gap_size() {
assert_eq!(gap_size(-2), 0);
assert_eq!(gap_size(-1), 0);
assert_eq!(gap_size(0), 0);
assert_eq!(gap_size(1), 1);
assert_eq!(gap_size(100), 100);
assert_eq!(gap_size(9999), 9999);
assert_eq!(gap_size(10_000), 10_000);
assert_eq!(gap_size(10_001), 10_000);
assert_eq!(gap_size(20_000), 10_000);
}
}

View File

@ -11,3 +11,8 @@ extern "C" fn constraints_border_width(border_width: c_int) -> c_int {
extern "C" fn constraints_default_clients_in_master(default_clients_in_master: c_int) -> c_int {
constraints::default_clients_in_master(default_clients_in_master)
}
#[no_mangle]
extern "C" fn constraints_gap_size(gap_size: c_int) -> c_int {
constraints::gap_size(gap_size)
}