autoc 0.8

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,24 @@
1
+ require "autoc/code_builder"
2
+
3
+
4
+ module TypeBuilder
5
+
6
+
7
+ class Struct < CodeBuilder::Code
8
+ attr_reader :type
9
+ attr_reader :fields
10
+ def initialize(type)
11
+ @type = type
12
+ end
13
+ def to_s
14
+ type
15
+ end
16
+ def write_intf(stream)
17
+ stream << %$typedef struct #{type} #{type}; struct #{type} {$
18
+
19
+ stream << '};'
20
+ end
21
+ end # Struct
22
+
23
+
24
+ end # TypeBuilder
Binary file
@@ -0,0 +1,396 @@
1
+ #include <assert.h>
2
+ #include <stdio.h>
3
+ #include <string.h>
4
+
5
+
6
+ #include "test_auto.h"
7
+
8
+
9
+ void PrintIntVector(IntVector* vec) {
10
+ IntVectorIt it;
11
+ IntVectorItCtor(&it, vec);
12
+ while(IntVectorItHasNext(&it)) {
13
+ printf("%d ", IntVectorItNext(&it));
14
+ }
15
+ printf("\n");
16
+ }
17
+
18
+
19
+ void IntVectorTest() {
20
+ IntVector* vec;
21
+ printf("\n*** IntVector\n");
22
+ vec = IntVectorAssign(IntVectorNew(10));
23
+ printf("size = %d\n", IntVectorSize(vec));
24
+ IntVectorSet(vec, 0, 3);
25
+ IntVectorSet(vec, 1, 11);
26
+ IntVectorSet(vec, 2, 4);
27
+ IntVectorSet(vec, 3, 0);
28
+ IntVectorSet(vec, 4, 5);
29
+ IntVectorSet(vec, 5, 99);
30
+ IntVectorSet(vec, 6, 2);
31
+ IntVectorSet(vec, 7, 7);
32
+ IntVectorSet(vec, 8, 6);
33
+ IntVectorSet(vec, 9, 8);
34
+ PrintIntVector(vec);
35
+ printf("vec[0] == %d\n", IntVectorGet(vec, 0));
36
+ IntVectorSort(vec);
37
+ PrintIntVector(vec);
38
+ IntVectorDestroy(vec);
39
+ }
40
+
41
+
42
+ void IntSetTest() {
43
+ IntSet set;
44
+ IntSetIt it;
45
+ printf("\n*** IntSet\n");
46
+ IntSetCtor(&set);
47
+ printf("size = %d\n", IntSetSize(&set));
48
+ IntSetPut(&set, -1);
49
+ IntSetPut(&set, 3);
50
+ IntSetPut(&set, 0);
51
+ IntSetPut(&set, 3);
52
+ printf("size = %d\n", IntSetSize(&set));
53
+ IntSetPut(&set, 7);
54
+ printf("size = %d\n", IntSetSize(&set));
55
+ IntSetItCtor(&it, &set);
56
+ while(IntSetItHasNext(&it)) {
57
+ printf("- %d\n", IntSetItNext(&it));
58
+ }
59
+ IntSetDtor(&set);
60
+ }
61
+
62
+
63
+ size_t PCharHash(const char* s) {
64
+ size_t h = 0;
65
+ while(*s++) {
66
+ h += *s;
67
+ }
68
+ return h;
69
+ }
70
+
71
+
72
+ int PCharEqual(const char* lt, const char* rt) {
73
+ return strcmp(lt, rt) == 0;
74
+ }
75
+
76
+
77
+ void PrintPCharSet(PCharSet* set) {
78
+ PCharSetIt it;
79
+ PCharSetItCtor(&it, set);
80
+ while(PCharSetItHasNext(&it)) {
81
+ printf("%s ", PCharSetItNext(&it));
82
+ }
83
+ printf("\n");
84
+ }
85
+
86
+
87
+ void PCharSetTest() {
88
+ PCharSet set;
89
+ PCharSetIt it;
90
+ printf("\n*** PCharSet (I)\n");
91
+ PCharSetCtor(&set);
92
+ printf("size = %d\n", PCharSetSize(&set));
93
+ PCharSetPut(&set, "cat");
94
+ PCharSetPut(&set, "dog");
95
+ PCharSetPut(&set, "cat");
96
+ printf("size = %d\n", PCharSetSize(&set));
97
+ PCharSetPut(&set, "pig");
98
+ printf("size = %d\n", PCharSetSize(&set));
99
+ PCharSetItCtor(&it, &set);
100
+ while(PCharSetItHasNext(&it)) {
101
+ printf("- %s\n", PCharSetItNext(&it));
102
+ }
103
+ printf("contains(pig) == %d\n", PCharSetContains(&set, "pig"));
104
+ printf("contains(snake) == %d\n", PCharSetContains(&set, "snake"));
105
+ PCharSetPurge(&set);
106
+ PCharSetDtor(&set);
107
+ }
108
+
109
+
110
+ void PCharSetTest2() {
111
+ PCharSet *a, *b, *c;
112
+ printf("\n*** PCharSet (II)\n");
113
+ a = PCharSetAssign(PCharSetNew());
114
+ b = PCharSetAssign(PCharSetNew());
115
+ c = PCharSetAssign(PCharSetNew());
116
+ PCharSetPut(a, "cat");
117
+ PCharSetPut(a, "dog");
118
+ PCharSetPut(a, "pig");
119
+ PCharSetPut(a, "rat");
120
+ PCharSetPut(b, "mouse");
121
+ PCharSetPut(b, "snake");
122
+ PCharSetPut(b, "rat");
123
+
124
+ PCharSetPurge(c);
125
+ PCharSetOr(c, a);
126
+ PCharSetAnd(c, b);
127
+ PrintPCharSet(c);
128
+
129
+ PCharSetPurge(c);
130
+ PCharSetOr(c, a);
131
+ PCharSetOr(c, b);
132
+ PrintPCharSet(c);
133
+
134
+ PCharSetPurge(c);
135
+ PCharSetOr(c, a);
136
+ PCharSetXor(c, b);
137
+ PrintPCharSet(c);
138
+
139
+ PCharSetDestroy(a);
140
+ PCharSetDestroy(b);
141
+ PCharSetDestroy(c);
142
+ }
143
+
144
+
145
+ void PChar2IntMapTest() {
146
+ PChar2IntMap* map;
147
+ PChar2IntMapIt it;
148
+ printf("\n*** PChar2IntMap\n");
149
+ map = PChar2IntMapAssign(PChar2IntMapNew());
150
+ PChar2IntMapPut(map, "cat", 1);
151
+ PChar2IntMapPut(map, "dog", 2);
152
+ PChar2IntMapPut(map, "cat", 0);
153
+ PChar2IntMapPut(map, "pig", 0);
154
+ printf("size = %d\n", PChar2IntMapSize(map));
155
+ PChar2IntMapItCtor(&it, map);
156
+ while(PChar2IntMapItHasNext(&it)) {
157
+ PChar2IntMapEntry entry = PChar2IntMapItNext(&it);
158
+ printf("- %s --> %d\n", entry.key, entry.value);
159
+ }
160
+ PChar2IntMapPurge(map);
161
+ PChar2IntMapDestroy(map);
162
+ }
163
+
164
+
165
+ struct Box {
166
+ size_t refs;
167
+ int contents;
168
+ };
169
+
170
+
171
+ Box* BoxNew() {
172
+ Box* box = (Box*)malloc(sizeof(Box));
173
+ box->refs = 0;
174
+ box->contents = -1;
175
+ return box;
176
+ }
177
+
178
+
179
+ Box* BoxMake(int contents) {
180
+ Box* box = BoxNew();
181
+ box->contents = contents;
182
+ return box;
183
+ }
184
+
185
+
186
+ Box* BoxAssign(Box* box) {
187
+ assert(box);
188
+ ++box->refs;
189
+ return box;
190
+ }
191
+
192
+
193
+ int BoxEqual(Box* lt, Box* rt) {
194
+ assert(lt);
195
+ assert(rt);
196
+ return lt->contents == rt->contents;
197
+ }
198
+
199
+
200
+ size_t BoxHash(Box* box) {
201
+ assert(box);
202
+ return box->contents;
203
+ }
204
+
205
+
206
+ void BoxDestroy(Box* box) {
207
+ assert(box);
208
+ if(!--box->refs) free(box);
209
+ }
210
+
211
+
212
+ void BoxSetTest() {
213
+ int i;
214
+ BoxSet* set;
215
+ Box *b1, *b2, *b4;
216
+ printf("\n*** BoxSet\n");
217
+ b1 = BoxAssign(BoxMake(1));
218
+ b2 = BoxAssign(BoxMake(2));
219
+ b4 = BoxAssign(b2);
220
+ set = BoxSetAssign(BoxSetNew());
221
+ BoxSetPut(set, b1);
222
+ BoxSetPut(set, BoxMake(3));
223
+ BoxSetPut(set, b2);
224
+ BoxSetPut(set, b1);
225
+ printf("size = %d\n", BoxSetSize(set));
226
+ BoxDestroy(b1);
227
+ for(i = 0; i < 100; ++i) {
228
+ BoxSetPut(set, BoxMake(i));
229
+ }
230
+ printf("size = %d\n", BoxSetSize(set));
231
+ BoxSetPurge(set);
232
+ BoxSetDestroy(set);
233
+ BoxDestroy(b2);
234
+ BoxDestroy(b4);
235
+ }
236
+
237
+
238
+ void BoxVectorTest() {
239
+ BoxVector* vec;
240
+ Box* b1;
241
+ printf("\n*** BoxVector\n");
242
+ b1 = BoxAssign(BoxNew());
243
+ vec = BoxVectorAssign(BoxVectorNew(10));
244
+ printf("size = %d\n", BoxVectorSize(vec));
245
+ BoxVectorSet(vec, 7, b1);
246
+ BoxVectorResize(vec, 5);
247
+ printf("size = %d\n", BoxVectorSize(vec));
248
+ BoxVectorSet(vec, 3, BoxNew());
249
+ BoxVectorResize(vec, 10);
250
+ printf("size = %d\n", BoxVectorSize(vec));
251
+ BoxVectorDestroy(vec);
252
+ BoxDestroy(b1);
253
+ }
254
+
255
+
256
+ void BoxListTest() {
257
+ BoxList* list;
258
+ Box *b1,*b2;
259
+ printf("\n*** BoxList\n");
260
+ list = BoxListAssign(BoxListNew());
261
+ b1 = BoxAssign(BoxNew());
262
+ BoxListAdd(list, BoxMake(2));
263
+ BoxListAdd(list, BoxMake(1));
264
+ printf("size = %d\n", BoxListSize(list));
265
+ printf("contains([-1]) == %d\n", BoxListContains(list, b1));
266
+ BoxListChop(list);
267
+ BoxListRemove(list, BoxMake(1));
268
+ BoxListRemove(list, BoxMake(2));
269
+ BoxListRemove(list, BoxMake(3));
270
+ printf("size = %d\n", BoxListSize(list));
271
+ BoxListPurge(list);
272
+ BoxListAdd(list, BoxMake(7));
273
+ BoxListAdd(list, b1);
274
+ BoxListAdd(list, b1);
275
+ b2 = BoxAssign(BoxMake(3));
276
+ printf("size = %d\n", BoxListSize(list));
277
+ printf("contains([-1]) == %d\n", BoxListContains(list, b1));
278
+ BoxListReplaceAll(list, b1, b2);
279
+ printf("contains([-1]) == %d\n", BoxListContains(list, b1));
280
+ printf("contains([3]) == %d\n", BoxListContains(list, b2));
281
+ BoxListChop(list);
282
+ printf("size = %d\n", BoxListSize(list));
283
+ BoxListDestroy(list);
284
+ BoxDestroy(b1);
285
+ BoxDestroy(b2);
286
+ }
287
+
288
+
289
+ void Box2BoxMapTest() {
290
+ int i;
291
+ Box2BoxMap* map, *map2;
292
+ Box *b1, *b2;
293
+ printf("\n*** Box2BoxMap\n");
294
+ map = Box2BoxMapAssign(Box2BoxMapNew());
295
+ b1 = BoxAssign(BoxMake(-1));
296
+ b2 = BoxAssign(b1);
297
+ Box2BoxMapContainsKey(map, BoxMake(3));
298
+ Box2BoxMapContainsKey(map, b1);
299
+ Box2BoxMapReplace(map, BoxMake(-1), BoxMake(-1));
300
+ Box2BoxMapReplace(map, BoxMake(-2), BoxMake(-2));
301
+ Box2BoxMapPut(map, b1, BoxMake(3));
302
+ Box2BoxMapPut(map, BoxMake(2), BoxMake(2));
303
+ Box2BoxMapPut(map, BoxMake(1), BoxMake(1));
304
+ Box2BoxMapReplace(map, BoxMake(1), BoxMake(-3));
305
+ printf("size = %d\n", Box2BoxMapSize(map));
306
+ for(i = 0; i < 100; ++i) {
307
+ Box2BoxMapPut(map, BoxMake(i), BoxMake(-i));
308
+ }
309
+ printf("size = %d\n", Box2BoxMapSize(map));
310
+ i = -2; printf("map[Box(%d)] == Box(%d)\n", i, Box2BoxMapGet(map, BoxMake(i))->contents);
311
+ i = 3; printf("map[Box(%d)] == Box(%d)\n", i, Box2BoxMapGet(map, BoxMake(i))->contents);
312
+ printf("contains([99]) == %d\n", Box2BoxMapContainsKey(map, BoxMake(+99)));
313
+ Box2BoxMapRemove(map, BoxMake(+99));
314
+ printf("contains([99]) == %d\n", Box2BoxMapContainsKey(map, BoxMake(+99)));
315
+ map2 = Box2BoxMapAssign(map);
316
+ Box2BoxMapRemove(map2, BoxMake(-99));
317
+ Box2BoxMapPurge(map);
318
+ Box2BoxMapPut(map2, BoxMake(3), b2);
319
+ BoxDestroy(b2);
320
+ printf("size = %d\n", Box2BoxMapSize(map2));
321
+ Box2BoxMapDestroy(map2);
322
+ Box2BoxMapDestroy(map);
323
+ BoxDestroy(b1);
324
+ }
325
+
326
+
327
+ void PChar2IntVectorMapTest() {
328
+ size_t i;
329
+ PChar2IntVectorMap* map;
330
+ IntVector* vec;
331
+ printf("\n*** PChar2IntVectorMap\n");
332
+ map = PChar2IntVectorMapAssign(PChar2IntVectorMapNew());
333
+ PChar2IntVectorMapPut(map, "zero", IntVectorNew(3));
334
+ PChar2IntVectorMapPut(map, "one", IntVectorNew(3));
335
+ vec = PChar2IntVectorMapGet(map, "one"); for(i = 0; i < IntVectorSize(vec); ++i) {
336
+ IntVectorSet(vec, i, i);
337
+ }
338
+ printf("size = %d\n", IntVectorSize(vec));
339
+ PrintIntVector(vec);
340
+ IntVectorResize(PChar2IntVectorMapGet(map, "one"), 5);
341
+ printf("size = %d\n", IntVectorSize(vec));
342
+ PrintIntVector(vec);
343
+ PChar2IntVectorMapDestroy(map);
344
+ }
345
+
346
+
347
+ void PrintPCharQueue(PCharQueue* queue, int fwd) {
348
+ PCharQueueIt it;
349
+ PCharQueueItCtor(&it, queue, fwd);
350
+ while(PCharQueueItHasNext(&it)) {
351
+ printf("%s ", PCharQueueItNext(&it));
352
+ }
353
+ printf("\n");
354
+ }
355
+
356
+
357
+ void PCharQueueTest() {
358
+ PCharQueue* queue;
359
+ printf("\n*** PCharQueue\n");
360
+ queue = PCharQueueAssign(PCharQueueNew());
361
+ PCharQueuePrepend(queue, "one");
362
+ PCharQueuePrepend(queue, "zero");
363
+ PCharQueueAppend(queue, "two");
364
+ PCharQueueAppend(queue, "three");
365
+ printf("contains(zero) == %d\n", PCharQueueContains(queue, "zero"));
366
+ printf("contains(four) == %d\n", PCharQueueContains(queue, "four"));
367
+ PrintPCharQueue(queue, 1);
368
+ PrintPCharQueue(queue, 0);
369
+ PCharQueueRemove(queue, "two");
370
+ printf("head = %s\n", PCharQueueHead(queue));
371
+ printf("tail = %s\n", PCharQueueTail(queue));
372
+ PCharQueueChopTail(queue);
373
+ PCharQueueChopHead(queue);
374
+ PrintPCharQueue(queue, 1);
375
+ PCharQueuePurge(queue);
376
+ PCharQueueAppend(queue, "zero");
377
+ printf("head = %s\n", PCharQueueHead(queue));
378
+ printf("tail = %s\n", PCharQueueTail(queue));
379
+ PCharQueueDestroy(queue);
380
+ }
381
+
382
+
383
+ int main(int argc, char** argv) {
384
+ IntVectorTest();
385
+ IntSetTest();
386
+ PCharSetTest();
387
+ PCharSetTest2();
388
+ PChar2IntMapTest();
389
+ BoxSetTest();
390
+ BoxVectorTest();
391
+ BoxListTest();
392
+ Box2BoxMapTest();
393
+ PChar2IntVectorMapTest();
394
+ PCharQueueTest();
395
+ return 0;
396
+ }
@@ -0,0 +1,27 @@
1
+ require "autoc"
2
+
3
+
4
+ include CodeBuilder
5
+ include DataStructBuilder
6
+
7
+
8
+ Int = {:type=>"int", :compare=>nil}
9
+ PChar = {:type=>"const char*", :equal=>"PCharEqual", :hash=>"PCharHash"}
10
+ Box = {:type=>"Box*", :forward=>"typedef struct Box Box;", :assign=>"BoxAssign", :equal=>"BoxEqual", :hash=>"BoxHash", :ctor=>"BoxNew", :dtor=>"BoxDestroy"}
11
+
12
+
13
+ IntVector = Vector.new(:IntVector, Int)
14
+
15
+
16
+ CModule.generate!(:Test) do |m|
17
+ m << IntVector
18
+ m << Vector.new(:BoxVector, Box)
19
+ m << List.new(:BoxList, Box)
20
+ m << Queue.new(:PCharQueue, PChar)
21
+ m << HashSet.new(:BoxSet, Box)
22
+ m << HashSet.new(:IntSet, Int)
23
+ m << HashSet.new(:PCharSet, PChar)
24
+ m << HashMap.new(:Box2BoxMap, Box, Box)
25
+ m << HashMap.new(:PChar2IntMap, PChar, Int)
26
+ m << HashMap.new(:PChar2IntVectorMap, PChar, IntVector)
27
+ end