File: struct.c

package info (click to toggle)
c2go 0.26.11-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,052 kB
  • sloc: ansic: 6,037; sh: 82; makefile: 5
file content (674 lines) | stat: -rw-r--r-- 13,427 bytes parent folder | download | duplicates (3)
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
// Tests for structures.

#include <stdio.h>
#include <stdlib.h>
#include "tests.h"

struct programming
{
    float constant;
    char *pointer;
};

void pass_by_ref(struct programming *addr)
{
    char *s = "Show string member.";
    float v = 1.23+4.56;

    addr->constant += 4.56;
    addr->pointer = s;

    is_eq(addr->constant, v);
    is_streq(addr->pointer, "Show string member.");
}

void pass_by_val(struct programming value)
{
    value.constant++;

    is_eq(value.constant, 2.23);
    is_streq(value.pointer, "Programming in Software Development.");
}

/**
 * text
 */
typedef struct mainStruct{
    double constant;
} secondStruct;

/*
 * Text
 */
typedef struct {
    double t;
} ts_c;

// Text
typedef struct ff {
    int v1,v2;
} tt1, tt2;

// Text1
// Text2
struct outer {
    int i;
    struct z {
        int j;
    } inner;
};

struct xx {
    int i;
	/**
	 * Text
	 */
    struct yy {
        int j;
        struct zz {
            int k;
        } deep;
    } inner;
};

/**
 * Some function
 */
int summator(int i, float f){
	return i+(int)(f);
}

typedef struct J J;
struct J
{
	float f;
	int (*fu)(J *j, float i);
};

int j_function(J *j, float i)
{
	if (j != NULL)
	{
		return (int)(i+(*j).f);
	}
	return -1;
};

void struct_with_rec_fuction()
{
	J j;
	j.f = 5.0;
	j.fu = j_function;
	is_eq(j.fu(&j,4.0),9);
	is_eq(j_function(NULL, 4.0),-1);
}

struct FinFinS {
	double d;
	int (*f)(int(*)(int));
};

int FinF1(int a)
{
	return a+1;
}

int FinF2(int (*f)(int))
{
	int g = 45;
	return f(g);
}

void func_in_func_in_struct()
{
	diag("function in function in struct");
	struct FinFinS ffs;
	ffs.f = FinF2;
	int res = ffs.f(FinF1) ;
	is_eq(res , 46);
};

struct info {
	struct deep_info{
		int a, b, c;
	} con;
	struct star_deep_info{
		int sa, sb, sc;
	} * star_con;
};

void struct_in_struct_with_star()
{
	diag("struct in struct with star");
	struct info in;
	
	in.con.a = 45;
	is_eq(in.con.a,45);

	struct star_deep_info st;
	in.star_con = &st;

	in.star_con->sa = 45;
	is_eq(in.star_con->sa,45);
	
	struct info in2;
	
	struct star_deep_info st2;
	in2.star_con = &st2;

	in2.star_con->sa = 46;
	is_eq(in2.star_con->sa,46);
	is_eq(in.star_con->sa,45);
}

struct {
	int a;
} m;
typedef double ** y;

struct {
	int aa;
} mm;
typedef double ** yy;

struct mmm{
	int aaa;
};
typedef double *** yyy;

struct mmmm{
	int aaaa;
};
typedef double **** yyyy;

struct mmmmm0{
	int aaaaa;
}mmmmm;
typedef double ** yyyyy;

typedef struct {
	int st1;
} st2;
typedef double ** st3;

typedef struct st4{
	int st5;
} st6;
typedef double ** st7;
struct st4 st7a; 

typedef struct st4a{
	int st5a;
} * st6a;

typedef struct st4b{
	int st5b;
} *const* st6b;

struct st8{
	int st9;
	struct st10{
		int st11;
	};
};
typedef double ** st12;

struct st13{
	int st14;
	struct st16{
		int st17;
	}st18;
}st19;
typedef double ** st20;

typedef struct st21{
	int st22;
	struct st23{
		int st24;
	}st25;
}st26;
typedef double ** st27;

static struct unix_syscall {
  const char *zName;
} aSyscall[] = {
  { "open"   },
  { "close"  }
};

struct memory{
	int * one;
	float ** oop;
	double * two;
	struct memory * mm;
};

typedef double** subseg;

struct mesh {
	subseg *dummysub;
};

double * returner(int*const* i, double *d)
{
	(void)(i);
	return d;
}

void struct_null()
{
	struct memory  dm;
	float o = 99;
	float * oo = &o;
	float ** ooo = &oo;
	dm.oop = ooo;
	struct memory * m = &dm;
	m->one    = (int           *)(NULL);
	m->two    = (double        *)(NULL);
	m->mm     = (struct memory *)(NULL);
	m->one    = (void          *)(NULL);
	m->two    = (void          *)(NULL);
	m->mm     = (void          *)(NULL);
	*(m->oop) = (int           *) NULL ;
	 (m->oop) = (int           *) NULL ;
	(void)(dm);
	(void)(m);

	(void)summator(1,34.4);
	(void)returner(0,0);
	double fd = 56;
	returner(0, &fd);
	(void)(fd);
	
    static const struct {
      const char *zPattern;
      const char *zDesc;
    } aTrans[] = {
      { "rchar: ",                  "Bytes received by read():" },
      { "wchar: ",                  "Bytes sent to write():"    },
      { "syscr: ",                  "Read() system calls:"      },
      { "syscw: ",                  "Write() system calls:"     },
      { "read_bytes: ",             "Bytes read from storage:"  },
      { "write_bytes: ",            "Bytes written to storage:" },
      { "cancelled_write_bytes: ",  "Cancelled write bytes:"    },
    };

	is_eq(strlen(aTrans[3].zPattern),7);
	is_streq(aTrans[2].zPattern, "syscr: ");
	is_streq(aTrans[1].zDesc,"Bytes sent to write():");

	double d = 99;
	double * dd = &d;
	double **ddd = &dd;
	*(ddd) = (int *) NULL;
	(void)(ddd);
      
	struct memorypool {
		int **nowblock;
	};
	struct memorypool Vpool;
	int nowblock;
	int * s_nowblock = &nowblock;
	Vpool.nowblock = &s_nowblock;
	is_not_null(*Vpool.nowblock);
	*(Vpool.nowblock) = NULL;
	struct memorypool *pool = &Vpool;
	if (*(pool->nowblock) == (int *) NULL) {
		pass("ok");
	}

	mm.aa = 42;
	is_eq(mm.aa,42);

	struct mesh msh;
	subseg sub[10];
	for (int i=0;i<10;i++){sub[i] = (subseg)(ddd);}
	msh.dummysub = sub;
	struct mesh *ms = &msh;
	ms->dummysub[2] = (subseg) NULL;
	(void)(ms);
	pass("ok");
}

union STRS{
	double d;
	struct {
		double d;
	} T;
};

void struct_inside_union()
{
	union STRS s;
	s.T.d = 10.0;
	is_true(s.d != 0);
}

typedef int pointx;
typedef struct  {
    pointx x;
    int y;
} Point2;
const Point2 p2[] = { { .y = 4, .x = 5 } };
const Point2* getPoint(int index) {
    return &(p2[index]);
}
typedef unsigned char pcre_uchar;
typedef unsigned char pcre_uint8;
typedef unsigned int pcre_uint32;
typedef struct spu {
    pcre_uchar *hvm;
} spu;

void pointer_arithm_in_struct() {
    pcre_uchar str[] = "abcd";
    spu s;
    spu *ps = &s;
    ps->hvm = &str[1];
    is_true(ps->hvm == &str[1]);
    ps->hvm += 2;
    is_true(ps->hvm == &str[3]);

}

typedef struct pcre_extra {
  unsigned long int flags;
  void *study_data;
  unsigned long int match_limit;
  void *callout_data;
  const unsigned char *tables;
  unsigned long int match_limit_recursion;
  unsigned char **mark;
  void *executable_jit;
} pcre_extra;
typedef struct pcre_study_data {
  pcre_uint32 size;
  pcre_uint32 flags;
  pcre_uint8 start_bits[32];
  pcre_uint32 minlength;
} pcre_study_data;

void test_mark()
{
    pcre_extra *extra = NULL;
    pcre_study_data *study;
    pcre_uint8 *markptr;
    void * allocated = malloc(sizeof(pcre_extra) + sizeof(pcre_study_data));
    extra = (pcre_extra *)allocated;
    study = (pcre_study_data *)((char *)extra + sizeof(pcre_extra));
    memset(study->start_bits, 0, 32 * sizeof(pcre_uint8));
    extra->study_data = study;
    study->size = sizeof(pcre_study_data);
    extra->mark = &markptr;
    is_eq(study->size, 44);
    for (int i=0; i<32; i++)
        is_eq(study->start_bits[i], 0);
}

int main()
{
    plan(104);

    struct programming variable;
    char *s = "Programming in Software Development.";

    variable.constant = 1.23;
    variable.pointer = s;

    is_eq(variable.constant, 1.23);
    is_streq(variable.pointer, "Programming in Software Development.");

    pass_by_val(variable);
    pass_by_ref(&variable);

    struct mainStruct s1;
    s1.constant = 42.;
    is_eq(s1.constant, 42.);

    secondStruct s2;
    s2.constant = 42.;
    is_eq(s2.constant, 42.);

    ts_c c;
    c.t = 42.;
    is_eq(c.t , 42.);

    tt1 t1;
    t1.v1 = 42.;
    is_eq(t1.v1,42.)

    tt2 t2;
    t2.v1 = 42.;
    is_eq(t2.v1,42.)

    struct ff tf2;
    tf2.v2 = t1.v1;
    is_eq(tf2.v2,t1.v1);

    struct outer o;
    o.i = 12;
    o.inner.j = 34;
    is_eq(o.i + o.inner.j, 46);

    struct xx x;
    x.i = 12;
    x.inner.j = 34;
    x.inner.deep.k = 56;
    is_eq(x.i + x.inner.j + x.inner.deep.k, 102);

	struct u{
		int y;
	};
	struct u yy;
	yy.y = 42;
	is_eq(yy.y,42);
	
	diag("Typedef struct with same name")
	{
		typedef struct Uq Uq;
		struct Uq{
			int uq;
		};
		Uq uu;
		uu.uq = 42;
		is_eq(uu.uq,42);
	}

	diag("Initialization of struct")
	struct Point {
		int x;
		int y;
	};
	struct Point p = { .y = 2, .x = 3 };
	is_eq(p.x, 3);
	is_eq(p.y, 2);

	diag("Initialization of a const struct pointer")
	const Point2* pp2 = getPoint(0);
	int pointSum = pp2->x + pp2->y;
	is_eq(pointSum, 9);

	diag("ImplicitValueInitExpr")
	{
		typedef struct {
		    int x2;
		    int y2;
		} coord2;

		typedef struct {
		    coord2 position2;
		    int possibleSteps2;
		} extCoord2;

		extCoord2 followingSteps[2] =
	    {
	        {.possibleSteps2 = 1}, {.possibleSteps2 = 1},
	    };
		is_eq(followingSteps[0].possibleSteps2, 1);
	}
	{
		struct coord{
		    int x;
		    int y;
		};

		struct extCoord{
		    struct coord position;
		    int possibleSteps;
		};

		struct extCoord followingSteps[2] =
	    {
	        {.possibleSteps = 1}, {.possibleSteps = 1},
	    };
		is_eq(followingSteps[0].possibleSteps, 1);
	}

	diag("Double typedef type")
	{
		typedef int  int2;
		typedef int2 int3;
		typedef int3 int4;

		is_eq((int)((int4)((int3)((int2)(42)))),42);
	}
	{
		typedef size_t size2;
		is_eq(((size2)((size_t)(56))),56.0)
	}
	{
		is_eq((size_t)(43),43);
	}

	diag("Function pointer inside struct")
	{
		struct F1{
			  int x;
			  int (*f)(int, float);
		};
		struct F1 f1;
		f1.x = 42;
		f1.f = summator;
		is_eq(f1.x,42);
		is_eq(f1.f(3,5),8);
	}
	{
		typedef struct {
			  int x;
			  int (*f)(int, float);
		} F2;
		F2 f2;
		f2.x = 42;
		f2.f = summator;
		is_eq(f2.x,42);
		is_eq(f2.f(3,5),8);
	}

	diag("typedef function")
	{
		typedef int ALIAS (int, float);
		ALIAS * f = summator;
		is_eq(f(3,5),8);
	}
	{
		typedef int ALIAS2 (int, float);
		ALIAS2 * f;
		f = summator;
		is_eq(f(3,5),8);
	}

	diag("typedef struct C C inside function")
	{
		typedef struct CCC CCC;
		struct CCC {
			float ff;
		};
		CCC c;
		c.ff = 3.14;
		is_eq(c.ff,3.14);
	}
	typedef struct CP CP;
	struct CP {
		float ff;
	};
	CP cp;
	cp.ff = 3.14;
	is_eq(cp.ff,3.14);

	diag("struct name from Go keyword")
	{ struct chan        {int i;}; struct chan        UU;  UU.i = 5; is_eq(UU.i,5);}
	{ struct defer       {int i;}; struct defer       UU;  UU.i = 5; is_eq(UU.i,5);}
	{ struct fallthrough {int i;}; struct fallthrough UU;  UU.i = 5; is_eq(UU.i,5);}
	{ struct func        {int i;}; struct func        UU;  UU.i = 5; is_eq(UU.i,5);}
	{ struct go          {int i;}; struct go          UU;  UU.i = 5; is_eq(UU.i,5);}
	{ struct import      {int i;}; struct import      UU;  UU.i = 5; is_eq(UU.i,5);}
	{ struct interface   {int i;}; struct interface   UU;  UU.i = 5; is_eq(UU.i,5);}
	{ struct map         {int i;}; struct map         UU;  UU.i = 5; is_eq(UU.i,5);}
	{ struct package     {int i;}; struct package     UU;  UU.i = 5; is_eq(UU.i,5);}
	{ struct range       {int i;}; struct range       UU;  UU.i = 5; is_eq(UU.i,5);}
	{ struct select      {int i;}; struct select      UU;  UU.i = 5; is_eq(UU.i,5);}
	{ struct type        {int i;}; struct type        UU;  UU.i = 5; is_eq(UU.i,5);}
	{ struct var         {int i;}; struct var         UU;  UU.i = 5; is_eq(UU.i,5);}
	{ struct _           {int i;}; struct _           UU;  UU.i = 5; is_eq(UU.i,5);}
	{ struct init        {int i;}; struct init        UU;  UU.i = 5; is_eq(UU.i,5);}
	{ struct len         {int i;}; struct len         UU;  UU.i = 5; is_eq(UU.i,5);}
	{ struct copy        {int i;}; struct copy        UU;  UU.i = 5; is_eq(UU.i,5);}
	{ struct fmt         {int i;}; struct fmt         UU;  UU.i = 5; is_eq(UU.i,5);}
	{ struct cap         {int i;}; struct cap         UU;  UU.i = 5; is_eq(UU.i,5);}

	// uncomment after success implementation of struct scope
	// https://github.com/elliotchance/c2go/issues/368
/*
	diag("Typedef struct name from Go keyword")
	{ typedef struct {int i;} chan        ;	chan        UU; UU.i = 5; is_eq(UU.i,5);}
	{ typedef struct {int i;} defer       ;	defer       UU; UU.i = 5; is_eq(UU.i,5);}
	{ typedef struct {int i;} fallthrough ;	fallthrough UU; UU.i = 5; is_eq(UU.i,5);}
	{ typedef struct {int i;} func        ;	func        UU; UU.i = 5; is_eq(UU.i,5);}
	{ typedef struct {int i;} go          ;	go          UU; UU.i = 5; is_eq(UU.i,5);}
	{ typedef struct {int i;} import      ;	import      UU; UU.i = 5; is_eq(UU.i,5);}
	{ typedef struct {int i;} interface   ;	interface   UU; UU.i = 5; is_eq(UU.i,5);}
	{ typedef struct {int i;} map         ;	map         UU; UU.i = 5; is_eq(UU.i,5);}
	{ typedef struct {int i;} package     ;	package     UU; UU.i = 5; is_eq(UU.i,5);}
	{ typedef struct {int i;} range       ;	range       UU; UU.i = 5; is_eq(UU.i,5);}
	{ typedef struct {int i;} select      ;	select      UU; UU.i = 5; is_eq(UU.i,5);}
	{ typedef struct {int i;} type        ;	type        UU; UU.i = 5; is_eq(UU.i,5);}
	{ typedef struct {int i;} var         ;	var         UU; UU.i = 5; is_eq(UU.i,5);}
	{ typedef struct {int i;} _           ;	_           UU; UU.i = 5; is_eq(UU.i,5);}
	{ typedef struct {int i;} init        ;	init        UU; UU.i = 5; is_eq(UU.i,5);}
*/

	struct_with_rec_fuction();

	diag("name of struct inside struct")
	{
		typedef struct TI TI;
		struct TI{
			TI *left, *right;
			double varTI;
		};
		TI t1;
		t1.varTI = 4.3;
		TI t2;
		t2.varTI = 4.1;
		TI tt;
		tt.left       = &t1;
		(*tt.left).right = &t2;
		tt.right      = &t2;
		is_eq((*tt.left  ).varTI, 4.3);
		is_eq((*(*tt.left).right).varTI, 4.1);
		is_eq((*tt.right ).varTI, 4.1);
	}
	
	struct_in_struct_with_star();
	struct_null();

	func_in_func_in_struct();

	struct_inside_union();

	pointer_arithm_in_struct();

	test_mark();

    done_testing();
}