ObjFW  Diff

Differences From Artifact [68866f4346]:

To Artifact [e3a197e96b]:

  • File tests/OFObject/OFObject.m — part of check-in [033054ad75] at 2009-05-29 19:21:57 on branch trunk — A few renames.

    OFExceptions:
    * OFNoMemException to OFOutOfMemoryException.
    * OFMemNotPartOfObjException to OFMemoryNotPartOfObjectException.

    OFObject:
    * -[addItemToMemoryPool:] to -[addMemoryToPool:].
    * -[allocWithSize:] to -[allocMemoryWithSize:].
    * -[allocNItems:withSize] to -[allocMemoryForNItems:withSize:].
    * -[resizeMem:toSize] to -[resizeMemory:toSize:].
    * -[resizeMem:toNItems:withSize:] to
    -[resizeMemoryToNItems:withSize:].
    * -[freeMem] to -[freeMemory:].

    OFString:
    * -[urlencode] to -[urlEncodedString].
    * -[urldecode] to -[urlDecodedString]. (user: js, size: 2874) [annotate] [blame] [check-ins using]


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
{
	OFObject *obj = [[OFObject alloc] init];
	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: NULL], OFMemNotPartOfObjException)


	/* Test allocating memory */
	puts("Allocating memory through object...");
	p = [obj allocWithSize: 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 allocWithSize: 4096];
	q = [obj allocWithSize: 4096];
	r = [obj allocWithSize: 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 allocWithSize: SIZE_MAX], OFNoMemException)


	puts("Allocating 1 byte...");
	p = [obj allocWithSize: 1];

	puts("Trying to resize that 1 byte to more than possible...");
	CATCH_EXCEPTION(p = [obj resizeMem: p
				    toSize: SIZE_MAX],
	    OFNoMemException)

	puts("Trying to resize NULL to 1024 bytes...");
	p = [obj resizeMem: NULL
		    toSize: 1024];
	[obj freeMem: p];

	puts("Trying to resize memory that is not part of object...");
	CATCH_EXCEPTION(p = [obj resizeMem: (void*)1
				    toSize: 1024],
	    OFMemNotPartOfObjException)

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

	return 0;
}







|
>



|




|




|



|
|
|




|
|
|




|
|
|



|
>


|


|
|
|


|
|
|


|
|
|





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
{
	OFObject *obj = [[OFObject alloc] init];
	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 freeMemory: NULL],
	    OFMemoryNotPartOfObjectException)

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

	/* Test freeing the just allocated memory */
	puts("Freeing just allocated memory...");
	[obj freeMemory: 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 freeMemory: p], OFMemoryNotPartOfObjectException)

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

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

	/* Try to free again */
	puts("Now trying to free them again...");
	CATCH_EXCEPTION([obj freeMemory: p], OFMemoryNotPartOfObjectException)
	CATCH_EXCEPTION([obj freeMemory: q], OFMemoryNotPartOfObjectException)
	CATCH_EXCEPTION([obj freeMemory: r], OFMemoryNotPartOfObjectException)
	puts("Got all 3!");

	puts("Trying to allocate more memory than possible...");
	CATCH_EXCEPTION(p = [obj allocMemoryWithSize: SIZE_MAX],
	    OFOutOfMemoryException)

	puts("Allocating 1 byte...");
	p = [obj allocMemoryWithSize: 1];

	puts("Trying to resize that 1 byte to more than possible...");
	CATCH_EXCEPTION(p = [obj resizeMemory: p
				       toSize: SIZE_MAX],
	    OFOutOfMemoryException)

	puts("Trying to resize NULL to 1024 bytes...");
	p = [obj resizeMemory: NULL
		       toSize: 1024];
	[obj freeMemory: p];

	puts("Trying to resize memory that is not part of object...");
	CATCH_EXCEPTION(p = [obj resizeMemory: (void*)1
				       toSize: 1024],
	    OFMemoryNotPartOfObjectException)

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

	return 0;
}