source: trunk/Vol/test/osiUnitTest.cpp @ 281

Last change on this file since 281 was 281, checked in by lou, 10 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#include "CoinHelperFunctions.hpp"
23
24#include "OsiVolSolverInterface.hpp"
25
26namespace {
27
28// Display message on stdout and stderr. Flush cout buffer before printing the
29// message, so that output comes out in order in spite of buffered cout.
30
31void testingMessage( const char * const msg )
32{
33  std::cout.flush() ;
34  std::cerr <<msg;
35  //cout <<endl <<"*****************************************"
36  //     <<endl <<msg <<endl;
37}
38
39
40/*
41  Utility routine to process command line parameters. An unrecognised parameter
42  will trigger the help message and a return value of false.
43 
44  This should be replaced with the one of the standard CoinUtils parameter
45  mechanisms.
46*/
47bool processParameters (int argc, const char **argv,
48                        std::map<std::string,std::string> &parms)
49
50{
51  assert(argc >= 1);
52  assert(argv != NULL);
53/*
54  Initialise the parameter keywords.
55*/
56  std::set<std::string> definedKeyWords;
57  definedKeyWords.insert("-cerr2cout");
58  definedKeyWords.insert("-mpsDir");
59  definedKeyWords.insert("-netlibDir");
60  definedKeyWords.insert("-testOsiSolverInterface");
61  definedKeyWords.insert("-nobuf");
62/*
63  Set default values for data directories.
64*/
65  const char dirsep =  CoinFindDirSeparator() ;
66  std::string pathTmp ;
67
68  pathTmp = ".." ;
69  pathTmp += dirsep ;
70  pathTmp += ".." ;
71  pathTmp += dirsep ;
72  pathTmp += "Data" ;
73  pathTmp += dirsep ;
74# ifdef COIN_MSVS
75  // Visual Studio builds are deeper in the directory tree
76  pathTmp = "..\\..\\"+pathTmp ;
77# endif
78
79  parms["-mpsDir"] = pathTmp + "Sample"  ;
80  parms["-netlibDir"] = pathTmp + "Netlib" ;
81
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/*
207  Process command line parameters.
208*/
209  std::map<std::string,std::string> parms ;
210
211  if (processParameters(argc,argv,parms) == false)
212  { return (1) ; }
213
214  std::string mpsDir = parms["-mpsDir"] ;
215  std::string netlibDir = parms["-netlibDir"] ;
216
217try {
218/*
219  Test Osi{Row,Col}Cut routines.
220*/
221  {
222    OsiVolSolverInterface volSi;
223    testingMessage( "Testing OsiRowCut with OsiVolSolverInterface\n" );
224    OsiRowCutUnitTest(&volSi,mpsDir);
225  }
226  {
227    OsiVolSolverInterface volSi;
228    testingMessage( "Testing OsiColCut with OsiVolSolverInterface\n" );
229    OsiColCutUnitTest(&volSi,mpsDir);
230  }
231
232/*
233  Run the OsiXXX class test. It's up to the OsiXxx implementor
234  to decide whether or not to run OsiSolverInterfaceCommonUnitTest. Arguably
235  this should be required.
236*/
237  testingMessage( "Testing OsiVolSolverInterface\n" );
238  OsiVolSolverInterfaceUnitTest(mpsDir,netlibDir);
239
240/*
241  We have run the specialised unit test. Check now to see if we need to
242  run through the Netlib problems.
243*/
244#if 0
245  if (parms.find("-testOsiSolverInterface") != parms.end())
246  {
247    // Create vector of solver interfaces
248    std::vector<OsiSolverInterface*> vecSi(1, new OsiVolSolverInterface);
249
250    testingMessage( "Testing OsiSolverInterface on Netlib problems.\n" );
251    OsiSolverInterfaceMpsUnitTest(vecSi,netlibDir);
252
253    delete vecSi[0];
254  }
255  else {
256    testingMessage( "***Skipped Testing of OsiVolSolverInterface on Netlib problems***\n" );
257    testingMessage( "***use -testOsiSolverInterface to run them.***\n" );
258  }
259#else
260  if (parms.find("-testOsiSolverInterface") != parms.end())
261  {
262    testingMessage( "Vol cannot solve Netlib problems, option -testOsiSolverInterface is ignored.\n" );
263  }
264#endif
265} catch (CoinError& error) {
266  std::cout.flush();
267  std::cerr << "Caught CoinError exception: ";
268  error.print(true);
269  totalErrCnt += 1;
270}
271/*
272  We're done. Report on the results.
273*/
274  if (totalErrCnt)
275  { std::cout.flush() ;
276    std::cerr
277      << "Tests completed with " << totalErrCnt << " errors." << std::endl ; 
278  } else
279  { testingMessage("All tests completed successfully\n") ; }
280  return totalErrCnt;
281}
Note: See TracBrowser for help on using the repository browser.