struct / union in initializer, RFE #901.
[sdcc.git] / sdcc / support / regression / tests / structassign.c.in
blobd6db3cd6504715d718500ddbda02981df28f116f
1 /* Test assignment of one struct/union to another, including transitivity.
3 type: char, int, long
5 */
6 #include <testfwk.h>
8 /* declare some structs and unions */
9 struct t { {type} a; };
10 struct s { {type} a; {type} b; };
12 struct t t1 = { 1 };
13 struct t t2 = { 2 };
15 struct s s1 = { 1, 2 };
16 struct s s2 = { 3, 4 };
17 #ifndef __SDCC_pdk14 // Lack of memory
18 struct s s3 = { 5, 6 };
20 union u { {type} a; float b; char c; };
22 union u u1, u2, u3;
23 #endif
25 /* struct: simple assignment */
26 void
27 testSimpleStructAssignment(void)
29 s1 = s2;
30 ASSERT(s1.a == 3);
31 ASSERT(s1.b == 4);
33 t1 = t2;
34 ASSERT(t1.a == 2);
37 /* struct: transitive assignment */
38 void
39 testTransitiveStructAssignment(void)
41 #ifndef __SDCC_pdk14 // Lack of memory
42 s1 = s2 = s3;
43 ASSERT(s1.a == 5);
44 ASSERT(s1.b == 6);
45 ASSERT(s2.a == 5);
46 ASSERT(s2.b == 6);
47 #endif
50 /* union: simple assignment */
51 void
52 testSimpleUnionAssignment(void)
54 #ifndef __SDCC_pdk14 // Lack of memory
55 u1.b = 0.0f;
56 u2.b = -1.5f;
57 u1 = u2;
58 ASSERT(u1.b == -1.5f);
59 #endif
62 /* union: transitive assignment */
63 void
64 testTransitiveUnionAssignment(void)
66 #ifndef __SDCC_pdk14 // Lack of memory
67 u1.b = 0.0f;
68 u2.b = 0.1f;
69 u3.b = -1.5f;
70 u1 = u2 = u3;
71 ASSERT(u1.b == -1.5f);
72 ASSERT(u2.b == -1.5f);
73 #endif
76 /* test for unintended double evaluation */
77 void
78 testUnintendedDoubleEval(void)
80 #ifndef __SDCC_pdk14 // Lack of memory
81 struct s ss[3];
82 int n = 2;
83 ss[2] = ss[--n] = ss[0];
84 ASSERT(n == 1);
85 #endif