| 1 |
#include <assert.h> |
| 2 |
#include <complex.h> |
| 3 |
|
| 4 |
// Use custom types for inspecting parts instead of including tgmath.h |
| 5 |
union cfloat_t { _Complex float z; struct { float re; float im; }; }; |
| 6 |
union cdouble_t { _Complex double z; struct { double re; double im; }; }; |
| 7 |
union creal_t { _Complex long double z; struct { long double re; long double im; }; }; |
| 8 |
|
| 9 |
// extern(C) tests |
| 10 |
extern "C" _Complex float ccomplexf() { return {2.0f, 1.0f}; } |
| 11 |
extern "C" _Complex double ccomplex() { return {2.0, 1.0}; } |
| 12 |
extern "C" _Complex long double ccomplexl() { return {2.0L, 1.0L}; } |
| 13 |
extern "C" void ccomplexf2(_Complex float c) { cfloat_t z = {c}; assert(z.re == 2 && z.im == 1); } |
| 14 |
extern "C" void ccomplex2(_Complex double c) { cdouble_t z = {c}; assert(z.re == 2 && z.im == 1); } |
| 15 |
extern "C" void ccomplexl2(_Complex long double c) { creal_t z = {c}; assert(z.re == 2 && z.im == 1); } |
| 16 |
|
| 17 |
// extern(C++) tests |
| 18 |
_Complex float cpcomplexf() { return {2.0f, 1.0f}; } |
| 19 |
_Complex double cpcomplex() { return {2.0, 1.0}; } |
| 20 |
_Complex long double cpcomplexl() { return {2.0L, 1.0L}; } |
| 21 |
void cpcomplexf(_Complex float c) { cfloat_t z = {c}; assert(z.re == 2 && z.im == 1); } |
| 22 |
void cpcomplex(_Complex double c) { cdouble_t z = {c}; assert(z.re == 2 && z.im == 1); } |
| 23 |
void cpcomplexl(_Complex long double c) { creal_t z = {c}; assert(z.re == 2 && z.im == 1); } |
| 24 |
|
| 25 |
// Struct tests |
| 26 |
struct wrap_complexf { _Complex float c; }; |
| 27 |
struct wrap_complex { _Complex double c; }; |
| 28 |
struct wrap_complexl { _Complex long double c; }; |
| 29 |
|
| 30 |
wrap_complexf wcomplexf() |
| 31 |
{ |
| 32 |
wrap_complexf s; |
| 33 |
s.c = {2.0f, 1.0f}; |
| 34 |
return s; |
| 35 |
} |
| 36 |
|
| 37 |
wrap_complex wcomplex() |
| 38 |
{ |
| 39 |
wrap_complex s; |
| 40 |
s.c = {2.0, 1.0}; |
| 41 |
return s; |
| 42 |
} |
| 43 |
|
| 44 |
wrap_complexl wcomplexl() |
| 45 |
{ |
| 46 |
wrap_complexl s; |
| 47 |
s.c = {2.0L, 1.0L}; |
| 48 |
return s; |
| 49 |
} |
| 50 |
|
| 51 |
void wcomplexf(wrap_complexf s) { cfloat_t z = {s.c}; assert(z.re == 2 && z.im == 1); } |
| 52 |
void wcomplex(wrap_complex s) { cdouble_t z = {s.c}; assert(z.re == 2 && z.im == 1); } |
| 53 |
void wcomplexl(wrap_complexl s) { creal_t z = {s.c}; assert(z.re == 2 && z.im == 1); } |
| 54 |
|
| 55 |
struct soft_complexf { float re; float im; }; |
| 56 |
struct soft_complex { double re; double im; }; |
| 57 |
struct soft_complexl { long double re; long double im; }; |
| 58 |
|
| 59 |
soft_complexf scomplexf() |
| 60 |
{ |
| 61 |
soft_complexf s; |
| 62 |
s.re = 2.0f; s.im = 1.0f; |
| 63 |
return s; |
| 64 |
} |
| 65 |
|
| 66 |
soft_complex scomplex() |
| 67 |
{ |
| 68 |
soft_complex s; |
| 69 |
s.re = 2.0; s.im = 1.0; |
| 70 |
return s; |
| 71 |
} |
| 72 |
|
| 73 |
soft_complexl scomplexl() |
| 74 |
{ |
| 75 |
soft_complexl s; |
| 76 |
s.re = 2.0L; s.im = 1.0L; |
| 77 |
return s; |
| 78 |
} |
| 79 |
|
| 80 |
void scomplexf(soft_complexf s) { assert(s.re == 2 && s.im == 1); } |
| 81 |
void scomplex(soft_complex s) { assert(s.re == 2 && s.im == 1); } |
| 82 |
void scomplexl(soft_complexl s) { assert(s.re == 2 && s.im == 1); } |