mirror of
https://github.com/zerotier/common-utils.git
synced 2026-05-22 16:28:56 -07:00
Add some unit tests
This commit is contained in:
+39
-3
@@ -418,7 +418,19 @@ mod tests {
|
||||
use super::ArrayVec;
|
||||
|
||||
#[test]
|
||||
fn array_vec() {
|
||||
fn popability() {
|
||||
let mut v = ArrayVec::<usize, 128>::new();
|
||||
for i in 0..128 {
|
||||
v.push(i);
|
||||
}
|
||||
assert_eq!(v.len(), 128);
|
||||
for _ in 0..128 {
|
||||
assert!(v.pop().is_some());
|
||||
}
|
||||
assert!(v.pop().is_none());
|
||||
}
|
||||
#[test]
|
||||
fn bounds() {
|
||||
let mut v = ArrayVec::<usize, 128>::new();
|
||||
for i in 0..128 {
|
||||
v.push(i);
|
||||
@@ -426,9 +438,33 @@ mod tests {
|
||||
assert_eq!(v.len(), 128);
|
||||
assert!(v.try_push(1000).is_err());
|
||||
assert_eq!(v.len(), 128);
|
||||
for _ in 0..128 {
|
||||
assert!(v.pop().is_some());
|
||||
}
|
||||
#[test]
|
||||
fn clear() {
|
||||
let mut v = ArrayVec::<usize, 128>::new();
|
||||
for i in 0..128 {
|
||||
v.push(i);
|
||||
}
|
||||
assert_eq!(v.len(), 128);
|
||||
v.clear();
|
||||
assert!(v.pop().is_none());
|
||||
}
|
||||
#[test]
|
||||
fn order() {
|
||||
let mut v = ArrayVec::<usize, 128>::new();
|
||||
for i in 0..128 {
|
||||
v.push(i);
|
||||
}
|
||||
assert_eq!(v.len(), 128);
|
||||
|
||||
assert!(v.first() == Some(&0));
|
||||
assert!(v.last() == Some(&127));
|
||||
|
||||
let size: usize = 128;
|
||||
for i in 0..size {
|
||||
let popped_val = v.pop();
|
||||
assert!(popped_val.is_some());
|
||||
assert!(popped_val == Some((size - 1) - i));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+2
-1
@@ -6,7 +6,8 @@
|
||||
* https://www.zerotier.com/
|
||||
*/
|
||||
|
||||
use base64::{engine::general_purpose, Engine as _};
|
||||
use base64::engine::general_purpose;
|
||||
use base64::Engine as _;
|
||||
|
||||
/// Encode a byte slice as Base64 using the URL-safe alphabet without padding
|
||||
#[inline(always)]
|
||||
|
||||
+2
-4
@@ -9,14 +9,12 @@
|
||||
use std::array::TryFromSliceError;
|
||||
use std::fmt::Debug;
|
||||
use std::hash::Hash;
|
||||
use std::ops::Deref;
|
||||
use std::ops::DerefMut;
|
||||
use std::ops::{Deref, DerefMut};
|
||||
|
||||
use serde::{Deserialize, Deserializer, Serialize, Serializer};
|
||||
use zeroize::Zeroize;
|
||||
|
||||
use crate::base64;
|
||||
use crate::hex;
|
||||
use crate::{base64, hex};
|
||||
|
||||
/// Fixed size Serde serializable byte array.
|
||||
/// This makes it easier to deal with blobs larger than 32 bytes (due to serde array limitations)
|
||||
|
||||
+1
-1
@@ -48,7 +48,7 @@ impl Buf {
|
||||
}
|
||||
}
|
||||
pub fn create_from(buffer: &[u8]) -> Buf {
|
||||
let mut buf = Self::new((buffer.len() + 7)/8*8);
|
||||
let mut buf = Self::new((buffer.len() + 7) / 8 * 8);
|
||||
let _ = buf.append(buffer);
|
||||
buf
|
||||
}
|
||||
|
||||
@@ -1227,4 +1227,22 @@ mod tests {
|
||||
let ip = InetAddress::from_str("1.2.3.4/1234").unwrap();
|
||||
assert_eq!("1.2.3.4/1234", ip.to_string());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn ip_from_invalid_string() {
|
||||
let ip4 = InetAddress::from_str("-1.-2.-3.-4/1234");
|
||||
assert!(ip4.is_err());
|
||||
let ip6 = InetAddress::from_str("-2603:6010:6e00:1118:d92a:ab88:4dfb:670a/1234");
|
||||
assert!(ip6.is_err());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn zero() {
|
||||
let mut ip4 = InetAddress::from_str("1.2.3.4/1234").unwrap();
|
||||
ip4.zero();
|
||||
assert!(ip4.is_nil());
|
||||
let mut ip6 = InetAddress::from_str("2603:6010:6e00:1118:d92a:ab88:4dfb:670a/1234").unwrap();
|
||||
ip6.zero();
|
||||
assert!(ip6.is_nil());
|
||||
}
|
||||
}
|
||||
|
||||
+2
-1
@@ -145,9 +145,10 @@ pub extern "C" fn unlikely_branch() {}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::ms_monotonic;
|
||||
use std::time::Duration;
|
||||
|
||||
use super::ms_monotonic;
|
||||
|
||||
#[test]
|
||||
fn monotonic_clock_sanity_check() {
|
||||
let start = ms_monotonic();
|
||||
|
||||
@@ -11,7 +11,6 @@
|
||||
|
||||
#[allow(unused_imports)]
|
||||
use std::mem::{needs_drop, size_of, MaybeUninit};
|
||||
|
||||
#[allow(unused_imports)]
|
||||
use std::ptr::copy_nonoverlapping;
|
||||
|
||||
|
||||
+3
-1
@@ -6,7 +6,9 @@
|
||||
* https://www.zerotier.com/
|
||||
*/
|
||||
|
||||
use std::{mem::MaybeUninit, ops::Deref, sync::RwLockReadGuard};
|
||||
use std::mem::MaybeUninit;
|
||||
use std::ops::Deref;
|
||||
use std::sync::RwLockReadGuard;
|
||||
|
||||
#[allow(unused)]
|
||||
pub struct MappedReadGuard<'a, S, T: ?Sized> {
|
||||
|
||||
Reference in New Issue
Block a user