xref: /haiku/src/tests/kits/support/bstring/StringRemoveTest.cpp (revision f960e7e9a2b236d50e202d5c46b4d759d46b233a)
16903f4c6SStefano Ceccherini #include "StringRemoveTest.h"
26903f4c6SStefano Ceccherini #include "cppunit/TestCaller.h"
36903f4c6SStefano Ceccherini #include <String.h>
46903f4c6SStefano Ceccherini #include <stdio.h>
56903f4c6SStefano Ceccherini 
6*f960e7e9Skrish_iyer 
StringRemoveTest(std::string name)7*f960e7e9Skrish_iyer StringRemoveTest::StringRemoveTest(std::string name)
8*f960e7e9Skrish_iyer 		: BTestCase(name)
96903f4c6SStefano Ceccherini {
106903f4c6SStefano Ceccherini }
116903f4c6SStefano Ceccherini 
126903f4c6SStefano Ceccherini 
~StringRemoveTest()136903f4c6SStefano Ceccherini StringRemoveTest::~StringRemoveTest()
146903f4c6SStefano Ceccherini {
156903f4c6SStefano Ceccherini }
166903f4c6SStefano Ceccherini 
176903f4c6SStefano Ceccherini 
186903f4c6SStefano Ceccherini void
PerformTest(void)196903f4c6SStefano Ceccherini StringRemoveTest::PerformTest(void)
206903f4c6SStefano Ceccherini {
216903f4c6SStefano Ceccherini 	BString *string1, *string2;
226903f4c6SStefano Ceccherini 
236903f4c6SStefano Ceccherini 	// Truncate(int32 newLength, bool lazy);
246903f4c6SStefano Ceccherini 	// lazy = true
256903f4c6SStefano Ceccherini 	NextSubTest();
266903f4c6SStefano Ceccherini 	string1 = new BString("This is a long string");
276903f4c6SStefano Ceccherini 	string1->Truncate(14, true);
286903f4c6SStefano Ceccherini 	CPPUNIT_ASSERT(strcmp(string1->String(), "This is a long") == 0);
296903f4c6SStefano Ceccherini 	CPPUNIT_ASSERT(string1->Length() == 14);
306903f4c6SStefano Ceccherini 	delete string1;
316903f4c6SStefano Ceccherini 
326903f4c6SStefano Ceccherini 	// lazy = false
336903f4c6SStefano Ceccherini 	NextSubTest();
346903f4c6SStefano Ceccherini 	string1 = new BString("This is a long string");
356903f4c6SStefano Ceccherini 	string1->Truncate(14, false);
366903f4c6SStefano Ceccherini 	CPPUNIT_ASSERT(strcmp(string1->String(), "This is a long") == 0);
376903f4c6SStefano Ceccherini 	CPPUNIT_ASSERT(string1->Length() == 14);
386903f4c6SStefano Ceccherini 	delete string1;
396903f4c6SStefano Ceccherini 
409f83f86cSStefano Ceccherini #ifndef TEST_R5
416903f4c6SStefano Ceccherini 	// new length is < 0
429f83f86cSStefano Ceccherini 	// it crashes r5 implementation, but ours works fine here,
435235e068SStefano Ceccherini 	// in this case, we just truncate to 0
449f83f86cSStefano Ceccherini 	NextSubTest();
459f83f86cSStefano Ceccherini 	string1 = new BString("This is a long string");
469f83f86cSStefano Ceccherini 	string1->Truncate(-3);
475235e068SStefano Ceccherini 	CPPUNIT_ASSERT(strcmp(string1->String(), "") == 0);
485235e068SStefano Ceccherini 	CPPUNIT_ASSERT(string1->Length() == 0);
499f83f86cSStefano Ceccherini 	delete string1;
509f83f86cSStefano Ceccherini #endif
516903f4c6SStefano Ceccherini 
526903f4c6SStefano Ceccherini 	// new length is > old length
536903f4c6SStefano Ceccherini 	NextSubTest();
546903f4c6SStefano Ceccherini 	string1 = new BString("This is a long string");
556903f4c6SStefano Ceccherini 	string1->Truncate(45);
566903f4c6SStefano Ceccherini 	CPPUNIT_ASSERT(strcmp(string1->String(), "This is a long string") == 0);
576903f4c6SStefano Ceccherini 	CPPUNIT_ASSERT(string1->Length() == 21);
586903f4c6SStefano Ceccherini 	delete string1;
596903f4c6SStefano Ceccherini 
606903f4c6SStefano Ceccherini 	// String was empty
616903f4c6SStefano Ceccherini 	NextSubTest();
626903f4c6SStefano Ceccherini 	string1 = new BString;
636903f4c6SStefano Ceccherini 	string1->Truncate(0);
646903f4c6SStefano Ceccherini 	CPPUNIT_ASSERT(strcmp(string1->String(), "") == 0);
656903f4c6SStefano Ceccherini 	CPPUNIT_ASSERT(string1->Length() == 0);
666903f4c6SStefano Ceccherini 	delete string1;
676903f4c6SStefano Ceccherini 
686903f4c6SStefano Ceccherini 	// Remove(int32 from, int32 length)
696903f4c6SStefano Ceccherini 	NextSubTest();
706903f4c6SStefano Ceccherini 	string1 = new BString("a String");
716903f4c6SStefano Ceccherini 	string1->Remove(2, 2);
726903f4c6SStefano Ceccherini 	CPPUNIT_ASSERT(strcmp(string1->String(), "a ring") == 0);
736903f4c6SStefano Ceccherini 	delete string1;
746903f4c6SStefano Ceccherini 
756903f4c6SStefano Ceccherini 	// String was empty
766903f4c6SStefano Ceccherini 	NextSubTest();
776903f4c6SStefano Ceccherini 	string1 = new BString;
786903f4c6SStefano Ceccherini 	string1->Remove(2, 1);
796903f4c6SStefano Ceccherini 	CPPUNIT_ASSERT(strcmp(string1->String(), "") == 0);
806903f4c6SStefano Ceccherini 	delete string1;
816903f4c6SStefano Ceccherini 
826903f4c6SStefano Ceccherini 	// from is beyond the end of the string
836903f4c6SStefano Ceccherini 	NextSubTest();
846903f4c6SStefano Ceccherini 	string1 = new BString("a String");
856903f4c6SStefano Ceccherini 	string1->Remove(20, 2);
866903f4c6SStefano Ceccherini 	CPPUNIT_ASSERT(strcmp(string1->String(), "a String") == 0);
876903f4c6SStefano Ceccherini 	delete string1;
886903f4c6SStefano Ceccherini 
895480b459SStefano Ceccherini 	// from + length exceeds Length() (R5 fails)
906903f4c6SStefano Ceccherini 	NextSubTest();
916903f4c6SStefano Ceccherini 	string1 = new BString("a String");
926903f4c6SStefano Ceccherini 	string1->Remove(4, 30);
935480b459SStefano Ceccherini 	CPPUNIT_ASSERT(strcmp(string1->String(), "a St") == 0);
946903f4c6SStefano Ceccherini 	delete string1;
956903f4c6SStefano Ceccherini 
966903f4c6SStefano Ceccherini 	NextSubTest();
976903f4c6SStefano Ceccherini 	string1 = new BString("a String");
986903f4c6SStefano Ceccherini 	string1->Remove(-3, 5);
996903f4c6SStefano Ceccherini 	CPPUNIT_ASSERT(strcmp(string1->String(), "ing") == 0);
1006903f4c6SStefano Ceccherini 	delete string1;
10196acb967SStefano Ceccherini 
10296acb967SStefano Ceccherini 	// RemoveFirst(BString&)
10396acb967SStefano Ceccherini 	NextSubTest();
10496acb967SStefano Ceccherini 	string1 = new BString("first second first");
10596acb967SStefano Ceccherini 	string2 = new BString("first");
10696acb967SStefano Ceccherini 	string1->RemoveFirst(*string2);
10796acb967SStefano Ceccherini 	CPPUNIT_ASSERT(strcmp(string1->String(), " second first") == 0);
10896acb967SStefano Ceccherini 	delete string1;
10996acb967SStefano Ceccherini 	delete string2;
11096acb967SStefano Ceccherini 
11196acb967SStefano Ceccherini 	NextSubTest();
11296acb967SStefano Ceccherini 	string1 = new BString("first second first");
11396acb967SStefano Ceccherini 	string2 = new BString("noway");
11496acb967SStefano Ceccherini 	string1->RemoveFirst(*string2);
11596acb967SStefano Ceccherini 	CPPUNIT_ASSERT(strcmp(string1->String(), "first second first") == 0);
11696acb967SStefano Ceccherini 	delete string1;
11796acb967SStefano Ceccherini 	delete string2;
11896acb967SStefano Ceccherini 
11996acb967SStefano Ceccherini 	// RemoveLast(Bstring&)
12096acb967SStefano Ceccherini 	NextSubTest();
12196acb967SStefano Ceccherini 	string1 = new BString("first second first");
12296acb967SStefano Ceccherini 	string2 = new BString("first");
12396acb967SStefano Ceccherini 	string1->RemoveLast(*string2);
12496acb967SStefano Ceccherini 	CPPUNIT_ASSERT(strcmp(string1->String(), "first second ") == 0);
12596acb967SStefano Ceccherini 	delete string1;
12696acb967SStefano Ceccherini 	delete string2;
12796acb967SStefano Ceccherini 
12896acb967SStefano Ceccherini 	NextSubTest();
12996acb967SStefano Ceccherini 	string1 = new BString("first second first");
13096acb967SStefano Ceccherini 	string2 = new BString("noway");
13196acb967SStefano Ceccherini 	string1->RemoveLast(*string2);
13296acb967SStefano Ceccherini 	CPPUNIT_ASSERT(strcmp(string1->String(), "first second first") == 0);
13396acb967SStefano Ceccherini 	delete string1;
13496acb967SStefano Ceccherini 	delete string2;
13596acb967SStefano Ceccherini 
13696acb967SStefano Ceccherini 	// RemoveAll(BString&)
13796acb967SStefano Ceccherini 	NextSubTest();
13896acb967SStefano Ceccherini 	string1 = new BString("first second first");
13996acb967SStefano Ceccherini 	string2 = new BString("first");
14096acb967SStefano Ceccherini 	string1->RemoveAll(*string2);
14196acb967SStefano Ceccherini 	CPPUNIT_ASSERT(strcmp(string1->String(), " second ") == 0);
14296acb967SStefano Ceccherini 	delete string1;
14396acb967SStefano Ceccherini 	delete string2;
14496acb967SStefano Ceccherini 
14596acb967SStefano Ceccherini 	NextSubTest();
14696acb967SStefano Ceccherini 	string1 = new BString("first second first");
14796acb967SStefano Ceccherini 	string2 = new BString("noway");
14896acb967SStefano Ceccherini 	string1->RemoveAll(*string2);
14996acb967SStefano Ceccherini 	CPPUNIT_ASSERT(strcmp(string1->String(), "first second first") == 0);
15096acb967SStefano Ceccherini 	delete string1;
15196acb967SStefano Ceccherini 	delete string2;
15296acb967SStefano Ceccherini 
15396acb967SStefano Ceccherini 	// RemoveFirst(const char*)
15496acb967SStefano Ceccherini 	NextSubTest();
15596acb967SStefano Ceccherini 	string1 = new BString("first second first");
15696acb967SStefano Ceccherini 	string1->RemoveFirst("first");
15796acb967SStefano Ceccherini 	CPPUNIT_ASSERT(strcmp(string1->String(), " second first") == 0);
15896acb967SStefano Ceccherini 	delete string1;
15996acb967SStefano Ceccherini 
16096acb967SStefano Ceccherini 	NextSubTest();
16196acb967SStefano Ceccherini 	string1 = new BString("first second first");
16296acb967SStefano Ceccherini 	string1->RemoveFirst("noway");
16396acb967SStefano Ceccherini 	CPPUNIT_ASSERT(strcmp(string1->String(), "first second first") == 0);
16496acb967SStefano Ceccherini 	delete string1;
16596acb967SStefano Ceccherini 
16696acb967SStefano Ceccherini 	NextSubTest();
16796acb967SStefano Ceccherini 	string1 = new BString("first second first");
16896acb967SStefano Ceccherini 	string1->RemoveFirst((char*)NULL);
16996acb967SStefano Ceccherini 	CPPUNIT_ASSERT(strcmp(string1->String(), "first second first") == 0);
17096acb967SStefano Ceccherini 	delete string1;
17196acb967SStefano Ceccherini 
17296acb967SStefano Ceccherini 	// RemoveLast(const char*)
17396acb967SStefano Ceccherini 	NextSubTest();
17496acb967SStefano Ceccherini 	string1 = new BString("first second first");
17596acb967SStefano Ceccherini 	string1->RemoveLast("first");
17696acb967SStefano Ceccherini 	CPPUNIT_ASSERT(strcmp(string1->String(), "first second ") == 0);
17796acb967SStefano Ceccherini 	delete string1;
17896acb967SStefano Ceccherini 
17996acb967SStefano Ceccherini 	NextSubTest();
18096acb967SStefano Ceccherini 	string1 = new BString("first second first");
18196acb967SStefano Ceccherini 	string1->RemoveLast("noway");
18296acb967SStefano Ceccherini 	CPPUNIT_ASSERT(strcmp(string1->String(), "first second first") == 0);
18396acb967SStefano Ceccherini 	delete string1;
18496acb967SStefano Ceccherini 
18596acb967SStefano Ceccherini 	// RemoveAll(const char*)
18696acb967SStefano Ceccherini 	NextSubTest();
18796acb967SStefano Ceccherini 	string1 = new BString("first second first");
18896acb967SStefano Ceccherini 	string1->RemoveAll("first");
18996acb967SStefano Ceccherini 	CPPUNIT_ASSERT(strcmp(string1->String(), " second ") == 0);
19096acb967SStefano Ceccherini 	delete string1;
19196acb967SStefano Ceccherini 
19296acb967SStefano Ceccherini 	NextSubTest();
19396acb967SStefano Ceccherini 	string1 = new BString("first second first");
19496acb967SStefano Ceccherini 	string1->RemoveAll("noway");
19596acb967SStefano Ceccherini 	CPPUNIT_ASSERT(strcmp(string1->String(), "first second first") == 0);
19696acb967SStefano Ceccherini 	delete string1;
19796acb967SStefano Ceccherini 
19896acb967SStefano Ceccherini 	// RemoveSet(const char*)
19996acb967SStefano Ceccherini 	NextSubTest();
20096acb967SStefano Ceccherini 	string1 = new BString("a sentence with (3) (642) numbers (2) in it");
20196acb967SStefano Ceccherini 	string1->RemoveSet("()3624 ");
20296acb967SStefano Ceccherini 	CPPUNIT_ASSERT(strcmp(string1->String(), "asentencewithnumbersinit") == 0);
20396acb967SStefano Ceccherini 	delete string1;
20496acb967SStefano Ceccherini 
20596acb967SStefano Ceccherini 	NextSubTest();
20696acb967SStefano Ceccherini 	string1 = new BString("a string");
20796acb967SStefano Ceccherini 	string1->RemoveSet("1345");
20896acb967SStefano Ceccherini 	CPPUNIT_ASSERT(strcmp(string1->String(), "a string") == 0);
20996acb967SStefano Ceccherini 	delete string1;
21096acb967SStefano Ceccherini 
21196acb967SStefano Ceccherini 	// MoveInto(BString &into, int32, int32)
21296acb967SStefano Ceccherini 	NextSubTest();
21396acb967SStefano Ceccherini 	string1 = new BString("some text");
21496acb967SStefano Ceccherini 	string2 = new BString("string");
21596acb967SStefano Ceccherini 	string2->MoveInto(*string1, 3, 2);
21696acb967SStefano Ceccherini 	CPPUNIT_ASSERT(strcmp(string1->String(), "in") == 0);
21796acb967SStefano Ceccherini 	CPPUNIT_ASSERT(strcmp(string2->String(), "strg") == 0);
21896acb967SStefano Ceccherini 	delete string1;
21996acb967SStefano Ceccherini 	delete string2;
22096acb967SStefano Ceccherini 
22196acb967SStefano Ceccherini 	NextSubTest();
22296acb967SStefano Ceccherini 	string1 = new BString("some text");
22396acb967SStefano Ceccherini 	string2 = new BString("string");
22496acb967SStefano Ceccherini 	string2->MoveInto(*string1, 0, 200);
22596acb967SStefano Ceccherini 	CPPUNIT_ASSERT(strcmp(string1->String(), "string") == 0);
2265480b459SStefano Ceccherini 	CPPUNIT_ASSERT(strcmp(string2->String(), "") == 0);
22796acb967SStefano Ceccherini 	delete string1;
22896acb967SStefano Ceccherini 	delete string2;
22996acb967SStefano Ceccherini 
23096acb967SStefano Ceccherini 	// MoveInto(char *, int32, int32)
23196acb967SStefano Ceccherini 	NextSubTest();
23296acb967SStefano Ceccherini 	char dest[100];
23396acb967SStefano Ceccherini 	memset(dest, 0, 100);
23496acb967SStefano Ceccherini 	string1 = new BString("some text");
23596acb967SStefano Ceccherini 	string1->MoveInto(dest, 3, 2);
23696acb967SStefano Ceccherini 	CPPUNIT_ASSERT(strcmp(dest, "e ") == 0);
23796acb967SStefano Ceccherini 	CPPUNIT_ASSERT(strcmp(string1->String(), "somtext") == 0);
23896acb967SStefano Ceccherini 	delete string1;
23996acb967SStefano Ceccherini 
24096acb967SStefano Ceccherini 	NextSubTest();
24196acb967SStefano Ceccherini 	string1 = new BString("some text");
24296acb967SStefano Ceccherini 	memset(dest, 0, 100);
24396acb967SStefano Ceccherini 	string1->MoveInto(dest, 0, 50);
24496acb967SStefano Ceccherini 	CPPUNIT_ASSERT(strcmp(dest, "some text") == 0);
2455480b459SStefano Ceccherini 	CPPUNIT_ASSERT(strcmp(string1->String(), "") == 0);
24696acb967SStefano Ceccherini 	delete string1;
2476903f4c6SStefano Ceccherini }
2486903f4c6SStefano Ceccherini 
2496903f4c6SStefano Ceccherini 
suite(void)2506903f4c6SStefano Ceccherini CppUnit::Test *StringRemoveTest::suite(void)
2516903f4c6SStefano Ceccherini {
2526903f4c6SStefano Ceccherini 	typedef CppUnit::TestCaller<StringRemoveTest>
2536903f4c6SStefano Ceccherini 		StringRemoveTestCaller;
2546903f4c6SStefano Ceccherini 
255*f960e7e9Skrish_iyer 	return(new StringRemoveTestCaller("BString::Remove Test",
256*f960e7e9Skrish_iyer 		&StringRemoveTest::PerformTest));
2576903f4c6SStefano Ceccherini }
258