ObjFW  Check-in [8b2f3fab96]

Overview
Comment:More exception testing stuff for OFObject.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: 8b2f3fab960cee16477f9b69e7563b42cad7e69c90f9372302a77df1ee1c15b9
User & Date: js on 2008-09-14 19:35:50
Other Links: manifest | tags
Context
2008-09-14
19:38
%zd -> %zu. check-in: 7cc77e2a1b user: js tags: trunk
19:35
More exception testing stuff for OFObject. check-in: 8b2f3fab96 user: js tags: trunk
19:03
More exceptions stuff. check-in: 98fe076bd8 user: js tags: trunk
Changes

Modified src/OFExceptions.m from [2ea46496b5] to [7125d2d9c0].

31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
	return [[OFNoMemException alloc] init: obj
				     withSize: size];
}

-     init: (id)obj
  withSize: (size_t)size
{
	fprintf(stderr, "ERROR: Could not allocate %zd byte for object %s!\n ",
	    size, [obj name]);

	return [super init];
}
@end

@implementation OFMemNotPartOfObjException







|







31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
	return [[OFNoMemException alloc] init: obj
				     withSize: size];
}

-     init: (id)obj
  withSize: (size_t)size
{
	fprintf(stderr, "ERROR: Could not allocate %zd byte for object %s!\n",
	    size, [obj name]);

	return [super init];
}
@end

@implementation OFMemNotPartOfObjException

Modified tests/OFObject/OFObject.m from [b4bf13ad01] to [201b5d89a5].

11
12
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
117
118
119

#import <stdio.h>
#import <stdlib.h>
#import <stdbool.h>

#import "OFObject.h"
#import "OFExceptions.h"














int
main()
{
	OFObject *obj = [OFObject new];
	bool caught;
	void *p, *q, *r;

	/* Test freeing memory not allocated by obj */
	puts("Freeing memory not allocated by object (should throw an "
	    "exception)...");
	caught = false;
	@try {
		[obj freeMem: (void*)123];
	} @catch (OFMemNotPartOfObjException *e) {
		caught = true;
		puts("CAUGHT! Resuming...");
	}
	if (!caught) {
		puts("NOT CAUGHT!");
		return 1;
	}

	/* Test allocating memory */
	puts("Allocating memory through object...");
	p = [obj getMem: 4096];
	puts("Allocated 4096 bytes.");

	/* Test freeing the just allocated memory */
	puts("Freeing just allocated memory...");
	[obj freeMem: p];
	puts("Free'd.");

	/* It shouldn't be recognized as part of our obj anymore */
	puts("Trying to free it again (should throw an exception)...");
	caught = false;
	@try {
		[obj freeMem: p];
	} @catch (OFMemNotPartOfObjException *e) {
		caught = true;
		puts("CAUGHT! Resuming...");
	}
	if (!caught) {
		puts("NOT CAUGHT!");
		return 1;
	}

	/* Test multiple memory chunks */
	puts("Allocating 3 chunks of memory...");
	p = [obj getMem: 4096];
	q = [obj getMem: 4096];
	r = [obj getMem: 4096];
	puts("Allocated 3 * 4096 bytes.");

	/* Free them */
	puts("Now freeing them...");
	[obj freeMem: p];
	[obj freeMem: q];
	[obj freeMem: r];
	puts("Freed them all.");

	/* Try to free again */
	puts("Now trying to free them again...");
	caught = false;
	@try {
		[obj freeMem: p];
	} @catch (OFMemNotPartOfObjException *e) {
		caught = true;
		puts("CAUGHT! Resuming...");
	}
	if (!caught) {
		puts("NOT CAUGHT!");
		return 1;
	}
	caught = false;
	@try {
		[obj freeMem: q];
	} @catch (OFMemNotPartOfObjException *e) {
		caught = true;
		puts("CAUGHT! Resuming...");
	}
	if (!caught) {
		puts("NOT CAUGHT!");
		return 1;
	}
	caught = false;
	@try {
		[obj freeMem: r];
	} @catch (OFMemNotPartOfObjException *e) {
		caught = true;
		puts("CAUGHT! Resuming...");
	}
	if (!caught) {
		puts("NOT CAUGHT!");
		return 1;
	}
	puts("Got all 3!");

	
	/* TODO: Test if freeing object frees all memory */

	return 0;
}







>
>
>
>
>
>
>
>
>
>
>
>
>











<
<
|
<
<
<
<
<
<
<
<













<
<
<
|
<
<
<
<
<
<
<

















<
<
<
|
<
<
<
<
<
<
<
<
<
<
|
<
<
<
<
<
<
<
<
<
<
|
<
<
<
<
|
<
|
|
>





11
12
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

#import <stdio.h>
#import <stdlib.h>
#import <stdbool.h>

#import "OFObject.h"
#import "OFExceptions.h"

#define CATCH_EXCEPTION(code, exception)	\
	caught = false;				\
	@try {					\
		code;				\
	} @catch (exception *e) {		\
		caught = true;			\
		puts("CAUGHT! Resuming...");	\
	}					\
	if (!caught) {				\
		puts("NOT CAUGHT!");		\
		return 1;			\
	}

int
main()
{
	OFObject *obj = [OFObject new];
	bool caught;
	void *p, *q, *r;

	/* Test freeing memory not allocated by obj */
	puts("Freeing memory not allocated by object (should throw an "
	    "exception)...");


	CATCH_EXCEPTION([obj freeMem: (void*)123], OFMemNotPartOfObjException)









	/* Test allocating memory */
	puts("Allocating memory through object...");
	p = [obj getMem: 4096];
	puts("Allocated 4096 bytes.");

	/* Test freeing the just allocated memory */
	puts("Freeing just allocated memory...");
	[obj freeMem: p];
	puts("Free'd.");

	/* It shouldn't be recognized as part of our obj anymore */
	puts("Trying to free it again (should throw an exception)...");



	CATCH_EXCEPTION([obj freeMem: p], OFMemNotPartOfObjException)








	/* Test multiple memory chunks */
	puts("Allocating 3 chunks of memory...");
	p = [obj getMem: 4096];
	q = [obj getMem: 4096];
	r = [obj getMem: 4096];
	puts("Allocated 3 * 4096 bytes.");

	/* Free them */
	puts("Now freeing them...");
	[obj freeMem: p];
	[obj freeMem: q];
	[obj freeMem: r];
	puts("Freed them all.");

	/* Try to free again */
	puts("Now trying to free them again...");



	CATCH_EXCEPTION([obj freeMem: p], OFMemNotPartOfObjException)










	CATCH_EXCEPTION([obj freeMem: q], OFMemNotPartOfObjException)










	CATCH_EXCEPTION([obj freeMem: r], OFMemNotPartOfObjException)




	puts("Got all 3!");

	
	puts("Trying to allocate more memory than possible...");
	CATCH_EXCEPTION(p = [obj getMem: 4294967295U], OFNoMemException)
	
	/* TODO: Test if freeing object frees all memory */

	return 0;
}