Loading...
Searching...
No Matches
TestGAMSSymbol.cs
1using System;
2using System.IO;
3using GAMS;
4using NLog;
5using NUnit.Framework;
6
7namespace GamsApiTests
8{
10 {
11 static readonly String PROPERTIES_FILE = "test.properties";
12 static GAMSWorkspace globalWS;
13 static GAMSDatabase globalDB;
14 static GAMSWorkspace ws;
15 static GAMSDatabase db;
16 static Logger logger = LogManager.GetCurrentClassLogger();
17
18 [OneTimeSetUp]
19 public void OneTimeSetUp(){
20 logger.Debug("entering TestGAMSSymbol | OneTimeSetUp");
21 HouseKeeper.initializeTestFrom(PROPERTIES_FILE, "TestGAMSSymbol");
22 logger.Info("HouseKeeper task initialized");
24 wsInfo.WorkingDirectory = HouseKeeper.gamsWorkingDir;
25 wsInfo.SystemDirectory = HouseKeeper.gamsSystemDir;
26 wsInfo.Debug = DebugLevel.Off;
27 globalWS = new GAMSWorkspace(wsInfo);
28 logger.Info("workspace created");
29 globalDB = globalWS.AddDatabase("globalDB");
30 logger.Info("database created");
31 TestGAMSDatabase.initializeDatabase(globalDB);
32 logger.Debug("exiting TestGAMSSymbol | OneTimeSetUp");
33 }
34
35 [OneTimeTearDown]
36 public void OneTimeTearDown()
37 {
38 logger.Debug("entering TestGAMSSymbol | OneTimeTearDown");
39 if (globalDB != null)
40 globalDB.Dispose();
41 logger.Debug("exiting TestGAMSSymbol | OneTimeTearDown");
42 }
43
44 [SetUp]
45 public void SetUp()
46 {
47 ws = null;
48 db = null;
49 }
50
51 [TearDown]
52 public void TearDown()
53 {
54 if (db != null)
55 db.Dispose();
56 }
57
58 [Test]
59 public void testClear1()
60 {
61 logger.Debug("entering TestGAMSSymbol | testClear1");
62
63 String dbName = "testClear1";
64 db = globalWS.AddDatabase(globalDB, dbName);
65 logger.Info("db added");
66 GAMSSet set = db.GetSet("plants");
67 logger.Info("got set [plants]");
68 set.AddRecord("Alburquerque");
69 logger.Info("set record added");
70 Assert.That(set.NumberRecords > 0, "expect symbol demand with a record.");
71
72 set.Clear();
73 logger.Info("set record cleared");
74
75 Assert.That(set.NumberRecords == 0, "expect symbol demand without a record.");
76 logger.Debug("exiting TestGAMSSymbol | testClear1");
77 }
78
79 [Test]
80 public void testClear2()
81 {
82 logger.Debug("entering TestGAMSSymbol | testClear2");
83
84 String dbName = "testClear2";
85 db = globalWS.AddDatabase(globalDB, dbName);
86 logger.Info("db added");
87 GAMSParameter parameter = db.GetParameter("capacity");
88 logger.Info("parameter added");
89
90 parameter.Clear();
91 logger.Info("parameter record cleared");
92
93 Assert.Throws<GAMSException>(() => parameter.FindRecord("Seattle"), "do not expect to find a record");
94 foreach(GAMSSymbolRecord record in parameter)
95 Assert.Fail("expect no symbol record after clearing symbol!");
96
97 logger.Debug("exiting TestGAMSSymbol | testClear2");
98 }
99
100 [Test]
101 public void testGetFirstRecord()
102 {
103 logger.Debug("entering TestGAMSSymbol | testGetFirstRecord");
104
105 db = globalWS.AddDatabase(globalDB);
106 logger.Info("parameter checked");
107
108 GAMSParameter d = db.GetParameter("distance");
109 logger.Info("paramter created");
110
111 GAMSParameterRecord record = d.FirstRecord();
112 logger.Info("forst parameter record retrieved");
113
114 int count = 0;
115 do
116 {
117 logger.Info("checking parameter record #" + count + " ");
118 Assert.AreEqual(2, record.Keys.Length, "expect record with 2 dimensions");
119 count++;
120 } while (record.MoveNext());
121 Assert.AreEqual(6, count, "expect 6 iterating record from the slice.");
122 logger.Debug("exiting TestGAMSSymbol | testGetFirstRecord");
123 }
124
125 [Test]
126 public void testGetLastRecord()
127 {
128 logger.Debug("entering TestGAMSSymbol | testGetLastRecord");
129
130 db = globalWS.AddDatabase(globalDB);
131 GAMSParameter d = db.GetParameter("distance");
132
133 GAMSParameterRecord record = d.LastRecord();
134 Assert.False(record.MoveNext(), "do not expect next record from last record");
135 logger.Info("parameter created");
136
137 record = d.LastRecord();
138 logger.Info("last parameter record retrieved");
139 int count = 0;
140 do
141 {
142 logger.Info("checking parameter record #" + count + " ");
143 Assert.AreEqual(2, record.Keys.Length, "expect record with 2 dimensions");
144 count++;
145 } while (record.MoveNext());
146
147 Assert.AreEqual(1, count, "expect 1 iterating record from the slice.");
148 logger.Debug("exiting TestGAMSSymbol | testGetLastRecord");
149 }
150
151 [Test]
152 public void testGetFirstRecordSlice()
153 {
154 logger.Debug("entering TestGAMSSymbol | testGetFirstRecordSlice");
155
156 db = globalWS.AddDatabase(globalDB);
157 String[] slice = new String[] { " ", "Chicago" };
158 GAMSParameter d = db.GetParameter("distance");
159
160 GAMSParameterRecord record = d.FirstRecord(slice);
161 Assert.AreEqual(2, record.Keys.Length, "expect record with 2 dimensions");
162 int count = 0;
163 do
164 {
165 Assert.AreEqual(record.Keys[1], "Chicago", "expect [Chicago] as slice key.");
166 count++;
167 } while (record.MoveNext());
168 Assert.AreEqual(2, count, "expect 2 iterating record from the slice.");
169
170 logger.Debug("exiting TestGAMSSymbol | testGetFirstRecordSlice");
171 }
172
173 [Test]
174 public void testGetLastRecordSlice()
175 {
176 logger.Debug("entering TestGAMSSymbol | testGetLastRecordSlice");
177
178 db = globalWS.AddDatabase(globalDB);
179 String[] slice = new String[] { " ", "Chicago" };
180 GAMSParameter d = db.GetParameter("distance");
181
182 GAMSParameterRecord record = d.LastRecord(slice);
183 Assert.AreEqual(2, record.Keys.Length, "expect record with 2 dimensions");
184 Assert.False(record.MoveNext(), "do not expect next record from last record");
185
186 record = d.LastRecord(slice);
187 int count = 0;
188 do
189 {
190 Assert.AreEqual(record.Keys[1], "Chicago", "expect [Chicago] as slice key.");
191 count++;
192 } while (record.MoveNext());
193 Assert.AreEqual(1, count, "expect 1 iterating record from the slice.");
194
195 logger.Debug("exiting TestGAMSSymbol | testGetLastRecordSlice");
196 }
197
198 [Test]
199 public void testCopySymbolSameDB()
200 {
201 logger.Debug("entering TestGAMSSymbol | testCopySymbolSameDB");
202
203 String dbName = "testCopySymbolSameDB";
204
205 db = globalWS.AddDatabase(globalDB, dbName);
206
207 GAMSSet j = db.GetSet("markets");
208 GAMSSet jj = db.AddSet("jj", j.Dim);
209
210 j.CopySymbol(jj);
211
212 Assert.AreEqual(jj.NumberRecords, j.NumberRecords, "expect symbol i with [" + jj.NumberRecords + "] records.");
213 Assert.AreEqual(jj.FirstRecord().Keys[0], j.FirstRecord().Keys[0], "expect symbols i and ii have the same (first) key.");
214 int n1 = jj.FirstRecord().Keys.Length;
215 if (n1 > 0)
216 Assert.AreEqual(jj.FirstRecord().Keys[n1 - 1], jj.FirstRecord().Keys[n1 - 1],
217 "expect symbols j and jj have the same (first record) key.");
218
219 int n = 0;
220 foreach (GAMSSetRecord record in jj)
221 {
222 Assert.AreEqual(1, record.Keys.Length);
223 switch (n)
224 {
225 case 0:
226 Assert.AreEqual("New-York", record.Keys[0], "expect first record with name [New-York]");
227 break;
228 case 1:
229 Assert.AreEqual("Chicago", record.Keys[0], "expect second record with name [Chicago]");
230 break;
231 case 2:
232 Assert.AreEqual("Topeka", record.Keys[0], "expect third record with name [Topeka]");
233 break;
234 }
235 n++;
236 }
237 Assert.AreEqual(3, n, "expect symbol ii with 3 records.");
238
239 logger.Debug("exiting TestGAMSSymbol | testCopySymbolSameDB");
240 }
241
242 [Test]
243 public void testCopySymbolSameDBDifferentDimension()
244 {
245 logger.Debug("entering TestGAMSSymbol | testCopySymbolSameDBDifferentDimension");
246
247 String dbName = "testCopySymbolSameDBDifferentDimension";
248 db = globalWS.AddDatabase(dbName);
249
250 Assert.Throws<GAMSException>(() => db.GetSet("plants"));
251 // GAMSSet target = db.AddSet("target", i.Dim + 1, i.Text);
252 // i.CopySymbol(target);
253
254 logger.Debug("exiting TestGAMSSymbol | testCopySymbolSameDBDifferentDimension");
255 }
256
257 [Test]
258 public void testCopySymbolSameDBDifferentType()
259 {
260 logger.Debug("entering TestGAMSSymbol | testCopySymbolSameDBDifferentType");
261
262 String dbName = "testCopySymbolSameDBDifferentType";
263 db = globalWS.AddDatabase(dbName);
264
265 Assert.Throws<GAMSException>(() => db.GetSet("plants"));
266
267 logger.Debug("exiting TestGAMSSymbol | testCopySymbolSameDBDifferentType");
268 }
269
270 [Test]
271 public void testCopySymbolDifferentDBDifferentWS()
272 {
273 logger.Debug("entering TestGAMSSymbol | testCopySymbolDifferentDBDifferentWS");
274
275 String dbName = "testCopySymbolDifferentDBDifferentWS";
276 db = globalWS.AddDatabase(globalDB, dbName);
277
278 GAMSSet i = globalDB.GetSet("plants");
279 GAMSSet ii = db.AddSet("newPlants", i.Dim, "new canning plants");
280 i.CopySymbol(ii);
281
282 Assert.AreEqual(3, ii.NumberRecords, "expect set[" + ii.Name + "] with 3 records.");
283 Assert.AreEqual(1, ii.Dim, "expect set[" + ii.Name + "] with 1 dimension.");
284
285 GAMSSetRecord rec = ii.FirstRecord();
286 Assert.AreEqual(1, rec.Keys.Length, "expect last record with 1 key.");
287 Assert.AreEqual("Seattle", rec.Keys[0], "expect first record with name [Seattle]");
288
289 rec = ii.LastRecord();
290 Assert.AreEqual(1, rec.Keys.Length, "expect last record with 1 key.");
291 Assert.AreEqual("San-Diego", rec.Keys[0], "expect last record with name [San-Diego]");
292
293 logger.Debug("exiting TestGAMSSymbol | testCopySymbolDifferentDBDifferentWS");
294 }
295
296 [Test]
297 public void testAddRecord()
298 {
299 logger.Debug("entering TestGAMSSymbol | testAddRecord");
300
301 db = globalWS.AddDatabase(globalDB);
302 GAMSSet j = db.GetSet("markets");
303 int oldNumberRecords = j.NumberRecords;
304
305 GAMSSetRecord rec = j.AddRecord("Alburquerque");
306 Assert.AreEqual(1, rec.Keys.Length, "expect last record with 1 key dimension.");
307 Assert.AreEqual(oldNumberRecords + 1, j.NumberRecords,
308 "expect set j with [" + (oldNumberRecords + 1) + "] records.");
309
310 logger.Debug("exiting TestGAMSSymbol | testAddRecord");
311 }
312
313 [Test]
314 public void testAddRecordEmptyKey()
315 {
316 logger.Debug("entering TestGAMSSymbol | testAddRecordEmptyKey");
317
318 db = globalWS.AddDatabase(globalDB);
319 GAMSSet j = db.GetSet("markets");
320 int oldNumberRecords = j.NumberRecords;
321
322 GAMSSetRecord rec = j.AddRecord("");
323 Assert.AreEqual(1, rec.Keys.Length, "expect last record with 1 key dimension.");
324 Assert.AreEqual(oldNumberRecords + 1, j.NumberRecords,
325 "expect set j with [" + (oldNumberRecords + 1) + "] records.");
326
327 logger.Debug("exiting TestGAMSSymbol | testAddRecordEmptyKey");
328 }
329
330 [Test]
331 public void testAddRecordNullKey()
332 {
333 logger.Debug("entering TestGAMSSymbol | testAddRecordNullKey");
334
335 db = globalWS.AddDatabase(globalDB);
336 GAMSSet j = db.GetSet("markets");
337
338 String[] keys = { null };
339 Assert.Throws<GAMSException>(() => j.AddRecord(keys));
340
341 logger.Debug("exiting TestGAMSSymbol | testAddRecordNullKey");
342 }
343
344 [Test]
345 public void testDeleteRecord1()
346 {
347 logger.Debug("entering TestGAMSSymbol | testDeleteRecord1");
348
349 db = globalWS.AddDatabase(globalDB);
350 GAMSSet j = db.GetSet("markets");
351 int oldNumberRecords = j.NumberRecords;
352
353 Assert.That(j.DeleteRecord("Chicago"), "expect a sucessful delete.");
354 Assert.AreEqual((oldNumberRecords - 1), j.NumberRecords,
355 "expect symbol i with [" + (oldNumberRecords - 1) + "] records.");
356
357 logger.Debug("exiting TestGAMSSymbol | testDeleteRecord1");
358 }
359
360 [Test]
361 public void testDeleteRecord2()
362 {
363 logger.Debug("entering TestGAMSSymbol | testDeleteRecord2");
364
365 db = globalWS.AddDatabase(globalDB);
366 GAMSSet j = db.GetSet("markets");
367
368 Assert.That(j.DeleteRecord("Chicago"), "expect a successful delete");
369
370 Assert.NotNull(globalDB.GetSet("markets").FindRecord("Chicago"),
371 "expect [Chicago] in markets of globalDB.");
372 Assert.NotNull(j.FindRecord("Topeka"), "expect [Topeka] in markets of db.");
373
374 logger.Debug("exiting TestGAMSSymbol | testDeleteRecord2");
375 }
376
377 [Test]
378 public void testDeleteRecord3()
379 {
380 logger.Debug("entering TestGAMSSymbol | testDeleteRecord3");
381
382 db = globalWS.AddDatabase(globalDB);
383 GAMSParameter demand = db.GetParameter("demand");
384
385 Assert.That(demand.DeleteRecord("Topeka"), "expect a successful delete.");
386 Assert.NotNull(globalDB.GetParameter("demand").FirstRecord("Topeka"),
387 "expect [Topeka] in demand of globalDB.");
388 Assert.NotNull(demand.FindRecord("Chicago"), "expect [Chicago] in demand of db.");
389
390 logger.Debug("exiting TestGAMSSymbol | testDeleteRecord3");
391 }
392
393 [Test]
394 public void testDeleteRecordDifferentDimensions()
395 {
396 logger.Debug("entering TestGAMSSymbol | testDeleteRecordDifferentDimensions");
397
398 db = globalWS.AddDatabase(globalDB);
399 GAMSSet j = db.GetSet("markets");
400
401 Assert.Throws<GAMSException>(() => j.DeleteRecord("Seattle", "Chicago"));
402
403 logger.Debug("exiting TestGAMSSymbol | testDeleteRecordDifferentDimensions");
404 }
405
406 [Test]
407 public void testDeleteRecordNullKey()
408 {
409 logger.Debug("entering TestGAMSSymbol | testDeleteRecordNullKey");
410
411 db = globalWS.AddDatabase(globalDB);
412 GAMSSet j = db.GetSet("markets");
413
414 String[] keys = { null };
415 Assert.Throws<GAMSException>(() => j.DeleteRecord(keys));
416
417 logger.Debug("exiting TestGAMSSymbol | testDeleteRecordNullKey");
418 }
419
420 [Test]
421 public void testDeleteRecordEmptyKey1()
422 {
423 logger.Debug("entering TestGAMSSymbol | testDeleteRecordEmptyKey1");
424
425 db = globalWS.AddDatabase(globalDB);
426 GAMSSet j = db.GetSet("markets");
427 int oldNumberRecords = j.NumberRecords;
428
429 String[] keys = { "" };
430 Assert.False(j.DeleteRecord(keys), "expect an unsucessful delete.");
431 Assert.AreEqual(oldNumberRecords, j.NumberRecords, "expect set j with [" + oldNumberRecords + "] records.");
432
433 logger.Debug("exiting TestGAMSSymbol | testDeleteRecordEmptyKey1");
434 }
435
436 [Test]
437 public void testDeleteRecordEmptyKey2()
438 {
439 logger.Debug("entering TestGAMSSymbol | testDeleteRecordEmptyKey2");
440
441 db = globalWS.AddDatabase(globalDB);
442 GAMSSet j = db.GetSet("markets");
443 int oldNumberRecords = j.NumberRecords;
444 GAMSSetRecord rec = j.AddRecord("");
445 Assert.NotNull(rec, "do not exepect a NULL set record");
446 Assert.AreEqual(oldNumberRecords + 1, j.NumberRecords, "expect set j with [" + oldNumberRecords + "] records.");
447
448 String[] keys = { "" };
449 Assert.That(j.DeleteRecord(keys), "expect an sucessful delete.");
450 Assert.AreEqual(oldNumberRecords, j.NumberRecords, "expect set j with [" + oldNumberRecords + "] records.");
451
452 logger.Debug("exiting TestGAMSSymbol | testDeleteRecordEmptyKey2");
453 }
454
455 [Test]
456 public void testDeleteRecordNonExistKey()
457 {
458 logger.Debug("entering TestGAMSSymbol | testDeleteRecordNonExistKey");
459
460 db = globalWS.AddDatabase(globalDB);
461 GAMSSet j = db.GetSet("markets");
462 int oldNumberRecords = j.NumberRecords;
463 String[] keys = { "ThisKeyProbablyDoesNotExistEver" };
464
465 Assert.False(j.DeleteRecord(keys), "expect an unsucessful delete.");
466 Assert.AreEqual(oldNumberRecords, j.NumberRecords, "expect set j with ["+(oldNumberRecords)+"] records.");
467
468 logger.Debug("exiting TestGAMSSymbol | testDeleteRecordNonExistKey");
469 }
470
471 [Test]
472 public void testFindNullRecord()
473 {
474 logger.Debug("entering TestGAMSSymbol | testFindNullRecord");
475
476 db = globalWS.AddDatabase(globalDB);
477 GAMSParameter c = db.GetParameter("capacity");
478
479 String[] keys = { null, null };
480 Assert.Throws<GAMSException>(() => c.FindRecord(keys));
481
482 logger.Debug("exiting TestGAMSSymbol | testFindNullRecord");
483 }
484
485 [Test]
486 public void testFindRecordDifferentDimension1()
487 {
488 logger.Debug("entering TestGAMSSymbol | testFindRecordDifferentDimension1");
489
490 db = globalWS.AddDatabase(globalDB);
491 GAMSSet j = db.GetSet("markets");
492
493 Assert.Throws<GAMSException>(() => j.FindRecord("San-Diego", "New-York"));
494
495 logger.Debug("exiting TestGAMSSymbol | testFindRecordDifferentDimension1");
496 }
497
498 [Test]
499 public void testFindRecordDifferentDimension2()
500 {
501 logger.Debug("entering TestGAMSSymbol | testFindRecordDifferentDimension2");
502
503 db = globalWS.AddDatabase(globalDB);
504 GAMSSet j = db.GetSet("markets");
505 Assert.Throws<GAMSException>(() => j.FindRecord());
506
507 logger.Debug("exiting TestGAMSSymbol | testFindRecordDifferentDimension2");
508 }
509
510 [Test]
511 public void testFindRecordNonExistKey()
512 {
513 logger.Debug("entering TestGAMSSymbol | testFindRecordNonExistKey");
514
515 db = globalWS.AddDatabase(globalDB);
516 GAMSSet j = db.GetSet("markets");
517 j.DeleteRecord("Chicago");
518
519 Assert.Throws<GAMSException>(() => j.FindRecord("Chicago"));
520
521 logger.Debug("exiting TestGAMSSymbol | testFindRecordNonExistKey");
522 }
523
524 [Test]
525 public void testFindRecord()
526 {
527 logger.Debug("entering TestGAMSSymbol | testFindRecord");
528
529 db = globalWS.AddDatabase(globalDB);
530 GAMSSet j = db.GetSet("markets");
531 j.AddRecord("Alburquerque");
532
533 GAMSSetRecord rec = j.FindRecord("Alburquerque");
534 Assert.NotNull(rec, "do not expect a NULL set record");
535
536 logger.Debug("exiting TestGAMSSymbol | testFindRecord");
537 }
538
539 [Test]
540 public void testFindRecordEmptyKey()
541 {
542 logger.Debug("entering TestGAMSSymbol | testFindRecordEmptyKey");
543
544 db = globalWS.AddDatabase(globalDB);
545 GAMSSet j = db.GetSet("markets");
546 int oldNumberRecords = j.NumberRecords;
547 GAMSSetRecord rec = j.AddRecord("");
548
549 GAMSSetRecord findRec = j.FindRecord("");
550 Assert.NotNull(findRec, "do not expect a NULL set record");
551 Assert.AreEqual(1, rec.Keys.Length, "expect last record with 1 key dimension.");
552 Assert.AreEqual(oldNumberRecords + 1, j.NumberRecords, "expect set j with [" + (oldNumberRecords + 1) + "] records.");
553
554
555 logger.Debug("exiting TestGAMSSymbol | testFindRecordEmptyKey");
556 }
557
558 [Test]
559 public void testFindRecordNullKey1()
560 {
561 logger.Debug("entering TestGAMSSymbol | testFindRecordNullKey1");
562
563 db = globalWS.AddDatabase(globalDB);
564 GAMSSet j = db.GetSet("markets");
565 int numberRecords = j.NumberRecords;
566
567 String[] keys = { null };
568 Assert.Throws<GAMSException>(() => j.FindRecord(keys), "do not expect to find a record with null key");
569 //Assert.AreEqual(numberRecords, j.NumberRecords, "expect the same number of records.");
570
571 logger.Debug("exiting TestGAMSSymbol | testFindRecordNullKey1");
572 }
573
574 [Test]
575 public void testFindRecordNullKey2()
576 {
577 logger.Debug("entering TestGAMSSymbol | testFindRecordNullKey2");
578
579 db = globalWS.AddDatabase(globalDB);
580 GAMSSet j = db.GetSet("markets");
581
582 String[] keys = { null };
583 Assert.Throws<GAMSException>(() => j.FindRecord(keys));
584
585 logger.Debug("exiting TestGAMSSymbol | testFindRecordNullKey2");
586 }
587
588 [Test]
589 public void testMergeRecordNullKey()
590 {
591 logger.Debug("entering TestGAMSSymbol | testMergeRecordNullKey");
592
593 db = globalWS.AddDatabase(globalDB);
594 GAMSSet j = db.GetSet("markets");
595
596 String[] keys = { null };
597 Assert.Throws<GAMSException>(() => j.MergeRecord(keys));
598
599 logger.Debug("exiting TestGAMSSymbol | testMergeRecordNullKey");
600 }
601
602 [Test]
603 public void testMergeRecordEmptyKey()
604 {
605 logger.Debug("entering TestGAMSSymbol | testMergeRecordEmptyKey");
606
607 db = globalWS.AddDatabase(globalDB);
608 GAMSSet i = db.GetSet("plants");
609 int numberRecords = i.NumberRecords;
610 String[] keys = { "" };
611
612 i.MergeRecord(keys);
613 Assert.AreEqual(numberRecords + 1, i.NumberRecords, "expect equal number of records");
614
615 logger.Debug("exiting TestGAMSSymbol | testMergeRecordEmptyKey");
616 }
617
618 [Test]
619 public void testMergeRecord()
620 {
621 logger.Debug("entering TestGAMSSymbol | testMergeRecord");
622
623 db = globalWS.AddDatabase(globalDB);
624 String plantName = "Alburquerque";
625 GAMSSet i = db.GetSet("plants");
626
627 i.MergeRecord(plantName);
628 Assert.NotNull(i.FindRecord(plantName), "do not exepect a NULL set record");
629
630 logger.Debug("exiting TestGAMSSymbol | testMergeRecord");
631 }
632
633 [Test]
634 public void testMergeRecordExistKey()
635 {
636 logger.Debug("entering TestGAMSSymbol | testMergeRecordExistKey");
637
638 db = globalWS.AddDatabase(globalDB);
639 String plantName = "Alburquerque";
640 GAMSSet i = db.GetSet("plants");
641 i.AddRecord(plantName);
642 int oldNumberRecords = i.NumberRecords;
643
644 i.MergeRecord(plantName);
645
646 Assert.NotNull(i.FindRecord(plantName), "do not exepect a NULL set record");
647 Assert.AreEqual(oldNumberRecords, i.NumberRecords, "expect the same number of records.");
648
649 logger.Debug("exiting TestGAMSSymbol | testMergeRecordExistKey");
650 }
651
652 [Test]
653 public void testMergeRecordNonExistKey()
654 {
655 logger.Debug("entering TestGAMSSymbol | testMergeRecordNonExistKey");
656
657 db = globalWS.AddDatabase(globalDB);
658 GAMSSet j = db.GetSet("markets");
659 int oldNumberRecords = j.NumberRecords;
660
661 j.MergeRecord("ThisKeyProbablyDoesNotExist");
662 Assert.NotNull(j.FindRecord("ThisKeyProbablyDoesNotExist"), "do not exepect a NULL set record");
663 Assert.AreEqual(oldNumberRecords + 1, j.NumberRecords, "do not expect the same number of records.");
664
665 logger.Debug("exiting TestGAMSSymbol | testMergeRecordNonExistKey");
666 }
667
668 [Test, Timeout(20000)]
669 [Ignore("heavy load")]
670 public void testAddOneMillionSetRecords()
671 {
672 logger.Debug("entering TestGAMSSymbol | testAddOneMillionSetRecords");
673
674 db = globalWS.AddDatabase();
675 Assert.NotNull(db, "expect a successful creation of GAMSDatabase.");
676
677 GAMSSet set = db.AddSet("set", 1);
678 Assert.AreEqual(1, set.Dim, "expect a set with 1 dimension.");
679 Assert.AreEqual(1, db.NrSymbols, "textexpect 1 symbol in database.");
680 Assert.AreEqual(0, set.NumberRecords, "expect symbol without a record.");
681
682 for (int i = 0; i < 1000000; i++)
683 set.AddRecord(i.ToString());
684
685 Assert.AreEqual(1000000, set.NumberRecords, "expect 1,000,000 records in set.");
686 int count = 0;
687 foreach (GAMSSetRecord rec in set)
688 {
689 Assert.NotNull(rec, "do not expect a NULL symbol record instance.");
690 Assert.That(rec is GAMSSetRecord, "expect a GAMSSetRecord instance.");
691 Assert.AreEqual("set", set.Name, "expect [set] as set name.");
692 count++;
693 }
694 Assert.AreEqual(1000000, count, "expect symbol with 1,000,000 records.");
695 Assert.AreEqual(1, db.NrSymbols, "expect 1 symbol in database.");
696
697 logger.Debug("exiting TestGAMSSymbol | testAddOneMillionSetRecords");
698 }
699
700 [Test]
701 public void testAddGAMSEquationDefaultValue()
702 {
703 logger.Debug("entering TestGAMSSymbol | testAddGAMSEquationDefaultValue");
704
705 db = globalWS.AddDatabase(globalDB);
706 GAMSEquation x = db.AddEquation("x", 1, EquType.E);
707
708 Assert.AreEqual(1, x.Dim, "expect 1 dimension for equation [x].");
709 Assert.AreEqual(EquType.E, x.EquType, "expect x with [" + EquType.E.ToString() + "] equation type.");
710
711 logger.Debug("exiting TestGAMSSymbol | testAddGAMSEquationDefaultValue");
712 }
713
714 [Test]
715 public void testAddGAMSVariableDefaultValue()
716 {
717 logger.Debug("entering TestGAMSSymbol | testAddGAMSVariableDefaultValue");
718
719 db = globalWS.AddDatabase(globalDB);
720 GAMSVariable x = db.AddVariable("x", 2, VarType.Positive);
721
722 Assert.AreEqual(2, x.Dim, "expect 2 dimensions for equation [x].");
723 Assert.AreEqual(VarType.Positive, x.VarType, "expect x with [" + VarType.Positive.ToString() + "] equation type.");
724
725 logger.Debug("exiting TestGAMSSymbol | testAddGAMSVariableDefaultValue");
726 }
727
728 [Test]
729 public void testGetGAMSEquationEquTypeValues()
730 {
731 logger.Debug("entering TestGAMSSymbol | testGetGAMSEquationEquTypeValues");
732
733 String testDir = HouseKeeper.gamsWorkingDir + Path.DirectorySeparatorChar +
734 "testGetGAMSEquationValues";
735 HouseKeeper.prepare(testDir);
736 ws = new GAMSWorkspace(testDir, HouseKeeper.gamsSystemDir);
737 db = ws.AddDatabase("testGAMSEquationValues");
738
739 GAMSJob job = ws.AddJobFromGamsLib("trnsport");
740 job.Run();
741
742 GAMSEquation supply = job.OutDB.GetEquation("supply");
743 Assert.AreEqual(EquType.L, supply.EquType, "expect supply with [" + EquType.L.ToString() + "] equation type.");
744 Assert.AreEqual(2, supply.NumberRecords, "expect 2 records for equation [supply].");
745 Assert.AreEqual(1, supply.Dim, "expect 1 dimension for equation [supply].");
746
747 GAMSEquationRecord rec = supply.FirstRecord();
748 Assert.AreEqual("seattle", rec.Keys[0], "expect key [seattle] for first record of equation [supply].");
749 Assert.AreEqual(350.0, rec.Level, "expect value for first equation record [supply.seattle].");
750 rec = supply.LastRecord();
751 Assert.AreEqual("san-diego", rec.Keys[0], "expect key [san-diego] for first record of equation [supply].");
752 Assert.AreEqual(550.0, rec.Level, "expect value for first equation record [supply.san-diego].");
753
754 GAMSEquation cost = job.OutDB.GetEquation("cost");
755 Assert.AreEqual(EquType.E, cost.EquType, "expect cost with [" + EquType.E.ToString() + "] equation type.");
756 Assert.AreEqual(1, cost.NumberRecords, "expect 1 records for equation [cost].");
757 Assert.AreEqual(0, cost.Dim, "expect 0 dimension for equation [cost].");
758
759 if (job != null)
760 {
761 if (job.OutDB != null)
762 job.OutDB.Dispose();
763 }
764
765 logger.Debug("exiting TestGAMSSymbol | testGetGAMSEquationEquTypeValues");
766 }
767
768 [Test]
769 public void testGetGAMSVarialbeVarTypeValues()
770 {
771 logger.Debug("entering TestGAMSSymbol | testGetGAMSVarialbeVarTypeValues");
772
773 String testDir = HouseKeeper.gamsWorkingDir + Path.DirectorySeparatorChar +
774 "testGetGAMSEquationValues";
775 HouseKeeper.prepare(testDir);
776 ws = new GAMSWorkspace(testDir, HouseKeeper.gamsSystemDir);
777 db = ws.AddDatabase("testGAMSEquationValues");
778
779 GAMSJob job = ws.AddJobFromGamsLib("trnsport");
780 job.Run();
781
782 GAMSVariable x = job.OutDB.GetVariable("x");
783 Assert.AreEqual(VarType.Positive, x.VarType, "expect x with [" + VarType.Positive + "] variable type.");
784 Assert.AreEqual(2, x.Dim, "expect 2 dimensions for variable [x].");
785 Assert.AreEqual(6, x.NumberRecords, "expect 6 records for variable [x].");
786
787 GAMSVariableRecord rec = x.FindRecord("seattle", "chicago");
788 Assert.NotNull(rec, "expect to find a record with keys(\"seattle\",\"chicago\") from variable [x].");
789 Assert.That((rec.Level - 300) <= gamsglobals.sv_eps,
790 "expect [300] as the value [z] with keys keys(\"seattle\",\"chicago\").");
791
792 GAMSVariable z = job.OutDB.GetVariable("z");
793 Assert.AreEqual(VarType.Free, z.VarType, "expect x with [" + VarType.Free + "] variable type.");
794 Assert.AreEqual(0, z.Dim, "expect 0 dimension for variable [z].");
795 Assert.AreEqual(1, z.NumberRecords, "expect 1 records for variable [z].");
796 Assert.That(z.FirstRecord().Level - 153.675 <= gamsglobals.sv_eps);
797
798 if (job != null)
799 {
800 if (job.OutDB != null)
801 job.OutDB.Dispose();
802 }
803
804 logger.Debug("exiting TestGAMSSymbol | testGetGAMSVarialbeVarTypeValues");
805 }
806
807 [Test]
808 public void testEquivalentSymbol()
809 {
810 logger.Debug("Entering TestGAMSSymbol | testEquivalentSymbol");
811 // given
812 String testDir = HouseKeeper.gamsWorkingDir + Path.DirectorySeparatorChar + "testEquivalentSymbol";
813 HouseKeeper.prepare(testDir);
814 GAMSWorkspace ws = new GAMSWorkspace(testDir, HouseKeeper.gamsSystemDir, DebugLevel.Off);
815
816 GAMSJob job = ws.AddJobFromGamsLib("trnsport");
817 job.Run();
818 GAMSDatabase db = job.OutDB;
819 int numberOfSymbols = db.NrSymbols;
820
821 { // given
822 GAMSSet j = db.GetSet("j");
823 GAMSSymbol symbol_j = j;
824 // when, then
825 Assert.True(symbol_j.Equals(j), "expect symbol_j and j to be equivalent");
826 Assert.True(symbol_j.Equals(db.GetSet("j")), "expect symbol_j and db.GetSet(\"j\") to be equivalent");
827
828 Assert.False(symbol_j.Equals(db.GetSet("i")), "symbol_j and db.GetSet(\"i\") are not equivalent");
829 Assert.False(symbol_j.Equals(db.GetParameter("a")), "symbol_j and db.GetParameter(\"a\") are not equivalent");
830 Assert.False(symbol_j.Equals(db.GetVariable("x")), "symbol_j and db.GetVariable(\"x\") are not equivalent");
831 Assert.False(symbol_j.Equals(db.GetEquation("cost")), "symbol_j and db.GetEquation(\"cost\") are not equivalent");
832 }
833 { // given
834 GAMSParameter a = db.GetParameter("a");
835 GAMSSymbol symbol_a = a;
836 // when, then
837 Assert.True(symbol_a.Equals(a), "expect symbol_a and a to be equivalent");
838 Assert.True(symbol_a.Equals(db.GetParameter("a")), "expect symbol_a and db.GetParameter(\"a\") to be equivalent");
839
840 Assert.False(symbol_a.Equals(db.GetSet("i")), "symbol_a and db.GetSet(\"i\") are not equivalent");
841 Assert.False(symbol_a.Equals(db.GetParameter("b")), "symbol_a and db.GetParameter(\"b\") are not equivalent");
842 Assert.False(symbol_a.Equals(db.GetVariable("x")), "symbol_a and db.getVariable(\"x\") are not equivalent");
843 Assert.False(symbol_a.Equals(db.GetEquation("cost")), "symbol_a and db.GetEquation(\"cost\") are not equivalent");
844 }
845 { // given
846 GAMSVariable z = db.GetVariable("z");
847 GAMSSymbol symbol_z = z;
848 // when, then
849 Assert.True(symbol_z.Equals(z), "expect symbol_z and z to be equivalent");
850 Assert.True(symbol_z.Equals(db.GetVariable("z")), "expect symbol_z and db.getVariable(\"z\") to be equivalent");
851
852 Assert.False(symbol_z.Equals(db.GetSet("i")), "symbol_z and db.GetSet(\"i\") are not equivalent");
853 Assert.False(symbol_z.Equals(db.GetParameter("a")), "symbol_z and db.GetParameter(\"a\") are not equivalent");
854 Assert.False(symbol_z.Equals(db.GetVariable("x")), "symbol_z and db.getVariable(\"x\") are not equivalent");
855 Assert.False(symbol_z.Equals(db.GetEquation("cost")), "symbol_z and db.GetEquation(\"cost\") are not equivalent");
856 }
857 { // given
858 GAMSVariable x1 = db.GetVariable("x");
859 GAMSVariable x2 = db.GetVariable("x");
860 GAMSVariable x3 = x1;
861 // when, then
862 Assert.True(x1.Equals(x2), "expect x1 and x2 to be equivalent");
864 Assert.True(x1.Equals(x3), "expect x1 and x3 to be equivalent");
865 Assert.True(x1 == x3, "expect x1 and x3 with the same memory reference");
866 }
867 Assert.AreEqual(db.NrSymbols, numberOfSymbols, "expect number of records in database remains the same after a few queries");
868 logger.Debug("Exiting TestGAMSSymbol | testEquivalentSymbol");
869 }
870
871 [Test]
872 public void testEquivalentSymbol_IteratingSymbol()
873 {
874 logger.Debug("Entering TestGAMSSymbol | testEquivalentSymbol_IteratingSymbol");
875
876 // given
877 String testDir = HouseKeeper.gamsWorkingDir + Path.DirectorySeparatorChar + "testEquivalentSymbol_IteratingSymbol";
878 HouseKeeper.prepare(testDir);
879 GAMSWorkspace ws = new GAMSWorkspace(testDir, HouseKeeper.gamsSystemDir, DebugLevel.Off);
880
881 GAMSJob job = ws.AddJobFromGamsLib("trnsport");
882 job.Run();
883 GAMSDatabase db = job.OutDB;
884
885 // when
886 GAMSSet i = db.GetSet("i");
887 GAMSParameter b = db.GetParameter("b");
888 GAMSParameter d = db.GetParameter("d");
889 GAMSParameter f = db.GetParameter("f");
890 GAMSVariable x = db.GetVariable("x");
891 GAMSEquation supply = db.GetEquation("supply");
892 // then
893
894 foreach (GAMSSymbol symbol in db)
895 {
896 if (symbol.Name.Equals("i"))
897 {
898 GAMSSetRecord rec = ((GAMSSet)symbol).FirstRecord();
899 rec.Text = "new explanatory text for i";
900 Assert.True(symbol.Equals(i), "expect iterating symbol and i to be equivalent");
901 Assert.True(rec.Text.Equals(i.FirstRecord().Text), "expect iterating symbol and i to be equivalent");
902 }
903 else if (symbol.Name.Equals("b"))
904 {
905 GAMSParameterRecord rec = ((GAMSParameter)symbol).FirstRecord();
906 rec.Value = 0.123;
907 Assert.True(symbol.Equals(b), "expect iterating symbol and b to be equivalent");
908 Assert.True(rec.Value == b.FirstRecord().Value, "expect iterating symbol and b to be equivalent");
909 }
910 else if (symbol.Name.Equals("d"))
911 {
912 GAMSParameterRecord rec = ((GAMSParameter)symbol).LastRecord();
913 rec.Value = 1.234;
914 Assert.True(symbol.Equals(d), "expect iterating symbol and d to be equivalent");
915 Assert.True(rec.Value == d.LastRecord().Value, "expect iterating symbol and d to be equivalent");
916 }
917 else if (symbol.Name.Equals("f"))
918 {
919 GAMSParameterRecord rec = ((GAMSParameter)symbol).FirstRecord();
920 rec.Value = 12.34;
921 Assert.True(symbol.Equals(f), "expect iterating symbol and f to be equivalent");
922 Assert.True(rec.Value == f.FirstRecord().Value, "expect iterating symbol and d to be equivalent");
923 }
924 else if (symbol.Name.Equals("x"))
925 {
926 GAMSVariableRecord rec = ((GAMSVariable)symbol).FirstRecord();
927 rec.Level = 123.4;
928 Assert.True(symbol.Equals(x), "expect iterating symbol and x to be equivalent");
929 Assert.True(rec.Level == x.FirstRecord().Level, "expect iterating symbol and x to be equivalent");
930 }
931 else if (symbol.Name.Equals("supply"))
932 {
933 GAMSEquationRecord rec = ((GAMSEquation)symbol).FirstRecord();
934 rec.Level = 1234.0;
935 Assert.True(symbol.Equals(supply), "expect iterating symbol and supply to be equivalent");
936 Assert.True(rec.Level == supply.FirstRecord().Level, "expect iterating symbol and supply to be equivalent");
937 }
938 }
939 logger.Debug("Exiting TestGAMSSymbol | testEquivalentSymbol_IteratingSymbol");
940 }
941
942 [Test]
943 public void testEquivalentSymbol_CopiedSymbol()
944 {
945 logger.Debug("Entering TestGAMSSymbol | testEquivalentSymbol_CopiedSymbol");
946
947 // given
948 String testDir = HouseKeeper.gamsWorkingDir + Path.DirectorySeparatorChar + "testEquivalentSymbol_CopiedSymbol";
949 HouseKeeper.prepare(testDir);
950 GAMSWorkspace ws = new GAMSWorkspace(testDir, HouseKeeper.gamsSystemDir, DebugLevel.Off);
951
952 GAMSJob job = ws.AddJobFromGamsLib("trnsport");
953 job.Run();
954 GAMSDatabase db = job.OutDB;
955
956 // when
957 db.GetSet("j").CopySymbol(db.GetSet("i"));
958 // then
959 Assert.True(db.GetSet("i").NumberRecords == 3);
960 Assert.True(db.GetSet("i").NumberRecords == db.GetSet("j").NumberRecords);
961 Assert.False(db.GetSet("i").Equals(db.GetSet("j")), "i and j are not equivalent");
962
963 // when
964 db.GetParameter("a").CopySymbol(db.GetParameter("b"));
965 // then
966 Assert.True(db.GetParameter("b").NumberRecords == 2);
967 Assert.True(db.GetParameter("b").NumberRecords == db.GetParameter("a").NumberRecords);
968 Assert.False(db.GetParameter("b").Equals(db.GetParameter("a")), "a and b are not equivalent");
969
970 // when
971 db.GetEquation("supply").CopySymbol(db.GetEquation("demand"));
972 // then
973 Assert.True(db.GetEquation("demand").NumberRecords == 2);
974 Assert.True(db.GetEquation("demand").NumberRecords == db.GetEquation("supply").NumberRecords);
975 Assert.False(db.GetEquation("demand").Equals(db.GetEquation("supply")), "supply and demand are not equivalent");
976
977 logger.Debug("Exiting TestGAMSSymbol | testEquivalentSymbol_CopiedSymbol");
978 }
979
980 [Test]
981 public void testEquivalentSymbol_SameSymbolFromOriginalDatabase()
982 {
983 logger.Debug("Entering TestGAMSSymbol | testEquivalentSymbol_SameSymbolFromOriginalDatabase");
984 // given
985 String testDir = HouseKeeper.gamsWorkingDir + Path.DirectorySeparatorChar + "testEquivalentSymbol_SameSymbolFromOriginalDatabase";
986 HouseKeeper.prepare(testDir);
987 GAMSWorkspace ws = new GAMSWorkspace(testDir, HouseKeeper.gamsSystemDir, DebugLevel.Off);
988
989 GAMSJob job = ws.AddJobFromGamsLib("trnsport");
990 job.Run();
991 GAMSDatabase db1 = job.OutDB;
992 GAMSDatabase db2 = ws.AddDatabase(db1);
993
994 // when
995 GAMSSet i1 = db1.GetSet("i");
996 GAMSSet i2 = db2.GetSet("i");
997 // then
998 Assert.False(i1.Equals(i2), "sets with same name from different databases are not equivalent");
999
1000 // when
1001 GAMSSymbol j1 = db1.GetSet("j");
1002 GAMSSymbol j2 = db2.GetSet("j");
1003 // then
1004 Assert.False(j1.Equals(j2), "sets with same name from different databases are not equivalent");
1005
1006 // when
1007 GAMSParameter b1 = db1.GetParameter("b");
1008 GAMSParameter b2 = db2.GetParameter("b");
1009 // then
1010 Assert.False(b1.Equals(b2), "parameters with same name from different databases are not equivalent");
1011
1012 // when
1013 GAMSVariable x1 = db1.GetVariable("x");
1014 GAMSVariable x2 = db2.GetVariable("x");
1015 // then
1016 Assert.False(x1.Equals(x2), "variables with same name from different databases are not equivalent");
1017
1018 // when
1019 GAMSEquation d1 = db1.GetEquation("demand");
1020 GAMSEquation d2 = db2.GetEquation("demand");
1021 // then
1022 Assert.False(d1.Equals(d2), "equations with same name from different databases are not equivalent");
1023
1024 // release resources hold by db1 and db2
1025 db1.Dispose();
1026 db2.Dispose();
1027 logger.Debug("Exiting TestGAMSSymbol | testEquivalentSymbol_SameSymbolFromOriginalDatabase");
1028 }
1029 }
1030}
1031
GAMSVariable GetVariable(string variableIdentifier)
GAMSSet AddSet(string identifier, int dimension, string explanatoryText="", SetType setType=SetType.multi)
GAMSParameter GetParameter(string parameterIdentifier)
GAMSSet GetSet(string setIdentifier)
GAMSVariable AddVariable(string identifier, int dimension, VarType varType, string explanatoryText="")
GAMSEquation GetEquation(string equationIdentifier)
GAMSEquation AddEquation(string identifier, int dimension, EquType equType, string explanatoryText="")
new GAMSEquationRecord FirstRecord()
new GAMSEquationRecord LastRecord()
GAMSDatabase OutDB
void Run(GAMSOptions gamsOptions=null, GAMSCheckpoint checkpoint=null, TextWriter output=null, Boolean createOutDB=true)
new GAMSParameterRecord LastRecord()
new GAMSParameterRecord FindRecord(params string[] keys)
new GAMSParameterRecord FirstRecord()
new GAMSSetRecord AddRecord(params string[] keys)
new GAMSSetRecord LastRecord()
new GAMSSetRecord FirstRecord()
new GAMSSetRecord MergeRecord(params string[] keys)
new GAMSSetRecord FindRecord(params string[] keys)
bool DeleteRecord(params string[] keys)
void CopySymbol(GAMSSymbol target)
override bool Equals(object obj)
new GAMSVariableRecord FindRecord(params string[] keys)
new GAMSVariableRecord FirstRecord()
GAMSJob AddJobFromGamsLib(string model, GAMSCheckpoint checkpoint=null, string jobName=null)
GAMSDatabase AddDatabase(string databaseName=null, string inModelName=null)
static void prepare(String dir)
Prepare directory by checking its existence. If exists, (non - recursively) delete all its contents,...
Definition: HouseKeeper.cs:30
static void initializeTestFrom(String filename, String subdir)
initialize class properties from fileName and prepare directory subdir
Definition: HouseKeeper.cs:114