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