44 TEST(ConstructorDestructor) {
47 ASSERT_NEQ_MSG(NULL, (
unsigned int) testable->m_string);
48 ASSERT_EQ_MSG(StringBuilder::DEFAULT_SPACE_ALLOCATED, testable->m_currentSpace);
49 ASSERT_EQ_MSG(0, testable->m_size);
50 ASSERT_EQ_MSG(0, strlen(testable->
to_string()));
56 const char testChar =
'a';
60 ASSERT_EQ_MSG(1, testable->
get_size());
62 ASSERT_EQ_MSG(testChar, testable->
to_string()[0]);
63 ASSERT_EQ_MSG(StringBuilder::DEFAULT_SPACE_ALLOCATED, testable->m_currentSpace);
74 ASSERT_EQ_MSG(2, testable->
get_size());
76 ASSERT_EQ_MSG(
'a', testable->
to_string()[0]);
77 ASSERT_EQ_MSG(
'b', testable->
to_string()[1]);
78 ASSERT_EQ_MSG(StringBuilder::DEFAULT_SPACE_ALLOCATED, testable->m_currentSpace);
90 ASSERT_EQ_MSG(3, testable->
get_size());
92 ASSERT_EQ_MSG(
'a', testable->
to_string()[0]);
93 ASSERT_EQ_MSG(
'b', testable->
to_string()[1]);
94 ASSERT_EQ_MSG(
'c', testable->
to_string()[2]);
95 ASSERT_EQ_MSG(StringBuilder::DEFAULT_SPACE_ALLOCATED, testable->m_currentSpace);
100 TEST(PutChar_ExactlyFull_SpaceShouldDouble) {
103 for (
int i = 0; i < StringBuilder::DEFAULT_SPACE_ALLOCATED - 1; ++i)
106 ASSERT_EQ_MSG(StringBuilder::DEFAULT_SPACE_ALLOCATED - 1, testable->
get_size());
108 ASSERT_EQ_MSG(StringBuilder::DEFAULT_SPACE_ALLOCATED << 1, testable->m_currentSpace);
109 for (
int i = 0; i < StringBuilder::DEFAULT_SPACE_ALLOCATED - 1; ++i)
110 ASSERT_EQ_MSG(
'a' + i, testable->
to_string()[i]);
115 TEST(PutChar_FirstNewAlloc) {
118 const unsigned int originalStringAddr = (
unsigned int) testable->
to_string();
120 for (
int i = 0; i < StringBuilder::DEFAULT_SPACE_ALLOCATED; ++i)
123 ASSERT_NEQ_MSG(originalStringAddr, (
unsigned int) testable->
to_string());
124 ASSERT_EQ_MSG(StringBuilder::DEFAULT_SPACE_ALLOCATED, testable->
get_size());
126 ASSERT_EQ_MSG(StringBuilder::DEFAULT_SPACE_ALLOCATED * 2, testable->m_currentSpace);
127 for (
int i = 0; i < StringBuilder::DEFAULT_SPACE_ALLOCATED; ++i)
128 ASSERT_EQ_MSG(
'a' + i, testable->
to_string()[i]);
133 TEST(PutChar_HugeString) {
136 const unsigned int originalStringAddr = (
unsigned int) testable->
to_string();
138 const int STRING_SIZE = 0x1000 - 1;
139 for (
int i = 0; i < STRING_SIZE; ++i)
142 ASSERT_NEQ_MSG(originalStringAddr, (
unsigned int) testable->
to_string());
143 ASSERT_EQ_MSG(STRING_SIZE, testable->
get_size());
145 ASSERT_EQ_MSG((STRING_SIZE + 1) << 1, testable->m_currentSpace);
146 for (
int i = 0; i < STRING_SIZE; ++i)
147 ASSERT_EQ_MSG(
'a', testable->
to_string()[i]);
157 ASSERT_NEQ_MSG(NULL, (
unsigned int) testable->m_string);
158 ASSERT_EQ_MSG(StringBuilder::DEFAULT_SPACE_ALLOCATED, testable->m_currentSpace);
159 ASSERT_EQ_MSG(0, testable->m_size);
160 ASSERT_EQ_MSG(0, strlen(testable->
to_string()));
165 TEST(Clear_OneChar) {
171 ASSERT_NEQ_MSG(NULL, (
unsigned int) testable->m_string);
172 ASSERT_EQ_MSG(StringBuilder::DEFAULT_SPACE_ALLOCATED, testable->m_currentSpace);
173 ASSERT_EQ_MSG(0, testable->m_size);
174 ASSERT_EQ_MSG(0, strlen(testable->
to_string()));
179 TEST(Clear_HugeString) {
182 const int STRING_SIZE = 0x1000 - 1;
183 for (
int i = 0; i < STRING_SIZE; ++i)
188 ASSERT_NEQ_MSG(NULL, (
unsigned int) testable->m_string);
189 ASSERT_EQ_MSG(StringBuilder::DEFAULT_SPACE_ALLOCATED, testable->m_currentSpace);
190 ASSERT_EQ_MSG(0, testable->m_size);
191 ASSERT_EQ_MSG(0, strlen(testable->
to_string()));
197 const char testString[] =
"Hello, world! My name is David Zemon. This my super long sentence.";
200 testable->
puts(testString);
202 ASSERT_NEQ_MSG(NULL, (
unsigned int) testable->
to_string());
203 ASSERT_EQ_MSG(
sizeof(testString) - 1, testable->
get_size());
204 ASSERT_EQ_MSG(0, strcmp(testString, testable->
to_string()));
210 START(StringBuilderTest);
212 RUN_TEST(ConstructorDestructor);
213 RUN_TEST(PutChar_one);
214 RUN_TEST(PutChar_two);
215 RUN_TEST(PutChar_three);
216 RUN_TEST(PutChar_ExactlyFull_SpaceShouldDouble);
217 RUN_TEST(PutChar_FirstNewAlloc);
218 RUN_TEST(PutChar_HugeString);
219 RUN_TEST(Clear_empty);
220 RUN_TEST(Clear_OneChar);
221 RUN_TEST(Clear_HugeString);
Generic definitions and functions for the Parallax Propeller.
void put_char(const char c)
Print a single character.
uint16_t get_size() const
Determine the length of the string, not including the null terminator.
void clear()
Remove all characters from the string and reallocate to the original size (if needed) ...
Build a dynamically-sized string in RAM using the PropWare::Printer interface.
void puts(const char string[])
Send a null-terminated character array. Though this method could be created using put_char...
const char * to_string() const
Retrieve the address of the string buffer.