forked from programming-nu/nu
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_memory.nu
73 lines (64 loc) · 3.3 KB
/
test_memory.nu
1
2
3
4
5
6
7
8
9
10
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
;; test_memory.nu
;; tests for Nu memory management.
;;
;; Copyright (c) 2008 Tim Burks, Radtastical Inc.
(class TestMemory is NuTestCase
(unless (and (eq (uname) "Darwin")
((NSGarbageCollector defaultCollector) isEnabled))
(- testCreationInObjCUsingObjC is
(NuTestHelper resetDeallocationCount)
(5 times:
(do (i)
(set x (NuTestHelper helperInObjCUsingAllocInit))))
(assert_equal 5 (NuTestHelper deallocationCount)))
(- testCreationInNuUsingObjC is
(NuTestHelper resetDeallocationCount)
(4 times:
(do (i)
(set x (NuTestHelper helperInNuUsingAllocInit))))
(assert_equal 4 (NuTestHelper deallocationCount)))
(- testCreationInObjCUsingNu is
(NuTestHelper resetDeallocationCount)
(3 times:
(do (i)
(set x (NuTestHelper helperInObjCUsingNew))))
(assert_equal 3 (NuTestHelper deallocationCount)))
(- testCreationInObjCUsingNuWithOwnership is
(NuTestHelper resetDeallocationCount)
(5 times:
(do (j)
(set a (array))
(2 times:
(do (i)
(set x (NuTestHelper helperInObjCUsingNew))
(a addObject: x))
;; at the end of each loop iteration, the two NuTestHelpers are deleted with the iteration's autorelease pool
(assert_equal (* 2 j) (NuTestHelper deallocationCount)))))
(assert_equal 10 (NuTestHelper deallocationCount)))
(- testIvarReleaseOnDealloc is
(class IvarReleaseHelper is NuTestHelper
;;(ivar (id) x) ;; currently declared ivars are not released, this is consistent with unretained outlets
(set myDeallocationCount 0) ;; closure gives this variable class scope.
(+ (int) myDeallocationCount is myDeallocationCount)
(- (void) dealloc is
(set myDeallocationCount (+ myDeallocationCount 1))
(set self nil) ;; remove self from the evaluation context to prevent a crash when the context releases its contents upon deallocation.
(super dealloc)))
(NuTestHelper resetDeallocationCount)
(let () ;; let wraps its evaluation with a dedicated autorelease pool
(set f ((IvarReleaseHelper alloc) init))
(f setX:((IvarReleaseHelper alloc) init))
(f setY:((IvarReleaseHelper alloc) init))
((f x) setX:(f y))
(f setZ:(f x))
(set f nil))
(assert_equal 3 (NuTestHelper deallocationCount))
(assert_equal 3 (IvarReleaseHelper myDeallocationCount)))))
(class NuTestHelper
(+ new is
((self alloc) init))
(+ helperInNuUsingAllocInit is
((self alloc) init))
(- init is
(super init)
self))