Debian Codegraph @

src/test/01-Basic/221-dangle-ref-loops.ck

raw
1 // verify func-call returning objects is properly refcounted
2 // as part or the stmt's object release mechanism | 1.5.1.7
3
4 // a class
5 class Foo { 5 => int x; }
6 // a file-global instance
7 Foo foo;
8 // a function that returns foo
9 fun Foo bar() { return foo; }
10 // another function that returns foo
11 fun Object zzz() { return foo; }
12
13 // for loop
14 for( int i; i < bar().x; i++ ) { }
15
16 // reset
17 5 => foo.x;
18
19 // bar() returns an object every iteration, verify the
20 // stmt reference count is correct
21 while( bar().x )
22 {
23 // do this a few times
24 bar().x--;
25 // break
26 if( bar().x == 2 ) { break; }
27 }
28
29 // reset
30 5 => foo.x;
31
32 // try until
33 until( bar().x == 0 )
34 {
35 // do this a few times
36 bar().x--;
37 // break
38 if( bar().x == 2 ) break;
39 }
40
41 // reset
42 5 => foo.x;
43
44 // do while
45 do {
46 // do this a few times
47 bar().x--;
48 // break
49 if( bar().x == 2 ) break;
50 } while( bar().x );
51
52 // reset
53 1001 => foo.x;
54
55 // do until
56 do {
57 // do this a few times
58 bar().x--;
59 // break
60 if( bar().x == 2 ) continue;
61 // do this a few times
62 bar().x--;
63 } until( bar().x < 1 );
64
65 20 => foo.x;
66
67 // loop
68 repeat( bar().x ) { bar().x--; }
69
70 // for each with array Object
71 for( int x : Std.range(0,20) ) { }
72
73 // for loop with dangling ref in conditional part 2
74 for( ; zzz() == null; ) { }
75
76 // reset
77 13 => bar().x;
78
79 // since bar() returns an object every iteration, verify the
80 // stmt reference count is correct
81 while( bar().x )
82 {
83 // do this a few times
84 bar().x--;
85 // break
86 while( bar().x ) break;
87 }
88
89 // check
90 if( Machine.refcount(foo) == 1 ) <<< "success" >>>;