+ fOutbuf += n;
+ fLength += n;
+ }
+ virtual void Flush() { Append("z", 1); }
+ int32_t length() { return fLength; }
+private:
+ char *fOutbuf;
+ int32_t fLength;
+};
+
+// Test the ByteSink base class.
+void
+StringTest::TestByteSink() {
+ char buffer[20];
+ buffer[4] = '!';
+ SimpleByteSink sink(buffer);
+ sink.Append("abc", 3);
+ sink.Flush();
+ if(!(sink.length() == 4 && 0 == memcmp("abcz", buffer, 4) && buffer[4] == '!')) {
+ errln("ByteSink (SimpleByteSink) did not Append() or Flush() as expected");
+ return;
+ }
+ char scratch[20];
+ int32_t capacity = -1;
+ char *dest = sink.GetAppendBuffer(0, 50, scratch, (int32_t)sizeof(scratch), &capacity);
+ if(dest != NULL || capacity != 0) {
+ errln("ByteSink.GetAppendBuffer(min_capacity<1) did not properly return NULL[0]");
+ return;
+ }
+ dest = sink.GetAppendBuffer(10, 50, scratch, 9, &capacity);
+ if(dest != NULL || capacity != 0) {
+ errln("ByteSink.GetAppendBuffer(scratch_capacity<min_capacity) did not properly return NULL[0]");
+ return;
+ }
+ dest = sink.GetAppendBuffer(5, 50, scratch, (int32_t)sizeof(scratch), &capacity);
+ if(dest != scratch || capacity != (int32_t)sizeof(scratch)) {
+ errln("ByteSink.GetAppendBuffer() did not properly return the scratch buffer");
+ }
+}
+
+void
+StringTest::TestCheckedArrayByteSink() {
+ char buffer[20]; // < 26 for the test code to work
+ buffer[3] = '!';
+ CheckedArrayByteSink sink(buffer, (int32_t)sizeof(buffer));
+ sink.Append("abc", 3);
+ if(!(sink.NumberOfBytesAppended() == 3 && sink.NumberOfBytesWritten() == 3 &&
+ 0 == memcmp("abc", buffer, 3) && buffer[3] == '!') &&
+ !sink.Overflowed()
+ ) {
+ errln("CheckedArrayByteSink did not Append() as expected");
+ return;
+ }
+ char scratch[10];
+ int32_t capacity = -1;
+ char *dest = sink.GetAppendBuffer(0, 50, scratch, (int32_t)sizeof(scratch), &capacity);
+ if(dest != NULL || capacity != 0) {
+ errln("CheckedArrayByteSink.GetAppendBuffer(min_capacity<1) did not properly return NULL[0]");
+ return;
+ }
+ dest = sink.GetAppendBuffer(10, 50, scratch, 9, &capacity);
+ if(dest != NULL || capacity != 0) {
+ errln("CheckedArrayByteSink.GetAppendBuffer(scratch_capacity<min_capacity) did not properly return NULL[0]");
+ return;
+ }
+ dest = sink.GetAppendBuffer(10, 50, scratch, (int32_t)sizeof(scratch), &capacity);
+ if(dest != buffer + 3 || capacity != (int32_t)sizeof(buffer) - 3) {
+ errln("CheckedArrayByteSink.GetAppendBuffer() did not properly return its own buffer");
+ return;
+ }
+ memcpy(dest, "defghijklm", 10);
+ sink.Append(dest, 10);
+ if(!(sink.NumberOfBytesAppended() == 13 && sink.NumberOfBytesWritten() == 13 &&
+ 0 == memcmp("abcdefghijklm", buffer, 13) &&
+ !sink.Overflowed())
+ ) {
+ errln("CheckedArrayByteSink did not Append(its own buffer) as expected");
+ return;
+ }
+ dest = sink.GetAppendBuffer(10, 50, scratch, (int32_t)sizeof(scratch), &capacity);
+ if(dest != scratch || capacity != (int32_t)sizeof(scratch)) {
+ errln("CheckedArrayByteSink.GetAppendBuffer() did not properly return the scratch buffer");
+ }
+ memcpy(dest, "nopqrstuvw", 10);
+ sink.Append(dest, 10);
+ if(!(sink.NumberOfBytesAppended() == 23 &&
+ sink.NumberOfBytesWritten() == (int32_t)sizeof(buffer) &&
+ 0 == memcmp("abcdefghijklmnopqrstuvwxyz", buffer, (int32_t)sizeof(buffer)) &&
+ sink.Overflowed())
+ ) {
+ errln("CheckedArrayByteSink did not Append(scratch buffer) as expected");
+ return;
+ }
+ sink.Reset().Append("123", 3);
+ if(!(sink.NumberOfBytesAppended() == 3 && sink.NumberOfBytesWritten() == 3 &&
+ 0 == memcmp("123defghijklmnopqrstuvwxyz", buffer, (int32_t)sizeof(buffer)) &&
+ !sink.Overflowed())
+ ) {
+ errln("CheckedArrayByteSink did not Reset().Append() as expected");
+ return;
+ }
+}
+
+void
+StringTest::TestStringByteSink() {
+#if U_HAVE_STD_STRING
+ // Not much to test because only the constructor and Append()
+ // are implemented, and trivially so.
+ U_STD_NSQ string result("abc"); // std::string
+ StringByteSink<U_STD_NSQ string> sink(&result);
+ sink.Append("def", 3);
+ if(result != "abcdef") {
+ errln("StringByteSink did not Append() as expected");
+ }
+#endif
+}
+
+#if defined(U_WINDOWS) && defined(_MSC_VER)
+#include <vector>
+#endif
+
+void
+StringTest::TestSTLCompatibility() {
+#if defined(U_WINDOWS) && defined(_MSC_VER)
+ /* Just make sure that it compiles with STL's placement new usage. */
+ std::vector<UnicodeString> myvect;
+ myvect.push_back(UnicodeString("blah"));
+#endif
+}
+
+void
+StringTest::TestCharString() {
+ IcuTestErrorCode errorCode(*this, "TestCharString()");
+ char expected[400];
+ static const char longStr[] =
+ "This is a long string that is meant to cause reallocation of the internal buffer of CharString.";
+ CharString chStr(longStr, errorCode);
+ if (0 != strcmp(longStr, chStr.data()) || (int32_t)strlen(longStr) != chStr.length()) {
+ errln("CharString(longStr) failed.");
+ }
+ CharString test("Test", errorCode);
+ CharString copy(test,errorCode);
+ copy.copyFrom(chStr, errorCode);
+ if (0 != strcmp(longStr, copy.data()) || (int32_t)strlen(longStr) != copy.length()) {
+ errln("CharString.copyFrom() failed.");
+ }
+ StringPiece sp(chStr.toStringPiece());
+ sp.remove_prefix(4);
+ chStr.append(sp, errorCode).append(chStr, errorCode);
+ strcpy(expected, longStr);
+ strcat(expected, longStr+4);
+ strcat(expected, longStr);
+ strcat(expected, longStr+4);
+ if (0 != strcmp(expected, chStr.data()) || (int32_t)strlen(expected) != chStr.length()) {
+ errln("CharString(longStr).append(substring of self).append(self) failed.");
+ }
+ chStr.clear().append("abc", errorCode).append("defghij", 3, errorCode);
+ if (0 != strcmp("abcdef", chStr.data()) || 6 != chStr.length()) {
+ errln("CharString.clear().append(abc).append(defghij, 3) failed.");
+ }
+ chStr.appendInvariantChars(UNICODE_STRING_SIMPLE(
+ "This is a long string that is meant to cause reallocation of the internal buffer of CharString."),
+ errorCode);
+ strcpy(expected, "abcdef");
+ strcat(expected, longStr);
+ if (0 != strcmp(expected, chStr.data()) || (int32_t)strlen(expected) != chStr.length()) {
+ errln("CharString.appendInvariantChars(longStr) failed.");
+ }
+ int32_t appendCapacity = 0;
+ char *buffer = chStr.getAppendBuffer(5, 10, appendCapacity, errorCode);
+ if (errorCode.isFailure()) {
+ return;
+ }
+ memcpy(buffer, "*****", 5);
+ chStr.append(buffer, 5, errorCode);
+ chStr.truncate(chStr.length()-3);
+ strcat(expected, "**");
+ if (0 != strcmp(expected, chStr.data()) || (int32_t)strlen(expected) != chStr.length()) {
+ errln("CharString.getAppendBuffer().append(**) failed.");