public
Description: The Nu programming language.
Homepage: http://programming.nu
Clone URL: git://github.com/timburks/nu.git
nu / test / test_memory.nu
100644 73 lines (63 sloc) 2.741 kb
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, Neon Design Technology, Inc.
 
(class TestMemory is NuTestCase
     
     (- 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 << x)))
               (assert_equal 0 (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
             (ivars)
             (ivar-accessors)
             (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))