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
|
// Tests for unions.
#include <stdio.h>
#include "tests.h"
union programming
{
int constant;
char *pointer;
};
union programming init_var()
{
union programming variable;
char *s = "Programming in Software Development.";
variable.pointer = s;
is_streq(variable.pointer, "Programming in Software Development.");
variable.constant = 123;
is_eq(variable.constant, 123);
return variable;
}
void pass_by_ref(union programming *addr)
{
char *s = "Show string member.";
int v = 123+456;
addr->constant += 456;
is_eq(addr->constant, v);
addr->pointer = s;
is_streq(addr->pointer, "Show string member.");
}
void var_by_val(union programming value)
{
value.constant++;
is_eq(value.constant, 124);
}
struct SHA3 {
union {
double iY;
double dY;
} uY;
float ffY;
};
union unknown {
double i2;
double d2;
};
struct SHA32 {
union unknown u2;
float ff2;
};
void union_inside_struct()
{
diag("Union inside struct")
struct SHA3 sha;
sha.ffY = 12.444;
sha.uY.iY = 4;
is_eq(sha.uY.iY, 4);
is_eq(sha.uY.dY, 4);
is_eq(sha.ffY , 12.444);
struct SHA32 sha2;
sha2.ff2 = 12.444;
sha2.u2.i2 = 4;
is_eq(sha2.u2.i2, 4);
is_eq(sha2.u2.d2, 4);
is_eq(sha2.ff2 , 12.444);
pass("ok");
}
typedef union myunion myunion;
typedef union myunion
{
double PI;
int B;
}MYUNION;
typedef union
{
double PI;
int B;
}MYUNION2;
void union_typedef()
{
diag("Typedef union")
union myunion m;
double v = 3.14;
m.PI = v;
is_eq(m.PI,3.14);
is_true(m.B != 0);
is_eq(v, 3.14);
v += 1.0;
is_eq(v, 4.14);
is_eq(m.PI,3.14);
MYUNION mm;
mm.PI = 3.14;
is_eq(mm.PI,3.14);
is_true(mm.B != 0);
myunion mmm;
mmm.PI = 3.14;
is_eq(mmm.PI,3.14);
is_true(mmm.B != 0);
MYUNION2 mmmm;
mmmm.PI = 3.14;
is_eq(mmmm.PI,3.14);
is_true(mmmm.B != 0);
}
typedef struct FuncDestructor FuncDestructor;
struct FuncDestructor {
int i;
};
typedef struct FuncDef FuncDef;
struct FuncDef {
int i;
union {
FuncDef *pHash;
FuncDestructor *pDestructor;
} u;
};
void union_inside_struct2()
{
FuncDef f;
FuncDestructor fd;
fd.i = 100;
f.u.pDestructor = &fd;
FuncDestructor * p_fd = f.u.pDestructor;
is_eq((*p_fd).i , 100);
is_true(f.u.pHash != NULL);
is_true(f.u.pDestructor != NULL);
int vHash = (*f.u.pHash).i;
is_eq(vHash , 100);
is_eq((*f.u.pHash).i , 100);
}
union UPNT{
int * a;
int * b;
int * c;
};
void union_pointers()
{
union UPNT u;
int v = 32;
u.a = &v;
is_eq(*u.a,32);
is_eq(*u.b,32);
is_eq(*u.c,32);
pass("ok")
}
union UPNTF{
int (*f1)(int);
int (*f2)(int);
};
int union_function(int a)
{
return a+1;
}
void union_func_pointers()
{
union UPNTF u;
u.f1 = union_function;
is_eq(u.f1(21), 22);
is_eq(u.f2(21), 22);
}
union array_union
{
float a[2];
float b[2];
};
void union_array()
{
union array_union arr;
arr.a[0] = 12;
arr.b[1] = 14;
is_eq( arr.a[0] , 12);
is_eq( arr.a[1] , 14);
is_eq( arr.b[0] , 12);
is_eq( arr.b[1] , 14);
}
typedef int ii;
typedef struct SHA SHA;
struct SHA{
union {
ii s[25];
unsigned char x[100];
} u;
unsigned uuu;
};
void union_arr_in_str()
{
SHA sha;
sha.uuu = 15;
is_eq(sha.uuu,15);
for (int i = 0 ; i< 25;i++)
sha.u.s[0] = 0;
is_eq(sha.u.s[0],0);
is_true(sha.u.x[0] == 0);
for (int i=0;i<6;i++){
sha.u.s[i] = (ii)(4);
sha.u.s[i] = (ii)(42) + sha.u.s[i];
}
is_eq(sha.u.s[5],46);
is_true(sha.u.x[0] != 0);
}
union un_struct{
struct {
short a;
short b;
} str;
long l;
};
void union_with_struct()
{
union un_struct u;
u.str.a = 12;
u.str.b = 45;
is_eq(u.str.a, 12);
is_eq(u.str.b, 45);
is_true( u.l > 0 );
}
int main()
{
plan(46);
union programming variable;
variable = init_var();
var_by_val(variable);
pass_by_ref(&variable);
union_inside_struct();
union_typedef();
union_inside_struct2();
union_pointers();
union_func_pointers();
union_array();
union_arr_in_str();
union_with_struct();
done_testing();
}
|