2using System.Collections;
3using System.Collections.Generic;
16 static readonly String PROPERTIES_FILE =
"test.properties";
17 static String workingDir;
19 static String testDir;
20 static Logger logger = LogManager.GetCurrentClassLogger();
23 public static void OneTimeSetup()
25 logger.Debug(
"TestGAMSIterator | OneTimeSetup");
28 logger.Debug(
"TestGAMSIterator | oneTimeSetup");
32 testDir = Path.Combine(
System.AppDomain.CurrentDomain.BaseDirectory, testname);
38 public static void OneTimeTearDown()
43 public static IEnumerable<object[]> TestData()
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 }
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 }
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 }
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}
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 }
91 return new List<object[]>
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 }
101 [Test, TestCaseSource(nameof(TestData))]
102 public void testDeleteSymbolDuringIteration(String libraryName, String modelName, String gdxFileName,
object[,] symbolsInfo)
105 logger.Debug(
"Entering TestGAMSIterator | testDeleteSymbolDuringIteration");
106 if (!
HouseKeeper.PrepareDatabaseFrom(libraryName, modelName, testDir))
108 Assert.Fail(
"cannot run [" + libraryName +
" " + modelName +
"], no input gdx available for testing");
109 logger.Debug(
"Exiting TestGAMSIterator | testDeleteSymbolDuringIteration");
113 wsInfo.WorkingDirectory = testDir;
114 wsInfo.SystemDirectory =
HouseKeeper.gamsSystemDir;
118 FileInfo gdxFile =
new FileInfo(Path.Combine(testDir, gdxFileName));
120 Assert.AreEqual(symbolsInfo.GetLength(0), db.
NrSymbols,
"expect " + symbolsInfo.GetLength(0) +
" symbols in " + modelName);
128 while (iterator.MoveNext())
131 Assert.AreEqual(symbolsInfo[count, 0], db.
GetSymbol((
string)symbolsInfo[count, 0]).
Name,
"expect symbol:" + symbolsInfo[count, 0]);
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);
138 catch (AssertionException)
142 db.
GetSymbol((
string)symbolsInfo[count, 0]);
146 Assert.AreEqual(numberOfSymbols, db.
NrSymbols,
"expect " + numberOfSymbols +
" symbols in " + modelName);
147 Assert.AreEqual(numberOfSymbols, count,
"expect " + numberOfSymbols +
" symbols in " + modelName);
152 logger.Debug(
"Exiting TestGAMSIterator | testDeleteSymbolDuringIteration");
155 [Test, TestCaseSource(nameof(TestData))]
156 public void testDeleteAllRecordsDuringIteration(String libraryName, String modelName, String gdxFileName,
object[,] symbolsInfo)
158 logger.Debug(
"Entering TestGAMSIterator | testDeleteAllRecordsDuringIteration");
159 if (!
HouseKeeper.PrepareDatabaseFrom(libraryName, modelName, testDir))
161 Assert.Fail(
"cannot run [" + libraryName +
" " + modelName +
"], no input gdx available for testing");
162 logger.Debug(
"Exiting TestGAMSIterator | testDeleteAllRecordsDuringIteration");
166 wsInfo.WorkingDirectory = testDir;
167 wsInfo.SystemDirectory =
HouseKeeper.gamsSystemDir;
171 FileInfo gdxFile =
new FileInfo(Path.Combine(testDir, gdxFileName));
173 Assert.AreEqual(symbolsInfo.GetLength(0), db.
NrSymbols,
"expect " + symbolsInfo.GetLength(0) +
" symbols in " + modelName);
177 for (
int i = 0; i < symbolsInfo.GetLength(0); i++)
182 int numberOfDeletedRecords = 0;
186 String[] keys = rec.
Keys;
188 ++numberOfDeletedRecords;
189 Assert.AreEqual(numberOfRecords - numberOfDeletedRecords, symbol.
NumberRecords,
"expect record" + keys +
" with " + numberOfDeletedRecords +
" deleted records in " + modelName);
193 Assert.Fail(
"expected symbol" + keys +
" not found.");
197 Assert.True(
true,
"expected symbol" + keys +
" not found.");
200 Assert.AreEqual(0, symbol.
NumberRecords,
"expect no records in " + modelName);
206 logger.Debug(
"Exiting TestGAMSIterator | testDeleteAllRecordsDuringIteration");
209 [Test, TestCaseSource(nameof(TestData))]
210 public void testDeleteRecordDuringIteration(String libraryName, String modelName, String gdxFileName,
object[,] symbolsInfo)
212 logger.Debug(
"Entering TestGAMSIterator | testDeleteRecordDuringIteration");
213 if (!
HouseKeeper.PrepareDatabaseFrom(libraryName, modelName, testDir))
215 Assert.Fail(
"cannot run [" + libraryName +
" " + modelName +
"], no input gdx available for testing");
216 logger.Debug(
"Exiting TestGAMSIterator | testDeleteRecordDuringIteration");
220 wsInfo.WorkingDirectory = testDir;
221 wsInfo.SystemDirectory =
HouseKeeper.gamsSystemDir;
225 FileInfo gdxFile =
new FileInfo(Path.Combine(testDir, gdxFileName));
227 Assert.AreEqual(symbolsInfo.GetLength(0), db.
NrSymbols,
"expect " + symbolsInfo.GetLength(0) +
" symbols in " + modelName);
230 for (
int i = 0; i < symbolsInfo.GetLength(0); i++)
232 List<string> myList =
new List<string>();
237 int numberOfDeletedRecords = 0;
241 String[] keys = rec.
Keys;
246 ++numberOfDeletedRecords;
250 Assert.Fail(
"expected symbol" + keys +
" not found.");
254 Assert.True(
true,
"expected symbol" + keys +
" not found.");
262 Assert.AreEqual(keys[idx++], k);
266 Assert.AreEqual(numberOfRecords - numberOfDeletedRecords, symbol.
NumberRecords,
"expect record" + keys +
" with " + numberOfDeletedRecords +
" deleted records in " + modelName);
271 Assert.AreEqual(number, numberOfRecords, symbol.
Name +
" expect " + number +
" iteration of records in " + modelName);
277 logger.Debug(
"Exiting TestGAMSIterator | testDeleteRecordDuringIteration");
IEnumerator GetEnumerator()
GAMSSymbol GetSymbol(string symbolIdentifier)
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,...
static void initializeTestFrom(String filename, String subdir)
initialize class properties from fileName and prepare directory subdir