pounce-cinterface 0.3.0

C ABI for POUNCE (port of Ipopt's Interfaces/IpStdCInterface.{h,cpp}). Provides CreateIpoptProblem / IpoptSolve / FreeIpoptProblem so existing PyIpopt / cyipopt / JuMP / AMPL clients link without source changes. Feature-complete for the upstream Ipopt C ABI surface.
Documentation
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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
//! Fortran 77 ABI shim — port of `Interfaces/IpStdFInterface.c`.
//!
//! Exposes the gfortran-style `ip<name>_` symbols that the upstream
//! Fortran example programs (`examples/hs071_f`) call. Each function
//! receives all its arguments as pointers (the F77 ABI), translates
//! to the C entry points in [`crate`], and translates back to the
//! Fortran-side `OKRetVal = 0 / NotOKRetVal = 1` convention.
//!
//! Trailing `_` matches gfortran / clang-flang's `F77_FUNC` mangling
//! when no underscores appear in the original name. Names with
//! embedded underscores would need `__`; none of the names exposed
//! here have any.
//!
//! Strings come in as `(char*, len_in_int)` pairs at the *end* of the
//! call (clang-flang / gfortran convention) — Fortran callers must
//! pass the lengths in the order the symbol declares. We accept the
//! length as an extra trailing `c_int` per string argument and copy
//! the buffer with trailing-space stripping ([`f2cstr`]).

use crate::{
    AddIpoptIntOption, AddIpoptNumOption, AddIpoptStrOption, CreateIpoptProblem, Eval_F_CB,
    Eval_G_CB, Eval_Grad_F_CB, Eval_H_CB, Eval_Jac_G_CB, FreeIpoptProblem, Index, Intermediate_CB,
    IpoptProblem, IpoptSolve, Number, SetIntermediateCallback,
};
use std::ffi::{c_char, c_int, c_void};

/// Fortran-side OK status (matches `IpStdFInterface.c::OKRetVal`).
const OK: Index = 0;
/// Fortran-side error status (matches `IpStdFInterface.c::NotOKRetVal`).
const NOT_OK: Index = 1;

/// Holds the Fortran user-data table (`IDAT`, `DDAT` integer/double
/// scratch buffers, plus the user's Fortran callbacks). This is what
/// `IpStdFInterface.c::FUserData` carries; we keep an opaque
/// `Box<FortranUserData>` that the C callbacks dereference.
struct FortranUserData {
    idat: *mut Index,
    ddat: *mut Number,
    eval_f: FEval_F_CB,
    eval_g: Option<FEval_G_CB>,
    eval_grad_f: FEval_Grad_F_CB,
    eval_jac_g: Option<FEval_Jac_G_CB>,
    eval_hess: Option<FEval_Hess_CB>,
    intermediate_cb: Option<FIntermediate_CB>,
    problem: IpoptProblem,
}

// Fortran callback function-pointer types. Each argument is by
// reference, matching `IpStdFInterface.c`.

pub type FEval_F_CB = unsafe extern "C" fn(
    n: *const Index,
    x: *mut Number,
    new_x: *const Index,
    obj_value: *mut Number,
    idat: *mut Index,
    ddat: *mut Number,
    ierr: *mut Index,
);

pub type FEval_G_CB = unsafe extern "C" fn(
    n: *const Index,
    x: *mut Number,
    new_x: *const Index,
    m: *const Index,
    g: *mut Number,
    idat: *mut Index,
    ddat: *mut Number,
    ierr: *mut Index,
);

pub type FEval_Grad_F_CB = unsafe extern "C" fn(
    n: *const Index,
    x: *mut Number,
    new_x: *const Index,
    grad_f: *mut Number,
    idat: *mut Index,
    ddat: *mut Number,
    ierr: *mut Index,
);

pub type FEval_Jac_G_CB = unsafe extern "C" fn(
    task: *const Index,
    n: *const Index,
    x: *mut Number,
    new_x: *const Index,
    m: *const Index,
    nnz_jac: *const Index,
    irow: *mut Index,
    jcol: *mut Index,
    values: *mut Number,
    idat: *mut Index,
    ddat: *mut Number,
    ierr: *mut Index,
);

pub type FEval_Hess_CB = unsafe extern "C" fn(
    task: *const Index,
    n: *const Index,
    x: *mut Number,
    new_x: *const Index,
    obj_factor: *const Number,
    m: *const Index,
    lambda: *mut Number,
    new_lambda: *const Index,
    nnz_hess: *const Index,
    irow: *mut Index,
    jcol: *mut Index,
    values: *mut Number,
    idat: *mut Index,
    ddat: *mut Number,
    ierr: *mut Index,
);

pub type FIntermediate_CB = unsafe extern "C" fn(
    alg_mode: *const Index,
    iter_count: *const Index,
    obj_value: *const Number,
    inf_pr: *const Number,
    inf_du: *const Number,
    mu: *const Number,
    d_norm: *const Number,
    regu_size: *const Number,
    alpha_du: *const Number,
    alpha_pr: *const Number,
    ls_trial: *const Index,
    idat: *mut Index,
    ddat: *mut Number,
    istop: *mut Index,
);

// ------------------------------------------------------------------
// C-side trampolines: these implement the C interface's `Eval_F_CB`
// etc. and unpack the Fortran user_data to call back into Fortran.
// ------------------------------------------------------------------

unsafe extern "C" fn c_eval_f(
    n: Index,
    x: *const Number,
    new_x: c_int,
    obj_value: *mut Number,
    user_data: *mut c_void,
) -> c_int {
    let fud = &mut *(user_data as *mut FortranUserData);
    let mut ierr: Index = 0;
    let n_local = n;
    let new_x_i: Index = new_x as Index;
    (fud.eval_f)(
        &n_local,
        x as *mut Number,
        &new_x_i,
        obj_value,
        fud.idat,
        fud.ddat,
        &mut ierr,
    );
    if ierr == OK {
        1
    } else {
        0
    }
}

unsafe extern "C" fn c_eval_grad_f(
    n: Index,
    x: *const Number,
    new_x: c_int,
    grad_f: *mut Number,
    user_data: *mut c_void,
) -> c_int {
    let fud = &mut *(user_data as *mut FortranUserData);
    let mut ierr: Index = 0;
    let n_local = n;
    let new_x_i: Index = new_x as Index;
    (fud.eval_grad_f)(
        &n_local,
        x as *mut Number,
        &new_x_i,
        grad_f,
        fud.idat,
        fud.ddat,
        &mut ierr,
    );
    if ierr == OK {
        1
    } else {
        0
    }
}

unsafe extern "C" fn c_eval_g(
    n: Index,
    x: *const Number,
    new_x: c_int,
    m: Index,
    g: *mut Number,
    user_data: *mut c_void,
) -> c_int {
    let fud = &mut *(user_data as *mut FortranUserData);
    let Some(cb) = fud.eval_g else {
        return 0;
    };
    let mut ierr: Index = 0;
    let n_local = n;
    let m_local = m;
    let new_x_i: Index = new_x as Index;
    cb(
        &n_local,
        x as *mut Number,
        &new_x_i,
        &m_local,
        g,
        fud.idat,
        fud.ddat,
        &mut ierr,
    );
    if ierr == OK {
        1
    } else {
        0
    }
}

unsafe extern "C" fn c_eval_jac_g(
    n: Index,
    x: *const Number,
    new_x: c_int,
    m: Index,
    nele_jac: Index,
    irow: *mut Index,
    jcol: *mut Index,
    values: *mut Number,
    user_data: *mut c_void,
) -> c_int {
    let fud = &mut *(user_data as *mut FortranUserData);
    let Some(cb) = fud.eval_jac_g else {
        return 0;
    };
    let task: Index = if !irow.is_null() && !jcol.is_null() && values.is_null() {
        0
    } else if irow.is_null() && jcol.is_null() && !values.is_null() {
        1
    } else {
        return 0;
    };
    let mut ierr: Index = 0;
    let n_local = n;
    let m_local = m;
    let nele_local = nele_jac;
    let new_x_i: Index = new_x as Index;
    cb(
        &task,
        &n_local,
        x as *mut Number,
        &new_x_i,
        &m_local,
        &nele_local,
        irow,
        jcol,
        values,
        fud.idat,
        fud.ddat,
        &mut ierr,
    );
    if ierr == OK {
        1
    } else {
        0
    }
}

#[allow(clippy::too_many_arguments)]
unsafe extern "C" fn c_eval_h(
    n: Index,
    x: *const Number,
    new_x: c_int,
    obj_factor: Number,
    m: Index,
    lambda: *const Number,
    new_lambda: c_int,
    nele_hess: Index,
    irow: *mut Index,
    jcol: *mut Index,
    values: *mut Number,
    user_data: *mut c_void,
) -> c_int {
    let fud = &mut *(user_data as *mut FortranUserData);
    let Some(cb) = fud.eval_hess else {
        return 0;
    };
    let task: Index = if !irow.is_null() && !jcol.is_null() && values.is_null() {
        0
    } else if irow.is_null() && jcol.is_null() && !values.is_null() {
        1
    } else {
        return 0;
    };
    let mut ierr: Index = 0;
    let n_local = n;
    let m_local = m;
    let nele_local = nele_hess;
    let new_x_i: Index = new_x as Index;
    let new_lam_i: Index = new_lambda as Index;
    cb(
        &task,
        &n_local,
        x as *mut Number,
        &new_x_i,
        &obj_factor,
        &m_local,
        lambda as *mut Number,
        &new_lam_i,
        &nele_local,
        irow,
        jcol,
        values,
        fud.idat,
        fud.ddat,
        &mut ierr,
    );
    if ierr == OK {
        1
    } else {
        0
    }
}

#[allow(clippy::too_many_arguments)]
unsafe extern "C" fn c_intermediate(
    alg_mod: Index,
    iter_count: Index,
    obj_value: Number,
    inf_pr: Number,
    inf_du: Number,
    mu: Number,
    d_norm: Number,
    regu_size: Number,
    alpha_du: Number,
    alpha_pr: Number,
    ls_trials: Index,
    user_data: *mut c_void,
) -> c_int {
    let fud = &mut *(user_data as *mut FortranUserData);
    let Some(cb) = fud.intermediate_cb else {
        return 1;
    };
    let mut istop: Index = 0;
    cb(
        &alg_mod,
        &iter_count,
        &obj_value,
        &inf_pr,
        &inf_du,
        &mu,
        &d_norm,
        &regu_size,
        &alpha_du,
        &alpha_pr,
        &ls_trials,
        fud.idat,
        fud.ddat,
        &mut istop,
    );
    if istop == OK {
        1
    } else {
        0
    }
}

// ------------------------------------------------------------------
// String marshalling. F77 passes (char*, len) where the buffer is
// blank-padded to `len`; we strip trailing spaces and NUL-terminate.
// ------------------------------------------------------------------

fn f2cstr(buf: *const c_char, slen: c_int) -> Vec<u8> {
    if buf.is_null() || slen <= 0 {
        return vec![0];
    }
    // SAFETY: caller asserts (buf, slen) are a valid Fortran character
    // slice of length `slen` bytes.
    let bytes = unsafe { std::slice::from_raw_parts(buf as *const u8, slen as usize) };
    let mut end = bytes.len();
    while end > 0 && bytes[end - 1] == b' ' {
        end -= 1;
    }
    let mut v = Vec::with_capacity(end + 1);
    v.extend_from_slice(&bytes[..end]);
    v.push(0);
    v
}

// ------------------------------------------------------------------
// Fortran entry points (gfortran trailing-underscore names).
// ------------------------------------------------------------------

/// `ipcreate_(N, X_L, X_U, M, G_L, G_U, NELE_JAC, NELE_HESS, IDX_STY,
///            EVAL_F, EVAL_G, EVAL_GRAD_F, EVAL_JAC_G, EVAL_HESS) -> fptr`
///
/// Returns an opaque handle (a Box<FortranUserData>) cast to a
/// pointer; pass back to [`ipfree_`] / [`ipsolve_`].
///
/// # Safety
/// All pointer arguments must be valid for the lifetime of the
/// returned handle. Bound arrays must hold `*N` / `*M` doubles.
#[allow(clippy::too_many_arguments)]
#[no_mangle]
pub unsafe extern "C" fn ipcreate_(
    n: *const Index,
    x_l: *const Number,
    x_u: *const Number,
    m: *const Index,
    g_l: *const Number,
    g_u: *const Number,
    nele_jac: *const Index,
    nele_hess: *const Index,
    idx_sty: *const Index,
    eval_f: FEval_F_CB,
    eval_g: Option<FEval_G_CB>,
    eval_grad_f: FEval_Grad_F_CB,
    eval_jac_g: Option<FEval_Jac_G_CB>,
    eval_hess: Option<FEval_Hess_CB>,
) -> *mut c_void {
    let problem = CreateIpoptProblem(
        *n,
        x_l,
        x_u,
        *m,
        g_l,
        g_u,
        *nele_jac,
        *nele_hess,
        *idx_sty,
        Some(c_eval_f),
        Some(c_eval_g),
        Some(c_eval_grad_f),
        Some(c_eval_jac_g),
        Some(c_eval_h),
    );
    if problem.is_null() {
        return std::ptr::null_mut();
    }
    let fud = Box::new(FortranUserData {
        idat: std::ptr::null_mut(),
        ddat: std::ptr::null_mut(),
        eval_f,
        eval_g,
        eval_grad_f,
        eval_jac_g,
        eval_hess,
        intermediate_cb: None,
        problem,
    });
    Box::into_raw(fud) as *mut c_void
}

/// `ipfree_(FProblem)` — frees the handle and zeroes the user's
/// pointer slot, mirroring `IpStdFInterface.c::F77_FUNC(ipfree)`.
///
/// # Safety
/// `fproblem` must point to a slot that holds a handle previously
/// returned by [`ipcreate_`], or NULL.
#[no_mangle]
pub unsafe extern "C" fn ipfree_(fproblem: *mut *mut c_void) {
    if fproblem.is_null() || (*fproblem).is_null() {
        return;
    }
    let raw = *fproblem as *mut FortranUserData;
    let fud = Box::from_raw(raw);
    FreeIpoptProblem(fud.problem);
    drop(fud);
    *fproblem = std::ptr::null_mut();
}

/// `ipsolve_(FProblem, X, G, OBJ_VAL, MULT_G, MULT_X_L, MULT_X_U, IDAT, DDAT) -> Index`.
///
/// # Safety
/// All pointer arguments must satisfy the contracts documented on
/// [`crate::IpoptSolve`]. `idat`/`ddat` are scratch arrays passed
/// back to the user's Fortran callbacks.
#[allow(clippy::too_many_arguments)]
#[no_mangle]
pub unsafe extern "C" fn ipsolve_(
    fproblem: *mut *mut c_void,
    x: *mut Number,
    g: *mut Number,
    obj_val: *mut Number,
    mult_g: *mut Number,
    mult_x_l: *mut Number,
    mult_x_u: *mut Number,
    idat: *mut Index,
    ddat: *mut Number,
) -> Index {
    if fproblem.is_null() || (*fproblem).is_null() {
        return -199;
    }
    let fud = &mut *(*fproblem as *mut FortranUserData);
    fud.idat = idat;
    fud.ddat = ddat;
    let fud_ptr = (*fproblem) as *mut c_void;
    IpoptSolve(
        fud.problem,
        x,
        g,
        obj_val,
        mult_g,
        mult_x_l,
        mult_x_u,
        fud_ptr,
    )
}

/// `ipaddstroption_(FProblem, KEYWORD, VALUE, klen, vlen) -> Index`.
/// Returns 0 (`OKRetVal`) on success, 1 on failure.
///
/// # Safety
/// `fproblem` must be valid; the `(KEYWORD, klen)` and `(VALUE, vlen)`
/// pairs must describe valid Fortran character slices.
#[no_mangle]
pub unsafe extern "C" fn ipaddstroption_(
    fproblem: *mut *mut c_void,
    keyword: *const c_char,
    value: *const c_char,
    klen: c_int,
    vlen: c_int,
) -> Index {
    if fproblem.is_null() || (*fproblem).is_null() {
        return NOT_OK;
    }
    let fud = &mut *(*fproblem as *mut FortranUserData);
    let k = f2cstr(keyword, klen);
    let v = f2cstr(value, vlen);
    let ok = AddIpoptStrOption(
        fud.problem,
        k.as_ptr() as *const c_char,
        v.as_ptr() as *const c_char,
    );
    if ok != 0 {
        OK
    } else {
        NOT_OK
    }
}

/// `ipaddnumoption_(FProblem, KEYWORD, VALUE, klen) -> Index`.
///
/// # Safety
/// `fproblem` must be valid; the `(KEYWORD, klen)` pair must describe
/// a valid Fortran character slice.
#[no_mangle]
pub unsafe extern "C" fn ipaddnumoption_(
    fproblem: *mut *mut c_void,
    keyword: *const c_char,
    value: *const Number,
    klen: c_int,
) -> Index {
    if fproblem.is_null() || (*fproblem).is_null() {
        return NOT_OK;
    }
    let fud = &mut *(*fproblem as *mut FortranUserData);
    let k = f2cstr(keyword, klen);
    let ok = AddIpoptNumOption(fud.problem, k.as_ptr() as *const c_char, *value);
    if ok != 0 {
        OK
    } else {
        NOT_OK
    }
}

/// `ipaddintoption_(FProblem, KEYWORD, VALUE, klen) -> Index`.
///
/// # Safety
/// `fproblem` must be valid; `(KEYWORD, klen)` must describe a valid
/// Fortran character slice.
#[no_mangle]
pub unsafe extern "C" fn ipaddintoption_(
    fproblem: *mut *mut c_void,
    keyword: *const c_char,
    value: *const Index,
    klen: c_int,
) -> Index {
    if fproblem.is_null() || (*fproblem).is_null() {
        return NOT_OK;
    }
    let fud = &mut *(*fproblem as *mut FortranUserData);
    let k = f2cstr(keyword, klen);
    let ok = AddIpoptIntOption(fud.problem, k.as_ptr() as *const c_char, *value);
    if ok != 0 {
        OK
    } else {
        NOT_OK
    }
}

/// `ipsetcallback_(FProblem, INTER_CB)` — install a Fortran-side
/// intermediate callback.
///
/// # Safety
/// `fproblem` must be valid; `inter_cb` must be a valid Fortran
/// callback for the lifetime of the problem.
#[no_mangle]
pub unsafe extern "C" fn ipsetcallback_(fproblem: *mut *mut c_void, inter_cb: FIntermediate_CB) {
    if fproblem.is_null() || (*fproblem).is_null() {
        return;
    }
    let fud = &mut *(*fproblem as *mut FortranUserData);
    fud.intermediate_cb = Some(inter_cb);
    let _: Index = SetIntermediateCallback(fud.problem, Some(c_intermediate as Intermediate_CB));
}

/// `ipunsetcallback_(FProblem)` — remove the intermediate callback.
///
/// # Safety
/// `fproblem` must be valid.
#[no_mangle]
pub unsafe extern "C" fn ipunsetcallback_(fproblem: *mut *mut c_void) {
    if fproblem.is_null() || (*fproblem).is_null() {
        return;
    }
    let fud = &mut *(*fproblem as *mut FortranUserData);
    fud.intermediate_cb = None;
    let _: Index = SetIntermediateCallback(fud.problem, None);
}

// Suppress unused import warning when the C ABI types aren't visible
// to dead-code analysis (they're referenced through public type
// aliases above).
const _: Eval_F_CB = c_eval_f;
const _: Eval_Grad_F_CB = c_eval_grad_f;
const _: Eval_G_CB = c_eval_g;
const _: Eval_Jac_G_CB = c_eval_jac_g;
const _: Eval_H_CB = c_eval_h;
const _: Intermediate_CB = c_intermediate;

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn f2cstr_strips_trailing_spaces() {
        let buf = b"hello    ";
        let v = f2cstr(buf.as_ptr() as *const c_char, buf.len() as c_int);
        assert_eq!(&v[..], b"hello\0");
    }

    #[test]
    fn f2cstr_handles_null_buf() {
        let v = f2cstr(std::ptr::null(), 5);
        assert_eq!(&v[..], &[0]);
    }

    #[test]
    fn f2cstr_keeps_embedded_spaces() {
        let buf = b"a b c    ";
        let v = f2cstr(buf.as_ptr() as *const c_char, buf.len() as c_int);
        assert_eq!(&v[..], b"a b c\0");
    }

    /// Drive a 1-D unconstrained quadratic through the Fortran ABI
    /// path: f(x) = (x - 3)^2.
    unsafe extern "C" fn fquad_eval_f(
        _n: *const Index,
        x: *mut Number,
        _new_x: *const Index,
        obj: *mut Number,
        _idat: *mut Index,
        _ddat: *mut Number,
        ierr: *mut Index,
    ) {
        let v = *x.offset(0);
        *obj = (v - 3.0) * (v - 3.0);
        *ierr = OK;
    }
    unsafe extern "C" fn fquad_eval_grad_f(
        _n: *const Index,
        x: *mut Number,
        _new_x: *const Index,
        grad: *mut Number,
        _idat: *mut Index,
        _ddat: *mut Number,
        ierr: *mut Index,
    ) {
        let v = *x.offset(0);
        *grad.offset(0) = 2.0 * (v - 3.0);
        *ierr = OK;
    }
    unsafe extern "C" fn fquad_eval_hess(
        task: *const Index,
        _n: *const Index,
        _x: *mut Number,
        _new_x: *const Index,
        obj_factor: *const Number,
        _m: *const Index,
        _lambda: *mut Number,
        _new_lambda: *const Index,
        _nnz_hess: *const Index,
        irow: *mut Index,
        jcol: *mut Index,
        values: *mut Number,
        _idat: *mut Index,
        _ddat: *mut Number,
        ierr: *mut Index,
    ) {
        if *task == 0 {
            *irow.offset(0) = 0;
            *jcol.offset(0) = 0;
        } else {
            *values.offset(0) = 2.0 * *obj_factor;
        }
        *ierr = OK;
    }

    #[test]
    fn fortran_ipsolve_drives_quadratic() {
        let n: Index = 1;
        let m: Index = 0;
        let nele_jac: Index = 0;
        let nele_hess: Index = 1;
        let idx_sty: Index = 0;
        let xl = [-1.0e20];
        let xu = [1.0e20];

        let mut fp: *mut c_void = unsafe {
            ipcreate_(
                &n,
                xl.as_ptr(),
                xu.as_ptr(),
                &m,
                std::ptr::null(),
                std::ptr::null(),
                &nele_jac,
                &nele_hess,
                &idx_sty,
                fquad_eval_f,
                None,
                fquad_eval_grad_f,
                None,
                Some(fquad_eval_hess),
            )
        };
        assert!(!fp.is_null());

        let mut x = [0.0_f64];
        let mut obj: Number = 0.0;
        let mut idat = [0_i32; 1];
        let mut ddat = [0.0_f64; 1];
        let rc = unsafe {
            ipsolve_(
                &mut fp,
                x.as_mut_ptr(),
                std::ptr::null_mut(),
                &mut obj,
                std::ptr::null_mut(),
                std::ptr::null_mut(),
                std::ptr::null_mut(),
                idat.as_mut_ptr(),
                ddat.as_mut_ptr(),
            )
        };
        assert_eq!(rc, 0); // Solve_Succeeded
        assert!((x[0] - 3.0).abs() < 1e-6, "x[0] = {}", x[0]);
        unsafe { ipfree_(&mut fp) };
        assert!(fp.is_null());
    }
}