xref: /haiku/src/tests/kits/support/bstring/StringConstructionTest.cpp (revision 02354704729d38c3b078c696adc1bbbd33cbcf72)
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 	// BString(BString&&)
61 #if __cplusplus >= 201103L
62 	NextSubTest();
63 	BString movableString(str);
64 	string = new BString(std::move(movableString));
65 	CPPUNIT_ASSERT(strcmp(string->String(), str) == 0);
66 	CPPUNIT_ASSERT(string->Length() == strlen(str));
67 	CPPUNIT_ASSERT(strcmp(movableString.String(), "") == 0);
68 	CPPUNIT_ASSERT(movableString.Length() == 0);
69 	delete string;
70 #endif
71 
72 	NextSubTest();
73 	string = new BString(str, 255);
74 	CPPUNIT_ASSERT(strcmp(string->String(), str) == 0);
75 	CPPUNIT_ASSERT((unsigned)string->Length() == strlen(str));
76 	delete string;
77 }
78 
79 
80 CppUnit::Test *StringConstructionTest::suite(void)
81 {
82 	typedef CppUnit::TestCaller<StringConstructionTest>
83 		StringConstructionTestCaller;
84 
85 	return(new StringConstructionTestCaller("BString::Construction Test",
86 		&StringConstructionTest::PerformTest));
87 }
88