Skip to content

Commit 5e02e29

Browse files
committed
Add tests for cinv
1 parent 9c88ae9 commit 5e02e29

File tree

4 files changed

+62
-0
lines changed

4 files changed

+62
-0
lines changed

tests/clib/test_functions.c

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -414,6 +414,11 @@ void mul_complexd(ctypes_complex_double *l, ctypes_complex_double *r, ctypes_com
414414
*out = ctypes_compat_cmul(*l, *r);
415415
}
416416

417+
void inv_complexd(ctypes_complex_double *a, ctypes_complex_double *out)
418+
{
419+
*out = ctypes_compat_cinv(*a);
420+
}
421+
417422
void rotdist_complexd(ctypes_complex_double *c, double *r, double *out) {
418423
ctypes_complex_double x = ctypes_compat_cmul(*c, (ctypes_compat_make_complex(cos(*r), sin(*r))));
419424
*out = fabs(ctypes_compat_creal(x)) + fabs(ctypes_compat_cimag(x));
@@ -429,6 +434,11 @@ void mul_complexld(ctypes_complex_long_double *l, ctypes_complex_long_double *r,
429434
*out = ctypes_compat_cmull(*l, *r);
430435
}
431436

437+
void inv_complexld(ctypes_complex_long_double *a, ctypes_complex_long_double *out)
438+
{
439+
*out = ctypes_compat_cinvl(*a);
440+
}
441+
432442
void rotdist_complexld(ctypes_complex_long_double *c, long double *r, long double *out) {
433443
ctypes_complex_long_double x = ctypes_compat_cmull(*c, (ctypes_compat_make_complexl(cosl(*r), sinl(*r))));
434444
*out = fabsl(ctypes_compat_creall(x)) + fabsl(ctypes_compat_cimagl(x));
@@ -439,6 +449,11 @@ void add_complexf(ctypes_complex_float *l, ctypes_complex_float *r, ctypes_compl
439449
*out = ctypes_compat_caddf(*l, *r);
440450
}
441451

452+
void inv_complexf(ctypes_complex_float *a, ctypes_complex_float *out)
453+
{
454+
*out = ctypes_compat_cinvf(*a);
455+
}
456+
442457
void mul_complexf(ctypes_complex_float *l, ctypes_complex_float *r, ctypes_complex_float *out)
443458
{
444459
*out = ctypes_compat_cmulf(*l, *r);
@@ -459,6 +474,11 @@ ctypes_complex_long_double mul_complexld_val(ctypes_complex_long_double l, ctype
459474
return ctypes_compat_cmull(l, r);
460475
}
461476

477+
ctypes_complex_long_double inv_complexld_val(ctypes_complex_long_double a)
478+
{
479+
return ctypes_compat_cinvl(a);
480+
}
481+
462482
long double rotdist_complexld_val(ctypes_complex_long_double c, long double r) {
463483
ctypes_complex_long_double x = ctypes_compat_cmull(c, (ctypes_compat_make_complexl(cosl(r), sinl(r))));
464484
return fabsl(ctypes_compat_creall(x)) + fabsl(ctypes_compat_cimagl(x));
@@ -474,6 +494,11 @@ ctypes_complex_double mul_complexd_val(ctypes_complex_double l, ctypes_complex_d
474494
return ctypes_compat_cmul(l, r);
475495
}
476496

497+
ctypes_complex_double inv_complexd_val(ctypes_complex_double a)
498+
{
499+
return ctypes_compat_cinv(a);
500+
}
501+
477502
double rotdist_complexd_val(ctypes_complex_double c, double r) {
478503
ctypes_complex_double x = ctypes_compat_cmul(c, (ctypes_compat_make_complex(cos(r), sin(r))));
479504
return fabs(ctypes_compat_creal(x)) + fabs(ctypes_compat_cimag(x));
@@ -489,6 +514,11 @@ ctypes_complex_float mul_complexf_val(ctypes_complex_float l, ctypes_complex_flo
489514
return ctypes_compat_cmulf(l, r);
490515
}
491516

517+
ctypes_complex_float inv_complexf_val(ctypes_complex_float a)
518+
{
519+
return ctypes_compat_cinvf(a);
520+
}
521+
492522
float rotdist_complexf_val(ctypes_complex_float c, float r) {
493523
ctypes_complex_float x = ctypes_compat_cmulf(c, (ctypes_compat_make_complexf(cosf(r), sinf(r))));
494524
return fabsf(ctypes_compat_crealf(x)) + fabsf(ctypes_compat_cimagf(x));

tests/clib/test_functions.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,21 +162,27 @@ DLL_EXTERN double retrieve_DBL_MIN(void);
162162
DLL_EXTERN double retrieve_DBL_MAX(void);
163163
DLL_EXTERN void add_complexd(ctypes_complex_double *, ctypes_complex_double *, ctypes_complex_double *);
164164
DLL_EXTERN void mul_complexd(ctypes_complex_double *, ctypes_complex_double *, ctypes_complex_double *);
165+
DLL_EXTERN void inv_complexd(ctypes_complex_double *, ctypes_complex_double *);
165166
DLL_EXTERN void rotdist_complexd(ctypes_complex_double *, double *, double *);
166167
DLL_EXTERN void add_complexld(ctypes_complex_long_double *, ctypes_complex_long_double *, ctypes_complex_long_double *);
167168
DLL_EXTERN void mul_complexld(ctypes_complex_long_double *, ctypes_complex_long_double *, ctypes_complex_long_double *);
169+
DLL_EXTERN void inv_complexld(ctypes_complex_long_double *, ctypes_complex_long_double *);
168170
DLL_EXTERN void rotdist_complexld(ctypes_complex_long_double *, long double *, long double *);
169171
DLL_EXTERN void add_complexf(ctypes_complex_float *, ctypes_complex_float *, ctypes_complex_float *);
170172
DLL_EXTERN void mul_complexf(ctypes_complex_float *, ctypes_complex_float *, ctypes_complex_float *);
173+
DLL_EXTERN void inv_complexf(ctypes_complex_float *, ctypes_complex_float *);
171174
DLL_EXTERN void rotdist_complexf(ctypes_complex_float *, float *, float *);
172175
DLL_EXTERN ctypes_complex_double add_complexd_val(ctypes_complex_double, ctypes_complex_double);
173176
DLL_EXTERN ctypes_complex_double mul_complexd_val(ctypes_complex_double, ctypes_complex_double);
177+
DLL_EXTERN ctypes_complex_double inv_complexd_val(ctypes_complex_double);
174178
DLL_EXTERN double rotdist_complexd_val(ctypes_complex_double, double);
175179
DLL_EXTERN ctypes_complex_long_double add_complexld_val(ctypes_complex_long_double, ctypes_complex_long_double);
176180
DLL_EXTERN ctypes_complex_long_double mul_complexld_val(ctypes_complex_long_double, ctypes_complex_long_double);
181+
DLL_EXTERN ctypes_complex_long_double inv_complexld_val(ctypes_complex_long_double);
177182
DLL_EXTERN long double rotdist_complexld_val(ctypes_complex_long_double, long double);
178183
DLL_EXTERN ctypes_complex_float add_complexf_val(ctypes_complex_float, ctypes_complex_float);
179184
DLL_EXTERN ctypes_complex_float mul_complexf_val(ctypes_complex_float, ctypes_complex_float);
185+
DLL_EXTERN ctypes_complex_float inv_complexf_val(ctypes_complex_float);
180186
DLL_EXTERN float rotdist_complexf_val(ctypes_complex_float, float);
181187
DLL_EXTERN void store_callback(int (*callback)(int));
182188
DLL_EXTERN int invoke_stored_callback(int);

tests/test-complex/stubs/functions.ml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,21 @@ struct
1717
F.(foreign name
1818
(ptr typ1 @-> ptr typ2 @-> ptr typ2 @-> returning void))
1919
let bind typ name = bind' typ typ name
20+
let bind1 typ1 name =
21+
F.(foreign name
22+
(ptr typ1 @-> ptr typ1 @-> returning void))
2023

2124
let add_complexd = bind complex64 "add_complexd"
2225
let mul_complexd = bind complex64 "mul_complexd"
26+
let inv_complexd = bind1 complex64 "inv_complexd"
2327
let rotdist_complexd = bind' complex64 double "rotdist_complexd"
2428
let add_complexld = bind complexld "add_complexld"
2529
let mul_complexld = bind complexld "mul_complexld"
30+
let inv_complexld = bind1 complexld "inv_complexld"
2631
let rotdist_complexld = bind' complexld ldouble "rotdist_complexld"
2732
let add_complexf = bind complex32 "add_complexf"
2833
let mul_complexf = bind complex32 "mul_complexf"
34+
let inv_complexf = bind1 complex32 "inv_complexf"
2935
let rotdist_complexf = bind' complex32 float "rotdist_complexf"
3036
end
3137

@@ -36,15 +42,21 @@ struct
3642
let bind' typ1 typ2 name =
3743
F.(foreign name (typ1 @-> typ2 @-> returning typ2))
3844
let bind typ name = bind' typ typ name
45+
let bind1' typ1 name =
46+
F.(foreign name (typ1 @-> returning typ1))
47+
let bind1 typ name = bind1' typ name
3948

4049
let add_complexd_val = bind complex64 "add_complexd_val"
4150
let mul_complexd_val = bind complex64 "mul_complexd_val"
51+
let inv_complexd_val = bind1 complex64 "inv_complexd_val"
4252
let rotdist_complexd_val = bind' complex64 double "rotdist_complexd_val"
4353
let add_complexld_val = bind complexld "add_complexld_val"
4454
let mul_complexld_val = bind complexld "mul_complexld_val"
55+
let inv_complexld_val = bind1 complexld "inv_complexld_val"
4556
let rotdist_complexld_val = bind' complexld ldouble "rotdist_complexld_val"
4657
let add_complexf_val = bind complex32 "add_complexf_val"
4758
let mul_complexf_val = bind complex32 "mul_complexf_val"
59+
let inv_complexf_val = bind1 complex32 "inv_complexf_val"
4860
let rotdist_complexf_val = bind' complex32 float "rotdist_complexf_val"
4961
end
5062

tests/test-complex/test_complex.ml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,15 +31,23 @@ struct
3131
!@rv
3232
in
3333
let wrap typ f l r = wrap' typ typ f l r in
34+
let wrap1 typ1 f a =
35+
let rv = allocate_n ~count:1 typ1 in
36+
f (allocate typ1 a) rv;
37+
!@rv
38+
in
3439

3540
let addz64 = wrap complex64 add_complexd
3641
and mulz64 = wrap complex64 mul_complexd
42+
and invz64 = wrap1 complex64 inv_complexd
3743
and rotz64 = wrap' complex64 double rotdist_complexd
3844
and addz32 = wrap complex32 add_complexf
3945
and mulz32 = wrap complex32 mul_complexf
46+
and invz32 = wrap1 complex32 inv_complexf
4047
and rotz32 = wrap' complex32 float rotdist_complexf
4148
and addzld = wrap complexld add_complexld
4249
and mulzld = wrap complexld mul_complexld
50+
and invzld = wrap1 complexld inv_complexld
4351
and rotzld = wrap' complexld ldouble rotdist_complexld
4452
in
4553

@@ -58,9 +66,11 @@ struct
5866

5967
assert_equal ~cmp:complex64_eq (Complex.add l r) (addz64 l r);
6068
assert_equal ~cmp:complex64_eq (Complex.mul l r) (mulz64 l r);
69+
assert_equal ~cmp:complex64_eq (Complex.inv l) (invz64 l);
6170

6271
assert_equal ~cmp:complex32_eq (Complex.add l r) (addz32 l r);
6372
assert_equal ~cmp:complex32_eq (Complex.mul l r) (mulz32 l r);
73+
assert_equal ~cmp:complex32_eq (Complex.inv l) (invz32 l);
6474

6575
(* test long double complex *)
6676
let re x = LDouble.(to_float (ComplexL.re x)) in
@@ -71,6 +81,7 @@ struct
7181
let l', r' = to_complexld l, to_complexld r in
7282
assert_equal ~cmp:complex64_eq (Complex.add l r) (of_complexld @@ addzld l' r');
7383
assert_equal ~cmp:complex64_eq (Complex.mul l r) (of_complexld @@ mulzld l' r');
84+
assert_equal ~cmp:complex64_eq (Complex.inv l) (of_complexld @@ invzld l');
7485

7586
(* The rotdist test is designed to check passing and returning long doubles.
7687
The function rotates a complex number by the given angle in radians,
@@ -125,9 +136,11 @@ struct
125136

126137
assert_equal ~cmp:complex64_eq (Complex.add l r) (add_complexd_val l r);
127138
assert_equal ~cmp:complex64_eq (Complex.mul l r) (mul_complexd_val l r);
139+
assert_equal ~cmp:complex64_eq (Complex.inv l) (inv_complexd_val l);
128140

129141
assert_equal ~cmp:complex32_eq (Complex.add l r) (add_complexf_val l r);
130142
assert_equal ~cmp:complex32_eq (Complex.mul l r) (mul_complexf_val l r);
143+
assert_equal ~cmp:complex32_eq (Complex.inv l) (inv_complexf_val l);
131144

132145
let zinf = { re = 0.; im = infinity } in
133146
let res = add_complexd_val zinf zinf in
@@ -147,6 +160,7 @@ struct
147160
let l', r' = to_complexld l, to_complexld r in
148161
assert_equal ~cmp:complex64_eq (Complex.add l r) (of_complexld @@ add_complexld_val l' r');
149162
assert_equal ~cmp:complex64_eq (Complex.mul l r) (of_complexld @@ mul_complexld_val l' r');
163+
assert_equal ~cmp:complex64_eq (Complex.inv l) (of_complexld @@ inv_complexld_val l');
150164

151165
assert_equal 0. (re (to_complexld zinf));
152166

0 commit comments

Comments
 (0)