source: tests/collections/queue.cfa@ e1358c0

Last change on this file since e1358c0 was 161b98e, checked in by Andrew Beach <ajbeach@…>, 10 months ago

Cleaned up some errors that did not show up in my local test but caused the nightly build to fail.

  • Property mode set to 100644
File size: 4.3 KB
RevLine 
[de2f969]1#include <fstream.hfa>
2#include <stdlib.hfa> // new, delete
3#include <bits/queue.hfa>
4
5int main() {
6 // Fred test
7
8 struct Fred {
9 inline Colable; // Plan 9 inheritance
10 int i;
11 };
12 void ?{}( Fred & fred ) { abort(); }
13 void ?{}( Fred & fred, int p ) with( fred ) {
14 i = p;
15 }
16 Fred *& Next( Fred * n ) {
17 return (Fred *)Next( (Colable *)n );
18 }
19
20 Queue(Fred) fred;
[657c36f]21 QueueIter(Fred) iter = { fred };
[de2f969]22 Fred & f;
23
24 sout | nlOff; // turn off auto newline
25
[657c36f]26 for ( ; iter | f; ) { // empty list
[de2f969]27 sout | f.i | ' ';
28 }
29 sout | "empty" | nl;
[bada452]30
[de2f969]31 for ( i; 10 ) {
32 add( fred, *new( 2 * i ) );
33 }
34
[62a7cc0]35 sout | head( fred ).i | tail( fred ).i | nl;
[de2f969]36
[657c36f]37 for ( QueueIter(Fred) iter = { fred }; iter | f; ) {
[de2f969]38 sout | f.i | ' ';
39 }
40 sout | nl;
41
42 for ( i; 9 ) {
43 delete( &drop( fred ) );
44 }
45
[657c36f]46 for ( over( iter, fred ); iter | f; ) {
[de2f969]47 sout | f.i | ' ';
48 }
49 sout | nl;
[bada452]50
[de2f969]51 for ( i; 10 ) {
52 add( fred, *new( 2 * i + 1 ) );
53 }
54
[62a7cc0]55 Fred * head = new( -1 ), tail = { -2 };
56 addHead( fred, *head );
57 addTail( fred, tail );
[de2f969]58
[62a7cc0]59 sout | head( fred ).i | succ( fred, head )->i | tail( fred ).i | nl;
[657c36f]60
61 for ( over( iter, fred ); iter | f; ) {
[62a7cc0]62 sout | f.i | ' ';
63 }
64 sout | nl;
65
66 remove( fred, *head );
67 remove( fred, tail );
68 delete( head );
69 delete( &dropTail( fred ) );
[de2f969]70
[657c36f]71 for ( over( iter, fred ); iter | f; ) {
[de2f969]72 sout | f.i | ' ';
73 }
74 sout | nl;
75
[62a7cc0]76 for ( i; 5 ) {
77 delete( &dropTail( fred ) );
78 }
[657c36f]79 for ( over( iter, fred ); iter | f; ) {
[62a7cc0]80 sout | f.i | ' ';
81 }
82 sout | nl;
83
[657c36f]84 for ( over( iter, fred ); iter | f; ) {
[62a7cc0]85 delete( &remove( fred, f ) );
86 }
[657c36f]87 for ( over( iter, fred ); iter | f; ) {
[62a7cc0]88 sout | f.i | ' ';
[de2f969]89 }
[62a7cc0]90 sout | "empty" | nl;
[de2f969]91
[161b98e]92 Fred & middle = f;
[de2f969]93 for ( i; 10 ) {
[62a7cc0]94 add( fred, *new( i ) );
95 if ( i == 4 ) {
96 &middle = &tail( fred );
[de2f969]97 }
98 }
[657c36f]99 for ( QueueIter(Fred) iter = { fred }; iter | f; ) {
[de2f969]100 sout | f.i | ' ';
101 }
102 sout | nl;
103
104 Queue(Fred) fred2;
105
[62a7cc0]106 split( fred2, fred, middle );
[de2f969]107
[657c36f]108 for ( over( iter, fred ); iter | f; ) {
[de2f969]109 sout | f.i | ' ';
110 }
111 sout | nl;
112
[657c36f]113 for ( over( iter, fred2 ); iter | f; ) {
[de2f969]114 sout | f.i | ' ';
115 }
116 sout | nl;
117
[62a7cc0]118 transfer( fred, fred2 );
[de2f969]119
[657c36f]120 for ( over( iter, fred ); iter | f; ) {
[de2f969]121 sout | f.i | ' ';
122 }
123 sout | nl;
124
[657c36f]125 for ( over( iter, fred ); iter | f; ) {
[de2f969]126 delete( &f );
127 }
128
129 // Mary test
130
131 struct Mary {
132 inline Fred; // Plan 9 inheritance
133 int j;
134 };
[bada452]135 __attribute__((unused))
[de2f969]136 void ?{}( Mary & mary ) { abort(); }
137 void ?{}( Mary & mary, int p ) with( mary ) {
138 ((Fred &)mary){ p };
[62a7cc0]139 j = p;
[de2f969]140 }
141 Mary *& Next( Mary * n ) {
142 return (Mary *)Next( (Fred *)n );
143 }
144
145 Queue(Mary) mary;
[657c36f]146 QueueIter(Mary) iter = { mary };
[de2f969]147 Mary & m;
148
[657c36f]149 for ( ; iter | m; ) { // empty list
[de2f969]150 sout | m.i | m.j | ' ';
151 }
152 sout | "empty" | nl;
[bada452]153
[de2f969]154 for ( i; 10 ) {
155 add( mary, *new( 2 * i ) );
156 }
157
[62a7cc0]158 sout | head( mary ).i | tail( mary ).i | nl;
159
[657c36f]160 for ( QueueIter(Mary) iter = { mary }; iter | m; ) {
[de2f969]161 sout | m.i | m.j | ' ';
162 }
163 sout | nl;
[bada452]164
[de2f969]165 for ( i; 9 ) {
166 delete( &drop( mary ) );
167 }
168
[657c36f]169 for ( over( iter, mary ); iter | m; ) {
[de2f969]170 sout | m.i | m.j | ' ';
171 }
172 sout | nl;
[bada452]173
[de2f969]174 for ( i; 10 ) {
175 add( mary, *new( 2 * i + 1 ) );
176 }
[62a7cc0]177
[657c36f]178 Mary * head = new( -1 ), tail = { -2 };
[62a7cc0]179 addHead( mary, *head );
180 addTail( mary, tail );
[657c36f]181
[62a7cc0]182 sout | head( mary ).i | succ( mary, head )->i | tail( mary ).i | nl;
[657c36f]183
184 for ( over( iter, mary ); iter | m; ) {
185 sout | m.i | m.j | ' ';
186 }
187 sout | nl;
188
[62a7cc0]189 remove( mary, *head );
190 remove( mary, tail );
191 delete( (Mary *)head );
192 delete( &dropTail( mary ) );
193
[657c36f]194 for ( over( iter, mary ); iter | m; ) {
[de2f969]195 sout | m.i | m.j | ' ';
196 }
197 sout | nl;
198
[657c36f]199 for ( i; 5 ) {
200 delete( &dropTail( mary ) );
201 }
202 for ( over( iter, mary ); iter | m; ) {
203 sout | m.i | m.j | ' ';
204 }
205 sout | nl;
206
207 for ( over( iter, mary ); iter | m; ) {
[62a7cc0]208 delete( &remove( mary, m ) );
209 }
[657c36f]210 for ( over( iter, mary ); iter | m; ) {
211 sout | m.i | m.j | ' ';
212 }
213 sout | "empty" | nl;
[62a7cc0]214
[161b98e]215 Mary & middle = m;
[62a7cc0]216 for ( i; 10 ) {
217 add( mary, *new( i ) );
218 if ( i == 4 ) {
219 &middle = &tail( mary );
220 }
221 }
[657c36f]222 for ( QueueIter(Mary) iter = { mary }; iter | m; ) {
[62a7cc0]223 sout | m.i | m.j | ' ';
224 }
225 sout | nl;
226
227 Queue(Mary) mary2;
228
229 split( mary2, mary, middle );
230
[657c36f]231 for ( over( iter, mary ); iter | m; ) {
[62a7cc0]232 sout | m.i | m.j | ' ';
233 }
234 sout | nl;
[657c36f]235 for ( over( iter, mary2 ); iter | m; ) {
[62a7cc0]236 sout | m.i | m.j | ' ';
237 }
238 sout | nl;
239
240 transfer( mary, mary2 );
241
[657c36f]242 for ( over( iter, mary ); iter | m; ) {
[62a7cc0]243 sout | m.i | m.j | ' ';
244 }
245 sout | nl;
[657c36f]246
247 for ( over( iter, mary ); iter | m; ) {
[de2f969]248 delete( &m );
249 }
250}
Note: See TracBrowser for help on using the repository browser.