ObjFW  Diff

Differences From Artifact [09c95b9e26]:

To Artifact [cab4bb0f80]:


13
14
15
16
17
18
19

























20
21
22


23
24
25
26
27
28
29
30

31
32
33
34
35

36
37
38
39
40

41
42
43
44
45
46
47

48
49
50
51
52
53
54

55
56
57
58
59
60
61

62
63
64
65
66
67
68

69
70
71
72
73
74
75

76
77
78
79
80
81
82
83
84
85
86
87
88
89
90

91
92
93
94

95
96
97
98
99

100
101
102
103

104
105

106
107
108

109
110
111
112
113


114
115
116
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56

57





58

59
60
61

62







63







64







65







66







67





68
69
70
71
72
73
74
75


76




77





78




79


80

81

82





83
84
85
86
87







+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+



+
+







-
+
-
-
-
-
-
+
-



-
+
-
-
-
-
-
-
-
+
-
-
-
-
-
-
-
+
-
-
-
-
-
-
-
+
-
-
-
-
-
-
-
+
-
-
-
-
-
-
-
+
-
-
-
-
-








-
-
+
-
-
-
-
+
-
-
-
-
-
+
-
-
-
-
+
-
-
+
-

-
+
-
-
-
-
-
+
+




#import <stdio.h>
#import <string.h>

#import "OFString.h"
#import "OFExceptions.h"

#define NUM_TESTS 10
#define SUCCESS								\
	printf("\r\033[1;%dmTests successful: %zd/%d\033[0m",		\
	    (i == NUM_TESTS - 1 ? 32 : 33), i + 1, NUM_TESTS);		\
	fflush(stdout);
#define FAIL								\
	printf("\r\033[K\033[1;31mTest %zd/%d failed!\033[m\n",		\
	    i + 1, NUM_TESTS);						\
	return 1;
#define CHECK(cond)							\
	if (cond) {							\
		SUCCESS							\
	} else {							\
		FAIL							\
	}								\
	i++;
#define CHECK_EXCEPT(code, exception)					\
	@try {								\
		code;							\
		FAIL							\
	} @catch (exception *e) {					\
		SUCCESS							\
	}								\
	i++;

int
main()
{
	size_t i = 0;

	OFString *s1 = [OFString newFromCString: "test"];
	OFString *s2 = [OFString newFromCString: ""];
	OFString *s3;
	OFString *s4 = [OFString new];

	s3 = [s1 clone];

	if (![s1 compareTo: s3])
	CHECK(![s1 compareTo: s3])
		puts("s1 and s3 match! GOOD!");
	else {
		puts("s1 and s3 don't match!");
		return 1;
	}


	[s2 appendCString: "123"];
	[s4 setTo: s2];

	if (![s2 compareTo: s4])
	CHECK(![s2 compareTo: s4])
		puts("s2 and s4 match! GOOD!");
	else {
		puts("s2 and s4 don't match!");
		return 1;
	}

	if (!strcmp([[s1 append: s2] cString], "test123"))
	CHECK(!strcmp([[s1 append: s2] cString], "test123"))
		puts("s1 appended with s2 is the expected string! GOOD!");
	else {
		puts("s1 appended with s2 is not the expected string!");
		return 1;
	}

	if (strlen([s1 cString]) == [s1 length] && [s1 length] == 7)
	CHECK(strlen([s1 cString]) == [s1 length] && [s1 length] == 7)
		puts("s1 has the expected length. GOOD!");
	else {
		puts("s1 does not have the expected length!");
		return 1;
	}

	if (!strcmp([[s1 reverse] cString], "321tset"))
	CHECK(!strcmp([[s1 reverse] cString], "321tset"))
		puts("Reversed s1 is expected string! GOOD!");
	else {
		puts("Reversed s1 is NOT the expected string!");
		return 1;
	}

	if (!strcmp([[s1 upper] cString], "321TSET"))
	CHECK(!strcmp([[s1 upper] cString], "321TSET"))
		puts("Upper s1 is expected string! GOOD!");
	else {
		puts("Upper s1 is NOT expected string!");
		return 1;
	}

	if (!strcmp([[s1 lower] cString], "321tset"))
	CHECK(!strcmp([[s1 lower] cString], "321tset"))
		puts("Lower s1 is expected string! GOOD!");
	else {
		puts("Lower s1 is NOT expected string!");
		return 1;
	}

	/* Also clears all the memory of the returned C strings */
	[s1 free];
	[s2 free];
	[s3 free];
	[s4 free];

	/* UTF-8 tests */
	@try {
		s1 = [OFString newFromCString: "\xE0\x80"];
	CHECK_EXCEPT(s1 = [OFString newFromCString: "\xE0\x80"],

		puts("First invalid UTF-8 not detected!");
		return 1;
	} @catch (OFInvalidEncodingException *e) {
	    OFInvalidEncodingException)
		puts("First invalid UTF-8 successfully detected!");
	}

	@try {
		s1 = [OFString newFromCString: "\xF0\x80\x80\xC0"];
	CHECK_EXCEPT(s1 = [OFString newFromCString: "\xF0\x80\x80\xC0"],

		puts("Second UTF-8 not detected!");
		return 1;
	} @catch (OFInvalidEncodingException *e) {
	    OFInvalidEncodingException)
		puts("Second UTF-8 successfully detected!");
	}


	s1 = [OFString newFromCString: "äöü€𝄞"];
	if (!strcmp([[s1 reverse] cString], "𝄞€üöä"))
	CHECK(!strcmp([[s1 reverse] cString], "𝄞€üöä"))
		puts("Reversed UTF-8 string is expected string! GOOD!");
	else {
		puts("Reversed UTF-8 string is NOT expected string!");
		return 1;
	}

	puts("");

	return 0;
}