aboutsummaryrefslogtreecommitdiff
path: root/lib/mlibc/tests/ansi/creal-cimag.c
diff options
context:
space:
mode:
authorIan Moffett <ian@osmora.org>2024-03-07 17:28:00 -0500
committerIan Moffett <ian@osmora.org>2024-03-07 17:28:32 -0500
commitbd5969fc876a10b18613302db7087ef3c40f18e1 (patch)
tree7c2b8619afe902abf99570df2873fbdf40a4d1a1 /lib/mlibc/tests/ansi/creal-cimag.c
parenta95b38b1b92b172e6cc4e8e56a88a30cc65907b0 (diff)
lib: Add mlibc
Signed-off-by: Ian Moffett <ian@osmora.org>
Diffstat (limited to 'lib/mlibc/tests/ansi/creal-cimag.c')
-rw-r--r--lib/mlibc/tests/ansi/creal-cimag.c37
1 files changed, 37 insertions, 0 deletions
diff --git a/lib/mlibc/tests/ansi/creal-cimag.c b/lib/mlibc/tests/ansi/creal-cimag.c
new file mode 100644
index 0000000..fc70aa3
--- /dev/null
+++ b/lib/mlibc/tests/ansi/creal-cimag.c
@@ -0,0 +1,37 @@
+#include <assert.h>
+#include <math.h>
+#include <complex.h>
+
+// FIXME: We should create a proper floating point facility
+// in order for other functions to be tested properly
+
+#define APPROXIMATELY_EQUAL(calculated, expected) fabs((calculated) - (expected)) <= 0.0000005
+#define APPROXIMATELY_EQUALF(calculated, expected) fabsf((calculated) - (expected)) <= 0.0000005f
+#define APPROXIMATELY_EQUALL(calculated, expected) fabsl((calculated) - (expected)) <= 0.0000005L
+
+#define IS_COMPLEX_NUMBER(Z) \
+ _Generic((Z), \
+ double complex: 1, \
+ float complex: 1, \
+ long double complex: 1, \
+ default: 0 \
+ )
+
+int main() {
+ assert(IS_COMPLEX_NUMBER(CMPLX(5.2, 4.3)));
+ double complex cz = CMPLX(5.2, 4.3);
+ assert(APPROXIMATELY_EQUAL(creal(cz), 5.2));
+ assert(APPROXIMATELY_EQUAL(cimag(cz), 4.3));
+
+ assert(IS_COMPLEX_NUMBER(CMPLXF(1.2f, 2.5f)));
+ float complex czf = CMPLXF(1.2f, 2.5f);
+ assert(APPROXIMATELY_EQUALF(crealf(czf), 1.2f));
+ assert(APPROXIMATELY_EQUALF(cimagf(czf), 2.5f));
+
+ assert(IS_COMPLEX_NUMBER(CMPLXL(0.1L, 123.54L)));
+ long double complex czl = CMPLXL(0.1L, 123.54L);
+ assert(APPROXIMATELY_EQUALL(creall(czl), 0.1L));
+ assert(APPROXIMATELY_EQUALL(cimagl(czl), 123.54L));
+
+ return 0;
+} \ No newline at end of file