Skip to content

Commit cd4edb3

Browse files
committed
allocator: add unit test and optimize constraint funcs
This commit aims to: 1. optimize Constraint's set functions 2. add some unit tests for Constraint Signed-off-by: Chao Wu <chaowu@linux.alibaba.com>
1 parent 5c1cf8d commit cd4edb3

File tree

1 file changed

+78
-9
lines changed

1 file changed

+78
-9
lines changed

crates/db-allocator/src/lib.rs

Lines changed: 78 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,28 @@
77

88
mod interval_tree;
99
pub use interval_tree::{IntervalTree, NodeState, Range};
10+
use std::fmt::{Display, Formatter};
11+
use std::result;
12+
13+
/// Error conditions that may appear during `Allocator` related operations.
14+
#[derive(Debug, PartialEq)]
15+
pub enum Error {
16+
/// Invalid Constraint Max and Min
17+
InvalidBoundary(u64, u64),
18+
}
19+
20+
impl Display for Error {
21+
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
22+
match self {
23+
Error::InvalidBoundary(max, min) => {
24+
write!(f, "invalid constraint max ({}) and min ({})", max, min)
25+
}
26+
}
27+
}
28+
}
1029

1130
/// Policy for resource allocation.
12-
#[derive(Copy, Clone, Debug)]
31+
#[derive(Copy, Clone, Debug, PartialEq)]
1332
pub enum AllocPolicy {
1433
/// Default allocation policy.
1534
Default,
@@ -32,6 +51,9 @@ pub struct Constraint {
3251
pub policy: AllocPolicy,
3352
}
3453

54+
/// Generic result type that may return `Allocator` errors.
55+
pub type Result<T> = result::Result<T, Error>;
56+
3557
impl Constraint {
3658
/// Create a new constraint object with default settings.
3759
pub fn new<T>(size: T) -> Self
@@ -48,35 +70,82 @@ impl Constraint {
4870
}
4971

5072
/// Set the min constraint.
51-
pub fn min<T>(mut self, min: T) -> Self
73+
pub fn min<T>(&mut self, min: T)
5274
where
5375
u64: From<T>,
5476
{
5577
self.min = u64::from(min);
56-
self
5778
}
5879

5980
/// Set the max constraint.
60-
pub fn max<T>(mut self, max: T) -> Self
81+
pub fn max<T>(&mut self, max: T)
6182
where
6283
u64: From<T>,
6384
{
6485
self.max = u64::from(max);
65-
self
6686
}
6787

6888
/// Set the alignment constraint.
69-
pub fn align<T>(mut self, align: T) -> Self
89+
pub fn align<T>(&mut self, align: T)
7090
where
7191
u64: From<T>,
7292
{
7393
self.align = u64::from(align);
74-
self
7594
}
7695

7796
/// Set the allocation policy.
78-
pub fn policy(mut self, policy: AllocPolicy) -> Self {
97+
pub fn policy(&mut self, policy: AllocPolicy) {
7998
self.policy = policy;
80-
self
99+
}
100+
101+
/// Validate the constraint
102+
pub fn validate(&self) -> Result<()> {
103+
if self.max < self.min {
104+
return Err(Error::InvalidBoundary(self.max, self.min));
105+
}
106+
Ok(())
107+
}
108+
}
109+
110+
#[cfg(test)]
111+
mod tests {
112+
use super::*;
113+
#[test]
114+
fn test_set_min() {
115+
let mut constraint = Constraint::new(2_u64);
116+
constraint.min(1_u64);
117+
assert_eq!(constraint.min, 1_u64);
118+
}
119+
120+
#[test]
121+
fn test_set_max() {
122+
let mut constraint = Constraint::new(2_u64);
123+
constraint.max(100_u64);
124+
assert_eq!(constraint.max, 100_u64);
125+
}
126+
127+
#[test]
128+
fn test_set_align() {
129+
let mut constraint = Constraint::new(2_u64);
130+
constraint.align(8_u64);
131+
assert_eq!(constraint.align, 8_u64);
132+
}
133+
134+
#[test]
135+
fn test_set_policy() {
136+
let mut constraint = Constraint::new(2_u64);
137+
constraint.policy(AllocPolicy::FirstMatch);
138+
assert_eq!(constraint.policy, AllocPolicy::FirstMatch);
139+
}
140+
141+
#[test]
142+
fn test_set_invalid_boundary() {
143+
let mut constraint = Constraint::new(2_u64);
144+
constraint.max(999_u64);
145+
constraint.min(1000_u64);
146+
assert_eq!(
147+
constraint.validate(),
148+
Err(Error::InvalidBoundary(999_u64, 1000_u64))
149+
)
81150
}
82151
}

0 commit comments

Comments
 (0)