Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cleanup, slab bugfixes and some tests #1

Merged
merged 3 commits into from
Sep 5, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@ authors = ["Carl Lerche <me@carllerche.com>"]

[dependencies.nix]

# git = "https://github.com/carllerche/nix-rust"
path = "libs/nix-rust"
git = "https://github.com/carllerche/nix-rust"

[[test]]

Expand Down
2 changes: 1 addition & 1 deletion src/buf/byte.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ pub struct ByteBuf {
}

impl ByteBuf {
pub fn new(mut capacity: uint) -> ByteBuf {
pub fn new(capacity: uint) -> ByteBuf {
// Handle 0 capacity case
if capacity == 0 {
return ByteBuf {
Expand Down
2 changes: 1 addition & 1 deletion src/os/epoll.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ impl Selector {

impl Drop for Selector {
fn drop(&mut self) {
close(self.epfd);
let _ = close(self.epfd);
}
}

Expand Down
64 changes: 61 additions & 3 deletions src/slab.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ impl<T> Slab<T> {
}

self.nxt = idx + 1;
self.len = idx;
self.len = idx + 1;
Ok(idx)
}
else {
Expand Down Expand Up @@ -85,7 +85,7 @@ impl<T> Slab<T> {
#[inline]
fn validate(&self, idx: uint) {
if idx >= self.len {
fail!("invalid index");
fail!("invalid index {} >= {}", idx, self.len);
}
}
}
Expand All @@ -97,7 +97,7 @@ impl<T> Index<uint, T> for Slab<T> {

let e = self.entry(idx);

if e.nxt == 0 {
if e.nxt != 0 {
fail!("invalid index");
}

Expand Down Expand Up @@ -139,3 +139,61 @@ struct Entry<T> {
nxt: uint, // Next available slot when available, 0 when in use
val: T // Value at slot
}

#[cfg(test)]
mod tests {
use super::Slab;

#[test]
fn test_insertion() {
let mut slab = Slab::new(1);
let token = slab.insert(10u).ok().expect("Failed to insert");
assert_eq!(slab[token], 10u);
}

#[test]
fn test_repeated_insertion() {
let mut slab = Slab::new(10);

for i in range(0u, 10u) {
let token = slab.insert(i + 10u).ok().expect("Failed to insert");
assert_eq!(slab[token], i + 10u);
}

slab.insert(20).err().expect("Inserted when full");
}

#[test]
fn test_repeated_insertion_and_removal() {
let mut slab = Slab::new(10);
let mut tokens = vec![];

for i in range(0u, 10u) {
let token = slab.insert(i + 10u).ok().expect("Failed to insert");
tokens.push(token);
assert_eq!(slab[token], i + 10u);
}

for &i in tokens.iter() {
slab.remove(i);
}

slab.insert(20).ok().expect("Failed to insert in newly empty slab");
}

#[test]
fn test_insertion_when_full() {
let mut slab = Slab::new(1);
slab.insert(10u).ok().expect("Failed to insert");
slab.insert(10u).err().expect("Inserted into a full slab");
}

#[test]
fn test_removal_is_successful() {
let mut slab = Slab::new(1);
let t1 = slab.insert(10u).ok().expect("Failed to insert");
slab.remove(t1);
let t2 = slab.insert(20u).ok().expect("Failed to insert");
assert_eq!(slab[t2], 20u);
}
}
17 changes: 7 additions & 10 deletions test/test_echo_server.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use mio::*;
use mio::buf::RingBuf;
use mio::buf::{RingBuf, SliceBuf};

#[deriving(Show)]
struct EchoConn {
Expand Down Expand Up @@ -99,14 +99,14 @@ struct EchoClient {

// Sends a message and expects to receive the same exact message, one at a time
impl EchoClient {
fn new(sock: TcpSocket, msgs: Vec<&'static str>) {
let curr = msgs.remove(0);
fn new(sock: TcpSocket, mut msgs: Vec<&'static str>) -> EchoClient {
let curr = msgs.remove(0).unwrap();

EchoClient {
sock: sock,
msgs: msgs,
tx: SliceBuf::wrap(curr),
rx: SliceBuf::wrap(curr)
tx: SliceBuf::wrap(curr.as_bytes()),
rx: SliceBuf::wrap(curr.as_bytes())
}
}

Expand All @@ -133,11 +133,8 @@ impl EchoHandler {
sock: srv,
conns: Slab::new(128)
},
client: EchoClient {
sock: client,
msgs: msgs,
curr: None
}

client: EchoClient::new(client, msgs)
}
}
}
Expand Down