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
|
/*
** float-union-assignment.cpp
**
** Martin Brain
** martin.brain@cs.ox.ac.uk
** 26/09/12
**
** A reduced test case for problems with returning and assigning fields in a union.
**
*/
#include <assert.h>
#include <stdint.h>
typedef struct _components {
unsigned int negative:1;
unsigned int exponent:8;
unsigned int mantissa:23;
} components;
typedef union _ieee754_float {
components ieee;
float f;
} ieee754_float;
float returnsField (uint32_t index) {
ieee754_float c;
c.ieee.negative = index & 0x1;
c.ieee.exponent = 0;
c.ieee.mantissa = 0;
return c.f;
}
ieee754_float returnsStructure (uint32_t index) {
ieee754_float c;
c.ieee.negative = index & 0x1;
c.ieee.exponent = 0;
c.ieee.mantissa = 0;
return c;
}
void testOne (void) {
ieee754_float f1, f2;
f1 = returnsStructure(0);
f2 = returnsStructure(1);
assert(f1.ieee.negative != f2.ieee.negative);
return;
}
void testTwo (void) {
ieee754_float f1, f2;
f1 = returnsStructure(0);
f2 = returnsStructure(1);
assert(f1.ieee.negative != f2.ieee.negative);
f1.f = returnsField(0);
f2.f = returnsField(1);
assert(f1.ieee.negative != f2.ieee.negative);
return 1;
}
int testThree (void) {
ieee754_float f1, f2;
f1.f = returnsField(0);
f2.f = returnsField(1);
assert(f1.ieee.negative != f2.ieee.negative);
return 1;
}
int main (void) {
testOne();
testTwo();
testThree();
return 0;
}
|