source: tests/collections/vector-demo.cfa @ e21f253

ADTast-experimentalenumforall-pointer-decaypthread-emulationqualifiedEnum
Last change on this file since e21f253 was 44856ed, checked in by Michael Brooks <mlbrooks@…>, 3 years ago

Baseline "new" vector, with iterators.

Implementation has not had thorough correctness testing, e.g. checking wraparound
behaviours, and at least one such case is commented as unimplemented.

Implementation has not been optimized at the instruction path level, though a basic
iteration performance check has it within 5% of c++ std::vector.

  • Property mode set to 100644
File size: 4.4 KB
Line 
1#include <containers/vector2.hfa>
2
3void raiiTests() {
4    printf("raiiTests=================\n");
5
6    vector( float ) v = { 25 };
7
8    push_last( v, 1 );
9    printf( "Having pushed, length is %ld\n", v`length );
10
11    float y = v[0]`val;
12    printf( "by transit, got pos0 = %f\n", y );
13
14    {
15        vector_exit(float) it = v[0];
16
17        float z = it`val;
18        printf( "by exit, got pos0 = %f\n", z );
19
20        // forbid modification while locked
21      #ifdef TRY_MOD_WHILE_LOCKED_1
22        push_last( v, 1 );   // runtime assertion failure
23        printf( "Having pushed, length is %ld\n", v`length );
24      #endif
25    }
26
27    push_last( v, 2 );
28    printf( "Having pushed, length is %ld\n", v`length );
29
30    // deletion scope for some exits
31    // point is the function decls and calls, though
32    {
33        void helperE(vector_exit(float) it) {
34            float q = it`val;
35            printf( "helperE sees %f\n", q );
36        }
37
38        vector_exit(float) it2 = v[1];
39        helperE(it2);
40
41        // can't call with implied exit (wish I could, low priority)
42      #ifdef TRY_IMPLIED_EXIT_1
43        helperE( v[1] ); // Invalid application of existing declaration(s)
44      #endif
45
46        //------
47
48        vector_exit(float) producerE( vector( float ) & theVec ) {
49          return theVec[0];
50        }
51
52        vector_exit(float) it3 = producerE( v );
53        float z = it3`val;
54        printf( "producerE gave %f\n", z );
55
56        float zzzz = producerE( v )`val;
57        printf( "producerE again gave %f\n", zzzz );
58
59        //------
60
61        void helperT(vector_transit(float) it) {
62            float q = it`val;
63            printf( "helperT sees %f\n", q );
64        }
65
66        helperT( v[1] );
67
68        //------
69
70        vector_transit(float) producerT( vector( float ) & theVec ) {
71            return theVec[0];
72        }
73
74        float zz = producerT( v )`val;
75        printf( "producerT gave %f\n", zz );
76    }
77
78    //=======
79
80    {
81        vector_permit(float) it = v[0];
82
83        float z = it`val;
84        printf( "by permit, got pos0 = %f\n", z );
85
86        // allow modification while permits exist
87        push_last( v, 1 );
88        printf( "Having pushed, length is %ld\n", v`length );
89
90        // forbid passing permit by value
91      #ifdef TRY_PASS_PERMIT_BYVAL_1
92        void f( vector_permit(float) xx ) {
93            printf("can't get here\n");
94        }
95        f( it ); // Unique best alternative includes deleted identifier
96      #endif
97
98        // can declare function that returns permit (wish to forbid)
99        vector_permit(float) g( vector( float ) & theVec ) {
100            return theVec[0]; // want to forbid
101        }
102
103        // forbid calling a function that returns permit by value
104      #ifdef TRY_RETURN_PERMIT_BYVAL_1
105        vector_permit(float) ofG = g( v ); // Unique best alternative includes deleted identifier
106      #endif
107       
108        // allow declaration of permit, populating from exit
109        vector_exit(float) h( vector( float ) & theVec ) {
110            return theVec[0];
111        }
112
113        vector_permit(float) ofH = h( v );
114        float zh = ofH`val;
115        printf( "into permit from call, got ofH = %f\n", zh );
116    }
117}
118
119void stayValidTests() {
120    printf("stayValidTests============\n");
121    vector( float ) v = { 4 };
122    push_last(v, 0.0f);
123    push_last(v, 0.1f);
124    push_last(v, 0.2f);
125    // v is [0.0, 0.1, 0.2]
126
127    vector_permit(float) iter = v[1];  // iter at 0.1
128
129    float val1 = iter`val;
130    printf("before %f\n", val1);
131
132    insert_before( v, 1, 98.6f );  // v is [0.0, 98.6, 0.1, 0.2]; iter at 0.1
133
134    float val2 = iter`val;
135    printf("after, logical: %f\n", val2);
136
137    // we had filled it to the brim
138    assert( v`capacity == 4 && v`length == 4 );
139
140    push_last(v, -100); // v is [0.0, 98.6, 0.1, 0.2, 0.3]; iter at 0.1, but RTP it's not looking at the old memory's 0.1
141
142    // that made it bigger
143    assert( v`capacity >  5 && v`length == 5 );
144
145    v[2] = -0.1;  // v is [0.0, 98.6, -0.1, 0.2, 0.3]; iter at -0.1, where only the new memory had that change
146
147    float val3 = iter`val;
148    printf("after, physical: %f\n", val3);
149}
150
151void loopTests() {
152    printf("loopTests=================\n");
153    vector(float) v = { 4 };
154    push_last(v, 0.0f);
155    push_last(v, 0.1f);
156    push_last(v, 0.2f);
157    float toPrint;
158
159    while( vector_exit(float) it = v`origin; it`moveNext ) {
160        toPrint *= it;
161        printf("loop sees %f\n", toPrint);
162    }
163}
164
165int main() {
166    raiiTests();
167    stayValidTests();
168    loopTests();
169}
Note: See TracBrowser for help on using the repository browser.