public static string CalendarTxt(Calendar input) { // First, we want to get the individual service IDs GeneratorDictionary <string, int> services = new GeneratorDictionary <string, int>(new SingleValueGenerator <string, int>(0)); if (input.Monday != null) { services[input.Monday] += 1; } if (input.Tuesday != null) { services[input.Tuesday] += 2; } if (input.Wednesday != null) { services[input.Wednesday] += 4; } if (input.Thursday != null) { services[input.Thursday] += 8; } if (input.Friday != null) { services[input.Friday] += 16; } if (input.Saturday != null) { services[input.Saturday] += 32; } if (input.Sunday != null) { services[input.Sunday] += 64; } // Including those for which only exceptions exist foreach (string service in input.Exceptions.Values) { if (service != null) { services[service] += 0; // This'll leave existing values unchanged but generate 0s for new values } } StringBuilder ret = new StringBuilder(); foreach (string service in services.Keys) { ret.Append(service + "," + input.Start.ToString("yyyyMMdd") + "," + input.End.ToString("yyyyMMdd") + ","); ret.Append(((services[service] & 1) == 1) ? "1," : "0,"); ret.Append(((services[service] & 2) == 2) ? "1," : "0,"); ret.Append(((services[service] & 4) == 4) ? "1," : "0,"); ret.Append(((services[service] & 8) == 8) ? "1," : "0,"); ret.Append(((services[service] & 16) == 16) ? "1," : "0,"); ret.Append(((services[service] & 32) == 32) ? "1," : "0,"); ret.Append(((services[service] & 64) == 64) ? "1\n" : "0\n"); } return(ret.ToString()); }
public void GenerationSpecifiedInConstructorTest() { var d = new GeneratorDictionary <string, string>(k => k.ToLowerInvariant()) { { "test", "unrelated" } }; Assert.AreEqual("unrelated", d["test"]); Assert.AreEqual("testtwo", d["TestTwo"]); }
public void SimpleDictionaryTest() { var d = new GeneratorDictionary <int, string> { { 1, "Hello" }, { 2, "World" } }; Assert.AreEqual(2, d.Count); Assert.AreEqual("World", d[2]); }
public void GenerationTest() { var d = new GeneratorDictionary <string, string>(); d.ItemGeneration += (sender, e) => e.Result = e.Key.ToUpperInvariant(); d.Add("Test", "Some String"); Assert.AreEqual(1, d.Count); Assert.AreEqual("Some String", d["Test"]); Assert.AreEqual("ABC", d["abc"]); Assert.AreEqual("DEF", d["Def"]); Assert.AreEqual(3, d.Count); CollectionAssert.AreEquivalent(new string[] { "Test", "abc", "Def" }, (System.Collections.ICollection)d.Keys); }
public void FailedGenerationTest() { var d = new GeneratorDictionary <string, int>(); d.ItemGeneration += (sender, e) => { bool parsed = int.TryParse(e.Key, System.Globalization.NumberStyles.Integer, System.Globalization.CultureInfo.InvariantCulture, out int n); e.Result = n; e.ResultAvailable = parsed; }; Assert.AreEqual(42, d["42"]); Assert.AreEqual(123, d["123"]); Assert.IsTrue(d.TryGetValue("104", out int test)); Assert.AreEqual(104, test); Assert.IsFalse(d.TryGetValue("asdf", out test)); Assert.IsFalse(d.ContainsKey("asdf")); Assert.ThrowsException <KeyNotFoundException>(() => test = d["not an integer"]); }