source: trunk/Cbc/test/osiUnitTest.cpp @ 1574

Last change on this file since 1574 was 1574, checked in by lou, 8 years ago

Change to EPL license notice.

File size: 8.0 KB
Line 
1// $Id$
2// Copyright (C) 2000, International Business Machines
3// Corporation and others.  All Rights Reserved.
4// This code is licensed under the terms of the Eclipse Public License (EPL).
5
6#include "CoinPragma.hpp"
7
8#include "OsiConfig.h"
9
10#ifdef NDEBUG
11#undef NDEBUG
12#endif
13
14#include <cassert>
15#include <cstdio>
16#include <iostream>
17#include <map>
18
19#include "OsiUnitTests.hpp"
20
21#include "CoinError.hpp"
22
23#include "OsiCbcSolverInterface.hpp"
24
25namespace {
26
27// Display message on stdout and stderr. Flush cout buffer before printing the
28// message, so that output comes out in order in spite of buffered cout.
29
30void testingMessage( const char * const msg )
31{
32  std::cout.flush() ;
33  std::cerr <<msg;
34  //cout <<endl <<"*****************************************"
35  //     <<endl <<msg <<endl;
36}
37
38
39/*
40  Utility routine to process command line parameters. An unrecognised parameter
41  will trigger the help message and a return value of false.
42 
43  This should be replaced with the one of the standard CoinUtils parameter
44  mechanisms.
45*/
46bool processParameters (int argc, const char **argv,
47                        std::map<std::string,std::string> &parms)
48
49{
50  assert(argc >= 1);
51  assert(argv != NULL);
52/*
53  Initialise the parameter keywords.
54*/
55  std::set<std::string> definedKeyWords;
56  definedKeyWords.insert("-cerr2cout");
57  definedKeyWords.insert("-mpsDir");
58  definedKeyWords.insert("-netlibDir");
59  definedKeyWords.insert("-testOsiSolverInterface");
60  definedKeyWords.insert("-nobuf");
61/*
62  Set default values for data directories.
63*/
64  std::string dirsep(1,CoinFindDirSeparator()) ;
65  std::string upOne = ".."+dirsep ;
66  std::string pathTmp ;
67/*
68  Establish likely defaults for the Sample and Miplib directories.
69*/
70# ifdef SAMPLEDIR
71    pathTmp = SAMPLEDIR ;
72# else
73    pathTmp = upOne+upOne+"Data"+dirsep+"Sample" ;
74# endif
75  parms["-mpsDir"] = pathTmp  ;
76# ifdef NETLIBDIR
77    pathTmp = NETLIBDIR ;
78# else
79    pathTmp = upOne+upOne+"Data"+dirsep+"Netlib" ;
80# endif
81  parms["-netlibDir"] = pathTmp ;
82/*
83  Read the command line parameters and fill a map of parameter keys and
84  associated data. The parser allows for parameters which are only a keyword,
85  or parameters of the form keyword=value (no spaces).
86*/
87  for (int i = 1 ; i < argc ; i++)
88  { std::string parm(argv[i]) ;
89    std::string key,value ;
90    std::string::size_type eqPos = parm.find('=');
91
92    if (eqPos == std::string::npos)
93    { key = parm ; }
94    else
95    { key = parm.substr(0,eqPos) ;
96      value = parm.substr(eqPos+1) ; }
97/*
98  Is the specifed key valid?
99*/
100    if (definedKeyWords.find(key) == definedKeyWords.end())
101    { std::cerr << "Undefined parameter \"" << key << "\"." << std::endl ;
102      std::cerr
103        << "Usage: " << argv[0]
104        << " [-nobuf] [-mpsDir=V1] [-netlibDir=V2] "
105        << "[-testOsiSolverInterface] " << std::endl ;
106      std::cerr << "  where:" << std::endl ;
107      std::cerr
108        << "    "
109        << "-cerr2cout: redirect cerr to cout; sometimes useful." << std::endl
110        << "\t" << "to synchronise cout & cerr." << std::endl ;
111      std::cerr
112        << "    "
113        << "-mpsDir: directory containing mps test files." << std::endl
114        << "\t" << "Default value V1=\"../../Data/Sample\"" << std::endl ;
115      std::cerr
116        << "    "
117        << "-netlibDir: directory containing netlib files." << std::endl
118        << "\t" << "Default value V2=\"../../Data/Netlib\"" << std::endl ;
119      std::cerr
120        << "    "
121        << "-testOsiSolverInterface: "
122        << "run each OSI on the netlib problem set." << std::endl
123        << "\t"
124        << "Default is to not run the netlib problem set." << std::endl ;
125      std::cerr
126        << "    "
127        << "-nobuf: use unbuffered output." << std::endl
128        << "\t" << "Default is buffered output." << std::endl ;
129     
130      return (false) ; }
131/*
132  Valid keyword; stash the value for later reference.
133*/
134    parms[key]=value ; }
135/*
136  Tack the directory separator onto the data directories so we don't have to
137  worry about it later.
138*/
139  parms["-mpsDir"] += dirsep ;
140  parms["-netlibDir"] += dirsep ;
141/*
142  Did the user request unbuffered i/o? It seems we need to go after this
143  through stdio --- using pubsetbuf(0,0) on the C++ streams has no
144  discernible affect. Nor, for that matter, did setting the unitbuf flag on
145  the streams. Why? At a guess, sync_with_stdio connects the streams to the
146  stdio buffers, and the C++ side isn't programmed to change them?
147*/
148  if (parms.find("-nobuf") != parms.end())
149  { // std::streambuf *coutBuf, *cerrBuf ;
150    // coutBuf = std::cout.rdbuf() ;
151    // coutBuf->pubsetbuf(0,0) ;
152    // cerrBuf = std::cerr.rdbuf() ;
153    // cerrBuf->pubsetbuf(0,0) ;
154    setbuf(stderr,0) ;
155    setbuf(stdout,0) ; }
156/*
157  Did the user request a redirect for cerr? This must occur before any i/o is
158  performed.
159*/
160  if (parms.find("-cerr2cout") != parms.end())
161  { std::cerr.rdbuf(std::cout.rdbuf()) ; }
162
163  return (true) ; }
164
165
166}       // end file-local namespace
167
168
169
170//----------------------------------------------------------------
171// unitTest [-nobuf] [-mpsDir=V1] [-netlibDir=V2] [-testOsiSolverInterface]
172//
173// where:
174//   -nobuf: remove buffering on cout (stdout); useful to keep cout and cerr
175//       messages synchronised when redirecting output to a file or pipe.
176//   -mpsDir: directory containing mps test files
177//       Default value V1="../../Data/Sample"   
178//   -netlibDir: directory containing netlib files
179//       Default value V2="../../Data/Netlib"
180//   -testOsiSolverInterface
181//       If specified, then OsiSolveInterface::unitTest
182//       is skipped over and not run.
183//
184// All parameters are optional.
185//----------------------------------------------------------------
186
187int main (int argc, const char *argv[])
188
189{ int totalErrCnt = 0;
190
191/*
192  Start off with various bits of initialisation that don't really belong
193  anywhere else.
194
195  First off, synchronise C++ stream i/o with C stdio. This makes debugging
196  output a bit more comprehensible. It still suffers from interleave of cout
197  (stdout) and cerr (stderr), but -nobuf deals with that.
198*/
199  std::ios::sync_with_stdio() ;
200/*
201  Suppress an popup window that Windows shows in response to a crash. See
202  note at head of file.
203*/
204  WindowsErrorPopupBlocker();
205/*
206  Process command line parameters.
207*/
208  std::map<std::string,std::string> parms ;
209
210  if (processParameters(argc,argv,parms) == false)
211  { return (1) ; }
212
213  std::string mpsDir = parms["-mpsDir"] ;
214  std::string netlibDir = parms["-netlibDir"] ;
215
216try {
217/*
218  Test Osi{Row,Col}Cut routines.
219*/
220  {
221    OsiCbcSolverInterface cbcSi;
222    testingMessage( "Testing OsiRowCut with OsiCbcSolverInterface\n" );
223    OsiRowCutUnitTest(&cbcSi,mpsDir);
224  }
225  {
226    OsiCbcSolverInterface cbcSi;
227    testingMessage( "Testing OsiColCut with OsiCbcSolverInterface\n" );
228    OsiColCutUnitTest(&cbcSi,mpsDir);
229  }
230  {
231    OsiCbcSolverInterface cbcSi;
232    testingMessage( "Testing OsiRowCutDebugger with OsiCbcSolverInterface\n" );
233    OsiRowCutDebuggerUnitTest(&cbcSi,mpsDir);
234  }
235
236/*
237  Run the OsiXXX class test. It's up to the OsiCbc implementor
238  to decide whether or not to run OsiSolverInterfaceCommonUnitTest. Arguably
239  this should be required.
240*/
241  testingMessage( "Testing OsiCbcSolverInterface\n" );
242  OsiCbcSolverInterfaceUnitTest(mpsDir,netlibDir);
243
244/*
245  We have run the specialised unit test. Check now to see if we need to
246  run through the Netlib problems.
247*/
248  if (parms.find("-testOsiSolverInterface") != parms.end())
249  {
250    // Create vector of solver interfaces
251    std::vector<OsiSolverInterface*> vecSi(1, new OsiCbcSolverInterface);
252
253    testingMessage( "Testing OsiSolverInterface on Netlib problems.\n" );
254    OsiSolverInterfaceMpsUnitTest(vecSi,netlibDir);
255
256    delete vecSi[0];
257  }
258  else {
259    testingMessage( "***Skipped Testing of OsiCbcSolverInterface on Netlib problems***\n" );
260    testingMessage( "***use -testOsiSolverInterface to run them.***\n" );
261  }
262} catch (CoinError& error) {
263  std::cout.flush();
264  std::cerr << "Caught CoinError exception: ";
265  error.print(true);
266  totalErrCnt += 1;
267}
268/*
269  We're done. Report on the results.
270*/
271  if (totalErrCnt)
272  { std::cout.flush() ;
273    std::cerr
274      << "Tests completed with " << totalErrCnt << " errors." << std::endl ; 
275  } else
276  { testingMessage("All tests completed successfully\n") ; }
277  return totalErrCnt;
278}
Note: See TracBrowser for help on using the repository browser.