Start paring down std::str. Issue #855

This commit is contained in:
Brian Anderson 2011-08-31 15:23:34 -07:00
parent cb55ef6e12
commit 53a9d5a1d2
24 changed files with 396 additions and 719 deletions

View File

@ -49,6 +49,12 @@ fn debug_fn<T>(x: &T) { rustrt::debug_fn::<T>(x); }
fn ptr_cast<T, U>(x: @T) -> @U { ret rustrt::debug_ptrcast::<T, U>(x); }
fn trap(s: str) { rustrt::debug_trap(s); }
fn refcount<T>(a: &@T) -> uint {
let p: *uint = unsafe::reinterpret_cast(a);
ret *p;
}
// Local Variables:
// mode: rust;
// fill-column: 78;

View File

@ -87,7 +87,7 @@ obj new_reader(rdr: buf_reader) {
}
let b0 = c0 as u8;
let w = str::utf8_char_width(b0);
let w = istr::utf8_char_width(b0);
assert (w > 0u);
if w == 1u { ret b0 as char; }
let val = 0u;

View File

@ -4,7 +4,7 @@ concat, connect, to_upper, replace, char_slice, trim_left, trim_right, trim,
unshift_char, shift_char, pop_char, push_char, is_utf8, from_chars, to_chars,
char_len, char_at, bytes, is_ascii, shift_byte, pop_byte, unsafe_from_byte,
unsafe_from_bytes, from_char, char_range_at, str_from_cstr, sbuf,
as_buf, push_byte;
as_buf, push_byte, utf8_char_width, safe_slice;
export from_estr, to_estr, from_estrs, to_estrs;

View File

@ -2,57 +2,57 @@
import rustrt::sbuf;
import uint::le;
export sbuf;
export rustrt;
// export rustrt;
export eq;
export lteq;
export hash;
export is_utf8;
export is_ascii;
export alloc;
// export hash;
// export is_utf8;
// export is_ascii;
// export alloc;
export byte_len;
export buf;
export bytes;
export unsafe_from_byte;
export str_from_cstr;
export str_from_buf;
export push_utf8_bytes;
// export bytes;
// export unsafe_from_byte;
// export str_from_cstr;
// export str_from_buf;
// export push_utf8_bytes;
export from_char;
export from_chars;
export utf8_char_width;
export char_range_at;
// export from_chars;
// export utf8_char_width;
// export char_range_at;
export char_at;
export char_len;
export to_chars;
export push_char;
export pop_char;
export shift_char;
export unshift_char;
export refcount;
export index;
export rindex;
// export to_chars;
// export push_char;
// export pop_char;
// export shift_char;
// export unshift_char;
// export refcount;
// export index;
// export rindex;
export find;
export starts_with;
export ends_with;
// export starts_with;
// export ends_with;
export substr;
export slice;
export shift_byte;
export pop_byte;
export push_byte;
export unshift_byte;
export split;
export concat;
export connect;
// export slice;
// export shift_byte;
// export pop_byte;
// export push_byte;
// export unshift_byte;
// export split;
// export concat;
// export connect;
export to_upper;
export safe_slice;
// export safe_slice;
export unsafe_from_bytes;
export is_empty;
export is_not_empty;
export is_whitespace;
export replace;
export char_slice;
export trim_left;
export trim_right;
export trim;
// export is_empty;
// export is_not_empty;
// export is_whitespace;
// export replace;
// export char_slice;
// export trim_left;
// export trim_right;
// export trim;
native "rust" mod rustrt {
type sbuf;
@ -339,7 +339,7 @@ fn index(s: str, c: u8) -> int {
}
fn rindex(s: str, c: u8) -> int {
let n: int = str::byte_len(s) as int;
let n: int = byte_len(s) as int;
while n >= 0 { if s[n] == c { ret n; } n -= 1; }
ret n;
}
@ -390,15 +390,14 @@ fn slice(s: str, begin: uint, end: uint) -> str {
// FIXME: Typestate precondition
assert (begin <= end);
assert (end <= str::byte_len(s));
assert (end <= byte_len(s));
ret rustrt::str_slice(s, begin, end);
}
fn safe_slice(s: str, begin: uint, end: uint) : le(begin, end) -> str {
assert (end <=
str::byte_len(s)); // would need some magic to
// make this a precondition
// would need some magic to
// make this a precondition
assert (end <= byte_len(s));
ret rustrt::str_slice(s, begin, end);
}
@ -441,7 +440,7 @@ fn split(s: str, sep: u8) -> [str] {
ends_with_sep = true;
} else { accum += unsafe_from_byte(c); ends_with_sep = false; }
}
if str::byte_len(accum) != 0u || ends_with_sep { v += [accum]; }
if byte_len(accum) != 0u || ends_with_sep { v += [accum]; }
ret v;
}

View File

@ -1,9 +1,9 @@
// error-pattern: Unsatisfied precondition constraint (for example, le(a, b)
// error-pattern:precondition constraint (for example, uint::le(a, b)
use std;
import std::str::*;
import std::istr::*;
fn main() {
let a: uint = 4u;
let b: uint = 1u;
log_err safe_slice("kitties", a, b);
log_err safe_slice(~"kitties", a, b);
}

View File

@ -1,6 +1,6 @@
// error-pattern:Unsatisfied precondition constraint (for example, le(b, d
use std;
import std::str::*;
import std::istr::*;
import std::uint::*;
fn main() {
@ -16,5 +16,5 @@ fn main() {
// the next statement, since it's not true in the
// prestate.
let d <- a;
log safe_slice("kitties", b, d);
log safe_slice(~"kitties", b, d);
}

View File

@ -1,11 +1,11 @@
// error-pattern:Predicate le(a, b) failed
use std;
import std::str::*;
import std::istr::*;
import std::uint::le;
fn main() {
let a: uint = 4u;
let b: uint = 1u;
check (le(a, b));
log_err safe_slice("kitties", a, b);
log_err safe_slice(~"kitties", a, b);
}

View File

@ -2,16 +2,17 @@
// -*- rust -*-
use std;
import std::str;
import std::dbg;
// FIXME: import std::dbg.const_refcount. Currently
// cross-crate const references don't work.
const const_refcount: uint = 0x7bad_face_u;
tag t { make_t(str); clam; }
tag t { make_t(@int); clam; }
fn foo(s: str) {
fn foo(s: @int) {
let count = dbg::refcount(s);
let x: t = make_t(s); // ref up
alt x {
@ -21,15 +22,15 @@ fn foo(s: str) {
}
_ { log "?"; fail; }
}
log str::refcount(s);
assert (str::refcount(s) == const_refcount);
log dbg::refcount(s);
assert (dbg::refcount(s) == count + 1u);
}
fn main() {
let s: str = "hi"; // ref up
let s: @int = @0; // ref up
foo(s); // ref up then down
log str::refcount(s);
assert (str::refcount(s) == const_refcount);
log dbg::refcount(s);
assert (dbg::refcount(s) == 1u);
}

View File

@ -1,5 +1,5 @@
use std;
import std::str::*;
import std::istr::*;
import std::uint::*;
fn main() {
@ -8,5 +8,5 @@ fn main() {
let c: uint = 17u;
check (le(a, b));
c <- a;
log safe_slice("kitties", c, b);
log safe_slice(~"kitties", c, b);
}

View File

@ -1,5 +1,5 @@
use std;
import std::str::*;
import std::istr::*;
import std::uint::*;
fn main() {
@ -7,5 +7,5 @@ fn main() {
let b: uint = 4u;
check (le(a, b));
let c <- a;
log safe_slice("kitties", c, b);
log safe_slice(~"kitties", c, b);
}

View File

@ -1,5 +1,5 @@
use std;
import std::str::*;
import std::istr::*;
import std::uint::*;
fn main() {
@ -7,5 +7,5 @@ fn main() {
let b: uint = 1u;
check (le(b, a));
b <-> a;
log safe_slice("kitties", a, b);
log safe_slice(~"kitties", a, b);
}

View File

@ -1,5 +1,5 @@
use std;
import std::str::*;
import std::istr::*;
import std::uint::*;
fn main() {
@ -7,5 +7,5 @@ fn main() {
let b: uint = 4u;
check (le(a, b));
let c = b;
log safe_slice("kitties", a, c);
log safe_slice(~"kitties", a, c);
}

View File

@ -1,10 +1,10 @@
use std;
import std::str::*;
import std::istr::*;
import std::uint::*;
fn main() {
let a: uint = 1u;
let b: uint = 4u;
check (le(a, b));
log safe_slice("kitties", a, b);
log safe_slice(~"kitties", a, b);
}

View File

@ -9,7 +9,6 @@ use std;
import option = std::option::t;
import std::option::some;
import std::option::none;
import std::str;
import std::istr;
import std::vec;
import std::map;
@ -20,40 +19,42 @@ import std::comm::send;
import std::comm::recv;
import std::comm;
fn map(filename: str, emit: map_reduce::putter) { emit(filename, "1"); }
fn map(filename: &istr, emit: map_reduce::putter) { emit(filename, ~"1"); }
mod map_reduce {
export putter;
export mapper;
export map_reduce;
type putter = fn(str, str);
type putter = fn(&istr, &istr);
type mapper = fn(str, putter);
type mapper = fn(&istr, putter);
tag ctrl_proto { find_reducer([u8], chan<int>); mapper_done; }
fn start_mappers(ctrl: chan<ctrl_proto>, inputs: &[str]) {
for i: str in inputs { task::spawn(bind map_task(ctrl, i)); }
fn start_mappers(ctrl: chan<ctrl_proto>, inputs: &[istr]) {
for i: istr in inputs {
task::spawn(bind map_task(ctrl, i));
}
}
fn map_task(ctrl: chan<ctrl_proto>, input: str) {
fn map_task(ctrl: chan<ctrl_proto>, input: -istr) {
let intermediates = map::new_str_hash();
fn emit(im: &map::hashmap<istr, int>, ctrl: chan<ctrl_proto>,
key: str, val: str) {
key: &istr, val: &istr) {
let c;
alt im.find(istr::from_estr(key)) {
alt im.find(key) {
some(_c) { c = _c }
none. {
let p = port();
log_err "sending find_reducer";
send(ctrl, find_reducer(str::bytes(key), chan(p)));
send(ctrl, find_reducer(istr::bytes(key), chan(p)));
log_err "receiving";
c = recv(p);
log_err c;
im.insert(istr::from_estr(key), c);
im.insert(key, c);
}
}
}
@ -62,7 +63,7 @@ mod map_reduce {
send(ctrl, mapper_done);
}
fn map_reduce(inputs: &[str]) {
fn map_reduce(inputs: &[istr]) {
let ctrl = port();
// This task becomes the master control task. It spawns others
@ -93,5 +94,5 @@ mod map_reduce {
}
fn main() {
map_reduce::map_reduce(["../src/test/run-pass/hashmap-memory.rs"]);
map_reduce::map_reduce([~"../src/test/run-pass/hashmap-memory.rs"]);
}

View File

@ -1,31 +0,0 @@
use std;
import std::istr;
import std::vec;
fn main() {
// Chars of 1, 2, 3, and 4 bytes
let chs: [char] = ['e', 'é', '€', 0x10000 as char];
let s: istr = istr::from_chars(chs);
assert (istr::byte_len(s) == 10u);
assert (istr::char_len(s) == 4u);
assert (vec::len::<char>(istr::to_chars(s)) == 4u);
assert (istr::eq(istr::from_chars(istr::to_chars(s)), s));
assert (istr::char_at(s, 0u) == 'e');
assert (istr::char_at(s, 1u) == 'é');
assert (istr::is_utf8(istr::bytes(s)));
assert (!istr::is_utf8([0x80_u8]));
assert (!istr::is_utf8([0xc0_u8]));
assert (!istr::is_utf8([0xc0_u8, 0x10_u8]));
let stack = ~"a×c€";
assert (istr::pop_char(stack) == '€');
assert (istr::pop_char(stack) == 'c');
istr::push_char(stack, 'u');
assert (istr::eq(stack, ~"a×u"));
assert (istr::shift_char(stack) == 'a');
assert (istr::shift_char(stack) == '×');
istr::unshift_char(stack, 'ß');
assert (istr::eq(stack, ~"ßu"));
}

View File

@ -1,31 +1,31 @@
use std;
import std::str;
import std::istr;
import std::vec;
fn main() {
// Chars of 1, 2, 3, and 4 bytes
let chs: [char] = ['e', 'é', '€', 0x10000 as char];
let s: str = str::from_chars(chs);
let s: istr = istr::from_chars(chs);
assert (str::byte_len(s) == 10u);
assert (str::char_len(s) == 4u);
assert (vec::len::<char>(str::to_chars(s)) == 4u);
assert (str::eq(str::from_chars(str::to_chars(s)), s));
assert (str::char_at(s, 0u) == 'e');
assert (str::char_at(s, 1u) == 'é');
assert (istr::byte_len(s) == 10u);
assert (istr::char_len(s) == 4u);
assert (vec::len::<char>(istr::to_chars(s)) == 4u);
assert (istr::eq(istr::from_chars(istr::to_chars(s)), s));
assert (istr::char_at(s, 0u) == 'e');
assert (istr::char_at(s, 1u) == 'é');
assert (str::is_utf8(str::bytes(s)));
assert (!str::is_utf8([0x80_u8]));
assert (!str::is_utf8([0xc0_u8]));
assert (!str::is_utf8([0xc0_u8, 0x10_u8]));
assert (istr::is_utf8(istr::bytes(s)));
assert (!istr::is_utf8([0x80_u8]));
assert (!istr::is_utf8([0xc0_u8]));
assert (!istr::is_utf8([0xc0_u8, 0x10_u8]));
let stack = "a×c€";
assert (str::pop_char(stack) == '€');
assert (str::pop_char(stack) == 'c');
str::push_char(stack, 'u');
assert (str::eq(stack, "a×u"));
assert (str::shift_char(stack) == 'a');
assert (str::shift_char(stack) == '×');
str::unshift_char(stack, 'ß');
assert (str::eq(stack, "ßu"));
let stack = ~"a×c€";
assert (istr::pop_char(stack) == '€');
assert (istr::pop_char(stack) == 'c');
istr::push_char(stack, 'u');
assert (istr::eq(stack, ~"a×u"));
assert (istr::shift_char(stack) == 'a');
assert (istr::shift_char(stack) == '×');
istr::unshift_char(stack, 'ß');
assert (istr::eq(stack, ~"ßu"));
}

View File

@ -1,82 +0,0 @@
// -*- rust -*-
use std;
import std::str;
import std::vec;
// FIXME: import std::dbg::const_refcount. Currently
// cross-crate const references don't work.
const const_refcount: uint = 0x7bad_face_u;
fn fast_growth() {
let v: [int] = [1, 2, 3, 4, 5];
v += [6, 7, 8, 9, 0];
log v[9];
assert (v[0] == 1);
assert (v[7] == 8);
assert (v[9] == 0);
}
fn slow_growth() {
let v: [int] = [];
let u: [int] = v;
v += [17];
log v[0];
assert (v[0] == 17);
}
fn slow_growth2_helper(s: str) { // ref up: s
obj acc(mutable v: [str]) {
fn add(s: &str) { v += [s]; }
}
let ss: str = s; // ref up: s
let mumble: str = "mrghrm"; // ref up: mumble
{
/**
* Within this block, mumble goes into a vec that is referenced
* both by the local slot v and the acc's v data field. When we
* add(s) on the acc, its v undergoes a slow append (allocate a
* new vec, copy over existing elements). Here we're testing to
* see that this slow path goes over well. In particular, the
* copy of existing elements should increment the ref count of
* mumble, the existing str in the originally- shared vec.
*/
let v: [str] = [mumble]; // ref up: mumble
let a: acc = acc(v);
a.add(s); // ref up: mumble, s
log str::refcount(s);
log str::refcount(mumble);
assert (str::refcount(s) == const_refcount);
assert (str::refcount(mumble) == const_refcount);
log v[0];
log vec::len::<str>(v);
assert (str::eq(v[0], mumble));
assert (vec::len::<str>(v) == 1u);
} // ref down: mumble, s,
log str::refcount(s);
log str::refcount(mumble);
assert (str::refcount(s) == const_refcount);
assert (str::refcount(mumble) == const_refcount);
log mumble;
log ss;
}
// ref down
fn slow_growth2() {
let s: str = "hi"; // ref up: s
slow_growth2_helper(s);
log str::refcount(s);
assert (str::refcount(s) == const_refcount);
}
fn main() { fast_growth(); slow_growth(); slow_growth2(); }

View File

@ -1,287 +0,0 @@
import std::istr;
import std::vec;
#[test]
fn test_eq() {
assert istr::eq(~"", ~"");
assert istr::eq(~"foo", ~"foo");
assert !istr::eq(~"foo", ~"bar");
}
#[test]
fn test_lteq() {
assert istr::lteq(~"", ~"");
assert istr::lteq(~"", ~"foo");
assert istr::lteq(~"foo", ~"foo");
assert !istr::eq(~"foo", ~"bar");
}
#[test]
fn test_bytes_len() {
assert (istr::byte_len(~"") == 0u);
assert (istr::byte_len(~"hello world") == 11u);
assert (istr::byte_len(~"\x63") == 1u);
assert (istr::byte_len(~"\xa2") == 2u);
assert (istr::byte_len(~"\u03c0") == 2u);
assert (istr::byte_len(~"\u2620") == 3u);
assert (istr::byte_len(~"\U0001d11e") == 4u);
}
#[test]
fn test_index_and_rindex() {
assert (istr::index(~"hello", 'e' as u8) == 1);
assert (istr::index(~"hello", 'o' as u8) == 4);
assert (istr::index(~"hello", 'z' as u8) == -1);
assert (istr::rindex(~"hello", 'l' as u8) == 3);
assert (istr::rindex(~"hello", 'h' as u8) == 0);
assert (istr::rindex(~"hello", 'z' as u8) == -1);
}
#[test]
fn test_split() {
fn t(s: &istr, c: char, i: int, k: &istr) {
log ~"splitting: " + s;
log i;
let v = istr::split(s, c as u8);
log ~"split to: ";
for z: istr in v { log z; }
log ~"comparing: " + v[i] + ~" vs. " + k;
assert (istr::eq(v[i], k));
}
t(~"abc.hello.there", '.', 0, ~"abc");
t(~"abc.hello.there", '.', 1, ~"hello");
t(~"abc.hello.there", '.', 2, ~"there");
t(~".hello.there", '.', 0, ~"");
t(~".hello.there", '.', 1, ~"hello");
t(~"...hello.there.", '.', 3, ~"hello");
t(~"...hello.there.", '.', 5, ~"");
}
#[test]
fn test_find() {
fn t(haystack: &istr, needle: &istr, i: int) {
let j: int = istr::find(haystack, needle);
log ~"searched for " + needle;
log j;
assert (i == j);
}
t(~"this is a simple", ~"is a", 5);
t(~"this is a simple", ~"is z", -1);
t(~"this is a simple", ~"", 0);
t(~"this is a simple", ~"simple", 10);
t(~"this", ~"simple", -1);
}
#[test]
fn test_substr() {
fn t(a: &istr, b: &istr, start: int) {
assert (istr::eq(istr::substr(a, start as uint,
istr::byte_len(b)), b));
}
t(~"hello", ~"llo", 2);
t(~"hello", ~"el", 1);
t(~"substr should not be a challenge", ~"not", 14);
}
#[test]
fn test_concat() {
fn t(v: &[istr], s: &istr) { assert (istr::eq(istr::concat(v), s)); }
t([~"you", ~"know", ~"I'm", ~"no", ~"good"], ~"youknowI'mnogood");
let v: [istr] = [];
t(v, ~"");
t([~"hi"], ~"hi");
}
#[test]
fn test_connect() {
fn t(v: &[istr], sep: &istr, s: &istr) {
assert (istr::eq(istr::connect(v, sep), s));
}
t([~"you", ~"know", ~"I'm", ~"no", ~"good"], ~" ",
~"you know I'm no good");
let v: [istr] = [];
t(v, ~" ", ~"");
t([~"hi"], ~" ", ~"hi");
}
#[test]
fn test_to_upper() {
// to_upper doesn't understand unicode yet,
// but we need to at least preserve it
let unicode = ~"\u65e5\u672c";
let input = ~"abcDEF" + unicode + ~"xyz:.;";
let expected = ~"ABCDEF" + unicode + ~"XYZ:.;";
let actual = istr::to_upper(input);
assert (istr::eq(expected, actual));
}
#[test]
fn test_slice() {
assert (istr::eq(~"ab", istr::slice(~"abc", 0u, 2u)));
assert (istr::eq(~"bc", istr::slice(~"abc", 1u, 3u)));
assert (istr::eq(~"", istr::slice(~"abc", 1u, 1u)));
fn a_million_letter_a() -> istr {
let i = 0;
let rs = ~"";
while i < 100000 { rs += ~"aaaaaaaaaa"; i += 1; }
ret rs;
}
fn half_a_million_letter_a() -> istr {
let i = 0;
let rs = ~"";
while i < 100000 { rs += ~"aaaaa"; i += 1; }
ret rs;
}
assert (istr::eq(half_a_million_letter_a(),
istr::slice(a_million_letter_a(), 0u, 500000u)));
}
#[test]
fn test_starts_with() {
assert (istr::starts_with(~"", ~""));
assert (istr::starts_with(~"abc", ~""));
assert (istr::starts_with(~"abc", ~"a"));
assert (!istr::starts_with(~"a", ~"abc"));
assert (!istr::starts_with(~"", ~"abc"));
}
#[test]
fn test_ends_with() {
assert (istr::ends_with(~"", ~""));
assert (istr::ends_with(~"abc", ~""));
assert (istr::ends_with(~"abc", ~"c"));
assert (!istr::ends_with(~"a", ~"abc"));
assert (!istr::ends_with(~"", ~"abc"));
}
#[test]
fn test_is_empty() {
assert (istr::is_empty(~""));
assert (!istr::is_empty(~"a"));
}
#[test]
fn test_is_not_empty() {
assert (istr::is_not_empty(~"a"));
assert (!istr::is_not_empty(~""));
}
#[test]
fn test_replace() {
let a = ~"a";
check (istr::is_not_empty(a));
assert (istr::replace(~"", a, ~"b") == ~"");
assert (istr::replace(~"a", a, ~"b") == ~"b");
assert (istr::replace(~"ab", a, ~"b") == ~"bb");
let test = ~"test";
check (istr::is_not_empty(test));
assert (istr::replace(~" test test ", test, ~"toast")
== ~" toast toast ");
assert (istr::replace(~" test test ", test, ~"") == ~" ");
}
#[test]
fn test_char_slice() {
assert (istr::eq(~"ab", istr::char_slice(~"abc", 0u, 2u)));
assert (istr::eq(~"bc", istr::char_slice(~"abc", 1u, 3u)));
assert (istr::eq(~"", istr::char_slice(~"abc", 1u, 1u)));
assert (istr::eq(~"\u65e5", istr::char_slice(~"\u65e5\u672c", 0u, 1u)));
}
#[test]
fn trim_left() {
assert (istr::trim_left(~"") == ~"");
assert (istr::trim_left(~"a") == ~"a");
assert (istr::trim_left(~" ") == ~"");
assert (istr::trim_left(~" blah") == ~"blah");
assert (istr::trim_left(~" \u3000 wut") == ~"wut");
assert (istr::trim_left(~"hey ") == ~"hey ");
}
#[test]
fn trim_right() {
assert (istr::trim_right(~"") == ~"");
assert (istr::trim_right(~"a") == ~"a");
assert (istr::trim_right(~" ") == ~"");
assert (istr::trim_right(~"blah ") == ~"blah");
assert (istr::trim_right(~"wut \u3000 ") == ~"wut");
assert (istr::trim_right(~" hey") == ~" hey");
}
#[test]
fn trim() {
assert (istr::trim(~"") == ~"");
assert (istr::trim(~"a") == ~"a");
assert (istr::trim(~" ") == ~"");
assert (istr::trim(~" blah ") == ~"blah");
assert (istr::trim(~"\nwut \u3000 ") == ~"wut");
assert (istr::trim(~" hey dude ") == ~"hey dude");
}
#[test]
fn is_whitespace() {
assert (istr::is_whitespace(~""));
assert (istr::is_whitespace(~" "));
assert (istr::is_whitespace(~"\u2009")); // Thin space
assert (istr::is_whitespace(~" \n\t "));
assert (!istr::is_whitespace(~" _ "));
}
#[test]
fn is_ascii() {
assert istr::is_ascii(~"");
assert istr::is_ascii(~"a");
assert !istr::is_ascii(~"\u2009");
}
#[test]
fn shift_byte() {
let s = ~"ABC";
let b = istr::shift_byte(s);
assert s == ~"BC";
assert b == 65u8;
}
#[test]
fn pop_byte() {
let s = ~"ABC";
let b = istr::pop_byte(s);
assert s == ~"AB";
assert b == 67u8;
}
#[test]
fn unsafe_from_bytes() {
let a = [65u8, 65u8, 65u8, 65u8, 65u8, 65u8, 65u8];
let b = istr::unsafe_from_bytes(a);
assert b == ~"AAAAAAA";
}
#[test]
fn str_from_cstr() {
let a = [65u8, 65u8, 65u8, 65u8, 65u8, 65u8, 65u8, 0u8];
let b = vec::to_ptr(a);
let c = istr::str_from_cstr(b);
assert c == ~"AAAAAAA";
}
#[test]
fn as_buf() {
let a = ~"Abcdefg";
let b = istr::as_buf(a, { |buf|
assert *buf == 65u8;
100
});
assert b == 100;
}
#[test]
fn as_buf_small() {
let a = ~"A";
let b = istr::as_buf(a, { |buf|
assert *buf == 65u8;
100
});
assert b == 100;
}

View File

@ -3,7 +3,6 @@
// -*- rust -*-
use std;
import std::map;
import std::str;
import std::istr;
import std::uint;
import std::util;
@ -15,8 +14,8 @@ fn test_simple() {
fn eq_uint(x: &uint, y: &uint) -> bool { ret x == y; }
let hasher_uint: map::hashfn<uint> = util::id;
let eqer_uint: map::eqfn<uint> = eq_uint;
let hasher_str: map::hashfn<str> = str::hash;
let eqer_str: map::eqfn<str> = str::eq;
let hasher_str: map::hashfn<istr> = istr::hash;
let eqer_str: map::eqfn<istr> = istr::eq;
log "uint -> uint";
let hm_uu: map::hashmap<uint, uint> =
map::mk_hashmap::<uint, uint>(hasher_uint, eqer_uint);
@ -30,49 +29,49 @@ fn test_simple() {
assert (hm_uu.get(12u) == 14u);
assert (!hm_uu.insert(12u, 12u));
assert (hm_uu.get(12u) == 12u);
let ten: str = "ten";
let eleven: str = "eleven";
let twelve: str = "twelve";
let ten: istr = ~"ten";
let eleven: istr = ~"eleven";
let twelve: istr = ~"twelve";
log "str -> uint";
let hm_su: map::hashmap<str, uint> =
map::mk_hashmap::<str, uint>(hasher_str, eqer_str);
assert (hm_su.insert("ten", 12u));
let hm_su: map::hashmap<istr, uint> =
map::mk_hashmap::<istr, uint>(hasher_str, eqer_str);
assert (hm_su.insert(~"ten", 12u));
assert (hm_su.insert(eleven, 13u));
assert (hm_su.insert("twelve", 14u));
assert (hm_su.insert(~"twelve", 14u));
assert (hm_su.get(eleven) == 13u);
assert (hm_su.get("eleven") == 13u);
assert (hm_su.get("twelve") == 14u);
assert (hm_su.get("ten") == 12u);
assert (!hm_su.insert("twelve", 14u));
assert (hm_su.get("twelve") == 14u);
assert (!hm_su.insert("twelve", 12u));
assert (hm_su.get("twelve") == 12u);
assert (hm_su.get(~"eleven") == 13u);
assert (hm_su.get(~"twelve") == 14u);
assert (hm_su.get(~"ten") == 12u);
assert (!hm_su.insert(~"twelve", 14u));
assert (hm_su.get(~"twelve") == 14u);
assert (!hm_su.insert(~"twelve", 12u));
assert (hm_su.get(~"twelve") == 12u);
log "uint -> str";
let hm_us: map::hashmap<uint, str> =
map::mk_hashmap::<uint, str>(hasher_uint, eqer_uint);
assert (hm_us.insert(10u, "twelve"));
assert (hm_us.insert(11u, "thirteen"));
assert (hm_us.insert(12u, "fourteen"));
assert (str::eq(hm_us.get(11u), "thirteen"));
assert (str::eq(hm_us.get(12u), "fourteen"));
assert (str::eq(hm_us.get(10u), "twelve"));
assert (!hm_us.insert(12u, "fourteen"));
assert (str::eq(hm_us.get(12u), "fourteen"));
assert (!hm_us.insert(12u, "twelve"));
assert (str::eq(hm_us.get(12u), "twelve"));
let hm_us: map::hashmap<uint, istr> =
map::mk_hashmap::<uint, istr>(hasher_uint, eqer_uint);
assert (hm_us.insert(10u, ~"twelve"));
assert (hm_us.insert(11u, ~"thirteen"));
assert (hm_us.insert(12u, ~"fourteen"));
assert (istr::eq(hm_us.get(11u), ~"thirteen"));
assert (istr::eq(hm_us.get(12u), ~"fourteen"));
assert (istr::eq(hm_us.get(10u), ~"twelve"));
assert (!hm_us.insert(12u, ~"fourteen"));
assert (istr::eq(hm_us.get(12u), ~"fourteen"));
assert (!hm_us.insert(12u, ~"twelve"));
assert (istr::eq(hm_us.get(12u), ~"twelve"));
log "str -> str";
let hm_ss: map::hashmap<str, str> =
map::mk_hashmap::<str, str>(hasher_str, eqer_str);
assert (hm_ss.insert(ten, "twelve"));
assert (hm_ss.insert(eleven, "thirteen"));
assert (hm_ss.insert(twelve, "fourteen"));
assert (str::eq(hm_ss.get("eleven"), "thirteen"));
assert (str::eq(hm_ss.get("twelve"), "fourteen"));
assert (str::eq(hm_ss.get("ten"), "twelve"));
assert (!hm_ss.insert("twelve", "fourteen"));
assert (str::eq(hm_ss.get("twelve"), "fourteen"));
assert (!hm_ss.insert("twelve", "twelve"));
assert (str::eq(hm_ss.get("twelve"), "twelve"));
let hm_ss: map::hashmap<istr, istr> =
map::mk_hashmap::<istr, istr>(hasher_str, eqer_str);
assert (hm_ss.insert(ten, ~"twelve"));
assert (hm_ss.insert(eleven, ~"thirteen"));
assert (hm_ss.insert(twelve, ~"fourteen"));
assert (istr::eq(hm_ss.get(~"eleven"), ~"thirteen"));
assert (istr::eq(hm_ss.get(~"twelve"), ~"fourteen"));
assert (istr::eq(hm_ss.get(~"ten"), ~"twelve"));
assert (!hm_ss.insert(~"twelve", ~"fourteen"));
assert (istr::eq(hm_ss.get(~"twelve"), ~"fourteen"));
assert (!hm_ss.insert(~"twelve", ~"twelve"));
assert (istr::eq(hm_ss.get(~"twelve"), ~"twelve"));
log "*** finished test_simple";
}
@ -117,14 +116,14 @@ fn test_growth() {
i += 1u;
}
log "str -> str";
let hasher_str: map::hashfn<str> = str::hash;
let eqer_str: map::eqfn<str> = str::eq;
let hm_ss: map::hashmap<str, str> =
map::mk_hashmap::<str, str>(hasher_str, eqer_str);
let hasher_str: map::hashfn<istr> = istr::hash;
let eqer_str: map::eqfn<istr> = istr::eq;
let hm_ss: map::hashmap<istr, istr> =
map::mk_hashmap::<istr, istr>(hasher_str, eqer_str);
i = 0u;
while i < num_to_insert {
assert (hm_ss.insert(istr::to_estr(uint::to_str(i, 2u)),
istr::to_estr(uint::to_str(i * i, 2u))));
assert (hm_ss.insert(uint::to_str(i, 2u),
uint::to_str(i * i, 2u)));
log ~"inserting \"" + uint::to_str(i, 2u) + ~"\" -> \"" +
uint::to_str(i * i, 2u) + ~"\"";
i += 1u;
@ -132,25 +131,25 @@ fn test_growth() {
log "-----";
i = 0u;
while i < num_to_insert {
log "get(\"" + istr::to_estr(uint::to_str(i, 2u)) + "\") = \"" +
hm_ss.get(istr::to_estr(uint::to_str(i, 2u))) + "\"";
assert (str::eq(hm_ss.get(istr::to_estr(uint::to_str(i, 2u))),
istr::to_estr(uint::to_str(i * i, 2u))));
log ~"get(\"" + uint::to_str(i, 2u) + ~"\") = \"" +
hm_ss.get(uint::to_str(i, 2u)) + ~"\"";
assert (istr::eq(hm_ss.get(uint::to_str(i, 2u)),
uint::to_str(i * i, 2u)));
i += 1u;
}
assert (hm_ss.insert(istr::to_estr(uint::to_str(num_to_insert, 2u)),
istr::to_estr(uint::to_str(17u, 2u))));
assert (str::eq(hm_ss.get(
istr::to_estr(uint::to_str(num_to_insert, 2u))),
istr::to_estr(uint::to_str(17u, 2u))));
assert (hm_ss.insert(uint::to_str(num_to_insert, 2u),
uint::to_str(17u, 2u)));
assert (istr::eq(hm_ss.get(
uint::to_str(num_to_insert, 2u)),
uint::to_str(17u, 2u)));
log "-----";
hm_ss.rehash();
i = 0u;
while i < num_to_insert {
log "get(\"" + istr::to_estr(uint::to_str(i, 2u)) + "\") = \"" +
hm_ss.get(istr::to_estr(uint::to_str(i, 2u))) + "\"";
assert (str::eq(hm_ss.get(istr::to_estr(uint::to_str(i, 2u))),
istr::to_estr(uint::to_str(i * i, 2u))));
log ~"get(\"" + uint::to_str(i, 2u) + ~"\") = \"" +
hm_ss.get(uint::to_str(i, 2u)) + ~"\"";
assert (istr::eq(hm_ss.get(uint::to_str(i, 2u)),
uint::to_str(i * i, 2u)));
i += 1u;
}
log "*** finished test_growth";
@ -249,18 +248,18 @@ fn test_removal() {
#[test]
fn test_contains_key() {
let key = "k";
let map = map::mk_hashmap::<str, str>(str::hash, str::eq);
let key = ~"k";
let map = map::mk_hashmap::<istr, istr>(istr::hash, istr::eq);
assert (!map.contains_key(key));
map.insert(key, "val");
map.insert(key, ~"val");
assert (map.contains_key(key));
}
#[test]
fn test_find() {
let key = "k";
let map = map::mk_hashmap::<str, str>(str::hash, str::eq);
let key = ~"k";
let map = map::mk_hashmap::<istr, istr>(istr::hash, istr::eq);
assert (std::option::is_none(map.find(key)));
map.insert(key, "val");
assert (std::option::get(map.find(key)) == "val");
map.insert(key, ~"val");
assert (std::option::get(map.find(key)) == ~"val");
}

View File

@ -26,7 +26,6 @@ mod sha1;
mod sort;
mod str_buf;
mod str;
mod istr;
mod task;
mod test;
mod uint;

View File

@ -1,90 +1,107 @@
use std;
import std::str;
import std::istr;
import std::vec;
#[test]
fn test_eq() {
assert istr::eq(~"", ~"");
assert istr::eq(~"foo", ~"foo");
assert !istr::eq(~"foo", ~"bar");
}
#[test]
fn test_lteq() {
assert istr::lteq(~"", ~"");
assert istr::lteq(~"", ~"foo");
assert istr::lteq(~"foo", ~"foo");
assert !istr::eq(~"foo", ~"bar");
}
#[test]
fn test_bytes_len() {
assert (str::byte_len("") == 0u);
assert (str::byte_len("hello world") == 11u);
assert (str::byte_len("\x63") == 1u);
assert (str::byte_len("\xa2") == 2u);
assert (str::byte_len("\u03c0") == 2u);
assert (str::byte_len("\u2620") == 3u);
assert (str::byte_len("\U0001d11e") == 4u);
assert (istr::byte_len(~"") == 0u);
assert (istr::byte_len(~"hello world") == 11u);
assert (istr::byte_len(~"\x63") == 1u);
assert (istr::byte_len(~"\xa2") == 2u);
assert (istr::byte_len(~"\u03c0") == 2u);
assert (istr::byte_len(~"\u2620") == 3u);
assert (istr::byte_len(~"\U0001d11e") == 4u);
}
#[test]
fn test_index_and_rindex() {
assert (str::index("hello", 'e' as u8) == 1);
assert (str::index("hello", 'o' as u8) == 4);
assert (str::index("hello", 'z' as u8) == -1);
assert (str::rindex("hello", 'l' as u8) == 3);
assert (str::rindex("hello", 'h' as u8) == 0);
assert (str::rindex("hello", 'z' as u8) == -1);
assert (istr::index(~"hello", 'e' as u8) == 1);
assert (istr::index(~"hello", 'o' as u8) == 4);
assert (istr::index(~"hello", 'z' as u8) == -1);
assert (istr::rindex(~"hello", 'l' as u8) == 3);
assert (istr::rindex(~"hello", 'h' as u8) == 0);
assert (istr::rindex(~"hello", 'z' as u8) == -1);
}
#[test]
fn test_split() {
fn t(s: &str, c: char, i: int, k: &str) {
log "splitting: " + s;
fn t(s: &istr, c: char, i: int, k: &istr) {
log ~"splitting: " + s;
log i;
let v = str::split(s, c as u8);
log "split to: ";
for z: str in v { log z; }
log "comparing: " + v[i] + " vs. " + k;
assert (str::eq(v[i], k));
let v = istr::split(s, c as u8);
log ~"split to: ";
for z: istr in v { log z; }
log ~"comparing: " + v[i] + ~" vs. " + k;
assert (istr::eq(v[i], k));
}
t("abc.hello.there", '.', 0, "abc");
t("abc.hello.there", '.', 1, "hello");
t("abc.hello.there", '.', 2, "there");
t(".hello.there", '.', 0, "");
t(".hello.there", '.', 1, "hello");
t("...hello.there.", '.', 3, "hello");
t("...hello.there.", '.', 5, "");
t(~"abc.hello.there", '.', 0, ~"abc");
t(~"abc.hello.there", '.', 1, ~"hello");
t(~"abc.hello.there", '.', 2, ~"there");
t(~".hello.there", '.', 0, ~"");
t(~".hello.there", '.', 1, ~"hello");
t(~"...hello.there.", '.', 3, ~"hello");
t(~"...hello.there.", '.', 5, ~"");
}
#[test]
fn test_find() {
fn t(haystack: &str, needle: &str, i: int) {
let j: int = str::find(haystack, needle);
log "searched for " + needle;
fn t(haystack: &istr, needle: &istr, i: int) {
let j: int = istr::find(haystack, needle);
log ~"searched for " + needle;
log j;
assert (i == j);
}
t("this is a simple", "is a", 5);
t("this is a simple", "is z", -1);
t("this is a simple", "", 0);
t("this is a simple", "simple", 10);
t("this", "simple", -1);
t(~"this is a simple", ~"is a", 5);
t(~"this is a simple", ~"is z", -1);
t(~"this is a simple", ~"", 0);
t(~"this is a simple", ~"simple", 10);
t(~"this", ~"simple", -1);
}
#[test]
fn test_substr() {
fn t(a: &str, b: &str, start: int) {
assert (str::eq(str::substr(a, start as uint, str::byte_len(b)), b));
fn t(a: &istr, b: &istr, start: int) {
assert (istr::eq(istr::substr(a, start as uint,
istr::byte_len(b)), b));
}
t("hello", "llo", 2);
t("hello", "el", 1);
t("substr should not be a challenge", "not", 14);
t(~"hello", ~"llo", 2);
t(~"hello", ~"el", 1);
t(~"substr should not be a challenge", ~"not", 14);
}
#[test]
fn test_concat() {
fn t(v: &[str], s: &str) { assert (str::eq(str::concat(v), s)); }
t(["you", "know", "I'm", "no", "good"], "youknowI'mnogood");
let v: [str] = [];
t(v, "");
t(["hi"], "hi");
fn t(v: &[istr], s: &istr) { assert (istr::eq(istr::concat(v), s)); }
t([~"you", ~"know", ~"I'm", ~"no", ~"good"], ~"youknowI'mnogood");
let v: [istr] = [];
t(v, ~"");
t([~"hi"], ~"hi");
}
#[test]
fn test_connect() {
fn t(v: &[str], sep: &str, s: &str) {
assert (str::eq(str::connect(v, sep), s));
fn t(v: &[istr], sep: &istr, s: &istr) {
assert (istr::eq(istr::connect(v, sep), s));
}
t(["you", "know", "I'm", "no", "good"], " ", "you know I'm no good");
let v: [str] = [];
t(v, " ", "");
t(["hi"], " ", "hi");
t([~"you", ~"know", ~"I'm", ~"no", ~"good"], ~" ",
~"you know I'm no good");
let v: [istr] = [];
t(v, ~" ", ~"");
t([~"hi"], ~" ", ~"hi");
}
#[test]
@ -92,120 +109,179 @@ fn test_to_upper() {
// to_upper doesn't understand unicode yet,
// but we need to at least preserve it
let unicode = "\u65e5\u672c";
let input = "abcDEF" + unicode + "xyz:.;";
let expected = "ABCDEF" + unicode + "XYZ:.;";
let actual = str::to_upper(input);
assert (str::eq(expected, actual));
let unicode = ~"\u65e5\u672c";
let input = ~"abcDEF" + unicode + ~"xyz:.;";
let expected = ~"ABCDEF" + unicode + ~"XYZ:.;";
let actual = istr::to_upper(input);
assert (istr::eq(expected, actual));
}
#[test]
fn test_slice() {
assert (str::eq("ab", str::slice("abc", 0u, 2u)));
assert (str::eq("bc", str::slice("abc", 1u, 3u)));
assert (str::eq("", str::slice("abc", 1u, 1u)));
fn a_million_letter_a() -> str {
assert (istr::eq(~"ab", istr::slice(~"abc", 0u, 2u)));
assert (istr::eq(~"bc", istr::slice(~"abc", 1u, 3u)));
assert (istr::eq(~"", istr::slice(~"abc", 1u, 1u)));
fn a_million_letter_a() -> istr {
let i = 0;
let rs = "";
while i < 100000 { rs += "aaaaaaaaaa"; i += 1; }
let rs = ~"";
while i < 100000 { rs += ~"aaaaaaaaaa"; i += 1; }
ret rs;
}
fn half_a_million_letter_a() -> str {
fn half_a_million_letter_a() -> istr {
let i = 0;
let rs = "";
while i < 100000 { rs += "aaaaa"; i += 1; }
let rs = ~"";
while i < 100000 { rs += ~"aaaaa"; i += 1; }
ret rs;
}
assert (str::eq(half_a_million_letter_a(),
str::slice(a_million_letter_a(), 0u, 500000u)));
assert (istr::eq(half_a_million_letter_a(),
istr::slice(a_million_letter_a(), 0u, 500000u)));
}
#[test]
fn test_starts_with() {
assert (istr::starts_with(~"", ~""));
assert (istr::starts_with(~"abc", ~""));
assert (istr::starts_with(~"abc", ~"a"));
assert (!istr::starts_with(~"a", ~"abc"));
assert (!istr::starts_with(~"", ~"abc"));
}
#[test]
fn test_ends_with() {
assert (str::ends_with("", ""));
assert (str::ends_with("abc", ""));
assert (str::ends_with("abc", "c"));
assert (!str::ends_with("a", "abc"));
assert (!str::ends_with("", "abc"));
assert (istr::ends_with(~"", ~""));
assert (istr::ends_with(~"abc", ~""));
assert (istr::ends_with(~"abc", ~"c"));
assert (!istr::ends_with(~"a", ~"abc"));
assert (!istr::ends_with(~"", ~"abc"));
}
#[test]
fn test_is_empty() {
assert (str::is_empty(""));
assert (!str::is_empty("a"));
assert (istr::is_empty(~""));
assert (!istr::is_empty(~"a"));
}
#[test]
fn test_is_not_empty() {
assert (str::is_not_empty("a"));
assert (!str::is_not_empty(""));
assert (istr::is_not_empty(~"a"));
assert (!istr::is_not_empty(~""));
}
#[test]
fn test_replace() {
let a = "a";
check (str::is_not_empty(a));
assert (str::replace("", a, "b") == "");
assert (str::replace("a", a, "b") == "b");
assert (str::replace("ab", a, "b") == "bb");
let test = "test";
check (str::is_not_empty(test));
assert (str::replace(" test test ", test, "toast") == " toast toast ");
assert (str::replace(" test test ", test, "") == " ");
let a = ~"a";
check (istr::is_not_empty(a));
assert (istr::replace(~"", a, ~"b") == ~"");
assert (istr::replace(~"a", a, ~"b") == ~"b");
assert (istr::replace(~"ab", a, ~"b") == ~"bb");
let test = ~"test";
check (istr::is_not_empty(test));
assert (istr::replace(~" test test ", test, ~"toast")
== ~" toast toast ");
assert (istr::replace(~" test test ", test, ~"") == ~" ");
}
#[test]
fn test_char_slice() {
assert (str::eq("ab", str::char_slice("abc", 0u, 2u)));
assert (str::eq("bc", str::char_slice("abc", 1u, 3u)));
assert (str::eq("", str::char_slice("abc", 1u, 1u)));
assert (str::eq("\u65e5", str::char_slice("\u65e5\u672c", 0u, 1u)));
assert (istr::eq(~"ab", istr::char_slice(~"abc", 0u, 2u)));
assert (istr::eq(~"bc", istr::char_slice(~"abc", 1u, 3u)));
assert (istr::eq(~"", istr::char_slice(~"abc", 1u, 1u)));
assert (istr::eq(~"\u65e5", istr::char_slice(~"\u65e5\u672c", 0u, 1u)));
}
#[test]
fn trim_left() {
assert (str::trim_left("") == "");
assert (str::trim_left("a") == "a");
assert (str::trim_left(" ") == "");
assert (str::trim_left(" blah") == "blah");
assert (str::trim_left(" \u3000 wut") == "wut");
assert (str::trim_left("hey ") == "hey ");
assert (istr::trim_left(~"") == ~"");
assert (istr::trim_left(~"a") == ~"a");
assert (istr::trim_left(~" ") == ~"");
assert (istr::trim_left(~" blah") == ~"blah");
assert (istr::trim_left(~" \u3000 wut") == ~"wut");
assert (istr::trim_left(~"hey ") == ~"hey ");
}
#[test]
fn trim_right() {
assert (str::trim_right("") == "");
assert (str::trim_right("a") == "a");
assert (str::trim_right(" ") == "");
assert (str::trim_right("blah ") == "blah");
assert (str::trim_right("wut \u3000 ") == "wut");
assert (str::trim_right(" hey") == " hey");
assert (istr::trim_right(~"") == ~"");
assert (istr::trim_right(~"a") == ~"a");
assert (istr::trim_right(~" ") == ~"");
assert (istr::trim_right(~"blah ") == ~"blah");
assert (istr::trim_right(~"wut \u3000 ") == ~"wut");
assert (istr::trim_right(~" hey") == ~" hey");
}
#[test]
fn trim() {
assert (str::trim("") == "");
assert (str::trim("a") == "a");
assert (str::trim(" ") == "");
assert (str::trim(" blah ") == "blah");
assert (str::trim("\nwut \u3000 ") == "wut");
assert (str::trim(" hey dude ") == "hey dude");
assert (istr::trim(~"") == ~"");
assert (istr::trim(~"a") == ~"a");
assert (istr::trim(~" ") == ~"");
assert (istr::trim(~" blah ") == ~"blah");
assert (istr::trim(~"\nwut \u3000 ") == ~"wut");
assert (istr::trim(~" hey dude ") == ~"hey dude");
}
#[test]
fn is_whitespace() {
assert (str::is_whitespace(""));
assert (str::is_whitespace(" "));
assert (str::is_whitespace("\u2009")); // Thin space
assert (str::is_whitespace(" \n\t "));
assert (!str::is_whitespace(" _ "));
assert (istr::is_whitespace(~""));
assert (istr::is_whitespace(~" "));
assert (istr::is_whitespace(~"\u2009")); // Thin space
assert (istr::is_whitespace(~" \n\t "));
assert (!istr::is_whitespace(~" _ "));
}
// Local Variables:
// mode: rust;
// fill-column: 78;
// indent-tabs-mode: nil
// c-basic-offset: 4
// buffer-file-coding-system: utf-8-unix
// compile-command: "make -k -C $RBUILD 2>&1 | sed -e 's/\\/x\\//x:\\//g'";
// End:
#[test]
fn is_ascii() {
assert istr::is_ascii(~"");
assert istr::is_ascii(~"a");
assert !istr::is_ascii(~"\u2009");
}
#[test]
fn shift_byte() {
let s = ~"ABC";
let b = istr::shift_byte(s);
assert s == ~"BC";
assert b == 65u8;
}
#[test]
fn pop_byte() {
let s = ~"ABC";
let b = istr::pop_byte(s);
assert s == ~"AB";
assert b == 67u8;
}
#[test]
fn unsafe_from_bytes() {
let a = [65u8, 65u8, 65u8, 65u8, 65u8, 65u8, 65u8];
let b = istr::unsafe_from_bytes(a);
assert b == ~"AAAAAAA";
}
#[test]
fn str_from_cstr() {
let a = [65u8, 65u8, 65u8, 65u8, 65u8, 65u8, 65u8, 0u8];
let b = vec::to_ptr(a);
let c = istr::str_from_cstr(b);
assert c == ~"AAAAAAA";
}
#[test]
fn as_buf() {
let a = ~"Abcdefg";
let b = istr::as_buf(a, { |buf|
assert *buf == 65u8;
100
});
assert b == 100;
}
#[test]
fn as_buf_small() {
let a = ~"A";
let b = istr::as_buf(a, { |buf|
assert *buf == 65u8;
100
});
assert b == 100;
}

View File

@ -2,14 +2,12 @@
// -*- rust -*-
use std;
import std::str;
import std::istr;
#[test]
fn test() {
let s = "hello";
let sb = str::buf(s);
let s_cstr = str::str_from_cstr(sb);
assert (str::eq(s_cstr, s));
let s_buf = str::str_from_buf(sb, 5u);
assert (str::eq(s_buf, s));
let s = ~"hello";
let sb = istr::as_buf(s, { |b| b });
let s_cstr = istr::str_from_cstr(sb);
assert (istr::eq(s_cstr, s));
}

View File

@ -2,7 +2,7 @@ use std;
import std::treemap::*;
import std::option::some;
import std::option::none;
import std::str;
import std::istr;
#[test]
fn init_treemap() {
@ -63,12 +63,12 @@ fn traverse_in_order() {
fn u8_map() {
let m = init();
let k1 = str::bytes("foo");
let k2 = str::bytes("bar");
let k1 = istr::bytes(~"foo");
let k2 = istr::bytes(~"bar");
insert(m, k1, "foo");
insert(m, k2, "bar");
insert(m, k1, ~"foo");
insert(m, k2, ~"bar");
assert(find(m, k2) == some("bar"));
assert(find(m, k1) == some("foo"));
assert(find(m, k2) == some(~"bar"));
assert(find(m, k1) == some(~"foo"));
}

View File

@ -2,17 +2,17 @@
// -*- rust -*-
use std;
import std::str;
import std::istr;
import std::vec;
#[test]
fn test_simple() {
let s1: str = "All mimsy were the borogoves";
let s1: istr = ~"All mimsy were the borogoves";
let v: [u8] = str::bytes(s1);
let s2: str = str::unsafe_from_bytes(v);
let v: [u8] = istr::bytes(s1);
let s2: istr = istr::unsafe_from_bytes(v);
let i: uint = 0u;
let n1: uint = str::byte_len(s1);
let n1: uint = istr::byte_len(s1);
let n2: uint = vec::len::<u8>(v);
assert (n1 == n2);
while i < n1 {
@ -23,6 +23,4 @@ fn test_simple() {
assert (a == b);
i += 1u;
}
log "refcnt is";
log str::refcount(s1);
}