Loading...
Searching...
No Matches
TestGAMSWorkspace.cs
1using System;
2using System.IO;
3using System.Reflection;
4using System.Text.RegularExpressions;
5using System.Threading;
6using GAMS;
7using NLog;
8using NUnit.Framework;
9
10namespace GamsApiTests
11{
12 [TestFixture]
13 public class TestGAMSWorkspace
14 {
15 private static readonly String PROPERTIES_FILE = "test.properties";
16 private static String gamsDefaultPath = null;
17 private static String APIVersion;
18 private static int majorVersion;
19 private static int minorVersion;
20 private static int goldVersion;
21 private static String userDefinedSystemDir = null;
22 private static String tempPath = Path.GetTempPath();
23 private static String workingDir;
24 private String prefix = "_gams_net_gcp";
25 private GAMSWorkspaceInfo wsInfo = null;
26 private GAMSWorkspace ws = null;
27 private GAMSDatabase db = null;
28
29 static Logger logger = LogManager.GetCurrentClassLogger();
30
31 [OneTimeSetUp]
32 public static void OneTimeSetup()
33 {
34 logger.Debug("TestGAMSWorkspace | OneTimeSetup");
35 workingDir = HouseKeeper.gamsAbsoluteWorkingDir;
36 HouseKeeper.initializeTestFrom(PROPERTIES_FILE, "TestGAMSWorkspace");
38 if (gamsDefaultPath == null)
39 gamsDefaultPath = HouseKeeper.gamsSystemDir;
40 userDefinedSystemDir = InitializeGAMSDirectoryForUserDefinedConfiguration();
41 APIVersion = HouseKeeper.gamsVersion;
42 majorVersion = Int16.Parse(Regex.Split(APIVersion, "\\.")[0]);
43 minorVersion = Int16.Parse(Regex.Split(APIVersion, "\\.")[1]);
44 goldVersion = Int16.Parse(Regex.Split(APIVersion, "\\.")[2]);
45 logger.Debug("TestGAMSWorkspace | oneTimeSetup");
46 }
47
48 [OneTimeTearDown]
49 public static void OneTimeTearDown()
50 {
51 logger.Debug("entering TestGAMSWorkspace | OneTimeTearDown");
52 Thread.Sleep(1000);
53 HouseKeeper.rcleanup(HouseKeeper.gamsWorkingDir);
54 logger.Debug("exiting TestGAMSWorkspace | OneTimeTearDown");
55 }
56
61 private static string InitializeGAMSDirectoryForUserDefinedConfiguration()
62 {
63 return HouseKeeper.gamsSystemDir;
64 }
65
66 [SetUp]
67 public void SetUp()
68 {
69 wsInfo = new GAMSWorkspaceInfo();
70 ws = null;
71 db = null;
72 }
73
74 [TearDown]
75 public void TearDown()
76 {
77 if (db != null)
78 db.Dispose();
79 }
80
81 [Test]
82 public void testAPIVersion()
83 {
84 logger.Debug("entering Test GAMSWorkspace | testAPIVersion");
85
86 Assert.AreEqual(APIVersion, GAMSWorkspace.APIVersion, "expect [" +
87 APIVersion + "] as API release number in GAMSWorkspace.");
88 Assert.AreEqual(majorVersion, GAMSWorkspace.APIMajorRelNumber, "expect [" +
89 APIVersion + "] as API major release number in GAMSWorkspace.");
90 Assert.AreEqual(minorVersion, GAMSWorkspace.APIMinorRelNumber, "expect [" +
91 APIVersion + "] as API minor release number in GAMSWorkspace.");
92 Assert.AreEqual(goldVersion, GAMSWorkspace.APIGoldRelNumber, "expect [" +
93 APIVersion + "] as API gold release number in GAMSWorkspace.");
94 logger.Debug("exiting Test GAMSWorkspace | testAPIVersion");
95 }
96
97 [Test]
98 public void testGAMSVersion()
99 {
100 logger.Debug("TestGAMSWorkspace | testGAMSVersion");
101
102 ws = new GAMSWorkspace();
103 Assert.NotNull(ws, "expect a successful creation of GAMSWorkspace.");
104 Assert.AreEqual(APIVersion, ws.Version, "expect [" + APIVersion + "] as GAMS release number in GAMSWorkspace.");
105 Assert.AreEqual(majorVersion, ws.MajorRelNumber, "expect [" + majorVersion + "] as GAMS major release number in GAMSWorkspace.");
106 Assert.AreEqual(minorVersion, ws.MinorRelNumber, "expect [" + minorVersion + "] as GAMS minor release number in GAMSWorkspace.");
107 Assert.AreEqual(goldVersion, ws.GoldRelNumber, "expect [" + goldVersion + "] as GAMS gold release number in GAMSWorkspace.");
108
109 logger.Debug("exiting TestGAMSWorkspace | testGAMSVersion");
110 }
111
112 [Test]
113 public void testGAMSWorkspaceInfo1()
114 {
115 logger.Debug("enterting TestGAMSWorkspace | testGAMSWorkspaceInfo1");
116
117 wsInfo = new GAMSWorkspaceInfo();
118
119 testExpectedWorkspaceInfoResult(null, null, DebugLevel.KeepFiles);
120 logger.Debug("exiting TestGamsWorkspace | testGAMSVersion");
121 }
122
123 [Test]
124 public void testGAMSWorkspaceInfo2()
125 {
126 logger.Debug("enterting TestGAMSWorkspace | testGAMSWorkspace2");
127
128 wsInfo.SystemDirectory = HouseKeeper.gamsAbsoluteSystemDir;
129
130 testExpectedWorkspaceInfoResult(null, HouseKeeper.gamsAbsoluteSystemDir, DebugLevel.Off);
131 logger.Debug("exiting TestGamsWorkspace | testGAMSWorkspace2");
132 }
133
134
135 [Test]
136 public void testGAMSWorkspaceInfo3()
137 {
138 logger.Debug("enterting TestGAMSWorkspace | testGAMSWorkspaceInfo3");
139
140 wsInfo.WorkingDirectory = HouseKeeper.gamsWorkingDir;
141
142 testExpectedWorkspaceInfoResult(HouseKeeper.gamsWorkingDir, null, DebugLevel.Off);
143 logger.Debug("exiting TestGamsWorkspace | testGAMSWorkspaceInfo3");
144 }
145
146 [Test]
147 public void testGAMSWorkspaceInfo4()
148 {
149 logger.Debug("enterting TestGAMSWorkspace | testGAMSWorkspaceInfo4");
150
151 Assert.False(Directory.Exists("testGAMSWorkspaceInfo4"),
152 "does not expect existence of [" + workingDir +
153 "]"
154 );
155 wsInfo.WorkingDirectory = workingDir;
156 testExpectedWorkspaceInfoResult(workingDir, null, DebugLevel.Off);
157 logger.Debug("exiting TestGamsWorkspace | testGAMSWorkspaceInfo4");
158 }
159
160 // Test ommitted, C# throws an exception when trying to create a file with an empty string as argument
161 //[test]
162 //public void testgamsworkspaceinfo5()
163 //{
164 // logger.debug("enterting testgamsworkspace | testgamsworkspaceinfo5");
165
166 // string workingdir = file.create("").tostring();
167 // string parentdir = directory.getparent(housekeeper.gamsworkingdir).tostring();
168 //
169 // assert.areequal(path.getfullpath(workingdir), path.getfullpath(parentdir),
170 // "expect an empty string refers to current (in this case parent) directory.");
171 // wsinfo.workingdirectory = path.getfullpath(workingdir);
172
173 // testexpectedworkspaceinforesult(path.getfullpath(workingdir), null, debuglevel.off);
174 // logger.debug("exiting testgamsworkspace | testgamsworkspaceinfo5");
175 //}
176
177 // see above
178 //[Test]
179 //public void testGAMSWorkspaceInfo6()
180 //{
181 // logger.Debug("enterting TestGAMSWorkspace | testGAMSWorkspaceInfo6");
182
183 // wsInfo.WorkingDirectory = "";
184
185 // // testExpectedWorkspaceInfoResult("", null, DebugLevel.Off);
186 // logger.Debug("exiting TestGamsWorkspace | testGAMSWorkspaceInfo6");
187 //}
188
189 [Test]
190 public void testGAMSWorkspaceInfo7()
191 {
192 logger.Debug("enterting TestGAMSWorkspace | testGAMSWorkspaceInfo7");
193
194 String userDir = Path.GetDirectoryName(
195 Assembly.GetExecutingAssembly().Location);
196 wsInfo.WorkingDirectory = userDir;
197
199 logger.Debug("exiting TestGamsWorkspace | testGAMSWorkspaceInfo7");
200 }
201
202 // see above
203 //[Test]
204 //public void testGAMSWorkspaceInfo8()
205 //{
206 // logger.Debug("enterting TestGAMSWorkspace | testGAMSWorkspaceInfo8");
207
208 // wsInfo.SystemDirectory = String.Empty;
209
210 // testExpectedWorkspaceInfoResult(null, String.Empty, DebugLevel.Off);
211 // logger.Debug("exiting TestGamsWorkspace | testGAMSWorkspaceInfo8");
212 //}
213
214 [Test]
215 public void testGAMSWorkspaceInfo9()
216 {
217 logger.Debug("enterting TestGAMSWorkspace | testGAMSWorkspaceInfo9");
218
219 wsInfo.Debug = DebugLevel.KeepFiles;
220
221 testExpectedWorkspaceInfoResult(null, null, DebugLevel.KeepFiles);
222 logger.Debug("exiting TestGamsWorkspace | testGAMSWorkspaceInfo9");
223 }
224 [Test]
225 public void testGAMSWorkspaceInfo10()
226 {
227 logger.Debug("enterting TestGAMSWorkspace | testGAMSWorkspaceInfo10");
228
229 wsInfo = new GAMSWorkspaceInfo(HouseKeeper.gamsWorkingDir,
230 HouseKeeper.gamsAbsoluteSystemDir,
231 DebugLevel.KeepFiles);
232
234 HouseKeeper.gamsAbsoluteSystemDir,
235 DebugLevel.KeepFiles);
236 logger.Debug("exiting TestGamsWorkspace | testGAMSWorkspaceInfo10");
237 }
238
239 [Test]
240 public void testDefaultConstructor()
241 {
242 logger.Debug("enterting TestGAMSWorkspace | testDefaultConstructor");
243
244 ws = new GAMSWorkspace();
245
246 testExpectedWorkspaceResult(tempPath, gamsDefaultPath, DebugLevel.Off);
247 logger.Debug("exiting TestGamsWorkspace | testDefaultConstructor");
248 }
249
250 [Test]
251 public void testGAMSWorkspaceConstructorWithNullGAMSWorkspaceInfo()
252 {
253 logger.Info("test exception");
254
255 Assert.Throws<GAMSException>(() => new GAMSWorkspace((GAMSWorkspaceInfo)null));
256 Assert.That(true);
257 }
258
259 [Test]
260 public void testConstructorWithGAMSWorkspaceInfo1()
261 {
262 logger.Debug("entering TestGAMSWorkspace | testConstructorWithGAMSWorkspaceInfo1");
263 ws = new GAMSWorkspace(wsInfo);
264 testExpectedWorkspaceResult(tempPath, gamsDefaultPath, DebugLevel.Off);
265 logger.Debug("exiting TestGAMSWorkspace | testConstructorWithGAMSWorkspaceInfo1");
266 }
267
268 [Test]
269 public void testConstructorWothGAMSWorkspaceInfo2()
270 {
271 logger.Debug("entering TestGAMSWorkspace | testConstructorWothGAMSWorkspaceInfo2");
272 wsInfo.SystemDirectory = null;
273 ws = new GAMSWorkspace(wsInfo);
274 testExpectedWorkspaceResult(tempPath, gamsDefaultPath, DebugLevel.Off);
275 logger.Debug("exiting TestGAMSWorkspace | testConstructorWothGAMSWorkspaceInfo2");
276 }
277
278 [Test]
279 public void testConstructorWithGAMSWorkspaceInfo3()
280 {
281 logger.Debug("entering TestGAMSWorkspace | testConstructorWithGAMSWorkspaceInfo3");
282 wsInfo.WorkingDirectory = null;
283 ws = new GAMSWorkspace(wsInfo);
284 testExpectedWorkspaceResult(tempPath, gamsDefaultPath, DebugLevel.Off);
285 logger.Debug("exiting TestGAMSWorkspace | testConstructorWithGAMSWorkspaceInfo3");
286 }
287
288 // test ommited, in .NET API setting DebugLevel to NULL is not allowed
289 //[Test]
290 //public void testConstructorWithGAMSWorkspaceInfo4()
291 //{
292 // logger.Debug("entering TestGAMSWorkspace | testConstructorWithGAMSWorkspaceInfo4");
293 // wsInfo.Debug = null;
294
295 // // insert test here
296
297 // testExpectedWorkspaceResult(HouseKeeper.gamsWorkingDir, gamsDefaultPath, DebugLevel.KeepFiles);
298 // logger.Debug("exiting TestGAMSWorkspace | testConstructorWithGAMSWorkspaceInfo4");
299 //}
300
301 [Test]
302 public void testConstructorWithGAMSWorkspaceInfo5()
303 {
304 logger.Debug("entering TestGAMSWorkspace | testConstructorWithGAMSWorkspaceInfo5");
305
306 wsInfo.Debug = DebugLevel.KeepFiles;
307 ws = new GAMSWorkspace(wsInfo);
308
309 testExpectedWorkspaceResult(tempPath, gamsDefaultPath, DebugLevel.KeepFiles);
310 logger.Debug("exiting TestGAMSWorkspace | testConstructorWithGAMSWorkspaceInfo5");
311 }
312
313 // test ommitted. .NET API does not allow setting debug level to undefinded
314 //[Test]
315 //public void testConstructorWithGAMSWorkspaceInfo6()
316 //{
317 // logger.Debug("entering TestGAMSWorkspace | testConstructorWithGAMSWorkspaceInfo6");
318 // wsInfo.Debug = DebugLevel.UNDEFINED_LEVEL;
319
320 // // insert test here
321
322 // testExpectedWorkspaceResult(HouseKeeper.gamsWorkingDir, gamsDefaultPath, DebugLevel.KeepFiles);
323 // logger.Debug("exiting TestGAMSWorkspace | testConstructorWithGAMSWorkspaceInfo6");
324 //}
325
326 [Test]
327 public void testConstructorWithGAMSWorkspaceInfo7()
328 {
329 logger.Debug("entering TestGAMSWorkspace | testConstructorWithGAMSWorkspaceInfo7");
330 wsInfo.SystemDirectory = userDefinedSystemDir;
331 wsInfo.WorkingDirectory = HouseKeeper.gamsWorkingDir;
332
333 ws = new GAMSWorkspace(wsInfo);
334
335 testExpectedWorkspaceResult(HouseKeeper.gamsWorkingDir, userDefinedSystemDir, DebugLevel.Off);
336 logger.Debug("exiting TestGAMSWorkspace | testConstructorWithGAMSWorkspaceInfo7");
337 }
338
339 [Test]
340 public void testConstructorWithGAMSWorkspaceInfo8()
341 {
342 logger.Debug("entering TestGAMSWorkspace | testConstructorWithGAMSWorkspaceInfo8");
343 wsInfo.SystemDirectory = userDefinedSystemDir;
344 wsInfo.Debug = DebugLevel.KeepFiles;
345
346 ws = new GAMSWorkspace(wsInfo);
347
348 testExpectedWorkspaceResult(tempPath, userDefinedSystemDir, DebugLevel.KeepFiles);
349 logger.Debug("exiting TestGAMSWorkspace | testConstructorWithGAMSWorkspaceInfo8");
350 }
351
352 [Test]
353 public void testConstructorWithGAMSWorkspaceInfo9()
354 {
355 logger.Debug("entering TestGAMSWorkspace | testConstructorWithGAMSWorkspaceInfo9");
356 wsInfo.WorkingDirectory = HouseKeeper.gamsWorkingDir;
357 wsInfo.Debug = DebugLevel.ShowLog;
358
359 ws = new GAMSWorkspace(wsInfo);
360
361 testExpectedWorkspaceResult(HouseKeeper.gamsWorkingDir, gamsDefaultPath, DebugLevel.ShowLog);
362 logger.Debug("exiting TestGAMSWorkspace | testConstructorWithGAMSWorkspaceInfo9");
363 }
364
365 [Test]
366 public void testConstructorWithGAMSWorkspaceInfo10()
367 {
368 logger.Debug("entering TestGAMSWorkspace | testConstructorWithGAMSWorkspaceInfo10");
369 wsInfo.SystemDirectory = userDefinedSystemDir;
370 wsInfo.WorkingDirectory = HouseKeeper.gamsWorkingDir;
371 wsInfo.Debug = DebugLevel.KeepFiles;
372
373 ws = new GAMSWorkspace(wsInfo);
374
375 testExpectedWorkspaceResult(HouseKeeper.gamsWorkingDir, gamsDefaultPath, DebugLevel.KeepFiles);
376 logger.Debug("exiting TestGAMSWorkspace | testConstructorWithGAMSWorkspaceInfo10");
377 }
378
379 [Test]
380 public void testConstructorWithGAMSWorkspaceInfo11()
381 {
382 logger.Debug("test exception");
383 wsInfo.SystemDirectory = "";
384
385 Assert.False(HouseKeeper.isGAMSDirectory(wsInfo.SystemDirectory));
386 Assert.Throws<GAMSException>(() => new GAMSWorkspace(wsInfo));
387 }
388
389 [Test]
390 public void testConstructorWithGAMSWorkspaceInfo12()
391 {
392 logger.Debug("test exception");
393 wsInfo.WorkingDirectory = "";
394
395 Assert.Throws<GAMSException>(() => ws = new GAMSWorkspace(wsInfo));
396 }
397
398 [Test]
399 public void testConstructorWithGAMSWorkspaceInfoWhitespaces1()
400 {
401 logger.Debug("test exception");
402
403 wsInfo.WorkingDirectory = " ";
404 Assert.Throws<GAMSException>(() => ws = new GAMSWorkspace(wsInfo));
405 }
406
407 [Test]
408 public void testConstructorWithGAMSWorkspaceInfoWhitespaces2()
409 {
410 logger.Debug("test exception");
411
412 wsInfo.WorkingDirectory = " ";
413 Assert.Throws<GAMSException>(() => ws = new GAMSWorkspace(wsInfo));
414 }
415
416 [Test]
417 public void testCronstructorWithThreeParameters1()
418 {
419 logger.Debug("entering TestGAMSWorkspace | testCronstructorWithThreeParameters1");
420
421 ws = new GAMSWorkspace(null, userDefinedSystemDir, HouseKeeper.gamsDebugLevel);
422 testExpectedWorkspaceResult(tempPath, userDefinedSystemDir, HouseKeeper.gamsDebugLevel);
423 logger.Debug("exiting TestGAMSWorkspace | testCronstructorWithThreeParameters1");
424 }
425
426 [Test]
427 public void testCronstructorWithThreeParameters2()
428 {
429 logger.Debug("entering TestGAMSWorkspace | testCronstructorWithThreeParameters2");
430
431 ws = new GAMSWorkspace(HouseKeeper.gamsWorkingDir, null, HouseKeeper.gamsDebugLevel);
432
433 testExpectedWorkspaceResult(HouseKeeper.gamsWorkingDir, gamsDefaultPath, HouseKeeper.gamsDebugLevel);
434 logger.Debug("exiting TestGAMSWorkspace | testCronstructorWithThreeParameters2");
435 }
436
437 [Test]
438 public void testCronstructorWithThreeParameters3()
439 {
440 logger.Debug("entering TestGAMSWorkspace | testCronstructorWithThreeParameters3");
441
442 ws = new GAMSWorkspace(HouseKeeper.gamsWorkingDir, userDefinedSystemDir, DebugLevel.Off);
443
444 testExpectedWorkspaceResult(HouseKeeper.gamsWorkingDir, userDefinedSystemDir, HouseKeeper.gamsDebugLevel);
445 logger.Debug("exiting TestGAMSWorkspace | testCronstructorWithThreeParameters3");
446 }
447
448 [Test]
449 public void testCronstructorWithThreeParameters4()
450 {
451 logger.Debug("entering TestGAMSWorkspace | testCronstructorWithThreeParameters4");
452 String dirName = "testConstructorWithThreeParameters4";
453 Assert.False(Directory.Exists(dirName));
454
455 ws = new GAMSWorkspace(HouseKeeper.gamsAbsoluteWorkingDir, null, HouseKeeper.gamsDebugLevel);
456
457 testExpectedWorkspaceResult(HouseKeeper.gamsWorkingDir, userDefinedSystemDir, HouseKeeper.gamsDebugLevel);
458 logger.Debug("exiting TestGAMSWorkspace | testCronstructorWithThreeParameters4");
459 }
460
461 [Test]
462 public void testConstructorWithThreeParameters5()
463 {
464 logger.Debug("entering TestGAMSWorkspace | testConstructorWithThreeParameters5");
465
466 ws = new GAMSWorkspace();
467
468 testExpectedWorkspaceResult(Path.GetTempPath(), gamsDefaultPath, DebugLevel.Off);
469
470 logger.Debug("exiting TestGAMSWorkspace | testConstructorWithThreeParameters5");
471 }
472
473 [Test]
474 public void testConstructorWithThreeParameters6()
475 {
476 logger.Debug("entering TestGAMSWorkspace | testConstructorWithThreeParameters6");
477
478 ws = new GAMSWorkspace(HouseKeeper.gamsWorkingDir, userDefinedSystemDir, HouseKeeper.gamsDebugLevel);
479
480 testExpectedWorkspaceResult(HouseKeeper.gamsWorkingDir, userDefinedSystemDir, DebugLevel.Off);
481 logger.Debug("exiting TestGAMSWorkspace | testConstructorWithThreeParameters6");
482 }
483
484 [Test]
485 public void testConstructorWithThreeParameters7()
486 {
487 logger.Debug("entering TestGAMSWorkspace | testConstructorWithThreeParameters7");
488
489 Assert.Throws<GAMSException>(() => new GAMSWorkspace(HouseKeeper.gamsWorkingDir,
490 "./This/is/a/rediculous/gams/system/directory/ever@#$%^",
491 HouseKeeper.gamsDebugLevel));
492
493 logger.Debug("exiting TestGAMSWorkspace | testConstructorWithThreeParameters7");
494 }
495
496 [Test]
497 public void testConstructorWithThreeParameters8()
498 {
499 logger.Debug("entering TestGAMSWorkspace | testConstructorWithThreeParameters8");
500
501 Assert.False(HouseKeeper.isGAMSDirectory(Path.GetDirectoryName(
502 Assembly.GetExecutingAssembly().Location)), "do not expect current directory as gams system directory");
503
504 Assert.Throws<GAMSException>(() => new GAMSWorkspace(null, "", DebugLevel.Off));
505
506 logger.Debug("exiting TestGAMSWorkspace | testConstructorWithThreeParameters8");
507 }
508
509 [Test]
510 public void testConstructorWithThreeParametersWhitespaces1()
511 {
512 logger.Debug("entering TestGAMSWorkspace | testConstructorWithThreeParametersWhitespaces2");
513
514 Assert.Throws<GAMSException>(() => new GAMSWorkspace("", null, DebugLevel.Off));
515
516 logger.Debug("exiting TestGAMSWorkspace | testConstructorWithThreeParametersWhitespaces2");
517 }
518
519 [Test]
520 public void testConstructorWithThreeParametersWhitespaces2()
521 {
522 logger.Debug("entering TestGAMSWorkspace | testConstructorWithThreeParametersWhitespaces3");
523
524 Assert.Throws<GAMSException>(() => new GAMSWorkspace("", "", DebugLevel.Off));
525
526 logger.Debug("exiting TestGAMSWorkspace | testConstructorWithThreeParametersWhitespaces3");
527 }
528
529 [Test]
530 public void testSetMyEPS()
531 {
532 // TODO: fix this test
533 logger.Debug("entering TestGAMSWorkspace | testSetMyEPS");
534
535 double oldValue = gamsglobals.sv_eps;
536 double newValue = 5E-300;
537 // double eps;
538
539 ws = new GAMSWorkspace();
540 ws.MyEPS = newValue;
541
542 // Assert.AreEqual(newValue, ws.MyEPS, "expect EPS value with new value");
543
544 Assert.That(newValue != oldValue, "do not expect same special value.");
545 // Assert.AreEqual(gamsglobals.sv_eps, newValue, "expect a new special value in GAMSGlobals.SpecialValues.EPS.");
546
547 logger.Debug("exiting TestGAMSWorkspace | testSetMyEPS");
548 }
549
550 [Test]
551 public void testAddDefaultOption()
552 {
553 logger.Debug("entering TestGAMSWorkspace | testAddDefaultOption");
554
555 ws = new GAMSWorkspace();
556
557 GAMSOptions opt = ws.AddOptions();
558 Assert.AreEqual(0, opt.Defines.Count, "expect 0 definitions in opt.");
559 opt.Defines.Add("gdxincname", "tdata");
560 opt.Defines.Add("useBig", "1");
561 opt.Defines.Add("nrScen", "100");
562 GAMSOptions newopt = ws.AddOptions(opt);
563
564 Assert.AreEqual(opt.Defines.Count, newopt.Defines.Count, "expect [opt] and [newopt] with the same definition.");
565 Assert.AreEqual(3, opt.Defines.Count, "expect [opt] and [newopt] with the same number of definitions.");
566 String nopt;
567 String oopt;
568 opt.Defines.TryGetValue("gdxincname", out nopt);
569 newopt.Defines.TryGetValue("gdxincname", out oopt);
570 Assert.AreEqual(oopt, nopt, "expect [opt] and [newopt] with the same definition of [gdxincname].");
571 Assert.AreEqual(opt.Defines.Count, newopt.Defines.Count, "expect [opt] and [newopt] with the same number of definitions.");
572
573 foreach (String key in opt.Defines.Keys)
574 {
575 Assert.NotNull(key, "do not expect a NULL definition of [" + key + "] in [opt].");
576 }
577
578 logger.Debug("exiting TestGAMSWorkspace | testAddDefaultOption");
579 }
580
581 [Test]
582 public void testAddDefaultCheckpoint()
583 {
584 logger.Debug("entering TestGAMSWorkspace | testAddDefaultCheckpoint");
585
586 ws = new GAMSWorkspace();
587
589 Assert.That(cp.Name.StartsWith(prefix), "expect checkpoint name [" + cp.Name + "] with prefix [" + prefix + "].");
590
591 logger.Debug("exiting TestGAMSWorkspace | testAddDefaultCheckpoint");
592 }
593
594 [Test]
595 public void testAddCheckpointNullName()
596 {
597 logger.Debug("entering TestGAMSWorkspace | testAddCheckpointNullName");
598
599 ws = new GAMSWorkspace();
600
601 GAMSCheckpoint cp = ws.AddCheckpoint(null);
602 Assert.That(cp.Name.StartsWith(prefix), "expect checkpoint name [" + cp.Name + "] with prefix [" + prefix + "].");
603
604 logger.Debug("exiting TestGAMSWorkspace | testAddCheckpointNullName");
605 }
606
607 [Test]
608 public void testCheckpointEmptyName()
609 {
610 logger.Debug("entering TestGAMSWorkspace | testCheckpointEmptyName");
611
612 ws = new GAMSWorkspace();
613
614 GAMSCheckpoint cp = ws.AddCheckpoint("");
615 Assert.That(cp.Name.StartsWith(prefix), "expect checkpoint name [" + cp.Name + "] with prefix [" + prefix + "].");
616
617 logger.Debug("exiting TestGAMSWorkspace | testCheckpointEmptyName");
618 }
619
620 [Test]
621 public void testAddDefaultCheckpoint1()
622 {
623 logger.Debug("entering TestGAMSWorkspace | testAddDefaultCheckpoint1");
624
625 DirectoryInfo workingDir = new DirectoryInfo(HouseKeeper.gamsWorkingDir + Path.DirectorySeparatorChar + "testDefaultCheckpoint1");
626 Assert.False(workingDir.Exists, "does not expect existence of [" + workingDir.FullName + "]");
627
628 ws = new GAMSWorkspace(workingDir.FullName);
629 GAMSJob job = ws.AddJobFromGamsLib("trnsport");
630 Assert.NotNull(job, "does not expect a NULL GAMSJob");
631 String jobName = "testAddDefaultCheckpoint1";
632
634 Assert.Throws<GAMSException>(() => ws.AddJobFromFile("trnsport.gms", cp, jobName), "");
635
636 logger.Debug("exiting TestGAMSWorkspace | testAddDefaultCheckpoint1");
637 }
638
639 [Test]
640 public void testAddDefaultCheckpoint2()
641 {
642 logger.Debug("entering TestGAMSWorkspace | testAddDefaultCheckpoint2");
643
644 DirectoryInfo workingDir = new DirectoryInfo(HouseKeeper.gamsWorkingDir + Path.DirectorySeparatorChar + "testAddDefaultCheckpoint2");
645 Assert.False(workingDir.Exists, "does not expect existence of [" + workingDir.FullName + "]");
646
647 ws = new GAMSWorkspace(workingDir.FullName);
648 GAMSJob job = ws.AddJobFromGamsLib("cutstock");
649 Assert.NotNull(job, "does not expect a NULL GAMSJob");
650 String jobName = "testAddDefaultCheckpoint2";
651
653 FileInfo file = new FileInfo(workingDir.FullName + Path.DirectorySeparatorChar + cp.Name + ".g00");
654
655 Assert.False(file.Exists, "do not expect GAMSCheckpoint file [" + file.Name + "] under [" + ws.WorkingDirectory + "].");
656
657 job.Run(cp);
658 logger.Info("job executed with checkpoint cp");
659
660 file.Refresh();
661 Assert.That(file.Exists, "expect GAMSCheckpoint file [" + file.Name + "] under [" + ws.WorkingDirectory + "].");
662
663 job = ws.AddJobFromFile("cutstock.gms", cp, jobName);
664 Assert.NotNull(job, "do not expect a NUll job.");
665
666 logger.Debug("exiting TestGAMSWorkspace | testAddDefaultCheckpoint2");
667 }
668
669 [Test]
670 public void testAddCheckpointString1()
671 {
672 logger.Debug("entering TestGAMSWorkspace | testAddCheckpointString1");
673
674 FileInfo workingDir = new FileInfo(HouseKeeper.gamsWorkingDir + Path.DirectorySeparatorChar + "testAddCheckpointDuplicateName");
675 Assert.False(workingDir.Exists, "does not expect existence of [" + workingDir.FullName + "]");
676
677 ws = new GAMSWorkspace(workingDir.FullName);
678
679 GAMSJob job = ws.AddJobFromGamsLib("trnsport");
680 Assert.NotNull(job, "does not expect a NULL GAMSJob");
681 String jobName = "testAddCheckpointString1";
682 String cpName = "testAddCheckpointString1";
683
684 GAMSCheckpoint cp = ws.AddCheckpoint(cpName);
685 job.Run(cp);
686
687 job = ws.AddJobFromFile("trnsport.gms", cp, jobName);
688 logger.Debug("exiting TestGAMSWorkspace | testAddCheckpointString1");
689 }
690
691 [Test]
692 public void testAddCheckpointDuplicateName()
693 {
694 logger.Debug("entering TestGAMSWorkspace | testAddCheckpointDuplicateName");
695
696 FileInfo workingDir = new FileInfo(HouseKeeper.gamsWorkingDir + Path.DirectorySeparatorChar + "testAddCheckpointDuplicateName");
697 Assert.False(workingDir.Exists, "does not expect existence of [" + workingDir.FullName + "]");
698
699 ws = new GAMSWorkspace(workingDir.FullName);
700
701 String cpFileName = "testAddCheckpointDuplicateName";
702
703 ws.AddCheckpoint(cpFileName);
704 Assert.Throws<GAMSException>(() => ws.AddCheckpoint(cpFileName));
705
706 HouseKeeper.rcleanup(workingDir.FullName);
707 logger.Debug("exiting TestGAMSWorkspace | testAddCheckpointDuplicateName");
708 }
709
710 [Test]
711 public void testAddDefaultDatabase()
712 {
713 logger.Debug("entering TestGAMSWorkspace | testAddDefaultDatabase");
714
715 wsInfo = new GAMSWorkspaceInfo();
716 wsInfo.WorkingDirectory = HouseKeeper.gamsWorkingDir;
717 ws = new GAMSWorkspace(wsInfo);
718
719 db = ws.AddDatabase();
720 testAnEmptyDatabase();
721
722 logger.Debug("exiting TestGAMSWorkspace | testAddDefaultDatabase");
723 }
724
725 [Test]
726 public void testAddDatabaseWithName()
727 {
728 logger.Debug("entering TestGAMSWorkspace | testAddDatabaseWithName");
729
730 wsInfo = new GAMSWorkspaceInfo();
731 wsInfo.WorkingDirectory = HouseKeeper.gamsWorkingDir;
732 ws = new GAMSWorkspace(wsInfo);
733 String dbName = "testAddDatabaseWithName";
734 db = ws.AddDatabase(dbName);
735
736 testAnEmptyDatabase();
737 Assert.AreEqual(dbName, db.Name, "expect as [" + dbName + "] as db.getName().");
738
739 logger.Debug("exiting TestGAMSWorkspace | testAddDatabaseWithName");
740 }
741
742 [Test]
743 public void testAddDatabaseFromNullGDX()
744 {
745 logger.Debug("entering TestGAMSWorkspace | testAddDatabaseFromNullGDX");
746
747 wsInfo = new GAMSWorkspaceInfo();
748 wsInfo.WorkingDirectory = HouseKeeper.gamsWorkingDir;
749 ws = new GAMSWorkspace(wsInfo);
750
751 Assert.Throws<GAMSException>(() => ws.AddDatabaseFromGDX("ThisGDXFileNameProbablyDoesNotExist"));
752
753 logger.Debug("exiting TestGAMSWorkspace | testAddDatabaseFromNullGDX");
754 }
755
756 [Test]
757 public void testAddDatabaseFromNonExistGDXFile()
758 {
759 logger.Debug("entering TestGAMSWorkspace | testAddDatabaseFromNonExistGDXFile");
760
761 wsInfo = new GAMSWorkspaceInfo();
762 wsInfo.WorkingDirectory = HouseKeeper.gamsWorkingDir;
763 ws = new GAMSWorkspace(wsInfo);
764
765 Assert.Throws<GAMSException>(() => ws.AddDatabaseFromGDX("ThisGDXFileNameProbablyDoesNotExist"));
766
767 logger.Debug("exiting TestGAMSWorkspace | testAddDatabaseFromNonExistGDXFile");
768 }
769
770
771 private void testAnEmptyDatabase()
772 {
773 Assert.AreEqual(0, db.NrSymbols, "does not expect a symbol in newly created database.");
774
775 foreach(GAMSSymbol symbol in db)
776 Assert.Fail("does not expect a symbol in a newly created database.");
777
778 GAMSSymbol sym = null;
779 Assert.Null(sym, "expect a NULL symbol");
780 Assert.Throws<GAMSException>(() => db.GetSymbol("x"));
781
782 Assert.Throws<GAMSException>(() => db.GetSet("x"));
783
784 Assert.Throws<GAMSException>(() => db.GetParameter("x"));
785
786 Assert.Throws<GAMSException>(() => db.GetEquation("x"));
787
788 Assert.Throws<GAMSException>(() => db.GetVariable("x"));
789 }
790
791
792
799 public void testExpectedWorkspaceInfoResult(String workdir, String sysdir, DebugLevel debug)
800 {
801 if (workdir == null)
802 {
803 Assert.Null(wsInfo.WorkingDirectory, "expect a NULL working directory.");
804 }
805 else
806 {
807 Assert.NotNull(wsInfo.WorkingDirectory, "does not expect a NULL wsInfo.WorkingDirectory.");
808 StringAssert.AreEqualIgnoringCase(new DirectoryInfo(workdir).FullName,
809 new DirectoryInfo(wsInfo.WorkingDirectory).FullName,
810 "expect as [" + workdir + "] as wsInfo.WorkingDirectory."
811 );
812 }
813
814 if (sysdir == null)
815 {
816 Assert.Null(wsInfo.SystemDirectory, "expect a NULL system directory.");
817 }
818 else
819 {
820 Assert.NotNull(wsInfo.SystemDirectory, "does not expect a NULL wsInfo.getSystemDirectory().");
821 StringAssert.AreEqualIgnoringCase(new DirectoryInfo(sysdir).FullName,
822 new DirectoryInfo(wsInfo.SystemDirectory).FullName,
823 "expect as [" + sysdir + "] as wsInfo.getSystemDirectory()."
824 );
825 }
826 }
827
828 public void testExpectedWorkspaceResult(String workdir, String sysdir, DebugLevel debug)
829 {
830 Assert.NotNull(ws, "expect a successful creation of GAMSWorkspace.");
831 Assert.NotNull(ws.SystemDirectory, "does not expect a NULL system directory");
832 StringAssert.AreEqualIgnoringCase(Directory.CreateDirectory(sysdir).FullName,
833 Directory.CreateDirectory(Path.GetFullPath(ws.SystemDirectory)).FullName,
834 "expect a gams system dir being set to [" + sysdir + "].", null);
835 Assert.True(Directory.Exists(sysdir), "expect an existence of a directory");
836 Assert.True(HouseKeeper.isGAMSDirectory(sysdir), "expect to find optgam.def in a directory");
837 Assert.NotNull(HouseKeeper.findGAMS(sysdir), "expect to find gams in directory");
838 Assert.NotNull(ws.WorkingDirectory, "does not expect a NULL working directory");
839 Assert.True(Directory.Exists(workdir), "expect an existence of working directory [" + workdir + "]");
840
841 String expectedWorkDir = Path.GetFullPath(workdir);
842 String wsWorkDir = Path.GetFullPath(ws.WorkingDirectory);
843
844 if (Path.GetTempPath().Equals(expectedWorkDir))
845 {
846 String actualWorkDir = Path.GetFullPath(Directory.GetParent(ws.WorkingDirectory).ToString());
847 Assert.AreEqual(new DirectoryInfo(expectedWorkDir), Directory.GetParent(ws.WorkingDirectory),
848 "expect a gams working dir being set to [" + expectedWorkDir + "]."
849 );
850 }
851 else
852 {
853 Assert.AreEqual(expectedWorkDir, wsWorkDir,
854 "expect a gams working dir being set to [" + expectedWorkDir + "].");
855 }
856 }
857
858 [Test]
859 public void testAddOptionFromOptFile()
860 {
861 logger.Debug("Entering TestGAMSWorkspace | testAddOptionFromOptFile");
862 // given
863 wsInfo.WorkingDirectory = HouseKeeper.gamsWorkingDir;
864 // when
865 ws = new GAMSWorkspace(wsInfo);
866
867 String optfilename = "testoption.pf";
868 try
869 {
870 FileInfo optfile = new FileInfo(Path.Combine(ws.WorkingDirectory, optfilename));
871
872 using (StreamWriter writer = new StreamWriter(optfile.FullName))
873 {
874 writer.WriteLine("IterLim=1");
875 }
876
877
878 // when
879 GAMSOptions opt = ws.AddOptions(optfilename);
880 // then
881 Assert.AreEqual(opt.IterLim, 1, "expect option [IterLim] with value 1.");
882 }
883 catch (Exception e)
884 {
885 Assert.Fail("Exception should not be thrown : " + e.ToString());
886 }
887 logger.Debug("Exiting TestGAMSWorkspace | testAddOptionFromOptFile");
888 }
889
890 [Test]
891 public void testAddOptionFromNonExistingOptFile()
892 {
893 logger.Debug("Entering TestGAMSWorkspace | testAddOptionFromNonExistingOptFile");
894 // given
895 ws = new GAMSWorkspace();
896 String filename = "ThisIsAStrangeFileName.pf";
897 // when
898
899 Assert.Throws<GAMSException>(() => ws.AddOptions(filename));
900 logger.Debug("Exiting TestGAMSWorkspace | testAddOptionNonExistingFromOptFile");
901 }
902
903
904 [Test]
905 public void testAddDuplicatedDatabaseWithName()
906 {
907 logger.Debug("Entering TestGAMSWorkspace | testAddDuplicatedDatabaseWithName");
908 // given
909 wsInfo = new GAMSWorkspaceInfo();
910 wsInfo.WorkingDirectory = HouseKeeper.gamsWorkingDir;
911 ws = new GAMSWorkspace(wsInfo);
912 db = ws.AddDatabase();
913 // when
914 Assert.Throws<GAMSException>(() => ws.AddDatabase(db.Name));
915 }
916
917
918 //[Test]
919 //public void testAddDatabaseFromForcedNameGDX()
920 //{
921 // logger.Debug("Entering TestGAMSWorkspace | testAddDatabaseFromForcedNameGDX");
922 // // given
923 // wsInfo = new GAMSWorkspaceInfo();
924 // wsInfo.WorkingDirectory = HouseKeeper.gamsWorkingDir;
925 // ws = new GAMSWorkspace(wsInfo);
926 // GAMSDatabase db = ws.AddDatabase();
927 // GAMSParameter p = db.AddParameter("p", 1);
928 // FileInfo gdxFile = new FileInfo(Path.Combine(ws.WorkingDirectory, "p.gdx"));
929 // db.Export(gdxFile.FullName);
930 // // when non-public method addDatabaseFromForcedNameGDX is called with existing dbName
931 // GAMSDatabase anotherdb = new GAMSDatabase(gdxFile.FullName, ws, databaseName: db.Name, inModelName: null, forceName: true); //// Did not know how to force name in .NET API
932
933 // // then
934 // Assert.True(anotherdb.Name.Equals(db.Name), "expect same database name.");
935 // logger.Debug("Exiting TestGAMSWorkspace | testAddDatabaseFromForcedNameGDX");
936 //}
937
938 [Test]
939 public void testAddDatabaseFromSourceWithDuplicatedName()
940 {
941 logger.Debug("Entering TestGAMSWorkspace | testAddDatabaseFromForcedNameGDX");
942 // given
943 wsInfo = new GAMSWorkspaceInfo();
944 wsInfo.WorkingDirectory = HouseKeeper.gamsWorkingDir;
945 ws = new GAMSWorkspace(wsInfo);
946 GAMSDatabase db = ws.AddDatabase();
947 GAMSDatabase pdb = ws.AddDatabase();
948 GAMSParameter p = pdb.AddParameter("p", 1);
949
950 // when addDatabase is called with source Database and existing dbName
951 Assert.Throws<GAMSException>(() => ws.AddDatabase(pdb, db.Name));
952 }
953
954 [Test]
955 public void testAddDatabaseFromGDXWithDuplicatedName()
956 {
957 logger.Debug("Entering TestGAMSWorkspace | testAddDatabaseFromGDXWithDuplicatedName");
958 // given
959 wsInfo = new GAMSWorkspaceInfo();
960 wsInfo.WorkingDirectory = HouseKeeper.gamsWorkingDir;
961 ws = new GAMSWorkspace(wsInfo);
962 GAMSDatabase db = ws.AddDatabase();
963 GAMSParameter p = db.AddParameter("p", 1);
964 FileInfo gdxFile = new FileInfo(Path.Combine(ws.WorkingDirectory, "p.gdx"));
965 db.Export(gdxFile.FullName);
966
967 // when addDatabaseFromGDX is called with existing dbName
968 Assert.Throws<GAMSException>(() => ws.AddDatabaseFromGDX(gdxFile.FullName, db.Name));
969 }
970
971 [Test]
972 public void testEquivalentWorkspace()
973 {
974 logger.Debug("Entering TestGAMSWorkspace | testEquivalentWorkspace");
975 // given
977 // when
978 GAMSWorkspace ws1 = new GAMSWorkspace(wsInfo);
979 GAMSWorkspace ws2 = new GAMSWorkspace();
980 // then
981 Assert.True(ws1 != ws2);
982
983 // when
984 GAMSWorkspace ws3 = new GAMSWorkspace();
985 // then
986 Assert.True(ws1 != ws3);
987 Assert.True(ws2 != ws3);
988 logger.Debug("Exiting TestGAMSWorkspace | testEquivalentWorkspace");
989 }
990
991 public static GAMSJob createAndRunJobFromGamsLib(GAMSWorkspace ws, string lib)
992 {
993 GAMSJob job = null;
994 job = ws.AddJobFromGamsLib(lib);
995 Assert.NotNull(job);
996
997 job.Run();
998 Assert.NotNull(job);
999 Assert.NotNull(job.OutDB);
1000 return job;
1001 }
1002 }
1003}
GAMSParameter AddParameter(string identifier, int dimension, string explanatoryText="")
void Export(string filePath=null)
GAMSDatabase OutDB
void Run(GAMSOptions gamsOptions=null, GAMSCheckpoint checkpoint=null, TextWriter output=null, Boolean createOutDB=true)
Dictionary< string, string > Defines
static int APIMinorRelNumber
GAMSJob AddJobFromGamsLib(string model, GAMSCheckpoint checkpoint=null, string jobName=null)
GAMSDatabase AddDatabaseFromGDX(string gdxFileName, string databaseName=null, string inModelName=null)
GAMSDatabase AddDatabase(string databaseName=null, string inModelName=null)
static string APIVersion
GAMSCheckpoint AddCheckpoint(string checkpointName=null)
GAMSJob AddJobFromFile(string fileName, GAMSCheckpoint checkpoint=null, string jobName=null)
GAMSOptions AddOptions(GAMSOptions optFrom=null)
static int APIMajorRelNumber
static int APIGoldRelNumber
static String findGAMSFromPathEnvironmentVariable()
Find a valid GAMS system directory from environment.
Definition: HouseKeeper.cs:186
static bool rcleanup(String dir)
(recursively) delete all contents under dir, and delete dir.
Definition: HouseKeeper.cs:77
static String findGAMS(String systemPath)
Find a valid GAMS system directory from the given path. A valid GAMS system directory contains a file...
Definition: HouseKeeper.cs:196
static void initializeTestFrom(String filename, String subdir)
initialize class properties from fileName and prepare directory subdir
Definition: HouseKeeper.cs:114
static bool isGAMSDirectory(String dir)
check a GAMS directory.
Definition: HouseKeeper.cs:104
void testExpectedWorkspaceInfoResult(String workdir, String sysdir, DebugLevel debug)
Tests globally set workspaceInfo for expected parameters (workdir, sysdir, debuglevel)