public void IgnoresCase() { CaseInsensitiveHashtable st = new CaseInsensitiveHashtable(); st.Add("key", "value"); Assert.AreEqual("value", st["KEY"]); st["KeY"] = "value2"; Assert.AreEqual(1, st.Count); Assert.AreEqual("value2", st["key"]); try { st.Add("KEY", "value2"); Assert.Fail(); } catch (ArgumentException) { } Hashtable ht = new Hashtable(); ht.Add("key", "value"); ht.Add("KEY", "value"); try { st = new CaseInsensitiveHashtable(ht, CultureInfo.InvariantCulture); Assert.Fail(); } catch (ArgumentException) { } }
public void AcceptsNonStringKeys() { CaseInsensitiveHashtable st = new CaseInsensitiveHashtable(); object key = new object(); st.Add(key, "value"); Assert.AreEqual(1, st.Count); Assert.AreEqual("value", st[key]); Assert.IsNull(st[new object()]); }
public void IsSerializable() { CaseInsensitiveHashtable storiginal = new CaseInsensitiveHashtable(); storiginal.Add("key", "value"); CaseInsensitiveHashtable st = (CaseInsensitiveHashtable)SerializeDeserializeObject(storiginal); Assert.AreNotSame(storiginal, st); Assert.AreEqual("value", st["KEY"]); Assert.AreEqual(1, st.Count); }
//template.frm: // frml _DJRD log(qJealw) = -log(dtqjeal) + log(fXal) // + e11*log(pqjeal/pxal/dtqjeal) // + e12*log(pqjoal/pxal/dtqjoal) + k1 $ //frml _DJRD log(qJoalw) = -log(dtqjoal) + log(fXal) // + e12*bshal*log(pqjeal/pxal/dtqjeal) // + e22*log(pqjoal/pxal/dtqjoal) // + gradk2*log(graddag) + k2 $ //frml _DJRD log(qJoalw) = -log(dtqjoal) + log(fXal) // + e12*bshal*log(pqjeal/pxal/dtqjeal) // + (-e12*bshal+e11+e12)*log(pqjoal/pxal/dtqjoal) // + gradk2*log(graddag) + k2 $ //frml _SJRD Dlog(qJeal) = v1*dlog(qJealw) + c1*log(qJealw(-1)/qJeal(-1)) $ //frml _SJRD Dlog(qJoal) = v2*dlog(qJoalw) + c2*log(qJoalw(-1)/qJoal(-1)) // + (1-v2)*gradk2*Dlog(graddag) $ //combine with .out file from tsp. public static void tspUtility(String dataFile, String templateFile, String frmOutputFile) { CaseInsensitiveHashtable ht = new CaseInsensitiveHashtable(); CaseInsensitiveHashtable htConst = new CaseInsensitiveHashtable(); List <string> al = new List <string>(5000); List <string> alType = new List <string>(5000); List <string> al1 = new List <string>(5000); //contains output Program.tokensFromFileToArrayList(dataFile, true, false, al, alType); List <string> aaName = new List <string>(); List <string> aaValue = new List <string>(); bool constFlag = false; bool constFlagVars = true; bool constFlagData = false; bool constFlagNegative = false; bool lsqFlag1 = false; bool lsqFlag2 = false; for (int i = 0; i < al.Count - Globals.extra; i++) { if (i >= 4 && al[i - 4] == "CONSTANTS" && al[i - 3] == ":" && al[i - 2] == "\r\n" && al[i - 1] == "\r\n") { constFlag = true; } if (al[i] == "Log" && al[i + 1] == "likelihood") { lsqFlag1 = true; } if (lsqFlag1 && al[i] == "Parameter" && al[i + 1] == "Estimate" && al[i + 2] == "Error" && al[i + 3] == "t" && al[i + 4] == "-" && al[i + 5] == "statistic" && al[i + 6] == "P" && al[i + 7] == "-" && al[i + 8] == "value") { i += 9; lsqFlag2 = true; } if (constFlag) { //bool VALUEencountered = false; if (constFlagVars) { for (int iii = i; iii < int.MaxValue; iii++) { if (al[iii] == "\r\n") { if (al[iii + 1] != "VALUE") { constFlag = false; } break; } } if (constFlag) { if (alType[i] == "Word") { aaName.Add(al[i]); } else if (al[i] == "\r\n") { constFlagVars = false; constFlagData = true; //i++; //so that consFlagData part starts at the right place continue; //with i } else { G.Writeln("*** ERROR in TSP utility"); } } } if (constFlagData) { if (al[i] == "-" && alType[i + 1] == "Number") { i++; //jump to number constFlagNegative = true; } if (alType[i] == "Word") { if (al[i] == "VALUE") { //ok! //i++; //to jump to first data continue; //with i } else { G.Writeln("*** ERROR in TSP utility"); } } if (alType[i] == "Number") { if (constFlagNegative) { aaValue.Add("-" + al[i]); } else { aaValue.Add(al[i]); } constFlagNegative = false; } else if (al[i] == "\r\n") { constFlagVars = true; constFlagData = false; for (int iii = i; iii < int.MaxValue; iii++) { if (al[iii] != "\r\n") { i = iii - 1; //to jump past line breaks, to first non-line break break; } } } else if (al[i] == "-") { //ok } else { G.Writeln("*** ERROR in TSP utility"); } } } if (lsqFlag2) { if (al[i] == "Standard" && al[i + 1] == "Errors" && al[i + 2] == "computed") { lsqFlag1 = false; lsqFlag2 = false; } else if (alType[i] == "Word") { String par = (string)al[i]; String val = ""; if (al[i + 1] == "-") { val = "-" + al[i + 2]; } else { val = (string)al[i + 1]; } if (ht.ContainsKey(par)) { ht.Remove(par); } ht.Add(par, val); } } } for (int i = 0; i < aaName.Count; i++) { if (htConst.ContainsKey(aaName[i])) { htConst.Remove(aaName[i]); } htConst.Add(aaName[i], aaValue[i]); } al = new List <string>(5000); alType = new List <string>(5000); Program.tokensFromFileToArrayList(templateFile, false, false, al, alType); //---------------------------------------------------- // int allCounter = 0; int constCounter = 0; bool skipOne = false; for (int i = 0; i < al.Count - Globals.extra; i++) { if (G.TspUtilityFindType(al, alType, i, 0) == "Symbol" && G.TspUtilityFindType(al, alType, i, +1) == "Word") { String varName = G.TspUtilityFindToken(al, alType, i, +1); Object value1 = ht[varName]; Object value2 = htConst[varName]; Object value = value1; if (value1 == null && value2 != null) { value = value2; constCounter++; } if (value != null) { allCounter++; //found in hash table, i.e. the value of this varName should be substituted in skipOne = true; //It would be strange to have a variable being a number as the first item in the template, so therefore i>=1 is ok. //Even if so, the code should work fine. if (i >= 1 && ((String)value).StartsWith("-")) { if (G.TspUtilityFindToken(al, alType, i, 0) == "+") { //... + a1 + ... ---> ... -0.1234 + ... al1.Add(G.Add0Ifmissing((String)value)); } else if (G.TspUtilityFindToken(al, alType, i, 0) == "=" || G.TspUtilityFindToken(al, alType, i, 0) == "(") { //... = a1 + ... ---> ... = -0.1234 + ... //... ( a1 + ... ---> ... ( -0.1234 + ... al1.Add(al[i]); al1.Add(G.Add0Ifmissing((String)value)); } else { //... * a1 + ... ---> ... * (-0.1234) + ... al1.Add(al[i]); al1.Add("("); al1.Add(G.Add0Ifmissing((String)value)); al1.Add(")"); } } else { //val is positive //val does not start with "-" al1.Add(al[i]); al1.Add(G.Add0Ifmissing((String)value)); } //i++; } else { //Word is not a variable al1.Add(al[i]); al1.Add(varName); } i = G.TspUtilitiesFindIndex(al, alType, i, +1); //jumps forward to the new i } else { //current pos is not a symbol, and/or next non-whitespace token is not a word al1.Add(al[i]); } } using (FileStream fs = Program.WaitForFileStream(frmOutputFile, Program.GekkoFileReadOrWrite.Write)) using (StreamWriter res = G.GekkoStreamWriter(fs)) { for (int i = 0; i < al1.Count; i++) { res.Write(al1[i]); } res.Flush(); res.Close(); G.Writeln("Inserted " + allCounter + " numbers, hereof " + (allCounter - constCounter) + " parameter values and " + constCounter + " constant values."); G.Writeln("Output is in the file: output.frm."); G.Writeln(); } }