Google OR-Tools v9.15
a fast and portable software suite for combinatorial optimization
Loading...
Searching...
No Matches
solver_port_c_test.c
Go to the documentation of this file.
1// Copyright 2010-2025 Google LLC
2// Licensed under the Apache License, Version 2.0 (the "License");
3// you may not use this file except in compliance with the License.
4// You may obtain a copy of the License at
5//
6// http://www.apache.org/licenses/LICENSE-2.0
7//
8// Unless required by applicable law or agreed to in writing, software
9// distributed under the License is distributed on an "AS IS" BASIS,
10// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11// See the License for the specific language governing permissions and
12// limitations under the License.
13
14// A minimal use of MathOptSolve() from C to ensure the code compiles in C.
15// NOTE: this file is .c, not .cc, so the blaze will test this compiles as C.
16
17#include <stddef.h>
18#include <stdio.h>
19#include <stdlib.h>
20
22
24 // Empty model
25 const size_t model_size = 0;
26 const void* model = NULL;
27 // Solve with glop
28 const int solver_type = 3;
29 void* solve_result = NULL;
30 size_t solve_result_size = 0;
31 char* status_msg = NULL;
32 const int error =
33 MathOptSolve(model, model_size, solver_type, /*interrupter=*/NULL,
34 &solve_result, &solve_result_size, &status_msg);
35 if (error) {
36 printf("error on MathOptSolve, status code: %d, status message: %s", error,
37 status_msg);
38 // If you handle the error instead of crashing, be sure to free status_msg.
39 abort();
40 }
41 MathOptFree(status_msg);
42 MathOptFree(solve_result);
43}
44
46 // Empty model
47 const size_t model_size = 0;
48 const void* model = NULL;
49 // Solve with glop
50 const int solver_type = 3;
51 void* solve_result = NULL;
52 size_t solve_result_size = 0;
53 char* status_msg = NULL;
54 struct MathOptInterrupter* interrupter = MathOptNewInterrupter();
55 MathOptInterrupt(interrupter);
56 const int error =
57 MathOptSolve(model, model_size, solver_type, /*interrupter=*/NULL,
58 &solve_result, &solve_result_size, &status_msg);
59 if (error) {
60 printf("error on MathOptSolve, status code: %d, status message: %s", error,
61 status_msg);
62 // If you handle the error instead of crashing, be sure to free status_msg.
63 abort();
64 }
65 if (MathOptIsInterrupted(interrupter) == 0) {
66 printf("interrupter should be interrupted");
67 abort();
68 }
69 MathOptFreeInterrupter(interrupter);
70 MathOptFree(status_msg);
71 MathOptFree(solve_result);
72}
73
74int main(int argc, char** argv) {
77 return EXIT_SUCCESS;
78}
void MathOptFree(void *ptr)
Definition solver.cc:147
int MathOptIsInterrupted(const MathOptInterrupter *interrupter)
Definition solver.cc:107
void MathOptInterrupt(MathOptInterrupter *interrupter)
Definition solver.cc:103
int MathOptSolve(const void *model, const size_t model_size, const int solver_type, MathOptInterrupter *const interrupter, void **solve_result, size_t *solve_result_size, char **status_msg)
Definition solver.cc:112
MathOptInterrupter * MathOptNewInterrupter()
Definition solver.cc:97
void MathOptFreeInterrupter(MathOptInterrupter *interrupter)
Definition solver.cc:99
int main(int argc, char **argv)
void TestSolveEmptyModel()
void TestInterruptSolveEmptyModel()