using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using Cosmos.TestRunner; namespace Cosmos.Compiler.Tests.Bcl.System.Collections.Generic { public static class DictionaryTest { public static void Execute() { { var dictionary = new Dictionary { {"a", "a"}, {"b", "b" }, {"c", "c"} }; Assert.IsTrue(dictionary.ContainsKey("a"), "Dictionary ContainsKey does not work1"); Assert.IsFalse(dictionary.ContainsKey("d"), "Dictionary ContainsKey does not work 2"); //String test Assert.IsTrue(dictionary["a"] == "a", "Dictionary [] operator (get) does not work"); dictionary["b"] = "d"; Assert.IsTrue(dictionary["b"] == "d", "Dictionary [] operator (set existing) does not work"); Assert.IsTrue(dictionary.Count == 3, "Dictionary.Count does not work"); dictionary["d"] = "d"; Assert.IsTrue(dictionary["d"] == "d", "Dictionary [] operator (set not existing) does not work"); /* We added another key so now Count should be 4 */ Assert.IsTrue(dictionary.Count == 4, "Dictionary.Count (after new key) does not work"); Dictionary.KeyCollection keyColl = dictionary.Keys; foreach (string key in keyColl) { Assert.IsTrue(key == "a" || key == "b" || key == "c" || key == "d", "Dictionary.Keys returns invalid key"); } dictionary.Add("e", "e"); /* We added another key so now Count should be 5 */ Assert.IsTrue(dictionary.Count == 5, "Dictionary.Count (after Added key) does not work"); /* Now we remove "e" key, the operation should succeed and Count should be 4 again */ Assert.IsTrue(dictionary.Remove("e"), "Dictionary.Remove() of existing key does not work"); Assert.IsTrue(dictionary.Count == 4, "Dictionary.Count (after Removed key) does not work"); /* Now we remove "f" key, the operation should fail as there is not "f" key */ Assert.IsFalse(dictionary.Remove("f"), "Dictionary.Remove() of not existing key does not work"); Assert.IsTrue(dictionary.TryGetValue("a", out string val), "Dictionary.TryGetValue() of existing key does not work"); Assert.IsFalse(dictionary.TryGetValue("f", out string val2), "Dictionary.TryGetValue() of not existing key does not work"); dictionary.Clear(); Assert.IsTrue(dictionary.Count == 0, "Dictionary().Clear does not work"); Assert.IsFalse(dictionary.ContainsKey("a"), "Dictionary().Clear does not work"); dictionary.Add("b", "basds"); Assert.IsTrue(dictionary.Count == 1, "Dictionary().Clear prevents correctly adding values again"); Assert.IsTrue(dictionary["b"] == "basds", "Dictionary().Clear prevents correctly adding values again"); } { var dictionary2 = new Dictionary { { "One", 1 }, { "Two", 2}, { "Three", 3 }, }; Assert.IsTrue(dictionary2.ContainsKey("One"), "Dictionary ContainsKey does not work1"); Assert.IsFalse(dictionary2.ContainsKey("Four"), "Dictionary ContainsKey does not work2"); Assert.IsTrue(dictionary2["One"] == 1, "Dictionary operator [] does not work"); dictionary2["Two"] = 22; Assert.IsTrue(dictionary2["Two"] == 22, "Dictionary [] operator (set existing) does not work"); Assert.IsTrue(dictionary2.Count == 3, "Dictionary.Count does not work"); dictionary2["Four"] = 4; Assert.IsTrue(dictionary2["Four"] == 4, "Dictionary [] operator (set not existing) does not work"); /* We added another key so now Count should be 4 */ Assert.IsTrue(dictionary2.Count == 4, "Dictionary.Count (after new key) does not work"); Dictionary.KeyCollection keyColl2 = dictionary2.Keys; foreach (string key in keyColl2) { Assert.IsTrue(key == "One" || key == "Two" || key == "Three" || key == "Four", "Dictionary.Keys returns invalid key"); } dictionary2.Add("Five", 5); /* We added another key so now Count should be 5 */ Assert.IsTrue(dictionary2.Count == 5, "Dictionary.Count (after Added key) does not work"); /* Now we remove "Five" key, the operation should succeed and Count should be 4 again */ Assert.IsTrue(dictionary2.Remove("Five"), "Dictionary.Remove() of existing key does not work"); Assert.IsTrue(dictionary2.Count == 4, "Dictionary.Count (after Removed key) does not work"); /* Now we remove "Six" key, the operation should fail as there is not "Six" key */ Assert.IsFalse(dictionary2.Remove("Six"), "Dictionary.Remove() of not existing key does not work"); Assert.IsTrue(dictionary2.TryGetValue("One", out int val3), "Dictionary.TryGetValue() of existing key does not work"); Assert.IsFalse(dictionary2.TryGetValue("Six", out int val4), "Dictionary.TryGetValue() of not existing key does not work"); dictionary2.Clear(); Assert.IsTrue(dictionary2.Count == 0, "Dictionary().Clear does not work"); Assert.IsFalse(dictionary2.ContainsKey("Five"), "Dictionary().Clear does not work"); dictionary2.Add("b", 9); Assert.IsTrue(dictionary2.Count == 1, "Dictionary().Clear prevents correctly adding values again"); Assert.IsTrue(dictionary2["b"] == 9, "Dictionary().Clear prevents correctly adding values again"); } //#region "Dictionary Tests" //{ // var dictionary = new Dictionary // { // { 'a', 'a' }, // { 'b', 'b' }, // { 'c', 'c' }, // }; // Assert.IsTrue(dictionary.ContainsKey('a'), "Dictionary ContainsKey does not work1"); // Assert.IsFalse(dictionary.ContainsKey('d'), "Dictionary ContainsKey does not work2"); // Assert.IsTrue(dictionary['a'] == 'a', "Dictionary operator [] does not work"); // dictionary['b'] = 'v'; // Assert.IsTrue(dictionary['b'] == 'v', "Dictionary [] operator (set existing) does not work"); // Assert.IsTrue(dictionary.Count == 4, "Dictionary.Count does not work"); // dictionary['d'] = 'd'; // Assert.IsTrue(dictionary['d'] == 'd', "Dictionary [] operator (set not existing) does not work"); // /* We added another key so now Count should be 4 */ // Assert.IsTrue(dictionary.Count == 4, "Dictionary.Count (after new key) does not work"); // Dictionary.KeyCollection keyColl = dictionary.Keys; // foreach (var key in keyColl) // { // Assert.IsTrue(key == 'a' || key == 'b' || key == 'c' || key == 'd', "Dictionary.Keys returns invalid key"); // } // dictionary.Add('e', 'e'); // /* We added another key so now Count should be 5 */ // Assert.IsTrue(dictionary.Count == 5, "Dictionary.Count (after Added key) does not work"); // /* Now we remove "5" key, the operation should succeed and Count should be 4 again */ // Assert.IsTrue(dictionary.Remove('e'), "Dictionary.Remove() of existing key does not work"); // Assert.IsTrue(dictionary.Count == 4, "Dictionary.Count (after Removed key) does not work"); // /* Now we remove "6" key, the operation should fail as there is not "6" key */ // Assert.IsFalse(dictionary.Remove('f'), "Dictionary.Remove() of not existing key does not work"); // Assert.IsTrue(dictionary.TryGetValue('a', out char val1), "Dictionary.TryGetValue() of existing key does not work"); // Assert.IsFalse(dictionary.TryGetValue('f', out char val2), "Dictionary.TryGetValue() of not existing key does not work"); //} //#endregion #region "Dictionary Tests" { var dictionary = new Dictionary { { 1, 1 }, { 2, 2 }, { 3, 3 }, }; Assert.IsTrue(dictionary.ContainsKey(1), "Dictionary ContainsKey does not work1"); Assert.IsFalse(dictionary.ContainsKey(4), "Dictionary ContainsKey does not work2"); Assert.IsTrue(dictionary[1] == 1, "Dictionary operator [] does not work"); dictionary[2] = 22; Assert.IsTrue(dictionary[2] == 22, "Dictionary [] operator (set existing) does not work"); Assert.IsTrue(dictionary.Count == 3, "Dictionary.Count does not work"); dictionary[4] = 4; Assert.IsTrue(dictionary[4] == 4, "Dictionary [] operator (set not existing) does not work"); /* We added another key so now Count should be 4 */ Assert.IsTrue(dictionary.Count == 4, "Dictionary.Count (after new key) does not work"); Dictionary.KeyCollection keyColl = dictionary.Keys; foreach (var key in keyColl) { Assert.IsTrue(key == 1 || key == 2 || key == 3 || key == 4, "Dictionary.Keys returns invalid key"); } dictionary.Add(5, 5); /* We added another key so now Count should be 5 */ Assert.IsTrue(dictionary.Count == 5, "Dictionary.Count (after Added key) does not work"); /* Now we remove "5" key, the operation should succeed and Count should be 4 again */ Assert.IsTrue(dictionary.Remove(5), "Dictionary.Remove() of existing key does not work"); Assert.IsTrue(dictionary.Count == 4, "Dictionary.Count (after Removed key) does not work"); /* Now we remove "6" key, the operation should fail as there is not "6" key */ Assert.IsFalse(dictionary.Remove(6), "Dictionary.Remove() of not existing key does not work"); Assert.IsTrue(dictionary.TryGetValue(1, out sbyte val1), "Dictionary.TryGetValue() of existing key does not work"); Assert.IsFalse(dictionary.TryGetValue(6, out sbyte val2), "Dictionary.TryGetValue() of not existing key does not work"); dictionary.Clear(); Assert.IsTrue(dictionary.Count == 0, "Dictionary().Clear does not work"); } #endregion #region "Dictionary Tests" { var dictionary = new Dictionary { { 1, 1 }, { 2, 2 }, { 3, 3 }, }; Assert.IsTrue(dictionary.ContainsKey(1), "Dictionary ContainsKey does not work1"); Assert.IsFalse(dictionary.ContainsKey(4), "Dictionary ContainsKey does not work2"); Assert.IsTrue(dictionary[1] == 1, "Dictionary operator [] does not work"); dictionary[2] = 22; Assert.IsTrue(dictionary[2] == 22, "Dictionary [] operator (set existing) does not work"); Assert.IsTrue(dictionary.Count == 3, "Dictionary.Count does not work"); dictionary[4] = 4; Assert.IsTrue(dictionary[4] == 4, "Dictionary [] operator (set not existing) does not work"); /* We added another key so now Count should be 4 */ Assert.IsTrue(dictionary.Count == 4, "Dictionary.Count (after new key) does not work"); Dictionary.KeyCollection keyColl = dictionary.Keys; foreach (var key in keyColl) { Assert.IsTrue(key == 1 || key == 2 || key == 3 || key == 4, "Dictionary.Keys returns invalid key"); } dictionary.Add(5, 5); /* We added another key so now Count should be 5 */ Assert.IsTrue(dictionary.Count == 5, "Dictionary.Count (after Added key) does not work"); /* Now we remove "5" key, the operation should succeed and Count should be 4 again */ Assert.IsTrue(dictionary.Remove(5), "Dictionary.Remove() of existing key does not work"); Assert.IsTrue(dictionary.Count == 4, "Dictionary.Count (after Removed key) does not work"); /* Now we remove "6" key, the operation should fail as there is not "6" key */ Assert.IsFalse(dictionary.Remove(6), "Dictionary.Remove() of not existing key does not work"); Assert.IsTrue(dictionary.TryGetValue(1, out byte val1), "Dictionary.TryGetValue() of existing key does not work"); Assert.IsFalse(dictionary.TryGetValue(6, out byte val2), "Dictionary.TryGetValue() of not existing key does not work"); dictionary.Clear(); Assert.IsTrue(dictionary.Count == 0, "Dictionary().Clear does not work"); } #endregion #region "Dictionary Tests" { var dictionary = new Dictionary { { 1, 1 }, { 2, 2 }, { 3, 3 }, }; Assert.IsTrue(dictionary.ContainsKey(1), "Dictionary ContainsKey does not work1"); Assert.IsFalse(dictionary.ContainsKey(4), "Dictionary ContainsKey does not work2"); Assert.IsTrue(dictionary[1] == 1, "Dictionary operator [] does not work"); dictionary[2] = 22; Assert.IsTrue(dictionary[2] == 22, "Dictionary [] operator (set existing) does not work"); Assert.IsTrue(dictionary.Count == 3, "Dictionary.Count does not work"); dictionary[4] = 4; Assert.IsTrue(dictionary[4] == 4, "Dictionary [] operator (set not existing) does not work"); /* We added another key so now Count should be 4 */ Assert.IsTrue(dictionary.Count == 4, "Dictionary.Count (after new key) does not work"); Dictionary.KeyCollection keyColl = dictionary.Keys; foreach (var key in keyColl) { Assert.IsTrue(key == 1 || key == 2 || key == 3 || key == 4, "Dictionary.Keys returns invalid key"); } dictionary.Add(5, 5); /* We added another key so now Count should be 5 */ Assert.IsTrue(dictionary.Count == 5, "Dictionary.Count (after Added key) does not work"); /* Now we remove "5" key, the operation should succeed and Count should be 4 again */ Assert.IsTrue(dictionary.Remove(5), "Dictionary.Remove() of existing key does not work"); Assert.IsTrue(dictionary.Count == 4, "Dictionary.Count (after Removed key) does not work"); /* Now we remove "6" key, the operation should fail as there is not "6" key */ Assert.IsFalse(dictionary.Remove(6), "Dictionary.Remove() of not existing key does not work"); Assert.IsTrue(dictionary.TryGetValue(1, out short val1), "Dictionary.TryGetValue() of existing key does not work"); Assert.IsFalse(dictionary.TryGetValue(6, out short val2), "Dictionary.TryGetValue() of not existing key does not work"); dictionary.Clear(); Assert.IsTrue(dictionary.Count == 0, "Dictionary().Clear does not work"); } #endregion #region "Dictionary Tests" { var dictionary = new Dictionary { { 1, 1 }, { 2, 2 }, { 3, 3 }, }; Assert.IsTrue(dictionary.ContainsKey(1), "Dictionary ContainsKey does not work1"); Assert.IsFalse(dictionary.ContainsKey(4), "Dictionary ContainsKey does not work2"); Assert.IsTrue(dictionary[1] == 1, "Dictionary operator [] does not work"); dictionary[2] = 22; Assert.IsTrue(dictionary[2] == 22, "Dictionary [] operator (set existing) does not work"); Assert.IsTrue(dictionary.Count == 3, "Dictionary.Count does not work"); dictionary[4] = 4; Assert.IsTrue(dictionary[4] == 4, "Dictionary [] operator (set not existing) does not work"); /* We added another key so now Count should be 4 */ Assert.IsTrue(dictionary.Count == 4, "Dictionary.Count (after new key) does not work"); Dictionary.KeyCollection keyColl = dictionary.Keys; foreach (var key in keyColl) { Assert.IsTrue(key == 1 || key == 2 || key == 3 || key == 4, "Dictionary.Keys returns invalid key"); } dictionary.Add(5, 5); /* We added another key so now Count should be 5 */ Assert.IsTrue(dictionary.Count == 5, "Dictionary.Count (after Added key) does not work"); /* Now we remove "5" key, the operation should succeed and Count should be 4 again */ Assert.IsTrue(dictionary.Remove(5), "Dictionary.Remove() of existing key does not work"); Assert.IsTrue(dictionary.Count == 4, "Dictionary.Count (after Removed key) does not work"); /* Now we remove "6" key, the operation should fail as there is not "6" key */ Assert.IsFalse(dictionary.Remove(6), "Dictionary.Remove() of not existing key does not work"); Assert.IsTrue(dictionary.TryGetValue(1, out ushort val1), "Dictionary.TryGetValue() of existing key does not work"); Assert.IsFalse(dictionary.TryGetValue(6, out ushort val2), "Dictionary.TryGetValue() of not existing key does not work"); dictionary.Clear(); Assert.IsTrue(dictionary.Count == 0, "Dictionary().Clear does not work"); } #endregion #region "Dictionary Tests" { var dictionary = new Dictionary { { 1, 1 }, { 2, 2 }, { 3, 3 }, }; Assert.IsTrue(dictionary.ContainsKey(1), "Dictionary ContainsKey does not work1"); Assert.IsFalse(dictionary.ContainsKey(4), "Dictionary ContainsKey does not work2"); Assert.IsTrue(dictionary[1] == 1, "Dictionary operator [] does not work"); dictionary[2] = 22; Assert.IsTrue(dictionary[2] == 22, "Dictionary [] operator (set existing) does not work"); Assert.IsTrue(dictionary.Count == 3, "Dictionary.Count does not work"); dictionary[4] = 4; Assert.IsTrue(dictionary[4] == 4, "Dictionary [] operator (set not existing) does not work"); /* We added another key so now Count should be 4 */ Assert.IsTrue(dictionary.Count == 4, "Dictionary.Count (after new key) does not work"); Dictionary.KeyCollection keyColl = dictionary.Keys; foreach (var key in keyColl) { Assert.IsTrue(key == 1 || key == 2 || key == 3 || key == 4, "Dictionary.Keys returns invalid key"); } dictionary.Add(5, 5); /* We added another key so now Count should be 5 */ Assert.IsTrue(dictionary.Count == 5, "Dictionary.Count (after Added key) does not work"); /* Now we remove "5" key, the operation should succeed and Count should be 4 again */ Assert.IsTrue(dictionary.Remove(5), "Dictionary.Remove() of existing key does not work"); Assert.IsTrue(dictionary.Count == 4, "Dictionary.Count (after Removed key) does not work"); /* Now we remove "6" key, the operation should fail as there is not "6" key */ Assert.IsFalse(dictionary.Remove(6), "Dictionary.Remove() of not existing key does not work"); Assert.IsTrue(dictionary.TryGetValue(1, out int val1), "Dictionary.TryGetValue() of existing key does not work"); Assert.IsFalse(dictionary.TryGetValue(6, out int val2), "Dictionary.TryGetValue() of not existing key does not work"); dictionary.Clear(); Assert.IsTrue(dictionary.Count == 0, "Dictionary().Clear does not work"); } #endregion #region "Dictionary Tests" { var dictionary = new Dictionary { { 1, 1 }, { 2, 2 }, { 3, 3 }, }; Assert.IsTrue(dictionary.ContainsKey(1), "Dictionary ContainsKey does not work1"); Assert.IsFalse(dictionary.ContainsKey(4), "Dictionary ContainsKey does not work2"); Assert.IsTrue(dictionary[1] == 1, "Dictionary operator [] does not work"); dictionary[2] = 22; Assert.IsTrue(dictionary[2] == 22, "Dictionary [] operator (set existing) does not work"); Assert.IsTrue(dictionary.Count == 3, "Dictionary.Count does not work"); dictionary[4] = 4; Assert.IsTrue(dictionary[4] == 4, "Dictionary [] operator (set not existing) does not work"); /* We added another key so now Count should be 4 */ Assert.IsTrue(dictionary.Count == 4, "Dictionary.Count (after new key) does not work"); Dictionary.KeyCollection keyColl = dictionary.Keys; foreach (var key in keyColl) { Assert.IsTrue(key == 1 || key == 2 || key == 3 || key == 4, "Dictionary.Keys returns invalid key"); } dictionary.Add(5, 5); /* We added another key so now Count should be 5 */ Assert.IsTrue(dictionary.Count == 5, "Dictionary.Count (after Added key) does not work"); /* Now we remove "5" key, the operation should succeed and Count should be 4 again */ Assert.IsTrue(dictionary.Remove(5), "Dictionary.Remove() of existing key does not work"); Assert.IsTrue(dictionary.Count == 4, "Dictionary.Count (after Removed key) does not work"); /* Now we remove "6" key, the operation should fail as there is not "6" key */ Assert.IsFalse(dictionary.Remove(6), "Dictionary.Remove() of not existing key does not work"); Assert.IsTrue(dictionary.TryGetValue(1, out uint val1), "Dictionary.TryGetValue() of existing key does not work"); Assert.IsFalse(dictionary.TryGetValue(6, out uint val2), "Dictionary.TryGetValue() of not existing key does not work"); dictionary.Clear(); Assert.IsTrue(dictionary.Count == 0, "Dictionary().Clear does not work"); } #endregion #region "Dictionary Tests" { var dictionary = new Dictionary { { 1, 1 }, { 2, 2 }, { 3, 3 }, }; Assert.IsTrue(dictionary.ContainsKey(1), "Dictionary ContainsKey does not work1"); Assert.IsFalse(dictionary.ContainsKey(4), "Dictionary ContainsKey does not work2"); Assert.IsTrue(dictionary[1] == 1, "Dictionary operator [] does not work"); dictionary[2] = 22; Assert.IsTrue(dictionary[2] == 22, "Dictionary [] operator (set existing) does not work"); Assert.IsTrue(dictionary.Count == 3, "Dictionary.Count does not work"); dictionary[4] = 4; Assert.IsTrue(dictionary[4] == 4, "Dictionary [] operator (set not existing) does not work"); /* We added another key so now Count should be 4 */ Assert.IsTrue(dictionary.Count == 4, "Dictionary.Count (after new key) does not work"); Dictionary.KeyCollection keyColl = dictionary.Keys; foreach (var key in keyColl) { Assert.IsTrue(key == 1 || key == 2 || key == 3 || key == 4, "Dictionary.Keys returns invalid key"); } dictionary.Add(5, 5); /* We added another key so now Count should be 5 */ Assert.IsTrue(dictionary.Count == 5, "Dictionary.Count (after Added key) does not work"); /* Now we remove "5" key, the operation should succeed and Count should be 4 again */ Assert.IsTrue(dictionary.Remove(5), "Dictionary.Remove() of existing key does not work"); Assert.IsTrue(dictionary.Count == 4, "Dictionary.Count (after Removed key) does not work"); /* Now we remove "6" key, the operation should fail as there is not "6" key */ Assert.IsFalse(dictionary.Remove(6), "Dictionary.Remove() of not existing key does not work"); Assert.IsTrue(dictionary.TryGetValue(1, out long val1), "Dictionary.TryGetValue() of existing key does not work"); Assert.IsFalse(dictionary.TryGetValue(6, out long val2), "Dictionary.TryGetValue() of not existing key does not work"); dictionary.Clear(); Assert.IsTrue(dictionary.Count == 0, "Dictionary().Clear does not work"); } #endregion #region "Dictionary Tests" { var dictionary = new Dictionary { { 1, 1 }, { 2, 2 }, { 3, 3 }, }; Assert.IsTrue(dictionary.ContainsKey(1), "Dictionary ContainsKey does not work1"); Assert.IsFalse(dictionary.ContainsKey(4), "Dictionary ContainsKey does not work2"); Assert.IsTrue(dictionary[1] == 1, "Dictionary operator [] does not work"); dictionary[2] = 22; Assert.IsTrue(dictionary[2] == 22, "Dictionary [] operator (set existing) does not work"); Assert.IsTrue(dictionary.Count == 3, "Dictionary.Count does not work"); dictionary[4] = 4; Assert.IsTrue(dictionary[4] == 4, "Dictionary [] operator (set not existing) does not work"); /* We added another key so now Count should be 4 */ Assert.IsTrue(dictionary.Count == 4, "Dictionary.Count (after new key) does not work"); Dictionary.KeyCollection keyColl = dictionary.Keys; foreach (var key in keyColl) { Assert.IsTrue(key == 1 || key == 2 || key == 3 || key == 4, "Dictionary.Keys returns invalid key"); } dictionary.Add(5, 5); /* We added another key so now Count should be 5 */ Assert.IsTrue(dictionary.Count == 5, "Dictionary.Count (after Added key) does not work"); /* Now we remove "5" key, the operation should succeed and Count should be 4 again */ Assert.IsTrue(dictionary.Remove(5), "Dictionary.Remove() of existing key does not work"); Assert.IsTrue(dictionary.Count == 4, "Dictionary.Count (after Removed key) does not work"); /* Now we remove "6" key, the operation should fail as there is not "6" key */ Assert.IsFalse(dictionary.Remove(6), "Dictionary.Remove() of not existing key does not work"); Assert.IsTrue(dictionary.TryGetValue(1, out ulong val1), "Dictionary.TryGetValue() of existing key does not work"); Assert.IsFalse(dictionary.TryGetValue(6, out ulong val2), "Dictionary.TryGetValue() of not existing key does not work"); dictionary.Clear(); Assert.IsTrue(dictionary.Count == 0, "Dictionary().Clear does not work"); } #endregion //TODO: Add GUID test once newGUID returns something other than a zero initialized guid. } } }