xref: /haiku/src/tests/kits/support/bstring/StringConstructionTest.cpp (revision bd6068614473f87449dfa2eaa67fad1527c61e11)
1 #include "StringConstructionTest.h"
2 #include "cppunit/TestCaller.h"
3 #include <String.h>
4 
5 
6 StringConstructionTest::StringConstructionTest(std::string name)
7 		: BTestCase(name)
8 {
9 }
10 
11 
12 StringConstructionTest::~StringConstructionTest()
13 {
14 }
15 
16 
17 void
18 StringConstructionTest::PerformTest(void)
19 {
20 	BString *string;
21 	const char *str = "Something";
22 
23 	// BString()
24 	NextSubTest();
25 	string = new BString;
26 	CPPUNIT_ASSERT(strcmp(string->String(), "") == 0);
27 	CPPUNIT_ASSERT(string->Length() == 0);
28 	delete string;
29 
30 	// BString(const char*)
31 	NextSubTest();
32 	string = new BString(str);
33 	CPPUNIT_ASSERT(strcmp(string->String(), str) == 0);
34 	CPPUNIT_ASSERT((unsigned)string->Length() == strlen(str));
35 	delete string;
36 
37 	// BString(NULL)
38 	NextSubTest();
39 	string = new BString(NULL);
40 	CPPUNIT_ASSERT(strcmp(string->String(), "") == 0);
41 	CPPUNIT_ASSERT(string->Length() == 0);
42 	delete string;
43 
44 	// BString(BString&)
45 	NextSubTest();
46 	BString anotherString("Something Else");
47 	string = new BString(anotherString);
48 	CPPUNIT_ASSERT(strcmp(string->String(), anotherString.String()) == 0);
49 	CPPUNIT_ASSERT(string->Length() == anotherString.Length());
50 	delete string;
51 
52 	// BString(const char*, int32)
53 	NextSubTest();
54 	string = new BString(str, 5);
55 	CPPUNIT_ASSERT(strcmp(string->String(), str) != 0);
56 	CPPUNIT_ASSERT(strncmp(string->String(), str, 5) == 0);
57 	CPPUNIT_ASSERT(string->Length() == 5);
58 	delete string;
59 
60 	NextSubTest();
61 	string = new BString(str, 255);
62 	CPPUNIT_ASSERT(strcmp(string->String(), str) == 0);
63 	CPPUNIT_ASSERT((unsigned)string->Length() == strlen(str));
64 	delete string;
65 }
66 
67 
68 CppUnit::Test *StringConstructionTest::suite(void)
69 {
70 	typedef CppUnit::TestCaller<StringConstructionTest>
71 		StringConstructionTestCaller;
72 
73 	return(new StringConstructionTestCaller("BString::Construction Test",
74 		&StringConstructionTest::PerformTest));
75 }
76