xref: /haiku/src/tests/kits/support/string_utf8_tests.cpp (revision cbe0a0c436162d78cc3f92a305b64918c839d079)
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <SupportDefs.h>
4 #include <String.h>
5 #include <InterfaceDefs.h>
6 
7 
8 inline void
9 expect(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 
30 int
31 main(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