1#include <stdio.h> 2#include <stdlib.h> 3#include <SupportDefs.h> 4#include <String.h> 5#include <InterfaceDefs.h> 6 7 8inline void 9expect(BString &string, const char *expect, size_t bytes, int32 chars) 10{ 11 printf("expect: \"%s\" %lu %ld\n", expect, bytes, chars); 12 printf("got: \"%s\" %lu %ld\n", string.String(), string.Length(), string.CountChars()); 13 if (bytes != (size_t)string.Length()) { 14 printf("expected byte length mismatch\n"); 15 exit(1); 16 } 17 18 if (chars != string.CountChars()) { 19 printf("expected char count mismatch\n"); 20 exit(2); 21 } 22 23 if (memcmp(string.String(), expect, bytes) != 0) { 24 printf("expected string mismatch\n"); 25 exit(3); 26 } 27} 28 29 30int 31main(int argc, char *argv[]) 32{ 33 printf("setting string to ��-��-��\n"); 34 BString string("��-��-��"); 35 expect(string, "��-��-��", 8, 5); 36 37 printf("replacing �� and �� by ellipsis\n"); 38 string.ReplaceCharsSet("����", B_UTF8_ELLIPSIS); 39 expect(string, B_UTF8_ELLIPSIS "-��-" B_UTF8_ELLIPSIS, 10, 5); 40 41 printf("moving the last char (ellipsis) to a seperate string\n"); 42 BString ellipsis; 43 string.MoveCharsInto(ellipsis, 4, 1); 44 expect(string, B_UTF8_ELLIPSIS "-��-", 7, 4); 45 expect(ellipsis, B_UTF8_ELLIPSIS, 3, 1); 46 47 printf("removing all - and ellipsis chars\n"); 48 string.RemoveCharsSet("-" B_UTF8_ELLIPSIS); 49 expect(string, "��", 2, 1); 50 51 printf("reset the string to ������" B_UTF8_ELLIPSIS "������\n"); 52 string.SetToChars("������" B_UTF8_ELLIPSIS "������", 5); 53 expect(string, "������" B_UTF8_ELLIPSIS "��", 11, 5); 54 55 printf("truncating string to 4 characters\n"); 56 string.TruncateChars(4); 57 expect(string, "������" B_UTF8_ELLIPSIS, 9, 4); 58 59 printf("appending 2 chars out of \"������\"\n"); 60 string.AppendChars("������", 2); 61 expect(string, "������" B_UTF8_ELLIPSIS "����", 13, 6); 62 63 printf("removing chars 1 through 4\n"); 64 string.RemoveChars(1, 3); 65 expect(string, "������", 6, 3); 66 67 printf("inserting 2 ellipsis out of 6 chars at offset 1\n"); 68 string.InsertChars("������" B_UTF8_ELLIPSIS B_UTF8_ELLIPSIS "��", 3, 2, 1); 69 expect(string, "��" B_UTF8_ELLIPSIS B_UTF8_ELLIPSIS "����", 12, 5); 70 71 printf("prepending 3 out of 5 chars\n"); 72 string.PrependChars("����+����", 3); 73 expect(string, "����+��" B_UTF8_ELLIPSIS B_UTF8_ELLIPSIS "����", 17, 8); 74 75 printf("comparing first 5 chars which should succeed\n"); 76 const char *compare = "����+��" B_UTF8_ELLIPSIS "different"; 77 if (string.CompareChars(compare, 5) != 0) { 78 printf("comparison failed\n"); 79 return 1; 80 } 81 82 printf("comparing first 6 chars which should fail\n"); 83 if (string.CompareChars(compare, 6) == 0) { 84 printf("comparison succeeded\n"); 85 return 2; 86 } 87 88 printf("counting bytes of 3 chars from offset 2 expect 6\n"); 89 if (string.CountBytes(2, 3) != 6) { 90 printf("got wrong byte count\n"); 91 return 3; 92 } 93 94 printf("all tests succeeded\n"); 95 return 0; 96} 97