ObjFW  Diff

Differences From Artifact [d6db94dbe6]:

To Artifact [ad07c5ffd2]:


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
117
118
119
120
121
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
117

118
119
120
121






122





























123







124


125








126


127





















128
129







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




-
+



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

-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-


	if (!caught) {					\
		puts("NOT CAUGHT!");			\
		return 1;				\
	}

const char *str = "Hallo!";

#define TEST(type) \
	puts("Trying to add too much to an array...");			\
	a = [type newWithItemSize: 4096];				\
	CATCH_EXCEPTION([a addNItems: SIZE_MAX				\
			  fromCArray: NULL],				\
	    OFOutOfRangeException)					\
									\
	puts("Trying to add something after that error...");		\
	p = [a getMemWithSize: 4096];					\
	memset(p, 255, 4096);						\
	[a add: p];							\
	if (!memcmp([a last], p, 4096))					\
		puts("[a last] matches with p!");			\
	else {								\
		puts("[a last] does not match p!");			\
		abort();						\
	}								\
	[a freeMem: p];							\
									\
	puts("Adding more data...");					\
	q = [a getMemWithSize: 4096];					\
	memset(q, 42, 4096);						\
	[a add: q];							\
	if (!memcmp([a last], q, 4096))					\
		puts("[a last] matches with q!");			\
	else {								\
		puts("[a last] does not match q!");			\
		abort();						\
	}								\
	[a freeMem: q];							\
									\
	puts("Adding multiple items at once...");			\
	p = [a getMemWithSize: 8192];					\
	memset(p, 64, 8192);						\
	[a addNItems: 2							\
	  fromCArray: p];						\
	if (!memcmp([a last], [a item: [a items] - 2], 4096) &&		\
	    !memcmp([a item: [a items] - 2], p, 4096))			\
		puts("[a last], [a item: [a items] - 2] and p match!");	\
	else {								\
		puts("[a last], [a item: [a items] - 2] and p did not match!");\
		abort();						\
	}								\
	[a freeMem: p];							\
									\
	i = [a items];							\
	puts("Removing 2 items...");					\
	[a removeNItems: 2];						\
	if ([a items] + 2 != i) {					\
		puts("[a items] + 2 != i!");				\
		abort();						\
	}								\
									\
	puts("Trying to remove more data than we added...");		\
	CATCH_EXCEPTION([a removeNItems: [a items] + 1],		\
	    OFOutOfRangeException);					\
									\
	puts("Trying to access an index that does not exist...");	\
	CATCH_EXCEPTION([a item: [a items]], OFOutOfRangeException);	\
									\
	[a free];							\
									\
	puts("Creating new array and using it to build a string...");	\
	a = [type newWithItemSize: 1];				\
									\
	for (i = 0; i < strlen(str); i++)				\
		[a add: (void*)(str + i)];				\
	[a add: ""];							\
									\
	if (!strcmp([a data], str))					\
		puts("Built string matches!");				\
	else {								\
		puts("Built string does not match!");			\
		abort();						\
	}								\
									\
	[a free];

int
main()
{
	BOOL caught;
	OFArray *a;
	id a;
	void *p, *q;
	size_t i;

	puts("Trying to add too much to an array...");
	a = [OFArray newWithItemSize: 4096];
	CATCH_EXCEPTION([a addNItems: SIZE_MAX
			  fromCArray: NULL],
	    OFOutOfRangeException)

	puts("== TESTING OFArray ==");
	puts("Trying to add something after that error...");
	p = [a getMemWithSize: 4096];
	memset(p, 255, 4096);
	[a add: p];
	if (!memcmp([a last], p, 4096))
		puts("[a last] matches with p!");
	else {
		puts("[a last] does not match p!");
		abort();
	}
	[a freeMem: p];

	puts("Adding more data...");
	q = [a getMemWithSize: 4096];
	memset(q, 42, 4096);
	[a add: q];
	if (!memcmp([a last], q, 4096))
		puts("[a last] matches with q!");
	else {
		puts("[a last] does not match q!");
		abort();
	}
	[a freeMem: q];

	puts("Adding multiple items at once...");
	p = [a getMemWithSize: 8192];
	memset(p, 64, 8192);
	[a addNItems: 2
	  fromCArray: p];
	TEST(OFArray)
	if (!memcmp([a last], [a item: [a items] - 2], 4096) &&
	    !memcmp([a item: [a items] - 2], p, 4096))
		puts("[a last], [a item: [a items] - 2] and p match!");
	else {
		puts("[a last], [a item: [a items] - 2] and p did not match!");
		abort();
	}

	[a freeMem: p];

	puts("== TESTING OFBigArray ==");
	i = [a items];
	puts("Removing 2 items...");
	[a removeNItems: 2];
	if ([a items] + 2 != i) {
		puts("[a items] + 2 != i!");
		abort();
	}

	TEST(OFBigArray)
	puts("Trying to remove more data than we added...");
	CATCH_EXCEPTION([a removeNItems: [a items] + 1], OFOutOfRangeException);

	puts("Trying to access an index that does not exist...");
	CATCH_EXCEPTION([a item: [a items]], OFOutOfRangeException);

	[a free];

	puts("Creating new array and using it to build a string...");
	a = [OFArray newWithItemSize: 1];

	for (i = 0; i < strlen(str); i++)
		[a add: (void*)(str + i)];
	[a add: ""];

	if (!strcmp([a data], str))
		puts("Built string matches!");
	else {
		puts("Built string does not match!");
		abort();
	}

	[a free];

	return 0;
}