All pastes #197961 Raw Edit

Miscellany

public text v1 · immutable
#197961 ·published 2006-10-11 17:13 UTC
rendered paste body
unittest
{
	string s = string("foo"c);
	string t = string("Oh noes!"w);
	string u = string("% on a plne!"d);
	string v = string(string(string("WAaah! D:"c)[0..2].toUTF16() ~ "arrr! D:"w).toUTF32()); // make some chaos thingie

	assert(s.type() == typeid(typeof("foo"c)));
	assert(s.type() != typeid(typeof("Oh noes!"w)));
	assert(t.type() != typeid(typeof("% on a plne!"d)));
	assert(v.type() == u.type() );

	assert(s.startsWith("f"c));
	assert(s.startsWith("fo"w));
	assert(s.startsWith("foo"c));
	assert(!s.startsWith("fooo"c));
	assert(!s.startsWith("F"c));
	assert(s.startsWith("F"c, true));
	assert(!s.startsWith("E"c, true));
	assert(s.endsWith("o"c));
	assert(s.endsWith("oo"d));
	assert(s.endsWith("foo"c));
	assert(!s.endsWith("fooo"c));
	assert(!s.endsWith("ooo"c));
	assert(s.endsWith("O"c, true));
	assert(!s.endsWith("P"c, true));
	assert((s.dup ~ t.dup ~ u.dup ~ v.dup).startsWith(s));
	assert((s.dup ~ t.dup ~ u.dup ~ v.dup).endsWith(v));

	int i;
	s = string("The quick brown fox jumped over the lazy dog."w);
	i = s.find("Lazy"c, true);
	assert(-1 != i);
	assert(s[i .. i + 4] == "lazy"c);
	i = s.find("Lazy"c, false);
	assert(-1 == i);
	i = s.find("."c, true);
	assert(-1 != i);
	i = s.find("dog."c, true);
	assert(-1 != i);
	i = s.find(string("jumped"d), false);
	assert(-1 != i);
	
	wchar[] ws = "Arrrrr!";
	
	assert(!(s == "oof"c));
	assert(!(t == "oh nes!"w));
	assert(!(t == u));
	assert(!(t == s));
	assert(!(s == u));
	assert(string("foo"c) == s);
	assert(s == "foo"c);
	assert(ws == string(ws));
	assert(!(ws > string(ws)));
	assert(!(ws < string(ws)));
	assert(ws == string(ws)[0 .. ws.length]);
	assert(!(ws > string(ws)[0 .. ws.length]));
	assert(!(ws < string(ws)[0 .. ws.length]));
	assert(ws == string(ws).toUTF16());
	assert(ws == string(string(ws).toString()).toUTF16);
	assert(ws <= (string(ws) ~ ws));
	assert(ws >= string(ws)[0..ws.length / 2]);
	
	string s2;
	s2 = s.dup;
	assert(s.type != s2.type); // Compacted.
	assert(s == s2); // Same value.
	
	foreach ( uint i, inout dchar ch; u ) {
		t[i] = ch;
		s[s.length - i] = ch;
	}

	u ~= "\u0164" ~ "\u0020" ~ "\u9876"; // just to be funny and prevent from compacting ;)
	
	assert( t.dup().type( ) == t.type() );
	u.compact();
	v.compact();
	assert( s.type( ) != u.type( ) );
	
	assert( s.length < s.MAX_LENGTH );
	
	s = string();
	t.cleanup();
	u = string();
	v = string();

	assert(s.length == 0);
	assert(s.length == t.length);
	assert(t.length == 0);
	assert(u.length == 0);
	assert(v.length == 0);
}