1 /*
2  * MOAB, a Mesh-Oriented datABase, is a software component for creating,
3  * storing and accessing finite element mesh data.
4  *
5  * Copyright 2004 Sandia Corporation.  Under the terms of Contract
6  * DE-AC04-94AL85000 with Sandia Corporation, the U.S. Government
7  * retains certain rights in this software.
8  *
9  * This library is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Lesser General Public
11  * License as published by the Free Software Foundation; either
12  * version 2.1 of the License, or (at your option) any later version.
13  *
14  */
15 
16 /**\file FileOptions.cpp
17  *\author Jason Kraftcheck (kraftche@cae.wisc.edu)
18  *\date 2007-08-21
19  */
20 
21 #include "moab/FileOptions.hpp"
22 
23 #include <ctype.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <algorithm>
27 #include <iostream>
28 
29 using namespace moab;
30 
31 #define CHECK(A) \
32   if (MB_SUCCESS != (A)) { \
33     std::cerr << "Failure at line " << __LINE__ << ": error code " << (A) << std::endl; \
34     return 1; \
35   }
36 
37 #define EQUAL(A,B) \
38   if (A != B) { \
39     std::cerr << "Failure at line " << __LINE__ << ": expected " << (B) << " but got " << (A) << std::endl; \
40     return 2; \
41   }
42 
main()43 int main()
44 {
45   FileOptions tool( "INT1=1;NUL1;STR1=ABC;DBL1=1.0;dbl2=2.0;DBL3=3.0;INT2=2;nul2;NUL3;INT3=3;str2=once upon a time;str3==fubar=;;INTS=1-3,5,6;DBLS=1.0,2.0, 3.0;STRS=var1, var2_var2;STRS2=" );
46 
47   std::string s;
48   int i;
49   double d;
50   ErrorCode rval;
51 
52     // test basic get_option method without deleting entry
53   rval = tool.get_option( "STR1", s );
54   CHECK(rval);
55   EQUAL( s, "ABC" );
56 
57     // test basic get_option method again, this time deleting the entry
58   rval = tool.get_option( "STR1", s );
59   CHECK(rval);
60   EQUAL( s, "ABC" );
61 
62     // test basig get_option method with a null option
63   rval = tool.get_option( "NUL2", s );
64   CHECK( rval );
65   EQUAL( s.empty(), true );
66 
67 
68     // test null option
69   rval = tool.get_null_option( "nul1" );
70   CHECK( rval );
71 
72     // try null option method on non-null value
73   rval = tool.get_null_option( "INT1" ) ;
74   EQUAL( rval, MB_TYPE_OUT_OF_RANGE) ;
75 
76 
77     // test integer option
78   rval = tool.get_int_option( "int1", i );
79   CHECK( rval );
80   EQUAL( i, 1 );
81 
82   rval = tool.get_int_option( "int2", i );
83   CHECK( rval );
84   EQUAL( i, 2 );
85 
86     // test integer option on non-integer value
87   rval = tool.get_int_option( "dbl2", i );
88   EQUAL( rval, MB_TYPE_OUT_OF_RANGE );
89 
90     // test integer option on null value
91   rval = tool.get_int_option( "NUL3", i);
92   EQUAL( rval, MB_TYPE_OUT_OF_RANGE );
93 
94     // test double option
95   rval = tool.get_real_option( "dbl1", d );
96   CHECK( rval );
97   EQUAL( d, 1.0 );
98 
99   rval = tool.get_real_option( "dbl2", d );
100   CHECK( rval );
101   EQUAL( d, 2.0 );
102 
103   rval = tool.get_real_option( "int3", d );
104   CHECK( rval );
105   EQUAL( d, 3.0 );
106 
107     // test real option on non-real value
108   rval = tool.get_real_option( "str2", d );
109   EQUAL( rval, MB_TYPE_OUT_OF_RANGE );
110 
111 
112     // test real option on null value
113   rval = tool.get_real_option( "NUL3", d );
114   EQUAL( rval, MB_TYPE_OUT_OF_RANGE );
115 
116     // test get a simple string option
117   rval = tool.get_str_option( "DBL3", s );
118   CHECK( rval );
119   EQUAL( s, "3.0" );
120 
121     // test get a string with spaces
122   rval = tool.get_str_option("STR2", s );
123   CHECK( rval );
124   EQUAL( s, "once upon a time" );
125 
126     // try to get a string value for a null option
127   rval = tool.get_str_option( "nul3", s );
128   EQUAL( rval, MB_TYPE_OUT_OF_RANGE );
129 
130     // We haven't looked at all of the options yet
131   EQUAL( false, tool.all_seen() );
132   rval = tool.get_unseen_option( s );
133   CHECK( rval );
134   EQUAL( s, "str3" );
135 
136     // test options using generic get_option method
137 
138   rval = tool.get_option( "NUL3", s );
139   CHECK( rval );
140   EQUAL( s.empty(), true );
141 
142   rval = tool.get_option( "STR3", s );
143   CHECK( rval );
144   EQUAL( s, "=fubar=" );
145 
146     // test size of options string
147   unsigned l = tool.size();
148   EQUAL( l, 16u );
149 
150     // test ints option
151   std::vector<int> ivals;
152   rval = tool.get_ints_option("INTS", ivals);
153   CHECK( rval );
154   EQUAL(5, ivals.size());
155   EQUAL(1, ivals[0]);
156   EQUAL(2, ivals[1]);
157   EQUAL(3, ivals[2]);
158   EQUAL(5, ivals[3]);
159   EQUAL(6, ivals[4]);
160 
161     // test dbls option
162   std::vector<double> vals;
163   rval = tool.get_reals_option("DBLS", vals);
164   CHECK( rval );
165   EQUAL(3, vals.size());
166   EQUAL(1.0, vals[0]);
167   EQUAL(2.0, vals[1]);
168   EQUAL(3.0, vals[2]);
169 
170     // test strs option
171   std::vector<std::string> svals;
172   rval = tool.get_strs_option("STRS", svals);
173   CHECK( rval );
174   EQUAL(2, svals.size());
175   EQUAL("var1", svals[0]);
176   EQUAL("var2_var2", svals[1]);
177 
178   svals.clear();
179   rval = tool.get_strs_option("STRS2", svals);
180   EQUAL( MB_TYPE_OUT_OF_RANGE, rval );
181 
182     // We requested every option
183   EQUAL( true, tool.all_seen() );
184   rval = tool.get_unseen_option( s );
185   EQUAL( MB_ENTITY_NOT_FOUND, rval );
186 
187     // test alternate separator
188 
189   FileOptions tool2( ";+OPT1=ABC+OPT2=" );
190   l = tool2.size();
191   EQUAL( l, 2 );
192 
193     // We haven't looked at all of the options yet
194   EQUAL( false, tool2.all_seen() );
195   rval = tool2.get_unseen_option( s );
196   CHECK( rval );
197   EQUAL( s, "OPT1" );
198 
199   rval = tool2.get_option( "opt1", s );
200   CHECK( rval );
201   EQUAL( s, "ABC" );
202 
203   rval = tool2.get_option( "opt2", s );
204   CHECK( rval );
205   bool e = s.empty();
206   EQUAL( e, true );
207 
208   l = tool2.size();
209   EQUAL( l, 2 );
210 
211     // We requested every option
212   EQUAL( true, tool2.all_seen() );
213   rval = tool2.get_unseen_option( s );
214   EQUAL( MB_ENTITY_NOT_FOUND, rval );
215 
216 
217     // test empty options string
218 
219   FileOptions tool3( ";;;;" );
220   e = tool3.empty();
221   EQUAL( e, true );
222   l = tool3.size();
223   EQUAL( l, 0 );
224   EQUAL( true, tool3.all_seen() );
225 
226   FileOptions tool4(NULL);
227   e = tool4.empty();
228   EQUAL( e, true );
229   l = tool4.size();
230   EQUAL( l, 0 );
231   EQUAL( true, tool4.all_seen() );
232 
233   FileOptions tool5(";+");
234   e = tool5.empty();
235   EQUAL( e, true );
236   l = tool5.size();
237   EQUAL( l, 0 );
238   EQUAL( true, tool5.all_seen() );
239 
240     // test copy constructor
241 
242   FileOptions tool6( tool2 );
243 
244   rval = tool6.get_option( "opt1", s );
245   CHECK( rval );
246   EQUAL( s, "ABC" );
247 
248   rval = tool6.get_option( "opt2", s );
249   CHECK( rval );
250   e = s.empty();
251   EQUAL( e, true );
252 
253   l = tool6.size();
254   EQUAL( l, 2 );
255 
256   FileOptions tool7( tool5 );
257   e = tool7.empty();
258   EQUAL( e, true );
259   l = tool7.size();
260   EQUAL( l, 0 );
261 
262     // test assignment operator
263 
264   FileOptions tool8( tool2 );
265   tool8 = tool;
266   EQUAL( tool8.size(), tool.size() );
267 
268   return 0;
269 }
270 
271