2011-01-07 00:15:39 -06:00
|
|
|
// -*- rust -*-
|
|
|
|
|
2011-01-07 20:25:51 -06:00
|
|
|
// Regression tests for circular_buffer when using a unit
|
|
|
|
// that has a size that is not a power of two
|
2011-01-07 00:15:39 -06:00
|
|
|
use std;
|
2011-05-12 10:24:54 -05:00
|
|
|
import std::option;
|
2011-05-17 13:41:41 -05:00
|
|
|
import std::uint;
|
2011-08-15 18:54:02 -05:00
|
|
|
import std::comm;
|
2011-08-25 13:20:43 -05:00
|
|
|
import std::comm::port;
|
|
|
|
import std::comm::chan;
|
2011-08-15 18:54:02 -05:00
|
|
|
import std::comm::send;
|
2011-08-25 13:20:43 -05:00
|
|
|
import std::comm::recv;
|
2011-06-15 13:19:50 -05:00
|
|
|
|
2011-01-07 20:25:51 -06:00
|
|
|
// A 12-byte unit to send over the channel
|
2011-07-27 07:19:39 -05:00
|
|
|
type record = {val1: u32, val2: u32, val3: u32};
|
2011-01-07 00:15:39 -06:00
|
|
|
|
2011-06-15 13:19:50 -05:00
|
|
|
|
2011-01-07 20:25:51 -06:00
|
|
|
// Assuming that the default buffer size needs to hold 8 units,
|
|
|
|
// then the minimum buffer size needs to be 96. That's not a
|
|
|
|
// power of two so needs to be rounded up. Don't trigger any
|
|
|
|
// assertions.
|
2011-04-19 15:35:49 -05:00
|
|
|
fn test_init() {
|
2011-08-25 13:20:43 -05:00
|
|
|
let myport = port();
|
|
|
|
let mychan = chan(myport);
|
2011-07-27 07:19:39 -05:00
|
|
|
let val: record = {val1: 0u32, val2: 0u32, val3: 0u32};
|
2011-08-15 18:54:02 -05:00
|
|
|
send(mychan, val);
|
2011-01-07 00:15:39 -06:00
|
|
|
}
|
|
|
|
|
2011-06-15 13:19:50 -05:00
|
|
|
|
2011-01-07 20:29:32 -06:00
|
|
|
// Dump lots of items into the channel so it has to grow.
|
|
|
|
// Don't trigger any assertions.
|
2011-04-19 15:35:49 -05:00
|
|
|
fn test_grow() {
|
2011-08-25 13:20:43 -05:00
|
|
|
let myport = port();
|
|
|
|
let mychan = chan(myport);
|
2011-08-19 17:16:48 -05:00
|
|
|
for each i: uint in uint::range(0u, 100u) {
|
2011-08-15 18:54:02 -05:00
|
|
|
let val: record = {val1: 0u32, val2: 0u32, val3: 0u32};
|
|
|
|
comm::send(mychan, val);
|
|
|
|
}
|
2011-01-07 20:29:32 -06:00
|
|
|
}
|
|
|
|
|
2011-06-15 13:19:50 -05:00
|
|
|
|
2011-01-07 21:13:52 -06:00
|
|
|
// Don't allow the buffer to shrink below it's original size
|
2011-04-19 15:35:49 -05:00
|
|
|
fn test_shrink1() {
|
2011-08-25 13:20:43 -05:00
|
|
|
let myport = port();
|
|
|
|
let mychan = chan(myport);
|
2011-08-15 18:54:02 -05:00
|
|
|
send(mychan, 0i8);
|
2011-08-25 13:20:43 -05:00
|
|
|
let x = recv(myport);
|
2011-01-07 21:13:52 -06:00
|
|
|
}
|
|
|
|
|
2011-04-19 15:35:49 -05:00
|
|
|
fn test_shrink2() {
|
2011-08-25 13:20:43 -05:00
|
|
|
let myport = port();
|
|
|
|
let mychan = chan(myport);
|
2011-08-19 17:16:48 -05:00
|
|
|
for each i: uint in uint::range(0u, 100u) {
|
2011-08-15 18:54:02 -05:00
|
|
|
let val: record = {val1: 0u32, val2: 0u32, val3: 0u32};
|
|
|
|
send(mychan, val);
|
|
|
|
}
|
2011-08-25 13:20:43 -05:00
|
|
|
for each i: uint in uint::range(0u, 100u) { let x = recv(myport); }
|
2011-01-07 21:22:35 -06:00
|
|
|
}
|
|
|
|
|
2011-06-15 13:19:50 -05:00
|
|
|
|
2011-01-07 22:09:16 -06:00
|
|
|
// Test rotating the buffer when the unit size is not a power of two
|
2011-04-19 15:35:49 -05:00
|
|
|
fn test_rotate() {
|
2011-08-25 13:20:43 -05:00
|
|
|
let myport = port();
|
|
|
|
let mychan = chan(myport);
|
2011-08-19 17:16:48 -05:00
|
|
|
for each i: uint in uint::range(0u, 100u) {
|
2011-07-27 07:19:39 -05:00
|
|
|
let val = {val1: i as u32, val2: i as u32, val3: i as u32};
|
2011-08-15 18:54:02 -05:00
|
|
|
send(mychan, val);
|
2011-08-25 13:20:43 -05:00
|
|
|
let x = recv(myport);
|
2011-05-02 19:47:24 -05:00
|
|
|
assert (x.val1 == i as u32);
|
|
|
|
assert (x.val2 == i as u32);
|
|
|
|
assert (x.val3 == i as u32);
|
2011-01-08 17:19:55 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-06-15 13:19:50 -05:00
|
|
|
|
2011-01-08 17:19:55 -06:00
|
|
|
// Test rotating and growing the buffer when
|
|
|
|
// the unit size is not a power of two
|
2011-04-19 15:35:49 -05:00
|
|
|
fn test_rotate_grow() {
|
2011-08-25 13:20:43 -05:00
|
|
|
let myport = port::<record>();
|
|
|
|
let mychan = chan(myport);
|
2011-08-19 17:16:48 -05:00
|
|
|
for each j: uint in uint::range(0u, 10u) {
|
|
|
|
for each i: uint in uint::range(0u, 10u) {
|
2011-07-27 07:19:39 -05:00
|
|
|
let val: record =
|
|
|
|
{val1: i as u32, val2: i as u32, val3: i as u32};
|
2011-08-15 18:54:02 -05:00
|
|
|
send(mychan, val);
|
2011-01-07 22:09:16 -06:00
|
|
|
}
|
2011-08-19 17:16:48 -05:00
|
|
|
for each i: uint in uint::range(0u, 10u) {
|
2011-08-25 13:20:43 -05:00
|
|
|
let x = recv(myport);
|
2011-05-02 19:47:24 -05:00
|
|
|
assert (x.val1 == i as u32);
|
|
|
|
assert (x.val2 == i as u32);
|
|
|
|
assert (x.val3 == i as u32);
|
2011-01-07 22:09:16 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-04-19 15:35:49 -05:00
|
|
|
fn main() {
|
2011-01-07 20:25:51 -06:00
|
|
|
test_init();
|
2011-01-07 20:29:32 -06:00
|
|
|
test_grow();
|
2011-01-07 21:22:35 -06:00
|
|
|
test_shrink1();
|
|
|
|
test_shrink2();
|
2011-01-07 22:09:16 -06:00
|
|
|
test_rotate();
|
2011-01-08 17:19:55 -06:00
|
|
|
test_rotate_grow();
|
2011-01-07 00:15:39 -06:00
|
|
|
}
|
|
|
|
// Local Variables:
|
|
|
|
// mode: rust;
|
|
|
|
// fill-column: 78;
|
|
|
|
// indent-tabs-mode: nil
|
|
|
|
// c-basic-offset: 4
|
|
|
|
// buffer-file-coding-system: utf-8-unix
|
2011-06-15 14:01:19 -05:00
|
|
|
// compile-command: "make -k -C $RBUILD 2>&1 | sed -e 's/\\/x\\//x:\\//g'";
|
2011-01-07 00:15:39 -06:00
|
|
|
// End:
|