Changeset 5832
 Timestamp:
 May 31, 2012 9:08:36 PM (7 years ago)
 File:

 1 edited
Legend:
 Unmodified
 Added
 Removed

coopr.pyomo/trunk/coopr/pyomo/tests/unit/test_param.py
r5828 r5832 995 995 # Test that display actually displays the correct param value 996 996 def test_mutable_display(self): 997 tmp_stream = pyutilib.services.TempfileManager.create_tempfile(suffix = '.param_display.test') 997 998 model = ConcreteModel() 998 999 model.Q = Param(initialize=0.0) 999 1000 1000 self.assertEqual(model.Q, 0.0) 1001 model.Q = 1.01002 self.assertEqual(model.Q,1.0)1003 tmp_stream = pyutilib.services.TempfileManager.create_tempfile(suffix = '.param_display.test')1004 1001 f = open(tmp_stream,'w') 1005 1002 display(model.Q,f) … … 1008 1005 tmp = f.readlines() 1009 1006 f.close() 1010 print tmp 1007 val = float(tmp[1].strip()) 1008 self.assertEquals(model.Q, val) 1009 1010 model.Q = 1.0 1011 self.assertEqual(model.Q,1.0) 1012 f = open(tmp_stream,'w') 1013 display(model.Q,f) 1014 f.close() 1015 f = open(tmp_stream,'r') 1016 tmp = f.readlines() 1017 f.close() 1018 val = float(tmp[1].strip()) 1019 self.assertEquals(model.Q, val) 1020 1021 # Test that pprint actually displays the correct param value 1022 def test_mutable_pprint(self): 1023 tmp_stream = pyutilib.services.TempfileManager.create_tempfile(suffix = '.param_display.test') 1024 model = ConcreteModel() 1025 model.Q = Param(initialize=0.0) 1026 self.assertEqual(model.Q, 0.0) 1027 f = open(tmp_stream,'w') 1028 model.Q.pprint(ostream=f) 1029 f.close() 1030 f = open(tmp_stream,'r') 1031 tmp = f.readlines() 1032 f.close() 1033 val = float(tmp[1].strip()) 1034 self.assertEquals(model.Q, val) 1035 1036 model.Q = 1.0 1037 self.assertEqual(model.Q,1.0) 1038 f = open(tmp_stream,'w') 1039 model.Q.pprint(ostream=f) 1040 f.close() 1041 f = open(tmp_stream,'r') 1042 tmp = f.readlines() 1043 f.close() 1011 1044 val = float(tmp[1].strip()) 1012 1045 self.assertEquals(model.Q, val) … … 1148 1181 self.assertEqual(model.Q[1],0.0) 1149 1182 self.assertEqual(model.Q[2],1.0) 1183 1184 # Test that display actually displays the correct param value 1185 def test_mutable_display(self): 1186 tmp_stream = pyutilib.services.TempfileManager.create_tempfile(suffix = '.param_display.test') 1187 model = ConcreteModel() 1188 model.P = Param([1,2],default=0.0) 1189 model.Q = Param([1,2],initialize=0.0) 1190 model.R = Param([1,2]) 1191 model.R[1] = 0.0 1192 model.R[2] = 0.0 1193 # check initial values are correct 1194 #**** NOTE: Accessing the 1195 # value of indexed params which utilize 1196 # the default keyword actually causes the internal 1197 # rep to become dense for that index, which 1198 # changes display output 1199 for Item in [model.P, model.Q, model.R]: 1200 for i in [1,2]: 1201 self.assertEquals(Item[i],0.0) 1202 1203 # check that the correct value is printed 1204 # Treat the param using default a little differently 1205 for Item in [model.P]: 1206 f = open(tmp_stream,'w') 1207 display(Item,f) 1208 f.close() 1209 f = open(tmp_stream,'r') 1210 tmp = f.readlines() 1211 f.close() 1212 val = float(tmp[3].split('default:')[1]) 1213 for i in [1,2]: 1214 self.assertEquals(Item[i], val) 1215 for Item in [model.Q, model.R]: 1216 f = open(tmp_stream,'w') 1217 display(Item,f) 1218 f.close() 1219 f = open(tmp_stream,'r') 1220 tmp = f.readlines() 1221 f.close() 1222 for i in [1,2]: 1223 val = float(tmp[i].strip().split(':')[1]) 1224 self.assertEquals(Item[i], val) 1225 1226 model.P[1] = 1.0 1227 model.P[2] = 2.0 1228 model.Q[1] = 1.0 1229 model.Q[2] = 2.0 1230 model.R[1] = 1.0 1231 model.R[2] = 2.0 1232 1233 # check that the correct value is printed 1234 for Item in [model.P,model.Q, model.R]: 1235 f = open(tmp_stream,'w') 1236 display(Item,f) 1237 f.close() 1238 f = open(tmp_stream,'r') 1239 tmp = f.readlines() 1240 f.close() 1241 for i in [1,2]: 1242 val = float(tmp[i].strip().split(':')[1]) 1243 self.assertEquals(Item[i], val) 1244 1245 # Test that pprint actually displays the correct param value 1246 def test_mutable_pprint(self): 1247 tmp_stream = pyutilib.services.TempfileManager.create_tempfile(suffix = '.param_display.test') 1248 model = ConcreteModel() 1249 model.P = Param([1,2],default=0.0) 1250 model.Q = Param([1,2],initialize=0.0) 1251 model.R = Param([1,2]) 1252 model.R[1] = 0.0 1253 model.R[2] = 0.0 1254 # check initial values are correct 1255 #**** NOTE: Accessing the 1256 # value of indexed params which utilize 1257 # the default keyword actually causes the internal 1258 # rep to become dense for that index, which 1259 # changes pprint output 1260 for Item in [model.P, model.Q, model.R]: 1261 for i in [1,2]: 1262 self.assertEquals(Item[i],0.0) 1263 1264 # check that the correct value is printed 1265 # Treat the param using default a little differently 1266 for Item in [model.P]: 1267 f = open(tmp_stream,'w') 1268 Item.pprint(ostream=f) 1269 f.close() 1270 f = open(tmp_stream,'r') 1271 tmp = f.readlines() 1272 f.close() 1273 val = float(tmp[3].split('default:')[1]) 1274 for i in [1,2]: 1275 self.assertEquals(Item[i], val) 1276 for Item in [model.Q, model.R]: 1277 f = open(tmp_stream,'w') 1278 Item.pprint(ostream=f) 1279 f.close() 1280 f = open(tmp_stream,'r') 1281 tmp = f.readlines() 1282 f.close() 1283 for i in [1,2]: 1284 val = float(tmp[i].strip().split(':')[1]) 1285 self.assertEquals(Item[i], val) 1286 1287 model.P[1] = 1.0 1288 model.P[2] = 2.0 1289 model.Q[1] = 1.0 1290 model.Q[2] = 2.0 1291 model.R[1] = 1.0 1292 model.R[2] = 2.0 1293 1294 # check that the correct value is printed 1295 for Item in [model.P,model.Q, model.R]: 1296 f = open(tmp_stream,'w') 1297 Item.pprint(ostream=f) 1298 f.close() 1299 f = open(tmp_stream,'r') 1300 tmp = f.readlines() 1301 f.close() 1302 for i in [1,2]: 1303 val = float(tmp[i].strip().split(':')[1]) 1304 self.assertEquals(Item[i], val) 1150 1305 1151 1306 # Test mutability of indexed
Note: See TracChangeset
for help on using the changeset viewer.