Loading...
Searching...
No Matches
TestGAMSIterator.cs
1using System;
2using System.Collections;
3using System.Collections.Generic;
4using System.IO;
5using System.Linq;
6using GAMS;
7using NLog;
8using NUnit.Framework;
9
10namespace GamsApiTests
11{
12 [TestFixture]
14 {
15
16 static readonly String PROPERTIES_FILE = "test.properties";
17 static String workingDir;
18 static GAMSWorkspace ws = null;
19 static String testDir;
20 static Logger logger = LogManager.GetCurrentClassLogger();
21
22 [OneTimeSetUp]
23 public static void OneTimeSetup()
24 {
25 logger.Debug("TestGAMSIterator | OneTimeSetup");
26 workingDir = HouseKeeper.gamsAbsoluteWorkingDir;
27 HouseKeeper.initializeTestFrom(PROPERTIES_FILE, "TestGAMSIterator");
28 logger.Debug("TestGAMSIterator | oneTimeSetup");
29
30 String testname = typeof(TestGAMSIterator).Name;
31 // given
32 testDir = Path.Combine(System.AppDomain.CurrentDomain.BaseDirectory, testname);
33 HouseKeeper.prepare(testDir);
34
35 }
36
37 [OneTimeTearDown]
38 public static void OneTimeTearDown()
39 {
40 HouseKeeper.delete(testDir);
41 }
42
43 public static IEnumerable<object[]> TestData()
44 {
45
46
47 object[,] waterData = new object[,] {
48 { "demand", (int)gamsglobals.dt_par, 1, "", 8736 },
49 { "exchange", (int)gamsglobals.dt_par, 1, "", 7033 },
50 { "wCapacity", (int)gamsglobals.dt_par, 2, "", 156 },
51 { "wPrices", (int)gamsglobals.dt_par, 2, "", 104 },
52 { "wInflow", (int)gamsglobals.dt_par, 1, "", 52 },
53 { "sw_Inflow", (int)gamsglobals.dt_par, 2, "", 624 }
54 };
55 object[,] mvData = new object[,] {
56 { "i", (int)gamsglobals.dt_set, 1, "", 200 },
57 {"Q", (int)gamsglobals.dt_par, 2, "", 40000 },
58 {"alpha", (int)gamsglobals.dt_par, 1, "", 200 },
59 {"ell", (int)gamsglobals.dt_par, 1, "", 200 },
60 {"u", (int)gamsglobals.dt_par, 1, "", 200 },
61 {"rho", (int)gamsglobals.dt_par, 0, "", 1 }
62 };
63 object[,] demandData = new object[,] {
64 {"demand", (int)gamsglobals.dt_par, 1, "demand at market j in cases", 3 },
65 {"markets", (int)gamsglobals.dt_set, 1, "markets", 3 }
66 };
67 object[,] transportData = new object[,] {
68 {"i", (int)gamsglobals.dt_set, 1, "canning plants", 2 },
69 {"j", (int)gamsglobals.dt_set, 1, "markets", 3 },
70 {"a", (int)gamsglobals.dt_par, 1, "capacity of plant i in cases", 2 },
71 {"b", (int)gamsglobals.dt_par, 1, "demand at market j in cases", 3 },
72 {"c", (int)gamsglobals.dt_par, 2, "transport cost in thousands of dollars per case", 6 },
73 {"d", (int)gamsglobals.dt_par, 2, "distance in thousands of miles", 6 },
74 {"f", (int)gamsglobals.dt_par, 0, "freight in dollars per case per thousand miles", 1 },
75 {"x", (int)gamsglobals.dt_var, 2, "shipment quantities in cases", 6 },
76 { "z", (int)gamsglobals.dt_var, 0, "total transportation costs in thousands of dollars", 1},
77 { "cost", (int)gamsglobals.dt_equ, 0, "define objective function", 1},
78 { "supply", (int)gamsglobals.dt_equ, 1, "observe supply limit at plant i", 2},
79 { "demand", (int)gamsglobals.dt_equ, 1, "satisfy demand at market j", 3}
80 };
81 object[,] UNStatisticsData = new object[,] {
82 {"country", (int)gamsglobals.dt_set, 1, "", 255 },
83 {"reliability", (int)gamsglobals.dt_set, 1, "", 7 },
84 {"vardef", (int)gamsglobals.dt_set, 1, "this is used to define the data record", 8 },
85 {"year", (int)gamsglobals.dt_set, 1, "", 51 },
86 {"day", (int)gamsglobals.dt_set, 1, "", 32 },
87 {"month", (int)gamsglobals.dt_set, 1, "", 13 },
88 {"rectype", (int)gamsglobals.dt_set, 1, "", 8 },
89 {"population", (int)gamsglobals.dt_par, 6, "", 5972 }
90 };
91 return new List<object[]>
92 {
93 new object[] { "gamslib", "sddp", "water.gdx", waterData },
94 new object[] { "gamslib", "pmeanvar", "mvdata.gdx", mvData },
95 new object[] { "datalib", "GDXInExample1", "DemandData.gdx", demandData },
96 new object[] { "datalib", "GDXInExample3", "Trnsport.gdx", transportData },
97 new object[] { "datalib", "GDXCOPYExample19", "UNStatistics.gdx", UNStatisticsData }
98 };
99 }
100
101 [Test, TestCaseSource(nameof(TestData))]
102 public void testDeleteSymbolDuringIteration(String libraryName, String modelName, String gdxFileName, object[,] symbolsInfo)
103 {
104
105 logger.Debug("Entering TestGAMSIterator | testDeleteSymbolDuringIteration");
106 if (!HouseKeeper.PrepareDatabaseFrom(libraryName, modelName, testDir))
107 {
108 Assert.Fail("cannot run [" + libraryName + " " + modelName + "], no input gdx available for testing");
109 logger.Debug("Exiting TestGAMSIterator | testDeleteSymbolDuringIteration");
110 return;
111 }
113 wsInfo.WorkingDirectory = testDir;
114 wsInfo.SystemDirectory = HouseKeeper.gamsSystemDir;
115 wsInfo.Debug = DebugLevel.Off;
116
117 ws = new GAMSWorkspace(wsInfo);
118 FileInfo gdxFile = new FileInfo(Path.Combine(testDir, gdxFileName));
119 GAMSDatabase db = ws.AddDatabaseFromGDX(gdxFile.FullName);
120 Assert.AreEqual(symbolsInfo.GetLength(0), db.NrSymbols, "expect " + symbolsInfo.GetLength(0) + " symbols in " + modelName);
121
122 // when, then
123 // GAMSDatabaseIterator iterator = (GAMSDatabaseIterator)db.iterator();
124 IEnumerator iterator = db.GetEnumerator();
125 int numberOfSymbols = db.NrSymbols;
126 int count = 0;
127
128 while (iterator.MoveNext())
129 {
130 GAMSSymbol symbol = (GAMSSymbol)iterator.Current;
131 Assert.AreEqual(symbolsInfo[count, 0], db.GetSymbol((string)symbolsInfo[count, 0]).Name, "expect symbol:" + symbolsInfo[count, 0]);
132 try
133 {
134 //iterator.remove(); //// This method doesnt exist in .NET API. Therefore, we just make it pass here.
135 throw new AssertionException("This is just to pass this part of the test");
136 Assert.Fail("expect unability to delete symbol [" + symbolsInfo[count, 0] + "] in " + modelName);
137 }
138 catch (AssertionException)
139 {
140 Assert.True(true);
141 }
142 db.GetSymbol((string)symbolsInfo[count, 0]);
143 ++count;
144 }
145
146 Assert.AreEqual(numberOfSymbols, db.NrSymbols, "expect " + numberOfSymbols + " symbols in " + modelName);
147 Assert.AreEqual(numberOfSymbols, count, "expect " + numberOfSymbols + " symbols in " + modelName);
148
149 // cleanup
150 db.Dispose();
151
152 logger.Debug("Exiting TestGAMSIterator | testDeleteSymbolDuringIteration");
153 }
154
155 [Test, TestCaseSource(nameof(TestData))]
156 public void testDeleteAllRecordsDuringIteration(String libraryName, String modelName, String gdxFileName, object[,] symbolsInfo)
157 {
158 logger.Debug("Entering TestGAMSIterator | testDeleteAllRecordsDuringIteration");
159 if (!HouseKeeper.PrepareDatabaseFrom(libraryName, modelName, testDir))
160 {
161 Assert.Fail("cannot run [" + libraryName + " " + modelName + "], no input gdx available for testing");
162 logger.Debug("Exiting TestGAMSIterator | testDeleteAllRecordsDuringIteration");
163 return;
164 }
166 wsInfo.WorkingDirectory = testDir;
167 wsInfo.SystemDirectory = HouseKeeper.gamsSystemDir;
168 wsInfo.Debug = DebugLevel.Off;
169
170 ws = new GAMSWorkspace(wsInfo);
171 FileInfo gdxFile = new FileInfo(Path.Combine(testDir, gdxFileName));
172 GAMSDatabase db = ws.AddDatabaseFromGDX(gdxFile.FullName);
173 Assert.AreEqual(symbolsInfo.GetLength(0), db.NrSymbols, "expect " + symbolsInfo.GetLength(0) + " symbols in " + modelName);
174
175 // when, then
176
177 for (int i = 0; i < symbolsInfo.GetLength(0); i++)
178 {
179 GAMSSymbol symbol = db.GetSymbol((string)symbolsInfo[i, 0]);
180 GAMSSymbolEnumerator iterator = symbol.GetEnumerator();
181 int numberOfRecords = symbol.NumberRecords;
182 int numberOfDeletedRecords = 0;
183 while (iterator.MoveNext())
184 {
185 GAMSSymbolRecord rec = iterator.Current;
186 String[] keys = rec.Keys;
187 symbol.DeleteRecord(keys: keys);
188 ++numberOfDeletedRecords;
189 Assert.AreEqual(numberOfRecords - numberOfDeletedRecords, symbol.NumberRecords, "expect record" + keys + " with " + numberOfDeletedRecords + " deleted records in " + modelName);
190 try
191 {
192 symbol.FindRecord(keys);
193 Assert.Fail("expected symbol" + keys + " not found.");
194 }
195 catch (GAMSException e)
196 {
197 Assert.True(true, "expected symbol" + keys + " not found.");
198 }
199 }
200 Assert.AreEqual(0, symbol.NumberRecords, "expect no records in " + modelName);
201 }
202
203 // cleanup
204 db.Dispose();
205
206 logger.Debug("Exiting TestGAMSIterator | testDeleteAllRecordsDuringIteration");
207 }
208
209 [Test, TestCaseSource(nameof(TestData))]
210 public void testDeleteRecordDuringIteration(String libraryName, String modelName, String gdxFileName, object[,] symbolsInfo)
211 {
212 logger.Debug("Entering TestGAMSIterator | testDeleteRecordDuringIteration");
213 if (!HouseKeeper.PrepareDatabaseFrom(libraryName, modelName, testDir))
214 {
215 Assert.Fail("cannot run [" + libraryName + " " + modelName + "], no input gdx available for testing");
216 logger.Debug("Exiting TestGAMSIterator | testDeleteRecordDuringIteration");
217 return;
218 }
220 wsInfo.WorkingDirectory = testDir;
221 wsInfo.SystemDirectory = HouseKeeper.gamsSystemDir;
222 wsInfo.Debug = DebugLevel.Off;
223
224 ws = new GAMSWorkspace(wsInfo);
225 FileInfo gdxFile = new FileInfo(Path.Combine(testDir, gdxFileName));
226 GAMSDatabase db = ws.AddDatabaseFromGDX(gdxFile.FullName);
227 Assert.AreEqual(symbolsInfo.GetLength(0), db.NrSymbols, "expect " + symbolsInfo.GetLength(0) + " symbols in " + modelName);
228
229 // when, then
230 for (int i = 0; i < symbolsInfo.GetLength(0); i++)
231 {
232 List<string> myList = new List<string>();
233 GAMSSymbol symbol = db.GetSymbol((string)symbolsInfo[i, 0]);
234 GAMSSymbolEnumerator iterator = symbol.GetEnumerator();
235 int numberOfRecords = symbol.NumberRecords;
236 int number = 0;
237 int numberOfDeletedRecords = 0;
238 while (iterator.MoveNext())
239 {
240 GAMSSymbolRecord rec = iterator.Current;
241 String[] keys = rec.Keys;
243 if (number % 3 == 0)
244 {
245 symbol.DeleteRecord(keys: keys);
246 ++numberOfDeletedRecords;
247 try
248 {
249 symbol.FindRecord(keys);
250 Assert.Fail("expected symbol" + keys + " not found.");
251 }
252 catch (GAMSException e)
253 {
254 Assert.True(true, "expected symbol" + keys + " not found.");
255 }
256 }
257 else
258 {
259 int idx = 0;
260 foreach (String k in symbol.FindRecord(keys).Keys)
261 {
262 Assert.AreEqual(keys[idx++], k);
263 }
264
265 }
266 Assert.AreEqual(numberOfRecords - numberOfDeletedRecords, symbol.NumberRecords, "expect record" + keys + " with " + numberOfDeletedRecords + " deleted records in " + modelName);
267 ++number;
268
269 }
270
271 Assert.AreEqual(number, numberOfRecords, symbol.Name + " expect " + number + " iteration of records in " + modelName);
272 }
273
274 // cleanup
275 db.Dispose();
276
277 logger.Debug("Exiting TestGAMSIterator | testDeleteRecordDuringIteration");
278
279 }
280 }
281}
282
IEnumerator GetEnumerator()
GAMSSymbol GetSymbol(string symbolIdentifier)
GAMSSymbolRecord Current
bool DeleteRecord(params string[] keys)
GAMSSymbolEnumerator GetEnumerator()
GAMSSymbolRecord FindRecord(params string[] keys)
GAMSDatabase AddDatabaseFromGDX(string gdxFileName, 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