Loading...
Searching...
No Matches
TestGAMSSymbolRecord.cs
1using System;
2using System.Collections.Generic;
3using System.IO;
4using System.Linq;
5using GAMS;
6using NLog;
7using NUnit.Framework;
8
9namespace GamsApiTests
10{
11 [TestFixture]
13 {
14
15 static readonly String PROPERTIES_FILE = "test.properties";
16 static String workingDir;
17 static GAMSWorkspaceInfo wsInfo = null;
18 static GAMSWorkspace ws = null;
19 static GAMSDatabase db = null;
20 static Logger logger = LogManager.GetCurrentClassLogger();
21
22 [OneTimeSetUp]
23 public static void OneTimeSetup()
24 {
25 logger.Debug("TestGAMSSymbolRecord | OneTimeSetup");
26 workingDir = HouseKeeper.gamsAbsoluteWorkingDir;
27 HouseKeeper.initializeTestFrom(PROPERTIES_FILE, "TestGAMSSymbolRecord");
28 logger.Debug("TestGAMSSymbolRecord | oneTimeSetup");
29 }
30
31 [SetUp]
32 public void SetUp()
33 {
34 ws = null;
35 db = null;
36 }
37
38 [TearDown]
39 public void TearDown()
40 {
41 if (db != null)
42 db.Dispose();
43 }
44
45 [Test]
46 public void TestDisposedSymbolRecord()
47 {
48 logger.Debug("Entering TestGAMSSymbolRecord | testDisposedSymbolRecord");
49 String testDir = HouseKeeper.gamsWorkingDir + Path.DirectorySeparatorChar + "testDisposedSymbolRecord";
50 HouseKeeper.prepare(testDir);
51 GAMSWorkspace ws = new GAMSWorkspace(testDir, HouseKeeper.gamsSystemDir, DebugLevel.Off);
52
53 GAMSJob job = ws.AddJobFromGamsLib("trnsport");
54 job.Run();
55 GAMSDatabase db = job.OutDB;
56
57 GAMSSymbol symbol = db.GetSet("i");
58 GAMSSymbolRecord record = symbol.FindRecord("seattle");
59 record.Key(0);
60 // when
62 record = null;
63 // then
64 Assert.Throws<NullReferenceException>(() => record.MoveNext());
65 }
66
67 [Test]
68 public void TestDisposedAllSymbolRecords()
69 {
70 logger.Debug("Entering TestGAMSSymbolRecord | testDisposedAllSymbolRecords");
71 String testDir = HouseKeeper.gamsWorkingDir + Path.DirectorySeparatorChar + "testDisposedAllSymbolRecords";
72 HouseKeeper.prepare(testDir);
73 GAMSWorkspace ws = new GAMSWorkspace(testDir, HouseKeeper.gamsSystemDir, DebugLevel.Off);
74
75 GAMSJob job = ws.AddJobFromGamsLib("trnsport");
76 job.Run();
77 GAMSDatabase db = job.OutDB;
78
79 foreach (GAMSSetRecord record in db.GetSet("j"))
80 {
81 // when
82 GAMSSetRecord record2 = record;
84 record2 = null;
85
86 try
87 {
88 // then
89 Console.WriteLine(record2.Text);
90 Assert.Fail("do not expect an operation on GAMSSetRecord after disposed");
91 }
92 catch (NullReferenceException e)
93 {
94 Assert.True(record2 == null, "expect GAMSSetRecord object to be null after disposed");
95 }
96 break;
97 }
98
99
100 foreach (GAMSParameterRecord record in db.GetParameter("b"))
101 {
102 // when
103 GAMSParameterRecord record2 = record;
105 record2 = null;
106
107 try
108 {
109 // then
110 record2.Key(0);
111 Assert.Fail("do not expect an operation on GAMSParameterRecord after disposed");
112 }
113 catch (NullReferenceException e)
114 {
115 Assert.True(record2 == null, "expect GAMSParameterRecord object to be null after disposed");
116
117 }
118 break;
119 }
120
121 {
122 GAMSVariable x = db.GetVariable("x");
123 GAMSVariableRecord seattle_topeka = x.FindRecord("seattle", "topeka");
124 GAMSVariableRecord record = x.LastRecord();
125 record.Key(0);
126 // when
128 record = null;
129 // then
130 try
131 {
132 Console.WriteLine(record.Level);
133 Assert.Fail("do not expect an operation on GAMSVariableRecord after disposed");
134 }
135 catch (NullReferenceException e)
136 {
137 Assert.True(record == null, "expect GAMSVariableRecord object to be null after disposed");
138 }
139 }
140 }
141
142 [Test]
143 public void TestOrderOfDisposedSymbolRecords()
144 {
145 logger.Debug("Entering TestGAMSSymbolRecord | testOrderOfDisposedSymbolRecords");
146 String testDir = HouseKeeper.gamsWorkingDir + Path.DirectorySeparatorChar + "testOrderOfDisposedSymbolRecords";
147 HouseKeeper.prepare(testDir);
148 GAMSWorkspace ws = new GAMSWorkspace(testDir, HouseKeeper.gamsSystemDir, DebugLevel.Off);
149
150 GAMSJob job = ws.AddJobFromGamsLib("trnsport");
151 job.Run();
152 GAMSDatabase db = job.OutDB;
153
154 GAMSParameter d = db.GetParameter("d");
156 rec1.MoveNext();
157
158 String[] keys1 = rec1.Keys;
159 double value1 = rec1.Value;
160
161 GAMSParameterRecord rec2 = rec1;
162 // when
164 rec2 = null;
165 rec1 = null;
166 // then
167 try
168 {
169 rec1.MoveNext();
170 Assert.Fail("do not expect an operation on GAMParameterRecord after disposed");
171 }
172 catch (NullReferenceException e)
173 {
174 Assert.True((rec1 == null && rec2 == null), "expect GAMSParameterRecord object to be null after disposed");
175 }
176
177 rec1 = d.FirstRecord();
178 rec1.MoveNext();
179
180
181 // then
182 Assert.True(value1 == rec1.Value, "expect equuals equauls??");
183 Assert.True(rec1.Key(0).Equals(keys1[0]), "expect equuals equauls??");
184 Assert.True(rec1.Key(1).Equals(keys1[1]), "expect equuals equauls??");
185
186 // then
187 try
188 {
189 rec2.MoveNext();
190 Assert.Fail("do not expect an operation on GAMParameterRecord after disposed");
191 }
192 catch (NullReferenceException e)
193 {
194 Assert.True((rec1 != null && rec2 == null), "expect GAMSParameterRecord object to be null after disposed");
195 }
196 }
197
198 [Test]
199 public void TestEquivalentSymbolRecord()
200 {
201 logger.Debug("Entering TestGAMSSymbolRecord | testEquivalentSymbolRecord");
202 String testDir = HouseKeeper.gamsWorkingDir + Path.DirectorySeparatorChar + "testEquivalentSymbolRecord";
203 HouseKeeper.prepare(testDir);
204 GAMSWorkspace ws = new GAMSWorkspace(testDir, HouseKeeper.gamsSystemDir, DebugLevel.Off);
205
206 GAMSJob job = ws.AddJobFromGamsLib("trnsport");
207 job.Run();
208 GAMSDatabase db = job.OutDB;
209
210 { // when
211 GAMSSymbol symbol = db.GetSet("i");
212 GAMSSymbolRecord rec1 = symbol.FirstRecord();
213 GAMSSymbolRecord rec2 = symbol.FirstRecord();
214 GAMSSymbolRecord rec3 = symbol.FindRecord("seattle");
215 // then
216 Assert.True(rec1.Equals(rec2), "expect rec1 and rec2 to be equivalent");
217 Assert.True(rec1.Equals(rec3), "expect rec1 and rec3 to be equivalent");
218 Assert.False(rec1 == rec2, "rec1 and rec2 are not the same C# object");
219 Assert.False(rec1 == rec3, "rec1 and rec3 are not the same C# object");
220 // when, then
221 GAMSSymbolRecord rec4 = symbol.LastRecord();
222 Assert.False(rec1.Equals(rec4), "rec1 and rec4 are not equivalent");
223 Assert.False(rec1 == rec4, "rec1 and rec4 are not the same C# object");
224 // when, then
225 GAMSSetRecord rec5 = db.GetSet("i").LastRecord();
226 Assert.True(rec5.Equals(rec4), "expect rec5 and rec4 to be equivalent");
227 Assert.False(rec5 == rec4, "rec5 and rec4 are not the same C# object");
228 // when, then
229 GAMSSetRecord rec6 = db.GetSet("i").FindRecord("seattle");
230 Assert.True(rec6.Equals(rec1), "expect rec6 and rec1 to be equivalent");
231 Assert.False(rec6 == rec1, "rec6 and rec1 are not the same C# object");
232 }
233 { // when
234 GAMSSetRecord rec1 = db.GetSet("i").FirstRecord();
235 GAMSSetRecord rec2 = db.GetSet("j").FirstRecord();
236 // then
237 Assert.False(rec1.Equals(rec2), "expect rec1 and rec2 to be equivalent");
238 Assert.False(rec1 == rec2, "rec1 and rec2 are not the same C# object");
239 }
240 { // when
241 GAMSParameter b = db.GetParameter("b");
242 GAMSParameterRecord lastRec = b.LastRecord();
243 foreach (GAMSParameterRecord rec in b)
244 {
245 // then
246 if (rec.Key(0).Equals("topeka"))
247 {
248 Assert.True(rec.Equals(lastRec), "expect LastRec and rec to be equivalent");
249 Assert.False(rec == lastRec, "rec and lastRec are not the same C# object");
250 }
251 }
252
253 String[] slice = new String[] { " ", "chicago" };
254 GAMSParameter d = db.GetParameter("d");
255 // when
256 GAMSParameterRecord lastSlicedRecord = d.LastRecord(slice);
257 GAMSParameterRecord record = d.FindRecord("san-diego", "chicago");
258 Assert.True(lastSlicedRecord.Equals(record), "expect LastRec and rec to be equivalent");
259 Assert.False(lastSlicedRecord == record, "lastSlicedRecord and record are not the same C# object");
260 }
261 { // when
262 GAMSEquation cost = db.GetEquation("cost");
263 GAMSEquationRecord firstRec = cost.FirstRecord();
264 GAMSEquationRecord lastRec = cost.LastRecord();
265 Assert.True(lastRec.Equals(firstRec), "cost has only 1 record, expect LastRec and firstRec to be equivalent");
266 Assert.False(lastRec == firstRec, "lastRec and firstRec are not the same C# object");
267 }
268 { // when
269 GAMSVariable x = db.GetVariable("x");
270 GAMSVariableRecord firstRec = x.FindRecord("seattle", "topeka");
271 String[] slice = new String[] { " ", "topeka" };
272 // when
273 GAMSVariableRecord firstSlicedRec = x.FirstRecord(slice);
274 Assert.True(firstSlicedRec.Equals(firstRec), "expect firstSlicedRec and firstRec to be equivalent");
275 Assert.False(firstSlicedRec == firstRec, "firstSlicedRec and firstRec are not the same C# object");
276 }
277 { // given
278 GAMSVariableRecord r1 = db.GetVariable("x").FindRecord("seattle", "new-york");
279 GAMSVariableRecord r2 = db.GetVariable("x").FindRecord("seattle", "new-york");
280 GAMSVariableRecord r3 = r1;
282 // when, then
283 Assert.True(r1.Equals(r2), "expect r1 and r2 to be equivalent");
284 Assert.False(r1 == r2, "expect r1 and r2 with different memory references");
285 Assert.True(r1.Equals(r3), "expect r1 and r3 to be equivalent");
286 Assert.True(r1 == r3, "expect r1 and r3 with the same memory reference");
287
288 Assert.True(r1.Equals(r4), "expect r1 and r4 to be equivalent");
289 Assert.False(r1 == r4, "expect r1 and r4 with different memory references");
290 }
291 logger.Debug("Exiting TestGAMSSymbolRecord | testEquivalentSymbolRecord");
292 }
293
294 [Test]
295 public void TestEquivalentSymbolRecord_IteratingRecord()
296 {
297 logger.Debug("Entering TestGAMSSymbolRecord | testEquivalentSymbolRecord_IteratingRecord");
298
299 // given
300 String testDir = HouseKeeper.gamsWorkingDir + Path.DirectorySeparatorChar + "testEquivalentSymbol";
301 HouseKeeper.prepare(testDir);
302 GAMSWorkspace ws = new GAMSWorkspace(testDir, HouseKeeper.gamsSystemDir, DebugLevel.Off);
303
304 GAMSJob job = ws.AddJobFromGamsLib("trnsport");
305 job.Run();
306 GAMSDatabase db = job.OutDB;
307
308 { // when
309 GAMSSet j = db.GetSet("j");
310 GAMSSetRecord topeka = j.FindRecord("topeka");
311 GAMSSetRecord ny = j.FindRecord("new-york");
312 GAMSSetRecord chicago = j.FindRecord("chicago");
313
314 GAMSSetRecord record = j.FirstRecord();
315 Assert.True(record.Equals(ny), "expect ny as first record and to be equivalent to record");
316 int count = 0;
317 // then
318 do
319 {
320 if (record.Key(0).Equals("new-york"))
321 {
322 Assert.True(record.Equals(ny), "expect record and ny to be equivalent");
323 }
324 else if (record.Key(0).Equals("chicago"))
325 {
326 Assert.True(record.Equals(chicago), "expect record and chicago to be equivalent");
327 }
328 else if (record.Key(0).Equals("topeka"))
329 {
330 Assert.True(record.Equals(topeka), "expect record and topeka to be equivalent");
331 }
332 else
333 {
334 Assert.Fail("do not expect a record of b with other key than [(new-york),(chicago),(topeka)]");
335 }
336 Assert.AreEqual(1, record.Keys.Length, "expect record with 1 dimensions");
337 count++;
338 } while (record.MoveNext());
339 Assert.AreEqual(3, count, "expect 3 iterating record from j.");
340 }
341 { //when
342 GAMSParameter d = db.GetParameter("d");
343 GAMSParameterRecord seattle_chicago = d.FindRecord("seattle", "chicago");
344 foreach (GAMSParameterRecord record in d)
345 {
346 if ((record.Key(0).Equals("seattle") && record.Key(1).Equals("chicago")))
347 {
348 Assert.True(record.Equals(seattle_chicago), "expect record and seattle_chicago to be equivalent");
349 }
350 else
351 {
352 Assert.False(record.Equals(seattle_chicago), "record and seattle_chicago are not equivalent");
353 }
354 }
355 }
356 { //when
357 GAMSVariable x = db.GetVariable("x");
358 GAMSVariableRecord seattle_topeka = x.FindRecord("seattle", "topeka");
359 foreach (GAMSVariableRecord record in x)
360 {
361 if ((record.Key(0).Equals("seattle") && record.Key(1).Equals("topeka")))
362 {
363 Assert.True(record.Equals(seattle_topeka), "expect record and seattle_chicago to be equivalent");
364 }
365 else
366 {
367 Assert.False(record.Equals(seattle_topeka), "record and seattle_chicago are not equivalent");
368 }
369 }
370 }
371 { // when
372 GAMSEquation supply = db.GetEquation("supply");
373 GAMSEquationRecord sandiego = supply.FindRecord("san-diego");
374 GAMSEquationRecord seattle = supply.FindRecord("seattle");
375 // then
376 foreach (GAMSEquationRecord record in supply)
377 {
378 if (record.Key(0).Equals("san-diego"))
379 {
380 Assert.True(record.Equals(sandiego), "expect record and ny to be equivalent");
381 }
382 else if (record.Key(0).Equals("seattle"))
383 {
384 Assert.True(record.Equals(seattle), "expect record and chicago to be equivalent");
385 }
386 else
387 {
388 Assert.Fail("do not expect a record of b with other key than [(san-diego),(seattle)]");
389 }
390 }
391 }
392
393 logger.Debug("Exiting TestGAMSSymbolRecord | testEquivalentSymbolRecord_IteratingRecord");
394 }
395
396 [Test]
397 public void TestGetKeys()
398 {
399 logger.Debug("Entering TestGAMSSymbolRecord | testGetKeys");
400
401 String testDir = HouseKeeper.gamsWorkingDir + Path.DirectorySeparatorChar + "testGetKeys";
402 HouseKeeper.prepare(testDir);
403 GAMSWorkspace ws = new GAMSWorkspace(testDir, HouseKeeper.gamsSystemDir, DebugLevel.Off);
404
405 GAMSJob job = ws.AddJobFromGamsLib("trnsport");
406 job.Run();
407 GAMSDatabase db = job.OutDB;
408
409 // when, then
410 GAMSSymbolRecord rec_i = db.GetSet("i").LastRecord();
411 Assert.AreEqual(rec_i.Keys.Length, 1, "expect i with 1 dimension");
412 Assert.True(rec_i.Keys[0].Equals("san-diego"), "expect (sandiego) as keys of last record of i");
413
414 GAMSSymbolRecord rec_d = db.GetParameter("d").LastRecord();
415 Assert.AreEqual(rec_d.Keys.Length, 2, "expect d with 2 dimension");
416 Assert.True(rec_d.Keys[0].Equals("san-diego"), "expect (sandiego, topeka) as keys of last record of d");
417 Assert.True(rec_d.Keys[1].Equals("topeka"), "expect (sandiego, topeka) as keys of last record of d");
418
419 GAMSSymbolRecord rec_f = db.GetParameter("f").LastRecord();
420 Assert.AreEqual(rec_f.Keys.Length, 0, "expect scalar f with 0 dimension");
421
422 GAMSSymbolRecord rec_x = db.GetVariable("x").LastRecord();
423 Assert.AreEqual(rec_x.Keys.Length, 2, "expect x with 2 dimension");
424 Assert.True(rec_x.Keys[0].Equals("san-diego"), "expect (sandiego, topeka) as keys of last record of x");
425 Assert.True(rec_x.Keys[1].Equals("topeka"), "expect (sandiego, topeka) as keys of last record of x");
426
427 GAMSSymbolRecord rec_cost = db.GetEquation("cost").LastRecord();
428 Assert.AreEqual(rec_cost.Keys.Length, 0, "expect cost with 0 dimension");
429
430 GAMSSymbolRecord rec_demand = db.GetEquation("demand").LastRecord();
431 Assert.AreEqual(rec_demand.Keys.Length, 1, "expect demand with 1 dimension");
432 Assert.True(rec_demand.Keys[0].Equals("topeka"), "expect (topeka) as keys of last record of d");
433
434 logger.Debug("Exiting TestGAMSSymbolRecord | testGetKeys");
435 }
436
437 [Test]
438 public void TestGetKey_index()
439 {
440 logger.Debug("Entering TestGAMSSymbolRecord | testGetKey_index");
441 String testDir = HouseKeeper.gamsWorkingDir + Path.DirectorySeparatorChar + "testGetKeysIndex";
442 HouseKeeper.prepare(testDir);
443 GAMSWorkspace ws = new GAMSWorkspace(testDir, HouseKeeper.gamsSystemDir, DebugLevel.Off);
444
445 GAMSJob job = ws.AddJobFromGamsLib("trnsport");
446 job.Run();
447 GAMSDatabase db = job.OutDB;
448
449 // when, then
450 GAMSSymbolRecord rec_i = db.GetSet("i").FirstRecord();
451 Assert.True(rec_i.Key(0).Equals("seattle"), "expect (seattle) as keys of last record of i");
452
453 // when, then
454 GAMSSymbolRecord rec_d = db.GetParameter("d").LastRecord();
455 Assert.True(rec_d.Key(0).Equals("san-diego"), "expect (sandiego, topeka) as keys of last record of d");
456 Assert.True(rec_d.Key(1).Equals("topeka"), "expect (sandiego, topeka) as keys of last record of d");
457
458 // when, then
459 GAMSSymbolRecord rec_x = db.GetVariable("x").LastRecord();
460 Assert.True(rec_x.Key(0).Equals("san-diego"), "expect (sandiego, topeka) as keys of last record of x");
461 Assert.True(rec_x.Key(1).Equals("topeka"), "expect (sandiego, topeka) as keys of last record of x");
462
463 // when, then
464 GAMSSymbolRecord rec_demand = db.GetEquation("demand").FirstRecord();
465 Assert.True(rec_demand.Key(0).Equals("new-york"), "expect (new-york) as keys of last record of demand");
466
467 logger.Debug("Exiting TestGAMSSymbolRecord | testGetKey_index");
468 }
469
471 //[Test]
472 //public void TestGetSymbol()
473 //{
474 // logger.Debug("Entering TestGAMSSymbolRecord | testGetKey_index");
475 // String testDir = HouseKeeper.gamsWorkingDir + Path.DirectorySeparatorChar + "testGetKeys";
476 // HouseKeeper.prepare(testDir);
477 // GAMSWorkspace ws = new GAMSWorkspace(testDir, HouseKeeper.gamsSystemDir, DebugLevel.Off);
478
479 // GAMSJob job = ws.AddJobFromGamsLib("trnsport");
480 // job.Run();
481 // GAMSDatabase db = job.OutDB;
482
483 // // when, then
484 // GAMSSymbol i = db.GetSet("i");
485 // GAMSSymbolRecord rec_i1 = i.FirstRecord();
486 // GAMSSymbolRecord rec_i2 = db.GetSet("i").FirstRecord();
487 // Assert.True(rec_i1.getSymbol().Equals(i));
488 // Assert.True(rec_i2.getSymbol().Equals(i));
489
490 // // when, then
491 // GAMSSymbol <?> d = db.GetParameter("d");
492 // GAMSSymbolRecord rec_d1 = d.LastRecord();
493 // GAMSSymbolRecord rec_d2 = db.GetParameter("d").LastRecord();
494 // Assert.True(rec_d1.getSymbol().Equals(d));
495 // Assert.True(rec_d2.getSymbol().Equals(d));
496
497 // // when, then
498 // GAMSSymbol <?> x = db.GetVariable("x");
499 // GAMSSymbolRecord rec_x1 = x.LastRecord();
500 // GAMSSymbolRecord rec_x2 = db.GetVariable("x").LastRecord();
501 // Assert.True(rec_x1.getSymbol().Equals(x));
502 // Assert.True(rec_x2.getSymbol().Equals(x));
503
504 // // when, then
505 // GAMSSymbol <?> demand = db.GetEquation("demand");
506 // GAMSSymbolRecord rec_demand1 = demand.FirstRecord();
507 // GAMSSymbolRecord rec_demand2 = db.GetEquation("demand").FirstRecord();
508 // Assert.True(rec_demand1.getSymbol().Equals(demand));
509 // Assert.True(rec_demand2.getSymbol().Equals(demand));
510
511 // logger.Debug("Exiting TestGAMSSymbolRecord | testGetKey_index");
512 //}
513
514 }
515}
516
GAMSVariable GetVariable(string variableIdentifier)
GAMSParameter GetParameter(string parameterIdentifier)
GAMSSet GetSet(string setIdentifier)
GAMSEquation GetEquation(string equationIdentifier)
new GAMSEquationRecord FindRecord(params string[] keys)
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 LastRecord()
new GAMSSetRecord FirstRecord()
new GAMSSetRecord FindRecord(params string[] keys)
override bool Equals(object obj)
string Key(int index)
GAMSSymbolRecord FirstRecord()
GAMSSymbolRecord FindRecord(params string[] keys)
GAMSSymbolRecord LastRecord()
new GAMSVariableRecord FindRecord(params string[] keys)
new GAMSVariableRecord FirstRecord()
new GAMSVariableRecord LastRecord()
GAMSJob AddJobFromGamsLib(string model, GAMSCheckpoint checkpoint=null, string jobName=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